Sponge
CS144's user-space TCP library
parser.hh
Go to the documentation of this file.
1 #ifndef SPONGE_LIBSPONGE_PARSER_HH
2 #define SPONGE_LIBSPONGE_PARSER_HH
3 
4 #include "buffer.hh"
5 
6 #include <cstdint>
7 #include <cstdlib>
8 #include <string>
9 #include <utility>
10 
12 enum class ParseResult {
13  NoError = 0,
14  BadChecksum,
20 };
21 
24 
25 class NetParser {
26  private:
29 
31  void _check_size(const size_t size);
32 
34  template <typename T>
35  T _parse_int();
36 
37  public:
39 
40  Buffer buffer() const { return _buffer; }
41 
43  ParseResult get_error() const { return _error; }
44 
47  void set_error(ParseResult res) { _error = res; }
48 
50  bool error() const { return get_error() != ParseResult::NoError; }
51 
53  uint32_t u32();
54 
56  uint16_t u16();
57 
59  uint8_t u8();
60 
62  void remove_prefix(const size_t n);
63 };
64 
65 struct NetUnparser {
66  template <typename T>
67  static void _unparse_int(std::string &s, T val);
68 
70  static void u32(std::string &s, const uint32_t val);
71 
73  static void u16(std::string &s, const uint16_t val);
74 
76  static void u8(std::string &s, const uint8_t val);
77 };
78 
79 #endif // SPONGE_LIBSPONGE_PARSER_HH
A reference-counted read-only string that can discard bytes from the front.
Definition: buffer.hh:15
uint32_t u32()
Parse a 32-bit integer in network byte order from the data stream.
Definition: parser.cc:62
uint16_t u16()
Parse a 16-bit integer in network byte order from the data stream.
Definition: parser.cc:64
Buffer _buffer
Definition: parser.hh:27
ParseResult get_error() const
Get the current value stored in BaseParser::_error.
Definition: parser.hh:43
void set_error(ParseResult res)
Set BaseParser::_error.
Definition: parser.hh:47
ParseResult _error
Result of parsing so far.
Definition: parser.hh:28
T _parse_int()
Generic integer parsing method (used by u32, u16, u8)
Definition: parser.cc:27
NetParser(Buffer buffer)
Definition: parser.hh:38
void remove_prefix(const size_t n)
Remove n bytes from the buffer.
Definition: parser.cc:45
bool error() const
Returns true if there has been an error.
Definition: parser.hh:50
Buffer buffer() const
Definition: parser.hh:40
void _check_size(const size_t size)
Check that there is sufficient data to parse the next token.
Definition: parser.cc:20
uint8_t u8()
Parse an 8-bit integer in network byte order from the data stream.
Definition: parser.cc:66
std::string as_string(const ParseResult r)
Output a string representation of a ParseResult.
Definition: parser.cc:7
ParseResult
The result of parsing or unparsing an IP datagram, TCP segment, Ethernet frame, or ARP message.
Definition: parser.hh:12
@ PacketTooShort
Not enough data to finish parsing.
@ BadChecksum
Bad checksum.
@ TruncatedPacket
Packet length is shorter than header claims.
@ NoError
Success.
@ WrongIPVersion
Got a version of IP other than 4.
@ Unsupported
Packet uses unsupported features.
@ HeaderTooShort
Header length is shorter than minimum required.
static void u32(std::string &s, const uint32_t val)
Write a 32-bit integer into the data stream in network byte order.
Definition: parser.cc:68
static void _unparse_int(std::string &s, T val)
Definition: parser.cc:54
static void u16(std::string &s, const uint16_t val)
Write a 16-bit integer into the data stream in network byte order.
Definition: parser.cc:70
static void u8(std::string &s, const uint8_t val)
Write an 8-bit integer into the data stream in network byte order.
Definition: parser.cc:72