There are roughly 15 billion devices connected to the internet, but only 4 billion IPv4 addresses. How is this mathematically possible? The answer is Network Address Translation (NAT) and RFC 1918.
In 1996, the internet engineers realized we were running out of addresses. They reserved three blocks of IPs exclusively for private networks. These addresses are unroutable on the public internet:
Your laptop, your phone, and your smart TV probably all have 192.168.x.x addresses. But when you load a webpage, the web server doesn't see your private IP (because it can't route data back to it). It sees your router's public IP.
When your laptop sends a packet to the internet, the router intercepts it. It changes the Source IP from your private IP to its own public IP, and changes the Source Port to a random port it generates. It then records this swap in a NAT Translation Table.
When the web server replies to the router's public IP and port, the router checks its table, rewrites the destination back to your laptop's private IP and port, and forwards it to you.
Click to send a request from an internal device. Watch how the router translates the private source address into its own public address using the NAT table.
| Internal Address (Private) | Mapped Address (Public) | Status |
|---|
NAT is the duct tape that kept the IPv4 internet alive for an extra 20 years. However, it completely breaks peer-to-peer protocols. If two laptops on different home networks try to talk directly to each other, both are hidden behind NAT routers that will block incoming traffic unless a translation rule was already established. This is why technologies like STUN and TURN exist for WebRTC and gaming.