-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsr_nat.c
205 lines (168 loc) · 5.14 KB
/
sr_nat.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
#include <signal.h>
#include <assert.h>
#include "sr_nat.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int sr_nat_init (struct sr_nat *nat) { /* Initializes the nat */
assert(nat);
/* Acquire mutex lock */
pthread_mutexattr_init(&(nat->attr));
pthread_mutexattr_settype(&(nat->attr), PTHREAD_MUTEX_RECURSIVE);
int success = pthread_mutex_init(&(nat->lock), &(nat->attr));
/* Initialize timeout thread */
pthread_attr_init(&(nat->thread_attr));
pthread_attr_setdetachstate(&(nat->thread_attr), PTHREAD_CREATE_JOINABLE);
pthread_attr_setscope(&(nat->thread_attr), PTHREAD_SCOPE_SYSTEM);
pthread_attr_setscope(&(nat->thread_attr), PTHREAD_SCOPE_SYSTEM);
pthread_create(&(nat->thread), &(nat->thread_attr), sr_nat_timeout, nat);
/* CAREFUL MODIFYING CODE ABOVE THIS LINE! */
nat->mappings = NULL;
return success;
}
void sr_nat_init_timings (
struct sr_nat *nat,
unsigned int icmp_qt,
unsigned int tcp_eit,
unsigned int tcp_tit
) {
nat->icmp_qt = icmp_qt;
nat->tcp_eit = tcp_eit;
nat->tcp_tit = tcp_tit;
}
/* Destroys the nat (free memory) */
int sr_nat_destroy (struct sr_nat *nat) {
pthread_mutex_lock(&(nat->lock));
/* free nat memory here */
while (nat->mappings) {
/* pthread_mutex_unlock(&(nat->lock)); */
sr_nat_remove_mapping(nat, nat->mappings);
/* pthread_mutex_lock(&(nat->lock)); */
}
pthread_kill(nat->thread, SIGKILL);
return pthread_mutex_destroy(&(nat->lock)) &&
pthread_mutexattr_destroy(&(nat->attr));
}
/* Periodic Timout handling */
void *sr_nat_timeout (void *nat_ptr) {
struct sr_nat *nat = (struct sr_nat *) nat_ptr;
while (1) {
sleep(1.0);
pthread_mutex_lock(&(nat->lock));
time_t curtime = time(NULL);
struct sr_nat_mapping* m_walker = nat->mappings;
while (m_walker) {
if (m_walker->type == nat_mapping_icmp
&& difftime(curtime, m_walker->last_updated) > nat->icmp_qt) {
m_walker->valid = 0;
}
/* TODO: Established vs. Transitory */
if (m_walker->type == nat_mapping_tcp
&& difftime(curtime, m_walker->last_updated) > nat->tcp_tit) {
m_walker->valid = 0;
}
m_walker = m_walker->next;
}
pthread_mutex_unlock(&(nat->lock));
}
return NULL;
}
/* Get the mapping associated with given external port.
You must free the returned structure if it is not NULL. */
struct sr_nat_mapping *sr_nat_lookup_external (
struct sr_nat *nat,
uint16_t aux_ext,
sr_nat_mapping_type type
) {
pthread_mutex_lock(&(nat->lock));
struct sr_nat_mapping *m_walker = nat->mappings, *copy = NULL;
while (m_walker) {
if ((m_walker->aux_ext == aux_ext) &&
(m_walker->type == type) && m_walker->valid) {
break;
}
m_walker = m_walker->next;
}
if (m_walker) {
copy = malloc(sizeof(struct sr_nat_mapping));
memcpy(copy, m_walker, sizeof(struct sr_nat_mapping));
}
pthread_mutex_unlock(&(nat->lock));
return copy;
}
/* Get the mapping associated with given internal (ip, port) pair.
You must free the returned structure if it is not NULL. */
struct sr_nat_mapping *sr_nat_lookup_internal (
struct sr_nat *nat,
uint32_t ip_int,
uint16_t aux_int,
sr_nat_mapping_type type
) {
pthread_mutex_lock(&(nat->lock));
struct sr_nat_mapping *m_walker = nat->mappings, *copy = NULL;
while (m_walker) {
if ((m_walker->ip_int == ip_int) &&
(m_walker->aux_int == aux_int) &&
(m_walker->type == type) && m_walker->valid) {
break;
}
m_walker = m_walker->next;
}
if (m_walker) {
copy = malloc(sizeof(struct sr_nat_mapping));
memcpy(copy, m_walker, sizeof(struct sr_nat_mapping));
}
pthread_mutex_unlock(&(nat->lock));
return copy;
}
/* Insert a new mapping into the nat's mapping table.
Actually returns a copy to the new mapping, for thread safety.
*/
struct sr_nat_mapping *sr_nat_insert_mapping (
struct sr_nat *nat,
uint32_t ip_int,
uint16_t aux_int,
sr_nat_mapping_type type
) {
pthread_mutex_lock(&(nat->lock));
struct sr_nat_mapping *mapping = malloc(sizeof(struct sr_nat_mapping)),
*copy = malloc(sizeof(struct sr_nat_mapping));
mapping->type = type;
mapping->ip_int = ip_int;
mapping->aux_int = aux_int;
mapping->aux_ext = (rand() % (UINT16_MAX - 1024)) + 1025; /* Don't use ports (0-1024) */
mapping->last_updated = time(NULL);
mapping->valid = 1;
mapping->conns = NULL;
mapping->next = nat->mappings;
nat->mappings = mapping;
memcpy(copy, mapping, sizeof(struct sr_nat_mapping));
pthread_mutex_unlock(&(nat->lock));
return copy;
}
/* Remove a mapping in the nat mapping table */
void sr_nat_remove_mapping (
struct sr_nat *nat,
struct sr_nat_mapping* mapping
) {
pthread_mutex_lock(&(nat->lock));
if (mapping) {
struct sr_nat_mapping *curr, *prev = NULL, *next = NULL;
for (curr = nat->mappings; curr != NULL; curr = curr->next) {
if (curr == mapping) {
if (prev) {
next = curr->next;
prev->next = next;
} else {
next = curr->next;
mapping = next;
}
break;
}
prev = curr;
}
free(mapping);
}
pthread_mutex_unlock(&(nat->lock));
}