networks/traceroute-how-a-countdown-number-reveals-every-stop-between-you-and-a.md

Traceroute: how a countdown number reveals every stop between you and a server

Networks · 5 min read ·

Your data crosses a dozen machines on its way to a server, and you never see one of them. Yet a tool called traceroute lists every stop in order.

The secret is a small countdown number inside each packet, the TTL. Every router subtracts 1, and the router that reaches 0 drops the packet and sends a note back. Send probes with TTL 1, 2, 3 and you learn who answers at each step.

Send your own packets through a five-stop path, run a traceroute, and see why some hops show only stars.

Five circles in a row showing a TTL countdown 3, 2, 1, 0 where the packet is dropped at the fourth stop

Your computer can send a message to a server on the other side of the world, and the message crosses a dozen machines on the way. You never see any of them. Yet a tool called traceroute lists every one, in order. How can a message be made to reveal the road it took? The answer is a single small number stored inside every packet.

A packet carries a countdown

Data on the internet travels in packets: small chunks, each with a header that works like the address label on a parcel. The machines that pass packets along are called routers. Each one reads the destination, picks the next machine, and forwards the packet. One hop is one router handing a packet to the next.

One field in that header is the TTL, short for "time to live". The sender sets it to a starting number (64 is common on Linux and macOS, 128 on Windows). Every router subtracts 1 before forwarding. If the number reaches 0, the router throws the packet away instead of forwarding it. (In the newer IPv6 version of the protocol the same field is called the hop limit.)

Why have this at all? Mistakes in routing tables can send a packet in a circle. Without a countdown, it would loop forever and pile up. TTL guarantees every packet eventually dies.

The dying packet sends a note

When a router drops a packet because its TTL hit 0, it does not stay silent. It sends a small message back to the original sender called an ICMP "Time Exceeded" message (ICMP is the protocol machines use to report problems about packets). That message arrives from the router's own address. So the sender learns that router's address.

That is the trick. If you choose the TTL on purpose, you choose which router drops your packet, and so which router writes back:

  • TTL 1: the first router drops it and replies.
  • TTL 2: the second router drops it and replies.
  • TTL 3: the third router, and so on, until a packet has enough TTL to reach the destination itself.

Sending probes with TTL 1, 2, 3 and so on, and writing down who answers each one, gives you the whole route. That is exactly what traceroute does (it is called tracert on Windows).

Try it: send your own packets

Below is a made-up path with five stops. Use the buttons to pick a TTL, then send one packet and watch where it dies. Then run a full traceroute. The times are invented example numbers, not measurements.

The same idea in code

You do not need real routers to see the logic. This Python program models the path above and sends probes with TTL 1 to 5. It does what a router does: subtract 1, and stop at 0.

route = ["home router", "ISP gateway", "city exchange",
         "backbone router", "example.com server"]

def probe(ttl):
    for hop, name in enumerate(route, start=1):
        if hop == len(route):
            return hop, name, "arrived"
        ttl -= 1                      # every router subtracts 1
        if ttl == 0:                  # ...and drops the packet at zero
            return hop, name, "time exceeded"

for ttl in range(1, 6):
    hop, name, result = probe(ttl)
    print(f"TTL {ttl} -> hop {hop}: {name} ({result})")

Running it prints:

TTL 1 -> hop 1: home router (time exceeded)
TTL 2 -> hop 2: ISP gateway (time exceeded)
TTL 3 -> hop 3: city exchange (time exceeded)
TTL 4 -> hop 4: backbone router (time exceeded)
TTL 5 -> hop 5: example.com server (arrived)

Notice the last line. A packet whose TTL is more than enough does not die: it reaches the destination, and the destination answers normally. That different answer is how traceroute knows it has arrived and can stop.

Reading real output

On a real machine you run traceroute example.com (macOS and Linux) or tracert example.com (Windows). Real tools usually send three probes per hop and print the round-trip time of each, so a line shows three times. A few things are normal and not a sign of trouble:

  • Lines with * * *. Some routers are set not to send "Time Exceeded" messages, or a firewall blocks them. The packets still pass through; you just do not get a name for that hop.
  • A time that jumps up and then falls again. A router treats answering your probe as low priority work, so a single slow reply does not mean the traffic through it is slow.
  • Different routes on different days. The internet picks paths dynamically, so two runs can list different routers.

By default traceroute gives up after 30 hops. Also, the reply addresses show the router on the path you probed; the return trip is not guaranteed to take the same road.

So what for your own code?

Next time a site feels slow, traceroute helps you answer a useful first question: is the delay near me, or far away? If the time jumps at hop 2, the problem is probably your connection to your provider. If it jumps at hop 12, the slowness is somewhere in the middle of the internet, and nothing you change at home will fix it. It is also a good reminder that "the internet" is really a chain of ordinary machines, each doing one small job: subtract 1, forward.

$ ls networks/

see all →
networks/nat-how-a-dozen-devices-share-one-internet-address.md NAT: how a dozen devices share one internet address Your home has one public internet address but a dozen devices. NAT is how your router lets them all share it and still get each reply to… 5 min read · 26 Oct networks/diffie-hellman-how-two-strangers-agree-on-a-secret-while-everyone-list.md Diffie-Hellman: how two strangers agree on a secret while everyone listens How can a browser and a website that have never met agree on a secret key while everyone on the Wi-Fi is listening? The trick is… 5 min read · 20 Oct networks/tcp-how-a-network-that-drops-data-still-delivers-every-byte-in-order.md TCP: how a network that drops data still delivers every byte in order The internet underneath your apps drops data and makes no promise to deliver it. TCP adds the promise anyway: it numbers every piece, the… 5 min read · 17 Oct networks/cidr-and-subnets-what-the-24-in-192-168-1-0-24-means.md CIDR and subnets: what the /24 in 192.168.1.0/24 means What does the `/24` in `192.168.1.0/24` mean? It counts how many of an IPv4 address's 32 bits name the network, and how many are left to… 5 min read · 14 Oct networks/dns-lookups-why-a-website-is-slower-the-first-time-you-visit.md DNS lookups: why a website is slower the first time you visit Typing a name like www.example.com sets off a short chain of questions: a resolver asks a root server, then a .com server, then the… 5 min read · 11 Oct networks/latency-vs-bandwidth-why-a-faster-plan-can-t-speed-up-a-small-page.md Latency vs bandwidth: why a faster plan can't speed up a small page You upgraded to a much faster internet plan, yet a small web page loads just as slowly. Why? Network speed is two different numbers… 5 min read · 8 Oct