|
Sponge
CS144's user-space TCP library
|
Go to the documentation of this file.
32 ver = first_byte >> 4;
33 hlen = first_byte & 0x0f;
39 df =
static_cast<bool>(fo_val & 0x4000);
40 mf =
static_cast<bool>(fo_val & 0x2000);
41 offset = fo_val & 0x1fff;
49 if (data_size < 4 * hlen) {
58 if (data_size !=
len) {
69 check.
add({original_serialized_version.
str().data(),
size_t(4 * hlen)});
90 const uint8_t first_byte = (ver << 4) | (hlen & 0xf);
96 const uint16_t fo_val = (df ? 0x4000 : 0) | (mf ? 0x2000 : 0) | (offset & 0x1fff);
126 uint32_t pcksum = (src >> 16) + (src & 0xffff);
127 pcksum += (dst >> 16) + (dst & 0xffff);
129 pcksum += payload_length();
136 ss <<
hex <<
boolalpha <<
"IP version: " << +ver <<
'\n'
137 <<
"IP hdr len: " << +hlen <<
'\n'
138 <<
"IP tos: " << +tos <<
'\n'
139 <<
"IP dgram len: " << +
len <<
'\n'
140 <<
"IP id: " << +
id <<
'\n'
141 <<
"Flags: df: " << df <<
" mf: " << mf <<
'\n'
142 <<
"Offset: " << +offset <<
'\n'
143 <<
"TTL: " << +ttl <<
'\n'
144 <<
"Protocol: " << +proto <<
'\n'
145 <<
"Checksum: " << +cksum <<
'\n'
146 <<
"Src addr: " << +src <<
'\n'
147 <<
"Dst addr: " << +dst <<
'\n';
154 <<
"len=" << +
len <<
", "
155 <<
"protocol=" << +proto <<
", " << (ttl >= 10 ?
"" :
"ttl=" +
::to_string(ttl) +
", ")
156 <<
"src=" << inet_ntoa({htobe32(src)}) <<
", "
157 <<
"dst=" << inet_ntoa({htobe32(dst)});
uint16_t u16()
Parse a 16-bit integer in network byte order from the data stream.
static void u8(std::string &s, const uint8_t val)
Write an 8-bit integer into the data stream in network byte order.
void remove_prefix(const size_t n)
Remove n bytes from the buffer.
void add(std::string_view data)
bool error() const
Returns true if there has been an error.
@ BadChecksum
Bad checksum.
static void u16(std::string &s, const uint16_t val)
Write a 16-bit integer into the data stream in network byte order.
A reference-counted read-only string that can discard bytes from the front.
ParseResult
The result of parsing or unparsing an IP datagram, TCP segment, Ethernet frame, or ARP message.
@ TruncatedPacket
Packet length is shorter than header claims.
static void u32(std::string &s, const uint32_t val)
Write a 32-bit integer into the data stream in network byte order.
uint8_t u8()
Parse an 8-bit integer in network byte order from the data stream.
@ WrongIPVersion
Got a version of IP other than 4.
@ HeaderTooShort
Header length is shorter than minimum required.
The internet checksum algorithm.
@ PacketTooShort
Not enough data to finish parsing.
ParseResult get_error() const
Get the current value stored in BaseParser::_error.
size_t size() const
Size of the string.
std::string_view str() const
uint32_t u32()
Parse a 32-bit integer in network byte order from the data stream.