Most content in this document is based on "Computer Networking: A Top-Down Approach" by Jim Kurose.
Network Address Translation (NAT) is a technique used by routers to map multiple private IP addresses in a local network to a single public IP address. This approach was developed to mitigate the scarcity of IPv4 addresses by allowing local hosts to share one public IP for internet access. NAT works by transparently switching IP addresses and port numbers in packet headers, maintaining a translation table to ensure that incoming responses are correctly routed back to the original host in the local network.
NAT refers to a IP header manipulation in packets conducted by a router. The source private IP of the outgoing packet is switched to the router’s public IP address at the router. In the same manner, the router switches the destination IP address of the incoming packet to the destination host’s private IP.
There are exactly 2^32 or 4,294,967,296 total IPv4 addresses. If the number of devices(hosts) existing at this moment is less than this number in the internet, each devices can be assigned a public IP address and devices can communicate each other with the assigned IP address.
The reality is the number of devices that wants to connect to the internet keeps growing, making the IP address scarce. To overcome the limited supply of IP address, people have decided to divide IP addresses into two types:
10.x.x.x)172.16.x.x)192.168.x.x)To make use of scarce public IP address as efficiently as possible, people have decided to assign a public IP addresses to the router in a local network and let it represent other hosts when connecting to the internet. In other words, the router is the only device assigned with a public IP in a local network.
192.168.0.2 request a web page on some web server (port 80) with 128.1.2.3. The host 192.168.0.2 assigns the (arbitary) source port number 3345 and sends the datagram into the LAN.5001, and replaces the original source port number 3345 with the new source port number 5001. The NAT router also adds an entry to its NAT translation table.5001.192.168.0.2) and port (3345) for the browser in the home netowrk, then rewrites the datagram’s destination address and port, and forwards it into the home network.