CS43 Lab7 - A Router Stack  1.2019
sr_arpcache.h
Go to the documentation of this file.
1 /**
2  * \file
3  * This file defines an ARP cache (\link sr_arpcache \endlink), which is made of two structures: an ARP
4  * request queue (\link sr_arpreq \endlink), and ARP cache entries (\link sr_arpentry \endlink).
5  *
6  * Here, we present pseudocode for use of these structures.
7  * \n
8  *
9  * The outline for sending a packet using the ARP cache is as follows:
10  * \code{.cpp}
11  * // When sending packet to next_hop_ip
12  * entry = arpcache_lookup(next_hop_ip)
13  * if entry:
14  * use next_hop_ip->mac mapping in entry to send the packet
15  * free entry
16  * else:
17  * req = arpcache_queuereq(next_hop_ip, packet, len)
18  * handle_arpreq(req)
19  * \endcode
20  * \n
21  *
22  * The handle_arpreq() function is a function you should write, and it should
23  * handle sending ARP requests if necessary:
24  * \code{.cpp}
25  * function handle_arpreq(req):
26  * if difftime(now, req->sent) > 1.0
27  * if req->times_sent >= 5:
28  * send icmp host unreachable to source addr of all pkts waiting
29  * on this request
30  * arpreq_destroy(req)
31  * else:
32  * send arp request
33  * req->sent = now
34  * req->times_sent++
35  * \endcode
36  * \n
37  *
38  * The ARP reply processing code should move entries from the ARP request
39  * queue to the ARP cache:
40  * \code{.cpp}
41  * // When servicing an arp reply that gives us an IP->MAC mapping
42  * req = arpcache_insert(ip, mac)
43  * if req:
44  * send all packets on the req->packets linked list
45  * arpreq_destroy(req)
46  * \endcode
47  * \n
48  *
49  * Since handle_arpreq as defined in the comments above could destroy your
50  * current request, make sure to save the next pointer before calling
51  * handle_arpreq when traversing through the ARP requests linked list.
52  */
53 
54 #ifndef SR_ARPCACHE_H
55 #define SR_ARPCACHE_H
56 
57 #include <inttypes.h>
58 #include <time.h>
59 #include <pthread.h>
60 #include "sr_if.h"
61 
62 #define SR_ARPCACHE_SZ 100
63 #define SR_ARPCACHE_TO 15.0
64 
65 /**
66  * A packet queued, waiting for the resolution of an \link sr_arpreq \endlink.
67  */
68 struct sr_packet {
69  uint8_t *buf; /**< The raw Ethernet frame of the packet, presumably with the dest MAC empty */
70  unsigned int len; /**< Length of the raw ethernet frame */
71  char *iface; /**< The outgoing interface */
72  struct sr_packet *next; /**< A pointer to the next packet waiting on the same request.
73  * NULL if there are no more packets. */
74 };
75 
76 /**
77  * An entry in the ARP cache.
78  */
79 struct sr_arpentry {
80  unsigned char mac[6]; /**< MAC address of the MAC/IP mapping. */
81  uint32_t ip; /**< IP addr in network byte order of the MAC/IP mapping. */
82  time_t added; /**< Time that the entry was added.
83  * The entry invalidated after SR_ARPCACHE_TO seconds.
84  * This is already tracked for you.*/
85  int valid; /**< Boolean indicating whether or not the entry is valid.
86  * This is already tracked for you*/
87 };
88 
89 /**
90  * An outstanding ARP request with queued packets waiting on the response.
91  */
92 struct sr_arpreq {
93  uint32_t ip; /**< The IP trying to be resolved by the request. */
94  time_t sent; /**< Last time this ARP request was sent.
95  * You should update this. If the ARP request
96  * was never sent, will be 0. */
97  uint32_t times_sent; /**< Number of times this request was sent.
98  * You should update this. */
99  struct sr_packet *packets; /**< List of \link sr_packet \endlink waiting
100  on this request to finish. */
101  struct sr_arpreq *next; /**< Pointer to the next outstanding ARP request.
102  * NULL if there are no more in the linked list. */
103 };
104 
105 /**
106  * An ARP cache which tracks IP/MAC mappings, as well as outstanding ARP requests.
107  */
108 struct sr_arpcache {
109  struct sr_arpentry entries[SR_ARPCACHE_SZ]; /**< The list of cached IP/MAC mappings. */
110  struct sr_arpreq *requests; /**< The linked list of outstanding ARP requests. */
111  pthread_mutex_t lock; /**< A lock to ensure that the cache isn't
112  * concurrently modified by multiple threads,
113  * you shouldn't need to use this, the starter code
114  * handles it for you. */
115  pthread_mutexattr_t attr; /**< Attributes for the mutex lock, you shouldn't
116  * need to use this, the starter code handles it
117  * for you. */
118 };
119 
120 /**
121  * Checks if an IP->MAC mapping is in the cache. IP is in network byte order.
122  * You must free the returned structure if it is not NULL.
123  * */
124 struct sr_arpentry *sr_arpcache_lookup(struct sr_arpcache *cache, uint32_t ip);
125 
126 /** Adds an ARP request to the ARP request queue. If the request is already on
127  * the queue, adds the packet to the linked list of packets for this sr_arpreq
128  * that corresponds to this ARP request. The packet argument should not be
129  * freed by the caller.
130 
131  * A pointer to the ARP request is returned; it should be freed. The caller
132  * can remove the ARP request from the queue by calling sr_arpreq_destroy.
133  */
134 struct sr_arpreq *sr_arpcache_queuereq(struct sr_arpcache *cache,
135  uint32_t ip,
136  uint8_t *packet, /* borrowed */
137  unsigned int packet_len,
138  char *iface);
139 
140 /**
141  * Inserts an ARP cache entry.
142  * This method performs two functions:
143  * 1) Looks up this IP in the request queue. If it is found, returns a pointer
144  * to the sr_arpreq with this IP. Otherwise, returns NULL.
145  * 2) Inserts this IP to MAC mapping in the cache, and marks it valid.
146  * */
147 struct sr_arpreq *sr_arpcache_insert(struct sr_arpcache *cache,
148  unsigned char *mac,
149  uint32_t ip);
150 
151 /** Frees all memory associated with this arp request entry. If this arp request
152  * entry is on the arp request queue, it is removed from the queue.
153  * */
154 void sr_arpreq_destroy(struct sr_arpcache *cache, struct sr_arpreq *entry);
155 
156 /** Prints out the ARP table. */
157 void sr_arpcache_dump(struct sr_arpcache *cache);
158 
159 /* You shouldn't have to call these methods--they're already called in the
160  starter code for you. The init call is a constructor, the destroy call is
161  a destructor, and a cleanup thread times out cache entries every 15
162  seconds. */
163 
164 /**
165  * \link sr_arpcache \endlink constructor, you shouldn't need to call this, the
166  * starter code calls it for you.
167  * */
168 int sr_arpcache_init(struct sr_arpcache *cache);
169 
170 /**
171  * \link sr_arpcache \endlink destructor, you shouldn't need to call this, the
172  * starter code calls it for you.
173  * */
174 int sr_arpcache_destroy(struct sr_arpcache *cache);
175 
176 /**
177  * A cleanup command which clears out ARP cache entries every 15 seconds,
178  * you shouldn't need to call this, it is called on a separte thread by
179  * the starter code for you.
180  * */
181 void *sr_arpcache_timeout(void *cache_ptr);
182 
183 #endif
uint32_t ip
IP addr in network byte order of the MAC/IP mapping.
Definition: sr_arpcache.h:81
pthread_mutexattr_t attr
Attributes for the mutex lock, you shouldn&#39;t need to use this, the starter code handles it for you...
Definition: sr_arpcache.h:115
uint32_t ip
The IP trying to be resolved by the request.
Definition: sr_arpcache.h:93
struct sr_packet * packets
List of sr_packet waiting on this request to finish.
Definition: sr_arpcache.h:99
time_t added
Time that the entry was added.
Definition: sr_arpcache.h:82
unsigned int len
Length of the raw ethernet frame.
Definition: sr_arpcache.h:70
void sr_arpreq_destroy(struct sr_arpcache *cache, struct sr_arpreq *entry)
Frees all memory associated with this arp request entry.
Definition: sr_arpcache.c:153
An ARP cache which tracks IP/MAC mappings, as well as outstanding ARP requests.
Definition: sr_arpcache.h:108
struct sr_arpreq * sr_arpcache_insert(struct sr_arpcache *cache, unsigned char *mac, uint32_t ip)
Inserts an ARP cache entry.
Definition: sr_arpcache.c:110
char * iface
The outgoing interface.
Definition: sr_arpcache.h:71
uint8_t * buf
The raw Ethernet frame of the packet, presumably with the dest MAC empty.
Definition: sr_arpcache.h:69
An entry in the ARP cache.
Definition: sr_arpcache.h:79
An outstanding ARP request with queued packets waiting on the response.
Definition: sr_arpcache.h:92
struct sr_arpreq * requests
The linked list of outstanding ARP requests.
Definition: sr_arpcache.h:110
Data structures and methods for handeling interfaces.
int valid
Boolean indicating whether or not the entry is valid.
Definition: sr_arpcache.h:85
struct sr_packet * next
A pointer to the next packet waiting on the same request.
Definition: sr_arpcache.h:72
A packet queued, waiting for the resolution of an sr_arpreq.
Definition: sr_arpcache.h:68
struct sr_arpentry * sr_arpcache_lookup(struct sr_arpcache *cache, uint32_t ip)
Checks if an IP->MAC mapping is in the cache.
Definition: sr_arpcache.c:34
void sr_arpcache_dump(struct sr_arpcache *cache)
Prints out the ARP table.
Definition: sr_arpcache.c:192
int sr_arpcache_destroy(struct sr_arpcache *cache)
sr_arpcache destructor, you shouldn&#39;t need to call this, the starter code calls it for you...
Definition: sr_arpcache.c:224
void * sr_arpcache_timeout(void *cache_ptr)
A cleanup command which clears out ARP cache entries every 15 seconds, you shouldn&#39;t need to call thi...
Definition: sr_arpcache.c:230
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.
Definition: sr_arpcache.c:65
pthread_mutex_t lock
A lock to ensure that the cache isn&#39;t concurrently modified by multiple threads, you shouldn&#39;t need t...
Definition: sr_arpcache.h:111
int sr_arpcache_init(struct sr_arpcache *cache)
sr_arpcache constructor, you shouldn&#39;t need to call this, the starter code calls it for you...
Definition: sr_arpcache.c:207
time_t sent
Last time this ARP request was sent.
Definition: sr_arpcache.h:94
struct sr_arpreq * next
Pointer to the next outstanding ARP request.
Definition: sr_arpcache.h:101
uint32_t times_sent
Number of times this request was sent.
Definition: sr_arpcache.h:97