Jump to content

Networking: How Does A Packet Know Where To Go?


BorysSokolov

Recommended Posts

I'm trying to learn more about the hardware aspect of web development, and networking - particularly TCP/IP, and I figured there must be an expert on internet infrastructure somewhere on these forums :) I've read up a little on the subject, but so far all I've got is a bundle of random information that don't seem to "click" together, so I'm looking for someone to fill in the gaps.

 

The main question I have is, how exactly does a computer know where to send an internet request?; since that's a pretty broad question I'll just list what I know so far, and if you find an an error, or if I'm missing something, please let me know.

 

Alright, so suppose I am on my local PC, and I'm trying to access a website. I type in www.randomsite.com, and the computer contacts the DNS server to fetch the domain's IP address. Let's say it's 108.50.200.198. At this point, the computer knows the public IP of the destination, but still hasn't figured out where exactly the address lies. So, it sends the information somewhere (or is this still done within the computer?), where it is determined whether the destination IP is on the local network, or outside the network. If the address is calculated to exist within the LAN, then the packet is labeled with the MAC address of the destination and sent to the correct host; however, if the destination exists outside the local network, the packet is handed over to the default gateway, that then routes it through, onto the internet, where it is then the ISPs job to get it to its correct address. Once the packet reaches the destination network, a port (in this case most likely port 80) forwards it to the host, which packs it  with the requested information (does it make a new packet or just reuse the old one?), and sends it back to the source network. Then, my router receives the packet, and somehow remembering which device requested it, sends it back to my computer, displaying the website on my screen (obviously, more than once packet would need to be sent).

 

Phew...is this at least the basic concept? Or am I missing something?

 

 

Thanks in advance.

Link to comment
Share on other sites

The main question I have is, how exactly does a computer know where to send an internet request?

It all has to do with the routing table of the computer. Basically the sequence of events goes roughly like this:

1) The OS's TCP/IP stack examins the destination IP and attempts to match it up to some entry on the routing table. Essentially this is the 'Is it local or not' step.

2) Assuming the address is not local, it typically goes to the default gateway. If you have a LAN, then it would end up at your local router, which most likely would just send it on to the ISP's router. If you don't have a LAN, then it'd just go directly to the ISP's router.

3) The router goes through a process similar to what your PC did. It looks at the destination IP and matches it to a route in it's routing table. If it matches one, it will send the packet down that route. If it can't find a match then the packet goes up to the next router in the chain. This process of passing up to a higher-level router repeats until a match is found (or there are no more possible routes).

 

As for how the PC and router determines which route the IP is a part of, this has to do with the Netmask. A route basically defines an IP prefix and specifies which port belongs to that prefix. So for example say your ISP's router had 5 ports, each connected to another network.

Lets assume the ports are connected like so:

 

Port 1: 192.168.10.100/24

Port 2: 192.168.20.100/24

Port 3: 192.168.30.100/24

Port 4: 192.168.40.100/24

Port 5: 192.168.50.100/24*

 

 

The /24 part in the above is the netmask, specified using CIDR notation. With the values I chose it basically splits the IP at the last dot so the first 3 sections denote the network, and the last denotes the host. Assume the * denotes the default route/gateway

 

If the router gets a packet destined for 192.168.45.203 then it would attempt to match that IP up to it's various routes. First it will take that destination IP and break it down to just the network portion, which is: 192.168.40. Then it will go through it's routing table looking for a matching network entry. In this case, it finds one on Port 4, so it would forward that packet out to whatever is connected to Port 4.

 

If the router would have been unable to find a match (say the destination IP was 192.168.100.48), then it would forward the packet to it's default gateway, which in this case is 192.168.50.100. Then that router would repeat the process until a valid route was found.

 

Once the packet reaches the destination network,a port (in this case most likely port 80) forwards it to the host

Not exactly. The IP is what determines the host. The port indicates the service. So it would make it to your computer by IP address alone. Once it has reached your computer, the OS accepts the packet and examins it.

The OS looks at what service (port) the packet is destined for and then checks it's records to see if any process is expecting packets destined for that service (ie, apache). If so, it will pass the packet on to that process

which then pickes it up and does whatever. If the OS can't find any processes that will accept that packet, it generates an error response and sends that back to the original packet sender.

 

 

That is essentially the super-simplified basic understanding of how networking works. There is potentionally a lot more going on, such as NAT's, Firewalls, Tunneling, etc. NAT's may be causing you some confusion, evidenced by a couple things you mentioned: "a port ... forwards it to the host"/"and somehow remembering which device requested it".

 

Involving NAT is what introduces the need for port-based forwarding of packets, which requires the device to remember where packets originated from. If you're behind a NAT, when you send a packet out, the router will take note of that packet's source IP and port #. It then re-writes that bit of the packet with it's own IP and port # and sends the packet up the chain. When a reply packet is recived the router will check it's destination port# and try to match it up to it's list of sent port numbers. If it finds a match it will re-write the packet with the proper destination IP and port and forward it on. If it doesn't find a match the packet is usually just dropped.

Link to comment
Share on other sites

If the router gets a packet destined for 192.168.45.203 then it ...

Correction: 192.168.45.203 should actually be 192.168.40.203 in the example. 192.168.45.203 would not match any of the routes and end up going to the default gateway.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.