Sponge
CS144's user-space TCP library
Public Member Functions | Static Public Attributes | List of all members
TCPHeader Struct Reference

TCP segment header More...

#include <tcp_header.hh>

Public Member Functions

bool operator== (const TCPHeader &other) const
 
ParseResult parse (NetParser &p)
 Parse the TCP fields from the provided NetParser. More...
 
std::string serialize () const
 Serialize the TCP fields. More...
 
std::string summary () const
 Return a string containing a human-readable summary of the header. More...
 
std::string to_string () const
 Return a string containing a header in human-readable format. More...
 

Public Attributes

TCP Header fields
uint16_t sport = 0
 source port More...
 
uint16_t dport = 0
 destination port More...
 
WrappingInt32 seqno {0}
 sequence number More...
 
WrappingInt32 ackno {0}
 ack number More...
 
uint8_t doff = LENGTH / 4
 data offset More...
 
bool urg = false
 urgent flag More...
 
bool ack = false
 ack flag More...
 
bool psh = false
 push flag More...
 
bool rst = false
 rst flag More...
 
bool syn = false
 syn flag More...
 
bool fin = false
 fin flag More...
 
uint16_t win = 0
 window size More...
 
uint16_t cksum = 0
 checksum More...
 
uint16_t uptr = 0
 

Static Public Attributes

static constexpr size_t LENGTH = 20
 TCP header length, not including options More...
 

Detailed Description

TCP segment header

Note
TCP options are not supported
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Port | Destination Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Acknowledgment Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Data | |U|A|P|R|S|F| |
| Offset| Reserved |R|C|S|S|Y|I| Window |
| | |G|K|H|T|N|N| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Checksum | Urgent Pointer |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| data |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Definition at line 9 of file tcp_header.hh.

Member Function Documentation

◆ operator==()

bool TCPHeader::operator== ( const TCPHeader other) const

Definition at line 103 of file tcp_header.cc.

◆ parse()

ParseResult TCPHeader::parse ( NetParser p)

Parse the TCP fields from the provided NetParser.

Parameters
[in,out]pis a NetParser from which the TCP fields will be extracted
Returns
a ParseResult indicating success or the reason for failure

It is important to check for (at least) the following potential errors (but note that NetParser inherently checks for certain errors; use that fact to your advantage!):

  • data stream inside the NetParser is too short to contain a header
  • the header's doff field is shorter than the minimum allowed
  • there is less data in the header than the doff field claims
  • the checksum is bad

Definition at line 17 of file tcp_header.cc.

◆ serialize()

string TCPHeader::serialize ( ) const

Serialize the TCP fields.

Serialize the TCPHeader to a string (does not recompute the checksum)

Definition at line 51 of file tcp_header.cc.

◆ summary()

string TCPHeader::summary ( ) const

Return a string containing a human-readable summary of the header.

Definition at line 96 of file tcp_header.cc.

◆ to_string()

string TCPHeader::to_string ( ) const

Return a string containing a header in human-readable format.

Returns
A string with the header's contents

Definition at line 81 of file tcp_header.cc.

Member Data Documentation

◆ ack

bool TCPHeader::ack = false

ack flag

Definition at line 43 of file tcp_header.hh.

◆ ackno

WrappingInt32 TCPHeader::ackno {0}

ack number

Definition at line 40 of file tcp_header.hh.

◆ cksum

uint16_t TCPHeader::cksum = 0

checksum

Definition at line 49 of file tcp_header.hh.

◆ doff

uint8_t TCPHeader::doff = LENGTH / 4

data offset

Definition at line 41 of file tcp_header.hh.

◆ dport

uint16_t TCPHeader::dport = 0

destination port

Definition at line 38 of file tcp_header.hh.

◆ fin

bool TCPHeader::fin = false

fin flag

Definition at line 47 of file tcp_header.hh.

◆ LENGTH

constexpr size_t TCPHeader::LENGTH = 20
staticconstexpr

TCP header length, not including options

Definition at line 10 of file tcp_header.hh.

◆ psh

bool TCPHeader::psh = false

push flag

Definition at line 44 of file tcp_header.hh.

◆ rst

bool TCPHeader::rst = false

rst flag

Definition at line 45 of file tcp_header.hh.

◆ seqno

WrappingInt32 TCPHeader::seqno {0}

sequence number

Definition at line 39 of file tcp_header.hh.

◆ sport

uint16_t TCPHeader::sport = 0

source port

Definition at line 37 of file tcp_header.hh.

◆ syn

bool TCPHeader::syn = false

syn flag

Definition at line 46 of file tcp_header.hh.

◆ uptr

uint16_t TCPHeader::uptr = 0

urgent pointer

Definition at line 50 of file tcp_header.hh.

◆ urg

bool TCPHeader::urg = false

urgent flag

Definition at line 42 of file tcp_header.hh.

◆ win

uint16_t TCPHeader::win = 0

window size

Definition at line 48 of file tcp_header.hh.


The documentation for this struct was generated from the following files: