blob: 9ff24ce96c1941fdd490956ac05dff7aebe74ee9 [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{
akmhoqueaaa10552013-01-24 08:00:42 -0600179 // problem in this method I think ... need to fix it.
180
181 if ( nlsr->debugging )
182 {
183 printf("update_ccnd_fib_for_orig_router called\n");
184 }
akmhoque8fdd6412012-12-04 15:05:33 -0600185
186 int res;
187 struct hashtb_enumerator ee;
188 struct hashtb_enumerator *e = &ee;
189
190
191 hashtb_start(nlsr->npt, e);
192 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
193
194 if(res == HT_NEW_ENTRY)
195 {
196 hashtb_delete(e);
197 }
198 else if ( res == HT_OLD_ENTRY )
199 {
200 struct npt_entry *ne;
201 ne=e->data;
202 int num_face=hashtb_n(ne->face_list);
203 int last_face,first_face;
204
205 int *faces=(int *)malloc(num_face*sizeof(int));
206 int *route_costs=(int *)malloc(num_face*sizeof(int));
207
208 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
209 sort_faces_by_distance(faces,route_costs,0,num_face);
210
akmhoqueaaa10552013-01-24 08:00:42 -0600211 int m;
212 for ( m =0 ; m< num_face ; m++)
213 {
214 printf("Face: %d Cost: %d \n",faces[m],route_costs[m]);
215 }
akmhoque8fdd6412012-12-04 15:05:33 -0600216
217 first_face=num_face-1;
218
219 if ( nlsr->multi_path_face_num == 0 )
220 {
221 last_face=first_face;
222 }
223 else
224 {
225 if ( num_face <= nlsr->multi_path_face_num)
226 {
227 last_face=0;
228 }
229 else if ( nlsr->multi_path_face_num == 0)
230 {
231 last_face=num_face-nlsr->multi_path_face_num;
232 }
233 }
234
235 int i,j, nl_element;
236 struct name_list_entry *nle;
237 struct hashtb_enumerator eenle;
238 struct hashtb_enumerator *enle = &eenle;
239
240 hashtb_start(ne->name_list, enle);
241 nl_element=hashtb_n(ne->name_list);
242
243 for (i=0;i<nl_element;i++)
244 {
245 nle=enle->data;
246
247 for( j=first_face; j>= last_face; j--)
248 {
akmhoque8fdd6412012-12-04 15:05:33 -0600249
250 if ( is_neighbor(orig_router) == 0 )
251 {
252 if ( nlsr->debugging )
253 printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
254 if ( nlsr->detailed_logging )
255 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoqueaaa10552013-01-24 08:00:42 -0600256 //add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);
akmhoque8fdd6412012-12-04 15:05:33 -0600257 }
258 else
259 {
260 if ( j == last_face && is_neighbor(nle->name)==0)
261 {
262 if ( nlsr->debugging )
263 printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
264 if ( nlsr->detailed_logging )
265 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoqueaaa10552013-01-24 08:00:42 -0600266 //add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);
akmhoque8fdd6412012-12-04 15:05:33 -0600267 }
268 }
269 }
270
271
272 hashtb_next(enle);
273 }
274 hashtb_end(enle);
275
276
277
278 free(faces);
279 free(route_costs);
280
281 }
282 hashtb_end(e);
283
284}
285
286int
287delete_npt_entry_by_router_and_name_prefix(char *orig_router, char *name_prefix)
288{
289 if ( strcmp(orig_router,nlsr->router_name)== 0)
290 {
291 return -1;
292 }
293
294 struct npt_entry *ne;
295
296 int res,res_nle;
297 struct hashtb_enumerator ee;
298 struct hashtb_enumerator *e = &ee;
299
300
301 hashtb_start(nlsr->npt, e);
302 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
303
304 if(res == HT_NEW_ENTRY)
305 {
306 hashtb_delete(e);
307 return -1;
308 }
309 else if (res == HT_OLD_ENTRY)
310 {
311 ne=e->data;
312
313 struct hashtb_enumerator eenle;
314 struct hashtb_enumerator *enle = &eenle;
315
316 hashtb_start(ne->name_list, enle);
317 res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
318
319 if(res_nle == HT_NEW_ENTRY )
320 {
321 hashtb_delete(enle);
322 }
323 else if(res_nle == HT_OLD_ENTRY )
324 {
325 struct name_list_entry *nle;
326
327 nle=enle->data;
328
329 int j;
330 int num_face=hashtb_n(ne->face_list);
331 int last_face,first_face;
332
333 int *faces=(int *)malloc(num_face*sizeof(int));
334 int *route_costs=(int *)malloc(num_face*sizeof(int));
335
336 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
337 sort_faces_by_distance(faces,route_costs,0,num_face);
338
339
340 first_face=num_face-1;
341
342 if ( nlsr->multi_path_face_num == 0 )
343 {
344 last_face=first_face;
345 }
346 else
347 {
348 if ( num_face <= nlsr->multi_path_face_num)
349 {
350 last_face=0;
351 }
352 else if ( nlsr->multi_path_face_num == 0)
353 {
354 last_face=num_face-nlsr->multi_path_face_num;
355 }
356 }
357
358 for( j=first_face; j>= last_face; j--)
359 {
360
361 if ( is_neighbor(orig_router) == 0 )
362 {
363 if ( nlsr->debugging )
364 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
365 if ( nlsr->detailed_logging )
366 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
367 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
368 }
369 else
370 {
371 if ( j == last_face && is_neighbor(nle->name)==0)
372 {
373 if ( nlsr->debugging )
374 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
375 if ( nlsr->detailed_logging )
376 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
377 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
378 }
379 }
380
381 }
382
akmhoque8fdd6412012-12-04 15:05:33 -0600383 }
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);
akmhoque8fdd6412012-12-04 15:05:33 -0600434
435 int j, nl_element,face_list_element;
436 struct name_list_entry *nle;
437 struct hashtb_enumerator eenle;
438 struct hashtb_enumerator *enle = &eenle;
439
440 hashtb_start(ne->name_list, enle);
441 nl_element=hashtb_n(ne->name_list);
442
443 for (j=0;j<nl_element;j++)
444 {
445 nle=enle->data;
446 if ( nlsr->debugging )
447 printf(" Name Prefix: %s \n",nle->name);
448 if ( nlsr->detailed_logging )
449 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix: %s \n",nle->name);
450 hashtb_next(enle);
451 }
452 hashtb_end(enle);
453
454 struct face_list_entry *fle;
455
456 struct hashtb_enumerator eef;
457 struct hashtb_enumerator *ef = &eef;
458
459 hashtb_start(ne->face_list, ef);
460 face_list_element=hashtb_n(ne->face_list);
461 if ( face_list_element <= 0 )
462 {
463 if ( nlsr->debugging )
464 printf(" Face: No Face \n");
465 if ( nlsr->detailed_logging )
466 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
467
468 }
469 else
470 {
471 for(j=0;j<face_list_element;j++)
472 {
473 fle=ef->data;
474 if ( nlsr->debugging )
475 printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
476 if ( nlsr->detailed_logging )
477 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
478 hashtb_next(ef);
479 }
480 }
481 hashtb_end(ef);
482
483
484 hashtb_next(e);
485 }
486
487 hashtb_end(e);
488
489 if ( nlsr->debugging )
490 printf("\n");
491 if ( nlsr->detailed_logging )
492 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
493}
494
495void
496delete_orig_router_from_npt(char *orig_router)
497{
498 int res,num_face,num_prefix;
499 struct npt_entry *ne;
500
501 struct hashtb_enumerator ee;
502 struct hashtb_enumerator *e = &ee;
503
504 hashtb_start(nlsr->npt, e);
505 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
506
507 if ( res == HT_OLD_ENTRY )
508 {
509 ne=e->data;
510 num_prefix=hashtb_n(ne->name_list);
511 if ( num_prefix > 0 )
512 {
513 num_face=hashtb_n(ne->face_list);
514
515 if ( num_face > 0 )
516 {
517 int j, nl_element;
518 struct name_list_entry *nle;
519 struct hashtb_enumerator eenle;
520 struct hashtb_enumerator *enle = &eenle;
521
522 hashtb_start(ne->name_list, enle);
523 nl_element=hashtb_n(ne->name_list);
524
525 for (j=0;j<nl_element;j++)
526 {
527 nle=enle->data;
528 delete_npt_entry_by_router_and_name_prefix(ne->orig_router,nle->name);
529 hashtb_next(enle);
530 }
531 hashtb_end(enle);
532
533 }
534
535 }
536 hashtb_destroy(&ne->name_list);
537 hashtb_destroy(&ne->face_list);
538 hashtb_delete(e);
539 }
540 else if ( res == HT_NEW_ENTRY )
541 {
542 hashtb_delete(e);
543 }
544 hashtb_end(e);
545}
546
547
548void
549add_face_to_npt_by_face_id(char *dest_router, int face_id, int route_cost)
550{
551 if ( nlsr->debugging )
552 {
553 printf("add_face_to_npt_by_face_id called\n");
554 printf("Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
555 }
556 if ( nlsr->detailed_logging )
557 {
558 writeLogg(__FILE__,__FUNCTION__,__LINE__,"add_face_to_npt_by_face_id called\n");
559 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
560 }
561
akmhoque613446f2013-01-23 10:40:12 -0600562 //print_routing_table();
akmhoque8fdd6412012-12-04 15:05:33 -0600563
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;
akmhoqueaaa10552013-01-24 08:00:42 -0600609
610
611
612
akmhoque8fdd6412012-12-04 15:05:33 -0600613 }
614 hashtb_end(ef);
615 }
616 else if (res == HT_NEW_ENTRY)
617 {
618 hashtb_delete(e);
619 }
620
621 hashtb_end(e);
622}
623
624
625void
626add_new_fib_entries_to_npt(void)
627{
628 if ( nlsr->debugging )
629 printf("add_new_fib_entries_to_npt called\n");
630 if ( nlsr->detailed_logging )
631 writeLogg(__FILE__,__FUNCTION__,__LINE__,"add_new_fib_entries_to_npt called\n");
632 int i,j, rt_element,face_list_element;
633
634 struct routing_table_entry *rte;
635
636 struct hashtb_enumerator ee;
637 struct hashtb_enumerator *e = &ee;
638
639 hashtb_start(nlsr->routing_table, e);
640 rt_element=hashtb_n(nlsr->routing_table);
641
642 for(i=0;i<rt_element;i++)
643 {
644 rte=e->data;
645
646 struct face_list_entry *fle;
647
648 struct hashtb_enumerator eef;
649 struct hashtb_enumerator *ef = &eef;
650
651 hashtb_start(rte->face_list, ef);
652 face_list_element=hashtb_n(rte->face_list);
653 if ( face_list_element <= 0 )
654 {
655 if ( nlsr->debugging )
akmhoquee19e3ee2013-01-22 14:38:45 -0600656 printf(" Face: No Face \n");
akmhoque8fdd6412012-12-04 15:05:33 -0600657 if ( nlsr->detailed_logging )
akmhoquee19e3ee2013-01-22 14:38:45 -0600658 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
akmhoque8fdd6412012-12-04 15:05:33 -0600659 }
660 else
661 {
662 for(j=0;j<face_list_element;j++)
663 {
664 fle=ef->data;
akmhoqueeda9c362013-01-23 08:54:29 -0600665 add_face_to_npt_by_face_id(rte->dest_router,fle->next_hop_face,fle->route_cost);
akmhoque8fdd6412012-12-04 15:05:33 -0600666 hashtb_next(ef);
667 }
668 }
669 hashtb_end(ef);
670
671 hashtb_next(e);
672 }
673
674 hashtb_end(e);
675
676}
677
678
679void
680delete_face_from_npt_by_face_id(char *dest_router, int face_id)
681{
682 if ( nlsr->debugging )
683 printf("delete_face_from_npt_by_face_id\n");
684 if ( nlsr->detailed_logging )
685 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_face_from_npt_by_face_id\n");
686
687 int res,res1;
688 struct npt_entry *ne;
689
690 struct hashtb_enumerator ee;
691 struct hashtb_enumerator *e = &ee;
692
693 hashtb_start(nlsr->npt, e);
694 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
695
696 if ( res == HT_OLD_ENTRY )
697 {
698 ne=e->data;
699
700 struct hashtb_enumerator eef;
701 struct hashtb_enumerator *ef = &eef;
702
703 hashtb_start(ne->face_list, ef);
704 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
akmhoqueaaa10552013-01-24 08:00:42 -0600705 /*
akmhoque8fdd6412012-12-04 15:05:33 -0600706 if ( res1 == HT_OLD_ENTRY )
707 {
708 hashtb_delete(ef);
709 }
710 else if ( res1 == HT_NEW_ENTRY )
711 {
712 hashtb_delete(ef);
713 }
akmhoqueaaa10552013-01-24 08:00:42 -0600714 */
715 hashtb_delete(ef);
akmhoque8fdd6412012-12-04 15:05:33 -0600716 hashtb_end(ef);
717 }
718 else if (res == HT_NEW_ENTRY)
719 {
720 hashtb_delete(e);
721 }
722
723 hashtb_end(e);
724}
725
726int
727delete_old_face_from_npt(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
728{
729 if(flags == CCN_SCHEDULE_CANCEL)
730 {
731 return -1;
732 }
akmhoqueeda9c362013-01-23 08:54:29 -0600733
734 print_npt();
735
akmhoque8fdd6412012-12-04 15:05:33 -0600736 nlsr_lock();
737
738 if ( nlsr->debugging )
739 printf("delete_old_face_from_npt\n");
740 if ( nlsr->detailed_logging )
741 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_old_face_from_npt\n");
742
743 if ( ev->evdata != NULL )
744 {
745 if ( nlsr->debugging )
746 printf("Event Data: %s \n",(char *)ev->evdata);
747 if ( nlsr->detailed_logging )
748 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Event Data: %s \n",(char *)ev->evdata);
749 char *sep="|";
750 char *rem;
751 char *orig_router;
752 char *faceid;
753 int face_id;
754
755 char *face_data=(char *)malloc(strlen((char *)ev->evdata)+1);
756 memset(face_data,0,strlen((char *)ev->evdata)+1);
757 memcpy(face_data+strlen(face_data),(char *)ev->evdata,strlen((char *)ev->evdata));
758
759 orig_router=strtok_r(face_data,sep,&rem);
760 faceid=strtok_r(NULL,sep,&rem);
761 face_id=atoi(faceid);
762
763 if ( nlsr->debugging )
764 printf("Orig Router: %s Face: %d \n",orig_router,face_id);
765 if ( nlsr->detailed_logging )
766 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig Router: %s Face: %d \n",orig_router,face_id);
767
768 delete_face_from_npt_by_face_id(orig_router,face_id);
769 }
770
771 nlsr_unlock();
772
773 return 0;
774}
775
776void
777clean_old_fib_entries_from_npt(void)
778{
779
780
781 if ( nlsr->debugging )
782 printf("clean_old_fib_entries_from_npt called\n\n");
783 if ( nlsr->detailed_logging )
784 writeLogg(__FILE__,__FUNCTION__,__LINE__,"clean_old_fib_entries_from_npt called\n\n");
785 int i, npt_element;
786
787 struct npt_entry *ne;
788
789 struct hashtb_enumerator ee;
790 struct hashtb_enumerator *e = &ee;
791
792 hashtb_start(nlsr->npt, e);
793 npt_element=hashtb_n(nlsr->npt);
794
795 for(i=0;i<npt_element;i++)
796 {
797 ne=e->data;
798
799 int j,k, nl_element,face_list_element;
800 struct face_list_entry *fle;
801
802 struct hashtb_enumerator eef;
803 struct hashtb_enumerator *ef = &eef;
804
805 hashtb_start(ne->face_list, ef);
806 face_list_element=hashtb_n(ne->face_list);
807 if ( face_list_element <= 0 )
808 {
809 if ( nlsr->debugging )
akmhoquee49af7a2013-01-22 14:58:37 -0600810 printf(" Face: No Face \n");
akmhoque8fdd6412012-12-04 15:05:33 -0600811 if ( nlsr->detailed_logging )
812 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
813
814 }
815 else
816 {
817 for(j=0;j<face_list_element;j++)
818 {
819 fle=ef->data;
820 int check=does_face_exist_for_router(ne->orig_router,fle->next_hop_face);
821 if ( check == 0 )
822 {
823 struct name_list_entry *nle;
824 struct hashtb_enumerator eenle;
825 struct hashtb_enumerator *enle = &eenle;
826
827 hashtb_start(ne->name_list, enle);
828 nl_element=hashtb_n(ne->name_list);
829
830 for (k=0;k<nl_element;k++)
831 {
832 nle=enle->data;
akmhoque8fdd6412012-12-04 15:05:33 -0600833 if( is_neighbor(nle->name) == 0 )
834 {
835 if ( nlsr->debugging )
836 printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
837 if ( nlsr->detailed_logging )
838 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
akmhoque613446f2013-01-23 10:40:12 -0600839 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 -0600840 }
841
842
843 hashtb_next(enle);
844 }
845 hashtb_end(enle);
846
akmhoqueaaa10552013-01-24 08:00:42 -0600847 /*char faceid[20];
akmhoque8fdd6412012-12-04 15:05:33 -0600848 memset(faceid,0,20);
849 sprintf(faceid,"%d",fle->next_hop_face);
850 char *evdata=(char *)malloc(strlen(ne->orig_router)+strlen(faceid)+2);
851 memset(evdata,0,strlen(ne->orig_router)+strlen(faceid)+2);
852 memcpy(evdata+strlen(evdata),ne->orig_router,strlen(ne->orig_router));
853 memcpy(evdata+strlen(evdata),"|",1);
854 memcpy(evdata+strlen(evdata),faceid,strlen(faceid));
855
akmhoquee19e3ee2013-01-22 14:38:45 -0600856 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_old_face_from_npt, (void *)evdata, 0);
akmhoque8fdd6412012-12-04 15:05:33 -0600857
akmhoqueaaa10552013-01-24 08:00:42 -0600858 */
859 hashtb_delete(ef);
860 j++;
861
862 }
863 else
864 {
865 hashtb_next(ef);
866 }
akmhoque8fdd6412012-12-04 15:05:33 -0600867 }
868 }
869 hashtb_end(ef);
870
871
872 hashtb_next(e);
873 }
874
875 hashtb_end(e);
876
877}
878
879void
880update_npt_with_new_route(void)
881{
akmhoque580ccf32013-01-23 09:12:33 -0600882 if ( nlsr->debugging )
883 printf("update_npt_with_new_route called\n");
884
akmhoque8fdd6412012-12-04 15:05:33 -0600885 clean_old_fib_entries_from_npt();
akmhoqueaaa10552013-01-24 08:00:42 -0600886 print_npt();
akmhoque8fdd6412012-12-04 15:05:33 -0600887 add_new_fib_entries_to_npt();
akmhoqueaaa10552013-01-24 08:00:42 -0600888 print_npt();
889
akmhoque8fdd6412012-12-04 15:05:33 -0600890 int i, npt_element;
891
892 struct npt_entry *ne;
893
894 struct hashtb_enumerator ee;
895 struct hashtb_enumerator *e = &ee;
896
897 hashtb_start(nlsr->npt, e);
898 npt_element=hashtb_n(nlsr->npt);
899
900 for(i=0;i<npt_element;i++)
901 {
902
903 ne=e->data;
akmhoque613446f2013-01-23 10:40:12 -0600904 update_ccnd_fib_for_orig_router(ne->orig_router);
akmhoque8fdd6412012-12-04 15:05:33 -0600905 hashtb_next(e);
906 }
907
908 hashtb_end(e);
909}
910
911
912
913void
914sort_faces_by_distance(int *faces,int *route_costs,int start,int element)
915{
916 int i,j;
917 int temp_cost;
918 int temp_face;
919
920 for ( i=start ; i < element ; i ++)
921 {
922 for( j=i+1; j<element; j ++)
923 {
924 if (route_costs[j] < route_costs[i] )
925 {
926 temp_cost=route_costs[j];
927 route_costs[j]=route_costs[i];
928 route_costs[i]=temp_cost;
929
930 temp_face=faces[j];
931 faces[j]=faces[i];
932 faces[i]=temp_face;
933 }
934 }
935 }
936
937}
938
939void
940get_all_faces_for_orig_router_from_npt(char *orig_router, int *faces, int *route_costs, int num_faces)
941{
942
943 int res,face_list_element,j;
944 struct hashtb_enumerator ee;
945 struct hashtb_enumerator *e = &ee;
946
947
948 hashtb_start(nlsr->npt, e);
949 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
950
951 if(res == HT_NEW_ENTRY)
952 {
953 hashtb_delete(e);
954 }
955 else if ( res == HT_OLD_ENTRY )
956 {
957 struct npt_entry *ne;
958 ne=e->data;
959
960 struct face_list_entry *fle;
961
962 struct hashtb_enumerator eef;
963 struct hashtb_enumerator *ef = &eef;
964
965 hashtb_start(ne->face_list, ef);
966 face_list_element=hashtb_n(ne->face_list);
967 for(j=0;j<face_list_element;j++)
968 {
969 fle=ef->data;
970 faces[j]=fle->next_hop_face;
971 route_costs[j]=fle->route_cost;
972 hashtb_next(ef);
973 }
974 hashtb_end(ef);
975
976
977 }
978 hashtb_end(e);
979
980}
981
982void
983destroy_faces_by_orig_router(char *orig_router)
984{
985
986 int res;
987 struct hashtb_enumerator ee;
988 struct hashtb_enumerator *e = &ee;
989
990
991 hashtb_start(nlsr->npt, e);
992 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
993
994 if(res == HT_NEW_ENTRY)
995 {
996 hashtb_delete(e);
997 }
998 else if ( res == HT_OLD_ENTRY )
999 {
1000 struct npt_entry *ne;
1001 ne=e->data;
1002 int num_face=hashtb_n(ne->face_list);
1003 int last_face,first_face;
1004
1005 int *faces=(int *)malloc(num_face*sizeof(int));
1006 int *route_costs=(int *)malloc(num_face*sizeof(int));
1007
1008 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
1009 sort_faces_by_distance(faces,route_costs,0,num_face);
1010
1011
1012 first_face=num_face-1;
1013
1014 if ( nlsr->multi_path_face_num == 0 )
1015 {
1016 last_face=first_face;
1017 }
1018 else
1019 {
1020 if ( num_face <= nlsr->multi_path_face_num)
1021 {
1022 last_face=0;
1023 }
1024 else if ( nlsr->multi_path_face_num == 0)
1025 {
1026 last_face=num_face-nlsr->multi_path_face_num;
1027 }
1028 }
1029
1030 int i,j, nl_element;
1031 struct name_list_entry *nle;
1032 struct hashtb_enumerator eenle;
1033 struct hashtb_enumerator *enle = &eenle;
1034
1035 hashtb_start(ne->name_list, enle);
1036 nl_element=hashtb_n(ne->name_list);
1037
1038 for (i=0;i<nl_element;i++)
1039 {
1040 nle=enle->data;
1041
1042 for( j=first_face; j>= last_face; j--)
1043 {
1044 if ( is_neighbor(orig_router) == 0 )
1045 {
1046 if ( nlsr->debugging )
1047 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1048 if ( nlsr->detailed_logging )
1049 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1050 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
1051 }
1052 else
1053 {
1054 if ( j == last_face && is_neighbor(nle->name)==0)
1055 {
1056 if ( nlsr->debugging )
1057 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1058 if ( nlsr->detailed_logging )
1059 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1060 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
1061 }
1062 }
1063 }
1064
1065 hashtb_next(enle);
1066 }
1067 hashtb_end(enle);
1068
1069
1070
1071 free(faces);
1072 free(route_costs);
1073
1074 }
1075 hashtb_end(e);
1076
1077}
1078
1079void
1080destroy_all_face_by_nlsr(void)
1081{
1082 int i, npt_element;
1083
1084 struct npt_entry *ne;
1085
1086 struct hashtb_enumerator ee;
1087 struct hashtb_enumerator *e = &ee;
1088
1089 hashtb_start(nlsr->npt, e);
1090 npt_element=hashtb_n(nlsr->npt);
1091
1092 for(i=0;i<npt_element;i++)
1093 {
1094 ne=e->data;
1095 destroy_faces_by_orig_router(ne->orig_router);
1096 hashtb_next(e);
1097 }
1098
1099 hashtb_end(e);
1100
1101 if ( nlsr->debugging )
1102 printf("\n");
1103 if ( nlsr->detailed_logging )
1104 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
1105}