blob: 0a02ed4aa2ffce332f671f30441cef40fcbde7a5 [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
akmhoquef9b35b32013-01-24 10:25:48 -0600210 /*int m;
akmhoqueaaa10552013-01-24 08:00:42 -0600211 for ( m =0 ; m< num_face ; m++)
212 {
213 printf("Face: %d Cost: %d \n",faces[m],route_costs[m]);
akmhoquef9b35b32013-01-24 10:25:48 -0600214 }*/
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
akmhoquef9b35b32013-01-24 10:25:48 -0600250 //printf("FIB Entry Name: %s Face: %d Router Cost: %d \n",nle->name,faces[j],route_costs[j]);
akmhoquedc27b952013-01-24 09:34:00 -0600251
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]);
akmhoquef9b35b32013-01-24 10:25:48 -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]);
akmhoquef9b35b32013-01-24 10:25:48 -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]);
akmhoquef9b35b32013-01-24 10:25:48 -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]);
akmhoquef9b35b32013-01-24 10:25:48 -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;
akmhoquef9b35b32013-01-24 10:25:48 -0600835 if( is_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 )
akmhoquef9b35b32013-01-24 10:25:48 -0600840 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
841 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
akmhoquef9b35b32013-01-24 10:25:48 -0600849
akmhoqueaaa10552013-01-24 08:00:42 -0600850 hashtb_delete(ef);
851 j++;
852
853 }
854 else
855 {
856 hashtb_next(ef);
857 }
akmhoque8fdd6412012-12-04 15:05:33 -0600858 }
859 }
860 hashtb_end(ef);
861
862
863 hashtb_next(e);
864 }
865
866 hashtb_end(e);
867
868}
869
870void
871update_npt_with_new_route(void)
872{
akmhoque580ccf32013-01-23 09:12:33 -0600873 if ( nlsr->debugging )
874 printf("update_npt_with_new_route called\n");
875
akmhoque8fdd6412012-12-04 15:05:33 -0600876 clean_old_fib_entries_from_npt();
akmhoqueaaa10552013-01-24 08:00:42 -0600877 print_npt();
akmhoque8fdd6412012-12-04 15:05:33 -0600878 add_new_fib_entries_to_npt();
akmhoqueaaa10552013-01-24 08:00:42 -0600879 print_npt();
880
akmhoque8fdd6412012-12-04 15:05:33 -0600881 int i, npt_element;
882
883 struct npt_entry *ne;
884
885 struct hashtb_enumerator ee;
886 struct hashtb_enumerator *e = &ee;
887
888 hashtb_start(nlsr->npt, e);
889 npt_element=hashtb_n(nlsr->npt);
890
891 for(i=0;i<npt_element;i++)
892 {
893
894 ne=e->data;
akmhoque613446f2013-01-23 10:40:12 -0600895 update_ccnd_fib_for_orig_router(ne->orig_router);
akmhoque8fdd6412012-12-04 15:05:33 -0600896 hashtb_next(e);
897 }
898
899 hashtb_end(e);
900}
901
902
903
904void
905sort_faces_by_distance(int *faces,int *route_costs,int start,int element)
906{
907 int i,j;
908 int temp_cost;
909 int temp_face;
910
911 for ( i=start ; i < element ; i ++)
912 {
913 for( j=i+1; j<element; j ++)
914 {
915 if (route_costs[j] < route_costs[i] )
916 {
917 temp_cost=route_costs[j];
918 route_costs[j]=route_costs[i];
919 route_costs[i]=temp_cost;
920
921 temp_face=faces[j];
922 faces[j]=faces[i];
923 faces[i]=temp_face;
924 }
925 }
926 }
927
928}
929
930void
931get_all_faces_for_orig_router_from_npt(char *orig_router, int *faces, int *route_costs, int num_faces)
932{
933
934 int res,face_list_element,j;
935 struct hashtb_enumerator ee;
936 struct hashtb_enumerator *e = &ee;
937
938
939 hashtb_start(nlsr->npt, e);
940 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
941
942 if(res == HT_NEW_ENTRY)
943 {
944 hashtb_delete(e);
945 }
946 else if ( res == HT_OLD_ENTRY )
947 {
948 struct npt_entry *ne;
949 ne=e->data;
950
951 struct face_list_entry *fle;
952
953 struct hashtb_enumerator eef;
954 struct hashtb_enumerator *ef = &eef;
955
956 hashtb_start(ne->face_list, ef);
957 face_list_element=hashtb_n(ne->face_list);
958 for(j=0;j<face_list_element;j++)
959 {
960 fle=ef->data;
961 faces[j]=fle->next_hop_face;
962 route_costs[j]=fle->route_cost;
963 hashtb_next(ef);
964 }
965 hashtb_end(ef);
966
967
968 }
969 hashtb_end(e);
970
971}
972
973void
974destroy_faces_by_orig_router(char *orig_router)
975{
976
977 int res;
978 struct hashtb_enumerator ee;
979 struct hashtb_enumerator *e = &ee;
980
981
982 hashtb_start(nlsr->npt, e);
983 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
984
985 if(res == HT_NEW_ENTRY)
986 {
987 hashtb_delete(e);
988 }
989 else if ( res == HT_OLD_ENTRY )
990 {
991 struct npt_entry *ne;
992 ne=e->data;
993 int num_face=hashtb_n(ne->face_list);
994 int last_face,first_face;
995
996 int *faces=(int *)malloc(num_face*sizeof(int));
997 int *route_costs=(int *)malloc(num_face*sizeof(int));
998
999 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
1000 sort_faces_by_distance(faces,route_costs,0,num_face);
1001
1002
1003 first_face=num_face-1;
1004
1005 if ( nlsr->multi_path_face_num == 0 )
1006 {
1007 last_face=first_face;
1008 }
1009 else
1010 {
1011 if ( num_face <= nlsr->multi_path_face_num)
1012 {
1013 last_face=0;
1014 }
1015 else if ( nlsr->multi_path_face_num == 0)
1016 {
1017 last_face=num_face-nlsr->multi_path_face_num;
1018 }
1019 }
1020
1021 int i,j, nl_element;
1022 struct name_list_entry *nle;
1023 struct hashtb_enumerator eenle;
1024 struct hashtb_enumerator *enle = &eenle;
1025
1026 hashtb_start(ne->name_list, enle);
1027 nl_element=hashtb_n(ne->name_list);
1028
1029 for (i=0;i<nl_element;i++)
1030 {
1031 nle=enle->data;
1032
1033 for( j=first_face; j>= last_face; j--)
1034 {
akmhoque9ca1bb82013-01-24 09:57:57 -06001035 if ( is_active_neighbor(orig_router) == 0 )
akmhoque8fdd6412012-12-04 15:05:33 -06001036 {
1037 if ( nlsr->debugging )
1038 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1039 if ( nlsr->detailed_logging )
1040 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoquef9b35b32013-01-24 10:25:48 -06001041 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
akmhoque8fdd6412012-12-04 15:05:33 -06001042 }
1043 else
1044 {
akmhoque9ca1bb82013-01-24 09:57:57 -06001045 if ( j == last_face && is_active_neighbor(nle->name)==0)
akmhoque8fdd6412012-12-04 15:05:33 -06001046 {
1047 if ( nlsr->debugging )
1048 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1049 if ( nlsr->detailed_logging )
1050 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoquef9b35b32013-01-24 10:25:48 -06001051 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
akmhoque8fdd6412012-12-04 15:05:33 -06001052 }
1053 }
1054 }
1055
1056 hashtb_next(enle);
1057 }
1058 hashtb_end(enle);
1059
1060
1061
1062 free(faces);
1063 free(route_costs);
1064
1065 }
1066 hashtb_end(e);
1067
1068}
1069
1070void
1071destroy_all_face_by_nlsr(void)
1072{
1073 int i, npt_element;
1074
1075 struct npt_entry *ne;
1076
1077 struct hashtb_enumerator ee;
1078 struct hashtb_enumerator *e = &ee;
1079
1080 hashtb_start(nlsr->npt, e);
1081 npt_element=hashtb_n(nlsr->npt);
1082
1083 for(i=0;i<npt_element;i++)
1084 {
1085 ne=e->data;
1086 destroy_faces_by_orig_router(ne->orig_router);
1087 hashtb_next(e);
1088 }
1089
1090 hashtb_end(e);
1091
1092 if ( nlsr->debugging )
1093 printf("\n");
1094 if ( nlsr->detailed_logging )
1095 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
1096}