CS43 Lab7 - A Router Stack  1.2019
Classes | Macros | Functions
sr_arpcache.h File Reference

This file defines an ARP cache (sr_arpcache), which is made of two structures: an ARP request queue (sr_arpreq), and ARP cache entries (sr_arpentry). More...

#include <inttypes.h>
#include <time.h>
#include <pthread.h>
#include "sr_if.h"
Include dependency graph for sr_arpcache.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  sr_packet
 A packet queued, waiting for the resolution of an sr_arpreq. More...
 
struct  sr_arpentry
 An entry in the ARP cache. More...
 
struct  sr_arpreq
 An outstanding ARP request with queued packets waiting on the response. More...
 
struct  sr_arpcache
 An ARP cache which tracks IP/MAC mappings, as well as outstanding ARP requests. More...
 

Macros

#define SR_ARPCACHE_SZ   100
 
#define SR_ARPCACHE_TO   15.0
 

Functions

struct sr_arpentrysr_arpcache_lookup (struct sr_arpcache *cache, uint32_t ip)
 Checks if an IP->MAC mapping is in the cache. More...
 
struct sr_arpreqsr_arpcache_queuereq (struct sr_arpcache *cache, uint32_t ip, uint8_t *packet, unsigned int packet_len, char *iface)
 Adds an ARP request to the ARP request queue. More...
 
struct sr_arpreqsr_arpcache_insert (struct sr_arpcache *cache, unsigned char *mac, uint32_t ip)
 Inserts an ARP cache entry. More...
 
void sr_arpreq_destroy (struct sr_arpcache *cache, struct sr_arpreq *entry)
 Frees all memory associated with this arp request entry. More...
 
void sr_arpcache_dump (struct sr_arpcache *cache)
 Prints out the ARP table. More...
 
int sr_arpcache_init (struct sr_arpcache *cache)
 sr_arpcache constructor, you shouldn't need to call this, the starter code calls it for you.
 
int sr_arpcache_destroy (struct sr_arpcache *cache)
 sr_arpcache destructor, you shouldn't need to call this, the starter code calls it for you.
 
void * sr_arpcache_timeout (void *cache_ptr)
 A cleanup command which clears out ARP cache entries every 15 seconds, you shouldn't need to call this, it is called on a separte thread by the starter code for you.
 

Detailed Description

This file defines an ARP cache (sr_arpcache), which is made of two structures: an ARP request queue (sr_arpreq), and ARP cache entries (sr_arpentry).

Here, we present pseudocode for use of these structures.
The outline for sending a packet using the ARP cache is as follows:

// When sending packet to next_hop_ip
entry = arpcache_lookup(next_hop_ip)
if entry:
use next_hop_ip->mac mapping in entry to send the packet
free entry
else:
req = arpcache_queuereq(next_hop_ip, packet, len)


The handle_arpreq() function is a function you should write, and it should handle sending ARP requests if necessary:

function handle_arpreq(req):
if difftime(now, req->sent) > 1.0
if req->times_sent >= 5:
send icmp host unreachable to source addr of all pkts waiting
on this request
arpreq_destroy(req)
else:
send arp request
req->sent = now
req->times_sent++


The ARP reply processing code should move entries from the ARP request queue to the ARP cache:

// When servicing an arp reply that gives us an IP->MAC mapping
req = arpcache_insert(ip, mac)
if req:
send all packets on the req->packets linked list
arpreq_destroy(req)


Since handle_arpreq as defined in the comments above could destroy your current request, make sure to save the next pointer before calling handle_arpreq when traversing through the ARP requests linked list.

Function Documentation

◆ sr_arpcache_dump()

void sr_arpcache_dump ( struct sr_arpcache cache)

Prints out the ARP table.

◆ sr_arpcache_insert()

struct sr_arpreq* sr_arpcache_insert ( struct sr_arpcache cache,
unsigned char *  mac,
uint32_t  ip 
)

Inserts an ARP cache entry.

This method performs two functions: 1) Looks up this IP in the request queue. If it is found, returns a pointer to the sr_arpreq with this IP. Otherwise, returns NULL. 2) Inserts this IP to MAC mapping in the cache, and marks it valid.

◆ sr_arpcache_lookup()

struct sr_arpentry* sr_arpcache_lookup ( struct sr_arpcache cache,
uint32_t  ip 
)

Checks if an IP->MAC mapping is in the cache.

IP is in network byte order. You must free the returned structure if it is not NULL.

◆ sr_arpcache_queuereq()

struct sr_arpreq* sr_arpcache_queuereq ( struct sr_arpcache cache,
uint32_t  ip,
uint8_t *  packet,
unsigned int  packet_len,
char *  iface 
)

Adds an ARP request to the ARP request queue.

If the request is already on the queue, adds the packet to the linked list of packets for this sr_arpreq that corresponds to this ARP request. The packet argument should not be freed by the caller.

A pointer to the ARP request is returned; it should be freed. The caller can remove the ARP request from the queue by calling sr_arpreq_destroy.

◆ sr_arpreq_destroy()

void sr_arpreq_destroy ( struct sr_arpcache cache,
struct sr_arpreq entry 
)

Frees all memory associated with this arp request entry.

If this arp request entry is on the arp request queue, it is removed from the queue.