Traceroute maps the path to a destination like 8.8.8.8 by exploiting the IP TTL field. Every router decrements TTL by one. When TTL hits zero, the router sends ICMP Time Exceeded (type 11, code 0) back to you.
Incremental TTL Strategy
Send packets with increasing TTL starting at 1:
TTL 1: expires at first hop (e.g. 192.168.1.1)
TTL 2: expires at second hop
TTL 30: reaches destination 8.8.8.8
When the destination is finally reached:
UDP traceroute: ICMP Port Unreachable (type 3, code 3)
ICMP traceroute: Echo Reply (type 0)
TCP SYN traceroute: SYN-ACK or RST
Setting TTL with Raw Sockets
c
Loading…
Standard sockets may not expose TTL control on all platforms. Raw sockets or IPPROTO_ICMP sockets let you set the value directly.
Why This Exercise
This task asks you to document traceroute logic with hop numbers and expected ICMP responses. Van Jacobson's original 1988 traceroute used exactly this TTL-expiry behavior, and network engineers still rely on it to localize latency or packet loss in a path. The mtr tool on every Linux box still increments TTL and reads ICMP Time Exceeded messages the same way.
Implement a C program that runs the traceroute state machine over a supplied sequence of ICMP replies.
Input (stdin, whitespace-separated):
max_hops, the TTL you are willing to go up to (1..64)
Then one reply type per probe, in hop order:
11: ICMP Time Exceeded (an intermediate router; the trace continues)
3: ICMP Destination Unreachable (port unreachable; the UDP destination answered)
0: ICMP Echo Reply (the ICMP destination answered)
-1: no reply before the probe timeout (trace aborts)
Requirements:
Process probes starting at TTL 1, incrementing per probe, exactly like traceroute
Time Exceeded prints the hop and continues; Echo Reply / Destination Unreachable print the hop and finish the trace; timeout prints the hop and aborts
If the trace finishes (any terminal reply) print the "trace ended" line with the hop number; if max_hops probes all return Time Exceeded without a terminal reply, print the "not reached" line
Print exactly the lines specified below, nothing else
Output format:
Per hop: "hop N time-exceeded" | "hop N dest-unreachable port-unreachable" | "hop N echo-reply" | "hop N timeout"
Terminal line: "trace ended at hop N" or "destination not reached within H hops"
Success Criteria:
Type 11 keeps the trace alive; type 3 and type 0 both terminate it at the destination
A timeout is a distinct outcome from a reply: the trace stops there
The hop counter reflects the TTL that produced each reply