Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

8 · What really happens when you run curl

In one sentence: before a single packet goes to the server, curl turns the name into an address (the hosts file first, then a DNS server on UDP port 53); after that, every step of lessons 3 to 7 happens in order, and one tcpdump shows them all.

The problem it solves

Lesson 1 started with curl http://web:8080/ failing five ways. The first was curl: (6) Could not resolve host: web: no packet to web was ever sent, because nothing could say which address web is. People and config files use names; packets carry addresses. This lesson fills in that first step, then follows one request all the way, so you can see each question of lesson 1 answered on the wire.

The idea: a phone book, checked in order

Turning a name into an address is resolving it. A program asks the system’s resolver, which looks in two places, in the order /etc/nsswitch.conf gives:

hosts:          files dns
  1. files: /etc/hosts, a local list of address name lines;
  2. dns: a DNS server, whose address is in /etc/resolv.conf, asked over UDP port 53.

getent hosts <name> looks a name up through the same path a program uses (the hosts file, then DNS); getent ahosts <name> makes the same call as most programs, curl among them (getaddrinfo). dig <name> asks a DNS server directly and skips /etc/hosts. That difference matters below.

Your lab container’s resolver

Docker wrote the lab container’s /etc/resolv.conf from your server’s:

# Generated by Docker Engine.
...
nameserver 169.254.169.254
search .
...

dig example.com sends one question there and prints the answer (shortened):

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 23060
example.com.		300	IN	A	172.66.147.243
example.com.		300	IN	A	104.20.23.154
;; Query time: 15 msec
;; SERVER: 169.254.169.254#53(169.254.169.254) (UDP)

status: NOERROR means it found the name; each A line is an IPv4 address; SERVER says who answered.

A DNS server for the lab

This lesson’s lab is Practical 2’s switch with a small DNS server, dnsmasq, on green (10.0.0.3), answering web.lab with blue’s address, and a web service on blue. Read along here; Practical 3 has you build this lab, with dnsmasq, yourself. ip netns exec uses /etc/netns/<name>/resolv.conf, when it exists, as the namespace’s resolv.conf, so red can get its own:

mkdir -p /etc/netns/red && echo "nameserver 10.0.0.3" > /etc/netns/red/resolv.conf
ip netns exec red dig web.lab
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
web.lab.		0	IN	A	10.0.0.2
;; SERVER: 10.0.0.3#53(10.0.0.3) (UDP)

curl -v shows the resolver’s answer before it connects:

* Host web.lab:8080 was resolved.
* IPv6: (none)
* IPv4: 10.0.0.2
*   Trying 10.0.0.2:8080...

When dig and your program disagree

Add a line 10.0.0.9 web.lab to red’s hosts file (/etc/netns/red/hosts). Now:

AskAnswer
getent hosts web.lab10.0.0.9 web.lab (files come first)
dig +short web.lab10.0.0.2 (DNS only)
curl http://web.lab:8080/Failed to connect to web.lab port 8080 after 3088 ms

curl uses the resolver, gets 10.0.0.9, and fails after 3 seconds, the same failure as lesson 1’s 10.0.0.9 row (lesson 5: nobody answers ARP for it). When dig looks right and the program still goes somewhere else, look in /etc/hosts. Take the line out again: rm /etc/netns/red/hosts.

When resolving fails

Could not resolve host covers several different failures:

What’s wrongdig sayscurlafter
the server has no answer for the namestatus: REFUSED for nosuch.labCould not resolve host: nosuch.lab0.009 s
the DNS server program is stoppedcommunications error to 10.0.0.3#53: connection refusedCould not resolve host: web.lab0.009 s or 5.015 s
resolv.conf names an address with no servercommunications error to 10.0.0.9#53: timed outCould not resolve host: web.lab6.136 s

In the first row, green’s dnsmasq runs with --no-resolv: with no server to pass other names on to, it refuses them. One in charge of all of .lab says NXDOMAIN. The second row is lesson 6’s “refused” for UDP: green’s kernel answers the query with ICMP 10.0.0.3 udp port 53 unreachable. In the third, nothing answers at that address at all.

curl prints the same words in both, so ask dig, which names the server it tried and says which: connection refused at once, or timed out after its limit.

With the server stopped, the wait depends on those ICMP answers. Green’s kernel sends only a few in a row to one address (net.ipv4.icmp_ratelimit); a query left without one waits 5 s. So curl took 5.015 s right after other lookups, 0.009 s alone.

The whole path, in one tcpdump

Red’s first curl http://web.lab:8080/, after ip -n red neigh flush all, watched on veth-red (options cut):

