Sub topic 2
Headers and payloads: how a protocol does its job
This chapter assumes you know what a network and an IP address are. It also assumes you can read bytes written in hexadecimal and run commands in a terminal. It explains the header and payload split that every later chapter builds on. Encapsulation across the layers comes in the next chapter.
The two parts of a protocol message
A protocol is a set of rules that two programs follow so they can understand each other. A message sent under those rules has two parts:
- The payload is the data the protocol carries for someone else. The protocol does not interpret it.
- The header is the information the protocol adds in front of the payload so it can do its own job.
The postal system works the same way. The letter is the payload. The envelope, with its address, stamp and postcode, is the header. The postal service reads only the envelope. It never opens the letter.
This split is what makes layering possible. A protocol only has to understand its own header, and the payload can be anything. Ethernet can carry IPv4, IPv6 or ARP. IP can carry TCP, UDP or ICMP. Each protocol treats what it carries as opaque bytes.
What a header is for
Headers are not decoration. Each field answers a question the receiving protocol must settle before it can act. Different protocols ask the same few questions, so their headers share a pattern.
| Question the receiver must answer | Typical header field | Example |
|---|---|---|
| Who sent this and who should get it? | Addresses or ports | IPv4 source and destination address, TCP source and destination port |
| What is inside the payload? | A type or protocol number | Ethernet EtherType, IPv4 protocol field |
| Where does the payload end? | A length field | UDP length, IPv4 total length |
| Did the bytes arrive intact? | A checksum | TCP, UDP and IPv4 header checksums |
| How should this message be handled? | Flags and control values | TCP flags, IPv4 time to live |
| Is this message part of an ordered conversation? | Sequence numbers | TCP sequence number |
Not every protocol has every field. UDP is small because it makes few promises. TCP has more fields because it promises ordered, reliable delivery, and that needs sequence numbers, acknowledgements and flags.
Header layout: fields at fixed positions
Most network headers are binary, not text. Each field has a fixed size and sits at a fixed offset from the start of the header. The receiver does no searching. It reads bytes 0 and 1 as one field, bytes 2 and 3 as the next, and so on.
Multi-byte numbers use network byte order, which is big-endian: the most significant byte comes first. The port number 80 is 0x0050, so it is sent as the bytes 00 50. Reading those bytes as little-endian gives 0x5000, which is 20480. Getting byte order wrong is the most common mistake when writing a parser by hand.
Some headers have a variable length. IPv4 and TCP both allow optional fields called options. These headers therefore contain a field that gives their own length: the IPv4 IHL field and the TCP data offset field. Both count in 4-byte words, so a value of 5 means 20 bytes.
Worked example: a UDP datagram
UDP has one of the simplest headers in use. It is exactly 8 bytes, made of four 16-bit fields:
- Source port
- Destination port
- Length of header plus payload, in bytes
- Checksum
The program below builds a UDP datagram by hand and then parses it. The struct format "!HHHH" means network byte order (!) followed by four unsigned 16-bit integers (H).
import struct
payload = b"hello"
# UDP header: source port, destination port, length, checksum.
# Length counts the 8-byte header plus the payload.
# A checksum of 0 means "not computed" for UDP over IPv4.
header = struct.pack("!HHHH", 5000, 53, 8 + len(payload), 0)
datagram = header + payload
# Receiver side: read the fixed header, then use the length field
sport, dport, length, checksum = struct.unpack("!HHHH", datagram[:8])
body = datagram[8:length]
print(sport, dport, length, checksum)
print(body)The output is:
5000 53 13 0
b'hello'The receiver never had to guess where the payload begins or ends. The header has a fixed size, and the length field says how far the datagram extends.
Why a length field matters
Imagine a stream of bytes that holds several messages back to back. Without a length, the receiver cannot tell where one message stops and the next starts. A length field fixes this, and it is the basis of framing: splitting a byte stream into separate messages.
The next program defines a toy protocol with a 3-byte header (a 1-byte type and a 2-byte length) and parses two messages out of one byte string.
import struct
def encode(msg_type, payload):
return struct.pack("!BH", msg_type, len(payload)) + payload
stream = encode(1, b"hi") + encode(2, b"world")
offset = 0
while offset < len(stream):
msg_type, length = struct.unpack_from("!BH", stream, offset)
offset += 3
payload = stream[offset:offset + length]
offset += length
print(msg_type, payload)It prints:
1 b'hi'
2 b'world'The loop reads a header, uses its length to take exactly that many payload bytes, and then moves to the next header. The type field is available for the receiver to decide what to do with each payload.
The header tells the receiver what the payload is
A payload is opaque to the protocol that carries it. The receiver still has to know which protocol should read it next. The header therefore carries a demultiplexing field, a number that selects the next protocol or program. Demultiplexing means separating one incoming flow into several destinations.
- The Ethernet EtherType field says whether the payload is IPv4 (0x0800), IPv6 (0x86DD) or ARP (0x0806).
- The IPv4 protocol field says whether the payload is TCP (6), UDP (17) or ICMP (1).
- The TCP and UDP destination port says which program on the host gets the data.
This chain of "what comes next" values is how a host moves a received frame upward to the correct application. A later chapter follows it through the whole stack.
Trailers, checksums and padding
Most protocols put everything in a header, but a few add a trailer after the payload. Ethernet does this. The frame check sequence is a 4-byte checksum at the end of the frame. It sits there because the sender can compute it only after the whole frame is known.
A checksum is a small value computed from the other bytes of a message. The receiver recomputes it and compares. A mismatch means the bytes changed in transit, and the message is discarded. A checksum detects accidental damage. It does not protect against deliberate tampering, because anyone who changes the data can recompute it.
Some protocols also use padding, which is extra bytes added to meet a minimum or alignment requirement. Ethernet requires a minimum payload of 46 bytes, so shorter payloads get padding. The padding is not part of the upper-layer message. This is why IPv4 has its own total length field. It lets the receiver ignore Ethernet padding after the real IP packet ends.
Common errors when working with headers
- Wrong byte order. Use
!in Python'sstructformats. The default uses your machine's native order and may add padding between fields. - Treating the length field as payload length only. In UDP, the length includes the 8-byte header. In IPv4, the total length includes the IP header. Check what each field counts.
- Assuming a fixed header size. IPv4 and TCP headers can be longer than 20 bytes when options are present. Read the IHL or data offset field before slicing.
- Reading past the end. Always check that the buffer is at least as long as the header you are about to parse. Parsers that skip this check are a classic source of security bugs.
- Mixing up bits and bytes. Some fields are smaller than a byte. The IPv4 version and IHL share one byte, 4 bits each, and need masking and shifting.
Exercises
Exercise
A UDP datagram carries a 20-byte payload. What value is in its length field, and which bytes are these in hexadecimal on the wire?
Show answer
The length field holds 28, because it counts the 8-byte header plus the 20-byte payload. In hexadecimal that is 0x001C, sent as the two bytes 00 1C in network byte order.
Exercise
A receiver gets a frame whose EtherType is 0x0800, and the enclosed IPv4 packet has a protocol field of 17. Which protocol reads each payload, and what does each header field do?
Show answer
The EtherType tells the receiver that the Ethernet payload is an IPv4 packet, so IPv4 reads it next. The IPv4 protocol value 17 says the IPv4 payload is a UDP datagram, so UDP reads it after that. Both fields are demultiplexing fields that name the protocol for the next layer.
Exercise
Why does IPv4 have a total length field when the Ethernet frame already has a size?
Show answer
Ethernet may add padding to reach its minimum payload size. The IPv4 total length lets the receiver find where the real IP packet ends and discard the padding.