blob: 9510aa78da84f2746123bd56fdaaf0d096a47e5a [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 {
akmhoque8fdd6412012-12-04 15:05:33 -0600238
239 if ( is_neighbor(orig_router) == 0 )
240 {
241 if ( nlsr->debugging )
242 printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
243 if ( nlsr->detailed_logging )
244 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoque8fdd6412012-12-04 15:05:33 -0600245 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);
246 }
247 else
248 {
249 if ( j == last_face && is_neighbor(nle->name)==0)
250 {
251 if ( nlsr->debugging )
252 printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
253 if ( nlsr->detailed_logging )
254 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoque8fdd6412012-12-04 15:05:33 -0600255 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);
256 }
257 }
258 }
259
260
261 hashtb_next(enle);
262 }
263 hashtb_end(enle);
264
265
266
267 free(faces);
268 free(route_costs);
269
270 }
271 hashtb_end(e);
272
273}
274
275int
276delete_npt_entry_by_router_and_name_prefix(char *orig_router, char *name_prefix)
277{
278 if ( strcmp(orig_router,nlsr->router_name)== 0)
279 {
280 return -1;
281 }
282
283 struct npt_entry *ne;
284
285 int res,res_nle;
286 struct hashtb_enumerator ee;
287 struct hashtb_enumerator *e = &ee;
288
289
290 hashtb_start(nlsr->npt, e);
291 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
292
293 if(res == HT_NEW_ENTRY)
294 {
295 hashtb_delete(e);
296 return -1;
297 }
298 else if (res == HT_OLD_ENTRY)
299 {
300 ne=e->data;
301
302 struct hashtb_enumerator eenle;
303 struct hashtb_enumerator *enle = &eenle;
304
305 hashtb_start(ne->name_list, enle);
306 res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
307
308 if(res_nle == HT_NEW_ENTRY )
309 {
310 hashtb_delete(enle);
311 }
312 else if(res_nle == HT_OLD_ENTRY )
313 {
314 struct name_list_entry *nle;
315
316 nle=enle->data;
317
318 int j;
319 int num_face=hashtb_n(ne->face_list);
320 int last_face,first_face;
321
322 int *faces=(int *)malloc(num_face*sizeof(int));
323 int *route_costs=(int *)malloc(num_face*sizeof(int));
324
325 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
326 sort_faces_by_distance(faces,route_costs,0,num_face);
327
328
329 first_face=num_face-1;
330
331 if ( nlsr->multi_path_face_num == 0 )
332 {
333 last_face=first_face;
334 }
335 else
336 {
337 if ( num_face <= nlsr->multi_path_face_num)
338 {
339 last_face=0;
340 }
341 else if ( nlsr->multi_path_face_num == 0)
342 {
343 last_face=num_face-nlsr->multi_path_face_num;
344 }
345 }
346
347 for( j=first_face; j>= last_face; j--)
348 {
349
350 if ( is_neighbor(orig_router) == 0 )
351 {
352 if ( nlsr->debugging )
353 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
354 if ( nlsr->detailed_logging )
355 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
356 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
357 }
358 else
359 {
360 if ( j == last_face && is_neighbor(nle->name)==0)
361 {
362 if ( nlsr->debugging )
363 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
364 if ( nlsr->detailed_logging )
365 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
366 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
367 }
368 }
369
370 }
371
akmhoque8fdd6412012-12-04 15:05:33 -0600372 }
373
374 hashtb_end(enle);
375 }
376
377 hashtb_end(e);
378
379 return 0;
380}
381
382void
383print_npt(void)
384{
385
386 if ( nlsr->debugging )
387 {
388 printf("\n");
389 printf("print_npt called\n");
390 }
391 if ( nlsr->detailed_logging )
392 {
393 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
394 writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_npt called\n");
395 }
396 int i, npt_element;
397
398 struct npt_entry *ne;
399
400 struct hashtb_enumerator ee;
401 struct hashtb_enumerator *e = &ee;
402
403 hashtb_start(nlsr->npt, e);
404 npt_element=hashtb_n(nlsr->npt);
405
406 for(i=0;i<npt_element;i++)
407 {
408 if ( nlsr->debugging )
409 {
410 printf("\n");
411 printf("----------NPT ENTRY %d------------------\n",i+1);
412 }
413 if ( nlsr->detailed_logging )
414 {
415 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
416 writeLogg(__FILE__,__FUNCTION__,__LINE__,"----------NPT ENTRY %d------------------\n",i+1);
417 }
418 ne=e->data;
419 if ( nlsr->debugging )
420 printf(" Origination Router: %s \n",ne->orig_router);
421 if ( nlsr->detailed_logging )
422 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s \n",ne->orig_router);
akmhoque8fdd6412012-12-04 15:05:33 -0600423
424 int j, nl_element,face_list_element;
425 struct name_list_entry *nle;
426 struct hashtb_enumerator eenle;
427 struct hashtb_enumerator *enle = &eenle;
428
429 hashtb_start(ne->name_list, enle);
430 nl_element=hashtb_n(ne->name_list);
431
432 for (j=0;j<nl_element;j++)
433 {
434 nle=enle->data;
435 if ( nlsr->debugging )
436 printf(" Name Prefix: %s \n",nle->name);
437 if ( nlsr->detailed_logging )
438 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix: %s \n",nle->name);
439 hashtb_next(enle);
440 }
441 hashtb_end(enle);
442
443 struct face_list_entry *fle;
444
445 struct hashtb_enumerator eef;
446 struct hashtb_enumerator *ef = &eef;
447
448 hashtb_start(ne->face_list, ef);
449 face_list_element=hashtb_n(ne->face_list);
450 if ( face_list_element <= 0 )
451 {
452 if ( nlsr->debugging )
453 printf(" Face: No Face \n");
454 if ( nlsr->detailed_logging )
455 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
456
457 }
458 else
459 {
460 for(j=0;j<face_list_element;j++)
461 {
462 fle=ef->data;
463 if ( nlsr->debugging )
464 printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
465 if ( nlsr->detailed_logging )
466 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
467 hashtb_next(ef);
468 }
469 }
470 hashtb_end(ef);
471
472
473 hashtb_next(e);
474 }
475
476 hashtb_end(e);
477
478 if ( nlsr->debugging )
479 printf("\n");
480 if ( nlsr->detailed_logging )
481 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
482}
483
484void
485delete_orig_router_from_npt(char *orig_router)
486{
487 int res,num_face,num_prefix;
488 struct npt_entry *ne;
489
490 struct hashtb_enumerator ee;
491 struct hashtb_enumerator *e = &ee;
492
493 hashtb_start(nlsr->npt, e);
494 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
495
496 if ( res == HT_OLD_ENTRY )
497 {
498 ne=e->data;
499 num_prefix=hashtb_n(ne->name_list);
500 if ( num_prefix > 0 )
501 {
502 num_face=hashtb_n(ne->face_list);
503
504 if ( num_face > 0 )
505 {
506 int j, nl_element;
507 struct name_list_entry *nle;
508 struct hashtb_enumerator eenle;
509 struct hashtb_enumerator *enle = &eenle;
510
511 hashtb_start(ne->name_list, enle);
512 nl_element=hashtb_n(ne->name_list);
513
514 for (j=0;j<nl_element;j++)
515 {
516 nle=enle->data;
517 delete_npt_entry_by_router_and_name_prefix(ne->orig_router,nle->name);
518 hashtb_next(enle);
519 }
520 hashtb_end(enle);
521
522 }
523
524 }
525 hashtb_destroy(&ne->name_list);
526 hashtb_destroy(&ne->face_list);
527 hashtb_delete(e);
528 }
529 else if ( res == HT_NEW_ENTRY )
530 {
531 hashtb_delete(e);
532 }
533 hashtb_end(e);
534}
535
536
537void
538add_face_to_npt_by_face_id(char *dest_router, int face_id, int route_cost)
539{
540 if ( nlsr->debugging )
541 {
542 printf("add_face_to_npt_by_face_id called\n");
543 printf("Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
544 }
545 if ( nlsr->detailed_logging )
546 {
547 writeLogg(__FILE__,__FUNCTION__,__LINE__,"add_face_to_npt_by_face_id called\n");
548 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
549 }
550
akmhoquee49af7a2013-01-22 14:58:37 -0600551 print_routing_table();
akmhoque8fdd6412012-12-04 15:05:33 -0600552
553 int res,res1;
554 struct npt_entry *ne;
555
556 struct hashtb_enumerator ee;
557 struct hashtb_enumerator *e = &ee;
558
559 hashtb_start(nlsr->npt, e);
560 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
561
562 if ( res == HT_OLD_ENTRY )
563 {
564 if ( nlsr->debugging )
565 printf("Dest Router Found \n");
566 if ( nlsr->detailed_logging )
567 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest Router Found \n");
568
569 ne=e->data;
570
571 struct hashtb_enumerator eef;
572 struct hashtb_enumerator *ef = &eef;
573
574 hashtb_start(ne->face_list, ef);
575 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
576
577 if ( res1 == HT_OLD_ENTRY )
578 {
579 if ( nlsr->debugging )
580 printf("Face Found \n");
581 if ( nlsr->detailed_logging )
582 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face Found \n");
583 struct face_list_entry *fle;//=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
584 fle=ef->data;
585 fle->next_hop_face=face_id;
586 fle->route_cost=route_cost;
587 }
588 else if ( res1 == HT_NEW_ENTRY )
589 {
590 if ( nlsr->debugging )
591 printf("Face Not Found \n");
592 if ( nlsr->detailed_logging )
593 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face Not 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;
akmhoque8fdd6412012-12-04 15:05:33 -0600598 }
599 hashtb_end(ef);
600 }
601 else if (res == HT_NEW_ENTRY)
602 {
603 hashtb_delete(e);
604 }
605
606 hashtb_end(e);
607}
608
609
610void
611add_new_fib_entries_to_npt(void)
612{
613 if ( nlsr->debugging )
614 printf("add_new_fib_entries_to_npt called\n");
615 if ( nlsr->detailed_logging )
616 writeLogg(__FILE__,__FUNCTION__,__LINE__,"add_new_fib_entries_to_npt called\n");
617 int i,j, rt_element,face_list_element;
618
619 struct routing_table_entry *rte;
620
621 struct hashtb_enumerator ee;
622 struct hashtb_enumerator *e = &ee;
623
624 hashtb_start(nlsr->routing_table, e);
625 rt_element=hashtb_n(nlsr->routing_table);
626
627 for(i=0;i<rt_element;i++)
628 {
629 rte=e->data;
630
631 struct face_list_entry *fle;
632
633 struct hashtb_enumerator eef;
634 struct hashtb_enumerator *ef = &eef;
635
636 hashtb_start(rte->face_list, ef);
637 face_list_element=hashtb_n(rte->face_list);
638 if ( face_list_element <= 0 )
639 {
640 if ( nlsr->debugging )
akmhoquee19e3ee2013-01-22 14:38:45 -0600641 printf(" Face: No Face \n");
akmhoque8fdd6412012-12-04 15:05:33 -0600642 if ( nlsr->detailed_logging )
akmhoquee19e3ee2013-01-22 14:38:45 -0600643 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
akmhoque8fdd6412012-12-04 15:05:33 -0600644 }
645 else
646 {
647 for(j=0;j<face_list_element;j++)
648 {
649 fle=ef->data;
akmhoqueeda9c362013-01-23 08:54:29 -0600650 add_face_to_npt_by_face_id(rte->dest_router,fle->next_hop_face,fle->route_cost);
akmhoque8fdd6412012-12-04 15:05:33 -0600651 hashtb_next(ef);
652 }
653 }
654 hashtb_end(ef);
655
656 hashtb_next(e);
657 }
658
659 hashtb_end(e);
660
661}
662
663
664void
665delete_face_from_npt_by_face_id(char *dest_router, int face_id)
666{
667 if ( nlsr->debugging )
668 printf("delete_face_from_npt_by_face_id\n");
669 if ( nlsr->detailed_logging )
670 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_face_from_npt_by_face_id\n");
671
672 int res,res1;
673 struct npt_entry *ne;
674
675 struct hashtb_enumerator ee;
676 struct hashtb_enumerator *e = &ee;
677
678 hashtb_start(nlsr->npt, e);
679 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
680
681 if ( res == HT_OLD_ENTRY )
682 {
683 ne=e->data;
684
685 struct hashtb_enumerator eef;
686 struct hashtb_enumerator *ef = &eef;
687
688 hashtb_start(ne->face_list, ef);
689 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
690
691 if ( res1 == HT_OLD_ENTRY )
692 {
693 hashtb_delete(ef);
694 }
695 else if ( res1 == HT_NEW_ENTRY )
696 {
697 hashtb_delete(ef);
698 }
699 hashtb_end(ef);
700 }
701 else if (res == HT_NEW_ENTRY)
702 {
703 hashtb_delete(e);
704 }
705
706 hashtb_end(e);
707}
708
709int
710delete_old_face_from_npt(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
711{
712 if(flags == CCN_SCHEDULE_CANCEL)
713 {
714 return -1;
715 }
akmhoqueeda9c362013-01-23 08:54:29 -0600716
717 print_npt();
718
akmhoque8fdd6412012-12-04 15:05:33 -0600719 nlsr_lock();
720
721 if ( nlsr->debugging )
722 printf("delete_old_face_from_npt\n");
723 if ( nlsr->detailed_logging )
724 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_old_face_from_npt\n");
725
726 if ( ev->evdata != NULL )
727 {
728 if ( nlsr->debugging )
729 printf("Event Data: %s \n",(char *)ev->evdata);
730 if ( nlsr->detailed_logging )
731 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Event Data: %s \n",(char *)ev->evdata);
732 char *sep="|";
733 char *rem;
734 char *orig_router;
735 char *faceid;
736 int face_id;
737
738 char *face_data=(char *)malloc(strlen((char *)ev->evdata)+1);
739 memset(face_data,0,strlen((char *)ev->evdata)+1);
740 memcpy(face_data+strlen(face_data),(char *)ev->evdata,strlen((char *)ev->evdata));
741
742 orig_router=strtok_r(face_data,sep,&rem);
743 faceid=strtok_r(NULL,sep,&rem);
744 face_id=atoi(faceid);
745
746 if ( nlsr->debugging )
747 printf("Orig Router: %s Face: %d \n",orig_router,face_id);
748 if ( nlsr->detailed_logging )
749 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig Router: %s Face: %d \n",orig_router,face_id);
750
751 delete_face_from_npt_by_face_id(orig_router,face_id);
752 }
753
754 nlsr_unlock();
755
756 return 0;
757}
758
759void
760clean_old_fib_entries_from_npt(void)
761{
762
763
764 if ( nlsr->debugging )
765 printf("clean_old_fib_entries_from_npt called\n\n");
766 if ( nlsr->detailed_logging )
767 writeLogg(__FILE__,__FUNCTION__,__LINE__,"clean_old_fib_entries_from_npt called\n\n");
768 int i, npt_element;
769
770 struct npt_entry *ne;
771
772 struct hashtb_enumerator ee;
773 struct hashtb_enumerator *e = &ee;
774
775 hashtb_start(nlsr->npt, e);
776 npt_element=hashtb_n(nlsr->npt);
777
778 for(i=0;i<npt_element;i++)
779 {
780 ne=e->data;
781
782 int j,k, nl_element,face_list_element;
783 struct face_list_entry *fle;
784
785 struct hashtb_enumerator eef;
786 struct hashtb_enumerator *ef = &eef;
787
788 hashtb_start(ne->face_list, ef);
789 face_list_element=hashtb_n(ne->face_list);
790 if ( face_list_element <= 0 )
791 {
792 if ( nlsr->debugging )
akmhoquee49af7a2013-01-22 14:58:37 -0600793 printf(" Face: No Face \n");
akmhoque8fdd6412012-12-04 15:05:33 -0600794 if ( nlsr->detailed_logging )
795 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
796
797 }
798 else
799 {
800 for(j=0;j<face_list_element;j++)
801 {
802 fle=ef->data;
803 int check=does_face_exist_for_router(ne->orig_router,fle->next_hop_face);
804 if ( check == 0 )
805 {
806 struct name_list_entry *nle;
807 struct hashtb_enumerator eenle;
808 struct hashtb_enumerator *enle = &eenle;
809
810 hashtb_start(ne->name_list, enle);
811 nl_element=hashtb_n(ne->name_list);
812
813 for (k=0;k<nl_element;k++)
814 {
815 nle=enle->data;
akmhoque8fdd6412012-12-04 15:05:33 -0600816 if( is_neighbor(nle->name) == 0 )
817 {
818 if ( nlsr->debugging )
819 printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
820 if ( nlsr->detailed_logging )
821 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
akmhoque580ccf32013-01-23 09:12:33 -0600822 //add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, fle->next_hop_face);
akmhoque8fdd6412012-12-04 15:05:33 -0600823 }
824
825
826 hashtb_next(enle);
827 }
828 hashtb_end(enle);
829
830 char faceid[20];
831 memset(faceid,0,20);
832 sprintf(faceid,"%d",fle->next_hop_face);
833 char *evdata=(char *)malloc(strlen(ne->orig_router)+strlen(faceid)+2);
834 memset(evdata,0,strlen(ne->orig_router)+strlen(faceid)+2);
835 memcpy(evdata+strlen(evdata),ne->orig_router,strlen(ne->orig_router));
836 memcpy(evdata+strlen(evdata),"|",1);
837 memcpy(evdata+strlen(evdata),faceid,strlen(faceid));
838
akmhoquee19e3ee2013-01-22 14:38:45 -0600839 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_old_face_from_npt, (void *)evdata, 0);
akmhoque8fdd6412012-12-04 15:05:33 -0600840
841 }
842
843 hashtb_next(ef);
844 }
845 }
846 hashtb_end(ef);
847
848
849 hashtb_next(e);
850 }
851
852 hashtb_end(e);
853
854}
855
856void
857update_npt_with_new_route(void)
858{
akmhoque580ccf32013-01-23 09:12:33 -0600859 if ( nlsr->debugging )
860 printf("update_npt_with_new_route called\n");
861
akmhoque8fdd6412012-12-04 15:05:33 -0600862 clean_old_fib_entries_from_npt();
akmhoque0b217bf2013-01-23 09:49:47 -0600863
864 print_routing_table();
865
akmhoque8fdd6412012-12-04 15:05:33 -0600866 add_new_fib_entries_to_npt();
867
868 int i, npt_element;
869
870 struct npt_entry *ne;
871
872 struct hashtb_enumerator ee;
873 struct hashtb_enumerator *e = &ee;
874
875 hashtb_start(nlsr->npt, e);
876 npt_element=hashtb_n(nlsr->npt);
877
878 for(i=0;i<npt_element;i++)
879 {
880
881 ne=e->data;
akmhoque580ccf32013-01-23 09:12:33 -0600882 //update_ccnd_fib_for_orig_router(ne->orig_router);
akmhoque8fdd6412012-12-04 15:05:33 -0600883 hashtb_next(e);
884 }
885
886 hashtb_end(e);
887}
888
889
890
891void
892sort_faces_by_distance(int *faces,int *route_costs,int start,int element)
893{
894 int i,j;
895 int temp_cost;
896 int temp_face;
897
898 for ( i=start ; i < element ; i ++)
899 {
900 for( j=i+1; j<element; j ++)
901 {
902 if (route_costs[j] < route_costs[i] )
903 {
904 temp_cost=route_costs[j];
905 route_costs[j]=route_costs[i];
906 route_costs[i]=temp_cost;
907
908 temp_face=faces[j];
909 faces[j]=faces[i];
910 faces[i]=temp_face;
911 }
912 }
913 }
914
915}
916
917void
918get_all_faces_for_orig_router_from_npt(char *orig_router, int *faces, int *route_costs, int num_faces)
919{
920
921 int res,face_list_element,j;
922 struct hashtb_enumerator ee;
923 struct hashtb_enumerator *e = &ee;
924
925
926 hashtb_start(nlsr->npt, e);
927 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
928
929 if(res == HT_NEW_ENTRY)
930 {
931 hashtb_delete(e);
932 }
933 else if ( res == HT_OLD_ENTRY )
934 {
935 struct npt_entry *ne;
936 ne=e->data;
937
938 struct face_list_entry *fle;
939
940 struct hashtb_enumerator eef;
941 struct hashtb_enumerator *ef = &eef;
942
943 hashtb_start(ne->face_list, ef);
944 face_list_element=hashtb_n(ne->face_list);
945 for(j=0;j<face_list_element;j++)
946 {
947 fle=ef->data;
948 faces[j]=fle->next_hop_face;
949 route_costs[j]=fle->route_cost;
950 hashtb_next(ef);
951 }
952 hashtb_end(ef);
953
954
955 }
956 hashtb_end(e);
957
958}
959
960void
961destroy_faces_by_orig_router(char *orig_router)
962{
963
964 int res;
965 struct hashtb_enumerator ee;
966 struct hashtb_enumerator *e = &ee;
967
968
969 hashtb_start(nlsr->npt, e);
970 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
971
972 if(res == HT_NEW_ENTRY)
973 {
974 hashtb_delete(e);
975 }
976 else if ( res == HT_OLD_ENTRY )
977 {
978 struct npt_entry *ne;
979 ne=e->data;
980 int num_face=hashtb_n(ne->face_list);
981 int last_face,first_face;
982
983 int *faces=(int *)malloc(num_face*sizeof(int));
984 int *route_costs=(int *)malloc(num_face*sizeof(int));
985
986 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
987 sort_faces_by_distance(faces,route_costs,0,num_face);
988
989
990 first_face=num_face-1;
991
992 if ( nlsr->multi_path_face_num == 0 )
993 {
994 last_face=first_face;
995 }
996 else
997 {
998 if ( num_face <= nlsr->multi_path_face_num)
999 {
1000 last_face=0;
1001 }
1002 else if ( nlsr->multi_path_face_num == 0)
1003 {
1004 last_face=num_face-nlsr->multi_path_face_num;
1005 }
1006 }
1007
1008 int i,j, nl_element;
1009 struct name_list_entry *nle;
1010 struct hashtb_enumerator eenle;
1011 struct hashtb_enumerator *enle = &eenle;
1012
1013 hashtb_start(ne->name_list, enle);
1014 nl_element=hashtb_n(ne->name_list);
1015
1016 for (i=0;i<nl_element;i++)
1017 {
1018 nle=enle->data;
1019
1020 for( j=first_face; j>= last_face; j--)
1021 {
1022 if ( is_neighbor(orig_router) == 0 )
1023 {
1024 if ( nlsr->debugging )
1025 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1026 if ( nlsr->detailed_logging )
1027 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1028 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
1029 }
1030 else
1031 {
1032 if ( j == last_face && is_neighbor(nle->name)==0)
1033 {
1034 if ( nlsr->debugging )
1035 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1036 if ( nlsr->detailed_logging )
1037 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1038 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
1039 }
1040 }
1041 }
1042
1043 hashtb_next(enle);
1044 }
1045 hashtb_end(enle);
1046
1047
1048
1049 free(faces);
1050 free(route_costs);
1051
1052 }
1053 hashtb_end(e);
1054
1055}
1056
1057void
1058destroy_all_face_by_nlsr(void)
1059{
1060 int i, npt_element;
1061
1062 struct npt_entry *ne;
1063
1064 struct hashtb_enumerator ee;
1065 struct hashtb_enumerator *e = &ee;
1066
1067 hashtb_start(nlsr->npt, e);
1068 npt_element=hashtb_n(nlsr->npt);
1069
1070 for(i=0;i<npt_element;i++)
1071 {
1072 ne=e->data;
1073 destroy_faces_by_orig_router(ne->orig_router);
1074 hashtb_next(e);
1075 }
1076
1077 hashtb_end(e);
1078
1079 if ( nlsr->debugging )
1080 printf("\n");
1081 if ( nlsr->detailed_logging )
1082 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
1083}