2

Electrical engineer here trying to understand things. So I guess this might be simple or very complex, but how exactly does a router route traffic between say 2 PCs that are on a LAN?

eg. PC-1 and PC-2 are two laptops sitting on the table in front of me that are connected to the same router/modem. PC-1 IP is 192.168.10.1 PC-2 IP is 192.168.10.2. After reading this very informative article here: https://www.think-like-a-computer.com/2011/07/18/how-routing-works/

I understand that when they communicate to say PC-3 in Germany on IP address 200.200.100.3 the router abstracts the IP address to subnet mask 200.200.100.x, sends it along to a different router, which sends it to a different router, which eventually routes it to the subnet mask connected to PC-3's router with address 200.200.100.x, which then identifies the packet as headed to PC-3 and sends it there. But how does the last step actually happen?

What language is the code that routers operate on written in, C/C++/Assembler? How would it be possible to send a packet(s) manually from PC-1 to PC-2? Is this sort of low-level communication possible in desktop PCs running Windows 10, through the command line or a program? Can I read this source code that the router is using to route packets on a LAN or is it publicly available somewhere? I have done a lot of searching but every website basically says 'and then the router identifies the packet as belonging to PC-x and sends it on its way' without going into more detail as to how that actually happens, probably because the average Google user troubleshooting has no need for this knowledge. Thank you very much!

1
  • 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 can provide and accept your own answer.
    – Ron Maupin
    Dec 15, 2019 at 19:27

3 Answers 3

3

The box you (and the sales guys) are calling a "router" is probablly not just a router, it's a combination of an Ethernet switch, a router with NAT support, possiblly a wireless access point etc.

When two PCs on the LAN communicate the actual IP routing part of the "router" is not involved, the PCs will generate Ethernet frames directly addresssed to each others MAC addresses and they will only pass through the Ethernet switching part of your "router".

To send packets hosts and routers will first look in their routing table. The routing table will provide an interface name, and for non-local routes will provide a next-hop IP address. For locally connected networks there is no next-hop IP address in the routing table, instead the destination address is used as the next-hop IP address.

When sending out a packet over an Ethernet or Ethernet-like interface, the next-hop IP address needs to be translated to a MAC address. For IPv4 the system will first look in the arp table for the interface, if a match is found the packet will be sent, otherwise it will be queued while an ARP request is made. When the arp response is received an entry will be added to the ARP table and the queued packets will be release.

The process for IPv6 is similar in concept but different in the details.


P.S. In the old days every computer on the internet had a globally reachable IP address, that is no longer the case though (at least for IPv4). Your home or buisinesses router will normally be configured to perform network address translation, essentially from the internets point of view all the computers on your LAN look like a single computer.

The routers at your ISP on the other generally will not be performing address translation.

3

how exactly does a router route traffic between say 2 PCs that are on a LAN?

If your "LAN" refers to a shared L2 segment (connected by switch or hub) - usually this also means the same IP subnet as well - then there's no router involved at all. Packets are encapsulated in frames and those are forwarded directly from node to node by the switch(es) in between, based on the destination MAC address in the frame. The destination IP address is only used to find the destination's MAC (IPv4 uses ARP for that).

Packets that are addressed to an IP destination outside the local subnet are instead encapsulated in frames addressed to the gateway/router towards that destination. In the simplest case, everything outbound is sent to the default gateway.

This repeats for each hop/gateway/router on the way which makes the same kind of forwarding decision based on its own routing table, until the last hop determines that the destination IP is local and sends the packet directly using L2 framing.

(Slightly simplified - not everything is IP and Ethernet but those are the common protocols.)

What language is the code that routers operate on written in, C/C++/Assembler?

Pretty much anything suited for system programming. It's pretty standard for a modern enterprise-grade router to be running in hardware for the most part. Also, L3 switches generally route in hardware. Ternary content-addressable memory (TCAM) is commonly used for a single-step route lookup.

Windows NT flavors and of course Linux can route natively when activated. That however, is off-topic here. Linux is open source, so you can study the kernel code.

3
  • Thank you very much! I guess this is the part "It's pretty standard for a modern router to be running in hardware for the most part. Also, L3 switches generally route in hardware" where I would like more information. How does it route in hardware? What kind of resources would help me answer this question, if you are aware of any? I understand this goes outside the realm of network engineering into computer engineering, I just have no idea even what words to key into Google to get the info I want. Thanks!
    – user62443
    Oct 3, 2019 at 20:41
  • 1
    There is no standard for hardware routing, each ASIC manufacturer has their own method. Basically, TCAM (ternary content-addressable memory) is used to match a destination address to the whole routing table at once and find the best match. Sorry, but resource recommendations are explictly off-topic here.
    – Zac67
    Oct 3, 2019 at 20:56
  • 1
    @user62443, the RFCs lay out the framework for IP operation. The actual method and code that is implemented to adhere to the RFCs it up to the implementer and will vary although the bulk of the code is likely similar today (there has been nearly 40 years of IP development to streamline the code implementation).
    – YLearn
    Oct 4, 2019 at 3:39
0

When a router sends a packet to an unresolved directly connected host it uses Glean Adjacency.

From Wikipedia:

Glean adjacency: This adjacency is created when the router knows that either the destination IP's subnet is directly connected to the router itself and it does not know that destination device's MAC address, or the router knows the IP address of the router to forward a packet to for a destination, but it does not know that router's MAC address. Packets that trigger this entry will generate an ARP request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.