Search code examples
network-programmingnat

Dynamic Network Address Translation


The question I'm asking is probably very elementary, but It is about the subject of DNAT in Networking. Imagine we have a LAN with 2 computers that both of them want to connect with a single server, outside of LAN (Internet). My question is how our Home router or NAT Server can differentiate received packets from that Server for two computers if two computers are requesting from the same server when translating IP addresses. What Information Help DNAT to Forward Packets to The Right PC?


Solution

  • NAT devices use layer-4 identifiers to map the answers back to the hosts in the LAN. For TCP/UDP it uses port numbers, although different types of NAT will use these port numbers differently.

    Wikipedia has a nice diagram of what a NAT table looks like: NAT translation

    Note the port numbers in the NAT translation table.

    For protocols other than TCP/UDP (i.e. protocols that have no layer-4 port numbers) other identifiers are used for the mapping. For example, for ICMP pings the identifier field is used.