Matheion

MA-NET1 · Module 12

Packet reading — DNS query and answer

Topics: DNS messages · UDP port 53 · TTL and cache

Overview

Session 12 — DNS query and response in a capture

Before Mira's phone can send a single byte to api.skylark.example, it has to turn that name into an address. In the application-layer sessions you learned why that lookup exists and how a hierarchy of servers answers it. Now you will see the lookup itself as two small frames in an invented capture pane: one query leaving the phone, one response coming back.

You will learn to tell a query from a response using the UDP ports and the QR flag, to read the counts that say how many questions and answers a message holds, to pick the A record out of the answer section, and to compare an uncached lookup with a cached one using invented millisecond timings. A short fictional lookup-tool printout shows the same facts in friendlier clothing.

All names, addresses (203.0.113.40, 198.51.100.53, 10.0.4.23) and numbers are invented for practice, and nothing needs to be installed.

Plan: Concepts → Examples → Quiz.

Time: about 55–70 minutes, including the timing comparisons.

Concepts

Core ideas — a DNS lookup as it appears in a capture

When you read a capture of a web visit, the first frames are rarely the web at all. Before the TCP handshake, before the HTTP request, there is usually one tiny question and one tiny answer: what address belongs to this name? That exchange is DNS, and because it is short, self-contained, and fast, it is one of the best places to practise reading packets. In this session we follow Mira, whose Skylark Forecast app is about to contact api.skylark.example for the first time since her phone woke up. Every address and timing below is invented.


Where this exchange sits in the story

Mira's phone received its network settings by DHCP, and among them was the address of a resolver, a helper machine run by her access provider BayLink Access. In this capture the resolver is 198.51.100.53. The phone does not walk the DNS hierarchy itself; it just asks the resolver and waits. That asking is a single query frame, and the resolver's answer is a single response frame.

Here is the beginning of the capture, in the packet-list pane:

No.  Time(s)    Source          Destination     Proto  Len  Info
  1  0.000000   10.0.4.23       198.51.100.53   DNS     79  Standard query    0x7a3c A api.skylark.example
  2  0.041200   198.51.100.53   10.0.4.23       DNS     95  Standard response 0x7a3c A api.skylark.example A 203.0.113.40
  3  0.041980   10.0.4.23       203.0.113.40    TCP     58  51822 → 80 [SYN] Seq=0

Look at the three lines together before looking at any one of them. Frame 1 goes from the phone to the resolver. Frame 2 comes back. Frame 3 is the first frame of the web visit, and its destination address, 203.0.113.40, is exactly the address that frame 2 delivered. That is DNS doing its job: the answer in the middle of the capture becomes the destination of what follows. Frame 3 leaves only 0.78 ms after the answer arrived (0.041980 − 0.041200), because the app was waiting for precisely this.


DNS rides on UDP, and the ports say who is who

DNS lookups are normally carried in UDP datagrams, not TCP segments. There is no handshake, no connection, and no teardown: one datagram out, one datagram back. This is a good fit because a lookup is tiny and the app would otherwise pay a round trip for a handshake before doing any useful work. (If a response is too big for one datagram, or the server asks, DNS can switch to TCP, but the everyday lookup uses UDP.)

Here is the transport layer of frame 1 and frame 2:

Frame 1   User Datagram Protocol:  Source port 49731   Destination port 53
Frame 2   User Datagram Protocol:  Source port 53      Destination port 49731

The well-known port for DNS servers is 53. The client side uses an ephemeral port: a high, temporary number the operating system picks for this one conversation (49731 here). So the quickest direction test is: which side holds port 53? In frame 1 the destination is 53, so this is a message to a DNS server, a query. In frame 2 the source is 53, so the message comes from a DNS server, a response. The ephemeral number swaps sides, which is also how the phone's operating system knows which program waits for this answer.

Common confusion: Port 53 does not prove a frame is DNS, and a server is not "the one that sends first". The port is a strong hint, not a guarantee. What actually identifies the protocol is how the bytes are structured, which is why viewers decode them and show you Proto: DNS. And "who sends first" has nothing to do with client or server: a server is whoever listens on the well-known port.


Inside a DNS message: the header

