blob: d5ce2cc014862bf2e27e3fb370d6b10913bdc8a0 [file] [log] [blame]
akmhoque3560cb62012-09-09 10:52:30 -05001#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"
akmhoque810a5b52012-09-09 16:53:14 -050026#include "nlsr_route.h"
akmhoque3cced642012-09-24 16:20:20 -050027#include "nlsr_adl.h"
akmhoque3560cb62012-09-09 10:52:30 -050028
29int
akmhoquede61ba92012-09-20 22:19:12 -050030add_npt_entry(char *orig_router, char *name_prefix, int num_face, int *faces, int *route_costs)
akmhoque3560cb62012-09-09 10:52:30 -050031{
32 if ( strcmp(orig_router,nlsr->router_name)== 0)
33 {
34 return -1;
35 }
akmhoque3cced642012-09-24 16:20:20 -050036
akmhoque3560cb62012-09-09 10:52:30 -050037 struct npt_entry *ne=(struct npt_entry*)malloc(sizeof(struct npt_entry ));
38
akmhoquede61ba92012-09-20 22:19:12 -050039 int res,res_nle,res_fle;
akmhoque3560cb62012-09-09 10:52:30 -050040 struct hashtb_enumerator ee;
41 struct hashtb_enumerator *e = &ee;
akmhoque810a5b52012-09-09 16:53:14 -050042
akmhoque3560cb62012-09-09 10:52:30 -050043
44 hashtb_start(nlsr->npt, e);
akmhoque810a5b52012-09-09 16:53:14 -050045 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
akmhoque3560cb62012-09-09 10:52:30 -050046
47 if(res == HT_NEW_ENTRY)
48 {
49 ne=e->data;
50
akmhoque810a5b52012-09-09 16:53:14 -050051 ne->orig_router=(char *)malloc(strlen(orig_router)+1);
52 memset(ne->orig_router,0,strlen(orig_router)+1);
53 memcpy(ne->orig_router,orig_router,strlen(orig_router));
akmhoque3560cb62012-09-09 10:52:30 -050054
akmhoque810a5b52012-09-09 16:53:14 -050055
akmhoquede61ba92012-09-20 22:19:12 -050056
akmhoque3560cb62012-09-09 10:52:30 -050057
akmhoque810a5b52012-09-09 16:53:14 -050058 struct hashtb_param param_nle = {0};
59 ne->name_list= hashtb_create(sizeof(struct name_list_entry ), &param_nle);
akmhoque3560cb62012-09-09 10:52:30 -050060
akmhoque810a5b52012-09-09 16:53:14 -050061 struct hashtb_enumerator eenle;
62 struct hashtb_enumerator *enle = &eenle;
akmhoque3560cb62012-09-09 10:52:30 -050063
akmhoque810a5b52012-09-09 16:53:14 -050064 hashtb_start(ne->name_list, enle);
65 res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
akmhoque3560cb62012-09-09 10:52:30 -050066
akmhoque810a5b52012-09-09 16:53:14 -050067 if(res_nle == HT_NEW_ENTRY )
akmhoque3560cb62012-09-09 10:52:30 -050068 {
akmhoquede61ba92012-09-20 22:19:12 -050069 struct name_list_entry *nle=(struct name_list_entry *)malloc(sizeof(struct name_list_entry));
akmhoque810a5b52012-09-09 16:53:14 -050070 nle=enle->data;
71 nle->name=(char *)malloc(strlen(name_prefix)+1);
72 memset(nle->name,0,strlen(name_prefix)+1);
73 memcpy(nle->name,name_prefix,strlen(name_prefix));
akmhoque3560cb62012-09-09 10:52:30 -050074
akmhoque810a5b52012-09-09 16:53:14 -050075
akmhoque3560cb62012-09-09 10:52:30 -050076
77 }
akmhoque810a5b52012-09-09 16:53:14 -050078 hashtb_end(enle);
79
akmhoquede61ba92012-09-20 22:19:12 -050080 struct hashtb_param param_fle = {0};
81 ne->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
akmhoque3560cb62012-09-09 10:52:30 -050082
akmhoquede61ba92012-09-20 22:19:12 -050083 if ( num_face > 0 )
84 {
85 struct hashtb_enumerator eef;
86 struct hashtb_enumerator *ef = &eef;
87
88 hashtb_start(ne->face_list, ef);
89 int i;
90
akmhoque3cced642012-09-24 16:20:20 -050091 for ( i=0; i < num_face ; i++)
akmhoquede61ba92012-09-20 22:19:12 -050092 {
93 int face=faces[i];
94 res_fle = hashtb_seek(ef, &face, sizeof(face), 0);
95
96 if ( res_fle == HT_NEW_ENTRY )
97 {
98 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
99 fle=ef->data;
100 fle->next_hop_face=face;
101 fle->route_cost=route_costs[i];
102 }
103
104 }
105 hashtb_end(ef);
106 }
107
akmhoque3560cb62012-09-09 10:52:30 -0500108 }
109 else if (res == HT_OLD_ENTRY)
110 {
111 free(ne);
112 struct npt_entry *one;
113
114 one=e->data;
115
akmhoque810a5b52012-09-09 16:53:14 -0500116 struct hashtb_enumerator eenle;
117 struct hashtb_enumerator *enle = &eenle;
akmhoque3560cb62012-09-09 10:52:30 -0500118
akmhoque810a5b52012-09-09 16:53:14 -0500119 hashtb_start(one->name_list, enle);
120 res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
121
122 if(res_nle == HT_NEW_ENTRY )
akmhoque3560cb62012-09-09 10:52:30 -0500123 {
akmhoquede61ba92012-09-20 22:19:12 -0500124 struct name_list_entry *nle=(struct name_list_entry *)malloc(sizeof(struct name_list_entry));
akmhoque810a5b52012-09-09 16:53:14 -0500125 nle=enle->data;
126 nle->name=(char *)malloc(strlen(name_prefix)+1);
127 memset(nle->name,0,strlen(name_prefix)+1);
128 memcpy(nle->name,name_prefix,strlen(name_prefix));
akmhoque3560cb62012-09-09 10:52:30 -0500129 }
akmhoque810a5b52012-09-09 16:53:14 -0500130 else if(res_nle == HT_OLD_ENTRY )
akmhoque3560cb62012-09-09 10:52:30 -0500131 {
akmhoquede61ba92012-09-20 22:19:12 -0500132
akmhoque3560cb62012-09-09 10:52:30 -0500133 }
akmhoque810a5b52012-09-09 16:53:14 -0500134 hashtb_end(enle);
135
akmhoquede61ba92012-09-20 22:19:12 -0500136 if ( num_face > 0 )
137 {
138 struct hashtb_enumerator eef;
139 struct hashtb_enumerator *ef = &eef;
140
141 hashtb_start(one->face_list, ef);
142 int i;
143
144 for ( i=0; i< num_face ; i ++)
145 {
146 int face=faces[i];
147 res_fle = hashtb_seek(ef, &face, sizeof(face), 0);
148
149 if ( res_fle == HT_NEW_ENTRY )
150 {
151 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
152 fle=ef->data;
153 fle->next_hop_face=face;
154 fle->route_cost=route_costs[i];
155 }
156
157 }
158 hashtb_end(ef);
159 }
akmhoque810a5b52012-09-09 16:53:14 -0500160
akmhoque3560cb62012-09-09 10:52:30 -0500161
162 }
163 hashtb_end(e);
akmhoque3cced642012-09-24 16:20:20 -0500164
165 update_ccnd_fib_for_orig_router(orig_router);
166
akmhoque3560cb62012-09-09 10:52:30 -0500167 return res;
168}
169
akmhoque3cced642012-09-24 16:20:20 -0500170void
171update_ccnd_fib_for_orig_router(char *orig_router)
172{
173
174 int res;
175 struct hashtb_enumerator ee;
176 struct hashtb_enumerator *e = &ee;
177
178
179 hashtb_start(nlsr->npt, e);
180 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
181
182 if(res == HT_NEW_ENTRY)
183 {
184 hashtb_delete(e);
185 }
186 else if ( res == HT_OLD_ENTRY )
187 {
188 struct npt_entry *ne;
189 ne=e->data;
190 int num_face=hashtb_n(ne->face_list);
191 int last_face,first_face;
192
193 int *faces=(int *)malloc(num_face*sizeof(int));
194 int *route_costs=(int *)malloc(num_face*sizeof(int));
195
196 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
197 sort_faces_by_distance(faces,route_costs,0,num_face);
198
199
200 first_face=num_face-1;
201
202 if ( nlsr->multi_path_face_num == 0 )
203 {
204 last_face=first_face;
205 }
206 else
207 {
208 if ( num_face <= nlsr->multi_path_face_num)
209 {
210 last_face=0;
211 }
212 else if ( nlsr->multi_path_face_num == 0)
213 {
214 last_face=num_face-nlsr->multi_path_face_num;
215 }
216 }
217
218 int i,j, nl_element;
219 struct name_list_entry *nle;
220 struct hashtb_enumerator eenle;
221 struct hashtb_enumerator *enle = &eenle;
222
223 hashtb_start(ne->name_list, enle);
224 nl_element=hashtb_n(ne->name_list);
225
226 for (i=0;i<nl_element;i++)
227 {
228 nle=enle->data;
229
230 for( j=first_face; j>= last_face; j--)
231 {
232 if ( j == last_face )
233 {
234 if( is_neighbor(nle->name) == 0 )
235 {
236 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);
237 }
238 }
239 else
240 {
241 if ( num_face-nlsr->multi_path_face_num > 0 && is_neighbor(orig_router) == 0 )
242 {
243 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);
244 }
245 }
246 }
247
248 hashtb_next(enle);
249 }
250 hashtb_end(enle);
251
252
253
254 free(faces);
255 free(route_costs);
256
257 }
258 hashtb_end(e);
259
260}
261
akmhoqueffacaa82012-09-13 17:48:30 -0500262int
akmhoque3cced642012-09-24 16:20:20 -0500263delete_npt_entry_by_router_and_name_prefix(char *orig_router, char *name_prefix)
akmhoqueffacaa82012-09-13 17:48:30 -0500264{
265 if ( strcmp(orig_router,nlsr->router_name)== 0)
266 {
267 return -1;
268 }
269
270 struct npt_entry *ne;
271
272 int res,res_nle;
273 struct hashtb_enumerator ee;
274 struct hashtb_enumerator *e = &ee;
275
276
277 hashtb_start(nlsr->npt, e);
278 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
279
280 if(res == HT_NEW_ENTRY)
281 {
282 hashtb_delete(e);
283 return -1;
284 }
285 else if (res == HT_OLD_ENTRY)
286 {
287 ne=e->data;
288
289 struct hashtb_enumerator eenle;
290 struct hashtb_enumerator *enle = &eenle;
291
292 hashtb_start(ne->name_list, enle);
293 res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
294
295 if(res_nle == HT_NEW_ENTRY )
296 {
297 hashtb_delete(enle);
298 }
299 else if(res_nle == HT_OLD_ENTRY )
300 {
akmhoque3cced642012-09-24 16:20:20 -0500301 struct name_list_entry *nle;
302
303 nle=enle->data;
304
305 int j;
306 int num_face=hashtb_n(ne->face_list);
307 int last_face,first_face;
308
309 int *faces=(int *)malloc(num_face*sizeof(int));
310 int *route_costs=(int *)malloc(num_face*sizeof(int));
311
312 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
313 sort_faces_by_distance(faces,route_costs,0,num_face);
314
315
316 first_face=num_face-1;
317
318 if ( nlsr->multi_path_face_num == 0 )
akmhoqueffacaa82012-09-13 17:48:30 -0500319 {
akmhoque3cced642012-09-24 16:20:20 -0500320 last_face=first_face;
akmhoqueffacaa82012-09-13 17:48:30 -0500321 }
akmhoque3cced642012-09-24 16:20:20 -0500322 else
323 {
324 if ( num_face <= nlsr->multi_path_face_num)
325 {
326 last_face=0;
327 }
328 else if ( nlsr->multi_path_face_num == 0)
329 {
330 last_face=num_face-nlsr->multi_path_face_num;
331 }
332 }
333
334 for( j=first_face; j>= last_face; j--)
335 {
336 if ( j == last_face )
337 {
338 if( is_neighbor(nle->name) == 0 )
339 {
340 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
341 }
342 }
343 else
344 {
345 if ( num_face-nlsr->multi_path_face_num > 0 && is_neighbor(orig_router) == 0 )
346 {
347 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
348 }
349 }
350 }
351
352
353
354
akmhoqueffacaa82012-09-13 17:48:30 -0500355 hashtb_delete(enle);
356 }
357
358 hashtb_end(enle);
akmhoqueda5b6832012-09-13 22:33:55 -0500359
360 if ( hashtb_n(ne->name_list) == 0 )
361 {
362 hashtb_delete(e);
363 }
akmhoqueffacaa82012-09-13 17:48:30 -0500364 }
365
366 hashtb_end(e);
367
368 return 0;
369}
akmhoque3560cb62012-09-09 10:52:30 -0500370
371void
372print_npt(void)
373{
374 printf("\n");
375 printf("print_npt called\n\n");
376 int i, npt_element;
377
378 struct npt_entry *ne;
379
380 struct hashtb_enumerator ee;
381 struct hashtb_enumerator *e = &ee;
382
383 hashtb_start(nlsr->npt, e);
384 npt_element=hashtb_n(nlsr->npt);
385
386 for(i=0;i<npt_element;i++)
387 {
388 printf("\n");
389 printf("----------NPT ENTRY %d------------------\n",i+1);
390 ne=e->data;
akmhoque810a5b52012-09-09 16:53:14 -0500391 printf(" Origination Router: %s \n",ne->orig_router);
akmhoquede61ba92012-09-20 22:19:12 -0500392 //ne->next_hop_face == NO_FACE ? printf(" Next Hop Face: NO_NEXT_HOP \n") : printf(" Next Hop Face: %d \n", ne->next_hop_face);
akmhoque3560cb62012-09-09 10:52:30 -0500393
akmhoquede61ba92012-09-20 22:19:12 -0500394 int j, nl_element,face_list_element;
akmhoque810a5b52012-09-09 16:53:14 -0500395 struct name_list_entry *nle;
396 struct hashtb_enumerator eenle;
397 struct hashtb_enumerator *enle = &eenle;
akmhoque3560cb62012-09-09 10:52:30 -0500398
akmhoque810a5b52012-09-09 16:53:14 -0500399 hashtb_start(ne->name_list, enle);
400 nl_element=hashtb_n(ne->name_list);
akmhoque3560cb62012-09-09 10:52:30 -0500401
akmhoque810a5b52012-09-09 16:53:14 -0500402 for (j=0;j<nl_element;j++)
akmhoque3560cb62012-09-09 10:52:30 -0500403 {
akmhoque810a5b52012-09-09 16:53:14 -0500404 nle=enle->data;
405 printf(" Name Prefix: %s \n",nle->name);
406 hashtb_next(enle);
akmhoque3560cb62012-09-09 10:52:30 -0500407 }
akmhoque810a5b52012-09-09 16:53:14 -0500408 hashtb_end(enle);
akmhoquede61ba92012-09-20 22:19:12 -0500409
410 struct face_list_entry *fle;
411
412 struct hashtb_enumerator eef;
413 struct hashtb_enumerator *ef = &eef;
414
415 hashtb_start(ne->face_list, ef);
416 face_list_element=hashtb_n(ne->face_list);
417 if ( face_list_element <= 0 )
418 {
419 printf(" Face: No Face \n");
420 }
421 else
422 {
423 for(j=0;j<face_list_element;j++)
424 {
425 fle=ef->data;
426 printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
427 hashtb_next(ef);
428 }
429 }
430 hashtb_end(ef);
431
akmhoque3560cb62012-09-09 10:52:30 -0500432
433 hashtb_next(e);
434 }
435
436 hashtb_end(e);
437
438 printf("\n");
439}
akmhoque810a5b52012-09-09 16:53:14 -0500440
441void
akmhoquede61ba92012-09-20 22:19:12 -0500442delete_orig_router_from_npt(char *orig_router)
akmhoque810a5b52012-09-09 16:53:14 -0500443{
akmhoquede61ba92012-09-20 22:19:12 -0500444 int res,num_face,num_prefix;
akmhoque810a5b52012-09-09 16:53:14 -0500445 struct npt_entry *ne;
446
447 struct hashtb_enumerator ee;
448 struct hashtb_enumerator *e = &ee;
449
450 hashtb_start(nlsr->npt, e);
451 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
452
453 if ( res == HT_OLD_ENTRY )
454 {
455 ne=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500456 num_prefix=hashtb_n(ne->name_list);
457 if ( num_prefix > 0 )
akmhoque810a5b52012-09-09 16:53:14 -0500458 {
akmhoquede61ba92012-09-20 22:19:12 -0500459 num_face=hashtb_n(ne->face_list);
460
461 if ( num_face > 0 )
akmhoque810a5b52012-09-09 16:53:14 -0500462 {
463 int j, nl_element;
464 struct name_list_entry *nle;
465 struct hashtb_enumerator eenle;
466 struct hashtb_enumerator *enle = &eenle;
467
468 hashtb_start(ne->name_list, enle);
469 nl_element=hashtb_n(ne->name_list);
470
471 for (j=0;j<nl_element;j++)
472 {
473 nle=enle->data;
akmhoquede61ba92012-09-20 22:19:12 -0500474
akmhoque3cced642012-09-24 16:20:20 -0500475 delete_npt_entry_by_router_and_name_prefix(ne->orig_router,nle->name);
476
akmhoque810a5b52012-09-09 16:53:14 -0500477 hashtb_next(enle);
478 }
479 hashtb_end(enle);
480
481 }
482
483 }
484 hashtb_destroy(&ne->name_list);
akmhoquede61ba92012-09-20 22:19:12 -0500485 hashtb_destroy(&ne->face_list);
akmhoque810a5b52012-09-09 16:53:14 -0500486 hashtb_delete(e);
487 }
488 else if ( res == HT_NEW_ENTRY )
489 {
490 hashtb_delete(e);
491 }
492 hashtb_end(e);
493}
akmhoquefbfd0982012-09-09 20:59:03 -0500494
akmhoquede61ba92012-09-20 22:19:12 -0500495
496void
497add_face_to_npt_by_face_id(char *dest_router, int face_id, int route_cost)
498{
499 printf("add_face_to_npt_by_face_id called\n");
500
501 printf("Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
502 int res,res1;
503 struct npt_entry *ne;
504
505 struct hashtb_enumerator ee;
506 struct hashtb_enumerator *e = &ee;
507
508 hashtb_start(nlsr->npt, e);
509 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
510
511 if ( res == HT_OLD_ENTRY )
512 {
513 printf("Dest Router Found \n");
514 ne=e->data;
515
516 struct hashtb_enumerator eef;
517 struct hashtb_enumerator *ef = &eef;
518
519 hashtb_start(ne->face_list, ef);
520 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
521
522 if ( res1 == HT_OLD_ENTRY )
523 {
524 printf("Face Found \n");
525 struct face_list_entry *fle;//=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
526 fle=ef->data;
527 fle->next_hop_face=face_id;
528 fle->route_cost=route_cost;
529 }
530 else if ( res1 == HT_NEW_ENTRY )
531 {
532 printf("Face Not Found \n");
533 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
534 fle=ef->data;
535 fle->next_hop_face=face_id;
536 fle->route_cost=route_cost;
537 //hashtb_delete(ef);
538 }
539 hashtb_end(ef);
540 }
541 else if (res == HT_NEW_ENTRY)
542 {
543 hashtb_delete(e);
544 }
545
546 hashtb_end(e);
547}
548
549
550void
akmhoque3cced642012-09-24 16:20:20 -0500551add_new_fib_entries_to_npt(void)
akmhoquede61ba92012-09-20 22:19:12 -0500552{
553 printf("add_new_fib_entries_to_npt called\n");
554 int i,j, rt_element,face_list_element;
555
556 struct routing_table_entry *rte;
557
558 struct hashtb_enumerator ee;
559 struct hashtb_enumerator *e = &ee;
560
561 hashtb_start(nlsr->routing_table, e);
562 rt_element=hashtb_n(nlsr->routing_table);
563
564 for(i=0;i<rt_element;i++)
565 {
akmhoquede61ba92012-09-20 22:19:12 -0500566 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500567
568 struct face_list_entry *fle;
569
570 struct hashtb_enumerator eef;
571 struct hashtb_enumerator *ef = &eef;
572
573 hashtb_start(rte->face_list, ef);
574 face_list_element=hashtb_n(rte->face_list);
575 if ( face_list_element <= 0 )
576 {
577 printf(" Face: No Face \n");
578 }
579 else
580 {
581 for(j=0;j<face_list_element;j++)
582 {
583 fle=ef->data;
akmhoquede61ba92012-09-20 22:19:12 -0500584 add_face_to_npt_by_face_id(rte->dest_router,fle->next_hop_face,fle->route_cost);
585 hashtb_next(ef);
586 }
587 }
588 hashtb_end(ef);
589
590 hashtb_next(e);
591 }
592
593 hashtb_end(e);
594
595}
596
597
598void
599delete_face_from_npt_by_face_id(char *dest_router, int face_id)
600{
601 printf("delete_face_from_npt_by_face_id called\n");
602
603 int res,res1;
604 struct npt_entry *ne;
605
606 struct hashtb_enumerator ee;
607 struct hashtb_enumerator *e = &ee;
608
609 hashtb_start(nlsr->npt, e);
610 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
611
612 if ( res == HT_OLD_ENTRY )
613 {
614 ne=e->data;
615
616 struct hashtb_enumerator eef;
617 struct hashtb_enumerator *ef = &eef;
618
619 hashtb_start(ne->face_list, ef);
620 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
621
622 if ( res1 == HT_OLD_ENTRY )
623 {
624 hashtb_delete(ef);
625 }
626 else if ( res1 == HT_NEW_ENTRY )
627 {
628 hashtb_delete(ef);
629 }
630 hashtb_end(ef);
631 }
632 else if (res == HT_NEW_ENTRY)
633 {
634 hashtb_delete(e);
635 }
636
637 hashtb_end(e);
638}
639
640int
641delete_old_face_from_npt(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
642{
643 if(flags == CCN_SCHEDULE_CANCEL)
644 {
645 return -1;
646 }
647
648 nlsr_lock();
649
650 printf("delete_old_face_from_npt called\n");
651 if ( ev->evdata != NULL )
652 {
653 printf("Event Data: %s \n",(char *)ev->evdata);
654 char *sep="|";
655 char *rem;
656 char *orig_router;
657 char *faceid;
658 int face_id;
659
660 char *face_data=(char *)malloc(strlen((char *)ev->evdata)+1);
661 memset(face_data,0,strlen((char *)ev->evdata)+1);
662 memcpy(face_data+strlen(face_data),(char *)ev->evdata,strlen((char *)ev->evdata));
663
664 orig_router=strtok_r(face_data,sep,&rem);
665 faceid=strtok_r(NULL,sep,&rem);
666 face_id=atoi(faceid);
667 printf("Orig Router: %s Face: %d \n",orig_router,face_id);
668
669 delete_face_from_npt_by_face_id(orig_router,face_id);
670 }
671
672 nlsr_unlock();
673
674 return 0;
675}
676
677void
akmhoque3cced642012-09-24 16:20:20 -0500678clean_old_fib_entries_from_npt(void)
akmhoquede61ba92012-09-20 22:19:12 -0500679{
680
681 printf("clean_old_fib_entries_from_npt called\n\n");
682 int i, npt_element;
683
684 struct npt_entry *ne;
685
686 struct hashtb_enumerator ee;
687 struct hashtb_enumerator *e = &ee;
688
689 hashtb_start(nlsr->npt, e);
690 npt_element=hashtb_n(nlsr->npt);
691
692 for(i=0;i<npt_element;i++)
693 {
694 ne=e->data;
695
696 int j,k, nl_element,face_list_element;
697 struct face_list_entry *fle;
698
699 struct hashtb_enumerator eef;
700 struct hashtb_enumerator *ef = &eef;
701
702 hashtb_start(ne->face_list, ef);
703 face_list_element=hashtb_n(ne->face_list);
704 if ( face_list_element <= 0 )
705 {
706 printf(" Face: No Face \n");
707 }
708 else
709 {
710 for(j=0;j<face_list_element;j++)
711 {
712 fle=ef->data;
713 int check=does_face_exist_for_router(ne->orig_router,fle->next_hop_face);
714 if ( check == 0 )
715 {
716 struct name_list_entry *nle;
717 struct hashtb_enumerator eenle;
718 struct hashtb_enumerator *enle = &eenle;
719
720 hashtb_start(ne->name_list, enle);
721 nl_element=hashtb_n(ne->name_list);
722
723 for (k=0;k<nl_element;k++)
724 {
725 nle=enle->data;
726
akmhoque3cced642012-09-24 16:20:20 -0500727 //delete all the fib entries here
728 if( is_neighbor(nle->name) == 0 )
729 {
730 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, fle->next_hop_face);
731 }
732
akmhoquede61ba92012-09-20 22:19:12 -0500733
734 hashtb_next(enle);
735 }
736 hashtb_end(enle);
737
738 char faceid[20];
739 memset(faceid,0,20);
740 sprintf(faceid,"%d",fle->next_hop_face);
741 char *evdata=(char *)malloc(strlen(ne->orig_router)+strlen(faceid)+2);
742 memset(evdata,0,strlen(ne->orig_router)+strlen(faceid)+2);
743 memcpy(evdata+strlen(evdata),ne->orig_router,strlen(ne->orig_router));
744 memcpy(evdata+strlen(evdata),"|",1);
745 memcpy(evdata+strlen(evdata),faceid,strlen(faceid));
746
747 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_old_face_from_npt, (void *)evdata, 0);
748
749 }
750
751 hashtb_next(ef);
752 }
753 }
754 hashtb_end(ef);
755
756
757 hashtb_next(e);
758 }
759
760 hashtb_end(e);
761
762}
763
764void
akmhoque3cced642012-09-24 16:20:20 -0500765update_npt_with_new_route(void)
akmhoquede61ba92012-09-20 22:19:12 -0500766{
767 clean_old_fib_entries_from_npt();
768 add_new_fib_entries_to_npt();
akmhoque3cced642012-09-24 16:20:20 -0500769
770 int i, npt_element;
771
772 struct npt_entry *ne;
773
774 struct hashtb_enumerator ee;
775 struct hashtb_enumerator *e = &ee;
776
777 hashtb_start(nlsr->npt, e);
778 npt_element=hashtb_n(nlsr->npt);
779
780 for(i=0;i<npt_element;i++)
781 {
782
783 ne=e->data;
784 update_ccnd_fib_for_orig_router(ne->orig_router);
785 hashtb_next(e);
786 }
787
788 hashtb_end(e);
789}
790
791
792
793void
794sort_faces_by_distance(int *faces,int *route_costs,int start,int element)
795{
796 int i,j;
797 int temp_cost;
798 int temp_face;
799
800 for ( i=start ; i < element ; i ++)
801 {
802 for( j=i+1; j<element; j ++)
803 {
804 if (route_costs[j] < route_costs[i] )
805 {
806 temp_cost=route_costs[j];
807 route_costs[j]=route_costs[i];
808 route_costs[i]=temp_cost;
809
810 temp_face=faces[j];
811 faces[j]=faces[i];
812 faces[i]=temp_face;
813 }
814 }
815 }
816
817}
818
819void
820get_all_faces_for_orig_router_from_npt(char *orig_router, int *faces, int *route_costs, int num_faces)
821{
822
823 int res,face_list_element,j;
824 struct hashtb_enumerator ee;
825 struct hashtb_enumerator *e = &ee;
826
827
828 hashtb_start(nlsr->npt, e);
829 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
830
831 if(res == HT_NEW_ENTRY)
832 {
833 hashtb_delete(e);
834 }
835 else if ( res == HT_OLD_ENTRY )
836 {
837 struct npt_entry *ne;
838 ne=e->data;
839
840 struct face_list_entry *fle;
841
842 struct hashtb_enumerator eef;
843 struct hashtb_enumerator *ef = &eef;
844
845 hashtb_start(ne->face_list, ef);
846 face_list_element=hashtb_n(ne->face_list);
847 for(j=0;j<face_list_element;j++)
848 {
849 fle=ef->data;
850 faces[j]=fle->next_hop_face;
851 route_costs[j]=fle->route_cost;
852 hashtb_next(ef);
853 }
854 hashtb_end(ef);
855
856
857 }
858 hashtb_end(e);
859
860}
861
862void
863destroy_faces_by_orig_router(char *orig_router)
864{
865
866 int res;
867 struct hashtb_enumerator ee;
868 struct hashtb_enumerator *e = &ee;
869
870
871 hashtb_start(nlsr->npt, e);
872 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
873
874 if(res == HT_NEW_ENTRY)
875 {
876 hashtb_delete(e);
877 }
878 else if ( res == HT_OLD_ENTRY )
879 {
880 struct npt_entry *ne;
881 ne=e->data;
882 int num_face=hashtb_n(ne->face_list);
883 int last_face,first_face;
884
885 int *faces=(int *)malloc(num_face*sizeof(int));
886 int *route_costs=(int *)malloc(num_face*sizeof(int));
887
888 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
889 sort_faces_by_distance(faces,route_costs,0,num_face);
890
891
892 first_face=num_face-1;
893
894 if ( nlsr->multi_path_face_num == 0 )
895 {
896 last_face=first_face;
897 }
898 else
899 {
900 if ( num_face <= nlsr->multi_path_face_num)
901 {
902 last_face=0;
903 }
904 else if ( nlsr->multi_path_face_num == 0)
905 {
906 last_face=num_face-nlsr->multi_path_face_num;
907 }
908 }
909
910 int i,j, nl_element;
911 struct name_list_entry *nle;
912 struct hashtb_enumerator eenle;
913 struct hashtb_enumerator *enle = &eenle;
914
915 hashtb_start(ne->name_list, enle);
916 nl_element=hashtb_n(ne->name_list);
917
918 for (i=0;i<nl_element;i++)
919 {
920 nle=enle->data;
921
922 for( j=first_face; j>= last_face; j--)
923 {
924 if ( j == last_face )
925 {
926 if( is_neighbor(nle->name) == 0 )
927 {
928 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
929 }
930 }
931 else
932 {
933 if ( num_face-nlsr->multi_path_face_num > 0 && is_neighbor(orig_router) == 0 )
934 {
935 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
936 }
937 }
938 }
939
940 hashtb_next(enle);
941 }
942 hashtb_end(enle);
943
944
945
946 free(faces);
947 free(route_costs);
948
949 }
950 hashtb_end(e);
951
akmhoquede61ba92012-09-20 22:19:12 -0500952}
953
akmhoquefbfd0982012-09-09 20:59:03 -0500954void
955destroy_all_face_by_nlsr(void)
956{
957 int i, npt_element;
958
959 struct npt_entry *ne;
960
961 struct hashtb_enumerator ee;
962 struct hashtb_enumerator *e = &ee;
963
964 hashtb_start(nlsr->npt, e);
965 npt_element=hashtb_n(nlsr->npt);
966
967 for(i=0;i<npt_element;i++)
968 {
akmhoquefbfd0982012-09-09 20:59:03 -0500969 ne=e->data;
akmhoque3cced642012-09-24 16:20:20 -0500970 destroy_faces_by_orig_router(ne->orig_router);
akmhoquefbfd0982012-09-09 20:59:03 -0500971 hashtb_next(e);
972 }
973
974 hashtb_end(e);
975
976 printf("\n");
977}