Matheion

MA-NET1 · Module 13

Packet reading — UDP header fields

Topics: UDP length · Ports · Checksum

Overview

Session 13 — UDP header fields from a capture

The previous session treated a DNS query as a message. This one picks up the envelope it travelled in: the UDP header. It is the smallest header you will meet in this course, only eight bytes, yet it holds four fields that every reader of captures needs to understand: the source port, the destination port, the length, and the checksum.

You will take the very same DNS query datagram from Mira's capture, look at it first as raw hexadecimal bytes, and then decode the bytes field by field. You will see exactly what the Length field counts (header plus payload, a detail that trips up almost everyone once), what the checksum protects and what it does not, and how a UDP header compares with the larger TCP header you studied earlier.

The numbers (a UDP length of 45 for the query, 61 for the answer) are invented for this lesson, and nothing needs to be installed to follow along.

Plan: Concepts → Examples → Quiz.

Time: about 55–70 minutes, including converting a few hex bytes by hand.

Concepts

Core ideas — the four fields of a UDP header, read from real-looking bytes

In the previous session you watched Mira's phone send a DNS question and receive an answer. You looked at the DNS message inside: the transaction ID, the flags, the A record. This time we turn the camera around and look at the wrapper. Every DNS message in that capture rode inside a UDP datagram, and the first eight bytes of that datagram are the UDP header. Once you can read those eight bytes you can read the transport layer of almost any UDP capture, whether it carries DNS, a video call, or a game.

All numbers here are invented for practice. The phone is 10.0.4.23, the resolver run by BayLink Access is 198.51.100.53, and the question is still about api.skylark.example.


The datagram as raw bytes

A capture viewer normally shows decoded fields, but underneath it is just a list of bytes written in hexadecimal. Two hex digits make one byte, so c2 is one byte and 00 35 is two. Here is the whole UDP datagram of the DNS query (frame 1 of the previous session), sixteen bytes per row:

offset  bytes                                            meaning
0000    c2 43 00 35 00 2d 68 3e  7a 3c 01 00 00 01 00 00
0010    00 00 00 00 03 61 70 69  07 73 6b 79 6c 61 72 6b
0020    07 65 78 61 6d 70 6c 65  00 00 01 00 01

There are 16 + 16 + 13 = 45 bytes in total. The first eight (c2 43 00 35 00 2d 68 3e) are the UDP header. Everything after them, starting at 7a 3c, is the payload: the DNS message, 37 bytes long. The UDP layer does not care what the payload means; to UDP it is just bytes to deliver.

Notice how little there is. TCP needed sequence numbers, acknowledgement numbers, flags, and a window. UDP needs none of those, because it makes no promises about reliability or order. It adds just enough to answer two questions: which program should get this? and how big is it? The checksum is a bonus for catching damage.


Decoding the header, field by field

The UDP header is exactly four fields, each 16 bits (2 bytes) wide, in this fixed order.

| Bytes | Field | Hex in our datagram | Decimal | What it says | |---|---|---|---|---| | 0–1 | Source port | c2 43 | 49731 | Which program sent this (an ephemeral port) | | 2–3 | Destination port | 00 35 | 53 | Which program should receive it (DNS) | | 4–5 | Length | 00 2d | 45 | Size of header + payload, in bytes | | 6–7 | Checksum | 68 3e | (0x683e) | Error check over header, payload, and addresses |

To turn two hex bytes into a decimal number, treat the first as the "sixteens-of-sixteens" part. For the source port: c2 43 is 0xC243, which is 12 × 4096 + 2 × 256 + 4 × 16 + 3 = 49152 + 512 + 64 + 3 = 49731. For the destination port: 00 35 is 3 × 16 + 5 = 53. For the length: 00 2d is 2 × 16 + 13 = 45 (d stands for 13).

Match this with the decoded pane that a viewer would show for the same frame:

