Sponge
CS144's user-space TCP library
parser.cc
Go to the documentation of this file.
1 #include "parser.hh"
2 
3 using namespace std;
4 
7 string as_string(const ParseResult r) {
8  static constexpr const char *_names[] = {
9  "NoError",
10  "BadChecksum",
11  "PacketTooShort",
12  "WrongIPVersion",
13  "HeaderTooShort",
14  "TruncatedPacket",
15  };
16 
17  return _names[static_cast<size_t>(r)];
18 }
19 
20 void NetParser::_check_size(const size_t size) {
21  if (size > _buffer.size()) {
22  set_error(ParseResult::PacketTooShort);
23  }
24 }
25 
26 template <typename T>
28  constexpr size_t len = sizeof(T);
29  _check_size(len);
30  if (error()) {
31  return 0;
32  }
33 
34  T ret = 0;
35  for (size_t i = 0; i < len; i++) {
36  ret <<= 8;
37  ret += uint8_t(_buffer.at(i));
38  }
39 
40  _buffer.remove_prefix(len);
41 
42  return ret;
43 }
44 
45 void NetParser::remove_prefix(const size_t n) {
46  _check_size(n);
47  if (error()) {
48  return;
49  }
50  _buffer.remove_prefix(n);
51 }
52 
53 template <typename T>
54 void NetUnparser::_unparse_int(string &s, T val) {
55  constexpr size_t len = sizeof(T);
56  for (size_t i = 0; i < len; ++i) {
57  const uint8_t the_byte = (val >> ((len - i - 1) * 8)) & 0xff;
58  s.push_back(the_byte);
59  }
60 }
61 
62 uint32_t NetParser::u32() { return _parse_int<uint32_t>(); }
63 
64 uint16_t NetParser::u16() { return _parse_int<uint16_t>(); }
65 
66 uint8_t NetParser::u8() { return _parse_int<uint8_t>(); }
67 
68 void NetUnparser::u32(string &s, const uint32_t val) { return _unparse_int<uint32_t>(s, val); }
69 
70 void NetUnparser::u16(string &s, const uint16_t val) { return _unparse_int<uint16_t>(s, val); }
71 
72 void NetUnparser::u8(string &s, const uint8_t val) { return _unparse_int<uint8_t>(s, val); }
NetParser::u16
uint16_t u16()
Parse a 16-bit integer in network byte order from the data stream.
Definition: parser.cc:64
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::remove_prefix
void remove_prefix(const size_t n)
Remove n bytes from the buffer.
Definition: parser.cc:45
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
len
constexpr size_t len
Definition: tcp_benchmark.cc:12
std::string::push_back
T push_back(T... args)
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
NetUnparser::_unparse_int
static void _unparse_int(std::string &s, T val)
Definition: parser.cc:54
ParseResult
ParseResult
The result of parsing or unparsing an IP datagram, TCP segment, Ethernet frame, or ARP message.
Definition: parser.hh:12
std::uint8_t
as_string
string as_string(const ParseResult r)
Output a string representation of a ParseResult.
Definition: parser.cc:7
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
NetParser::u8
uint8_t u8()
Parse an 8-bit integer in network byte order from the data stream.
Definition: parser.cc:66
std
ParseResult::PacketTooShort
@ PacketTooShort
Not enough data to finish parsing.
NetParser::u32
uint32_t u32()
Parse a 32-bit integer in network byte order from the data stream.
Definition: parser.cc:62
parser.hh