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
NetParser::u16
uint16_t u16()
Parse a 16-bit integer in network byte order from the data stream.
Definition: parser.cc:64
ParseResult::NoError
@ NoError
Success.
NetUnparser::u8
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
NetParser::NetParser
NetParser(Buffer buffer)
Definition: parser.hh:38
NetParser::remove_prefix
void remove_prefix(const size_t n)
Remove n bytes from the buffer.
Definition: parser.cc:45
std::string
NetParser::_check_size
void _check_size(const size_t size)
Check that there is sufficient data to parse the next token.
Definition: parser.cc:20
NetParser::buffer
Buffer buffer() const
Definition: parser.hh:40
NetParser::error
bool error() const
Returns true if there has been an error.
Definition: parser.hh:50
ParseResult::BadChecksum
@ BadChecksum
Bad checksum.
NetUnparser::u16
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
NetParser::_parse_int
T _parse_int()
Generic integer parsing method (used by u32, u16, u8)
Definition: parser.cc:27
Buffer
A reference-counted read-only string that can discard bytes from the front.
Definition: buffer.hh:14
NetUnparser::_unparse_int
static void _unparse_int(std::string &s, T val)
Definition: parser.cc:54
ParseResult::Unsupported
@ Unsupported
Packet uses unsupported features.
NetUnparser
Definition: parser.hh:65
ParseResult
ParseResult
The result of parsing or unparsing an IP datagram, TCP segment, Ethernet frame, or ARP message.
Definition: parser.hh:12
NetParser::set_error
void set_error(ParseResult res)
Set BaseParser::_error.
Definition: parser.hh:47
ParseResult::TruncatedPacket
@ TruncatedPacket
Packet length is shorter than header claims.
NetParser
Definition: parser.hh:25
NetUnparser::u32
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
buffer.hh
NetParser::u8
uint8_t u8()
Parse an 8-bit integer in network byte order from the data stream.
Definition: parser.cc:66
ParseResult::WrongIPVersion
@ WrongIPVersion
Got a version of IP other than 4.
ParseResult::HeaderTooShort
@ HeaderTooShort
Header length is shorter than minimum required.
as_string
std::string as_string(const ParseResult r)
Output a string representation of a ParseResult.
Definition: parser.cc:7
ParseResult::PacketTooShort
@ PacketTooShort
Not enough data to finish parsing.
NetParser::get_error
ParseResult get_error() const
Get the current value stored in BaseParser::_error.
Definition: parser.hh:43
NetParser::_error
ParseResult _error
Result of parsing so far.
Definition: parser.hh:28
NetParser::_buffer
Buffer _buffer
Definition: parser.hh:27
NetParser::u32
uint32_t u32()
Parse a 32-bit integer in network byte order from the data stream.
Definition: parser.cc:62