Chapters

Anatomy of a UDP datagram

Everything UDP does fits in 8 bytes. This chapter opens those bytes one field at a time, using a real datagram: your laptop asking the home router "what is the IP of api.kade.lk?".

Layers, routing and transport · chapter 15 of 34 · 6 min read

Two parts: header and data

A UDP datagram is an 8-byte header followed by your data, the payload. The header has four fields of 2 bytes (16 bits) each. Protocol diagrams draw headers in rows 32 bits wide:

bit →08162431bytes 0-3Source port16 bits · 2 bytesDestination port16 bits · 2 bytesbytes 4-7Length16 bits · 2 bytesChecksum16 bits · 2 bytesbytes 8+Data (payload)0 to 65,527 bytes · for DNS: the question

Each row is 32 bits = 4 bytes. The header is exactly two rows. Everything after it is your data. Compare TCP’s header later: at least five rows.

The datagram we will open

When you open api.kade.lk in the browser, the laptop first sends a DNS question to the home router (its DNS server from DHCP, chapter 3):

From192.168.1.23 : 51000 the laptop's DNS client, on a temporary port
To192.168.1.1 : 53 the router's DNS service
PayloadDNS query: "A record for api.kade.lk" 29 bytes

Here are all 37 bytes of that datagram, exactly as they leave the laptop, grouped by field:

0000c7src port3800dst port3500length25ecchecksum991aID2bID01flags00flags001 q011 q000000
001600000000000003len 361a70p69i04len 46bk61a64d65e02len 26cl6bk
003200end00type A01type A00class01class
BytesFieldHexValue
0-1Source portc7 3851000
2-3Destination port00 3553 (DNS)
4-5Length00 2537 bytes (8 + 29)
6-7Checksumec 99see section 7
8-36Payload (DNS)1a 2b … 00 01query ID 0x1a2b, flags “please recurse”, 1 question: 3 api 4 kade 2 lk 0, type A (IPv4 address), class IN

Byte offsets count from the start of the UDP header. DNS writes names as length-prefixed labels: 3 letters “api”, 4 letters “kade”, 2 letters “lk”, then 0 for the end. The DNS chapter will open the payload fully; here it is just “data” to UDP.

Field 1 · Source port (2 bytes)

Identifies the sending application, so replies find their way back to the right program. Here: c7 38 = 0xC738 = 51000, a temporary port the OS picked for this lookup (chapter 1 called it an ephemeral port).

The source port is optional in UDP: it may be 0 when no reply is expected. In practice almost every app fills it in.

Field 2 · Destination port (2 bytes)

Says which application on the receiving machine should get the datagram. Here: 00 35 = 0x0035 = 53, the DNS service. Ports are how one IP address can run many services at once: the router answers DNS on 53 and DHCP on 67 at the same time.

2 bytes = 16 bits, so ports go from 0 to 65,535. They are grouped by convention:

#RangeNameUsed for
10–1023Well-knownStandard services. Opening one usually needs admin rights.
21024–49151RegisteredServices registered by apps (e.g. 3478 STUN, 5432 PostgreSQL)
349152–65535Ephemeral (dynamic)Temporary client ports. Linux actually uses 32768-60999 by default.
UDP portServiceSeen in
53DNSEvery name lookup
67 / 68DHCP server / clientChapter 3
123NTP (time sync)Every VM, including kade-api
443QUIC / HTTP/3Browsers talking to Google and Cloudflare
3478STUNHole punching (chapter 10)
5353mDNS (local device discovery)Finding the office printer by name
51820WireGuard VPNA possible office VPN
TCP and UDP have separate port numbers. UDP 53 and TCP 53 are two different doors, which is why firewall rules always say which protocol.

How the port picks the program

When a datagram arrives, the OS looks at its destination IP and port and finds the program that opened a socket on that port. This sorting is called demultiplexing. On the router:

Datagram to portSocket open?Result
53yes: DNS servicePayload handed to the DNS service
67yes: DHCP serverHanded to the DHCP server
33434noDropped; the OS sends back ICMP "port unreachable" (type 3, code 3). This is how traceroute's last hop answers (chapter 13).

Field 3 · Length (2 bytes)