User Datagram Protocol
    Source port:      49731
    Destination port: 53
    Length:           45
    Checksum:         0x683e  [correct]
    [Payload: 37 bytes]
Domain Name System (query)
    ...

Common confusion: Beginners often read the ports as one-byte numbers, but each is 16 bits. That is why port numbers go up to 65535 (that is 2^16 − 1). If a port ever looks tiny in hex, such as 00 35, the leading 00 byte is part of it.


Ports: how the datagram finds its program

An IP address gets a datagram to the right machine. The ports get it to the right program on that machine. When the resolver's operating system receives the query, it looks at destination port 53 and hands the payload to whatever program is listening there, namely the DNS service. When the answer returns, the roles flip: the source port of the response is 53 and the destination port is 49731, so Mira's operating system knows to give the payload to the app that sent the query from port 49731.

Here is the response datagram header for comparison:

00 35 c2 43 00 3d 7f 9f   ...

Reading: source port 00 35 = 53, destination port c2 43 = 49731, length 00 3d = 61, checksum 7f 9f. The two port fields have swapped places, exactly as the IP source and destination addresses would. The length changed because the response has an answer record added, and the checksum changed because the contents and addresses changed.


What the Length field counts

This is the field people most often misread. The UDP Length is the size of the whole UDP datagram: the 8-byte header plus the payload. It is not the payload size alone.

For our query: Length = 45, header = 8, so payload = 45 − 8 = 37 bytes. You can confirm that against the byte listing above: the DNS message starts at offset 8 (7a 3c) and the listing ends at offset 44 (01), which is 37 bytes.

Now relate it to the layers around it. The IP header above UDP is 20 bytes, so the IP total length is 20 + 45 = 65. The link-layer header adds 14 more, so the frame is 14 + 65 = 79 bytes, which matches the Len column from the previous session's packet list. For the response: UDP length 61, IP total 81, frame 95, again matching. Write the chain once and it will serve you for every UDP capture:

payload  +  8 (UDP header)  =  UDP length
UDP length  +  20 (IP header)  =  IP total length
IP total length  +  14 (link header)  =  frame length

Common confusion: Because the IP header already has a total-length field, it can look as though UDP's Length is redundant. In a sense it is: the receiver could work it out as IP total minus IP header. UDP keeps the field anyway as part of its simple, self-contained design, and it is also part of what the checksum covers. When the two ever disagree, the datagram is malformed and a careful receiver discards it.


The checksum: what it protects

The checksum lets the receiver detect that bits were damaged on the way. The sender computes a 16-bit value from the UDP header (with the checksum field treated as zero), the payload, and a few IP-level facts: the source address, the destination address, the protocol number (17 for UDP), and the length. Those IP-level facts are sometimes called a pseudo-header, because they take part in the calculation but are not sent as part of UDP. Including them lets the receiver notice a datagram that was delivered to the wrong address.

The receiver repeats the calculation. If the result does not match, the datagram is silently discarded; UDP does not ask for a retransmission. The application above, DNS in this case, simply notices that no answer arrived and asks again after a timeout.

Three practical points are worth knowing when reading captures:

  • It detects, it does not repair. A single flipped bit will be noticed with high probability, but UDP can do nothing but throw the datagram away.
  • Presence is optional in IPv4. A sender may put the value 00 00 in the field to mean "I did not compute a checksum". Many tools label this [checksum: none]. In IPv6 the checksum is mandatory.
  • A sender-side capture can show a wrong-looking checksum. Some network cards fill in the checksum after the capture program has copied the frame. If you capture on the sending machine, a viewer may print [incorrect, should be ...] for outgoing frames even though the network will see a perfectly good value. Check the same frame at the receiving side before concluding anything is broken.

UDP versus TCP in header size

A TCP header is at least 20 bytes and can grow to 60 with options. A UDP header is always 8. The difference is the price of features: sequence and acknowledgement numbers (2 × 32 bits), flags, a receive window, an urgent pointer, and variable options. UDP drops all of them.

