Day 6 — CS Fundamentals December — About Networking — Network Address Translation

The last article under CS Fundamentals December was about IP addresses, in general. And it mentioned about one thing that IPv4 addresses are not enough and are being outnumbered by the number of devices on Earth.

So, in this article, I will be writing about Network Address Translation and how it saved IPv4’s a$$.

Let’s dive in!

Introduction

In my last article, I mentioned about IPv6 addresses being one of the solutions to the problem of not-enough IPv4 addresses but IPv6 addresses have not been adopted to thaaat extent due to another solution and its convenience.

That solution is — Network Address Translation (NAT)

https://www.networkcomputing.com/sites/default/files/styles/flexslider_full/public/9-willy%2520wonka%2520IPv6_0.jpg?itok=NoJoLWqL

The good thing about this solution is that you still keep on using IPv4 addresses but you just change the way you use it.

This method helps in sneakily sidelining the requirement of each device having a unique IPv4 address so as to use the internet.

So, what I am gonna do is that I’ll explain NAT through an example of your home.

Let’s talk about your home

Your home has many devices. Your computer, your phone, your parents’ phones, etc.

Now natively, you might think that each one of them should have a unique IP address to use the internet but then, we have to deal with the problem of less number of IPv4 addresses as well, right?

So, enter your router (or MODEM, if you call it that :P)

So, you connect to your router at your home right for accessing the internet.

That router is, basically, the middleman in providing you the internet connection and it also performs this thing called Network Address Translation.

So, what it does is, it destroys the need to have unique addresses for each device in the home publicly.

It says, “You know what? All the devices connected to me can have unique IP addresses but those IP addresses need to be unique ONLY INSIDE THAT GROUP OF DEVICES CONNECTED TO ME

So, if a phone and computer are connected to a router, they can have IP addresses same as some other computer in your friends home but they (phone and the computer ) have to have different IP addresses from each other because they are connected to the same router.

This IP address which can be same with some other computer in some other home but has to be unique with the router is called Private IP address.

But how does this work, Yash!

Okay, so the moment, you connect your device to your router, the router sees the current list of devices connected to it and their corresponding IP addresses and then, provides this new device a private IP address which is different from the ones connected to the router.

“Okay, Yash! This is getting complicated. All I hear is ‘IP’, ‘Same’, ‘Different’, ‘Router’. Please teach better!”

Cool cool! Time for an example then :)

For example

When you connect your laptop to your router, the router checks the list of available private IP addresses and gives it to your laptop.

So, in this situation, let’s say your laptop gets the private IP address -> 172.16.0.0

Now, say, you connect your phone to your router, the router again tries to give a freely available private IP address to it, let it be, 172.16.0.1

Now, notice this, these IP addresses (172.16.0.0 and 172.16.0.1) can be taken up and used by anyone in this world. As I told, they are supposed to be unique “just” amongst the devices connected to that router, that’s it, and hence, they are called “private IP addresses”.

“But then, how will they communicate with some other computer on internet?”

Good Question!

So, the thing is that the private IP addresses are never used to communicate to outside-world computers. Being unique in their own group, devices connected to the same router can talk and communicate to each other using private IP addresses but not with any other computer NOT connected to that router.

So, they communicate by TRANSLATING the private IP address to public IP address when trying to send a NETWORK packet to the outside computer.

Who does that? Router.

So, let’s say, you want to check weather from yahoo weather website (yahoo server).

You send a request for that in the form of network packet, which has details like

Dude! I want to know the weather of Delhi. By the way, I am 172.16.0.0”.

This network packet is sent to router first. The router translates this sender’s private IP to an available public IP, say, 3.53.72.198 and changes the network packet to this,

Dude! I want to know the weather of Delhi. By the way, I am 3.53.72.198

and then, passes on the request to the yahoo server.

Now, yahoo server gets this request and responds with,

Okay 3.53.72.198 bro! Delhi’s weather is pretty messed up and it is 40 degrees.

and it sends it back to the 3.53.72.198 assuming that it is the sender.

But 3.53.72.198 leads to the router and the router sees this IP (3.53.72.198) and says,

“Aaah! I remember this IP from the routing IP table, I assigned it to the computer with private IP 172.16.0.0

and then, it changes the network packet to

Okay 172.16.0.0 bro! Delhi’s weather is pretty messed up and it is 40 degrees.

and passes the request to 172.16.0.0 and that computer receives this and hence, knows about Delhi’s messed up weather :P

Router knows which computer corresponds to which IP address (and port number, I’ll explain that) by maintaining a table inside it which it looks up to whenever it receives a request back from the internet.

That table is the NAT IP routing table.

This whole process of translating 172.16.0.0 to 3.53.72.198 and re-translating it back to 172.16.0.0 is called Network Address Translation.

How it saves IPv4’s a$$

This exact thing will happen with every device connected the router with the same public IP.

The next time, another computer/mobile/device, connected to the same router, tries to request any data from the internet, the router will assign the same public IP address 3.53.72.198 for it.

So, see, only one public IP address can be used by multiple devices (all the devices connected to that router) to access the internet.

Hence, reducing the need for unique public IP addresses for each device.

“Okay, final question, Yash! What if simultaneously two computers try to access the internet connected to that router. They both can’t have the same public IP address”

Another good question!

So, the truth is, only and only IP address does not entirely identify a computer. There is a thing called port number which legit identifies from where exactly the request is coming from.

Actually, any complete IP address looks like this -> 192.168.43.76:8000

Where, 192.168.43.76 is the IP and 8000 is the port number.

“But Yash! I have hardly seen any port number with IP and also in the last example of weather of Delhi, you did not mention anything about the port number :/”

Yes, if port number is not written, then the default port number corresponds according to the respective protocol is assumed.

For example, if the protocol is http and port number is not mentioned, then port number is assumed to be 80 and behind the scenes, the address would be read like 192.168.43.76:80.

if the protocol is https and port number is not mentioned, then port number is assumed to be 443 and behind the scenes, the address would be read like 192.168.43.76:443.

So, in the previous Delhi weather example, I did not mention that port number but that meant the port number was default port number i.e. 80 (considering http)

So, getting back to your question, if two computers try to simultaneously access internet through a router, so the router will assign them the same public IP address but on different ports.

So, computer 1 (172.16.0.0) to internet, would look like -> 3.53.72.198:8000

And, computer 2 (172.16.0.1) to internet, would look like -> 3.53.72.198:8001

Router knows which computer corresponds to which port number by maintaining a table inside it which it looks up to whenever it receives a request back from the internet.

That table is the NAT IP routing table.

This process of identifying different computers/applications on the basis of port number is called Port Address Translation (PAT) and it is a type of Network Address Translation technique.

Actually, PAT is most prominently used technique under NAT currently in the world.

Also, one last thing

What did the router say to the doctor when it went to the clinic?

https://www.networkcomputing.com/sites/default/files/styles/flexslider_full/public/10-IP%2520eel_0.jpg?itok=BJRcMZZO

Sorry XD

That’s it!

Phew! That was a pretty intense article!

Anyways, I really hope you understood the article. I’d prefer you to read it again, so as to reinforce the knowledge it is providing because that’s how NAT is, it is difficult to understand the first time but second time, you understand it beautifully.

Anyways, thanks a lot for reaching till here!

Find me on

LinkedInhttps://www.linkedin.com/in/yashvardhan-kukreja-607b24142/

GitHubhttps://www.github.com/yashvardhan-kukreja

Email — yash.kukreja.98@gmail.com

Adios!

Site Reliability Engineer @ Red Hat | ex-Grofers | Contributing to {Cloud, Kubernetes}-native OSS