Every DNS message, query or response, begins with the same 12-byte header, followed by up to four sections. Here is the decoded detail pane for the query, frame 1:

Domain Name System (query)
    Transaction ID: 0x7a3c
    Flags: 0x0100
        QR: 0  (this is a query)
        Opcode: 0 (standard query)
        AA: 0   TC: 0   RD: 1 (recursion desired)   RA: 0
        RCODE: 0
    Questions: 1   Answer RRs: 0   Authority RRs: 0   Additional RRs: 0
    Queries:
        api.skylark.example   type A   class IN

And the response, frame 2:

Domain Name System (response)
    Transaction ID: 0x7a3c
    Flags: 0x8180
        QR: 1  (this is a response)
        Opcode: 0 (standard query)
        AA: 0   TC: 0   RD: 1   RA: 1 (recursion available)
        RCODE: 0 (no error)
    Questions: 1   Answer RRs: 1   Authority RRs: 0   Additional RRs: 0
    Queries:
        api.skylark.example   type A   class IN
    Answers:
        api.skylark.example   type A   class IN   TTL 300   Address 203.0.113.40

Take the fields one at a time.

Transaction ID is a 16-bit label chosen by the client, copied unchanged into the response. Mira's phone may have several lookups in flight at once, and the ID is how it matches each answer to its question. Both frames say 0x7a3c, which is how you know frame 2 answers frame 1.

QR is a single bit, the "query or response" flag: 0 for a query, 1 for a response. That one bit is why the flags value changes from 0x0100 to 0x8180: the top bit of the 16-bit flags field is QR, and turning it on makes the leading hex digit 8. RD ("recursion desired") says "please do the whole lookup for me"; the phone sets it, because it is relying on the resolver. RA ("recursion available") is the resolver saying "yes, I do that"; it appears only in the response. RCODE is the result code: 0 means no error, and 3 means the name does not exist. AA says the answer came straight from an authoritative server; a resolver answering from its own work or cache leaves it at 0.

The four counts tell you how many entries sit in each following section. The query has 1 question and 0 answers; the response repeats the question (1) and adds 1 answer. If the counts and the actual sections disagree, something is wrong with the capture or the message.

Common confusion: The response contains the question again. It is not a mistake or a duplicate. Echoing the question lets the client confirm the answer is for the name it asked, and lets a viewer show you a complete story from one frame alone.


The A record

The answer section holds resource records. The one here is an A record, which maps a name to an IPv4 address. Read it left to right: name (api.skylark.example), type (A), class (IN, meaning Internet), TTL (300), and data (203.0.113.40).

The TTL in a DNS record is not the TTL in the IP header; the two share a name and nothing else. In DNS it is a number of seconds that a cache may keep the record before it must check again. The authoritative owners of skylark.example chose 300, so any resolver that learns this record may reuse it for up to five minutes.

A response can also hold several A records for one name, in which case Answer RRs is larger. Services do this to spread load, and clients usually just try the first address.


Uncached versus cached: what the timings mean

Run the capture a bit longer and the story repeats. Two minutes later, Mira's app asks again:

No.  Time(s)     Source          Destination     Proto  Info
  1  0.000000    10.0.4.23       198.51.100.53   DNS    Standard query    0x7a3c A api.skylark.example
  2  0.041200    198.51.100.53   10.0.4.23       DNS    Standard response 0x7a3c A ... TTL 300  Address 203.0.113.40
 ...
 31  120.500000  10.0.4.23       198.51.100.53   DNS    Standard query    0x91d4 A api.skylark.example
 32  120.503100  198.51.100.53   10.0.4.23       DNS    Standard response 0x91d4 A ... TTL 180  Address 203.0.113.40

The first lookup took 41.2 ms from query to response, and the second took 3.1 ms. The question and the answer are identical, so where did the 38 ms go? The first time, the resolver had nothing stored. It had to ask other servers (the hierarchy you studied earlier), and those conversations took time that the phone cannot see. The second time, the resolver already knew the record, so it replied from memory in the time of one short local round trip.

Two clues in frame 32 confirm this reading. The TTL has dropped from 300 to 180: the resolver's copy has been counting down for about 120 seconds, and it will expire in three more minutes. And the transaction ID and ephemeral port are different (0x91d4, a new port), because each lookup is its own independent exchange.

