Routing in Computer Networking
Tags: webdev, beginners, cybersecurity, networking
Published: 2024-04-23 15:53:25

The basis of the Internet is the transfer of information among networks. So even if you are a web developer or network engineer, it is very crucial to understand how routers aid this process. This article will be looking at the methods and tools that are used to make this happen.

## Prerequisites
The knowledge of the following is required to flow with this article.

* A basic understanding of what computer networking is.
* knowing what IP addresses are.
* Understand what subnetting is.

## What is Routing?
This is the choosing of a communication path between two or more devices for the sake of sending data from source to destination. To achieve this, a tool known as router is used.



## Router
A router facilitates communication between devices on different networks. This enables the sending and reception of data packets between networks based on their IP addresses. 

The router is basically a data package distributor for the internet. It operates at the network layer(layer 3) of the [OSI](https://www.imperva.com/learn/application-security/osi-model/)(Open System for Interconnection). You might also find the router being referred to as a network gateway at times. A router can be likened to a logistics/delivery driver who delivers items bought from an online store to the consumer.

A router is connected to at least two networks at a time. The networks could be:
* Two local area networks(LANs).
* Two wide area networks(WANs).
* A LAN and its internet service provider(ISP).


Below is an illustration of a router connecting a LAN to the internet.


![Router connecting LAN to Internet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vgawypayxtamo5y4pyb4.png)


### Switch
This article will not be complete without talking about switches and their relationship with routers and LANs.

Switches are used to connect devices on the same LAN. Switches are the intermediaries between devices in the same LAN and the router. Devices on the LAN connect to the switch via what is called ethernet switch ports.

Switches typically operate at layer 2(Data-Link layer) of the OSI model, primarily using MAC addresses to forward frames within a LAN. Switches maintain a MAC address table also known as a MAC address forwarding table which maps MAC addresses to the ports on the switch. There are also layer 3 switches that also have routing tables, however that is not our focus here.



> Question arising
A newbie might be wondering how does a router know where to send a packet of data?. We will take a look at how the router
performs its function before we take a look at a router's structure.

## Routing Table
From our question above; how does a router know where to send a packet of data? By using a routing table.

Also known as Routing Information Base(RIB). A routing table is information stored in a router containing the routes to various destinations. It is stored in the router's RAM and is maintained by the router's operating system. It is a data structure used by routers to determine the best path for forwarding packets towards their destinations. It contains information about destinations and metrics.

Recall we earlier likened the router to a driver of a delivery/logistics company. This topic brings back to my memory when I worked for an Ecommerce company; we used to give the delivery details of the consumers in an app to the drivers to use to locate them. In this scenario, we can liken the routing table to the app with the delivery details of consumers.

In the case of a router, we will say how does it acquire its routes? It does so statically or dynamically. By statically the routes are added manually to the routing table at the beginning and dont change except modified manually. For dynamic routing, the routes change according to network topology and conditions, the routing table is adjusted based on network changes.

Now going to take a look at what a routing table might look like.

| Destination   | Subnet Mask   | Next Hop     | Interface | Metric |
|---------------|---------------|--------------|-----------|--------|
| 192.168.1.0   | 255.255.255.0 | 192.168.1.1 | eth0      | 1      |
| 10.0.0.0      | 255.0.0.0     | 10.0.0.1     | eth1      | 2      |
| 172.16.0.0    | 255.255.0.0   | 172.16.0.1   | eth2      | 3      |
| 0.0.0.0       | 0.0.0.0       | 192.168.0.1 | eth3      | 10     |

Explanation of the entries:

* Destination:  This column represents the network destination. For example, 192.168.1.0 represents the destination network address.

* Subnet Mask:This column specifies the subnet mask associated with the destination network. It determines which portion of the IP address is the network portion and which portion is the host portion. For example, 255.255.255.0 means that the first 24 bits of the IP address are the network portion, leaving 8 bits for the host portion.

* Next Hop: This column indicates the next router or gateway IP address to which the packet should be forwarded in order to reach the destination network. For directly connected networks, this may be the IP address of the interface on the local router. For example, 192.168.1.1 is the next-hop router for the destination network 192.168.1.0.

* Interface: This column specifies the network through which the packet should be forwarded to reach the next hop router or destination router. For example, eth0 indicates that packets should be sent out through the Ethernet interface 0.

* Metric: This column represents the cost associated with each route. It's used by the router to determine the best path to reach a destination when multiple routes are available. Lower metric values indicate preferred routes. In this example, the route with the lowest metric value (1) for the destination 192.168.1.0/24 through eth0 would be chosen as the preferred route to reach that network. Similarly, for other destinations, the route with the lowest metric value would be preferred. The metric value can be based on factors such as hop count, bandwidth, delay, or administrative preference.

Let us now see a breakdown of each row of the above table:

1. Row 1: Packets destined for the network 192.168.1.0/24 should be forwarded to the next hop 192.168.1.1 via the interface eth0 with a metric of 1.

2. Row 2: Packets destined for the network 10.0.0.0/8 should be forwarded to the next hop 10.0.0.1 via the interface eth1 with a metric of 2.

3. Row 3: Packets destined for the network 172.16.0.0/16 should be forwarded to the next hop 172.16.0.1 via the interface eth2 with a metric of 3.

4. Row 4: The default route 0.0.0.0/0 indicates that packets with destinations not matching any specific routes should be forwarded to the next hop 192.168.0.1 via the interface eth3 with a higher metric of 10.

Talking more about row 4:
Why does a router need a default gateway?
This makes it find a way to other networks it doesn't know about. This makes it handle traffic destined for networks outside of its directly connected networks or subnets. The default route serves as an all routes for traffic that does not match any other route entry in the routing table. It serves as a fallback mechanism in case specific routes fail or are unavailable. If a router cannot find a more specific route for a packet, it uses the default route to prevent traffic from being dropped. One major use of this is routing traffic to the internet as we can't expect the router to know every address on the internet.

## Physical Components of Routers
The components of a router typically include:

* CPU(Central Processing Unit):  Responsible for executing instructions and managing tasks within the router.

* RAM: The routing table ARP(Address Resolution Protocol) cache and buffered packets are stored here. It is called the running configuration, providing memory for running processes. It is volatile in nature, meaning it doesn't retain data when the router is turned off.

* ROM: Contains the bootcode and firmware necessary for starting up the router. It performs the Power On Self Test(POST) operation.

* NVRAM: Means non-volatile RAM. This means it retains data after the router has been turned off. If we want any configurations permanent we save them here.

* Flash Memory: This serves as the router's secondary memory. It stores the router's operating system configuration files and other software components.

* Interfaces/Ports: Physical connectors where network cables are plugged in. Examples are ethernet ports, serial ports, and WAN interfaces.

* Switching Fabric: Handles the forwarding of data packets from the router's input port to the output port that has been specified by the LPM (Longest Prefix Match). It is basically a combination of hardware and software; some of which are high-speed interconnection network, switching ASICs(Application-specific integrated circuits), buffers.

## Longest Prefix Match
I mentioned the "Longest Prefix Match" while talking about the switching fabric in the last section. In order to understand it, we will take a look at some terms namely:
* IP prefix.
* prefix Length.
* Number of Possible Subnets Within a Network.
* Route via Longest Prefix Match.

### IP Prefix
An IP prefix also known as network prefix combines an IP address and a subnet mask to specify a range of IP addresses. The prefix indicates the network portion of the IP address which helps in routing and subnetting.

### Prefix Length
This helps us to obtain the  IP prefix. 
For example: 192.168.1.0/24,
the /24 represents the prefix length. The prefix length represents the number of bits in the network portion and the rest refers to the host. As we know an IPv4 address is 32bits. In the above scenario, the last 8 bits refer to the new host. 
So we have: 192.168.1.255
where:
192.168.1 is the network portion.
.255 is the host portion.
The range of IP addresses in this scenario will be:
192.168.1.0-192.168.1.255

Likewise if we have 192.168.1.0/16, we will have:
192.168.255.255
The range of IP addresses in this scenario will be: 192.168.1.0-192.168.255.255

### Number of Possible Subnets Within a Network
We can also use the prefix length to determine the possible number of subnets within a network. We can use the formula 2^(32-L).
Where L = prefix length

Suppose L = 24
2^(32-24)= 2^8
2^8 = 256
So there are 256 possible subnets in 192.168.1.0/24

### Route via Longest Prefix Match
Suppose we have 192.168.1.0/24 and 192.168.1.0/16 and we want to forward a packet to 192.168.1.20
and we thinking of which to use.

Recall that prefix indicates the network portion.


![IPv4 showing network and host portion](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w42jkzydfe6cifchzgg2.png)

There is a saying that the "Longest Prefix Match" always wins. In the above, 192.168.1.0/24 will be used.

## Conclusion
We have looked at what routing is, what routers are, and what they do. All the above are just enough to get beginners started with what routing is. The following links were instrumental in my understanding of routing and routers. You can refer to them for more studies to gain more perspectives into all we discussed.

* [Components of Routers](https://www.pynetlabs.com/components-of-a-router/)
* [How IP routing Works](https://www.google.com/url?sa=t&source=web&rct=j&opi=89978449&url=https://m.youtube.com/watch%3Fv%3DZyDitWhNx2U&ved=2ahUKEwifyo3T4dWFAxUNa0EAHaZoASgQo7QBegQIDBAG&usg=AOvVaw1WpqkJu_Gt7lkCpBJi7q1x)
* [What is a Router?](https://www.google.com/url?sa=t&source=web&rct=j&opi=89978449&url=https://m.youtube.com/watch%3Fv%3Dp9ScLm9S3B4&ved=2ahUKEwiN3OmI4tWFAxVWQEEAHVckB2cQwqsBegQIDRAF&usg=AOvVaw2aN9o1CAYBIszkkgnL5JsN)
* [Routing Table Explained](https://www.google.com/url?sa=t&source=web&rct=j&opi=89978449&url=https://m.youtube.com/watch%3Fv%3DuKiM9-tGuc4&ved=2ahUKEwj8n5uL5NWFAxVKTEEAHVe8D1UQwqsBegQILRAF&usg=AOvVaw0R8QO80o7mvZ2EjuoUbBUH)
* [What's  inside a router](https://www.google.com/url?sa=t&source=web&rct=j&opi=89978449&url=https://m.youtube.com/watch%3Fv%3DNfHR94JAO-s&ved=2ahUKEwixmL-I5dWFAxU3YUEAHbEUDggQo7QBegQIDBAG&usg=AOvVaw2HWjJMPbwjCJSi_gAfV6Q-)
* [What is a Next Hop](https://www.techopedia.com/definition/2447/next-hop)