Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

4 Answers

up vote 3 down vote accepted

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 10.1.2.3.
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) 10.1.2.3:80

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

share|improve this answer
    
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. –  ring0 Jan 10 '11 at 13:34
add comment

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

share|improve this answer
    
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
add comment

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)

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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