< Back
# Network Address Translation. For more indepth detail, please checkout the referneces. For human language, please continue reading the post. > References : https://docs.microsoft.com/en-us/azure/rtos/netx-duo/netx-duo-nat/chapter1 Network address translation (NAT). Let's not start from what it is, instead, let start with a basic senario. ```plantuml @startuml rectangle "Home" as home{ rectangle "Private Network" as pw{ rectangle "laptop A" as bw } rectangle router } rectangle "ISP" as isp cloud internet note bottom of bw localIP address 192.168.1.104 end note note bottom of router IP address w.x.y.z end note bw -right-> router router --> isp isp -right-> internet @enduml ``` When laptop A wants to connect to internet. It goes through router, isp then to the internet respectively. There is a question here. 1. that local IP is something the internet doesn't understand Internet only knows the legit public IP which ISP gives us when we buy their internet package. So they install router device in our home, say we have only one public IP to use here. It may be the case that we can get a few more public IP with some extra price but not too many of them. So we assign public IP to `laptop A` thing should work. In reality, there are multiple devices connect to router such as mobile phone, laptop, pc, tv. The picture will be ```plantuml @startuml rectangle "Home" as home{ rectangle "Private Network" as pw{ rectangle "laptop A\n IP 192.168.1.104" as la rectangle "laptop B\n IP 192.168.1.105" as lb rectangle "laptop C\n IP 192.168.1.106" as lc rectangle "Mobile D\n IP 192.168.1.107" as md rectangle "Mobile E\n IP 192.168.1.108" as me rectangle "TV F\n IP 192.168.1.109" as tf } rectangle router } rectangle "ISP" as isp cloud internet note bottom of router IP address w.x.y.z end note la ---> router lb ---> router lc ---> router md ---> router me ---> router tf ---> router router ---> isp isp -right--> internet @enduml ``` **Problem** we have only 1 public IP, how could we possibly have multiple devices connectd to the internet as if each of them own the router exclusively? **Solution** To explian this in a simple word, we map source and target(destination) in pair e.g - laptop A call watermelon.com - laptop B call orange.com when watermelon.com returns back to router, rotuer will reply to laptop A as it knows previously that laptop A was the caller for the particular target. The same goes to orange.com will reply back to laptop B. --- **New Problem** Now new problem arise. what if multiple devices are calling watermelon.com at the same time? The router would confuse to match branches of response with branches of requests **Solution** We still use the mapping approach though. When problem can't be solve with small set of variables, then we need addition variable just enough to solve the problem. Sound familiar, yup, the system of equations we learnt at hight school i.e. to solve x and y we need 2 different equations to reveal them. Consider `192.168.1.104` and `192.168.1.105` talks to `www.example.com` Here is how it work. `192.168.1.104` uses port `6372` to `example.com`. `192.168.1.105` is also using port `6372` to `example.com`. ```plantuml @startuml map "Device A" as a { Source IP => 192.168.1.104 port => 6372 } map "translated A" as translateA { Source IP => w.x.y.z port => 2015 } map "Device B" as b { Source IP => 192.168.1.105 port => 6372 } map "translated B" as translateB { Source IP => w.x.y.z port => 2016 } map "Device A" as a { Source IP => 192.168.1.104 port => 6372 } map "NAT" as nat { 1 *-> translateA 2 *--> translateB } a -> nat::1 b -up-> nat::2 @enduml ``` *translated A* and *translated B* are used to call example.com on port either 80 or 443. Hence the request will be - `w.x.y.z:2015` => example.com:443 - `w.x.y.z:2016` => example.com:443 When the results came back, if it returns to `w.x.y.z:2015` NAT would know how to convert it back to the private address e.g - `w.x.y.z:2015` => `192.168.1.104:6372` - `w.x.y.z:2016` => `192.168.1.105:6372` Resources - https://www.comptia.org/content/guides/what-is-network-address-translation - https://docs.microsoft.com/en-us/azure/rtos/netx-duo/netx-duo-nat/chapter1