-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathroute-discovery.c
559 lines (515 loc) · 19 KB
/
route-discovery.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/**
* \addtogroup routediscovery
* @{
*/
/*
* Copyright (c) 2007, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
*/
/**
* \file
* Route discovery protocol(Using LOADng)
* \author
* {Jiahao Liang, Zhikun Liu, Haimo Bai} @ USC
*/
#include "contiki.h"
#include "net/rime.h"
#include "net/rime/route.h"
#include "net/rime/route-discovery.h"
#include <stddef.h> /* For offsetof */
#include <stdio.h>
//This structure stores the <message> field of a RREQ and RREPpacket
//RREQ-Specific and RREP Message
typedef struct general_message{
//uint8_t addr-length:4;
uint8_t type;
uint8_t seqno;
/*if metric_type set to 0 hop_count is used otherwise route_metric is used*/
uint8_t metric_type;
uint32_t route_metric;
uint8_t hop_limit;
uint8_t hop_count;
rimeaddr_t originator;
rimeaddr_t destination;
//TODO:only used for RREP
uint8_t ackrequired;
}rreq_message,rrep_message;
//This structure stores the <message> field of a RREP-ACK packet.
typedef struct rrep_ack_message_struture {
//uint8_t addr-length:4;
uint8_t type;
uint8_t seqno;
rimeaddr_t destination;
}rrep_ack_message;
typedef struct rerr_message_struture {
//TODO:uint8_t addr-length:4;
uint8_t type;
uint8_t hop_limit;
uint8_t errorcode;
rimeaddr_t unreachable;
rimeaddr_t originator;
rimeaddr_t destination;
}rerr_message;
/*------------------------------------------------------------------------------------------------------------------------*/
#if CONTIKI_TARGET_NETSIM
#include "ether.h"
#endif
#define DEBUG 1
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#else
#define PRINTF(...)
#endif
/*------------------------------------------------------------------------------------------------------------------------*/
/*Parameters and constants*/
#define ROUTE_TIMEOUT 5
#define RREP_ACK_TIMEOUT 0.1
#define ROUTE_DISCOVERY_ENTRIES 8
#define MAX_RETRIES 3
#define MAX_REPAIR_RETRIES max_retries
#define NUM_REQ_ENTRIES 8
#define MEAN_BACKOFF 50
#define UNIFORM 1
#define EXPONENTIAL 1-uniform
#define ACK_REQUIRED 1
#define METRICS 0
#define MAX_HOP_COUNT 255
#define MAX_HOP_LIMIT 255
/*Defines*/
#define MAXVALUE 255
#define VERBOSE 1
#define BACKOFF 1
#define HOP_COUNT 0
#define FALSE -1
#define TRUE 0
#define SENDREP 1
#define FORWARD 2
#define DROP 3
#define MAXA(A,B) ( ((A>B)&&((A-B)<=(MAXVALUE/2)))||((A<B)&&((B-A)>(MAXVALUE/2))) )
static uint8_t rreq_seqno = 0;
static uint8_t rrep_seqno = 0;
static char rrep_pending = 0;
/*------------------------------------------------------------------------------------------------------------------------*/
/*check if rreq or rrep is valid return 0 means valid return -1 means invalid*/
//TODO input is rreq or should be con
int valid_check(struct general_message *input, const rimeaddr_t *from){
//address check is skipped;
struct route_entry *rt;
struct blacklist_tuple *bl;
if(rimeaddr_cmp(&input->originator,&rimeaddr_node_addr)){
PRINTF("valid_check: Receive RREQ from itself\n");
return FALSE;
}
rt = route_lookup(&input->originator);
if((rt!=NULL) && MAXA(rt->R_seq_num,input->seqno) ){
PRINTF("valid_check: Receive RREQ originator in already in routing table\n");
return FALSE;
}
//TODO: received address is not present as rimeaddr_t
if(input->type == RREQ_TYPE ){
bl = route_blacklist_lookup(from);
if(bl!=NULL){
PRINTF("valid_check:Receive RREQ previous is in black list\n");
return FALSE;
}
}
return TRUE;
}
/*------------------------------------------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------------------------------------------*/
static void
send_rreq(struct route_discovery_conn *c, rreq_message *input)
{
rreq_message *msg;
packetbuf_clear();
msg = packetbuf_dataptr();
packetbuf_set_datalen(sizeof(rreq_message));
msg->type = RREQ_TYPE;
msg->metric_type = input->metric_type;
msg->route_metric = input->route_metric;
msg->seqno = input->seqno;
msg->hop_count = input->hop_count;
msg->hop_limit = input->hop_limit;
rimeaddr_copy(&msg->destination,&input->destination);
rimeaddr_copy(&msg->originator,&input->originator);
//netflood_send(&c->rreqconn, c->rreq_id);
netflood_send(&c->rreqconn, msg->seqno);
//c->rreq_id++;
PRINTF("send_rreq: %d.%d: send_rreq orig: %d.%d dest: %d.%d "
"route_metric: %d hop_count: %d seqno: %d\n",
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
msg->originator.u8[0], msg->originator.u8[1],
msg->destination.u8[0], msg->destination.u8[1],
msg->route_metric, msg->hop_count, msg->seqno);
}
/*------------------------------------------------------------------------------------------------------------------------*/
static void
send_rrep(struct route_discovery_conn *c, rrep_message *input)
{
struct route_entry *rt;
rrep_message *msg;
packetbuf_clear();
msg = packetbuf_dataptr();
packetbuf_set_datalen(sizeof( rrep_message));
msg->type = RREP_TYPE;
msg->metric_type = input->metric_type;
msg->route_metric = input->route_metric;
msg->seqno = input->seqno;
msg->hop_count = 0;
msg->hop_limit = MAX_HOP_LIMIT;
rimeaddr_copy(&msg->destination,&input->destination);
rimeaddr_copy(&msg->originator,&input->originator);
rt = route_lookup(&msg->destination);
if(rt != NULL) {
PRINTF("send_rrep: %d.%d: send_rrep to %d.%d via %d.%d "
"orig: %d.%d dest: %d.%d route_metric: %d hop_count: %d seqno: %d\n",
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
msg->destination.u8[0],msg->destination.u8[1],
rt->R_next_addr.u8[0],rt->R_next_addr.u8[1],
msg->originator.u8[0], msg->originator.u8[1],
msg->destination.u8[0], msg->destination.u8[1],
msg->route_metric, msg->hop_count, msg->seqno);
unicast_send(&c->rrepconn, &rt->R_next_addr);
} else {
PRINTF("send_rrep: no route entry from %d.%d to %d.%d\n",
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
msg->destination.u8[0],msg->destination.u8[1]);
}
}
/*------------------------------------------------------------------------------------------------------------------------*/
static void
send_rrep_ack(struct route_discovery_conn *c, rrep_ack_message *input)
{
struct route_entry *rt;
rrep_ack_message *msg;
packetbuf_clear();
msg = packetbuf_dataptr();
packetbuf_set_datalen(sizeof(rrep_ack_message));
msg->seqno = input->seqno;
rimeaddr_copy(&msg->destination,&input->destination);
rt = route_lookup(&msg->destination);
if(rt != NULL) {
PRINTF("send_rrep_ack: %d.%d: send_rrep to %d.%d via %d.%d\n",
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
msg->destination.u8[0],msg->destination.u8[1],
rt->R_next_addr.u8[0],rt->R_next_addr.u8[1]);
unicast_send(&c->rrepconn, &rt->R_next_addr);
}
}
/*------------------------------------------------------------------------------------------------------------------------*/
static void
send_rerr(struct route_discovery_conn *c, rerr_message *input)
{
struct route_entry *rt;
rerr_message *msg;
packetbuf_clear();
msg = packetbuf_dataptr();
packetbuf_set_datalen(sizeof( rerr_message));
msg->errorcode = input->errorcode;
msg->hop_limit = input->hop_limit;
rimeaddr_copy(&msg->unreachable,&input->unreachable);
rimeaddr_copy(&msg->destination,&input->destination);
rimeaddr_copy(&msg->originator,&input->originator);
rt = route_lookup(&msg->destination);
if(rt != NULL) {
PRINTF("send_rerr: %d.%d: send_rrep to %d.%d via %d.%d\n",
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
msg->destination.u8[0],msg->destination.u8[1],
rt->R_next_addr.u8[0],rt->R_next_addr.u8[1]);
unicast_send(&c->rrepconn, &rt->R_next_addr);
}
}
/*------------------------------------------------------------------------------------------------------------------------*/
static int
rreq_msg_received(struct netflood_conn *nf, const rimeaddr_t *from)
{
int ret_val = 0;
rreq_message *msg = packetbuf_dataptr();
struct general_message new_msg; //the new msg, can be either rreq pr rrep
struct route_entry *rt;
struct route_discovery_conn *c = (struct route_discovery_conn *)
((char *)nf - offsetof(struct route_discovery_conn, rreqconn));
PRINTF("rreq_msg_received: %d.%d: rreq_packet_received from %d.%d "
"orig: %d.%d dest: %d.%d route_metric: %d hop_count: %d seqno: %d\n",
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
from->u8[0], from->u8[1],
msg->originator.u8[0], msg->originator.u8[1],
msg->destination.u8[0], msg->destination.u8[1],
msg->route_metric, msg->hop_count, msg->seqno);
ret_val = valid_check(msg, from);
if(ret_val!=0){
return ret_val;
}
rimeaddr_copy(&new_msg.destination,&msg->destination);
rimeaddr_copy(&new_msg.originator,&msg->originator);
rt = route_lookup(&msg->originator);
if(rt==NULL){
//TODO:check if route_add done 11.2.7 and check input parameter
//route_add the third parameter should be metric?
struct dist_tuple new_tup;
new_tup.weak_links = 0;
new_tup.route_cost = msg->hop_count + 1;
route_add(&msg->originator, from, &new_tup, msg->seqno);
}
else{
//TODO:
//route check to see if it need to be update
//route_remove(struct route_entry *e);
//route_add();
}
if(rimeaddr_cmp(&msg->destination, &rimeaddr_node_addr)) {
PRINTF("rreq_msg_received: %d.%d: route_packet_received: route request for our address\n",
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);
PRINTF("rreq_msg_received: from %d.%d rssi %d lqi %d\n",
from->u8[0], from->u8[1],
packetbuf_attr(PACKETBUF_ATTR_RSSI),
packetbuf_attr(PACKETBUF_ATTR_LINK_QUALITY));
//generate new rrep
new_msg.type = RREP_TYPE;
new_msg.metric_type = 0;
new_msg.route_metric = 0;
new_msg.seqno = rrep_seqno;
rrep_seqno++;
new_msg.hop_count = 0;
new_msg.hop_limit = MAX_HOP_LIMIT;
rimeaddr_t temp_dest;
rimeaddr_copy(&temp_dest,&new_msg.destination);
rimeaddr_copy(&new_msg.destination,&new_msg.originator);
rimeaddr_copy(&new_msg.originator,&temp_dest);
send_rrep(c, &new_msg);
return SENDREP; /* Don't continue to flood the rreq packet. */
}
else {
/* PRINTF("route request for %d\n", msg->dest_id);*/
PRINTF("rreq_msg_received: from %d.%d rssi %d lqi %d\n",
from->u8[0], from->u8[1],
packetbuf_attr(PACKETBUF_ATTR_RSSI),
packetbuf_attr(PACKETBUF_ATTR_LINK_QUALITY));
if(msg->hop_count < MAX_HOP_COUNT && msg->hop_limit >0){
//TODO:weak link ?
new_msg.hop_count = msg->hop_count + 1;
new_msg.hop_limit = msg->hop_limit - 1;
new_msg.route_metric = msg->hop_count + 1;
new_msg.seqno = msg->seqno;
//msg->metric_type = 0 means use hop otherwise use other metrics
//TODO consider other metrics document 11.2.4 11.2.5
new_msg.metric_type = msg->metric_type;
new_msg.type = RREQ_TYPE;
send_rreq(c,&new_msg);
return FORWARD;
}
}
return DROP;
}
/*------------------------------------------------------------------------------------------------------------------------*/
static int
rrep_msg_received(struct unicast_conn *uc, const rimeaddr_t *from)
{
int ret_val = 0;
rreq_message *msg = packetbuf_dataptr();
rreq_message new_msg;
struct route_entry *rt;
struct route_discovery_conn *c = (struct route_discovery_conn *)
((char *)uc - offsetof(struct route_discovery_conn, rrepconn));
PRINTF("rrep_msg_received: %d.%d: rrep_msg_received from %d.%d "
"orig: %d.%d dest: %d.%d route_metric: %d hop_count: %d seqno: %d\n",
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
from->u8[0], from->u8[1],
msg->originator.u8[0], msg->originator.u8[1],
msg->destination.u8[0], msg->destination.u8[1],
msg->route_metric, msg->hop_count, msg->seqno);
ret_val = valid_check(msg, from);
if(ret_val!=0){
return ret_val;
}
rt = route_lookup(&msg->originator);
if(rt==NULL){
//TODO:check if route_add done 11.2.7 and check input parameter
//route_add the third parameter should be metric?
struct dist_tuple new_tup;
new_tup.weak_links = 0;
new_tup.route_cost = msg->hop_count + 1;
route_add(&msg->originator, from, &new_tup, msg->seqno);
}
else{
//TODO:
//route check to see if it need to be update
//route_remove(struct route_entry *e);
//route_add();
}
/*if(msg->ackrequired){
send_rrep_ack(c,new_msg);
}*/
if(!rimeaddr_cmp(&msg->destination, &rimeaddr_node_addr)) {
//TODO:weak link ?
new_msg.hop_count = msg->hop_count + 1;
new_msg.hop_limit = msg->hop_limit - 1;
new_msg.seqno = msg->seqno;
new_msg.route_metric = msg->hop_count + 1;
new_msg.type = msg->type;
//msg->metric_type = 0 means use hop otherwise use other metrics
//TODO consider other metrics document 11.2.4 11.2.5
new_msg.metric_type = msg->metric_type;
rimeaddr_copy(&new_msg.destination,&msg->destination);
rimeaddr_copy(&new_msg.originator,&msg->originator);
send_rrep(c, &new_msg);
return SENDREP; /* Don't continue to flood the rreq packet. */
}else {
PRINTF("rrep_msg_received: rrep for us!\n");
rrep_pending = 0;
ctimer_stop(&c->t);
if(c->cb->new_route) {
rimeaddr_t originator;
/* If the callback modifies the packet, the originator address
will be lost. Therefore, we need to copy it into a local
variable before calling the callback. */
rimeaddr_copy(&originator, &msg->originator);
c->cb->new_route(c, &originator);
}
}
return TRUE;
}
/*------------------------------------------------------------------------------------------------------------------------*/
static int
rerr_msg_process(struct unicast_conn *uc, const rimeaddr_t *from)
{
rerr_message *msg = packetbuf_dataptr();
rerr_message new_msg;
struct route_entry *rt;
struct route_discovery_conn *c = (struct route_discovery_conn *)
((char *)uc - offsetof(struct route_discovery_conn, rreqconn));
new_msg.hop_limit = msg->hop_limit - 1;
new_msg.type = msg->type;
rt = route_lookup(&msg->unreachable);
if(rt != NULL && rimeaddr_cmp(&rt->R_next_addr,from)){
route_remove(rt);
uint8_t time = 255;
route_blacklist_add(&rt->R_dest_addr,time);
if(msg->hop_limit>0){
send_rerr(c,&new_msg);
}
return 1;
}
send_rerr(c,&new_msg);
return 0;
}
/*------------------------------------------------------------------------------------------------------------------------*/
static const struct unicast_callbacks rrep_callbacks = {rrep_msg_received};
static const struct netflood_callbacks rreq_callbacks = {rreq_msg_received, NULL, NULL};
/*------------------------------------------------------------------------------------------------------------------------*/
void
route_discovery_open(struct route_discovery_conn *c,
clock_time_t time,
uint16_t channels,
const struct route_discovery_callbacks *callbacks)
{
netflood_open(&c->rreqconn, time, channels + 0, &rreq_callbacks);
unicast_open(&c->rrepconn, channels + 1, &rrep_callbacks);
c->cb = callbacks;
PRINTF("route_discovery_open: \n");
}
/*------------------------------------------------------------------------------------------------------------------------*/
void
route_discovery_close(struct route_discovery_conn *c)
{
unicast_close(&c->rrepconn);
netflood_close(&c->rreqconn);
ctimer_stop(&c->t);
PRINTF("route_discovery_close: \n");
}
/*------------------------------------------------------------------------------------------------------------------------*/
static void
timeout_handler(void *ptr)
{
struct route_discovery_conn *c = ptr;
PRINTF("route_discovery: timeout, timed out\n");
rrep_pending = 0;
if(c->cb->timedout) {
c->cb->timedout(c);
}
}
/*------------------------------------------------------------------------------------------------------------------------*/
void rreq_initial(rreq_message *msg,const rimeaddr_t *addr){
msg->type = RREQ_TYPE;
msg->metric_type = 0;
msg->route_metric = 0;
msg->seqno = rreq_seqno;
msg->hop_count = 0;
msg->hop_limit = MAX_HOP_LIMIT;
rimeaddr_copy(&msg->destination,addr);
rimeaddr_copy(&msg->originator,&rimeaddr_node_addr);
rreq_seqno++;
}
/*------------------------------------------------------------------------------------------------------------------------*/
int
route_discovery_discover(struct route_discovery_conn *c, const rimeaddr_t *addr,
clock_time_t timeout)
{
if(rrep_pending) {
PRINTF("route_discovery_send: ignoring request because of pending response\n");
return 0;
}
rreq_message new_msg;
rreq_initial(&new_msg,addr);
PRINTF("route_discovery_send: sending route request\n");
ctimer_set(&c->t, timeout, timeout_handler, c);
rrep_pending = 1;
send_rreq(c, &new_msg);
return 1;
}
/*------------------------------------------------------------------------------------------------------------------------*/
int
route_discovery_repairs(struct route_discovery_conn *c, const rimeaddr_t *addr,
clock_time_t timeout)
{
}
/*------------------------------------------------------------------------------------------------------------------------*/
//generate the err msg
int
route_discovery_rerr(struct route_discovery_conn *c, uint8_t Error_Code,const rimeaddr_t *broken_source_addr,
const rimeaddr_t *broken_dest_addr)
{
//A packet with an RERR message is generated by the LOADng Router,detecting the link breakage
struct route_entry *rt;
rerr_message *msg;
packetbuf_clear();
msg = packetbuf_dataptr();
packetbuf_set_datalen(sizeof( rerr_message));
msg->errorcode = Error_Code;
msg->hop_limit = MAX_HOP_LIMIT;
rimeaddr_copy(&msg->unreachable,broken_dest_addr);
rimeaddr_copy(&msg->destination,broken_source_addr);
rimeaddr_copy(&msg->originator,&rimeaddr_node_addr);
rt = route_lookup(&msg->destination);
if(rt != NULL) {
unicast_send(&c->rrepconn, &rt->R_next_addr);
}
}