forked from wujingbang/aodv-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgw_list.c
More file actions
executable file
·222 lines (175 loc) · 5.18 KB
/
gw_list.c
File metadata and controls
executable file
·222 lines (175 loc) · 5.18 KB
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
/*
* Author : Krishna Ramachandran
* Date created : 08/26/05
*/
/***************************************************************************
Modified by Miguel Catalan Cid - miguel.catcid@gmail.com - Version: Mon Jan 1 2010
***************************************************************************/
//MCC - Change of discovery procedure - No routes are created
#include "gw_list.h"
extern u_int32_t g_null_ip;
gw_entry *gw_list;
u_int32_t g_default_gw;
//rwlock_t gw_lock = RW_LOCK_UNLOCKED;
DEFINE_RWLOCK(gw_lock);
inline void gw_read_lock(void) {
read_lock_bh(&gw_lock);
}
inline void gw_read_unlock(void) {
read_unlock_bh(&gw_lock);
}
inline void gw_write_lock(void) {
write_lock_bh(&gw_lock);
}
inline void gw_write_unlock(void) {
write_unlock_bh(&gw_lock);
}
void init_gw_list(void){
gw_list = NULL;
g_default_gw = g_null_ip;
}
void delete_gw_list(void){
gw_entry *tmp_entry = gw_list;
gw_entry *prev_entry = NULL;
while (tmp_entry != NULL) {
prev_entry = tmp_entry;
tmp_entry = tmp_entry->next;
kfree(prev_entry);
}
}
int update_gw(u_int32_t gw_ip, u_int32_t dst_id, u_int8_t num_hops,
u_int32_t path_metric) {
gw_entry *tmp_entry = gw_list;
gw_entry *new_entry = NULL;
while (tmp_entry != NULL) {
if (tmp_entry->ip == gw_ip) {
if (tmp_entry->dst_id > dst_id) { //old ST-RREQ
return 0;
} else if ((tmp_entry->dst_id == dst_id) && (tmp_entry->metric
< path_metric)) { //ST-RREQ already received through a better path
return 0;
} else {
tmp_entry->metric = path_metric;
tmp_entry->num_hops = num_hops;
tmp_entry->lifetime = getcurrtime() + ACTIVE_GWROUTE_TIMEOUT;
tmp_entry->dst_id = dst_id;
return 1;
}
}
tmp_entry = tmp_entry->next;
}
new_entry = (gw_entry *) kmalloc(sizeof(gw_entry), GFP_ATOMIC);
if (new_entry == NULL) {
printk("GW_entry: Can't allocate new entry\n");
return 1;
}
gw_write_lock(); //to avoid conflicts with read_proc and gateway assignation (netfilter socket) (uncontrolled interruption)
new_entry->ip = gw_ip;
new_entry->metric = path_metric;
new_entry->num_hops = num_hops;
new_entry->lifetime = getcurrtime() + ACTIVE_GWROUTE_TIMEOUT;
new_entry->dst_id = dst_id;
new_entry->next = NULL;
if (gw_list == NULL) {
gw_list = new_entry;
}
else {
new_entry->next = gw_list;
gw_list = new_entry;
}
gw_write_unlock();
return 1;
}
void delete_gw_list_entry(u_int32_t gw_ip) {
gw_entry *tmp_entry = gw_list;
gw_entry *prev_entry = NULL;
gw_write_lock(); //to avoid conflicts with read_proc and gateway assignation (netfilter socket) (uncontrolled interruption)
while (tmp_entry != NULL) {
if (tmp_entry->ip == gw_ip) {
if (prev_entry != NULL) {
prev_entry->next = tmp_entry->next;
} else
gw_list = tmp_entry->next;
gw_write_unlock();
kfree(tmp_entry);
return;
}
prev_entry = tmp_entry;
tmp_entry = tmp_entry->next;
}
gw_write_unlock();
}
int is_gw(u_int32_t ip) {
gw_entry *tmp_entry = gw_list;
gw_read_lock(); //called in gwuser_assignation (netlink interruption) - avoid possible conflicts
while (tmp_entry != NULL) {
if (tmp_entry->ip == ip) {
gw_read_unlock();
return 1;
}
tmp_entry = tmp_entry->next;
}
gw_read_unlock();
return 0;
}
void update_gw_lifetimes() {
u_int64_t currtime = getcurrtime();
gw_entry *tmp_entry = gw_list;
u_int32_t best_gw = g_null_ip;
u_int32_t best_metric = DEFAULT_ETT_METRIC;
while (tmp_entry != NULL) {
if (time_after( (unsigned long) currtime,
(unsigned long) tmp_entry->lifetime)) {
printk("timing out gateway %s\n", inet_ntoa(tmp_entry->ip));
delete_gw_list_entry(tmp_entry->ip);
}
else{
if (tmp_entry->metric < best_metric){
best_gw = tmp_entry->ip;
best_metric = tmp_entry->metric;
}
}
tmp_entry = tmp_entry->next;
}
g_default_gw = best_gw;
}
/****************************************************
read_gw_proc
----------------------------------------------------
Writes the gw list to /proc/gateways
MCC: Simplified version for UAMN assistance
****************************************************/
int read_gw_list(char *buffer, char **buffer_location, off_t offset,
int buffer_length, int *eof, void *data) {
static char *my_buffer;
char temp_buffer[200];
int len;
gw_entry *tmp_entry;
my_buffer = buffer;
gw_read_lock();
tmp_entry = gw_list;
sprintf(my_buffer, "\n");
while (tmp_entry != NULL) {
sprintf(temp_buffer, " IP: %s", inet_ntoa(tmp_entry->ip));
strcat(my_buffer, temp_buffer);
sprintf(temp_buffer, " Metric: %u", tmp_entry->metric);
strcat(my_buffer, temp_buffer);
sprintf(temp_buffer, " Hops: %d", tmp_entry->num_hops);
strcat(my_buffer, temp_buffer);
if (tmp_entry->ip == g_default_gw)
strcat(my_buffer, " default\n");
else
strcat(my_buffer, "\n");
tmp_entry = tmp_entry->next;
}
strcat(my_buffer, "\n");
gw_read_unlock();
len = strlen(my_buffer);
*buffer_location = my_buffer + offset;
len -= offset;
if (len > buffer_length)
len = buffer_length;
else if (len < 0)
len = 0;
return len;
}