forked from wujingbang/aodv-Android
/
rerr.c
executable file
·406 lines (333 loc) · 13.1 KB
/
rerr.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/***************************************************************************
rerr.c - description
-------------------
begin : Mon Aug 11 2003
copyright : (C) 2003 by Luke Klein-Berndt
email : kleinb@nist.gov
***************************************************************************/
/***************************************************************************
Modified by Miguel Catalan Cid - miguel.catcid@gmail.com - Version: Mon Jan 1 2010
***************************************************************************/
#include "rerr.h"
extern u_int32_t g_mesh_ip;
extern u_int32_t g_null_ip;
extern u_int8_t g_aodv_gateway;
extern u_int8_t g_routing_metric;
int reply_to_rrer(u_int32_t source, u_int32_t destination) {
if (source == g_mesh_ip || (source == g_null_ip && g_aodv_gateway))
return 1;
return 0;
}
//brk_dst_ipΪ�ѶϿ���·��Ŀ��ip
int gen_rerr(u_int32_t brk_dst_ip) {
aodv_route *tmp_route;
rerr *tmp_rerr;
int expired_routes = 0;
//�ҵ���һ��aodv·��
tmp_route = first_aodv_route();
//go through list
//����������һ��Ϊbrk_dst_ip��·����Ŀ�������ýڵ㷢��rerr����Ч����·��
while (tmp_route != NULL) {
//printk("tmp_route->next_hop is %s\n",inet_ntoa(tmp_route->next_hop));
if ((tmp_route->next_hop == brk_dst_ip) && (!tmp_route->self_route)&& (!tmp_route->neigh_route)){
if (!reply_to_rrer(tmp_route->src_ip, tmp_route->dst_ip)) { //i'm source of the route, don't send the rerr
if (tmp_route->state != INVALID) {
if ((tmp_rerr = (rerr *) kmalloc(sizeof(rerr), GFP_ATOMIC))
== NULL) {
#ifdef DEBUG
printk("Can't allocate new rrep\n");
#endif
return 0;
}
tmp_rerr->type = RERR_MESSAGE;
tmp_rerr->dst_count = 0; //unused
tmp_rerr->reserved = 0;
tmp_rerr->n = 0;
tmp_rerr->num_hops = 0;
tmp_rerr->dst_ip = tmp_route->dst_ip;
tmp_rerr->dst_id = htonl(tmp_route->dst_id);
#ifdef DTN
//if there is DTN register,current node will be the
//last DTN hop near the brk node
extern int dtn_register;
tmp_rerr->last_avail_ip = NULL;
tmp_rerr->src_ip = tmp_route->src_ip;
if(dtn_register==1)
{
tmp_rerr->last_avail_ip = g_mesh_ip;
//printk("last_avail_ip:%s\n",inet_ntoa(tmp_rerr->last_avail_ip));
//notice DTN layer
#include <linux/sched.h>
//JL: Added kernel threads interface:
#include <linux/kthread.h>
u_int32_t para[4];
para[0] = tmp_rerr->src_ip;
para[1] = tmp_rerr->dst_ip;
para[2] = tmp_rerr->last_avail_ip;
//���ʹ���
para[3] = (u_int32_t)tmp_rerr->type;
//
send2dtn((void*)para,DTNPORT);
}
else tmp_rerr->last_avail_ip = NULL;
#endif
#ifdef CaiDebug
//cai:test gen rerr
char local[20];
strcpy(local,inet_ntoa(g_mesh_ip));
char brk[20];
strcpy(brk,inet_ntoa(brk_dst_ip));
char last[20];
strcpy(last,inet_ntoa(tmp_route->last_hop));
printk("rerr src is %s,brk_dst_ip is %s,the last hop is %s\n",local,brk,last);
#endif
send_message(tmp_route->last_hop, NET_DIAMETER, tmp_rerr,sizeof(rerr));
kfree(tmp_rerr);
#ifdef RECOVERYPATH
/****************************************************
������·��������ÿ������һ���µ�rerr��ʱ��Ҳ
�½�һ��brk_list����Ŀ
*****************************************************/
brk_link *tmp_link;
u_int32_t lastavail=NULL;
#ifdef DTN
if(dtn_register==1)
{
lastavail = g_mesh_ip;
}
#endif
tmp_link = find_brk_link(tmp_route->src_ip,tmp_route->dst_ip);
if(tmp_link != NULL){//�Ѵ��ڣ������������ڣ������������ٳ���
//Update brk_link timelife������Ҫ��ʱ���Ĵ���
tmp_link->lifetime = getcurrtime() + BRK_LINK_TIMEOUT;
flush_brk_list();
}
else{
tmp_link = create_brk_link(tmp_route->src_ip,tmp_route->dst_ip,
tmp_route->last_hop,lastavail);//����������
if(tmp_link==NULL) printk("Creat new brk link failed!\n");
#ifdef CaiDebug
char s[20];
char d[20];
char l[20];
char la[20];
strcpy(s,inet_ntoa(tmp_route->src_ip));
strcpy(d,inet_ntoa(tmp_route->dst_ip));
strcpy(l,inet_ntoa(tmp_route->last_hop));
strcpy(la,inet_ntoa(lastavail));
printk("new brk link %s:%s:%s:%s\n",s,d,l,la);
#endif
flush_brk_list();
}
/******************************************************************/
#endif
}//route is not invalid
}//not the source of the route
#ifdef RECOVERYPATH
else{
/**************************************************** *****************************************************/
#ifdef DTN
//if there is DTN register,current node will be the
//last DTN hop near the brk node
extern int dtn_register;
u_int32_t last_avail;//put them out of #ifdef DTN or in is a Q
last_avail = NULL;
if(dtn_register==1)
{
last_avail = g_mesh_ip;
//notice DTN layer
#include <linux/sched.h>
//JL: Added kernel threads interface:
#include <linux/kthread.h>
u_int32_t para[4];
para[0] = tmp_route->src_ip;
para[1] = tmp_route->dst_ip;
para[2] = last_avail;
//���ʹ���
para[3] = (u_int32_t)RERR_MESSAGE;
//
send2dtn((void*)para,DTNPORT);
}
#endif
brk_link *tmp_link;
tmp_link = find_brk_link(tmp_route->src_ip,tmp_route->dst_ip);
if(tmp_link != NULL){
tmp_link->lifetime = getcurrtime() + BRK_LINK_TIMEOUT;
flush_brk_list();
}
else{
tmp_link = create_brk_link(tmp_route->src_ip,tmp_route->dst_ip,
NULL,last_avail);//notice the last hop is NULL
if(tmp_link==NULL) printk("Creat new brk link failed!\n");
#ifdef CaiDebug
char s[20];
char d[20];
char l[20];
char la[20];
strcpy(s,inet_ntoa(tmp_route->src_ip));
strcpy(d,inet_ntoa(tmp_route->dst_ip));
strcpy(l,"0.0.0.0");
strcpy(la,inet_ntoa(last_avail));
printk("new brk link %s:%s:%s:%s\n",s,d,l,la);
#endif
flush_brk_list();
}
/******************************************************************/
}//the source
#endif
if (tmp_route->state != INVALID){
expire_aodv_route(tmp_route);
expired_routes++;
#ifdef CaiDebug
char local[20];
strcpy(local,inet_ntoa(g_mesh_ip));
char last[20];
strcpy(last,inet_ntoa(tmp_route->last_hop));
printk("invalidate tmp_route %s to %s,expored_route is %d\n",local,last,expired_routes);
#endif
}
}
//move on to the next entry
tmp_route = tmp_route->next;
}
if (g_routing_metric == WCIM && expired_routes != 0)
update_my_load();
return 0;
}
int recv_rerr(task * tmp_packet) {
aodv_route *tmp_route;
rerr *tmp_rerr;
int num_hops;
int expired_routes = 0;
tmp_rerr = (rerr *) tmp_packet->data;
num_hops = tmp_rerr->num_hops+1;
#ifdef CaiDebug
printk("Recieved a route error from %s\n", inet_ntoa(tmp_packet->src_ip));
#endif
#ifdef DTN
extern int dtn_register;
int first_dtn=0;
#include <linux/sched.h>
//JL: Added kernel threads interface:
#include <linux/kthread.h>
u_int32_t para[4];
para[0] = tmp_rerr->src_ip;
para[1] = tmp_rerr->dst_ip;
para[2] = tmp_rerr->last_avail_ip;
para[3] = (u_int32_t)tmp_rerr->type;
if( dtn_register==1 ){
//����ע��DTN����lastavailΪ�ջ��߽��յ���tmp_rerr��СС�ڶ�����DTN��rerr
if( para[2]==NULL )//current node is the nearest DTN node to brk link
{
para[2] = g_mesh_ip;
first_dtn = 1;
}
// kthread_run(&send2dtn, (void*)para, "send2dtn");
send2dtn((void*)para,DTNPORT);//tmp_rerr->dst_ip, tmp_rerr->last_avail_ip);
}
#endif
tmp_route
= find_aodv_route_by_id(tmp_rerr->dst_ip, ntohl(tmp_rerr->dst_id));
if (tmp_route && tmp_route->next_hop == tmp_packet->src_ip) {
//if there is any hop before me
if (!reply_to_rrer(tmp_route->src_ip, tmp_route->dst_ip)) {
if (tmp_route->state != INVALID) { //route with active traffic
tmp_rerr->type = RERR_MESSAGE;
tmp_rerr->dst_count = 0; //unused
tmp_rerr->reserved = 0;
tmp_rerr->n = 0;
tmp_rerr->num_hops = num_hops;
tmp_rerr->dst_ip = tmp_route->dst_ip;
tmp_rerr->dst_id = htonl(tmp_route->dst_id);
#ifdef DTN
tmp_rerr->src_ip = tmp_route->src_ip;
if(dtn_register==1 && first_dtn==1)
{
tmp_rerr->last_avail_ip = g_mesh_ip;
}
#endif
send_message(tmp_route->last_hop, NET_DIAMETER, tmp_rerr,
sizeof(rerr));
#ifdef RECOVERYPATH
/****************************************************
������·��������ÿ������һ���µ�rerr��ʱ��Ҳ
�½�һ��brk_list����Ŀ
*****************************************************/
brk_link *tmp_link;
tmp_link = find_brk_link(tmp_route->src_ip,tmp_route->dst_ip);
if(tmp_link != NULL){//�Ѵ��ڣ������������ڣ������������ٳ���
//Update brk_link timelife������Ҫ��ʱ���Ĵ���
tmp_link->lifetime = getcurrtime() + BRK_LINK_TIMEOUT;
flush_brk_list();
}
else{
tmp_link = create_brk_link(tmp_route->src_ip,tmp_route->dst_ip,
tmp_route->last_hop,tmp_rerr->last_avail_ip);//����������,ע���˴���ast_avail_ipӦ���Զ�·��
if(tmp_link==NULL) printk("Creat new brk link failed!\n");
#ifdef CaiDebug
char s[20];
char d[20];
char l[20];
char la[20];
strcpy(s,inet_ntoa(tmp_route->src_ip));
strcpy(d,inet_ntoa(tmp_route->dst_ip));
strcpy(l,inet_ntoa(tmp_route->last_hop));
strcpy(la,inet_ntoa(tmp_rerr->last_avail_ip));
printk("new brk link %s:%s:%s:%s\n",s,d,l,la);
#endif
flush_brk_list();
}
/******************************************************************/
#endif
}
}//not the source
#ifdef RECOVERYPATH
else{
/****************************************************
������·��������ÿ������һ���µ�rerr��ʱ��Ҳ
�½�һ��brk_list����Ŀ
*****************************************************/
#ifdef DTN
u_int32_t last_avail;
last_avail = NULL;
if(dtn_register==1 && first_dtn==1)
{
last_avail = g_mesh_ip;
}
#endif
brk_link *tmp_link;
tmp_link = find_brk_link(tmp_route->src_ip,tmp_route->dst_ip);
if(tmp_link != NULL){//�Ѵ��ڣ������������ڣ������������ٳ���
//Update brk_link timelife������Ҫ��ʱ���Ĵ���
tmp_link->lifetime = getcurrtime() + BRK_LINK_TIMEOUT;
flush_brk_list();
}
else{
tmp_link = create_brk_link(tmp_route->src_ip,tmp_route->dst_ip,
NULL,last_avail);//notice the last hop is NULL
if(tmp_link==NULL) printk("Creat new brk link failed!\n");
#ifdef CaiDebug
char s[20];
char d[20];
char l[20];
char la[20];
strcpy(s,inet_ntoa(tmp_route->src_ip));
strcpy(d,inet_ntoa(tmp_route->dst_ip));
strcpy(l,"0.0.0.0");
strcpy(la,inet_ntoa(last_avail));
printk("new brk link %s:%s:%s:%s\n",s,d,l,la);
#endif
flush_brk_list();
}
/******************************************************************/
}//the source
#endif
if (tmp_route->state != INVALID) {
expire_aodv_route(tmp_route);
expired_routes++;
}
}
if (g_routing_metric == WCIM && expired_routes != 0)
update_my_load();
return 0;
}