Routers job is to determine the best path for a packet across the network
The primary responsibility of a router is to forward packets to local or remote network by deciding the best path to send packets and then forward packets toward their destination using that path.
The router uses the knowledge in a form of a table to determine the best path to forward the packet and the table is called routing table. When the router receives a packet, it reads its destination IP address in the packet layer 3 header. Then he searches for the best match with a network address in the routing table. The routing table also includes the interface to be used to forward the packet. Once a match is found, the router encapsulates the IP packet into the layer 2 frame of the outgoing or exit interface, and the packet is then forwarded toward its destination.
It is a common situation that the router changes the layer 2 (Data link), encapsulation type for packets that are received on some interface and then sent to some other kind of interface or different kind of medium. This is because different interfaces and different medium are using different data link technologies for transferring the packet across the network segment.
Let’s say that the router receives a packet that is encapsulated in one type of layer 2 frame such as an Ethernet frame. If router wants to forward that packet data to neighbor router and they are connected using serial connection then router will encapsulate the data in a different type of data link frame, such as Point-to-Point Protocol (PPP). PPP is the protocol that is used to encapsulate and send data across serial connections. So you se that the data link encapsulation depends on the type of interface on the router and the type of medium it connects to.
The different data link technologies that a router connects to can include Ethernet in the case of connecting to a LAN, or T1 connection using PPP, Frame Relay, and Asynchronous Transfer Mode (ATM) when we speak about WAN serial connections.
We can make and example of simple routing functionality and follow a packet from the source PC to the destination PC. Let’s see how the routing is really functioning…
You know now that the responsibility of the router to find the destination network in its routing table and forward the packet on toward its destination. In this case, router on the left receives the packet encapsulated in an Ethernet frame. After decapsulating the packet, router uses the destination IP address of the packet to search its routing table for a matching network address. After a destination network address is found in the routing table, router encapsulates the packet inside a PPP frame and forwards the packet to router on the right side.
Routing table will be explained in more details later. For now… In the routing table the IP address can be mapped to exit interface that is the interface that is the best choice to forward the packet toward his destination. In other case routing table can be written in the way that maps IP address of the destination with the next-hop. In that case it will not be the exit interface but the IP address of the neighboring device interface on which the packet must arrive after forwarding.
After the packet is received for the router on the right side the process of routing will happen again in the same way.
The router will analyze destination IP address and it will find in the routing table that the destination IP address in the packet is the address of a directly connected network.
That means that the router must send this packet directly to the host computer. Router finds the destination MAC address in the ARP cache and sends the packet by encapsulating the packet inside a Ethernet frame and forwards the packet to the PC with IP address 172.16.0.10.
That would be routing in a couple od words.