In this blog, we’ll dive deep into how routing works — the mechanism that powers the journey of data packets across the internet. We'll also create a simple Static Router simulation using Go (Golang).
🌍 What is Routing?
Routing is the process of determining the best path for data packets to travel from a source device to a destination device across interconnected networks.
📌 Operates at Layer 3 (Network Layer) of the OSI Model
📡 Protocol: IP (Internet Protocol)
🔌 Device: Router
🧠 Routing focuses on forwarding IP packets, not the actual physical movement of data.
🛣️ Types of Routing
1️⃣ Static Routing
- 🧑💻 Manually configured by network admins
- ✅ Best for small, stable networks
- 🔁 Doesn’t adapt to network changes
ip route add 10.0.0.0/24 via 192.168.1.1
2️⃣ Dynamic Routing
- 🤖 Routers communicate automatically using routing protocols.
- 🔁 Routes are updated based on network topology changes.
Protocols and Use Cases
Protocol | Use Case |
---|---|
RIP, OSPF | Internal networks (IGP) |
BGP | Between ISPs (EGP) |
🔁 Key Concepts in Routing
Term | Meaning |
---|---|
Router | Device that forwards packets between networks |
Next Hop | IP of the next router for forwarding the packet |
Routing Table | Maps destination networks to next hops |
Metric | Used to select the best path (cost, delay, etc.) |
CIDR/IP Prefix | Defines IP ranges (e.g., 192.168.0.0/16) |
🌐 Static Routing in Go 🧑💻
Let’s simulate packet forwarding using static routes in Go!
package main
import (
"fmt"
)
type Packet struct{
Source string
Destination string
Data string
}
type RoutingTable map[string]string
func(p *Packet) String() string{
return fmt.Sprintf("Packet: Source: %s, Destination: %s, Data: %s \n", p.Source, p.Destination, p.Data)
}
func (rt RoutingTable )GetnextHop(destination string) string{
nextHop, exists:= rt[destination]
if exists{
return nextHop
}
return "No Route Found"
}
func ForwardPacket(p *Packet, routingTable RoutingTable){
nextHop:= routingTable.GetnextHop(p.Destination)
fmt.Printf("The NextHop is %s\n", nextHop)
fmt.Println(p)
}
func main(){
router1RoutingTable:= RoutingTable{
"192.168.1.2" : "Router2",
}
router2RoutingTable:= RoutingTable{
"192.168.1.2": "Final Destination",
}
packet:= &Packet{
Source: "192.168.1.1",
Destination: "192.168.1.2",
Data: "Hello, This is a Packet",
}
fmt.Println("Router table 1")
ForwardPacket(packet, router1RoutingTable)
fmt.Println("Router table 2")
ForwardPacket(packet, router2RoutingTable)
}
🚦 How Dynamic Routing Works
Unlike static routing, dynamic routing enables routers to:
- 📬 Exchange network info
- 🔁 Automatically update routing tables
- ⚡ Adapt to topology changes
🔑 Key Concepts
Concept | Explanation |
---|---|
Routing Protocols | RIP, OSPF, BGP used to exchange route info |
Metric | Helps pick best route (e.g., speed, hops) |
Continuous Updates | Ensures routing tables reflect current topology |
Auto Adjustments | Quick rerouting in case of failures |
🧠 Types of Dynamic Routing Protocols
🟦 1. Distance Vector
- 📌 Shares entire routing table with neighbors
- 🔢 Decision: based on hop count
- ⚠️ Risk: Routing loops, slow convergence
✅ Examples:
- RIP – Simple, max 15 hops
- IGRP – Cisco, uses bandwidth & delay
🟩 2. Link State
- 📌 Each router builds full topology
- 📡 Shares info about directly connected links only
- ⚡ Fast convergence, great for large networks
✅ Examples:
- OSPF – Efficient, hierarchical
- IS-IS – Used by ISPs
🟨 3. Hybrid
- 📌 Mix of distance vector & link state
- 📈 Updates only on change
- ⚙️ Uses DUAL algorithm for fast, loop-free recovery
✅ Example:
- EIGRP (Cisco proprietary)
🟥 4. Path Vector
- 📌 Used between organizations (EGP)
- 🌍 Tracks the full AS path
- 🧠 Policy-based decision making
✅ Example:
- BGP – The Internet's main routing protocol
🧾 Conclusion
Routing is at the heart of networking, guiding how data flows across the globe 🌐.
Whether it's static (manually set routes) or dynamic (intelligent, adaptable protocols), routers ensure data finds its way efficiently. 🛰️
And with just a few lines of Go, we built a basic simulation of how a router decides the next hop for a packet. 🧑💻
If you enjoyed this post, give it a ❤️ or 🦄 and share your thoughts in the comments!
Happy networking! 🌐✨