blob: e3c848355b03f2e233b5dbc9193cd253852d1233 [file] [log] [blame]
akmhoque8fdd6412012-12-04 15:05:33 -06001#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#include <sys/types.h>
12#include <signal.h>
13
14
15
16#include <ccn/ccn.h>
17#include <ccn/uri.h>
18#include <ccn/keystore.h>
19#include <ccn/signing.h>
20#include <ccn/schedule.h>
21#include <ccn/hashtb.h>
22
23#include "nlsr.h"
24#include "nlsr_npt.h"
25#include "nlsr_fib.h"
26#include "nlsr_route.h"
27#include "nlsr_adl.h"
28#include "utility.h"
29
30int
31add_npt_entry(char *orig_router, char *name_prefix, int num_face, int *faces, int *route_costs)
32{
33 if ( strcmp(orig_router,nlsr->router_name)== 0)
34 {
35 return -1;
36 }
37
38 struct npt_entry *ne=(struct npt_entry*)malloc(sizeof(struct npt_entry ));
39
40 int res,res_nle,res_fle;
41 struct hashtb_enumerator ee;
42 struct hashtb_enumerator *e = &ee;
43
44
45 hashtb_start(nlsr->npt, e);
46 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
47
48 if(res == HT_NEW_ENTRY)
49 {
50 ne=e->data;
51
52 ne->orig_router=(char *)malloc(strlen(orig_router)+1);
53 memset(ne->orig_router,0,strlen(orig_router)+1);
54 memcpy(ne->orig_router,orig_router,strlen(orig_router));
55
56
57
58
59 struct hashtb_param param_nle = {0};
60 ne->name_list= hashtb_create(sizeof(struct name_list_entry ), &param_nle);
61
62 struct hashtb_enumerator eenle;
63 struct hashtb_enumerator *enle = &eenle;
64
65 hashtb_start(ne->name_list, enle);
66 res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
67
68 if(res_nle == HT_NEW_ENTRY )
69 {
70 struct name_list_entry *nle=(struct name_list_entry *)malloc(sizeof(struct name_list_entry));
71 nle=enle->data;
72 nle->name=(char *)malloc(strlen(name_prefix)+1);
73 memset(nle->name,0,strlen(name_prefix)+1);
74 memcpy(nle->name,name_prefix,strlen(name_prefix));
75
76
77
78 }
79 hashtb_end(enle);
80
81 struct hashtb_param param_fle = {0};
82 ne->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
83
84 if ( num_face > 0 )
85 {
86 struct hashtb_enumerator eef;
87 struct hashtb_enumerator *ef = &eef;
88
89 hashtb_start(ne->face_list, ef);
90 int i;
91
92 for ( i=0; i < num_face ; i++)
93 {
94 int face=faces[i];
95 if ( face != NO_FACE && face != ZERO_FACE)
96 {
97 res_fle = hashtb_seek(ef, &face, sizeof(face), 0);
98
99 if ( res_fle == HT_NEW_ENTRY )
100 {
101 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
102 fle=ef->data;
103 fle->next_hop_face=face;
104 fle->route_cost=route_costs[i];
105 }
106 }
107
108 }
109 hashtb_end(ef);
110 }
111
112 }
113 else if (res == HT_OLD_ENTRY)
114 {
115 free(ne);
116 struct npt_entry *one;
117
118 one=e->data;
119
120 struct hashtb_enumerator eenle;
121 struct hashtb_enumerator *enle = &eenle;
122
123 hashtb_start(one->name_list, enle);
124 res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
125
126 if(res_nle == HT_NEW_ENTRY )
127 {
128 struct name_list_entry *nle=(struct name_list_entry *)malloc(sizeof(struct name_list_entry));
129 nle=enle->data;
130 nle->name=(char *)malloc(strlen(name_prefix)+1);
131 memset(nle->name,0,strlen(name_prefix)+1);
132 memcpy(nle->name,name_prefix,strlen(name_prefix));
133 }
134 else if(res_nle == HT_OLD_ENTRY )
135 {
136
137 }
138 hashtb_end(enle);
139
140 if ( num_face > 0 )
141 {
142 struct hashtb_enumerator eef;
143 struct hashtb_enumerator *ef = &eef;
144
145 hashtb_start(one->face_list, ef);
146 int i;
147
148 for ( i=0; i< num_face ; i ++)
149 {
150 int face=faces[i];
151 if ( face != NO_FACE && face != ZERO_FACE)
152 {
153 res_fle = hashtb_seek(ef, &face, sizeof(face), 0);
154
155 if ( res_fle == HT_NEW_ENTRY )
156 {
157 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
158 fle=ef->data;
159 fle->next_hop_face=face;
160 fle->route_cost=route_costs[i];
161 }
162 }
163 }
164 hashtb_end(ef);
165 }
166
167
168 }
169 hashtb_end(e);
170
171 update_ccnd_fib_for_orig_router(orig_router);
172
173 return res;
174}
175
176void
177update_ccnd_fib_for_orig_router(char *orig_router)
178{
179
180 int res;
181 struct hashtb_enumerator ee;
182 struct hashtb_enumerator *e = &ee;
183
184
185 hashtb_start(nlsr->npt, e);
186 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
187
188 if(res == HT_NEW_ENTRY)
189 {
190 hashtb_delete(e);
191 }
192 else if ( res == HT_OLD_ENTRY )
193 {
194 struct npt_entry *ne;
195 ne=e->data;
196 int num_face=hashtb_n(ne->face_list);
197 int last_face,first_face;
198
199 int *faces=(int *)malloc(num_face*sizeof(int));
200 int *route_costs=(int *)malloc(num_face*sizeof(int));
201
202 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
203 sort_faces_by_distance(faces,route_costs,0,num_face);
204
205
206 first_face=num_face-1;
207
208 if ( nlsr->multi_path_face_num == 0 )
209 {
210 last_face=first_face;
211 }
212 else
213 {
214 if ( num_face <= nlsr->multi_path_face_num)
215 {
216 last_face=0;
217 }
218 else if ( nlsr->multi_path_face_num == 0)
219 {
220 last_face=num_face-nlsr->multi_path_face_num;
221 }
222 }
223
224 int i,j, nl_element;
225 struct name_list_entry *nle;
226 struct hashtb_enumerator eenle;
227 struct hashtb_enumerator *enle = &eenle;
228
229 hashtb_start(ne->name_list, enle);
230 nl_element=hashtb_n(ne->name_list);
231
232 for (i=0;i<nl_element;i++)
233 {
234 nle=enle->data;
235
236 for( j=first_face; j>= last_face; j--)
237 {
238 //printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
239 //printf("Orig Router: %s \n",orig_router);
240 //printf("Name Prefix: %s \n",nle->name);
241 //printf("Is neighbor Orig Router: %d \n",is_neighbor(orig_router));
242 //printf("Is neighbor Name Prefix: %d \n",is_neighbor(nle->name));
243
244 if ( is_neighbor(orig_router) == 0 )
245 {
246 if ( nlsr->debugging )
247 printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
248 if ( nlsr->detailed_logging )
249 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
250 //printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
251 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);
252 }
253 else
254 {
255 if ( j == last_face && is_neighbor(nle->name)==0)
256 {
257 if ( nlsr->debugging )
258 printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
259 if ( nlsr->detailed_logging )
260 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
261 //printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
262 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);
263 }
264 }
265 }
266
267
268 hashtb_next(enle);
269 }
270 hashtb_end(enle);
271
272
273
274 free(faces);
275 free(route_costs);
276
277 }
278 hashtb_end(e);
279
280}
281
282int
283delete_npt_entry_by_router_and_name_prefix(char *orig_router, char *name_prefix)
284{
285 if ( strcmp(orig_router,nlsr->router_name)== 0)
286 {
287 return -1;
288 }
289
290 struct npt_entry *ne;
291
292 int res,res_nle;
293 struct hashtb_enumerator ee;
294 struct hashtb_enumerator *e = &ee;
295
296
297 hashtb_start(nlsr->npt, e);
298 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
299
300 if(res == HT_NEW_ENTRY)
301 {
302 hashtb_delete(e);
303 return -1;
304 }
305 else if (res == HT_OLD_ENTRY)
306 {
307 ne=e->data;
308
309 struct hashtb_enumerator eenle;
310 struct hashtb_enumerator *enle = &eenle;
311
312 hashtb_start(ne->name_list, enle);
313 res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
314
315 if(res_nle == HT_NEW_ENTRY )
316 {
317 hashtb_delete(enle);
318 }
319 else if(res_nle == HT_OLD_ENTRY )
320 {
321 struct name_list_entry *nle;
322
323 nle=enle->data;
324
325 int j;
326 int num_face=hashtb_n(ne->face_list);
327 int last_face,first_face;
328
329 int *faces=(int *)malloc(num_face*sizeof(int));
330 int *route_costs=(int *)malloc(num_face*sizeof(int));
331
332 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
333 sort_faces_by_distance(faces,route_costs,0,num_face);
334
335
336 first_face=num_face-1;
337
338 if ( nlsr->multi_path_face_num == 0 )
339 {
340 last_face=first_face;
341 }
342 else
343 {
344 if ( num_face <= nlsr->multi_path_face_num)
345 {
346 last_face=0;
347 }
348 else if ( nlsr->multi_path_face_num == 0)
349 {
350 last_face=num_face-nlsr->multi_path_face_num;
351 }
352 }
353
354 for( j=first_face; j>= last_face; j--)
355 {
356
357 if ( is_neighbor(orig_router) == 0 )
358 {
359 if ( nlsr->debugging )
360 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
361 if ( nlsr->detailed_logging )
362 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
363 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
364 }
365 else
366 {
367 if ( j == last_face && is_neighbor(nle->name)==0)
368 {
369 if ( nlsr->debugging )
370 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
371 if ( nlsr->detailed_logging )
372 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
373 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
374 }
375 }
376
377 }
378
379
380
381
382 //hashtb_delete(enle); has to delete later
383 }
384
385 hashtb_end(enle);
386 }
387
388 hashtb_end(e);
389
390 return 0;
391}
392
393void
394print_npt(void)
395{
396
397 if ( nlsr->debugging )
398 {
399 printf("\n");
400 printf("print_npt called\n");
401 }
402 if ( nlsr->detailed_logging )
403 {
404 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
405 writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_npt called\n");
406 }
407 int i, npt_element;
408
409 struct npt_entry *ne;
410
411 struct hashtb_enumerator ee;
412 struct hashtb_enumerator *e = &ee;
413
414 hashtb_start(nlsr->npt, e);
415 npt_element=hashtb_n(nlsr->npt);
416
417 for(i=0;i<npt_element;i++)
418 {
419 if ( nlsr->debugging )
420 {
421 printf("\n");
422 printf("----------NPT ENTRY %d------------------\n",i+1);
423 }
424 if ( nlsr->detailed_logging )
425 {
426 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
427 writeLogg(__FILE__,__FUNCTION__,__LINE__,"----------NPT ENTRY %d------------------\n",i+1);
428 }
429 ne=e->data;
430 if ( nlsr->debugging )
431 printf(" Origination Router: %s \n",ne->orig_router);
432 if ( nlsr->detailed_logging )
433 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s \n",ne->orig_router);
434 //ne->next_hop_face == NO_FACE ? printf(" Next Hop Face: NO_NEXT_HOP \n") : printf(" Next Hop Face: %d \n", ne->next_hop_face);
435
436 int j, nl_element,face_list_element;
437 struct name_list_entry *nle;
438 struct hashtb_enumerator eenle;
439 struct hashtb_enumerator *enle = &eenle;
440
441 hashtb_start(ne->name_list, enle);
442 nl_element=hashtb_n(ne->name_list);
443
444 for (j=0;j<nl_element;j++)
445 {
446 nle=enle->data;
447 if ( nlsr->debugging )
448 printf(" Name Prefix: %s \n",nle->name);
449 if ( nlsr->detailed_logging )
450 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix: %s \n",nle->name);
451 hashtb_next(enle);
452 }
453 hashtb_end(enle);
454
455 struct face_list_entry *fle;
456
457 struct hashtb_enumerator eef;
458 struct hashtb_enumerator *ef = &eef;
459
460 hashtb_start(ne->face_list, ef);
461 face_list_element=hashtb_n(ne->face_list);
462 if ( face_list_element <= 0 )
463 {
464 if ( nlsr->debugging )
465 printf(" Face: No Face \n");
466 if ( nlsr->detailed_logging )
467 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
468
469 }
470 else
471 {
472 for(j=0;j<face_list_element;j++)
473 {
474 fle=ef->data;
475 if ( nlsr->debugging )
476 printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
477 if ( nlsr->detailed_logging )
478 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
479 hashtb_next(ef);
480 }
481 }
482 hashtb_end(ef);
483
484
485 hashtb_next(e);
486 }
487
488 hashtb_end(e);
489
490 if ( nlsr->debugging )
491 printf("\n");
492 if ( nlsr->detailed_logging )
493 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
494}
495
496void
497delete_orig_router_from_npt(char *orig_router)
498{
499 int res,num_face,num_prefix;
500 struct npt_entry *ne;
501
502 struct hashtb_enumerator ee;
503 struct hashtb_enumerator *e = &ee;
504
505 hashtb_start(nlsr->npt, e);
506 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
507
508 if ( res == HT_OLD_ENTRY )
509 {
510 ne=e->data;
511 num_prefix=hashtb_n(ne->name_list);
512 if ( num_prefix > 0 )
513 {
514 num_face=hashtb_n(ne->face_list);
515
516 if ( num_face > 0 )
517 {
518 int j, nl_element;
519 struct name_list_entry *nle;
520 struct hashtb_enumerator eenle;
521 struct hashtb_enumerator *enle = &eenle;
522
523 hashtb_start(ne->name_list, enle);
524 nl_element=hashtb_n(ne->name_list);
525
526 for (j=0;j<nl_element;j++)
527 {
528 nle=enle->data;
529 delete_npt_entry_by_router_and_name_prefix(ne->orig_router,nle->name);
530 hashtb_next(enle);
531 }
532 hashtb_end(enle);
533
534 }
535
536 }
537 hashtb_destroy(&ne->name_list);
538 hashtb_destroy(&ne->face_list);
539 hashtb_delete(e);
540 }
541 else if ( res == HT_NEW_ENTRY )
542 {
543 hashtb_delete(e);
544 }
545 hashtb_end(e);
546}
547
548
549void
550add_face_to_npt_by_face_id(char *dest_router, int face_id, int route_cost)
551{
552 if ( nlsr->debugging )
553 {
554 printf("add_face_to_npt_by_face_id called\n");
555 printf("Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
556 }
557 if ( nlsr->detailed_logging )
558 {
559 writeLogg(__FILE__,__FUNCTION__,__LINE__,"add_face_to_npt_by_face_id called\n");
560 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
561 }
562
563
564 int res,res1;
565 struct npt_entry *ne;
566
567 struct hashtb_enumerator ee;
568 struct hashtb_enumerator *e = &ee;
569
570 hashtb_start(nlsr->npt, e);
571 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
572
573 if ( res == HT_OLD_ENTRY )
574 {
575 if ( nlsr->debugging )
576 printf("Dest Router Found \n");
577 if ( nlsr->detailed_logging )
578 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest Router Found \n");
579
580 ne=e->data;
581
582 struct hashtb_enumerator eef;
583 struct hashtb_enumerator *ef = &eef;
584
585 hashtb_start(ne->face_list, ef);
586 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
587
588 if ( res1 == HT_OLD_ENTRY )
589 {
590 if ( nlsr->debugging )
591 printf("Face Found \n");
592 if ( nlsr->detailed_logging )
593 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face Found \n");
594 struct face_list_entry *fle;//=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
595 fle=ef->data;
596 fle->next_hop_face=face_id;
597 fle->route_cost=route_cost;
598 }
599 else if ( res1 == HT_NEW_ENTRY )
600 {
601 if ( nlsr->debugging )
602 printf("Face Not Found \n");
603 if ( nlsr->detailed_logging )
604 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face Not Found \n");
605 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
606 fle=ef->data;
607 fle->next_hop_face=face_id;
608 fle->route_cost=route_cost;
609 //hashtb_delete(ef);
610 }
611 hashtb_end(ef);
612 }
613 else if (res == HT_NEW_ENTRY)
614 {
615 hashtb_delete(e);
616 }
617
618 hashtb_end(e);
619}
620
621
622void
623add_new_fib_entries_to_npt(void)
624{
625 if ( nlsr->debugging )
626 printf("add_new_fib_entries_to_npt called\n");
627 if ( nlsr->detailed_logging )
628 writeLogg(__FILE__,__FUNCTION__,__LINE__,"add_new_fib_entries_to_npt called\n");
629 int i,j, rt_element,face_list_element;
630
631 struct routing_table_entry *rte;
632
633 struct hashtb_enumerator ee;
634 struct hashtb_enumerator *e = &ee;
635
636 hashtb_start(nlsr->routing_table, e);
637 rt_element=hashtb_n(nlsr->routing_table);
638
639 for(i=0;i<rt_element;i++)
640 {
641 rte=e->data;
642
643 struct face_list_entry *fle;
644
645 struct hashtb_enumerator eef;
646 struct hashtb_enumerator *ef = &eef;
647
648 hashtb_start(rte->face_list, ef);
649 face_list_element=hashtb_n(rte->face_list);
650 if ( face_list_element <= 0 )
651 {
652 if ( nlsr->debugging )
653 printf(" Face: No Face \n");
654 if ( nlsr->detailed_logging )
655 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
656 }
657 else
658 {
659 for(j=0;j<face_list_element;j++)
660 {
661 fle=ef->data;
662 add_face_to_npt_by_face_id(rte->dest_router,fle->next_hop_face,fle->route_cost);
663 hashtb_next(ef);
664 }
665 }
666 hashtb_end(ef);
667
668 hashtb_next(e);
669 }
670
671 hashtb_end(e);
672
673}
674
675
676void
677delete_face_from_npt_by_face_id(char *dest_router, int face_id)
678{
679 if ( nlsr->debugging )
680 printf("delete_face_from_npt_by_face_id\n");
681 if ( nlsr->detailed_logging )
682 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_face_from_npt_by_face_id\n");
683
684 int res,res1;
685 struct npt_entry *ne;
686
687 struct hashtb_enumerator ee;
688 struct hashtb_enumerator *e = &ee;
689
690 hashtb_start(nlsr->npt, e);
691 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
692
693 if ( res == HT_OLD_ENTRY )
694 {
695 ne=e->data;
696
697 struct hashtb_enumerator eef;
698 struct hashtb_enumerator *ef = &eef;
699
700 hashtb_start(ne->face_list, ef);
701 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
702
703 if ( res1 == HT_OLD_ENTRY )
704 {
705 hashtb_delete(ef);
706 }
707 else if ( res1 == HT_NEW_ENTRY )
708 {
709 hashtb_delete(ef);
710 }
711 hashtb_end(ef);
712 }
713 else if (res == HT_NEW_ENTRY)
714 {
715 hashtb_delete(e);
716 }
717
718 hashtb_end(e);
719}
720
721int
722delete_old_face_from_npt(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
723{
724 if(flags == CCN_SCHEDULE_CANCEL)
725 {
726 return -1;
727 }
728
729 nlsr_lock();
730
731 if ( nlsr->debugging )
732 printf("delete_old_face_from_npt\n");
733 if ( nlsr->detailed_logging )
734 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_old_face_from_npt\n");
735
736 if ( ev->evdata != NULL )
737 {
738 if ( nlsr->debugging )
739 printf("Event Data: %s \n",(char *)ev->evdata);
740 if ( nlsr->detailed_logging )
741 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Event Data: %s \n",(char *)ev->evdata);
742 char *sep="|";
743 char *rem;
744 char *orig_router;
745 char *faceid;
746 int face_id;
747
748 char *face_data=(char *)malloc(strlen((char *)ev->evdata)+1);
749 memset(face_data,0,strlen((char *)ev->evdata)+1);
750 memcpy(face_data+strlen(face_data),(char *)ev->evdata,strlen((char *)ev->evdata));
751
752 orig_router=strtok_r(face_data,sep,&rem);
753 faceid=strtok_r(NULL,sep,&rem);
754 face_id=atoi(faceid);
755
756 if ( nlsr->debugging )
757 printf("Orig Router: %s Face: %d \n",orig_router,face_id);
758 if ( nlsr->detailed_logging )
759 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig Router: %s Face: %d \n",orig_router,face_id);
760
761 delete_face_from_npt_by_face_id(orig_router,face_id);
762 }
763
764 nlsr_unlock();
765
766 return 0;
767}
768
769void
770clean_old_fib_entries_from_npt(void)
771{
772
773
774 if ( nlsr->debugging )
775 printf("clean_old_fib_entries_from_npt called\n\n");
776 if ( nlsr->detailed_logging )
777 writeLogg(__FILE__,__FUNCTION__,__LINE__,"clean_old_fib_entries_from_npt called\n\n");
778 int i, npt_element;
779
780 struct npt_entry *ne;
781
782 struct hashtb_enumerator ee;
783 struct hashtb_enumerator *e = &ee;
784
785 hashtb_start(nlsr->npt, e);
786 npt_element=hashtb_n(nlsr->npt);
787
788 for(i=0;i<npt_element;i++)
789 {
790 ne=e->data;
791
792 int j,k, nl_element,face_list_element;
793 struct face_list_entry *fle;
794
795 struct hashtb_enumerator eef;
796 struct hashtb_enumerator *ef = &eef;
797
798 hashtb_start(ne->face_list, ef);
799 face_list_element=hashtb_n(ne->face_list);
800 if ( face_list_element <= 0 )
801 {
802 if ( nlsr->debugging )
803 printf(" Face: No Face \n");
804 if ( nlsr->detailed_logging )
805 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
806
807 }
808 else
809 {
810 for(j=0;j<face_list_element;j++)
811 {
812 fle=ef->data;
813 int check=does_face_exist_for_router(ne->orig_router,fle->next_hop_face);
814 if ( check == 0 )
815 {
816 struct name_list_entry *nle;
817 struct hashtb_enumerator eenle;
818 struct hashtb_enumerator *enle = &eenle;
819
820 hashtb_start(ne->name_list, enle);
821 nl_element=hashtb_n(ne->name_list);
822
823 for (k=0;k<nl_element;k++)
824 {
825 nle=enle->data;
826
827 //delete all the fib entries here
828
829 //printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
830
831 if( is_neighbor(nle->name) == 0 )
832 {
833 if ( nlsr->debugging )
834 printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
835 if ( nlsr->detailed_logging )
836 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
837 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, fle->next_hop_face);
838 }
839
840
841 hashtb_next(enle);
842 }
843 hashtb_end(enle);
844
845 char faceid[20];
846 memset(faceid,0,20);
847 sprintf(faceid,"%d",fle->next_hop_face);
848 char *evdata=(char *)malloc(strlen(ne->orig_router)+strlen(faceid)+2);
849 memset(evdata,0,strlen(ne->orig_router)+strlen(faceid)+2);
850 memcpy(evdata+strlen(evdata),ne->orig_router,strlen(ne->orig_router));
851 memcpy(evdata+strlen(evdata),"|",1);
852 memcpy(evdata+strlen(evdata),faceid,strlen(faceid));
853
854 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_old_face_from_npt, (void *)evdata, 0);
855
856 }
857
858 hashtb_next(ef);
859 }
860 }
861 hashtb_end(ef);
862
863
864 hashtb_next(e);
865 }
866
867 hashtb_end(e);
868
869}
870
871void
872update_npt_with_new_route(void)
873{
874 clean_old_fib_entries_from_npt();
875 add_new_fib_entries_to_npt();
876
877 int i, npt_element;
878
879 struct npt_entry *ne;
880
881 struct hashtb_enumerator ee;
882 struct hashtb_enumerator *e = &ee;
883
884 hashtb_start(nlsr->npt, e);
885 npt_element=hashtb_n(nlsr->npt);
886
887 for(i=0;i<npt_element;i++)
888 {
889
890 ne=e->data;
891 update_ccnd_fib_for_orig_router(ne->orig_router);
892 hashtb_next(e);
893 }
894
895 hashtb_end(e);
896}
897
898
899
900void
901sort_faces_by_distance(int *faces,int *route_costs,int start,int element)
902{
903 int i,j;
904 int temp_cost;
905 int temp_face;
906
907 for ( i=start ; i < element ; i ++)
908 {
909 for( j=i+1; j<element; j ++)
910 {
911 if (route_costs[j] < route_costs[i] )
912 {
913 temp_cost=route_costs[j];
914 route_costs[j]=route_costs[i];
915 route_costs[i]=temp_cost;
916
917 temp_face=faces[j];
918 faces[j]=faces[i];
919 faces[i]=temp_face;
920 }
921 }
922 }
923
924}
925
926void
927get_all_faces_for_orig_router_from_npt(char *orig_router, int *faces, int *route_costs, int num_faces)
928{
929
930 int res,face_list_element,j;
931 struct hashtb_enumerator ee;
932 struct hashtb_enumerator *e = &ee;
933
934
935 hashtb_start(nlsr->npt, e);
936 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
937
938 if(res == HT_NEW_ENTRY)
939 {
940 hashtb_delete(e);
941 }
942 else if ( res == HT_OLD_ENTRY )
943 {
944 struct npt_entry *ne;
945 ne=e->data;
946
947 struct face_list_entry *fle;
948
949 struct hashtb_enumerator eef;
950 struct hashtb_enumerator *ef = &eef;
951
952 hashtb_start(ne->face_list, ef);
953 face_list_element=hashtb_n(ne->face_list);
954 for(j=0;j<face_list_element;j++)
955 {
956 fle=ef->data;
957 faces[j]=fle->next_hop_face;
958 route_costs[j]=fle->route_cost;
959 hashtb_next(ef);
960 }
961 hashtb_end(ef);
962
963
964 }
965 hashtb_end(e);
966
967}
968
969void
970destroy_faces_by_orig_router(char *orig_router)
971{
972
973 int res;
974 struct hashtb_enumerator ee;
975 struct hashtb_enumerator *e = &ee;
976
977
978 hashtb_start(nlsr->npt, e);
979 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
980
981 if(res == HT_NEW_ENTRY)
982 {
983 hashtb_delete(e);
984 }
985 else if ( res == HT_OLD_ENTRY )
986 {
987 struct npt_entry *ne;
988 ne=e->data;
989 int num_face=hashtb_n(ne->face_list);
990 int last_face,first_face;
991
992 int *faces=(int *)malloc(num_face*sizeof(int));
993 int *route_costs=(int *)malloc(num_face*sizeof(int));
994
995 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
996 sort_faces_by_distance(faces,route_costs,0,num_face);
997
998
999 first_face=num_face-1;
1000
1001 if ( nlsr->multi_path_face_num == 0 )
1002 {
1003 last_face=first_face;
1004 }
1005 else
1006 {
1007 if ( num_face <= nlsr->multi_path_face_num)
1008 {
1009 last_face=0;
1010 }
1011 else if ( nlsr->multi_path_face_num == 0)
1012 {
1013 last_face=num_face-nlsr->multi_path_face_num;
1014 }
1015 }
1016
1017 int i,j, nl_element;
1018 struct name_list_entry *nle;
1019 struct hashtb_enumerator eenle;
1020 struct hashtb_enumerator *enle = &eenle;
1021
1022 hashtb_start(ne->name_list, enle);
1023 nl_element=hashtb_n(ne->name_list);
1024
1025 for (i=0;i<nl_element;i++)
1026 {
1027 nle=enle->data;
1028
1029 for( j=first_face; j>= last_face; j--)
1030 {
1031 if ( is_neighbor(orig_router) == 0 )
1032 {
1033 if ( nlsr->debugging )
1034 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1035 if ( nlsr->detailed_logging )
1036 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1037 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
1038 }
1039 else
1040 {
1041 if ( j == last_face && is_neighbor(nle->name)==0)
1042 {
1043 if ( nlsr->debugging )
1044 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1045 if ( nlsr->detailed_logging )
1046 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1047 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
1048 }
1049 }
1050 }
1051
1052 hashtb_next(enle);
1053 }
1054 hashtb_end(enle);
1055
1056
1057
1058 free(faces);
1059 free(route_costs);
1060
1061 }
1062 hashtb_end(e);
1063
1064}
1065
1066void
1067destroy_all_face_by_nlsr(void)
1068{
1069 int i, npt_element;
1070
1071 struct npt_entry *ne;
1072
1073 struct hashtb_enumerator ee;
1074 struct hashtb_enumerator *e = &ee;
1075
1076 hashtb_start(nlsr->npt, e);
1077 npt_element=hashtb_n(nlsr->npt);
1078
1079 for(i=0;i<npt_element;i++)
1080 {
1081 ne=e->data;
1082 destroy_faces_by_orig_router(ne->orig_router);
1083 hashtb_next(e);
1084 }
1085
1086 hashtb_end(e);
1087
1088 if ( nlsr->debugging )
1089 printf("\n");
1090 if ( nlsr->detailed_logging )
1091 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
1092}