3

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?

4

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.

6
  • 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? – Lou Jun 26 '17 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 '17 at 21:51
  • 1
    But 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? – Lou Jul 6 '17 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 '17 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. – Lou Jul 11 '17 at 23:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.