UDP header:   [src port 16][dst port 16][length 16][checksum 16]          = 8 bytes
TCP header:   [src port 16][dst port 16][sequence 32][ack 32]
              [hdr len + flags 16][window 16][checksum 16][urgent 16]
              [options 0..40]                                              = 20..60 bytes

Two differences matter when you read captures. First, TCP has no length field: a receiver figures out how much payload there is by subtracting the IP and TCP header sizes from the IP total length, which is the arithmetic you did in the HTTP session. UDP does carry a length. Second, each UDP datagram stands alone. There is no handshake and no sequence number, so you cannot tell from the headers whether a datagram is part of a series or whether anything was lost. If you need that, the application has to supply it.

Common confusion: A smaller header does not make UDP "better" in general; it makes it cheaper and less informative. For a 37-byte question, an 8-byte header is a sensible deal. For a long file transfer where every byte must arrive in order, the missing features would have to be rebuilt by the application.


A reading routine for any UDP frame

  1. Find the UDP block and read the two ports. The lower, well-known one usually names the service; the high one is the client's ephemeral port.
  2. Read Length and subtract 8 to get the payload size. Check it against the payload the viewer lists.
  3. Check the chain: UDP length + 20 = IP total length, and IP total + 14 = frame length.
  4. Look at the checksum state: correct, absent (zero), or "incorrect" on a sender-side capture.
  5. Only then decode the payload as the application protocol.

What to carry forward

A UDP header is four 16-bit fields: source port, destination port, length, checksum, making 8 bytes. The ports pick the programs, the length counts header plus payload, and the checksum helps the receiver discard damaged datagrams. Together with the IP and link headers, the sizes always add up in the chain you wrote above. Next session leaves the transport layer and examines a different kind of message that rides directly inside IP: the control and diagnostic messages of ICMP, including ping and traceroute.

Examples

Session 13 — Worked examples

Each example has a Scene, a Capture, a Reading, and a Takeaway. The captures are invented, and the numbers have been chosen so that you can check every step by hand. Cover the Reading and have a go first.

A tiny reminder of the layout you need: the UDP header is four 16-bit fields (source port, destination port, length, checksum), 8 bytes in all.


Example A — Decoding a header from hex

Scene. Mira's teammate sends her the first eight bytes of the resolver's reply and asks, "Which program on your phone got this, and how big was the datagram?"

Capture.

offset  bytes
0000    00 35 c2 43 00 3d 7f 9f

Reading.

  1. Split the eight bytes into four pairs: 00 35, c2 43, 00 3d, 7f 9f. These are, in order, source port, destination port, length, and checksum.
  2. Source port. 00 35 = 0 × 4096 + 0 × 256 + 3 × 16 + 5 = 53. A source port of 53 tells us the datagram comes from a DNS service.
  3. Destination port. c2 43 = 12 × 4096 + 2 × 256 + 4 × 16 + 3 = 49152 + 512 + 64 + 3 = 49731. That is an ephemeral port: the one Mira's app used when it sent the query. So this reply is destined for that app.
  4. Length. 00 3d = 3 × 16 + 13 = 61 bytes for header plus payload.
  5. Payload size. 61 − 8 = 53 bytes. That is the size of the DNS response message (12-byte header, 25-byte question, 16-byte answer record).
  6. Checksum. 7f 9f is non-zero, so the sender did compute one.

Takeaway. To answer "which program" read the ports; to answer "how big" read the Length and subtract 8 for the payload. A reply simply has the two ports of the query in swapped order.


Example B — Walking the length chain backwards

Scene. Northline Relay's resolver answers a lookup for edge.northline.example with two address records. The packet list shows only the frame size, 114 bytes, and Mira wants to know how much DNS data it carried, working inwards from the outside.

Capture.

No.  Time(s)    Source          Destination     Proto  Len  Info
  2  0.040700   198.51.100.53   10.0.4.23       DNS    114  Standard response 0x5d11 ... 2 answers

