NAT: If two hosts initiate a connection to the same IP:PORT, with the same source PORT, how does the router handle it

nat;networkingrouter

I was just thinking and that idea came to my mind.

Let say I have hosts A and B inside my local area network. I have a Router, and a host C in the internet. I know that when initiating the connection, the source port is chosen randomly, but what happens if two hosts chose the same when initiating to the same host:port?

A:10123 -> C:80
B:10123 -> C:80

How would the router know which packets belongs to each one?

Cheers.

Best Answer

Just because hosts A and B chose port 10123 doesn't mean that is the port that the router uses. The router connects to host C from a source port of it's own choosing. So the connection looks more like this:

                                  router
                          +--------------------+
A:(src:10123, dest:80) -> | A:10123 -> C:43775 | -> C:80
B:(src:10123, dest:80) -> | B:10123 -> C:43776 | -> C:80
                          +--------------------+
Related Topic