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:
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):
| From | 192.168.1.23 : 51000 the laptop's DNS client, on a temporary port |
| To | 192.168.1.1 : 53 the router's DNS service |
| Payload | DNS 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:
| Bytes | Field | Hex | Value |
|---|---|---|---|
| 0-1 | Source port | c7 38 | 51000 |
| 2-3 | Destination port | 00 35 | 53 (DNS) |
| 4-5 | Length | 00 25 | 37 bytes (8 + 29) |
| 6-7 | Checksum | ec 99 | see section 7 |
| 8-36 | Payload (DNS) | 1a 2b … 00 01 | query 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:
| # | Range | Name | Used for |
|---|---|---|---|
| 1 | 0–1023 | Well-known | Standard services. Opening one usually needs admin rights. |
| 2 | 1024–49151 | Registered | Services registered by apps (e.g. 3478 STUN, 5432 PostgreSQL) |
| 3 | 49152–65535 | Ephemeral (dynamic) | Temporary client ports. Linux actually uses 32768-60999 by default. |
| UDP port | Service | Seen in |
|---|---|---|
| 53 | DNS | Every name lookup |
| 67 / 68 | DHCP server / client | Chapter 3 |
| 123 | NTP (time sync) | Every VM, including kade-api |
| 443 | QUIC / HTTP/3 | Browsers talking to Google and Cloudflare |
| 3478 | STUN | Hole punching (chapter 10) |
| 5353 | mDNS (local device discovery) | Finding the office printer by name |
| 51820 | WireGuard VPN | A possible office VPN |
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 port | Socket open? | Result |
|---|---|---|
| 53 | yes: DNS service | Payload handed to the DNS service |
| 67 | yes: DHCP server | Handed to the DHCP server |
| 33434 | no | Dropped; 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 field | Value | Bytes |
|---|---|---|
| Source IP | 192.168.1.23 | c0 a8 01 17 |
| Destination IP | 192.168.1.1 | c0 a8 01 01 |
| Zero, then protocol | 0, 17 (UDP) | 00 11 |
| UDP length | 37 | 00 25 |
How it is computed, with our real numbers:
| 1 | Put 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 |
| 2 | Add all the chunks together. | 0x4_1362 |
| 3 | Fold: add the part that overflowed past 16 bits back into the low 16 bits (0x1362 + 0x4). | 0x1366 |
| 4 | Flip 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:
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
Summary
| # | Field | Size | In our example | Job |
|---|---|---|---|---|
| 1 | Source port | 2 bytes | c7 38 = 51000 | Where replies go; optional (0) |
| 2 | Destination port | 2 bytes | 00 35 = 53 | Which program receives it |
| 3 | Length | 2 bytes | 00 25 = 37 | Header + data size, 8 to 65,535 |
| 4 | Checksum | 2 bytes | ec 99 | Detect damage; includes a pseudo-header; optional on IPv4 |
| - | Payload | 0-65,527 | 29-byte DNS query | Your data |
Try it yourself
Real DNS datagrams in hex
- Steps:
sudo tcpdump -n -X -i en0 udp port 53, thendig example.comin another terminal - What to look for: After the 20-byte IP header: your source port,
0035, the length, the checksum, then the name as07 example 03 com. - From section: 2. The datagram we will open
sudo tcpdump -n -X -i en0 udp port 53dig example.comChecksum 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 53A 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 9The OS's list of known ports
- What to look for:
domain 53/udp. - From section: 4. Field 2 · Destination port (2 bytes)
grep -w 53/udp /etc/servicesThe ephemeral port range
- What to look for:
32768 60999by default. - Runs on: Linux
- From section: 4. Field 2 · Destination port (2 bytes)
sysctl net.ipv4.ip_local_port_range