I have a basic networking question. Two PCs are communicating on a WLAN. Both the PCs are behind routers or modems.

Both the PCs have the same public IP. That public IP is most of the time is either of the router or of the modem. There can be more then one PC behind those routers and modems. How are the PCs are communicating? I can understand that packets can reach those routers or modems and then the Internet, but what after that? In the packet, the destination IP is set as the public IP. When the packet returns, how does the router or modem decide where to send the packet? Can anybody explain me this please.


Most of the mechanics behind this mystery is NAT-masquerading:

  • Your PC communicates with outside

for instance your PC is on a private network, with IP
Your router remembers the PC IP/port, and that it initialized a connection to a remote IP/port (WAN). Thus, when there is a reply on the same channel, the router forwards back the packet to the PC, on its LAN side.

  • An external device communicates with your PC

In this case, the ext. device initializes the connection, and thus the router doesn't know to which 10.x.x.x client it is supposed to forward the packet.
This is why the router needs a specific settings that says, for instance if your PC happens to be a Web server (http port)

  HTTP (WAN) ==> (LAN)

that says, the packets coming on the HTTP port go to the internal LAN address (your PC) on port 80.

  • Thanks for your help. Can you please tell me about any rfc related to this? – user58859 Jan 10 '11 at 11:20
  • Please refer to the Wikipedia link at the top of the answer. A few RFCs can be reached from the article. – Ring Ø Jan 10 '11 at 13:34

In a basic setup a device always only knows the next hop. This one also just knows the next hop to the desired destination. if the destination is not local to the host/router it sends the packet to the default gateway and so on and so forth.

in more complex situations you can have static routes pointing to an explicit gateway for a certain network destination.

if it gets too complex to manage those routes manually, dynamic route distribution protocols are used like RIP

even more elaborate protocols know more about the global distribution of ip blocks (via AS Numbers) and try to make decisions on where to route packets on cost and distance. examples are BGP and OSPF

  • sorry for -1, but this is not what I asked. – user58859 Jan 10 '11 at 11:08
  • as far as i can see your only sentence with a questionmark is: "Then how the router or modem decides where to send the packet?" and I thin i answered that! – lepole Jan 10 '11 at 12:30
  • Read the question again.I am talking about transfer of packet from router or modem to internal lan. No concept of hops here. – user58859 Jan 10 '11 at 18:04

I'm a bit puzzled by your "most of the time": I think it's either always or never. If the public IP of a PC is always coinciding with its router/modem, then the local network is using NAT to give access to the PC from outside the local network. => see first answer

If the public IP of a PC doesn't coincide with its router/modem, then the local network is directly accessible from the WAN (if we ignore possible firewalls that can block the way). => see second answer

If you are just starting to understand how routing works, you should probably first concentrate on the latter scenario, and maybe also simplify it as:

PC1 <-LAN1-> Router1 <-WAN_link-> Router2 <-LAN2-> PC2 

(like there would not be anything else around)


Good question. There are actually a few strategies, but essentially the router keeps a table of all NAT'd connections (client port, client IP, destination IP, destination port). When a return packet comes back it matches which client to forward it to based on where the packet has come from, and from which port.

Most NAT implementations are also PAT (port address translation implementations), to prevent two clients picking the same source port.

Some routers, e.g. Cisco devices do sequence number randomization for security reasons - it can help hide how many and what types of clients are behind the NAT. It can also try to protect the clients against types attacks that target the sequence number, or guess the next sequence number for the next connection.

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.