16:18:06.929323 ARP, Request who-has 10.0.0.3 tell 10.0.0.1, length 28
16:18:06.929353 ARP, Reply 10.0.0.3 is-at 82:3d:76:53:5e:58, length 28
16:18:06.929355 IP 10.0.0.1.42557 > 10.0.0.3.53: 58341+ A? web.lab. (25)
16:18:06.929381 IP 10.0.0.1.42557 > 10.0.0.3.53: 30187+ AAAA? web.lab. (25)
16:18:06.929474 IP 10.0.0.3.53 > 10.0.0.1.42557: 58341* 1/0/0 A 10.0.0.2 (41)
16:18:06.929498 IP 10.0.0.3.53 > 10.0.0.1.42557: 30187 Refused 0/0/0 (25)
16:18:06.929615 ARP, Request who-has 10.0.0.2 tell 10.0.0.1, length 28
16:18:06.929636 ARP, Reply 10.0.0.2 is-at 4a:ef:fa:45:62:f5, length 28
16:18:06.929638 IP 10.0.0.1.40736 > 10.0.0.2.8080: Flags [S], ...
16:18:06.929657 IP 10.0.0.2.8080 > 10.0.0.1.40736: Flags [S.], ...
16:18:06.929668 IP 10.0.0.1.40736 > 10.0.0.2.8080: Flags [.], ...
16:18:06.929720 IP 10.0.0.1.40736 > 10.0.0.2.8080: Flags [P.], seq 1:77, ...
16:18:06.929739 IP 10.0.0.2.8080 > 10.0.0.1.40736: Flags [P.], seq 1:45, ...
16:18:06.929773 IP 10.0.0.2.8080 > 10.0.0.1.40736: Flags [F.], ...

Read it against lesson 1’s six questions:

LinesQuestionLesson
(before any packet) a route to 10.0.0.3, then to 10.0.0.22 · which way?3, 4
ARP for 10.0.0.33 · who on the link?5
A? web.lab. and its answer A 10.0.0.21 · which address?8
ARP for 10.0.0.23 · who on the link?5
[S], [S.], [.] (no firewall dropped them)4, 5 · let through? listening?7, 6
[P.] 76 bytes out (GET /), 44 bytes back (HTTP/1.0 200 OK)6 · does the program answer?this one

The page leaves out three pure ACKs and red’s FIN: 18 packets in all, 0.48 ms from the first ARP to the last ACK. The handshake only proved the kernel listens (lesson 6); the 44-byte reply is the program itself answering. This order is the order the capstone’s script checks things in.

Try it on your computer

The capstone, Practical 3 · Capstone: netcheck, builds this lab, with a router in front of blue so a route can break too, and has you write a script that asks the six questions in this order and names the first one that fails.

Go deeper: reading dig, and why the AAAA was refused

status is the server’s verdict: NOERROR (found), NXDOMAIN (the name doesn’t exist), REFUSED (this server won’t answer it), SERVFAIL (it tried and failed). In flags, aa means the answer is authoritative: green’s dnsmasq is the source for web.lab itself. The 0 before IN A in web.lab. 0 IN A is the TTL: how many seconds the answer may be cached (300 for example.com).

curl asked for both A (IPv4) and AAAA (IPv6) at once, from the same port. The lab’s dnsmasq knows only web.lab’s IPv4 address and refused the AAAA; curl -v shows that as IPv6: (none) and carries on with IPv4. The dig manual and resolv.conf(5) have the rest.

Go deeper: per-namespace files, and timing a request

ip netns exec red bind-mounts every file in /etc/netns/red/ over the same name in /etc for the command it runs (ip-netns(8)): ip netns exec red cat /etc/resolv.conf prints nameserver 10.0.0.3 while the container’s own file is unchanged. Docker, likewise, generates each container’s own resolv.conf (“Generated by Docker Engine”, above).

curl -w prints how long each step took, in seconds since the start:

ip netns exec red curl -sS -o /dev/null -w "dns %{time_namelookup} connect %{time_connect} total %{time_total}\n" http://web.lab:8080/
dns 0.000573 connect 0.000675 total 0.000755

On a slow request, this splits “slow DNS” from “slow connect” from “slow server” in one line.

Common misunderstandings

  • “dig works, so the app will resolve it the same way.” dig skips /etc/hosts; programs don’t. Use getent ahosts to see what programs get.
  • “Could not resolve host means the name doesn’t exist.” It also means the DNS server is down or unreachable. dig names the server it tried and says refused or timed out.
  • “If the name resolves, the problem is the network.” The address could be the wrong one (a stale hosts line), and everything after it then fails for a reason that has nothing to do with the real server.

Check yourself

1. curl http://api:8080/ fails with "Could not resolve host" after about 5 seconds. dig api says connection refused. What's wrong? The DNS server: its machine is up (it answered with an ICMP "port unreachable"), but no DNS program listens on port 53. Check the server named in /etc/resolv.conf.
2. dig +short db says 10.0.5.20, but your app keeps connecting to 10.0.9.9. Where do you look? /etc/hosts (or whatever getent hosts db shows): files come before DNS, and dig never reads them.
3. In a tcpdump of a failing request you see the DNS answer and an ARP reply, then [S] three times and nothing back. Which question failed? "Let through?" (or the route back): the name, route and neighbour all worked, and the SYNs got no answer. A firewall dropping them (lesson 7) or a missing route back (lesson 4).

Next: Practical 3 · Capstone: netcheck.