There is a third possibility that a capture shows only by absence: if the phone's own cache already holds the answer, no frames cross the interface at all. A short, quiet capture is sometimes the evidence of a cache hit that happened one level earlier.

Common confusion: A fast second lookup does not mean "the network got faster". Compare like with like: the physical round trip to the resolver is the same in both cases. The difference is whether the resolver had to do extra work behind its own back before answering.


A fictional lookup-tool printout

Many systems include a small command-line tool that sends exactly the query above and prints a friendlier summary. Here is an invented example of what such output could look like (the layout is made up for this lesson):

$ lookup api.skylark.example
Resolver:   198.51.100.53, port 53
Answer type: not authoritative (from the resolver)

Name:       api.skylark.example
Address:    203.0.113.40
TTL:        300 s
Time:       41 ms

Every line corresponds to something you have already decoded. The resolver and port are the destination of frame 1. Not authoritative is AA: 0. The name and address are the answer record, and the TTL and time come from the record and the frame timestamps. The tool adds no new information; it only hides the layers you now know how to open.


What to carry forward

A DNS exchange in a capture is two UDP frames: a query to port 53 from an ephemeral port, and a response from port 53 back to that port. The QR bit says which is which, the transaction ID pairs them, the counts say how many questions and answers follow, and the A record carries the address plus a TTL that governs caching. Timing differences between repeated lookups usually reveal caching, and the TTL counting down confirms it. The next session zooms into the UDP header you have just been looking at, and asks exactly what its four fields mean.

Examples

Session 12 — Worked examples

Every example uses the four beats Scene → Capture → Reading → Takeaway. All captures are invented. If you can, cover the Reading with your hand and try each one yourself before checking.


Example A — Query or response? Reading ports and the QR flag

Scene. Mira scrolls through a long capture and wants to mark every DNS frame as "going out" or "coming back". She has two frames to practise on.

Capture.

Frame 1:  IP  Src 10.0.4.23      Dst 198.51.100.53
          UDP Src port 49731     Dst port 53
          DNS Transaction ID 0x7a3c   Flags 0x0100   Questions 1  Answer RRs 0

Frame 2:  IP  Src 198.51.100.53  Dst 10.0.4.23
          UDP Src port 53        Dst port 49731
          DNS Transaction ID 0x7a3c   Flags 0x8180   Questions 1  Answer RRs 1

Reading.

  1. By ports. Frame 1 has destination port 53, so it is addressed to a DNS server: a query. Frame 2 has source port 53, so it comes from a DNS server: a response.
  2. By the QR bit. The flags are written as a 16-bit value. 0x0100 is 0000 0001 0000 0000 in binary: the top bit (QR) is 0, so it is a query, and the lone 1 near the middle is the RD bit ("please recurse"). 0x8180 is 1000 0001 1000 0000: the top bit is 1, so it is a response; RD is still 1 (copied back), and the next 1 further along is RA ("recursion available").
  3. By the counts. The query carries 1 question and no answers; the response carries 1 question and 1 answer. Responses are the ones that hold answers.
  4. Pairing. Both frames show transaction ID 0x7a3c, and the ephemeral port 49731 simply swaps sides. Together these say frame 2 answers frame 1.

Takeaway. Three independent clues should agree: port 53 on the destination side means query, the QR bit set means response, and a non-zero answer count means response. When they agree you can mark frames quickly and confidently.


Example B — Reading the response record by record

Scene. The Skylark Forecast team wants to confirm that the address returned for api.skylark.example is the one in their documentation. They open frame 2 in full.

Capture.

Domain Name System (response)
    Transaction ID: 0x7a3c
    Flags: 0x8180   (QR=1, RD=1, RA=1, AA=0, RCODE=0)
    Questions: 1   Answer RRs: 1   Authority RRs: 0   Additional RRs: 0
    Queries:
        api.skylark.example   type A   class IN
    Answers:
        api.skylark.example   type A   class IN   TTL 300   Data length 4   Address 203.0.113.40

