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

IPv4 Internet datagram header More...

#include <ipv4_header.hh>

Public Member Functions

ParseResult parse (NetParser &p)
 Parse the IP fields from the provided NetParser. More...
 
uint16_t payload_length () const
 Length of the payload. More...
 
uint32_t pseudo_cksum () const
 pseudo-header's contribution to the TCP checksum More...
 
std::string serialize () const
 Serialize the IP 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

IPv4 Header fields
uint8_t ver = 4
 IP version. More...
 
uint8_t hlen = LENGTH / 4
 header length (multiples of 32 bits) More...
 
uint8_t tos = 0
 type of service More...
 
uint16_t len = 0
 total length of packet More...
 
uint16_t id = 0
 identification number More...
 
bool df = true
 don't fragment flag More...
 
bool mf = false
 more fragments flag More...
 
uint16_t offset = 0
 fragment offset field More...
 
uint8_t ttl = DEFAULT_TTL
 time to live field More...
 
uint8_t proto = PROTO_TCP
 protocol field More...
 
uint16_t cksum = 0
 checksum field More...
 
uint32_t src = 0
 src address More...
 
uint32_t dst = 0
 

Static Public Attributes

static constexpr uint8_t DEFAULT_TTL = 128
 A reasonable default TTL value. More...
 
static constexpr size_t LENGTH = 20
 IPv4 header length, not including options More...
 
static constexpr uint8_t PROTO_TCP = 6
 Protocol number for tcp. More...
 

Detailed Description

IPv4 Internet datagram header

Note
IP 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
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version| IHL |Type of Service| Total Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Identification |Flags| Fragment Offset |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Time to Live | Protocol | Header Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

This struct can be used to parse an existing IP header or to create a new one.

Definition at line 8 of file ipv4_header.hh.

Member Function Documentation

◆ parse()

ParseResult IPv4Header::parse ( NetParser p)

Parse the IP fields from the provided NetParser.

Parameters
[in,out]pis a NetParser from which the IP 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 is too short to contain a header
  • wrong IP version number
  • the header's hlen field is shorter than the minimum allowed
  • there is less data in the header than the doff field claims
  • there is less data in the full datagram than the len field claims
  • the checksum is bad

Definition at line 23 of file ipv4_header.cc.

◆ payload_length()

uint16_t IPv4Header::payload_length ( ) const

Length of the payload.

Definition at line 112 of file ipv4_header.cc.

◆ pseudo_cksum()

uint32_t IPv4Header::pseudo_cksum ( ) const

pseudo-header's contribution to the TCP checksum

This value is needed when computing the checksum of an encapsulated TCP segment.

0 7 8 15 16 23 24 31
+--------+--------+--------+--------+
| source address |
+--------+--------+--------+--------+
| destination address |
+--------+--------+--------+--------+
| zero |protocol| payload length |
+--------+--------+--------+--------+

Definition at line 125 of file ipv4_header.cc.

◆ serialize()

string IPv4Header::serialize ( ) const

Serialize the IP fields.

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

Definition at line 78 of file ipv4_header.cc.

◆ summary()

std::string IPv4Header::summary ( ) const

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

Definition at line 151 of file ipv4_header.cc.

◆ to_string()

std::string IPv4Header::to_string ( ) const

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

Returns
A string with the header's contents

Definition at line 134 of file ipv4_header.cc.

Member Data Documentation

◆ cksum

uint16_t IPv4Header::cksum = 0

checksum field

Definition at line 44 of file ipv4_header.hh.

◆ DEFAULT_TTL

constexpr uint8_t IPv4Header::DEFAULT_TTL = 128
staticconstexpr

A reasonable default TTL value.

Definition at line 10 of file ipv4_header.hh.

◆ df

bool IPv4Header::df = true

don't fragment flag

Definition at line 39 of file ipv4_header.hh.

◆ dst

uint32_t IPv4Header::dst = 0

dst address

Definition at line 46 of file ipv4_header.hh.

◆ hlen

uint8_t IPv4Header::hlen = LENGTH / 4

header length (multiples of 32 bits)

Definition at line 35 of file ipv4_header.hh.

◆ id

uint16_t IPv4Header::id = 0

identification number

Definition at line 38 of file ipv4_header.hh.

◆ len

uint16_t IPv4Header::len = 0

total length of packet

Definition at line 37 of file ipv4_header.hh.

◆ LENGTH

constexpr size_t IPv4Header::LENGTH = 20
staticconstexpr

IPv4 header length, not including options

Definition at line 9 of file ipv4_header.hh.

◆ mf

bool IPv4Header::mf = false

more fragments flag

Definition at line 40 of file ipv4_header.hh.

◆ offset

uint16_t IPv4Header::offset = 0

fragment offset field

Definition at line 41 of file ipv4_header.hh.

◆ proto

uint8_t IPv4Header::proto = PROTO_TCP

protocol field

Definition at line 43 of file ipv4_header.hh.

◆ PROTO_TCP

constexpr uint8_t IPv4Header::PROTO_TCP = 6
staticconstexpr

Protocol number for tcp.

Definition at line 11 of file ipv4_header.hh.

◆ src

uint32_t IPv4Header::src = 0

src address

Definition at line 45 of file ipv4_header.hh.

◆ tos

uint8_t IPv4Header::tos = 0

type of service

Definition at line 36 of file ipv4_header.hh.

◆ ttl

uint8_t IPv4Header::ttl = DEFAULT_TTL

time to live field

Definition at line 42 of file ipv4_header.hh.

◆ ver

uint8_t IPv4Header::ver = 4

IP version.

Definition at line 34 of file ipv4_header.hh.


The documentation for this struct was generated from the following files:
Address
Wrapper around IPv4 addresses and DNS operations.
Definition: address.hh:13
man4::zero
zero