Implemented the dataplane of an IPv4 router in C (≈20 hours) as part of an academic assignment, handling packet forwarding, ARP resolution, and ICMP messaging over POSIX sockets.
- Receive Ethernet frame and verify it carries an IPv4 packet.
- Recalculate and compare IP checksum; drop on mismatch.
- Check TTL; if ≤ 1 send ICMP Time Exceeded and drop.
- Decrement TTL, update checksum.
- Perform Longest Prefix Match in routing table; drop if no route.
- Lookup next-hop MAC in ARP cache; if missing queue packet and send ARP request.
- Prepend Ethernet header with resolved MAC and forward packet.
- Built a binary trie: each IP bit is a trie level.
- Functions:
trie_create,trie_insert(route, mask),get_best_route(dest_ip),free_trie. - On packet arrival, traverse bit by bit to find the longest matching route.
- ARP cache implemented as a linked list + pending-packet queue.
- Functions:
add_arp_entry,find_arp_entry,free_arp_cache. - On cache miss: enqueue packet, broadcast ARP request.
- On ARP reply: insert MAC into cache, dequeue and forward waiting packets.
- On ARP request: swap addresses and send ARP reply.
- Generate ICMP Time Exceeded (TTL expired) and Host Unreachable (no route).
- Reply to ICMP Echo Request with Echo Reply if destined to router.
- Build ICMP packets from scratch, recalc IP/ICMP checksums, set Ethernet headers accordingly.
- Endianness conversions between little- and big-endian when parsing headers.
- Ambiguous ICMP requirements required careful spec reading.
- ARP request construction and pending-packet queue management were error-prone.