{"id":18637563,"url":"https://github.com/justin-marian/router","last_synced_at":"2026-07-16T15:39:16.210Z","repository":{"id":227853175,"uuid":"770868561","full_name":"justin-marian/router","owner":"justin-marian","description":"Router implemented with static forwarding tables. It handles IPV4 , ARP, ICMP packets, and uses Longest Prefix Match for routing decisions.","archived":false,"fork":false,"pushed_at":"2024-03-15T14:26:03.000Z","size":499,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-17T13:52:40.837Z","etag":null,"topics":["arp","c","forwarding","icmp","routing-algorithm","routing-table"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsl-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/justin-marian.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-12T09:58:16.000Z","updated_at":"2024-03-15T14:04:25.000Z","dependencies_parsed_at":null,"dependency_job_id":"91e8936f-b298-4b8b-bba3-bbb26516933e","html_url":"https://github.com/justin-marian/router","commit_stats":null,"previous_names":["justin-marian/router"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/justin-marian/router","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justin-marian%2Frouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justin-marian%2Frouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justin-marian%2Frouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justin-marian%2Frouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/justin-marian","download_url":"https://codeload.github.com/justin-marian/router/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justin-marian%2Frouter/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35549640,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-07-16T02:00:06.687Z","response_time":83,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arp","c","forwarding","icmp","routing-algorithm","routing-table"],"created_at":"2024-11-07T05:36:45.220Z","updated_at":"2026-07-16T15:39:16.170Z","avatar_url":"https://github.com/justin-marian.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Router\r\n\r\n## Description\r\n\r\nRouter application responsible for forwarding packets within a network. The router operates on the principle of static forwarding, where routing decisions are pre-configured and do not change dynamically based on network conditions.\r\n\r\n## Packets Processing\r\n\r\n- Network interfaces are initialized based on command-line arguments.\r\n- The router is initialized using the provided configuration file.\r\n- Packet handling, enters a loop where it continuously receives network messages on any network interface.\r\n- Call corresponding handler function for IPv4 packets and for ARP packets type.\r\n- Error handling, if there's an error when receiving a message, it frees the router and exits with an error message.\r\n\r\n## Router Forwarding\r\n\r\nThe router navigates the routing table's `prefix tree` (`trie`) structure to find the insertion point.\r\nIt compares the **prefix and mask** of the new entry with existing ones to determine the insertion position.\r\nAfter finding the correct position, the new entry is added while **preserving the hierarchical structure**.\r\n\r\n**Packet Handling:**\r\n\r\n- **Handling Incoming Packets:**\r\n  - Extracts the **destination IP address** from the packet header upon receiving a packet.\r\n  - Performs an **LPM lookup** in the routing table to determine the **best route for forwarding**.\r\n\r\n- **Forwarding Decisions:**\r\n  - If a **matching route** is found, the router forwards the packet to the next hop.\r\n  - When **no matching route** is found **ICMP messages** are send, the packet was dropped because of:\r\n    - `\"Time Exceeded\"` (TTL expiration ttl \u003e= 1)\r\n    - `\"Destination Unreachable\"` (no available route)\r\n\r\n- **Initialization and Creation:**\r\n  - During initialization, the router allocates memory for its routing table structure and initializes it.\r\n  - The **routing table** structure is a `prefix tree` (`trie`), with each **node** representing a (**routing entry**).\r\n\r\n- **Routing Entry Structure:**\r\n  - Routing entries contain information about how to reach specific destinations in a network.\r\n  - Consists of a **network prefix** (**destination IP address range**), and with its attributes (**next hop** and **interface**).\r\n\r\n- **Insertion Process:**\r\n  - When adding a new routing entry, the admin typically updates the `rtable.txt` file.\r\n  - The router navigates the routing table's tree structure to find the insertion point.\r\n  - After finding the correct position, the new entry is added while preserving the hierarchical structure.\r\n  - Once the correct position is found, the new entry is added to the routing table, ensuring that it *maintains the hierarchical structure* based on **network prefixes**.\r\n  - Simple example illustrates how a new entry is inserted into the routing table represented as a `trie`.\r\n\r\n  ```r\r\n  Given the routing table:\r\n      - Prefix: 192.168.1.0/24, Next Hop: 10.0.0.1\r\n      - Prefix: 10.0.0.0/8, Next Hop: 192.168.0.1\r\n      - Prefix: 172.16.0.0/16, Next Hop: 192.168.0.1\r\n\r\n  To insert a new entry with Prefix: 192.168.0.0/20, Next Hop: 10.0.0.2:\r\n      - Traverse the routing table to find the appropriate position for the new entry.\r\n      - Compare the prefix and mask of the new entry with existing entries.\r\n      - Determine the correct position for insertion (e.g., between 192.168.1.0/24 and 172.16.0.0/16).\r\n      - Add the new entry to the routing table.\r\n  ```\r\n\r\n## Longest Prefix Match (LPM)\r\n\r\n### Algorithm Overview\r\n\r\n`Longest Prefix Match` is used by the router to determine the **best matching route** for a given **destination IP**.\r\n\r\n- When a router receives a packet, it needs to decide where to forward it based on the **destination IP** address.\r\n- The router looks in its routing table, with multiple entries with IP address prefixes and next-hop information.\r\n- For each entry in the routing table, the router compares the **destination IP** address with the stored prefixes.\r\n- Router follows the **most specific route** to the destination, the router chooses the one with the `longest prefix` (`most specific route`), improving routing efficiency and accuracy.\r\n\r\n## ARP\r\n\r\n- **Searching for ARP Table Entry:**\r\n  - Iterates through address entries to find an entry with a specified **IP**.\r\n  - Returns *the entry's index if found; otherwise, returns -1*.\r\n- **Inserting New ARP Table Entry:**\r\n  - Inserts a new entry with the provided IP and MAC address. Checks for duplicates, expands table capacity.\r\n\r\n### Handling Incoming ARP Packets\r\n\r\n- Upon receiving an ARP packet, the router checks its validity and type.\r\n  - `ARP requests` replies with router's MAC address.\r\n  - `ARP replies` it can perform 2 functions:\r\n    - caches sender's **IP and MAC addresses**\r\n    - processes waiting packets for the sender's IP with resolved MAC.\r\n\r\n### ARP Request\r\n\r\n- **Initialize Ethernet Header:**\r\n  - Sets the Ethernet type to ARP, determines source MAC address, and sets destination broadcast MAC.\r\n- **Update Packet Length:**\r\n  - Adjusts the packet buffer length based on Ethernet and ARP header sizes.\r\n- **Generate ARP Request Packet:**\r\n  - Prepares an ARP request packet, initializes Ethernet header, and updates packet length.\r\n\r\n### ARP Reply\r\n\r\n- **Set ARP Operation to Reply:**\r\n  - Indicates an ARP reply by setting the ARP operation field.\r\n- **Swap Target and Sender IP/MAC:**\r\n  - Exchanges target and sender **IP/MAC addresses** in the ARP header.\r\n- **Set Sender IP and MAC:**\r\n  - Assigns sender's **IP and MAC addresses** in the ARP header.\r\n- **Update Ethernet Header:**\r\n  - Modifies Ethernet header to set appropriate **MAC addresses** in the packet buffer.\r\n\r\n## ICMP\r\n\r\n- **Initialize ICMP Header:**\r\n  - Sets ICMP header fields in the packet buffer, calculates pointers, and adjusts packet length.\r\n  - Copies IP header and additional bytes for specific ICMP message types.\r\n- **Update ICMP Checksum:**\r\n  - Calculates and updates the ICMP checksum in the packet buffer.\r\n- **Generate New IPv4 Header:**\r\n  - Prepares a new IPv4 header for **ICMP messages**.\r\n- **Update Ethernet Header:**\r\n  - Modifies Ethernet header to include appropriate MAC addresses based on the router's interface.\r\n- **Generate ICMP Reply:**\r\n  - ICMP reply message: *initializes ICMP header, updates checksum, Ethernet header, and generates IPv4 header*.\r\n\r\n## Setup\r\n\r\nTo simulate a virtual network, we will use `Mininet` (network simulator that uses real kernel, switch, and app code).\r\n\r\nThis setup should work fine on **WSL 2**.\r\n\r\n- Update the package index:\r\n\r\n  ```bash\r\n  sudo apt update\r\n  ```\r\n\r\n- Install required packages:\r\n\r\n    ```bash\r\n    sudo apt install mininet openvswitch-testcontroller tshark python3-click python3-scapy xterm python3-pip\r\n    ```\r\n\r\n- Install Mininet using pip:\r\n\r\n    ```bash\r\n    sudo pip3 install mininet\r\n    ```\r\n\r\n- Increase font size in terminals (**optional**):\r\n\r\n    ```bash\r\n    echo \"xterm*font: *-fixed-*-*-*-18-*\" \u003e\u003e ~/.Xresources\r\n    xrdb -merge ~/.Xresources\r\n    ```\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjustin-marian%2Frouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjustin-marian%2Frouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjustin-marian%2Frouter/lists"}