This project implements the dataplane of a simple router in C. It handles IPv4 forwarding, ARP address resolution (with caching), and ICMP control messages.
-
IPv4 Packet Forwarding:
- Validates IP checksums.
- Decrements TTL and recalculates checksums.
- Performs Longest Prefix Match (LPM) on the routing table to find the next hop.
- Forwards packets to the appropriate interface or next hop.
-
Dynamic ARP:
- Maintains an ARP cache (static size of 100 entries) to map IP addresses to MAC addresses.
- Sends ARP Requests (
who-has) for next-hop IPs not in the cache. - Queues packets waiting for ARP resolution.
- Processes ARP Replies (
is-at) to update the cache and release queued packets.
-
ICMP Handling:
- Responds to Echo Requests (Ping) destined for the router interface with Echo Replies.
- Sends Time Exceeded (Type 11) messages if a packet's TTL drops to 0 or 1.
- Sends Destination Unreachable (Type 3) messages if no route is found for a packet.
This project requires a Linux environment (or WSL) with the following tools:
gccandmakepython3mininetscapy(Python library)tshark(Wireshark command-line tool)ethtool
A helper script is provided to install the necessary dependencies on Debian/Ubuntu-based systems (including WSL):
./install_deps.shTo compile the router, simply run:
makeThis ensures the router binary is created.
The project comes with a comprehensive test suite using Mininet. To run the tests:
./checker/checker.shThis script will start the Mininet topology, run the tests defined in checker/check.py, and output the score.
The current implementation uses a linear search through the routing table (rtable) to find the entry with the longest network mask (mask) that matches the destination IP. While simple, this is O(N) per packet.
- Replies: When an ARP reply is received, the cache is updated (
renew), and the queue of pending packets is checked. Any packet waiting for this IP is retrieved, its Ethernet header is populated with the new MAC address, and it is sent out. - Requests: When an ARP request is received for one of the router's own IPs, it immediately replies with its MAC address.
- Echo Reply: Handled by swapping source/destination IPs in the IP header and changing the ICMP type to
REPLY. - Error Messages: New packets are generated for
Time ExceededandDestination Unreachable, encapsulating the original IP header (standard ICMP behavior).
A simple queue (queue.h) is used to store packets (pkt struct) that are waiting for an ARP reply. The queue is checked every time a new ARP entry is added to the cache.
router.c- Main logic for the router dataplane.lib/- Helper libraries for data structures (queue, list) and parsing.include/- Header files for protocols and structures.checker/- Test infrastructure (Mininet topology and scripts).