How does the router know which device to send packets to?

15

my question does not concern how it moves through the internet, but how it moves through the router to a certain device. All devices connected to a router in a home network have the same external IP. Say device A is loading a page and packets are sent from an external source to the router because the packets know the external IP of device A and they are able to get to the router. But now, how does it get to device A? How does the router know to send it to device A instead of device B?

I think this involves the NAT, but i'm just looking for a logical explanation of what NAT does to accomplish this.

Dan Webster

Posted 2012-12-26T03:15:30.190

Reputation:

Question was closed 2014-12-16T22:30:13.843

I can't give you a very specific answer, but the router keeps track of where a request is going and who sent it. When the external source responds, the router simply checks to see which device attempted to contact the external source initially, and forwards the response to that device. If you need more information, look into NAT. – Jack Humphries – 2012-12-26T03:17:45.847

This question belongs to Superuser.com – TimothyP – 2012-12-26T03:18:16.780

Answers

11

When you initiate a connection out to an external site, the router is using a different source port number each time... it then uses that source port number (which is where the data returns to) as the key to look up which machine on your internal network the request came from. This is called PORT Address Translation (as opposed to Network Address Translation, which is when you have multiple network addresses available to your router).

Michael Bray

Posted 2012-12-26T03:15:30.190

Reputation: 226

1this would explain things like TCP connections, but some applications are connectionless. What about UDP packets? What if you're hosting a server on 1 device, even if it uses TCP for the connection, it wont be able to initialize a connection without the router knowing which device the server is on. – None – 2012-12-26T03:23:07.097

1For UDP connections you usually have to pre-define a port on your router that can be used to route to a specific device. This is the "port forwarding" feature of routers, and can be used either for incoming UDP packets (either in response to an outgoing message or a fresh incoming packet) as well as for incoming TCP packets (eg those initiated by a remote client instead of your own PC.) – Michael Bray – 2012-12-26T03:26:47.210

1

Normally, All device from Home network have different internal address (private address, always 192.168.0.1~192.168.0.255).

When a packet is send from Home network (let's say Device A: 192.168.0.123), when it pass through router, the router will translate the private address to router's external address (assume is 140.191.2.5), and the router also record this mapping in it's NAT.

When the response is back, the router mapping the NAT, and find the original source is Device A.

so the device A eventually receive the packet.

This is just a concept, for more detail, please refer wiki: NAT.

NCola.L

Posted 2012-12-26T03:15:30.190

Reputation: 21