Sponge
CS144's user-space TCP library
ipv4_datagram.cc
Go to the documentation of this file.
1 #include "ipv4_datagram.hh"
2 
3 #include "parser.hh"
4 #include "util.hh"
5 
6 #include <stdexcept>
7 #include <string>
8 
9 using namespace std;
10 
12  NetParser p{buffer};
13  _header.parse(p);
14  _payload = p.buffer();
15 
16  if (_payload.size() != _header.payload_length()) {
18  }
19 
20  return p.get_error();
21 }
22 
24  if (_payload.size() != _header.payload_length()) {
25  throw runtime_error("IPv4Datagram::serialize: payload is wrong size");
26  }
27 
28  IPv4Header header_out = _header;
29  header_out.cksum = 0;
30  const string header_zero_checksum = header_out.serialize();
31 
32  // calculate checksum -- taken over header only
33  InternetChecksum check;
34  check.add(header_zero_checksum);
35  header_out.cksum = check.value();
36 
37  BufferList ret;
38  ret.append(header_out.serialize());
39  ret.append(_payload);
40  return ret;
41 }
util.hh
InternetChecksum::add
void add(std::string_view data)
Definition: util.cc:89
BufferList::append
void append(const BufferList &other)
Append a BufferList.
Definition: buffer.cc:15
InternetChecksum::value
uint16_t value() const
Definition: util.cc:100
BufferList
A reference-counted discontiguous string that can discard bytes from the front.
Definition: buffer.hh:56
buffer
std::string buffer
Definition: parser_example.cc:7
ipv4_datagram.hh
IPv4Header::cksum
uint16_t cksum
checksum field
Definition: ipv4_header.hh:44
Buffer
A reference-counted read-only string that can discard bytes from the front.
Definition: buffer.hh:14
ParseResult
ParseResult
The result of parsing or unparsing an IP datagram, TCP segment, Ethernet frame, or ARP message.
Definition: parser.hh:12
std::runtime_error
IPv4Datagram::parse
ParseResult parse(const Buffer buffer)
Parse the segment from a string.
Definition: ipv4_datagram.cc:11
IPv4Header::serialize
std::string serialize() const
Serialize the IP fields.
Definition: ipv4_header.cc:78
NetParser
Definition: parser.hh:25
IPv4Header
IPv4 Internet datagram header
Definition: ipv4_header.hh:8
std
InternetChecksum
The internet checksum algorithm.
Definition: util.hh:55
ParseResult::PacketTooShort
@ PacketTooShort
Not enough data to finish parsing.
IPv4Datagram::serialize
BufferList serialize() const
Serialize the segment to a string.
Definition: ipv4_datagram.cc:23
parser.hh