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, that you can read bytes in hexadecimal, and that you can run commands in a terminal. It covers one idea that every later chapter relies on: a protocol does its work by putting a small header in front of the data it carries.
What a protocol needs to do
A protocol is an agreement about how two parties exchange messages. Both sides must know where a message starts, what it means and what to do with it. The sender cannot phone the receiver to explain each message, so the explanation travels with the message itself.
That explanation is the header. The data being carried is the payload. A protocol message is therefore a header followed by a payload.
| Part | Role | Example |
|---|---|---|
| Header | Control information the protocol needs to do its job | Source port, destination port, sequence number |
| Payload | The data the protocol carries for the layer above | An HTTP request |
Why headers exist
Each protocol solves one problem, and its header holds the fields needed to solve it:
- TCP must deliver bytes reliably and in order, so its header has sequence numbers, acknowledgment numbers and flags.
- IP must move a packet between networks, so its header has source and destination addresses and a time-to-live (TTL) counter.
- Ethernet must move a frame across one local link, so its header has destination and source MAC addresses.
A protocol reads and writes only its own header. It treats the payload as opaque bytes and never inspects it. This is what makes layering work: IP does not care whether its payload is a TCP segment or a UDP datagram. It only needs to record which one it is.
Fixed layout and the "next protocol" field
Headers have a fixed binary layout. Every field sits at a known position and has a known width, so the receiver can parse it without guessing. Multi-byte numbers are sent in network byte order, which is big-endian (most significant byte first).
A header must also say what its payload is. Each layer includes a demultiplexing field for this:
| Header | Field | Example value | Meaning |
|---|---|---|---|
| Ethernet | EtherType | `0x0800` | Payload is IPv4 |
| IPv4 | Protocol | `6` | Payload is TCP |
| TCP | Destination port | `80` | Deliver to the web server |
Many headers also contain a length. IPv4 has a header length field and a total length field. TCP has a data offset field that gives the header size. The receiver uses these to find where the header ends and the payload begins.
Parsing a header in code
The following Python program packs a minimal 4-byte header and then parses it. The header has a 16-bit port, an 8-bit type and an 8-bit length. The payload follows immediately after.
import struct
payload = b"hello"
# Header: port (2 bytes), type (1 byte), payload length (1 byte)
# "!" means network byte order (big-endian).
header = struct.pack("!HBB", 8080, 1, len(payload))
message = header + payload
# The receiver knows the layout, so it can split the message.
port, kind, length = struct.unpack("!HBB", message[:4])
body = message[4:4 + length]
print(port, kind, length)
print(body.decode())The output is:
8080 1 5
helloThe receiver reads the first 4 bytes using the agreed layout. The length field then tells it how many payload bytes to take. Real protocols follow the same pattern with more fields.
Common errors
- Reading a multi-byte field in the wrong byte order. Port 80 is
0x0050on the wire, and a little-endian read gives0x5000, which is 20480. - Assuming a header has a fixed size. IPv4 and TCP headers are 20 bytes only when they carry no options. Always use the header length field.
- Mixing up the header of one layer with the payload of the layer below. The Ethernet payload begins with the IP header, and the IP payload begins with the TCP header.
Exercise
An IPv4 header has the Protocol field set to 17, and the payload starts with the bytes 0x1F90 0x0035. Assuming protocol 17 is UDP, which fields do those four bytes hold, and what are their values?
Show answer
Protocol 17 means the payload is a UDP datagram, and a UDP header starts with the source port followed by the destination port, each 2 bytes. So 0x1F90 is source port 8080 and 0x0035 is destination port 53.
Exercise
Why does a protocol put a length field or a "next protocol" field in its header instead of making the receiver work these out from the payload?
Show answer
A protocol treats its payload as opaque bytes, so it cannot learn the payload's size or type by inspecting it. The header must state where the payload ends and which protocol should handle it. Only then can the receiver pass the payload to the right handler.