Reading.

  1. Success? RCODE 0 means no error. If the name had not existed, we would see RCODE 3 instead (see Example D).
  2. Authority? AA=0, so this answer came from the resolver (from its cache or from its own work), not straight from the Skylark-owned authoritative server. That is normal and no cause for worry.
  3. How many answers? Answer RRs: 1, and exactly one record is listed. The counts and the sections agree.
  4. Which record? Type A, the IPv4 address record. Its data length is 4 bytes, which is exactly the size of an IPv4 address (four numbers of 8 bits each).
  5. The address. 203.0.113.40, as documented. The next frame in the capture (the first TCP SYN) goes to this address, which is a pleasing confirmation that the phone used what it was told.
  6. How long may it be reused? TTL 300 seconds, so the resolver, and any cache that copies it, may serve this answer for up to five minutes without asking again.

Takeaway. To read an answer, check RCODE first, then the answer count, then the record type, and only then the address. The TTL tells you how long the answer may be reused.


Example C — Cached or uncached? Comparing timings

Scene. Skylark's mobile team is investigating why the first screen of the app feels slow after a phone has been idle, while later screens feel snappy. They capture the DNS side of two launches.

Capture.

Launch 1 (phone idle, resolver cold)
No.  Time(s)    Source         Destination    Info
  1  0.000000   10.0.4.23      198.51.100.53  Query    0x7a3c A api.skylark.example
  2  0.041200   198.51.100.53  10.0.4.23      Response 0x7a3c ... TTL 300  203.0.113.40

Launch 2 (same phone, 70 seconds later)
No.  Time(s)    Source         Destination    Info
  1  0.000000   10.0.4.23      198.51.100.53  Query    0x91d4 A api.skylark.example
  2  0.003100   198.51.100.53  10.0.4.23      Response 0x91d4 ... TTL 230  203.0.113.40

Launch 3 (same phone, 400 seconds after launch 1)
No.  Time(s)    Source         Destination    Info
  1  0.000000   10.0.4.23      198.51.100.53  Query    0xb205 A api.skylark.example
  2  0.039800   198.51.100.53  10.0.4.23      Response 0xb205 ... TTL 300  203.0.113.40

Reading.

  1. Launch 1 takes 0.041200 − 0.000000 = 41.2 ms. The TTL in the reply is the full 300, which suggests the resolver has just fetched the record fresh and has not been holding it long.
  2. Launch 2 takes 3.1 ms. The reply's TTL is 230, which is 300 − 70: the resolver has been counting down for those 70 seconds, so this answer came from its cache. Lookup time dropped by 41.2 − 3.1 = 38.1 ms, roughly a thirteen-fold speed-up (41.2 ÷ 3.1 ≈ 13.3).
  3. Launch 3 takes 39.8 ms, close to launch 1, and its TTL is back at the full 300. Launch 3 happened 400 seconds after launch 1, which is more than the 300-second TTL, so the cached entry had expired and the resolver had to fetch the record again. The slow-then-fast-then-slow pattern is the TTL expiring.
  4. What explains the slow first screen? An uncached lookup adds about 40 ms. That is small on its own, but if the idle phone also needs a TCP handshake and a TLS-style setup afterwards, those costs stack up. The DNS capture shows only one piece of that delay.
  5. Honest limit. The capture cannot show what the resolver did to find the answer. It can only show the symptom: time and TTL.

Takeaway. A short reply time together with a TTL below the record's full value is strong evidence of a cache hit. A full TTL and a long reply time suggest the resolver had to do the work itself.


Example D — A name that does not exist

Scene. Mira mistypes the host name in a config file as api.skylrk.example (a letter is missing). The app fails, and she opens the capture to find out why.

Capture.

No.  Time(s)    Source         Destination    Proto  Info
  1  0.000000   10.0.4.23      198.51.100.53  DNS    Standard query    0x3e90 A api.skylrk.example
  2  0.038700   198.51.100.53  10.0.4.23      DNS    Standard response 0x3e90 No such name  A api.skylrk.example

Detail of frame 2:
    Flags: 0x8183   (QR=1, RD=1, RA=1, RCODE=3 No such name)
    Questions: 1   Answer RRs: 0   Authority RRs: 0   Additional RRs: 0
    Queries:  api.skylrk.example   type A   class IN
    Answers:  (none)

