How does a router track connections? When you send a http request it goes to the router which is then sent to another router, etc over the internet; but then how does it know to go to the server, is it port forwarded to a load balancer? Finally when the packet comes back how foes it know to go back to the pc because the packet was sent back to the router not the pc? Is it possible to establish a connection to a pc controlled by a router, from outside that router I.e. can you direct a packet to a pc not directly connected to the internet?
Networking – How does a router route connections
networkingroutertcp
Related Solutions
IP datagrams have a "Time To Live" (TTL) value in the IP header. Each time a router forwards a datagram, it decrements (subtracts 1 from) the TTL. When the TTL reaches zero, the router drops (deletes, does not forward) the datagram, and sends back an ICMP "Destination Unreachable, TTL Exceeded" message.
Also, when your router got a packet from a host on eth0 that would have been better delivered to another host or via another router that was also on the data-link layer network (in this case, the Ethernet LAN) out eth0, it could send an ICMP Redirect message to inform the local host that sent that packet that it should really have sent it directly to that other host or router on the same Ethernet LAN.
Overall, routing loops are avoided through good network design, and through using route advertisement protocols and route table maintenance procedures that avoid loops.
In a nut-shell, when a router doesn't know how to route a packet it'll send the packet to the default gateway/the next "hop".
Basically, when your router can't find a valid rules (or more formally, it's route table) it will send the packet to the default gateway, which will typically send it upstream to your ISP.
Once the packet arrives at your ISP's routers, much like your own router, they will have their own route tables. But this time they'll be more detailed. Knowing about other customers and other ISP's.
This will continue on until the final router that has the final destination rule attached to it, sending it onto the specific computer/Interface.
All along this way, the packet includes source information indicating where the original packet came from. Your router (likely, with NAT) would had converted this source information from your local machine's internal only IP address (e.g. 192.168.1.25) to your WAN IP address given to you by your ISP (e.g. 121.147.148.55) and this WAN IP will be what's contained within the packet's source information.
Thus, all the upstream routers can simply perform the same routing as above, but in reverse to send your packet back to you. However, once it reaches your router. Your router has it's own special rules to know that that certain packet should be forwarded back to your local machine with it's internal only IP address. This is called Network Address Translation.
Best Answer
Routers connect networks via gateways or interfaces, simplistically:
Your home router will run connection tracking software - it will keep a list of connections that the machines on your network have been talking to on the internet. Thus it will know the addresses and ports to return the responses.
TCP packets have a special field that can contain the address of the original machine. The router uses something called masquerading to insert its return address, and the destination machine also includes it. This can save a bit of work on the connection tracker, but also reveal internal ip addresses to the remote server.
Returning UDP packets on the other hand rely on the router's connection tracker module to return reponses to the original machine. It was a difficult problem to solve until a few years ago.
New incoming connections won't have any entries in the connection tracker, so the router won't know where to send it unless given specific forwarding instructions for that type of packet, and you'll find in all routers a way of being able to specify which machine on your network will receive new requests for port 80 for example.
TCP is a complex and amazing state-driven protocol, packets don't have to take the same routes, can be broken up into smaller pieces, arrive out of order, with error correction requesting individual packets to be resent, and the datastream can be reassembled with the applications at either end not being aware of any difficulties, unless they are too great to affect performance. Many PhDs have been earned through it!