So the private addressing scheme works really well for computers that only have to access resources
inside the network, like workstations needing access to file servers and printers and so forth.
Routers inside the private network and route traffic between private addresses with no trouble whatsoever.
However, to access resources outside the network, like the Internet, for example, these computers
need to have a public address so that responses to their requests return to them.
Now, this is where Nat comes into play.
A workstation inside a private network makes a request to a computer on the Internet.
The switches or the routers within the network recognize that the request is not for a resource inside
the network.
So they send the request to the router, let's say the backbone router.
Now the backbone router sees the request from the computer with the internal IP.
It then makes the same request to the Internet using its own public address and returns a response from
the Internet resource to the computer inside the private network.
From the perspective of the resource on the Internet, it's sending information to the address of the
router.
From the perspective of the workstation, it appears that the communication is directly with the site
on the Internet.
So when Nat is used like this, all users inside the private network that access the Internet have the
same public IP address.
So that means only one public address is needed for hundreds or even thousands of users.
So let's have a look and see how it works.
Here's a typical home network.
There's a laptop desktop PC, a smartphone all connected to our home router.
And the home router is, of course, connected to the Internet.
So when we register with our ISP, the Internet service provider, we get an IP address that is accessible
throughout the Internet, and that's pretty much assigned to that home router.
We refer to it as real IP or public IP.
Now suppose that the IP address 20.20.20.20 is assigned to our home router.
Our devices inside the home network also have IP addresses.
Right.
But in this case, they get private IP addresses which are assigned by the home router and are not accessible
from the Internet.
As you can see here, the public IP addresses are red and the private IP addresses are green.
So let's consider a connection request from the smartphone which requests the home page of ABC.com.
So to reach the page, the smartphone has to go through the home router.
The packet has the source IP address and the source port address, as well as the destination IP address
and the destination port.
If it arrives at the web server with these values, it processes the request and tries to send the reply
packet to the IP address.
192.168.1.5.
But that's unreachable for the web server because it's a private IP address.
So when the packet arrives at the home router, instead of sending the packet right over the internet,
the home router changes the source IP address with its very own public IP address.
It also creates a record in the Nat forwarding table.
This table allows us to know which packets will be redirected to the smartphone when they come in.
So moving right along the packet, travels over the Internet and arrives at the web server.
The web server creates a reply packet where the source IP address is itself, and the destination IP
address is the public IP address of the home router.
When our home router receives a response, it looks at the Nat forwarding table and replaces the destination
IP and the port according to the mapping inside the table.
And finally the smartphone receives the packet.
Pretty cool, huh?