How does a gateway identify the destination PC when the response comes back to it?

Suppose there is an organisation which has many computers. All these computers have a specific range of IP addresses, and they are all connected through a gateway router.

How does the gateway router differentiate between the computers when sending the requests out of the company, and returning the responses to the company computers? In my understanding, a router acts as an entry and exit point for an internal network.

  • The different connections are separated from each other by 'sockets'. – arkascha Oct 18 '13 at 6:52
  • Did any answer help you? if so, you should accept the answer so that the question doesn't keep popping up forever, looking for an answer. Alternatively, you could provide and accept your own answer. – Ron Maupin Aug 8 '17 at 14:45

Making a few assumptions

  • You are talking about an Internet gateway router
  • You are using RFC1918 addresses on the inside
  • You have a single IP assigned to you from your ISP

With all of this being the case, Your gateway router is going to be performing PAT [Port Address Translation]

As a request goes through your gateway from a client it has a few important pieces of information. Source IP, source port, destination IP, destination port, all of which Your router will keep track of all of this in a table [Translation table].

So lets look at an example. In this example your IP that your router has from your ISP is

SRC IP = SRC Port = TCP/12345 ==> DST IP DST Port TCP/80

As this travels through the router it will translate the source info.

SRC IP = SRC Port = TCP/54321 ==> DST IP DST Port TCP/80

When responds it will respond like this.

SRC IP SRC Port TCP/80 ==> DST IP = DST Port = TCP/54321

Your router will look at its translation table and discover that TCP/54321 was the source port of a connection that initially came from with an original source port of TCP/12345.

The router will the rewrite the packet/segment and send it back to the inside host.

|improve this answer|||||

Plain and simple ...

In computer networking, a default gateway is the device that passes traffic from the local subnet to devices on other subnets. The default gateway often connects a local network to the Internet, although internal gateways for local networks also exist. Internet default gateways are typically one of two types:

On home or small business networks with a broadband router to share the Internet connection, the home router serves as the default gateway.

On home or small business networks without a router, such as for residences with dialup Internet access, a router at the Internet Service Provider location serves as the default gateway. Default network gateways can also be configured using an ordinary computer instead of a router. These gateways use two network adapters, one connected to the local subnet and one to the outside network. Either routers or gateway computers can be used to network local subnets such as those in larger businesses.

Source 1 - Source 2

|improve this answer|||||
  • assume that two systems are connected to a modem. stackoverflow.com website is opened from both the systems...How does the modem identifies which request has come from which machine,while sending response to the above request??? – ravigs Oct 18 '13 at 8:58
  • Modem will add a custom portnumber to the header so it knows which machine opened which session. Covered by NAT. – CustomX Oct 18 '13 at 9:02
  • Hi thielemans,Could you please elaborate your above answer??? why do we need custom port no??what kind of translation is done over there?? – ravigs Oct 18 '13 at 9:13
  • If you have 5 PC initiating a connection to the internet they use port 80. How will the router know which traffic is destined for which PC. Therefor the router will add a unique port value for that current session. When the traffic comes back, it will contain that unique port number and the router will be able to forward the traffic to the correct PC. – CustomX Oct 18 '13 at 9:17
  • @ravigs, you can find more information on PAT here – CustomX Oct 18 '13 at 9:26

A router has a routing table (statically set, or defined automatically via some routing protocol), which contains the data where to send the packet. In this table it maintans entries for destination networks, so it knows where to send packets with some destination number:

network1 - send via gateway1
network2 - directly connected to interface2
network3 - send via gateway3
default  - gateway4 (default gateway)

So when a packet comes to an interface, it checks the destination IP, see if it matches any of the networks defined in it's routing table, and sends the packet there. If it doesn't match any of the defined routes, it sends it via the default route/gateway ("all other destinations"). If the default route/gateway is not set, it sends back the "destination unreachable" icmp message to the packet source.

If you have NAT turned on, it also keeps a NAT table, where it keeps all the connections. So when your internal PC with an ip from a private range (eg sends a packet outside, the router replaces the private source address, in the IP header, with a public one, assigned to the router, and stores the connection in the NAT table. So when a packet comes back, to the routers public interface, it checks the NAT table to see where it should send it, and changes the destination IP (public one on the router) to an internal one (eg.

|improve this answer|||||

Your Answer

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