- Forwarding process
- Longest Prefix Match using trie
- ARP
- ICMP
router.c router.h-> main functionicmp.c icmp.h-> functions for sending icmp packetsarp.c arp.h-> contains arp queue, arp table, send and receive arp packetstrie.c trie.h-> search & insert in rtableutils.c utils.h-> auxiliary functions
-
Firstly, we receive a frame and we (as in, the router) have to decide whether it's
for us(check destionation mac address). If it's broadcast/our mac, we have to process it, otherwise, discard it. -
After we checked it's for us, we have to decide what kind of
protocolthe ethernet headerencapsulates.- And from here we have two options:
- IP header, and it is called the
hadle_ip_packet - or ARP header, and it's called handle
arp_packet
-
Handle_ip_packet function:
- recalculates
checksum - tests whether the
IP destination is us, the router, if it's true, calls handle_icmp, supposing it's an echo request kind of message for us. - Updates the
TTL-> if its 0 or 1 drops it and sends an icmp message notifying the sender why it has been dropped. Updates checksum, with the new TTL- Finds the
next hoopwhere the packet has to go, for that is looked up in atriein functionsearch_next_hoop. If there is no next hoop for the ip looked for, drops it and sends an icmp with the message Destination unreachable, otherwise, proceeds with sending it forward - With
find_mac_address_in_arpgets the mac address of the next_hoop, because we only have it's ip, not the mac.- If there's
no entryfor it in theARP table, puts the packet on hold, in a queue, for it to besent laterwhen the mac address asociated with the ip is available. (create_arp_queue_entry() + arp_enq()) and sends anarp request, broadcasting a message asking for the mac address of next_hoop->ip. - Otherwise, it proceeds with sending the frame forward, by now we know everything we need for sending it to the next hoop.
- If there's
- recalculates
-
handle_arp_packet function:
- Tests to see if the received packet is a ARP Request or ARP Reply.
- If it's an
ARP Request, calls functionhandle_arp_recv_request, creates a new frame in which sourse and destination mac are swapped from the received frame, in ether_header, and completing the source ip and mac with it's own in arp header. - If it's an
ARP Reply, means that the router initiated an ARP Request beforehand. Itsaves the frame's source mac and ip addressin it'sARP Table. it Searches through the queue of packets waiting for a destination mac address and if the significant pachet was waiting for the arrived mac, all it has to do iscomplete the destination macandsend it(it was put in queue with all the other informations it needed besides the destination mac). After that, it is removed from the queue.
-
handle_icmp function:
- It's the
default functioncalled when anicmp messageneeds to be sent (destination unreachable, ttl expired, echo reply) receiving as aparameterthe type and code of the icmp message, being able to guide itself from these to send the right icmp message. - The ethernet and ip header it the same for all types of icmp messages, besides the ip len field, which is calculated separately considering icmp type.
- For completing the icmp header and the payload, 2 functions are possibly called
icmp_echo_reply(), which populates icmp header with the specific fields(id and sequence from the received icmp request) and the payload being the payload from original frame.icmp_ttl_or_unrec(), which sends ip header + 8 bytes after from original frame.
- It's the
-
For efficiency,
searchingthe next hoop in theRouting Tableby longest prefix, it was used a trie. Every node in the trie it'sa bitfrom the ip address, the lowest being thelongest prefix. At the start of the router, the trie is populated with the entries from rtable.