Reading.

  1. The pair is still a query and a response: ports 53 and ephemeral, transaction IDs match (0x3e90).
  2. The flags 0x8183 differ from the happy-path 0x8180 only in the last hex digit, which is the RCODE: 3, "no such name".
  3. Answer RRs: 0. The response is perfectly well-formed, but it contains no address. There is nothing for the app to connect to, and notably no TCP SYN follows in the capture.
  4. The lookup took 38.7 ms, about the same as an uncached success. The resolver had to ask around before concluding the name does not exist.
  5. Conclusion: this is a naming problem, not a connectivity problem. The network delivered both frames fine. The fix is to correct skylrk to skylark.

Takeaway. An error is not the same as silence. A response with RCODE 3 and zero answers is the network working correctly and telling you the name is wrong.


Example E — Several records and a printout

Scene. Northline Relay publishes two addresses for edge.northline.example. Mira runs a fictional lookup tool and compares its printout with the capture.

Capture.

Frame 2:  DNS response 0x5d11   Flags 0x8180   Questions 1   Answer RRs 2
    Answers:
        edge.northline.example   A   IN   TTL 60   Address 203.0.113.60
        edge.northline.example   A   IN   TTL 60   Address 203.0.113.61
    Sizes:  DNS message 72 bytes   UDP length 80   IP total length 100   Frame 114 bytes

$ lookup edge.northline.example
Resolver:   198.51.100.53, port 53
Answer type: not authoritative
Name:       edge.northline.example
Address:    203.0.113.60
Address:    203.0.113.61
TTL:        60 s

Reading.

  1. Answer RRs: 2 matches the two Address lines in the printout. The tool shows one line per record.
  2. The TTL is only 60 seconds, so Northline wants clients to re-check often, perhaps so it can steer them to a healthy machine quickly. A short TTL trades more lookups (more frames like these) for faster changes.
  3. The size chain: the DNS message is 12 (header) + 28 (question) + 2 × 16 (answers) = 72 bytes. Add the 8-byte UDP header: 80. Add the 20-byte IP header: 100. Add a 14-byte link header: 114.
  4. Compare with Mira's single-record response in Example B, which was 53 bytes of DNS and a 95-byte frame. The second record added exactly 16 bytes of DNS, and the longer name edge.northline.example accounts for the rest.
  5. The tool's "not authoritative" is the same AA: 0 you have seen before. It does not mean the answer is wrong, only that the resolver rather than the owner supplied it.

Takeaway. A friendly printout is a summary of fields you can read yourself: one line per record, a TTL taken from the record, and an "authoritative" label taken from the AA flag.

Practice check — DNS query and response in a capture

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

  1. 1.Two frames appear in a capture: Frame 1: UDP Src port 49731, Dst port 53 Frame 2: UDP Src port 53, Dst port 49731 Which description fits best?

  2. 2.A DNS query has Flags 0x0100 and its response has Flags 0x8180. Which single change turns the query's top bit into the response's top bit, and what does it mean?

  3. 3.A response shows: Transaction ID 0x7a3c, Questions: 1, Answer RRs: 1, and Answers: api.skylark.example A IN TTL 300 Address 203.0.113.40. What does the TTL of 300 mean?

  4. 4.The first lookup of api.skylark.example takes 41.2 ms with a reply TTL of 300. A second lookup, 90 seconds later, takes 3.0 ms with a reply TTL of 210. What is the most reasonable explanation?

  5. 5.A response has Flags 0x8183, Questions: 1, Answer RRs: 0, for the question api.skylrk.example A. No TCP SYN follows. What is the best diagnosis?

  6. 6.A DNS response shows Answer RRs: 2, with records: edge.northline.example A IN TTL 60 Address 203.0.113.60 edge.northline.example A IN TTL 60 Address 203.0.113.61 What can you conclude?

  7. 7.In the query frame, the 'Questions' count is 1 and 'Answer RRs' is 0. In the response frame, Questions is 1 and Answer RRs is 1. Why does the response repeat the question?

  8. 8.Frame 2 (the DNS response) arrives at t = 0.041200 s with Address 203.0.113.40. Frame 3 at t = 0.041980 s is a TCP SYN. What is the most likely destination of frame 3, and how long after the answer did it leave?