Skip to content

Repository files navigation

Build Your Own Router

A simple IPv4 router with static routing, ARP, and ICMP support, implemented in C++11 and running on Mininet.

What It Does

This router receives raw Ethernet frames and processes them like a real router:

  • ARP: Responds to ARP requests, sends ARP requests to discover next-hop MACs, maintains a cache with 30-second TTL
  • IPv4 Forwarding: Validates checksums, decrements TTL, looks up routing table with longest-prefix-match, forwards packets to the correct interface
  • ICMP: Generates Echo Reply (for ping), Time Exceeded (for traceroute), and Port/Host Unreachable messages

Network Topology

          server1 (192.168.2.2/24)
              │
         sw0-eth1 (192.168.2.1/24)
              │
client ── sw0-eth3 (10.0.1.1/8) ── [Router sw0]
(10.0.1.100/8)                      │
                              sw0-eth2 (172.64.3.1/16)
                                     │
                              server2 (172.64.3.10/16)

Routing Table

Destination Gateway Mask Interface
0.0.0.0 10.0.1.100 0.0.0.0 sw0-eth3
192.168.2.2 0.0.0.0 255.255.255.0 sw0-eth1
172.64.3.10 0.0.0.0 255.255.0.0 sw0-eth2

What You Need to Implement

The starter code provides the framework. Three methods need to be filled in:

Method File What It Does
handlePacket() simple-router.cpp Entry point: dispatches Ethernet frames by type, validates MAC, calls ARP/IP handlers
periodicCheckArpRequestsAndCacheEntries() arp-cache.cpp Called every second: resends ARP requests, times out after 5 attempts, cleans expired cache
lookup() routing-table.cpp Longest-prefix-match lookup across the static routing table

Additional helper methods in simple-router.cpp handle ARP requests/replies, IP validation/forwarding, and ICMP message generation.

Build & Run

# Prerequisites (Ubuntu 16.04)
sudo bash setup.sh

# Compile
make

# Run (3 terminals required)
# Terminal 1: POX controller
/opt/pox/pox.py --verbose ucla_cs118

# Terminal 2: Mininet topology
chmod +x run.py
sudo ./run.py

# Terminal 3: Router
./router

Once all three are running, test in the Mininet CLI:

mininet> client ping server1
mininet> client traceroute server2
mininet> client wget http://192.168.2.2/index.html

Implementation Overview

Layer File Responsibility
Ethernet simple-router.cpp Frame dispatch by type (ARP/IPv4) and MAC filtering
ARP arp-cache.cpp IP↔MAC resolution, request/reply, cached entries with 30s TTL
IPv4 simple-router.cpp Checksum validation, TTL decrement, longest-prefix-match forwarding
ICMP simple-router.cpp Echo Reply (type 0), Time Exceeded (type 11), Port/Host Unreachable (type 3)
Routing routing-table.cpp Longest-prefix-match lookup over static routing table

Key Design Decisions

  • ARP timeout handling: Requests retry every 1 second, up to 5 times. On timeout, an ICMP Host Unreachable is sent back to the source via a reverse route lookup.
  • Interface selection for local replies: Echo Reply and ICMP errors are sent via the incoming interface (not the destination IP's interface), with the destination IP as the source address.
  • MAC preservation: Forwarded packets queued for ARP resolution retain their original source MAC, ensuring ICMP errors can reach the original sender.

Test Results

Test Score Status
Public autograde 45/45
Ping TTL correctness 10 pts ✅ ttl=63 after forwarding
Traceroute 1-hop 10 pts
ARP timeout (no crash) 5 pts
Host Unreachable 5 pts ✅ Verified with modified RTABLE
Large file download (10 MB) 10 pts ✅ md5 matched
Total 85/85 All passed

Documentation

Acknowledgement

Based on the CS118 project by Alexander Afanasyev, UCLA.

About

Personal implementation of SimpleRouter project of UCLA CS118, also the lab of Computer Networks 2025 Autumn course at School of Software, Tsinghua University.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages