blob: 7ebc8afc167e67ec85504013b1bfc6726035b4c0 [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"
21#include "nlsr_ndn.h"
22#include "utility.h"
23#include "nlsr_adl.h"
24
25void
akmhoque53f64222012-09-05 13:57:51 -050026add_adjacent_to_adl(struct name_prefix *np, int face)
akmhoque8a5babe2012-08-16 17:39:33 -050027{
28 printf("\nadd_adjacent_to_adl called\n");
akmhoque53f64222012-09-05 13:57:51 -050029 printf("Neighbor: %s Length: %d Face:%d\n",np->name,np->length,face);
akmhoque8a5babe2012-08-16 17:39:33 -050030
akmhoque53f64222012-09-05 13:57:51 -050031 struct ndn_neighbor *nbr=(struct ndn_neighbor *)malloc(sizeof(struct ndn_neighbor));
32
akmhoque8a5babe2012-08-16 17:39:33 -050033 struct hashtb_enumerator ee;
34 struct hashtb_enumerator *e = &ee;
35 int res;
36
37 hashtb_start(nlsr->adl, e);
akmhoque53f64222012-09-05 13:57:51 -050038 res = hashtb_seek(e, np->name, np->length, 0);
akmhoque2852a222012-08-21 12:09:00 -040039
akmhoquee7c4b6d2012-08-21 12:30:25 -040040 if(res == HT_NEW_ENTRY )
41 {
akmhoque8a5babe2012-08-16 17:39:33 -050042
akmhoque53f64222012-09-05 13:57:51 -050043 nbr = e->data;
akmhoque8a5babe2012-08-16 17:39:33 -050044
akmhoque53f64222012-09-05 13:57:51 -050045 nbr->neighbor=(struct name_prefix *)malloc(sizeof( struct name_prefix ));
46 nbr->neighbor->name=(char *)malloc(np->length);
47 memcpy(nbr->neighbor->name,np->name,np->length);
48 nbr->neighbor->length=np->length;
49 nbr->face=face;
50 nbr->status=NBR_DOWN;
51 nbr->info_interest_timed_out=0;
52 nbr->lsdb_synch_interval=300;
53 nbr->metric=10;
54 nbr->is_lsdb_send_interest_scheduled=0;
55
56
akmhoque8a5babe2012-08-16 17:39:33 -050057
akmhoque53f64222012-09-05 13:57:51 -050058 char *time_stamp=get_current_timestamp_micro();
59 nbr->last_lsdb_version=(char *)malloc(strlen(time_stamp)+1);
60 memcpy(nbr->last_lsdb_version,time_stamp,strlen(time_stamp)+1);
61 memset(nbr->last_lsdb_version,'0',strlen(time_stamp));
62
63 nbr->last_lsdb_requested=0;
64
65 //nbr->last_lsdb_requested=(char *)malloc(strlen(time_stamp)+1);
66 //memcpy(nbr->last_lsdb_requested,time_stamp,strlen(time_stamp)+1);
67 //memset(nbr->last_lsdb_requested,'0',strlen(time_stamp));
68
akmhoquee7c4b6d2012-08-21 12:30:25 -040069 }
akmhoque53f64222012-09-05 13:57:51 -050070
akmhoque8a5babe2012-08-16 17:39:33 -050071 hashtb_end(e);
akmhoque53f64222012-09-05 13:57:51 -050072}
akmhoque8a5babe2012-08-16 17:39:33 -050073
akmhoque53f64222012-09-05 13:57:51 -050074void
75print_adjacent(struct ndn_neighbor *nbr)
76{
77 printf("\nprint_adjacent called\n");
78 printf("--------Neighbor---------------------------\n");
79 printf(" Neighbor: %s \n",nbr->neighbor->name);
80 printf(" Length : %d \n",nbr->neighbor->length);
81 printf(" Face : %d \n",nbr->face);
82 printf(" Metric : %d \n",nbr->metric);
83 printf(" Status : %d \n",nbr->status);
84 printf(" LSDB Version: %s \n",nbr->last_lsdb_version);
85 printf(" Info Interest Timed Out : %d \n",nbr->info_interest_timed_out);
86 printf(" LSDB Synch Interval : %ld \n",nbr->lsdb_synch_interval);
87 printf(" Las Time LSDB Requested: %ld \n",nbr->last_lsdb_requested);
akmhoque8a5babe2012-08-16 17:39:33 -050088
89}
90
91void
92print_adjacent_from_adl(void)
93{
akmhoque53f64222012-09-05 13:57:51 -050094
akmhoque8a5babe2012-08-16 17:39:33 -050095 printf("print_adjacent_from_adl called \n");
96 int i, adl_element;
97 struct ndn_neighbor *nbr;
98
99 struct hashtb_enumerator ee;
100 struct hashtb_enumerator *e = &ee;
101
102 hashtb_start(nlsr->adl, e);
103 adl_element=hashtb_n(nlsr->adl);
104
105 for(i=0;i<adl_element;i++)
106 {
107 nbr=e->data;
akmhoque53f64222012-09-05 13:57:51 -0500108 print_adjacent(nbr);
akmhoque8a5babe2012-08-16 17:39:33 -0500109 hashtb_next(e);
110 }
111
112 hashtb_end(e);
113
akmhoque8a5babe2012-08-16 17:39:33 -0500114}
115
116void
akmhoque53f64222012-09-05 13:57:51 -0500117update_adjacent_status_to_adl(struct name_prefix *nbr, int status)
akmhoque8a5babe2012-08-16 17:39:33 -0500118{
119 printf("update_adjacent_status_to_adl called \n");
akmhoque53f64222012-09-05 13:57:51 -0500120 //int ret;
akmhoque8a5babe2012-08-16 17:39:33 -0500121 int res;
122 struct ndn_neighbor *nnbr;
123
124 struct hashtb_enumerator ee;
125 struct hashtb_enumerator *e = &ee;
126
127 hashtb_start(nlsr->adl, e);
akmhoque53f64222012-09-05 13:57:51 -0500128 res = hashtb_seek(e, nbr->name, nbr->length, 0);
129
akmhoque918ff9a2012-08-21 11:34:49 -0400130
akmhoquee7c4b6d2012-08-21 12:30:25 -0400131 if (res == HT_OLD_ENTRY)
132 {
133 nnbr=e->data;
akmhoque42098b12012-08-27 22:54:23 -0500134 if ( nnbr->status!=status )
135 {
136 nnbr->status=status;
137 nlsr->adj_build_flag++;
138 }
akmhoquee7c4b6d2012-08-21 12:30:25 -0400139 }
akmhoqueb903ded2012-08-21 12:56:28 -0400140 else if(res == HT_NEW_ENTRY)
141 {
142 hashtb_delete(e);
143 }
akmhoque53f64222012-09-05 13:57:51 -0500144
akmhoque8a5babe2012-08-16 17:39:33 -0500145 hashtb_end(e);
146}
147
akmhoque8a5babe2012-08-16 17:39:33 -0500148void
akmhoque53f64222012-09-05 13:57:51 -0500149update_lsdb_synch_interval_to_adl(struct name_prefix *nbr, long int interval)
akmhoque8a5babe2012-08-16 17:39:33 -0500150{
akmhoque53f64222012-09-05 13:57:51 -0500151 printf("uupdate_lsdb_synch_interval_to_adl called \n");
akmhoque8a5babe2012-08-16 17:39:33 -0500152
153 int res;
154 struct ndn_neighbor *nnbr;
155
156 struct hashtb_enumerator ee;
157 struct hashtb_enumerator *e = &ee;
158
159 hashtb_start(nlsr->adl, e);
akmhoque53f64222012-09-05 13:57:51 -0500160 res = hashtb_seek(e, nbr->name, nbr->length, 0);
akmhoque8a5babe2012-08-16 17:39:33 -0500161
akmhoque53f64222012-09-05 13:57:51 -0500162
163 if (res == HT_OLD_ENTRY)
akmhoquee7c4b6d2012-08-21 12:30:25 -0400164 {
165 nnbr=e->data;
akmhoque53f64222012-09-05 13:57:51 -0500166 if ( nnbr->lsdb_synch_interval!= interval )
167 {
168 nnbr->lsdb_synch_interval=interval;
169
170 }
akmhoquee7c4b6d2012-08-21 12:30:25 -0400171 }
akmhoqueb903ded2012-08-21 12:56:28 -0400172 else if(res == HT_NEW_ENTRY)
173 {
174 hashtb_delete(e);
175 }
akmhoque53f64222012-09-05 13:57:51 -0500176
akmhoque8a5babe2012-08-16 17:39:33 -0500177 hashtb_end(e);
akmhoque53f64222012-09-05 13:57:51 -0500178
179
akmhoque8a5babe2012-08-16 17:39:33 -0500180}
akmhoqued79438d2012-08-27 13:31:42 -0500181
akmhoque53f64222012-09-05 13:57:51 -0500182
akmhoqued79438d2012-08-27 13:31:42 -0500183void
akmhoque53f64222012-09-05 13:57:51 -0500184update_adjacent_timed_out_to_adl(struct name_prefix *nbr, int increment)
akmhoqued79438d2012-08-27 13:31:42 -0500185{
186 printf("update_adjacent_timed_out_to_adl called \n");
187
188 int res;
189 struct ndn_neighbor *nnbr;
190
191 struct hashtb_enumerator ee;
192 struct hashtb_enumerator *e = &ee;
193
194 hashtb_start(nlsr->adl, e);
akmhoque53f64222012-09-05 13:57:51 -0500195 res = hashtb_seek(e, nbr->name, nbr->length, 0);
akmhoqued79438d2012-08-27 13:31:42 -0500196
197 if( res == HT_OLD_ENTRY )
198 {
199 nnbr=e->data;
200 nnbr->info_interest_timed_out += increment;
201 }
202 else if(res == HT_NEW_ENTRY)
203 {
204 hashtb_delete(e);
205 }
akmhoque53f64222012-09-05 13:57:51 -0500206
akmhoqued79438d2012-08-27 13:31:42 -0500207 hashtb_end(e);
208}
209
210void
akmhoque53f64222012-09-05 13:57:51 -0500211update_adjacent_timed_out_zero_to_adl(struct name_prefix *nbr)
akmhoqued79438d2012-08-27 13:31:42 -0500212{
akmhoqued79438d2012-08-27 13:31:42 -0500213 printf("update_adjacent_timed_out_zero_to_adl called \n");
214 int time_out_number=get_timed_out_number(nbr);
215 update_adjacent_timed_out_to_adl(nbr,-time_out_number);
216
217}
218
219
akmhoque53f64222012-09-05 13:57:51 -0500220void
221update_adjacent_lsdb_version_to_adl(struct name_prefix *nbr, char *version)
akmhoqued79438d2012-08-27 13:31:42 -0500222{
akmhoque53f64222012-09-05 13:57:51 -0500223 printf("update_adjacent_timed_out_to_adl called \n");
akmhoqued79438d2012-08-27 13:31:42 -0500224
akmhoque53f64222012-09-05 13:57:51 -0500225 int res;
226 struct ndn_neighbor *nnbr;
227
228 struct hashtb_enumerator ee;
229 struct hashtb_enumerator *e = &ee;
230
231 hashtb_start(nlsr->adl, e);
232 res = hashtb_seek(e, nbr->name, nbr->length, 0);
233
234 if( res == HT_OLD_ENTRY )
235 {
236 nnbr=e->data;
237 free(nnbr->last_lsdb_version);
238 nnbr->last_lsdb_version=(char *)malloc(strlen(version)+1);
239 memset(nnbr->last_lsdb_version,0,strlen(version)+1);
240 memcpy(nnbr->last_lsdb_version,version,strlen(version)+1);
241 }
242 else if(res == HT_NEW_ENTRY)
243 {
244 hashtb_delete(e);
245 }
246
247 hashtb_end(e);
248
249}
250
251int
252get_timed_out_number(struct name_prefix *nbr)
253{
akmhoqued79438d2012-08-27 13:31:42 -0500254 printf("get_timed_out_number called \n");
255
256 int res,ret=-1;
257 struct ndn_neighbor *nnbr;
258
259 struct hashtb_enumerator ee;
260 struct hashtb_enumerator *e = &ee;
261
262 hashtb_start(nlsr->adl, e);
akmhoque53f64222012-09-05 13:57:51 -0500263 res = hashtb_seek(e, nbr->name, nbr->length, 0);
akmhoqued79438d2012-08-27 13:31:42 -0500264
265 if( res == HT_OLD_ENTRY )
266 {
267 nnbr=e->data;
268 ret=nnbr->info_interest_timed_out;
269 }
270 else if(res == HT_NEW_ENTRY)
271 {
272 hashtb_delete(e);
273 }
akmhoque53f64222012-09-05 13:57:51 -0500274
akmhoqued79438d2012-08-27 13:31:42 -0500275 hashtb_end(e);
276
277 return ret;
akmhoque53f64222012-09-05 13:57:51 -0500278
279 return 0;
akmhoqued79438d2012-08-27 13:31:42 -0500280}
akmhoque53f64222012-09-05 13:57:51 -0500281
282int
283is_adj_lsa_build(void)
284{
285 int ret=0;
286
287 int nbr_count=0;
288
289 int i, adl_element;
290 struct ndn_neighbor *nbr;
291
292 struct hashtb_enumerator ee;
293 struct hashtb_enumerator *e = &ee;
294
295 hashtb_start(nlsr->adl, e);
296 adl_element=hashtb_n(nlsr->adl);
297
298 for(i=0;i<adl_element;i++)
299 {
300 nbr=e->data;
301 if(nbr->status == 1 )
302 {
303 nbr_count++;
304 }
305 else if (nbr->info_interest_timed_out >= nlsr->interest_retry)
306 {
307 nbr_count++;
308 }
309 hashtb_next(e);
310 }
311
312 hashtb_end(e);
313 if(nbr_count == adl_element)
314 ret=1;
315
316 return ret;
317}
318
319int
320no_active_nbr(void)
321{
322 int i, adl_element;
323 int no_link=0;
324 struct ndn_neighbor *nbr;
325 struct hashtb_enumerator ee;
326 struct hashtb_enumerator *e = &ee;
327 hashtb_start(nlsr->adl, e);
328 adl_element=hashtb_n(nlsr->adl);
329
330 for(i=0;i<adl_element;i++)
331 {
332 nbr=e->data;
333 if( nbr->status == 1 )
334 no_link++;
335 hashtb_next(e);
336 }
337
338 hashtb_end(e);
339
340 return no_link;
341
342}
343
344long int
345len_active_nbr_data(void)
346{
347 int i, adl_element;
348 int no_link=0;
349 long int len=0;
350 struct ndn_neighbor *nbr;
351 struct hashtb_enumerator ee;
352 struct hashtb_enumerator *e = &ee;
353 hashtb_start(nlsr->adl, e);
354 adl_element=hashtb_n(nlsr->adl);
355
356 for(i=0;i<adl_element;i++)
357 {
358 nbr=e->data;
359 if( nbr->status == 1 )
360 {
361 char *temp_face=(char *)malloc(20);
362 char *temp_metric=(char *)malloc(20);
363 char *temp_length=(char *)malloc(20);
364
365 len+=strlen(nbr->neighbor->name);
366
367 memset( temp_face,0,20);
368 sprintf(temp_face,"%d",nbr->face);
369 len+=strlen(temp_face);
370
371 memset( temp_metric,0,20);
372 sprintf(temp_face,"%d",nbr->metric);
373 len+=strlen(temp_metric);
374
375 memset( temp_length,0,20);
376 sprintf(temp_length,"%d",nbr->neighbor->length);
377 len+=strlen(temp_length);
378
379 no_link++;
380
381 free(temp_face);
382 free(temp_metric);
383 free(temp_length);
384
385 }
386 hashtb_next(e);
387 }
388
389 hashtb_end(e);
390
391 len=len+ no_link*4+1;
392
393 return len;
394
395}
396
397void
398get_active_nbr_adj_data(struct ccn_charbuf *c)
399{
400
401 int i, adl_element;
402 struct ndn_neighbor *nbr;
403 struct hashtb_enumerator ee;
404 struct hashtb_enumerator *e = &ee;
405 hashtb_start(nlsr->adl, e);
406 adl_element=hashtb_n(nlsr->adl);
407
408 for(i=0;i<adl_element;i++)
409 {
410 nbr=e->data;
411 if( nbr->status == 1 )
412 {
413 ccn_charbuf_append_string(c,nbr->neighbor->name);
414 ccn_charbuf_append_string(c,"|");
415
416 char *temp_length=(char *)malloc(20);
417 memset(temp_length,0,20);
418 sprintf(temp_length,"%d",nbr->neighbor->length);
419 ccn_charbuf_append_string(c,temp_length);
420 free(temp_length);
421 ccn_charbuf_append_string(c,"|");
422
423 char *temp_face=(char *)malloc(20);
424 memset(temp_face,0,20);
425 sprintf(temp_face,"%d",nbr->face);
426 ccn_charbuf_append_string(c,temp_face);
427 free(temp_face);
428 ccn_charbuf_append_string(c,"|");
429
430 char *temp_metric=(char *)malloc(20);
431 memset(temp_metric,0,20);
432 sprintf(temp_metric,"%d",nbr->metric);
433 ccn_charbuf_append_string(c,temp_metric);
434 free(temp_metric);
435 ccn_charbuf_append_string(c,"|");
436
437 }
438 hashtb_next(e);
439 }
440
441 hashtb_end(e);
442}
443
444char *
445get_nbr_lsdb_version(char *nbr)
446{
447 printf("get_timed_out_number called \n");
448
449 char *version=NULL;
450
451 int res;
452 struct ndn_neighbor *nnbr;
453
454 struct hashtb_enumerator ee;
455 struct hashtb_enumerator *e = &ee;
456
457 hashtb_start(nlsr->adl, e);
458 res = hashtb_seek(e, nbr, strlen(nbr)+1, 0);
459
460 if (res == HT_OLD_ENTRY)
461 {
462 nnbr=e->data;
463 version=(char *)malloc(strlen(nnbr->last_lsdb_version)+1);
464 memset(version,0,strlen(nnbr->last_lsdb_version)+1);
465 memcpy(version,nnbr->last_lsdb_version,strlen(nnbr->last_lsdb_version)+1);
466 }
467 else if(res == HT_NEW_ENTRY)
468 {
469 hashtb_delete(e);
470 }
471
472 hashtb_end(e);
473
474 return version;
475}
476
477void
478update_adjacent_last_lsdb_requested_to_adl(char *nbr, long int timestamp)
479{
480 printf("update_adjacent_last_lsdb_requested_to_adl called \n");
481
482 int res;
483 struct ndn_neighbor *nnbr;
484
485 struct hashtb_enumerator ee;
486 struct hashtb_enumerator *e = &ee;
487
488 hashtb_start(nlsr->adl, e);
489 res = hashtb_seek(e, nbr, strlen(nbr)+1, 0);
490
491 if( res == HT_OLD_ENTRY )
492 {
493 nnbr=e->data;
494 nnbr->last_lsdb_requested=timestamp;
495
496 }
497 else if(res == HT_NEW_ENTRY)
498 {
499 hashtb_delete(e);
500 }
501
502 hashtb_end(e);
503
504}
505
506void
507adjust_adjacent_last_lsdb_requested_to_adl(char *nbr, long int sec)
508{
509 printf("update_adjacent_last_lsdb_requested_to_adl called \n");
510
511 int res;
512 struct ndn_neighbor *nnbr;
513
514 struct hashtb_enumerator ee;
515 struct hashtb_enumerator *e = &ee;
516
517 hashtb_start(nlsr->adl, e);
518 res = hashtb_seek(e, nbr, strlen(nbr)+1, 0);
519
520 if( res == HT_OLD_ENTRY )
521 {
522 nnbr=e->data;
523 nnbr->last_lsdb_requested=nnbr->last_lsdb_requested-sec;
524
525 }
526 else if(res == HT_NEW_ENTRY)
527 {
528 hashtb_delete(e);
529 }
530
531 hashtb_end(e);
532
533}
534
535
536long int
537get_nbr_last_lsdb_requested(char *nbr)
538{
539 printf("get_timed_out_number called \n");
540
541 long int last_lsdb_requested=0;
542
543 int res;
544 struct ndn_neighbor *nnbr;
545
546 struct hashtb_enumerator ee;
547 struct hashtb_enumerator *e = &ee;
548
549 hashtb_start(nlsr->adl, e);
550 res = hashtb_seek(e, nbr, strlen(nbr)+1, 0);
551
552 if (res == HT_OLD_ENTRY)
553 {
554 nnbr=e->data;
555 last_lsdb_requested=nnbr->last_lsdb_requested;
556 }
557 else if(res == HT_NEW_ENTRY)
558 {
559 hashtb_delete(e);
560 }
561
562 hashtb_end(e);
563
564 return last_lsdb_requested;
565}
566
567long int
568get_nbr_time_diff_lsdb_req(char *nbr)
569{
570 printf("get_nbr_time_diff_lsdb_req called \n");
571
572 long int time_diff=get_lsdb_synch_interval(nbr)+1;
573 int res;
574 struct ndn_neighbor *nnbr;
575
576 struct hashtb_enumerator ee;
577 struct hashtb_enumerator *e = &ee;
578
579 hashtb_start(nlsr->adl, e);
580 res = hashtb_seek(e, nbr, strlen(nbr)+1, 0);
581
582 if (res == HT_OLD_ENTRY)
583 {
584 nnbr=e->data;
585
586 if (nnbr->last_lsdb_requested == 0)
587 time_diff=get_lsdb_synch_interval(nbr)+1;
588 else time_diff=get_current_time_sec() - get_nbr_last_lsdb_requested(nbr);
589
590 }
591 else if(res == HT_NEW_ENTRY)
592 {
593 hashtb_delete(e);
594 }
595
596 hashtb_end(e);
597
598 return time_diff;
599}
600
601long int
602get_lsdb_synch_interval(char *nbr)
603{
604 printf("get_lsdb_synch_interval called \n");
605
606 long int lsdb_synch_interval=300;
607
608
609 int res;
610 struct ndn_neighbor *nnbr;
611
612 struct hashtb_enumerator ee;
613 struct hashtb_enumerator *e = &ee;
614
615 hashtb_start(nlsr->adl, e);
616 res = hashtb_seek(e, nbr, strlen(nbr)+1, 0);
617
618 if (res == HT_OLD_ENTRY)
619 {
620 nnbr=e->data;
621 lsdb_synch_interval=nnbr->lsdb_synch_interval;
622 }
623 else if(res == HT_NEW_ENTRY)
624 {
625 hashtb_delete(e);
626 }
627
628 hashtb_end(e);
629
630 return lsdb_synch_interval;
631
632}
633
634void
635set_is_lsdb_send_interest_scheduled_to_zero(char *nbr)
636{
637 printf("set_is_lsdb_send_interest_scheduled_to_zero called \n");
638
639 int res;
640 struct ndn_neighbor *nnbr;
641
642 struct hashtb_enumerator ee;
643 struct hashtb_enumerator *e = &ee;
644
645 hashtb_start(nlsr->adl, e);
646 res = hashtb_seek(e, nbr, strlen(nbr)+1, 0);
647
648 if (res == HT_OLD_ENTRY)
649 {
650 nnbr=e->data;
651 nnbr->is_lsdb_send_interest_scheduled=0;
652 }
653 else if(res == HT_NEW_ENTRY)
654 {
655 hashtb_delete(e);
656 }
657
658 hashtb_end(e);
659
660}
661
662
663int
664get_adjacent_status(struct name_prefix *nbr)
665{
666 printf("get_adjacent_status called \n");
667
668 int res;
669 int status=-1;
670 struct ndn_neighbor *nnbr;
671
672 struct hashtb_enumerator ee;
673 struct hashtb_enumerator *e = &ee;
674
675 hashtb_start(nlsr->adl, e);
676 res = hashtb_seek(e, nbr->name, nbr->length, 0);
677
678 if (res == HT_OLD_ENTRY)
679 {
680 nnbr=e->data;
681 status=nnbr->status;
682 }
683 else if(res == HT_NEW_ENTRY)
684 {
685 hashtb_delete(e);
686 }
687
688 hashtb_end(e);
689
690 return status;
691
692}
693