Link header: 14 bytes     IP header: 20 bytes     UDP header: 8 bytes

Reading.

  1. Remove the link header: 114 − 14 = 100. That is the IP total length.
  2. Remove the IP header: 100 − 20 = 80. That is the UDP Length field.
  3. Remove the UDP header: 80 − 8 = 72. That is the DNS message size.
  4. Cross-check against the DNS content. The message holds a 12-byte header, a question for a 22-character name (28 bytes once the length labels, the final zero byte, and the type and class fields are counted), and two 16-byte answer records: 12 + 28 + 32 = 72. The numbers meet in the middle, so nothing is missing.
  5. If someone told you "UDP Length is 72", you would know they had forgotten the header: Length counts the header too, so 72 is the payload and 80 is the Length.

Takeaway. The chain frame → IP total → UDP length → payload is a set of simple subtractions of 14, 20, and 8. Going inward, subtract; going outward, add.


Example C — The same question over UDP and over TCP

Scene. A teammate wonders whether the DNS question might as well have been sent over TCP. Skylark's team compares the bytes for the same 37-byte DNS question, ignoring TCP options to keep the arithmetic tidy.

Capture. Two invented sketches of a single question frame.

Over UDP (as in the capture)
    Link 14 | IP 20 | UDP 8 | DNS message 37                        = 79 bytes, 1 frame, no handshake

Over TCP (sketch)
    Handshake first:  SYN (54)  +  SYN-ACK (54)  +  ACK (54)         = 3 frames before any data
    Link 14 | IP 20 | TCP 20 | 2-byte length prefix + DNS message 37 = 93 bytes

Reading.

  1. UDP header = 8 bytes; TCP header = 20 bytes (the minimum). That is 12 more bytes for the same job.
  2. DNS over TCP also puts a two-byte length prefix in front of the message, because a TCP byte stream has no natural boundary between messages. UDP does not need it, since each datagram is already a unit. That adds 2 more bytes: so the TCP data frame is 79 + 12 + 2 = 93 bytes, which matches the sketch.
  3. The bigger cost is not bytes but round trips. The three handshake frames mean one full round trip before the question can even be sent. If the resolver is 3 ms away, that is an extra 3 ms before the 3 ms for the lookup itself. For the common case of a one-question, one-answer exchange, UDP is simply cheaper.
  4. TCP's extra header bytes are not waste: they carry sequence numbers, acknowledgements, and a window, which provide reliability. The point is that a tiny lookup does not need those services.
  5. Where TCP does earn its keep for DNS is when an answer is too large for one datagram. The "truncated" flag in the DNS header then tells the client to ask again over TCP.

Takeaway. UDP is not just a smaller header; it is also no handshake and no extra framing. The trade is that the application must handle loss and ordering itself.


Example D — Three checksum states

Scene. Skylark's testers capture the same app on three different machines and notice that the checksum line looks different each time. Which ones are worrying?

Capture.

Capture 1 (taken on the resolver, received frame)
    UDP  Src port 49731  Dst port 53   Length 45   Checksum 0x683e [correct]

Capture 2 (taken on a lab test box, received frame)
    UDP  Src port 40100  Dst port 40500  Length 12   Checksum 0x0000 [none]

