blob: c04aedaf26df239bfa57793324ba384312c8a0c6 [file] [log] [blame]
akmhoque8a5babe2012-08-16 17:39:33 -05001#include<stdio.h>
2#include<string.h>
3#include<stdlib.h>
4#include <unistd.h>
5#include <getopt.h>
6#include <sys/time.h>
7#include <assert.h>
8#ifdef HAVE_CONFIG_H
9#include <config.h>
10#endif
11
12
13#include <ccn/ccn.h>
14#include <ccn/uri.h>
15#include <ccn/keystore.h>
16#include <ccn/signing.h>
17#include <ccn/schedule.h>
18#include <ccn/hashtb.h>
19
20#include "nlsr.h"
akmhoque03004e62012-09-06 01:12:28 -050021#include "nlsr_npl.h"
akmhoque8a5babe2012-08-16 17:39:33 -050022#include "nlsr_adl.h"
akmhoque03004e62012-09-06 01:12:28 -050023#include "utility.h"
akmhoque8a5babe2012-08-16 17:39:33 -050024
akmhoque03004e62012-09-06 01:12:28 -050025void
26add_nbr_to_adl(struct name_prefix *new_nbr,int face)
akmhoque8a5babe2012-08-16 17:39:33 -050027{
akmhoque03004e62012-09-06 01:12:28 -050028 struct ndn_neighbor *nbr=(struct ndn_neighbor *)malloc(sizeof(struct ndn_neighbor )); //free
akmhoque53f64222012-09-05 13:57:51 -050029
akmhoque8a5babe2012-08-16 17:39:33 -050030 struct hashtb_enumerator ee;
31 struct hashtb_enumerator *e = &ee;
32 int res;
33
34 hashtb_start(nlsr->adl, e);
akmhoque03004e62012-09-06 01:12:28 -050035 res = hashtb_seek(e, new_nbr->name, new_nbr->length, 0);
akmhoque2852a222012-08-21 12:09:00 -040036
akmhoquee7c4b6d2012-08-21 12:30:25 -040037 if(res == HT_NEW_ENTRY )
38 {
akmhoque8a5babe2012-08-16 17:39:33 -050039
akmhoque53f64222012-09-05 13:57:51 -050040 nbr = e->data;
akmhoque8a5babe2012-08-16 17:39:33 -050041
akmhoque03004e62012-09-06 01:12:28 -050042 nbr->neighbor=(struct name_prefix *)malloc(sizeof( struct name_prefix )); //free
43 nbr->neighbor->name=(char *)malloc(new_nbr->length);
44 memcpy(nbr->neighbor->name,new_nbr->name,new_nbr->length);
45 nbr->neighbor->length=new_nbr->length;
akmhoque53f64222012-09-05 13:57:51 -050046 nbr->face=face;
47 nbr->status=NBR_DOWN;
48 nbr->info_interest_timed_out=0;
49 nbr->lsdb_synch_interval=300;
50 nbr->metric=10;
51 nbr->is_lsdb_send_interest_scheduled=0;
52
akmhoque8a5babe2012-08-16 17:39:33 -050053
akmhoque03004e62012-09-06 01:12:28 -050054 char *time_stamp=(char *)malloc(20);
55 get_current_timestamp_micro(time_stamp);
56 nbr->last_lsdb_version=(char *)malloc(strlen(time_stamp)+1); //free
akmhoque53f64222012-09-05 13:57:51 -050057 memcpy(nbr->last_lsdb_version,time_stamp,strlen(time_stamp)+1);
58 memset(nbr->last_lsdb_version,'0',strlen(time_stamp));
akmhoque03004e62012-09-06 01:12:28 -050059 nbr->last_info_version=(char *)malloc(strlen(time_stamp)+1); //free
60 memcpy(nbr->last_info_version,time_stamp,strlen(time_stamp)+1);
61 memset(nbr->last_info_version,'0',strlen(time_stamp));
62 free(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -050063
64 nbr->last_lsdb_requested=0;
akmhoquee7c4b6d2012-08-21 12:30:25 -040065 }
akmhoque53f64222012-09-05 13:57:51 -050066
akmhoque8a5babe2012-08-16 17:39:33 -050067 hashtb_end(e);
akmhoque53f64222012-09-05 13:57:51 -050068}
akmhoque8a5babe2012-08-16 17:39:33 -050069
akmhoque03004e62012-09-06 01:12:28 -050070
akmhoque53f64222012-09-05 13:57:51 -050071void
72print_adjacent(struct ndn_neighbor *nbr)
73{
akmhoque03004e62012-09-06 01:12:28 -050074 printf("print_adjacent called\n");
akmhoque53f64222012-09-05 13:57:51 -050075 printf("--------Neighbor---------------------------\n");
76 printf(" Neighbor: %s \n",nbr->neighbor->name);
77 printf(" Length : %d \n",nbr->neighbor->length);
78 printf(" Face : %d \n",nbr->face);
79 printf(" Metric : %d \n",nbr->metric);
80 printf(" Status : %d \n",nbr->status);
81 printf(" LSDB Version: %s \n",nbr->last_lsdb_version);
akmhoque03004e62012-09-06 01:12:28 -050082 printf(" Info Version: %s \n",nbr->last_info_version);
akmhoque53f64222012-09-05 13:57:51 -050083 printf(" Info Interest Timed Out : %d \n",nbr->info_interest_timed_out);
84 printf(" LSDB Synch Interval : %ld \n",nbr->lsdb_synch_interval);
85 printf(" Las Time LSDB Requested: %ld \n",nbr->last_lsdb_requested);
akmhoque03004e62012-09-06 01:12:28 -050086 printf(" IS_lsdb_send_interest_scheduled : %d \n",nbr->is_lsdb_send_interest_scheduled);
87
88 printf("\n");
akmhoque8a5babe2012-08-16 17:39:33 -050089
90}
91
92void
93print_adjacent_from_adl(void)
94{
akmhoque53f64222012-09-05 13:57:51 -050095
akmhoque8a5babe2012-08-16 17:39:33 -050096 printf("print_adjacent_from_adl called \n");
97 int i, adl_element;
98 struct ndn_neighbor *nbr;
99
100 struct hashtb_enumerator ee;
101 struct hashtb_enumerator *e = &ee;
102
103 hashtb_start(nlsr->adl, e);
104 adl_element=hashtb_n(nlsr->adl);
105
106 for(i=0;i<adl_element;i++)
107 {
108 nbr=e->data;
akmhoque53f64222012-09-05 13:57:51 -0500109 print_adjacent(nbr);
akmhoque8a5babe2012-08-16 17:39:33 -0500110 hashtb_next(e);
111 }
112
113 hashtb_end(e);
114
akmhoque8a5babe2012-08-16 17:39:33 -0500115}
116
akmhoque03004e62012-09-06 01:12:28 -0500117int
118get_adjacent_status(struct name_prefix *nbr)
119{
120 printf("get_adjacent_status called \n");
121
122 int res;
123 int status=-1;
124 struct ndn_neighbor *nnbr;
125
126 struct hashtb_enumerator ee;
127 struct hashtb_enumerator *e = &ee;
128
129 hashtb_start(nlsr->adl, e);
130 res = hashtb_seek(e, nbr->name, nbr->length, 0);
131
132 if (res == HT_OLD_ENTRY)
133 {
134 nnbr=e->data;
135 status=nnbr->status;
136 }
137 else if(res == HT_NEW_ENTRY)
138 {
139 hashtb_delete(e);
140 }
141
142 hashtb_end(e);
143
144 return status;
145
146}
147
148int
149get_timed_out_number(struct name_prefix *nbr)
150{
151 printf("get_timed_out_number called \n");
152
153 int res,ret=-1;
154 struct ndn_neighbor *nnbr;
155
156 struct hashtb_enumerator ee;
157 struct hashtb_enumerator *e = &ee;
158
159 hashtb_start(nlsr->adl, e);
160 res = hashtb_seek(e, nbr->name, nbr->length, 0);
161
162 if( res == HT_OLD_ENTRY )
163 {
164 nnbr=e->data;
165 ret=nnbr->info_interest_timed_out;
166 }
167 else if(res == HT_NEW_ENTRY)
168 {
169 hashtb_delete(e);
170 }
171
172 hashtb_end(e);
173
174 return ret;
175}
176
177
178void
179update_adjacent_timed_out_to_adl(struct name_prefix *nbr, int increment)
180{
181 printf("update_adjacent_timed_out_to_adl called \n");
182
183 int res;
184 struct ndn_neighbor *nnbr;
185
186 struct hashtb_enumerator ee;
187 struct hashtb_enumerator *e = &ee;
188
189 hashtb_start(nlsr->adl, e);
190 res = hashtb_seek(e, nbr->name, nbr->length, 0);
191
192 if( res == HT_OLD_ENTRY )
193 {
194 nnbr=e->data;
195 nnbr->info_interest_timed_out += increment;
196 }
197 else if(res == HT_NEW_ENTRY)
198 {
199 hashtb_delete(e);
200 }
201
202 hashtb_end(e);
203}
204
205void
206update_adjacent_timed_out_zero_to_adl(struct name_prefix *nbr)
207{
208 printf("update_adjacent_timed_out_zero_to_adl called \n");
209 int time_out_number=get_timed_out_number(nbr);
210 update_adjacent_timed_out_to_adl(nbr,-time_out_number);
211
212}
213
akmhoque8a5babe2012-08-16 17:39:33 -0500214void
akmhoque53f64222012-09-05 13:57:51 -0500215update_adjacent_status_to_adl(struct name_prefix *nbr, int status)
akmhoque8a5babe2012-08-16 17:39:33 -0500216{
217 printf("update_adjacent_status_to_adl called \n");
akmhoque03004e62012-09-06 01:12:28 -0500218
akmhoque8a5babe2012-08-16 17:39:33 -0500219 int res;
220 struct ndn_neighbor *nnbr;
221
222 struct hashtb_enumerator ee;
223 struct hashtb_enumerator *e = &ee;
224
225 hashtb_start(nlsr->adl, e);
akmhoque53f64222012-09-05 13:57:51 -0500226 res = hashtb_seek(e, nbr->name, nbr->length, 0);
227
akmhoque918ff9a2012-08-21 11:34:49 -0400228
akmhoquee7c4b6d2012-08-21 12:30:25 -0400229 if (res == HT_OLD_ENTRY)
230 {
231 nnbr=e->data;
akmhoque42098b12012-08-27 22:54:23 -0500232 if ( nnbr->status!=status )
233 {
234 nnbr->status=status;
235 nlsr->adj_build_flag++;
236 }
akmhoquee7c4b6d2012-08-21 12:30:25 -0400237 }
akmhoqueb903ded2012-08-21 12:56:28 -0400238 else if(res == HT_NEW_ENTRY)
239 {
240 hashtb_delete(e);
241 }
akmhoque53f64222012-09-05 13:57:51 -0500242
akmhoque8a5babe2012-08-16 17:39:33 -0500243 hashtb_end(e);
244}
245
akmhoque8a5babe2012-08-16 17:39:33 -0500246void
akmhoque53f64222012-09-05 13:57:51 -0500247update_lsdb_synch_interval_to_adl(struct name_prefix *nbr, long int interval)
akmhoque8a5babe2012-08-16 17:39:33 -0500248{
akmhoque53f64222012-09-05 13:57:51 -0500249 printf("uupdate_lsdb_synch_interval_to_adl called \n");
akmhoque8a5babe2012-08-16 17:39:33 -0500250
251 int res;
252 struct ndn_neighbor *nnbr;
253
254 struct hashtb_enumerator ee;
255 struct hashtb_enumerator *e = &ee;
256
257 hashtb_start(nlsr->adl, e);
akmhoque53f64222012-09-05 13:57:51 -0500258 res = hashtb_seek(e, nbr->name, nbr->length, 0);
akmhoque8a5babe2012-08-16 17:39:33 -0500259
akmhoque53f64222012-09-05 13:57:51 -0500260
261 if (res == HT_OLD_ENTRY)
akmhoquee7c4b6d2012-08-21 12:30:25 -0400262 {
263 nnbr=e->data;
akmhoque53f64222012-09-05 13:57:51 -0500264 if ( nnbr->lsdb_synch_interval!= interval )
265 {
266 nnbr->lsdb_synch_interval=interval;
akmhoque03004e62012-09-06 01:12:28 -0500267
akmhoque53f64222012-09-05 13:57:51 -0500268 }
akmhoquee7c4b6d2012-08-21 12:30:25 -0400269 }
akmhoqueb903ded2012-08-21 12:56:28 -0400270 else if(res == HT_NEW_ENTRY)
271 {
272 hashtb_delete(e);
273 }
akmhoque53f64222012-09-05 13:57:51 -0500274
akmhoque8a5babe2012-08-16 17:39:33 -0500275 hashtb_end(e);
276}
akmhoqued79438d2012-08-27 13:31:42 -0500277
akmhoque53f64222012-09-05 13:57:51 -0500278
akmhoque53f64222012-09-05 13:57:51 -0500279int
akmhoque03004e62012-09-06 01:12:28 -0500280no_active_nbr(void)
akmhoque53f64222012-09-05 13:57:51 -0500281{
akmhoque03004e62012-09-06 01:12:28 -0500282 int i, adl_element;
283 int no_link=0;
284 struct ndn_neighbor *nbr;
akmhoqued79438d2012-08-27 13:31:42 -0500285 struct hashtb_enumerator ee;
286 struct hashtb_enumerator *e = &ee;
akmhoque03004e62012-09-06 01:12:28 -0500287 hashtb_start(nlsr->adl, e);
288 adl_element=hashtb_n(nlsr->adl);
akmhoqued79438d2012-08-27 13:31:42 -0500289
akmhoque03004e62012-09-06 01:12:28 -0500290 for(i=0;i<adl_element;i++)
akmhoqued79438d2012-08-27 13:31:42 -0500291 {
akmhoque03004e62012-09-06 01:12:28 -0500292 nbr=e->data;
293 if( nbr->status == 1 )
294 no_link++;
295 hashtb_next(e);
akmhoqued79438d2012-08-27 13:31:42 -0500296 }
akmhoque53f64222012-09-05 13:57:51 -0500297
akmhoqued79438d2012-08-27 13:31:42 -0500298 hashtb_end(e);
299
akmhoque03004e62012-09-06 01:12:28 -0500300 return no_link;
akmhoque53f64222012-09-05 13:57:51 -0500301
akmhoqued79438d2012-08-27 13:31:42 -0500302}
akmhoque53f64222012-09-05 13:57:51 -0500303
304int
305is_adj_lsa_build(void)
306{
307 int ret=0;
308
309 int nbr_count=0;
310
311 int i, adl_element;
312 struct ndn_neighbor *nbr;
313
314 struct hashtb_enumerator ee;
315 struct hashtb_enumerator *e = &ee;
316
317 hashtb_start(nlsr->adl, e);
318 adl_element=hashtb_n(nlsr->adl);
319
320 for(i=0;i<adl_element;i++)
321 {
322 nbr=e->data;
323 if(nbr->status == 1 )
324 {
325 nbr_count++;
326 }
327 else if (nbr->info_interest_timed_out >= nlsr->interest_retry)
328 {
329 nbr_count++;
330 }
331 hashtb_next(e);
332 }
333
334 hashtb_end(e);
335 if(nbr_count == adl_element)
336 ret=1;
akmhoque03004e62012-09-06 01:12:28 -0500337
akmhoque53f64222012-09-05 13:57:51 -0500338 return ret;
339}
340
akmhoque53f64222012-09-05 13:57:51 -0500341
342void
343get_active_nbr_adj_data(struct ccn_charbuf *c)
344{
345
346 int i, adl_element;
347 struct ndn_neighbor *nbr;
348 struct hashtb_enumerator ee;
349 struct hashtb_enumerator *e = &ee;
350 hashtb_start(nlsr->adl, e);
351 adl_element=hashtb_n(nlsr->adl);
352
353 for(i=0;i<adl_element;i++)
354 {
355 nbr=e->data;
356 if( nbr->status == 1 )
357 {
358 ccn_charbuf_append_string(c,nbr->neighbor->name);
359 ccn_charbuf_append_string(c,"|");
akmhoque03004e62012-09-06 01:12:28 -0500360
akmhoque53f64222012-09-05 13:57:51 -0500361 char *temp_length=(char *)malloc(20);
362 memset(temp_length,0,20);
363 sprintf(temp_length,"%d",nbr->neighbor->length);
364 ccn_charbuf_append_string(c,temp_length);
365 free(temp_length);
366 ccn_charbuf_append_string(c,"|");
367
368 char *temp_face=(char *)malloc(20);
369 memset(temp_face,0,20);
370 sprintf(temp_face,"%d",nbr->face);
371 ccn_charbuf_append_string(c,temp_face);
372 free(temp_face);
373 ccn_charbuf_append_string(c,"|");
374
375 char *temp_metric=(char *)malloc(20);
376 memset(temp_metric,0,20);
377 sprintf(temp_metric,"%d",nbr->metric);
378 ccn_charbuf_append_string(c,temp_metric);
379 free(temp_metric);
380 ccn_charbuf_append_string(c,"|");
381
382 }
383 hashtb_next(e);
384 }
385
386 hashtb_end(e);
387}
388
akmhoque03004e62012-09-06 01:12:28 -0500389long int
390get_nbr_time_diff_lsdb_req(char *nbr)
391{
392 printf("get_nbr_time_diff_lsdb_req called \n");
393
394 long int time_diff=get_lsdb_synch_interval(nbr)+1;
395 int res;
396 struct ndn_neighbor *nnbr;
397
398 struct hashtb_enumerator ee;
399 struct hashtb_enumerator *e = &ee;
400
401 hashtb_start(nlsr->adl, e);
402 res = hashtb_seek(e, nbr, strlen(nbr)+1, 0);
403
404 if (res == HT_OLD_ENTRY)
405 {
406 nnbr=e->data;
407
408 if (nnbr->last_lsdb_requested == 0)
409 time_diff=get_lsdb_synch_interval(nbr)+1;
410 else time_diff=get_current_time_sec() - get_nbr_last_lsdb_requested(nbr);
411
412 }
413 else if(res == HT_NEW_ENTRY)
414 {
415 hashtb_delete(e);
416 }
417
418 hashtb_end(e);
419
420 return time_diff;
421}
422
423long int
424get_nbr_last_lsdb_requested(char *nbr)
425{
426 printf("get_timed_out_number called \n");
427
428 long int last_lsdb_requested=0;
429
430 int res;
431 struct ndn_neighbor *nnbr;
432
433 struct hashtb_enumerator ee;
434 struct hashtb_enumerator *e = &ee;
435
436 hashtb_start(nlsr->adl, e);
437 res = hashtb_seek(e, nbr, strlen(nbr)+1, 0);
438
439 if (res == HT_OLD_ENTRY)
440 {
441 nnbr=e->data;
442 last_lsdb_requested=nnbr->last_lsdb_requested;
443 }
444 else if(res == HT_NEW_ENTRY)
445 {
446 hashtb_delete(e);
447 }
448
449 hashtb_end(e);
450
451 return last_lsdb_requested;
452}
453
454long int
455get_lsdb_synch_interval(char *nbr)
456{
457 printf("get_lsdb_synch_interval called \n");
458
459 long int lsdb_synch_interval=300;
460
461
462 int res;
463 struct ndn_neighbor *nnbr;
464
465 struct hashtb_enumerator ee;
466 struct hashtb_enumerator *e = &ee;
467
468 hashtb_start(nlsr->adl, e);
469 res = hashtb_seek(e, nbr, strlen(nbr)+1, 0);
470
471 if (res == HT_OLD_ENTRY)
472 {
473 nnbr=e->data;
474 lsdb_synch_interval=nnbr->lsdb_synch_interval;
475 }
476 else if(res == HT_NEW_ENTRY)
477 {
478 hashtb_delete(e);
479 }
480
481 hashtb_end(e);
482
483 return lsdb_synch_interval;
484
485}
486
akmhoque53f64222012-09-05 13:57:51 -0500487char *
488get_nbr_lsdb_version(char *nbr)
489{
490 printf("get_timed_out_number called \n");
491
492 char *version=NULL;
493
494 int res;
495 struct ndn_neighbor *nnbr;
496
497 struct hashtb_enumerator ee;
498 struct hashtb_enumerator *e = &ee;
499
500 hashtb_start(nlsr->adl, e);
501 res = hashtb_seek(e, nbr, strlen(nbr)+1, 0);
akmhoque03004e62012-09-06 01:12:28 -0500502
akmhoque53f64222012-09-05 13:57:51 -0500503 if (res == HT_OLD_ENTRY)
504 {
505 nnbr=e->data;
506 version=(char *)malloc(strlen(nnbr->last_lsdb_version)+1);
507 memset(version,0,strlen(nnbr->last_lsdb_version)+1);
508 memcpy(version,nnbr->last_lsdb_version,strlen(nnbr->last_lsdb_version)+1);
509 }
510 else if(res == HT_NEW_ENTRY)
511 {
512 hashtb_delete(e);
513 }
514
515 hashtb_end(e);
516
517 return version;
518}
519
520void
521update_adjacent_last_lsdb_requested_to_adl(char *nbr, long int timestamp)
522{
523 printf("update_adjacent_last_lsdb_requested_to_adl called \n");
524
525 int res;
526 struct ndn_neighbor *nnbr;
527
528 struct hashtb_enumerator ee;
529 struct hashtb_enumerator *e = &ee;
530
531 hashtb_start(nlsr->adl, e);
532 res = hashtb_seek(e, nbr, strlen(nbr)+1, 0);
533
534 if( res == HT_OLD_ENTRY )
535 {
536 nnbr=e->data;
537 nnbr->last_lsdb_requested=timestamp;
akmhoque03004e62012-09-06 01:12:28 -0500538
539 }
540 else if(res == HT_NEW_ENTRY)
541 {
542 hashtb_delete(e);
543 }
544
545 hashtb_end(e);
546}
547
548void
549set_is_lsdb_send_interest_scheduled_to_zero(char *nbr)
550{
551 printf("set_is_lsdb_send_interest_scheduled_to_zero called \n");
552
553 int res;
554 struct ndn_neighbor *nnbr;
555
556 struct hashtb_enumerator ee;
557 struct hashtb_enumerator *e = &ee;
558
559 hashtb_start(nlsr->adl, e);
560 res = hashtb_seek(e, nbr, strlen(nbr)+1, 0);
561
562 if (res == HT_OLD_ENTRY)
563 {
564 nnbr=e->data;
565 nnbr->is_lsdb_send_interest_scheduled=0;
566 }
567 else if(res == HT_NEW_ENTRY)
568 {
569 hashtb_delete(e);
570 }
571
572 hashtb_end(e);
573}
574
575void
576update_adjacent_lsdb_version_to_adl(struct name_prefix *nbr, char *version)
577{
578 printf("update_adjacent_timed_out_to_adl called \n");
579
580 int res;
581 struct ndn_neighbor *nnbr;
582
583 struct hashtb_enumerator ee;
584 struct hashtb_enumerator *e = &ee;
585
586 hashtb_start(nlsr->adl, e);
587 res = hashtb_seek(e, nbr->name, nbr->length, 0);
588
589 if( res == HT_OLD_ENTRY )
590 {
591 nnbr=e->data;
592 free(nnbr->last_lsdb_version);
593 nnbr->last_lsdb_version=(char *)malloc(strlen(version)+1);
594 memset(nnbr->last_lsdb_version,0,strlen(version)+1);
595 memcpy(nnbr->last_lsdb_version,version,strlen(version)+1);
akmhoque53f64222012-09-05 13:57:51 -0500596 }
597 else if(res == HT_NEW_ENTRY)
598 {
599 hashtb_delete(e);
600 }
601
602 hashtb_end(e);
603
604}
605
606void
607adjust_adjacent_last_lsdb_requested_to_adl(char *nbr, long int sec)
608{
609 printf("update_adjacent_last_lsdb_requested_to_adl called \n");
610
611 int res;
612 struct ndn_neighbor *nnbr;
613
614 struct hashtb_enumerator ee;
615 struct hashtb_enumerator *e = &ee;
616
617 hashtb_start(nlsr->adl, e);
618 res = hashtb_seek(e, nbr, strlen(nbr)+1, 0);
619
620 if( res == HT_OLD_ENTRY )
621 {
622 nnbr=e->data;
623 nnbr->last_lsdb_requested=nnbr->last_lsdb_requested-sec;
akmhoque03004e62012-09-06 01:12:28 -0500624
akmhoque53f64222012-09-05 13:57:51 -0500625 }
626 else if(res == HT_NEW_ENTRY)
627 {
628 hashtb_delete(e);
629 }
630
631 hashtb_end(e);
632
633}
634
635