When my PC sends a request to a server, the transmitted IP address is the one of my private network. When the server responds to the request, how does the router know to which PC in the network to send the response?
1 Answer
The NAT process maintains the translations tables. When the client sends to the server, NAT creates a table entry which saves the fact that the client IP address and port sent something to the server IP address address and port. When something comes back from the server address and port to the public address and NAT selected port, NAT looks it up and sees that it should go to the client address and port.
-
Thanks, but what happens if two local devices choose the same ephemeral source port while connecting to the same target IP and port (e.g. google.com:80)? Does router then change the source port number too in order to distinguish the two sockets, and then rewrites it back when forwarding to the right PC?– LouJun 26, 2017 at 21:29
-
There are several forms of NAT. What you are implying is called NAPT (Network Address Port Translation), which translates both the layer-3 and layer-4 network addresses. In fact all four addresses (source and destination IP and TCP/UDP/ICMP) addresses combine for a NAT table entry.– Ron Maupin ♦Jun 26, 2017 at 21:51
-
1But isn't this common for all routers? If I have two computers behind a router and they both visit the same web page, then on the TCP level they are likely connecting to the same IP:port tuple? So I don't understand what you meant by "what you are implying", I thought this was the general, everyday case for any router out there?– LouJul 6, 2017 at 9:25
-
The default for all routers is no NAT. Unless your two PCs both have the same source address and are using the same source port, both of which are highly unlikely, then, no, they do not have the same four addresses in the connection with a server in common, NAT or no NAT.– Ron Maupin ♦Jul 6, 2017 at 15:34
-
They won't have the same source address, but the source port might get equal. Perhaps I am mistaken, but I thought router will rewrite the IP packet and place its external source address in the IP packet. So when the response packet arrives, it will be addressed to the router, not to the internal machine's IP. This means that router needs to forward the packet based on the port number only, unless the response packet somehow contains the local IP information.– LouJul 11, 2017 at 23:20