Addresses, routing, ports and NAT
How a packet finds one machine among billions, then one program on that machine, and why the box in the middle that rewrites addresses silently kills idle connections.
An IP address names a network interface. IPv4 addresses are 32 bits, written as four bytes such as 203.0.113.7. IPv6 addresses are 128 bits, such as 2001:db8::7. Addresses are grouped into prefixes: 203.0.113.0/24 means every address whose first 24 bits match, 256 addresses in all.
Routing: one hop at a time
No router knows the full path to your destination. Each one has a routing table mapping prefixes to a next hop and picks the most specific match, the longest prefix. Your laptop's table is usually one line: everything goes to the home router. Internet backbone routers learn hundreds of thousands of prefixes from their neighbours through BGP.
Every IP packet carries a TTL, a hop counter that each router decrements. At zero, the packet is dropped and the router sends back an ICMP error. traceroute exploits this: it sends packets with TTL 1, 2, 3 and so on, and each router that drops one reveals itself.
# Each line is one router on the path, with three round-trip samples
traceroute api.anthropic.com
# The kernel's routing decision for one destination
ip route get 1.1.1.1 # Linux
route -n get 1.1.1.1 # macOSRows of * * * usually mean a router that does not answer ICMP, not a broken path.
Ports: which program
IP gets a packet to a machine. A 16-bit port gets it to a program. A connection is identified by its 4-tuple: source IP, source port, destination IP, destination port. Servers listen on well-known ports such as 443. Clients pick a temporary ephemeral port for each outgoing connection, from a range that on Linux defaults to 32768 to 60999.
NAT: sharing one public address
Your home network uses private addresses such as 192.168.1.20, which mean nothing on the internet. The router performs network address translation: it rewrites the source of each outgoing packet to its own public IP and a port it chooses, and remembers the mapping in a table. Replies arriving at that public port are rewritten back and delivered inside.
A NAT table, and how an idle stream dies
| Inside | Outside (public) | Destination | Last packet | State |
|---|---|---|---|---|
192.168.1.20:51544 | 203.0.113.7:40001 | api.example.com:443 | idle 3 s | active |
192.168.1.20:51545 | 203.0.113.7:40002 | cdn.example.net:443 | idle 58 s | expiring |
192.168.1.20:51546 | 203.0.113.7:40003 | tools.example.com:443 | idle 75 s | expired |
The mapping table is state, and state must be cleaned up. NATs, cloud NAT gateways, firewalls and load balancers all expire entries that have been idle too long. Timeouts vary widely: UDP mappings often last only 30 seconds; cloud NAT gateways and load balancers commonly expire TCP after a few minutes; home routers vary.
- Agent starts a long tool run
- Server streams progress, then goes quiet while the tool works
- Path includes a load balancer
- Idle timeout 60 seconds (a common default)
- At 60 s of silence
- The balancer drops the connection state. Neither client nor server is told.
- At 5 min the tool finishes
- Server writes the result. The balancer has no state, so it resets or drops it.
- Client side
- Either a reset (
ECONNRESET) or, if packets are silently dropped, a hang until a timeout
Fix: send application heartbeats more often than the shortest idle timeout on the path, such as an SSE comment every 15 to 30 seconds.
Go deeperTCP keepalive versus application heartbeats
TCP has a built-in keepalive: after a period of silence, the kernel sends an empty probe that the peer must acknowledge. On Linux the default idle period is two hours, far longer than any middlebox timeout, so it does nothing useful unless you lower it with TCP_KEEPIDLE or the setKeepAlive(true, ms) call in Node.
Even when tuned, TCP keepalives only prove the TCP path is alive. They pass through some proxies without resetting the proxy's own HTTP-level idle timer. An application-level heartbeat, such as an SSE comment line or a WebSocket ping, travels through every layer and keeps every timer fresh. Prefer it for long streams.
Where you will meet it
AWS Application Load Balancers default to a 60-second idle timeout. Long-polling and streaming endpoints raise it or send heartbeats.
A NAT gateway has a limited number of ports per destination. A service that opens a new connection per request to one API can run out and see connection failures.
Carrier-grade NAT on cellular networks often uses short timeouts, which is why mobile apps lean on push notifications rather than long-held sockets.
Words to know
- Prefix
- A block of addresses sharing the same leading bits, such as
10.0.0.0/8. - TTL
- A hop counter in every IP packet. Routers decrement it and drop the packet at zero.
- Ephemeral port
- A temporary client-side port chosen for one outgoing connection.
- NAT
- Network address translation: rewriting addresses and ports so many devices share one public IP.
Check yourself
Answer out loud before you open each one.
What identifies a TCP connection?
The 4-tuple: source IP, source port, destination IP and destination port.
A quiet five-minute stream died with no error. Why?
A NAT, firewall or proxy on the path expired its idle state and dropped the connection silently.
Why are TCP keepalives often not enough?
The default interval is hours, and they may not reset idle timers in HTTP-level proxies. Application heartbeats travel through every layer.
Try it
Open a connection with nc example.com 80, wait ten minutes without typing, then type GET / HTTP/1.1, press Enter twice and see whether anything comes back. Repeat on a different network.