Finding where a request breaks

A request passes the name, the route, the port, the padlock and the answer. Each tool checks one of these steps. Go in order and stop at the first failure, and curl's error code and timings show where to look.

Part 9 of 9 · Networking4 min read
namedig(6)routeip route getportnc -zv(7) (28)padlockopenssl(60)answercurl -v500the first failure stops youhere: no answer to the SYNone question eachin order, stop at the first failurewhere the time wenttcpdump: any reply?the last word

“It is slow” and “it does not work” say nothing about where the problem is. A request passes five steps in a fixed order. The name must become an address. A route must lead to it. A port must accept the connection. The padlock must pass. Only then does the program answer. A failure at one step looks like a failure of the whole request.

Bub walks the path of a request and checks one station at a time. Each station has a tool that answers one question:

  • the name: dig
  • the route: ip route get
  • the port: nc -zv
  • the padlock: openssl s_client
  • the answer: curl -v

We go in order and stop at the first failure. curl alone already tells us a lot, so we start with it.

The error already names the step

curl ends with a different error for each kind of failure. We build five. The first is a name that does not exist. The others are a port with no program, a port whose packets are dropped, an expired certificate, and a program that answers with an error.

Shell
docker run --rm --cap-add NET_ADMIN alpine sh -c '
apk add -q curl iptables netcat-openbsd
iptables -A INPUT -p tcp --dport 9002 -j DROP 
printf "HTTP/1.1 500 Internal Server Error\r\nContent-Length: 4\r\nConnection: close\r\n\r\nfail" | nc -l -p 9003 >/dev/null &
sleep 1
for u in http://no-such-name.test http://127.0.0.1:9001 http://127.0.0.1:9002 \
       https://expired.badssl.com http://127.0.0.1:9003; do
curl -sS -o /dev/null -w "http %{http_code}\n" --connect-timeout 2 $u 2>&1 | head -1  
done'
curl: (6) Could not resolve host: no-such-name.test (Domain name not found)
curl: (7) Failed to connect to 127.0.0.1:9001 after 0 ms: Could not connect to server
curl: (28) Connection timed out after 2003 milliseconds
curl: (60) SSL certificate OpenSSL verify result: certificate has expired (10)
http 500

Here is what each marked part does:

  1. 1The iptables line makes port 9002 silent, as in part 3. printf ... | nc -l 9003 & is a tiny server that always answers 500.
  2. 2-w "http %{http_code}\n" prints the status code. The for loop tries the five addresses one after the other.
  3. 3--connect-timeout 2 gives up on connecting after 2 seconds.

We keep the first line of each result. Error 6 means the name did not resolve (part 2). Error 7 means curl could not connect. Here the packet arrived and the port refused it, because nothing listens there (part 3). Error 28 means no answer came in time: a firewall that drops packets, a wrong route from part 1, or a machine that is off. Error 60 means the padlock check failed (part 5). When all of these pass, curl prints the status of the answer, and a 500 means the server answered with an error (parts 4 and 8).

(6) Could not resolve hostno such namepart 2(7) Failed to connectport closedpart 3(28) Connection timed outno answerparts 1 and 3(60) SSL certificate verifybad padlockpart 5http 500program failedparts 4 and 8
Each curl error belongs to one step of the path, and each step belongs to one part of this series.

Where the time went

A slow request has no error to read. curl can print how long each step took, as a number of seconds from the start. We time a healthy request, and then a program that waits about two seconds before it answers:

Shell
docker run --rm alpine sh -c '
apk add -q curl netcat-openbsd
F="dns %{time_namelookup}  connect %{time_connect}  tls %{time_appconnect}  first byte %{time_starttransfer}  total %{time_total}\n" 
curl -s -o /dev/null -w "$F" https://example.com 
(while true; do { sleep 2; printf "HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok"; } | nc -l -p 8080 >/dev/null; done) & 
sleep 0.2
curl -s -o /dev/null -w "$F" http://127.0.0.1:8080'
dns 0.012025  connect 0.101938  tls 0.202275  first byte 0.296900  total 0.297020
dns 0.000044  connect 0.000374  tls 0.000000  first byte 1.792014  total 1.792119

Here is what each marked part does:

  1. 1The line that starts with F= saves the format. -w prints values that curl measured: time_namelookup is the name, time_connect the connection, time_appconnect the padlock, time_starttransfer the first byte, and time_total the end.
  2. 2curl -w "$F" uses the saved format, so that both commands print the same columns.
  3. 3The loop starts a slow server. It waits 2 seconds before it answers.

Each number includes the ones before it, so we read the differences. In the first line, the name took 12 ms, the connection another 90 ms, and the padlock another 100 ms. The answer arrived 95 ms after that. In the second line, the name and the connection took almost no time. The first byte arrived after 1.8 seconds, so the program was slow. It waits 2 seconds, and its timer started 0.2 seconds before the request. The network was fast, so we look at the program. The numbers of the first line depend on your connection to example.com, so yours can differ.

healthy0.3 sslow programwaiting for the program: 1.8 snameconnectpadlockwaiting for the program
The same total can hide different causes. The healthy request spends its time on the network. The slow one waits for the program.

The last word is the packets

Sometimes two tools disagree, or a firewall hides the reason. tcpdump shows the packets that a machine sent and received. Here is the dropped port from before, seen from the same machine:

Shell
docker run --rm --cap-add NET_ADMIN --cap-add NET_RAW alpine sh -c '
apk add -q curl iptables tcpdump
iptables -A INPUT -p tcp --dport 9002 -j DROP
tcpdump -nn -ttt -l -i lo -c 3 "tcp port 9002" 2>/dev/null | sed "s/^ *//" | cut -d" " -f1,3-7 &
sleep 1 
curl -s -o /dev/null --connect-timeout 5 http://127.0.0.1:9002
sleep 1'
00:00:00.000000 127.0.0.1.49570 > 127.0.0.1.9002: Flags [S],
00:00:01.043945 127.0.0.1.49570 > 127.0.0.1.9002: Flags [S],
00:00:01.024570 127.0.0.1.49570 > 127.0.0.1.9002: Flags [S],

Here is what each marked part does:

  1. 1sleep 1 gives tcpdump time to start before curl connects. The other options are the same as in part 3.

The first column is the time since the previous packet. The same SYN leaves again and again, and no answer comes back. The request was sent and nothing answered, so a firewall on the way or at the other end can be dropping it. In this test the firewall is on the same machine, and tcpdump sees the packet before the firewall drops it. A refusal would show a Flags [R.] line in reply.

SYN0 sSYN+1.04 sSYN+1.02 sno reply
The same SYN leaves again and again. Nothing comes back, so the packet is lost on the way or at the other end.

Key points

  • A request passes five steps in order: the name, the route, the port, the padlock and the answer.
  • curl names the failing step. Error 6 means a name did not resolve. Error 7 means the connection failed, for example a refusal on a closed port. Error 28 means no answer came in time. Error 60 means the padlock check failed. A status code means the server answered.
  • Refused means the packet arrived. Timed out means that nothing came back.
  • curl -w splits the total time into the name, the connection, the padlock and the wait for the program.
  • tcpdump shows whether a packet left and whether anything returned. Run every tool where the failure happens.

This was the last part of the series.

New posts by email

One email when a post ships, about twice a month. Nothing else, and one click to leave.

Latest: Finding where a request breaks · RSS