Capture 3 (taken on Mira's phone, outgoing frame)
    UDP  Src port 49731  Dst port 53   Length 45   Checksum 0xa3d1 [incorrect, should be 0x683e]

Reading.

  1. Capture 1 is straightforward: the checksum was verified at the receiving end and matches. Nothing to investigate.
  2. Capture 2 shows 0x0000, which for IPv4 means "the sender chose not to compute a checksum". It is allowed, though it means this datagram has no error detection at the transport layer. It is worth noting, not alarming. (In IPv6 this would be a problem, because the checksum is mandatory there.)
  3. Capture 3 is the tricky one. It says "incorrect", but also tells you what the value should be: 0x683e, the same value that capture 1 saw on the receiving side. That is a strong sign that the network card fills in the checksum after the capture program copied the frame (what is shown is a placeholder, not the final value). The datagram that left the phone was fine, as the resolver's own capture confirms.
  4. The deciding clue is therefore where each capture was taken. A "bad" checksum on a sender-side capture is often just an artefact. A "bad" checksum on a receiver-side capture would mean damage in transit, and the receiver would discard the datagram.

Takeaway. Read the checksum together with where the capture was taken and which direction the frame travelled. Zero means "none", and an "incorrect" label at the sender is frequently just timing.


Example E — A tiny datagram and the padding trap

Scene. In a lab network, Mira's phone (10.0.4.23) sends a four-byte status beacon, the text ping, to a test box at 10.0.4.9. The packet list reports a frame of 60 bytes, and a newcomer is confused: "Does the UDP payload have 60 bytes minus headers, so 18 bytes?"

Capture.

No.  Time(s)   Source      Destination  Proto  Len  Info
  1  0.000000  10.0.4.23   10.0.4.9     UDP     60  40100 → 40500  Len=4

Detail:
    Link:  14 bytes
    IP:    Total length 32      Protocol UDP (17)
    UDP:   Src port 40100  Dst port 40500  Length 12  Checksum 0xca0c [correct]
    Payload: 70 69 6e 67         ("ping")
    Trailer: 14 bytes of zeros  (link padding, not part of the datagram)

Reading.

  1. Start from the UDP block, not the frame. Length = 12, so the payload is 12 − 8 = 4 bytes, which is exactly the four letters of ping shown as 70 69 6e 67.
  2. Check the IP total length. 20 + 12 = 32, which matches the IP header's Total length 32.
  3. Add the link header. 14 + 32 = 46 bytes. But the packet list says 60. The difference, 60 − 46 = 14 bytes, is the trailer of zeros. A link technology like this one requires frames of at least 60 bytes (before its own check trailer), so the sender pads short frames with filler.
  4. Who is right? The UDP Length and the IP total length define the datagram. The padding sits outside it, and the receiving IP layer uses the IP total length to throw the padding away before handing the payload up.
  5. So the newcomer's 18 bytes is wrong: subtracting headers from the frame size works only when the frame is not padded. When the total is small, trust the Length fields.

Takeaway. The UDP Length field is the authority on how many bytes belong to the datagram. A frame that looks bigger than expected may simply carry link-level padding after the end of the IP datagram.

Practice check — UDP header fields from a capture

Multiple-choice check from the Matheion app lesson. Pick an answer for each question, then submit.

  1. 1.How many fields does a UDP header have, and how many bytes does it occupy?

  2. 2.The first eight bytes of a UDP datagram are: c2 43 00 35 00 2d 68 3e Which pair of values gives the source port and the destination port?

  3. 3.In that same datagram the Length field is 00 2d (decimal 45). How many bytes of payload does the datagram carry?

  4. 4.A packet list shows a DNS reply with frame length 95. The link header is 14 bytes and the IP header is 20 bytes. What is the UDP Length field?

  5. 5.A query leaves with Src port 49731, Dst port 53. The matching reply is captured. Which header ports should it show?

  6. 6.A capture shows: UDP Length 12, IP Total length 32, but the frame length is 60 and the viewer notes '14 bytes of trailing zeros'. What explains the extra bytes?

  7. 7.A capture taken on the sending phone shows an outgoing frame with 'Checksum 0xa3d1 [incorrect, should be 0x683e]'. The resolver's own capture of the same datagram shows 'Checksum 0x683e [correct]'. What is the most sensible conclusion?

  8. 8.Compare the 37-byte DNS question sent in one UDP datagram with the same question sent over TCP (minimum headers, plus a 2-byte length prefix, ignoring TCP options). Which statement is correct?