Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

nat
private ip
public ip
ipv4

What is NAT?

Educative Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

NAT stands for Network Address Translation. The router uses it to translate a private IP address to a public IP address, and vice versa.

Since there's a small number of IP addresses available in IPV4 (2^32 ~= 4.3 billion) as compared to the number of devices connected to the internet, the scheme of internal mapping is required where multiple devices are mapped to one public IP. Internally, each device is assigned a private IP from the reserved private space of 10.0.0.0/8.

Assigning private IP addresses to PCs

When the device wants to communicate to the internet, it sets the source IP address to its internal private IP address and routes the packet to the NAT server.

Usually, the NAT is installed on the router, where the router’s interface is connected to the internet via a public IP address. The NAT server replaces the source IP address of the packet with its public IP address and makes an entry in the NAT table against this mapping.

Example

Let's say a packet is sent from the PC 6 to any network X, as shown below. Here, the header of the packet contains the source address 10.0.0.6 with the port 3300, and the destination address 192.168.1.10 with port 78. This reaches the router where the NAT makes updates to the packet. An example of the packet traversing the NAT-based router before its transmission to the internet has been given below.

The packet is sent from PC# 06 to the router with the source IP address "1.0.0.6" and port "3300"
1 of 4

As shown above, when a packet is received at the router, the NAT replaces the source address 1.0.0.6 with its public IP address 85.13.217.9. NAT also replaces the source port number 3300 with a new port number 5454 that is available at the NAT’s public interface.

When the target device responds, the NAT translation table assists us in identifying the local address of the destination device. The target device sends a packet to the router with the destination address 85.13.217.9 and port 5454. NAT consults the NAT-table against the entry 85.13.217.9,5454 and replaces the destination IP and port number in the incoming packet with 1.0.0.6 and 3300, respectively. The packet is then routed internally to the destination host.

Advantages

  • It allows us to connect a large network of devices to the internet using a single IP address.
  • The local addresses are hidden from the outside network, which increases the security of the private network.

RELATED TAGS

nat
private ip
public ip
ipv4
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring