4

I'm sorry if this is a really stupid question; my networking knowledge is pretty sparse.

So, a user makes a HTTP request to some address. That request comes from an address on a local subnet, but the router knows to send the request to its default gateway because the destination IP doesn't match anything else in its routing table. The request hops through the internet and is eventually received by the destination machine (a web server). That all makes sense.

But now, how does the web server manage to send the request back to the local machine? Didn't the request come from an IP address on a local subnet? Where did it pick up the IP address of the local machine's router? And even if it has that router's IP, how does the packet get routed to the local machine once it gets to the router?

ejk314
  • 143

3 Answers3

2

The packet has a source address as mentioned earlier, but also has a port number assigned. This is especially needed in a NAT environment (where the internal IPs are private and external IP is constant - like sharing the internet connection). The router assigns a random unused port number and keeps a table of who the port number refers to (the user's computer). When the packet is received by the router, it looks at the port number and matches and sends to the computer/user.

feel free to update/correct this if found wrong, but that is the how it works. Hope this helps.

1

The web server's response is handled just like the client's request. They're just IP datagrams.

The web server has a default gateway that it sends its response to. The response "hops through the internet and is eventually received" by the client that originated the request.

The web server "knows" nothing about the requesting client's router.

Edit:

Your comment betrays an assumption that I made.

You're talking about a scenario where the requesting client computer has a private IP address and is attached to a network with a Network Address Translation (NAT) router at the border of the client's network (where that network connects to the Internet).

In that case, the client computer's IP address will be translated, by the NAT router, into the a public IP address assigned to the NAT router's "external" interface. The NAT router keeps track of the request such that the response is routed back to the client when it is received from the Internet.

0

The answer lies in SPI - Stateful Packet Inspection. This is how a router keeps track of where to send a response on its subnet. I don't know all the nitty-gritty myself (I have never needed to know), but if you follow up the link you will find as much as you probably need to know. If not, a search on "Stateful Packet Inspection" will fill any gaps.

AFH
  • 17,958