blob: d70c5da8890924549f5b7aef481917fcb585d375 [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 )
akmhoquee19e3ee2013-01-22 14:38:45 -0600653 printf(" Face: No Face \n");
akmhoque8fdd6412012-12-04 15:05:33 -0600654 if ( nlsr->detailed_logging )
akmhoquee19e3ee2013-01-22 14:38:45 -0600655 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
akmhoque8fdd6412012-12-04 15:05:33 -0600656 }
657 else
658 {
659 for(j=0;j<face_list_element;j++)
660 {
661 fle=ef->data;
akmhoquee19e3ee2013-01-22 14:38:45 -0600662 if (fle->next_hop_face > 0 )
663 add_face_to_npt_by_face_id(rte->dest_router,fle->next_hop_face,fle->route_cost);
akmhoque8fdd6412012-12-04 15:05:33 -0600664 hashtb_next(ef);
665 }
666 }
667 hashtb_end(ef);
668
669 hashtb_next(e);
670 }
671
672 hashtb_end(e);
673
674}
675
676
677void
678delete_face_from_npt_by_face_id(char *dest_router, int face_id)
679{
680 if ( nlsr->debugging )
681 printf("delete_face_from_npt_by_face_id\n");
682 if ( nlsr->detailed_logging )
683 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_face_from_npt_by_face_id\n");
684
685 int res,res1;
686 struct npt_entry *ne;
687
688 struct hashtb_enumerator ee;
689 struct hashtb_enumerator *e = &ee;
690
691 hashtb_start(nlsr->npt, e);
692 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
693
694 if ( res == HT_OLD_ENTRY )
695 {
696 ne=e->data;
697
698 struct hashtb_enumerator eef;
699 struct hashtb_enumerator *ef = &eef;
700
701 hashtb_start(ne->face_list, ef);
702 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
703
704 if ( res1 == HT_OLD_ENTRY )
705 {
706 hashtb_delete(ef);
707 }
708 else if ( res1 == HT_NEW_ENTRY )
709 {
710 hashtb_delete(ef);
711 }
712 hashtb_end(ef);
713 }
714 else if (res == HT_NEW_ENTRY)
715 {
716 hashtb_delete(e);
717 }
718
719 hashtb_end(e);
720}
721
722int
723delete_old_face_from_npt(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
724{
725 if(flags == CCN_SCHEDULE_CANCEL)
726 {
727 return -1;
728 }
729
730 nlsr_lock();
731
732 if ( nlsr->debugging )
733 printf("delete_old_face_from_npt\n");
734 if ( nlsr->detailed_logging )
735 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_old_face_from_npt\n");
736
737 if ( ev->evdata != NULL )
738 {
739 if ( nlsr->debugging )
740 printf("Event Data: %s \n",(char *)ev->evdata);
741 if ( nlsr->detailed_logging )
742 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Event Data: %s \n",(char *)ev->evdata);
743 char *sep="|";
744 char *rem;
745 char *orig_router;
746 char *faceid;
747 int face_id;
748
749 char *face_data=(char *)malloc(strlen((char *)ev->evdata)+1);
750 memset(face_data,0,strlen((char *)ev->evdata)+1);
751 memcpy(face_data+strlen(face_data),(char *)ev->evdata,strlen((char *)ev->evdata));
752
753 orig_router=strtok_r(face_data,sep,&rem);
754 faceid=strtok_r(NULL,sep,&rem);
755 face_id=atoi(faceid);
756
757 if ( nlsr->debugging )
758 printf("Orig Router: %s Face: %d \n",orig_router,face_id);
759 if ( nlsr->detailed_logging )
760 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig Router: %s Face: %d \n",orig_router,face_id);
761
762 delete_face_from_npt_by_face_id(orig_router,face_id);
763 }
764
765 nlsr_unlock();
766
767 return 0;
768}
769
770void
771clean_old_fib_entries_from_npt(void)
772{
773
774
775 if ( nlsr->debugging )
776 printf("clean_old_fib_entries_from_npt called\n\n");
777 if ( nlsr->detailed_logging )
778 writeLogg(__FILE__,__FUNCTION__,__LINE__,"clean_old_fib_entries_from_npt called\n\n");
779 int i, npt_element;
780
781 struct npt_entry *ne;
782
783 struct hashtb_enumerator ee;
784 struct hashtb_enumerator *e = &ee;
785
786 hashtb_start(nlsr->npt, e);
787 npt_element=hashtb_n(nlsr->npt);
788
789 for(i=0;i<npt_element;i++)
790 {
791 ne=e->data;
792
793 int j,k, nl_element,face_list_element;
794 struct face_list_entry *fle;
795
796 struct hashtb_enumerator eef;
797 struct hashtb_enumerator *ef = &eef;
798
799 hashtb_start(ne->face_list, ef);
800 face_list_element=hashtb_n(ne->face_list);
801 if ( face_list_element <= 0 )
802 {
803 if ( nlsr->debugging )
804 printf(" Face: No Face \n");
805 if ( nlsr->detailed_logging )
806 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
807
808 }
809 else
810 {
811 for(j=0;j<face_list_element;j++)
812 {
813 fle=ef->data;
814 int check=does_face_exist_for_router(ne->orig_router,fle->next_hop_face);
815 if ( check == 0 )
816 {
817 struct name_list_entry *nle;
818 struct hashtb_enumerator eenle;
819 struct hashtb_enumerator *enle = &eenle;
820
821 hashtb_start(ne->name_list, enle);
822 nl_element=hashtb_n(ne->name_list);
823
824 for (k=0;k<nl_element;k++)
825 {
826 nle=enle->data;
akmhoque8fdd6412012-12-04 15:05:33 -0600827 if( is_neighbor(nle->name) == 0 )
828 {
829 if ( nlsr->debugging )
830 printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
831 if ( nlsr->detailed_logging )
832 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
833 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, fle->next_hop_face);
834 }
835
836
837 hashtb_next(enle);
838 }
839 hashtb_end(enle);
840
841 char faceid[20];
842 memset(faceid,0,20);
843 sprintf(faceid,"%d",fle->next_hop_face);
844 char *evdata=(char *)malloc(strlen(ne->orig_router)+strlen(faceid)+2);
845 memset(evdata,0,strlen(ne->orig_router)+strlen(faceid)+2);
846 memcpy(evdata+strlen(evdata),ne->orig_router,strlen(ne->orig_router));
847 memcpy(evdata+strlen(evdata),"|",1);
848 memcpy(evdata+strlen(evdata),faceid,strlen(faceid));
849
akmhoquee19e3ee2013-01-22 14:38:45 -0600850 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_old_face_from_npt, (void *)evdata, 0);
akmhoque8fdd6412012-12-04 15:05:33 -0600851
852 }
853
854 hashtb_next(ef);
855 }
856 }
857 hashtb_end(ef);
858
859
860 hashtb_next(e);
861 }
862
863 hashtb_end(e);
864
865}
866
867void
868update_npt_with_new_route(void)
869{
870 clean_old_fib_entries_from_npt();
871 add_new_fib_entries_to_npt();
872
873 int i, npt_element;
874
875 struct npt_entry *ne;
876
877 struct hashtb_enumerator ee;
878 struct hashtb_enumerator *e = &ee;
879
880 hashtb_start(nlsr->npt, e);
881 npt_element=hashtb_n(nlsr->npt);
882
883 for(i=0;i<npt_element;i++)
884 {
885
886 ne=e->data;
887 update_ccnd_fib_for_orig_router(ne->orig_router);
888 hashtb_next(e);
889 }
890
891 hashtb_end(e);
892}
893
894
895
896void
897sort_faces_by_distance(int *faces,int *route_costs,int start,int element)
898{
899 int i,j;
900 int temp_cost;
901 int temp_face;
902
903 for ( i=start ; i < element ; i ++)
904 {
905 for( j=i+1; j<element; j ++)
906 {
907 if (route_costs[j] < route_costs[i] )
908 {
909 temp_cost=route_costs[j];
910 route_costs[j]=route_costs[i];
911 route_costs[i]=temp_cost;
912
913 temp_face=faces[j];
914 faces[j]=faces[i];
915 faces[i]=temp_face;
916 }
917 }
918 }
919
920}
921
922void
923get_all_faces_for_orig_router_from_npt(char *orig_router, int *faces, int *route_costs, int num_faces)
924{
925
926 int res,face_list_element,j;
927 struct hashtb_enumerator ee;
928 struct hashtb_enumerator *e = &ee;
929
930
931 hashtb_start(nlsr->npt, e);
932 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
933
934 if(res == HT_NEW_ENTRY)
935 {
936 hashtb_delete(e);
937 }
938 else if ( res == HT_OLD_ENTRY )
939 {
940 struct npt_entry *ne;
941 ne=e->data;
942
943 struct face_list_entry *fle;
944
945 struct hashtb_enumerator eef;
946 struct hashtb_enumerator *ef = &eef;
947
948 hashtb_start(ne->face_list, ef);
949 face_list_element=hashtb_n(ne->face_list);
950 for(j=0;j<face_list_element;j++)
951 {
952 fle=ef->data;
953 faces[j]=fle->next_hop_face;
954 route_costs[j]=fle->route_cost;
955 hashtb_next(ef);
956 }
957 hashtb_end(ef);
958
959
960 }
961 hashtb_end(e);
962
963}
964
965void
966destroy_faces_by_orig_router(char *orig_router)
967{
968
969 int res;
970 struct hashtb_enumerator ee;
971 struct hashtb_enumerator *e = &ee;
972
973
974 hashtb_start(nlsr->npt, e);
975 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
976
977 if(res == HT_NEW_ENTRY)
978 {
979 hashtb_delete(e);
980 }
981 else if ( res == HT_OLD_ENTRY )
982 {
983 struct npt_entry *ne;
984 ne=e->data;
985 int num_face=hashtb_n(ne->face_list);
986 int last_face,first_face;
987
988 int *faces=(int *)malloc(num_face*sizeof(int));
989 int *route_costs=(int *)malloc(num_face*sizeof(int));
990
991 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
992 sort_faces_by_distance(faces,route_costs,0,num_face);
993
994
995 first_face=num_face-1;
996
997 if ( nlsr->multi_path_face_num == 0 )
998 {
999 last_face=first_face;
1000 }
1001 else
1002 {
1003 if ( num_face <= nlsr->multi_path_face_num)
1004 {
1005 last_face=0;
1006 }
1007 else if ( nlsr->multi_path_face_num == 0)
1008 {
1009 last_face=num_face-nlsr->multi_path_face_num;
1010 }
1011 }
1012
1013 int i,j, nl_element;
1014 struct name_list_entry *nle;
1015 struct hashtb_enumerator eenle;
1016 struct hashtb_enumerator *enle = &eenle;
1017
1018 hashtb_start(ne->name_list, enle);
1019 nl_element=hashtb_n(ne->name_list);
1020
1021 for (i=0;i<nl_element;i++)
1022 {
1023 nle=enle->data;
1024
1025 for( j=first_face; j>= last_face; j--)
1026 {
1027 if ( is_neighbor(orig_router) == 0 )
1028 {
1029 if ( nlsr->debugging )
1030 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1031 if ( nlsr->detailed_logging )
1032 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1033 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
1034 }
1035 else
1036 {
1037 if ( j == last_face && is_neighbor(nle->name)==0)
1038 {
1039 if ( nlsr->debugging )
1040 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1041 if ( nlsr->detailed_logging )
1042 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1043 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
1044 }
1045 }
1046 }
1047
1048 hashtb_next(enle);
1049 }
1050 hashtb_end(enle);
1051
1052
1053
1054 free(faces);
1055 free(route_costs);
1056
1057 }
1058 hashtb_end(e);
1059
1060}
1061
1062void
1063destroy_all_face_by_nlsr(void)
1064{
1065 int i, npt_element;
1066
1067 struct npt_entry *ne;
1068
1069 struct hashtb_enumerator ee;
1070 struct hashtb_enumerator *e = &ee;
1071
1072 hashtb_start(nlsr->npt, e);
1073 npt_element=hashtb_n(nlsr->npt);
1074
1075 for(i=0;i<npt_element;i++)
1076 {
1077 ne=e->data;
1078 destroy_faces_by_orig_router(ne->orig_router);
1079 hashtb_next(e);
1080 }
1081
1082 hashtb_end(e);
1083
1084 if ( nlsr->debugging )
1085 printf("\n");
1086 if ( nlsr->detailed_logging )
1087 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
1088}