Sponge
CS144's user-space TCP library
tcp_segment.cc
Go to the documentation of this file.
1 #include "tcp_segment.hh"
2 
3 #include "parser.hh"
4 #include "util.hh"
5 
6 #include <variant>
7 
8 using namespace std;
9 
12 ParseResult TCPSegment::parse(const Buffer buffer, const uint32_t datagram_layer_checksum) {
13  InternetChecksum check(datagram_layer_checksum);
14  check.add(buffer);
15  if (check.value()) {
17  }
18 
19  NetParser p{buffer};
20  _header.parse(p);
21  _payload = p.buffer();
22  return p.get_error();
23 }
24 
26  return payload().str().size() + (header().syn ? 1 : 0) + (header().fin ? 1 : 0);
27 }
28 
30 BufferList TCPSegment::serialize(const uint32_t datagram_layer_checksum) const {
31  TCPHeader header_out = _header;
32  header_out.cksum = 0;
33 
34  // calculate checksum -- taken over entire segment
35  InternetChecksum check(datagram_layer_checksum);
36  check.add(header_out.serialize());
37  check.add(_payload);
38  header_out.cksum = check.value();
39 
40  BufferList ret;
41  ret.append(header_out.serialize());
42  ret.append(_payload);
43 
44  return ret;
45 }
util.hh
tcp_segment.hh
InternetChecksum::add
void add(std::string_view data)
Definition: util.cc:89
TCPSegment::serialize
BufferList serialize(const uint32_t datagram_layer_checksum=0) const
Serialize the segment to a string.
Definition: tcp_segment.cc:30
BufferList::append
void append(const BufferList &other)
Append a BufferList.
Definition: buffer.cc:15
InternetChecksum::value
uint16_t value() const
Definition: util.cc:100
TCPHeader::serialize
std::string serialize() const
Serialize the TCP fields.
Definition: tcp_header.cc:51
BufferList
A reference-counted discontiguous string that can discard bytes from the front.
Definition: buffer.hh:56
ParseResult::BadChecksum
@ BadChecksum
Bad checksum.
buffer
std::string buffer
Definition: parser_example.cc:7
Buffer
A reference-counted read-only string that can discard bytes from the front.
Definition: buffer.hh:14
TCPSegment::parse
ParseResult parse(const Buffer buffer, const uint32_t datagram_layer_checksum=0)
Parse the segment from a string.
Definition: tcp_segment.cc:12
ParseResult
ParseResult
The result of parsing or unparsing an IP datagram, TCP segment, Ethernet frame, or ARP message.
Definition: parser.hh:12
TCPHeader::cksum
uint16_t cksum
checksum
Definition: tcp_header.hh:49
std::uint32_t
NetParser
Definition: parser.hh:25
std
InternetChecksum
The internet checksum algorithm.
Definition: util.hh:55
TCPHeader
TCP segment header
Definition: tcp_header.hh:9
parser.hh
TCPSegment::length_in_sequence_space
size_t length_in_sequence_space() const
Segment's length in sequence space.
Definition: tcp_segment.cc:25