blob: e30eb6d751c59048420141ce109ec235ff02ddda [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
180 if ( nlsr->debugging )
181 {
182 printf("update_ccnd_fib_for_orig_router called\n");
183 }
akmhoque8fdd6412012-12-04 15:05:33 -0600184
185 int res;
186 struct hashtb_enumerator ee;
187 struct hashtb_enumerator *e = &ee;
188
189
190 hashtb_start(nlsr->npt, e);
191 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
192
193 if(res == HT_NEW_ENTRY)
194 {
195 hashtb_delete(e);
196 }
197 else if ( res == HT_OLD_ENTRY )
198 {
199 struct npt_entry *ne;
200 ne=e->data;
201 int num_face=hashtb_n(ne->face_list);
202 int last_face,first_face;
203
204 int *faces=(int *)malloc(num_face*sizeof(int));
205 int *route_costs=(int *)malloc(num_face*sizeof(int));
206
207 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
208 sort_faces_by_distance(faces,route_costs,0,num_face);
209
akmhoqueaaa10552013-01-24 08:00:42 -0600210 int m;
211 for ( m =0 ; m< num_face ; m++)
212 {
213 printf("Face: %d Cost: %d \n",faces[m],route_costs[m]);
214 }
akmhoque8fdd6412012-12-04 15:05:33 -0600215
216 first_face=num_face-1;
217
218 if ( nlsr->multi_path_face_num == 0 )
219 {
220 last_face=first_face;
221 }
222 else
223 {
224 if ( num_face <= nlsr->multi_path_face_num)
225 {
226 last_face=0;
227 }
akmhoquedc27b952013-01-24 09:34:00 -0600228 else
akmhoque8fdd6412012-12-04 15:05:33 -0600229 {
230 last_face=num_face-nlsr->multi_path_face_num;
231 }
akmhoquedc27b952013-01-24 09:34:00 -0600232
akmhoque8fdd6412012-12-04 15:05:33 -0600233 }
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
akmhoquedc27b952013-01-24 09:34:00 -0600250 printf("FIB Entry Name: %s Face: %d Router Cost: %d \n",nle->name,faces[j],route_costs[j]);
251
akmhoque9ca1bb82013-01-24 09:57:57 -0600252 if ( is_active_neighbor(orig_router) == 0 )
akmhoque8fdd6412012-12-04 15:05:33 -0600253 {
254 if ( nlsr->debugging )
255 printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
256 if ( nlsr->detailed_logging )
257 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoqueaaa10552013-01-24 08:00:42 -0600258 //add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);
akmhoque8fdd6412012-12-04 15:05:33 -0600259 }
260 else
261 {
akmhoque9ca1bb82013-01-24 09:57:57 -0600262 if ( j == last_face && is_active_neighbor(nle->name)==0)
akmhoque8fdd6412012-12-04 15:05:33 -0600263 {
264 if ( nlsr->debugging )
265 printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
266 if ( nlsr->detailed_logging )
267 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoqueaaa10552013-01-24 08:00:42 -0600268 //add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);
akmhoque8fdd6412012-12-04 15:05:33 -0600269 }
270 }
271 }
272
273
274 hashtb_next(enle);
275 }
276 hashtb_end(enle);
277
278
279
280 free(faces);
281 free(route_costs);
282
283 }
284 hashtb_end(e);
285
286}
287
288int
289delete_npt_entry_by_router_and_name_prefix(char *orig_router, char *name_prefix)
290{
291 if ( strcmp(orig_router,nlsr->router_name)== 0)
292 {
293 return -1;
294 }
295
296 struct npt_entry *ne;
297
298 int res,res_nle;
299 struct hashtb_enumerator ee;
300 struct hashtb_enumerator *e = &ee;
301
302
303 hashtb_start(nlsr->npt, e);
304 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
305
306 if(res == HT_NEW_ENTRY)
307 {
308 hashtb_delete(e);
309 return -1;
310 }
311 else if (res == HT_OLD_ENTRY)
312 {
313 ne=e->data;
314
315 struct hashtb_enumerator eenle;
316 struct hashtb_enumerator *enle = &eenle;
317
318 hashtb_start(ne->name_list, enle);
319 res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
320
321 if(res_nle == HT_NEW_ENTRY )
322 {
323 hashtb_delete(enle);
324 }
325 else if(res_nle == HT_OLD_ENTRY )
326 {
327 struct name_list_entry *nle;
328
329 nle=enle->data;
330
331 int j;
332 int num_face=hashtb_n(ne->face_list);
333 int last_face,first_face;
334
335 int *faces=(int *)malloc(num_face*sizeof(int));
336 int *route_costs=(int *)malloc(num_face*sizeof(int));
337
338 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
339 sort_faces_by_distance(faces,route_costs,0,num_face);
340
341
342 first_face=num_face-1;
343
344 if ( nlsr->multi_path_face_num == 0 )
345 {
346 last_face=first_face;
347 }
348 else
349 {
350 if ( num_face <= nlsr->multi_path_face_num)
351 {
352 last_face=0;
353 }
akmhoquedc27b952013-01-24 09:34:00 -0600354 else
akmhoque8fdd6412012-12-04 15:05:33 -0600355 {
356 last_face=num_face-nlsr->multi_path_face_num;
357 }
358 }
359
360 for( j=first_face; j>= last_face; j--)
361 {
362
akmhoque9ca1bb82013-01-24 09:57:57 -0600363 if ( is_active_neighbor(orig_router) == 0 )
akmhoque8fdd6412012-12-04 15:05:33 -0600364 {
365 if ( nlsr->debugging )
366 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
367 if ( nlsr->detailed_logging )
368 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoquedc27b952013-01-24 09:34:00 -0600369 //add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
akmhoque8fdd6412012-12-04 15:05:33 -0600370 }
371 else
372 {
akmhoque9ca1bb82013-01-24 09:57:57 -0600373 if ( j == last_face && is_active_neighbor(nle->name)==0)
akmhoque8fdd6412012-12-04 15:05:33 -0600374 {
375 if ( nlsr->debugging )
376 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
377 if ( nlsr->detailed_logging )
378 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoquedc27b952013-01-24 09:34:00 -0600379 //add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
akmhoque8fdd6412012-12-04 15:05:33 -0600380 }
381 }
382
383 }
384
akmhoque8fdd6412012-12-04 15:05:33 -0600385 }
386
387 hashtb_end(enle);
388 }
389
390 hashtb_end(e);
391
392 return 0;
393}
394
395void
396print_npt(void)
397{
398
399 if ( nlsr->debugging )
400 {
401 printf("\n");
402 printf("print_npt called\n");
403 }
404 if ( nlsr->detailed_logging )
405 {
406 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
407 writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_npt called\n");
408 }
409 int i, npt_element;
410
411 struct npt_entry *ne;
412
413 struct hashtb_enumerator ee;
414 struct hashtb_enumerator *e = &ee;
415
416 hashtb_start(nlsr->npt, e);
417 npt_element=hashtb_n(nlsr->npt);
418
419 for(i=0;i<npt_element;i++)
420 {
421 if ( nlsr->debugging )
422 {
423 printf("\n");
424 printf("----------NPT ENTRY %d------------------\n",i+1);
425 }
426 if ( nlsr->detailed_logging )
427 {
428 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
429 writeLogg(__FILE__,__FUNCTION__,__LINE__,"----------NPT ENTRY %d------------------\n",i+1);
430 }
431 ne=e->data;
432 if ( nlsr->debugging )
433 printf(" Origination Router: %s \n",ne->orig_router);
434 if ( nlsr->detailed_logging )
435 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s \n",ne->orig_router);
akmhoque8fdd6412012-12-04 15:05:33 -0600436
437 int j, nl_element,face_list_element;
438 struct name_list_entry *nle;
439 struct hashtb_enumerator eenle;
440 struct hashtb_enumerator *enle = &eenle;
441
442 hashtb_start(ne->name_list, enle);
443 nl_element=hashtb_n(ne->name_list);
444
445 for (j=0;j<nl_element;j++)
446 {
447 nle=enle->data;
448 if ( nlsr->debugging )
449 printf(" Name Prefix: %s \n",nle->name);
450 if ( nlsr->detailed_logging )
451 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix: %s \n",nle->name);
452 hashtb_next(enle);
453 }
454 hashtb_end(enle);
455
456 struct face_list_entry *fle;
457
458 struct hashtb_enumerator eef;
459 struct hashtb_enumerator *ef = &eef;
460
461 hashtb_start(ne->face_list, ef);
462 face_list_element=hashtb_n(ne->face_list);
463 if ( face_list_element <= 0 )
464 {
465 if ( nlsr->debugging )
466 printf(" Face: No Face \n");
467 if ( nlsr->detailed_logging )
468 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
469
470 }
471 else
472 {
473 for(j=0;j<face_list_element;j++)
474 {
475 fle=ef->data;
476 if ( nlsr->debugging )
477 printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
478 if ( nlsr->detailed_logging )
479 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
480 hashtb_next(ef);
481 }
482 }
483 hashtb_end(ef);
484
485
486 hashtb_next(e);
487 }
488
489 hashtb_end(e);
490
491 if ( nlsr->debugging )
492 printf("\n");
493 if ( nlsr->detailed_logging )
494 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
495}
496
497void
498delete_orig_router_from_npt(char *orig_router)
499{
500 int res,num_face,num_prefix;
501 struct npt_entry *ne;
502
503 struct hashtb_enumerator ee;
504 struct hashtb_enumerator *e = &ee;
505
506 hashtb_start(nlsr->npt, e);
507 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
508
509 if ( res == HT_OLD_ENTRY )
510 {
511 ne=e->data;
512 num_prefix=hashtb_n(ne->name_list);
513 if ( num_prefix > 0 )
514 {
515 num_face=hashtb_n(ne->face_list);
516
517 if ( num_face > 0 )
518 {
519 int j, nl_element;
520 struct name_list_entry *nle;
521 struct hashtb_enumerator eenle;
522 struct hashtb_enumerator *enle = &eenle;
523
524 hashtb_start(ne->name_list, enle);
525 nl_element=hashtb_n(ne->name_list);
526
527 for (j=0;j<nl_element;j++)
528 {
529 nle=enle->data;
530 delete_npt_entry_by_router_and_name_prefix(ne->orig_router,nle->name);
531 hashtb_next(enle);
532 }
533 hashtb_end(enle);
534
535 }
536
537 }
538 hashtb_destroy(&ne->name_list);
539 hashtb_destroy(&ne->face_list);
540 hashtb_delete(e);
541 }
542 else if ( res == HT_NEW_ENTRY )
543 {
544 hashtb_delete(e);
545 }
546 hashtb_end(e);
547}
548
549
550void
551add_face_to_npt_by_face_id(char *dest_router, int face_id, int route_cost)
552{
553 if ( nlsr->debugging )
554 {
555 printf("add_face_to_npt_by_face_id called\n");
556 printf("Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
557 }
558 if ( nlsr->detailed_logging )
559 {
560 writeLogg(__FILE__,__FUNCTION__,__LINE__,"add_face_to_npt_by_face_id called\n");
561 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
562 }
563
akmhoque613446f2013-01-23 10:40:12 -0600564 //print_routing_table();
akmhoque8fdd6412012-12-04 15:05:33 -0600565
566 int res,res1;
567 struct npt_entry *ne;
568
569 struct hashtb_enumerator ee;
570 struct hashtb_enumerator *e = &ee;
571
572 hashtb_start(nlsr->npt, e);
573 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
574
575 if ( res == HT_OLD_ENTRY )
576 {
577 if ( nlsr->debugging )
578 printf("Dest Router Found \n");
579 if ( nlsr->detailed_logging )
580 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest Router Found \n");
581
582 ne=e->data;
583
584 struct hashtb_enumerator eef;
585 struct hashtb_enumerator *ef = &eef;
586
587 hashtb_start(ne->face_list, ef);
588 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
589
590 if ( res1 == HT_OLD_ENTRY )
591 {
592 if ( nlsr->debugging )
593 printf("Face Found \n");
594 if ( nlsr->detailed_logging )
595 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face Found \n");
596 struct face_list_entry *fle;//=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
597 fle=ef->data;
598 fle->next_hop_face=face_id;
599 fle->route_cost=route_cost;
600 }
601 else if ( res1 == HT_NEW_ENTRY )
602 {
603 if ( nlsr->debugging )
604 printf("Face Not Found \n");
605 if ( nlsr->detailed_logging )
606 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face Not Found \n");
607 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
608 fle=ef->data;
609 fle->next_hop_face=face_id;
610 fle->route_cost=route_cost;
akmhoqueaaa10552013-01-24 08:00:42 -0600611
612
613
614
akmhoque8fdd6412012-12-04 15:05:33 -0600615 }
616 hashtb_end(ef);
617 }
618 else if (res == HT_NEW_ENTRY)
619 {
620 hashtb_delete(e);
621 }
622
623 hashtb_end(e);
624}
625
626
627void
628add_new_fib_entries_to_npt(void)
629{
630 if ( nlsr->debugging )
631 printf("add_new_fib_entries_to_npt called\n");
632 if ( nlsr->detailed_logging )
633 writeLogg(__FILE__,__FUNCTION__,__LINE__,"add_new_fib_entries_to_npt called\n");
634 int i,j, rt_element,face_list_element;
635
636 struct routing_table_entry *rte;
637
638 struct hashtb_enumerator ee;
639 struct hashtb_enumerator *e = &ee;
640
641 hashtb_start(nlsr->routing_table, e);
642 rt_element=hashtb_n(nlsr->routing_table);
643
644 for(i=0;i<rt_element;i++)
645 {
646 rte=e->data;
647
648 struct face_list_entry *fle;
649
650 struct hashtb_enumerator eef;
651 struct hashtb_enumerator *ef = &eef;
652
653 hashtb_start(rte->face_list, ef);
654 face_list_element=hashtb_n(rte->face_list);
655 if ( face_list_element <= 0 )
656 {
657 if ( nlsr->debugging )
akmhoquee19e3ee2013-01-22 14:38:45 -0600658 printf(" Face: No Face \n");
akmhoque8fdd6412012-12-04 15:05:33 -0600659 if ( nlsr->detailed_logging )
akmhoquee19e3ee2013-01-22 14:38:45 -0600660 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
akmhoque8fdd6412012-12-04 15:05:33 -0600661 }
662 else
663 {
664 for(j=0;j<face_list_element;j++)
665 {
666 fle=ef->data;
akmhoqueeda9c362013-01-23 08:54:29 -0600667 add_face_to_npt_by_face_id(rte->dest_router,fle->next_hop_face,fle->route_cost);
akmhoque8fdd6412012-12-04 15:05:33 -0600668 hashtb_next(ef);
669 }
670 }
671 hashtb_end(ef);
672
673 hashtb_next(e);
674 }
675
676 hashtb_end(e);
677
678}
679
680
681void
682delete_face_from_npt_by_face_id(char *dest_router, int face_id)
683{
684 if ( nlsr->debugging )
685 printf("delete_face_from_npt_by_face_id\n");
686 if ( nlsr->detailed_logging )
687 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_face_from_npt_by_face_id\n");
688
689 int res,res1;
690 struct npt_entry *ne;
691
692 struct hashtb_enumerator ee;
693 struct hashtb_enumerator *e = &ee;
694
695 hashtb_start(nlsr->npt, e);
696 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
697
698 if ( res == HT_OLD_ENTRY )
699 {
700 ne=e->data;
701
702 struct hashtb_enumerator eef;
703 struct hashtb_enumerator *ef = &eef;
704
705 hashtb_start(ne->face_list, ef);
706 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
akmhoqueaaa10552013-01-24 08:00:42 -0600707 /*
akmhoque8fdd6412012-12-04 15:05:33 -0600708 if ( res1 == HT_OLD_ENTRY )
709 {
710 hashtb_delete(ef);
711 }
712 else if ( res1 == HT_NEW_ENTRY )
713 {
714 hashtb_delete(ef);
715 }
akmhoqueaaa10552013-01-24 08:00:42 -0600716 */
717 hashtb_delete(ef);
akmhoque8fdd6412012-12-04 15:05:33 -0600718 hashtb_end(ef);
719 }
720 else if (res == HT_NEW_ENTRY)
721 {
722 hashtb_delete(e);
723 }
724
725 hashtb_end(e);
726}
727
728int
729delete_old_face_from_npt(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
730{
731 if(flags == CCN_SCHEDULE_CANCEL)
732 {
733 return -1;
734 }
akmhoqueeda9c362013-01-23 08:54:29 -0600735
736 print_npt();
737
akmhoque8fdd6412012-12-04 15:05:33 -0600738 nlsr_lock();
739
740 if ( nlsr->debugging )
741 printf("delete_old_face_from_npt\n");
742 if ( nlsr->detailed_logging )
743 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_old_face_from_npt\n");
744
745 if ( ev->evdata != NULL )
746 {
747 if ( nlsr->debugging )
748 printf("Event Data: %s \n",(char *)ev->evdata);
749 if ( nlsr->detailed_logging )
750 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Event Data: %s \n",(char *)ev->evdata);
751 char *sep="|";
752 char *rem;
753 char *orig_router;
754 char *faceid;
755 int face_id;
756
757 char *face_data=(char *)malloc(strlen((char *)ev->evdata)+1);
758 memset(face_data,0,strlen((char *)ev->evdata)+1);
759 memcpy(face_data+strlen(face_data),(char *)ev->evdata,strlen((char *)ev->evdata));
760
761 orig_router=strtok_r(face_data,sep,&rem);
762 faceid=strtok_r(NULL,sep,&rem);
763 face_id=atoi(faceid);
764
765 if ( nlsr->debugging )
766 printf("Orig Router: %s Face: %d \n",orig_router,face_id);
767 if ( nlsr->detailed_logging )
768 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig Router: %s Face: %d \n",orig_router,face_id);
769
770 delete_face_from_npt_by_face_id(orig_router,face_id);
771 }
772
773 nlsr_unlock();
774
775 return 0;
776}
777
778void
779clean_old_fib_entries_from_npt(void)
780{
781
782
783 if ( nlsr->debugging )
784 printf("clean_old_fib_entries_from_npt called\n\n");
785 if ( nlsr->detailed_logging )
786 writeLogg(__FILE__,__FUNCTION__,__LINE__,"clean_old_fib_entries_from_npt called\n\n");
787 int i, npt_element;
788
789 struct npt_entry *ne;
790
791 struct hashtb_enumerator ee;
792 struct hashtb_enumerator *e = &ee;
793
794 hashtb_start(nlsr->npt, e);
795 npt_element=hashtb_n(nlsr->npt);
796
797 for(i=0;i<npt_element;i++)
798 {
799 ne=e->data;
800
801 int j,k, nl_element,face_list_element;
802 struct face_list_entry *fle;
803
804 struct hashtb_enumerator eef;
805 struct hashtb_enumerator *ef = &eef;
806
807 hashtb_start(ne->face_list, ef);
808 face_list_element=hashtb_n(ne->face_list);
809 if ( face_list_element <= 0 )
810 {
811 if ( nlsr->debugging )
akmhoquee49af7a2013-01-22 14:58:37 -0600812 printf(" Face: No Face \n");
akmhoque8fdd6412012-12-04 15:05:33 -0600813 if ( nlsr->detailed_logging )
814 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
815
816 }
817 else
818 {
819 for(j=0;j<face_list_element;j++)
820 {
821 fle=ef->data;
822 int check=does_face_exist_for_router(ne->orig_router,fle->next_hop_face);
823 if ( check == 0 )
824 {
825 struct name_list_entry *nle;
826 struct hashtb_enumerator eenle;
827 struct hashtb_enumerator *enle = &eenle;
828
829 hashtb_start(ne->name_list, enle);
830 nl_element=hashtb_n(ne->name_list);
831
832 for (k=0;k<nl_element;k++)
833 {
834 nle=enle->data;
akmhoque9ca1bb82013-01-24 09:57:57 -0600835 if( is_active_neighbor(nle->name) == 0 )
akmhoque8fdd6412012-12-04 15:05:33 -0600836 {
837 if ( nlsr->debugging )
838 printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
839 if ( nlsr->detailed_logging )
840 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
akmhoquedc27b952013-01-24 09:34:00 -0600841 //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 -0600842 }
843
844
845 hashtb_next(enle);
846 }
847 hashtb_end(enle);
848
akmhoqueaaa10552013-01-24 08:00:42 -0600849 /*char faceid[20];
akmhoque8fdd6412012-12-04 15:05:33 -0600850 memset(faceid,0,20);
851 sprintf(faceid,"%d",fle->next_hop_face);
852 char *evdata=(char *)malloc(strlen(ne->orig_router)+strlen(faceid)+2);
853 memset(evdata,0,strlen(ne->orig_router)+strlen(faceid)+2);
854 memcpy(evdata+strlen(evdata),ne->orig_router,strlen(ne->orig_router));
855 memcpy(evdata+strlen(evdata),"|",1);
856 memcpy(evdata+strlen(evdata),faceid,strlen(faceid));
857
akmhoquee19e3ee2013-01-22 14:38:45 -0600858 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_old_face_from_npt, (void *)evdata, 0);
akmhoque8fdd6412012-12-04 15:05:33 -0600859
akmhoqueaaa10552013-01-24 08:00:42 -0600860 */
861 hashtb_delete(ef);
862 j++;
863
864 }
865 else
866 {
867 hashtb_next(ef);
868 }
akmhoque8fdd6412012-12-04 15:05:33 -0600869 }
870 }
871 hashtb_end(ef);
872
873
874 hashtb_next(e);
875 }
876
877 hashtb_end(e);
878
879}
880
881void
882update_npt_with_new_route(void)
883{
akmhoque580ccf32013-01-23 09:12:33 -0600884 if ( nlsr->debugging )
885 printf("update_npt_with_new_route called\n");
886
akmhoque8fdd6412012-12-04 15:05:33 -0600887 clean_old_fib_entries_from_npt();
akmhoqueaaa10552013-01-24 08:00:42 -0600888 print_npt();
akmhoque8fdd6412012-12-04 15:05:33 -0600889 add_new_fib_entries_to_npt();
akmhoqueaaa10552013-01-24 08:00:42 -0600890 print_npt();
891
akmhoque8fdd6412012-12-04 15:05:33 -0600892 int i, npt_element;
893
894 struct npt_entry *ne;
895
896 struct hashtb_enumerator ee;
897 struct hashtb_enumerator *e = &ee;
898
899 hashtb_start(nlsr->npt, e);
900 npt_element=hashtb_n(nlsr->npt);
901
902 for(i=0;i<npt_element;i++)
903 {
904
905 ne=e->data;
akmhoque613446f2013-01-23 10:40:12 -0600906 update_ccnd_fib_for_orig_router(ne->orig_router);
akmhoque8fdd6412012-12-04 15:05:33 -0600907 hashtb_next(e);
908 }
909
910 hashtb_end(e);
911}
912
913
914
915void
916sort_faces_by_distance(int *faces,int *route_costs,int start,int element)
917{
918 int i,j;
919 int temp_cost;
920 int temp_face;
921
922 for ( i=start ; i < element ; i ++)
923 {
924 for( j=i+1; j<element; j ++)
925 {
926 if (route_costs[j] < route_costs[i] )
927 {
928 temp_cost=route_costs[j];
929 route_costs[j]=route_costs[i];
930 route_costs[i]=temp_cost;
931
932 temp_face=faces[j];
933 faces[j]=faces[i];
934 faces[i]=temp_face;
935 }
936 }
937 }
938
939}
940
941void
942get_all_faces_for_orig_router_from_npt(char *orig_router, int *faces, int *route_costs, int num_faces)
943{
944
945 int res,face_list_element,j;
946 struct hashtb_enumerator ee;
947 struct hashtb_enumerator *e = &ee;
948
949
950 hashtb_start(nlsr->npt, e);
951 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
952
953 if(res == HT_NEW_ENTRY)
954 {
955 hashtb_delete(e);
956 }
957 else if ( res == HT_OLD_ENTRY )
958 {
959 struct npt_entry *ne;
960 ne=e->data;
961
962 struct face_list_entry *fle;
963
964 struct hashtb_enumerator eef;
965 struct hashtb_enumerator *ef = &eef;
966
967 hashtb_start(ne->face_list, ef);
968 face_list_element=hashtb_n(ne->face_list);
969 for(j=0;j<face_list_element;j++)
970 {
971 fle=ef->data;
972 faces[j]=fle->next_hop_face;
973 route_costs[j]=fle->route_cost;
974 hashtb_next(ef);
975 }
976 hashtb_end(ef);
977
978
979 }
980 hashtb_end(e);
981
982}
983
984void
985destroy_faces_by_orig_router(char *orig_router)
986{
987
988 int res;
989 struct hashtb_enumerator ee;
990 struct hashtb_enumerator *e = &ee;
991
992
993 hashtb_start(nlsr->npt, e);
994 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
995
996 if(res == HT_NEW_ENTRY)
997 {
998 hashtb_delete(e);
999 }
1000 else if ( res == HT_OLD_ENTRY )
1001 {
1002 struct npt_entry *ne;
1003 ne=e->data;
1004 int num_face=hashtb_n(ne->face_list);
1005 int last_face,first_face;
1006
1007 int *faces=(int *)malloc(num_face*sizeof(int));
1008 int *route_costs=(int *)malloc(num_face*sizeof(int));
1009
1010 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
1011 sort_faces_by_distance(faces,route_costs,0,num_face);
1012
1013
1014 first_face=num_face-1;
1015
1016 if ( nlsr->multi_path_face_num == 0 )
1017 {
1018 last_face=first_face;
1019 }
1020 else
1021 {
1022 if ( num_face <= nlsr->multi_path_face_num)
1023 {
1024 last_face=0;
1025 }
1026 else if ( nlsr->multi_path_face_num == 0)
1027 {
1028 last_face=num_face-nlsr->multi_path_face_num;
1029 }
1030 }
1031
1032 int i,j, nl_element;
1033 struct name_list_entry *nle;
1034 struct hashtb_enumerator eenle;
1035 struct hashtb_enumerator *enle = &eenle;
1036
1037 hashtb_start(ne->name_list, enle);
1038 nl_element=hashtb_n(ne->name_list);
1039
1040 for (i=0;i<nl_element;i++)
1041 {
1042 nle=enle->data;
1043
1044 for( j=first_face; j>= last_face; j--)
1045 {
akmhoque9ca1bb82013-01-24 09:57:57 -06001046 if ( is_active_neighbor(orig_router) == 0 )
akmhoque8fdd6412012-12-04 15:05:33 -06001047 {
1048 if ( nlsr->debugging )
1049 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1050 if ( nlsr->detailed_logging )
1051 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoquedc27b952013-01-24 09:34:00 -06001052 //add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
akmhoque8fdd6412012-12-04 15:05:33 -06001053 }
1054 else
1055 {
akmhoque9ca1bb82013-01-24 09:57:57 -06001056 if ( j == last_face && is_active_neighbor(nle->name)==0)
akmhoque8fdd6412012-12-04 15:05:33 -06001057 {
1058 if ( nlsr->debugging )
1059 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1060 if ( nlsr->detailed_logging )
1061 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoquedc27b952013-01-24 09:34:00 -06001062 //add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
akmhoque8fdd6412012-12-04 15:05:33 -06001063 }
1064 }
1065 }
1066
1067 hashtb_next(enle);
1068 }
1069 hashtb_end(enle);
1070
1071
1072
1073 free(faces);
1074 free(route_costs);
1075
1076 }
1077 hashtb_end(e);
1078
1079}
1080
1081void
1082destroy_all_face_by_nlsr(void)
1083{
1084 int i, npt_element;
1085
1086 struct npt_entry *ne;
1087
1088 struct hashtb_enumerator ee;
1089 struct hashtb_enumerator *e = &ee;
1090
1091 hashtb_start(nlsr->npt, e);
1092 npt_element=hashtb_n(nlsr->npt);
1093
1094 for(i=0;i<npt_element;i++)
1095 {
1096 ne=e->data;
1097 destroy_faces_by_orig_router(ne->orig_router);
1098 hashtb_next(e);
1099 }
1100
1101 hashtb_end(e);
1102
1103 if ( nlsr->debugging )
1104 printf("\n");
1105 if ( nlsr->detailed_logging )
1106 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
1107}