The size of the whole datagram, header plus data, in bytes. Here: 00 25 = 0x0025 = 37 = 8 header + 29 data. The minimum is 8 (a header with no data). The maximum is 65,535, though chapter 14 explained why real datagrams stay far smaller.

The IP header also carries a total length, so this field is technically redundant over IPv4. It makes UDP self-describing: its header alone says where its data ends.

Field 4 · Checksum (2 bytes)

An error-detection code. Here: ec 99. The receiver recomputes it and, if it does not match, silently throws the datagram away (UDP never asks for a resend).

The checksum covers three things: the UDP header, the data, and a pseudo-header, a few fields borrowed from the IP header that are never sent as part of UDP:

Pseudo-header fieldValueBytes
Source IP192.168.1.23c0 a8 01 17
Destination IP192.168.1.1c0 a8 01 01
Zero, then protocol0, 17 (UDP)00 11
UDP length3700 25
Why include IP fields? So a datagram that reaches the wrong address, or is mistaken for another protocol, fails the check. It is also why NAT must recompute the UDP checksum when it rewrites an IP or port (chapter 10).

How it is computed, with our real numbers:

1Put the checksum field to 0. Split pseudo-header + header + data into 16-bit chunks (pad with one zero byte if the length is odd).38 bytes + pad
2Add all the chunks together.0x4_1362
3Fold: add the part that overflowed past 16 bits back into the low 16 bits (0x1362 + 0x4).0x1366
4Flip every bit (ones' complement).0xEC99
✓The receiver adds everything including 0xEC99. A clean datagram always gives 0xFFFF.0xFFFF

Add it up

2 src port + 2 dst port + 2 length + 2 checksum = 8 bytes

That tiny, fixed header is exactly why UDP is so light. Here is our DNS question as it goes out on the WiFi, all layers together:

frame header · 14IP header · 20 · protocol 17UDP · 8DNS query · 29FCS · 4
75 bytes on the link in total. The useful question is 29 of them; UDP adds only 8.

Build your own datagram

Change the ports, addresses or payload and watch the header and checksum change. The checksum is computed for real, the same way the OS does it:

Source port
51000 → c738
Destination port
9999 → 270f
Length
8 + 10 = 18 → 0012
Checksum steps
sum 0x4859b → fold 0x859f → flip → 0x7a60
0000c7src38src27dst0fdst00len12len7ack60ck68h65e6cl6cl6fo20 6bk61a
001664d65e

Summary

#FieldSizeIn our exampleJob
1Source port2 bytesc7 38 = 51000Where replies go; optional (0)
2Destination port2 bytes00 35 = 53Which program receives it
3Length2 bytes00 25 = 37Header + data size, 8 to 65,535
4Checksum2 bytesec 99Detect damage; includes a pseudo-header; optional on IPv4
-Payload0-65,52729-byte DNS queryYour data
New terms: payload, pseudo-header, socket, demultiplexing, well-known / registered / ephemeral ports.

Try it yourself

Real DNS datagrams in hex

  • Steps: sudo tcpdump -n -X -i en0 udp port 53, then dig example.com in another terminal
  • What to look for: After the 20-byte IP header: your source port, 0035, the length, the checksum, then the name as 07 example 03 com.
  • From section: 2. The datagram we will open
sudo tcpdump -n -X -i en0 udp port 53dig example.com

Checksum checks

  • What to look for: [udp sum ok] on received packets. Sent packets may say "bad" because the network card fills the checksum in after tcpdump sees them (checksum offload).
  • From section: 7. Field 4 · Checksum (2 bytes)
sudo tcpdump -n -vv -i en0 udp port 53

A datagram to a closed port

  • Steps: nc -u 127.0.0.1 9, type a line
  • What to look for: An ICMP "port unreachable" comes back.
  • Runs on: with tcpdump -i lo0 icmp running (Linux: -i lo)
  • From section: 5. How the port picks the program
nc -u 127.0.0.1 9

The OS's list of known ports

grep -w 53/udp /etc/services

The ephemeral port range

sysctl net.ipv4.ip_local_port_range

Spotted a mistake, or something unclear? Suggest an edit on GitHub