CS43 Lab7 - A Router Stack
1.2019
|
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...
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_arpentry * | sr_arpcache_lookup (struct sr_arpcache *cache, uint32_t ip) |
Checks if an IP->MAC mapping is in the cache. More... | |
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. More... | |
struct sr_arpreq * | sr_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. | |
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:
The handle_arpreq() function is a function you should write, and it should handle sending ARP requests if necessary:
The ARP reply processing code should move entries from the ARP request queue to the ARP cache:
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.
void sr_arpcache_dump | ( | struct sr_arpcache * | cache | ) |
Prints out the ARP table.
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.
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.
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.
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.