-
Notifications
You must be signed in to change notification settings - Fork 0
/
sr_arpcache.c
263 lines (212 loc) · 8.02 KB
/
sr_arpcache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <netinet/in.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <sched.h>
#include <string.h>
#include "sr_arpcache.h"
#include "sr_router.h"
#include "sr_if.h"
#include "sr_protocol.h"
/*
This function gets called every second. For each request sent out, we keep
checking whether we should resend an request or destroy the arp request.
See the comments in the header file for an idea of what it should look like.
*/
void sr_arpcache_sweepreqs(struct sr_instance *sr) {
/*pthread_mutex_lock(&(sr->cache.lock));*/
if (!sr || !sr->cache.requests)
return;
struct sr_arpreq * reqs = sr->cache.requests;
struct sr_arpreq * nxt = reqs->next;
handle_arpreq(sr, reqs);
while(nxt)
{
reqs = nxt;
nxt = reqs->next;
handle_arpreq(sr, reqs);
}
/*pthread_mutex_unlock(&(sr->cache.lock));*/
}
/* You should not need to touch the rest of this code. */
/* 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_arpentry *sr_arpcache_lookup(struct sr_arpcache *cache, uint32_t ip) {
pthread_mutex_lock(&(cache->lock));
struct sr_arpentry *entry = NULL, *copy = NULL;
int i;
for (i = 0; i < SR_ARPCACHE_SZ; i++) {
if ((cache->entries[i].valid) && (cache->entries[i].ip == ip)) {
entry = &(cache->entries[i]);
}
}
/* Must return a copy b/c another thread could jump in and modify
table after we return. */
if (entry) {
copy = (struct sr_arpentry *) malloc(sizeof(struct sr_arpentry));
memcpy(copy, entry, sizeof(struct sr_arpentry));
}
pthread_mutex_unlock(&(cache->lock));
return copy;
}
/* 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. You should free the passed *packet.
A pointer to the ARP request is returned; it should not be freed. The caller
can remove the ARP request from the queue by calling sr_arpreq_destroy. */
struct sr_arpreq *sr_arpcache_queuereq(struct sr_arpcache *cache,
uint32_t ip,
uint8_t *packet, /* borrowed */
unsigned int packet_len,
char *iface)
{
pthread_mutex_lock(&(cache->lock));
struct sr_arpreq *req;
for (req = cache->requests; req != NULL; req = req->next) {
if (req->ip == ip) {
break;
}
}
/* If the IP wasn't found, add it */
if (!req) {
req = (struct sr_arpreq *) calloc(1, sizeof(struct sr_arpreq));
req->ip = ip;
req->next = cache->requests;
cache->requests = req;
}
/* Add the packet to the list of packets for this request */
if (packet && packet_len && iface) {
struct sr_packet *new_pkt = (struct sr_packet *)malloc(sizeof(struct sr_packet));
new_pkt->buf = (uint8_t *)malloc(packet_len);
memcpy(new_pkt->buf, packet, packet_len);
new_pkt->len = packet_len;
new_pkt->iface = (char *)malloc(sr_IFACE_NAMELEN);
strncpy(new_pkt->iface, iface, sr_IFACE_NAMELEN);
new_pkt->next = req->packets;
req->packets = new_pkt;
}
pthread_mutex_unlock(&(cache->lock));
return req;
}
/* 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_arpreq *sr_arpcache_insert(struct sr_arpcache *cache,
unsigned char *mac,
uint32_t ip)
{
pthread_mutex_lock(&(cache->lock));
struct sr_arpreq *req, *prev = NULL, *next = NULL;
for (req = cache->requests; req != NULL; req = req->next) {
if (req->ip == ip) {
if (prev) {
next = req->next;
prev->next = next;
}
else {
next = req->next;
cache->requests = next;
}
break;
}
prev = req;
}
int i;
for (i = 0; i < SR_ARPCACHE_SZ; i++) {
if (!(cache->entries[i].valid))
break;
}
if (i != SR_ARPCACHE_SZ) {
memcpy(cache->entries[i].mac, mac, 6);
cache->entries[i].ip = ip;
cache->entries[i].added = time(NULL);
cache->entries[i].valid = 1;
}
pthread_mutex_unlock(&(cache->lock));
return req;
}
/* 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. */
void sr_arpreq_destroy(struct sr_arpcache *cache, struct sr_arpreq *entry) {
pthread_mutex_lock(&(cache->lock));
if (entry) {
struct sr_arpreq *req, *prev = NULL, *next = NULL;
for (req = cache->requests; req != NULL; req = req->next) {
if (req == entry) {
if (prev) {
next = req->next;
prev->next = next;
}
else {
next = req->next;
cache->requests = next;
}
break;
}
prev = req;
}
struct sr_packet *pkt, *nxt;
for (pkt = entry->packets; pkt; pkt = nxt) {
nxt = pkt->next;
if (pkt->buf)
free(pkt->buf);
if (pkt->iface)
free(pkt->iface);
free(pkt);
}
free(entry);
}
pthread_mutex_unlock(&(cache->lock));
}
/* Prints out the ARP table. */
void sr_arpcache_dump(struct sr_arpcache *cache) {
fprintf(stderr, "\nMAC IP ADDED VALID\n");
fprintf(stderr, "-----------------------------------------------------------\n");
int i;
for (i = 0; i < SR_ARPCACHE_SZ; i++) {
struct sr_arpentry *cur = &(cache->entries[i]);
unsigned char *mac = cur->mac;
fprintf(stderr, "%.1x%.1x%.1x%.1x%.1x%.1x %.8x %.24s %d\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], ntohl(cur->ip), ctime(&(cur->added)), cur->valid);
}
fprintf(stderr, "\n");
}
/* Initialize table + table lock. Returns 0 on success. */
int sr_arpcache_init(struct sr_arpcache *cache) {
/* Seed RNG to kick out a random entry if all entries full. */
srand(time(NULL));
/* Invalidate all entries */
memset(cache->entries, 0, sizeof(cache->entries));
cache->requests = NULL;
/* Acquire mutex lock */
pthread_mutexattr_init(&(cache->attr));
pthread_mutexattr_settype(&(cache->attr), PTHREAD_MUTEX_RECURSIVE);
int success = pthread_mutex_init(&(cache->lock), &(cache->attr));
return success;
}
/* Destroys table + table lock. Returns 0 on success. */
int sr_arpcache_destroy(struct sr_arpcache *cache) {
return pthread_mutex_destroy(&(cache->lock)) && pthread_mutexattr_destroy(&(cache->attr));
}
/* Thread which sweeps through the cache and invalidates entries that were added
more than SR_ARPCACHE_TO seconds ago. */
void *sr_arpcache_timeout(void *sr_ptr) {
struct sr_instance *sr = sr_ptr;
struct sr_arpcache *cache = &(sr->cache);
while (1) {
sleep(1.0);
pthread_mutex_lock(&(cache->lock));
time_t curtime = time(NULL);
int i;
for (i = 0; i < SR_ARPCACHE_SZ; i++) {
if ((cache->entries[i].valid) && (difftime(curtime,cache->entries[i].added) > SR_ARPCACHE_TO)) {
cache->entries[i].valid = 0;
}
}
sr_arpcache_sweepreqs(sr);
pthread_mutex_unlock(&(cache->lock));
}
return NULL;
}