blob: 5116b4d2f8f3f106f3129245259cd602c98968b5 [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 {
akmhoque0820a2e2012-09-24 20:23:01 -0500236 printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500237 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);
238 }
239 }
240 else
241 {
242 if ( num_face-nlsr->multi_path_face_num > 0 && is_neighbor(orig_router) == 0 )
243 {
akmhoque0820a2e2012-09-24 20:23:01 -0500244 printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500245 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);
246 }
247 }
248 }
249
250 hashtb_next(enle);
251 }
252 hashtb_end(enle);
253
254
255
256 free(faces);
257 free(route_costs);
258
259 }
260 hashtb_end(e);
261
262}
263
akmhoqueffacaa82012-09-13 17:48:30 -0500264int
akmhoque3cced642012-09-24 16:20:20 -0500265delete_npt_entry_by_router_and_name_prefix(char *orig_router, char *name_prefix)
akmhoqueffacaa82012-09-13 17:48:30 -0500266{
267 if ( strcmp(orig_router,nlsr->router_name)== 0)
268 {
269 return -1;
270 }
271
272 struct npt_entry *ne;
273
274 int res,res_nle;
275 struct hashtb_enumerator ee;
276 struct hashtb_enumerator *e = &ee;
277
278
279 hashtb_start(nlsr->npt, e);
280 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
281
282 if(res == HT_NEW_ENTRY)
283 {
284 hashtb_delete(e);
285 return -1;
286 }
287 else if (res == HT_OLD_ENTRY)
288 {
289 ne=e->data;
290
291 struct hashtb_enumerator eenle;
292 struct hashtb_enumerator *enle = &eenle;
293
294 hashtb_start(ne->name_list, enle);
295 res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
296
297 if(res_nle == HT_NEW_ENTRY )
298 {
299 hashtb_delete(enle);
300 }
301 else if(res_nle == HT_OLD_ENTRY )
302 {
akmhoque3cced642012-09-24 16:20:20 -0500303 struct name_list_entry *nle;
304
305 nle=enle->data;
306
307 int j;
308 int num_face=hashtb_n(ne->face_list);
309 int last_face,first_face;
310
311 int *faces=(int *)malloc(num_face*sizeof(int));
312 int *route_costs=(int *)malloc(num_face*sizeof(int));
313
314 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
315 sort_faces_by_distance(faces,route_costs,0,num_face);
316
317
318 first_face=num_face-1;
319
320 if ( nlsr->multi_path_face_num == 0 )
akmhoqueffacaa82012-09-13 17:48:30 -0500321 {
akmhoque3cced642012-09-24 16:20:20 -0500322 last_face=first_face;
akmhoqueffacaa82012-09-13 17:48:30 -0500323 }
akmhoque3cced642012-09-24 16:20:20 -0500324 else
325 {
326 if ( num_face <= nlsr->multi_path_face_num)
327 {
328 last_face=0;
329 }
330 else if ( nlsr->multi_path_face_num == 0)
331 {
332 last_face=num_face-nlsr->multi_path_face_num;
333 }
334 }
335
336 for( j=first_face; j>= last_face; j--)
337 {
338 if ( j == last_face )
339 {
340 if( is_neighbor(nle->name) == 0 )
341 {
akmhoque0820a2e2012-09-24 20:23:01 -0500342 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500343 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
344 }
345 }
346 else
347 {
348 if ( num_face-nlsr->multi_path_face_num > 0 && is_neighbor(orig_router) == 0 )
349 {
akmhoque0820a2e2012-09-24 20:23:01 -0500350 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500351 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
352 }
353 }
354 }
355
356
357
358
akmhoqueffacaa82012-09-13 17:48:30 -0500359 hashtb_delete(enle);
360 }
361
362 hashtb_end(enle);
akmhoqueda5b6832012-09-13 22:33:55 -0500363
364 if ( hashtb_n(ne->name_list) == 0 )
365 {
366 hashtb_delete(e);
367 }
akmhoqueffacaa82012-09-13 17:48:30 -0500368 }
369
370 hashtb_end(e);
371
372 return 0;
373}
akmhoque3560cb62012-09-09 10:52:30 -0500374
375void
376print_npt(void)
377{
378 printf("\n");
379 printf("print_npt called\n\n");
380 int i, npt_element;
381
382 struct npt_entry *ne;
383
384 struct hashtb_enumerator ee;
385 struct hashtb_enumerator *e = &ee;
386
387 hashtb_start(nlsr->npt, e);
388 npt_element=hashtb_n(nlsr->npt);
389
390 for(i=0;i<npt_element;i++)
391 {
392 printf("\n");
393 printf("----------NPT ENTRY %d------------------\n",i+1);
394 ne=e->data;
akmhoque810a5b52012-09-09 16:53:14 -0500395 printf(" Origination Router: %s \n",ne->orig_router);
akmhoquede61ba92012-09-20 22:19:12 -0500396 //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 -0500397
akmhoquede61ba92012-09-20 22:19:12 -0500398 int j, nl_element,face_list_element;
akmhoque810a5b52012-09-09 16:53:14 -0500399 struct name_list_entry *nle;
400 struct hashtb_enumerator eenle;
401 struct hashtb_enumerator *enle = &eenle;
akmhoque3560cb62012-09-09 10:52:30 -0500402
akmhoque810a5b52012-09-09 16:53:14 -0500403 hashtb_start(ne->name_list, enle);
404 nl_element=hashtb_n(ne->name_list);
akmhoque3560cb62012-09-09 10:52:30 -0500405
akmhoque810a5b52012-09-09 16:53:14 -0500406 for (j=0;j<nl_element;j++)
akmhoque3560cb62012-09-09 10:52:30 -0500407 {
akmhoque810a5b52012-09-09 16:53:14 -0500408 nle=enle->data;
409 printf(" Name Prefix: %s \n",nle->name);
410 hashtb_next(enle);
akmhoque3560cb62012-09-09 10:52:30 -0500411 }
akmhoque810a5b52012-09-09 16:53:14 -0500412 hashtb_end(enle);
akmhoquede61ba92012-09-20 22:19:12 -0500413
414 struct face_list_entry *fle;
415
416 struct hashtb_enumerator eef;
417 struct hashtb_enumerator *ef = &eef;
418
419 hashtb_start(ne->face_list, ef);
420 face_list_element=hashtb_n(ne->face_list);
421 if ( face_list_element <= 0 )
422 {
423 printf(" Face: No Face \n");
424 }
425 else
426 {
427 for(j=0;j<face_list_element;j++)
428 {
429 fle=ef->data;
430 printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
431 hashtb_next(ef);
432 }
433 }
434 hashtb_end(ef);
435
akmhoque3560cb62012-09-09 10:52:30 -0500436
437 hashtb_next(e);
438 }
439
440 hashtb_end(e);
441
442 printf("\n");
443}
akmhoque810a5b52012-09-09 16:53:14 -0500444
445void
akmhoquede61ba92012-09-20 22:19:12 -0500446delete_orig_router_from_npt(char *orig_router)
akmhoque810a5b52012-09-09 16:53:14 -0500447{
akmhoquede61ba92012-09-20 22:19:12 -0500448 int res,num_face,num_prefix;
akmhoque810a5b52012-09-09 16:53:14 -0500449 struct npt_entry *ne;
450
451 struct hashtb_enumerator ee;
452 struct hashtb_enumerator *e = &ee;
453
454 hashtb_start(nlsr->npt, e);
455 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
456
457 if ( res == HT_OLD_ENTRY )
458 {
459 ne=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500460 num_prefix=hashtb_n(ne->name_list);
461 if ( num_prefix > 0 )
akmhoque810a5b52012-09-09 16:53:14 -0500462 {
akmhoquede61ba92012-09-20 22:19:12 -0500463 num_face=hashtb_n(ne->face_list);
464
465 if ( num_face > 0 )
akmhoque810a5b52012-09-09 16:53:14 -0500466 {
467 int j, nl_element;
468 struct name_list_entry *nle;
469 struct hashtb_enumerator eenle;
470 struct hashtb_enumerator *enle = &eenle;
471
472 hashtb_start(ne->name_list, enle);
473 nl_element=hashtb_n(ne->name_list);
474
475 for (j=0;j<nl_element;j++)
476 {
477 nle=enle->data;
akmhoquede61ba92012-09-20 22:19:12 -0500478
akmhoque3cced642012-09-24 16:20:20 -0500479 delete_npt_entry_by_router_and_name_prefix(ne->orig_router,nle->name);
480
akmhoque810a5b52012-09-09 16:53:14 -0500481 hashtb_next(enle);
482 }
483 hashtb_end(enle);
484
485 }
486
487 }
488 hashtb_destroy(&ne->name_list);
akmhoquede61ba92012-09-20 22:19:12 -0500489 hashtb_destroy(&ne->face_list);
akmhoque810a5b52012-09-09 16:53:14 -0500490 hashtb_delete(e);
491 }
492 else if ( res == HT_NEW_ENTRY )
493 {
494 hashtb_delete(e);
495 }
496 hashtb_end(e);
497}
akmhoquefbfd0982012-09-09 20:59:03 -0500498
akmhoquede61ba92012-09-20 22:19:12 -0500499
500void
501add_face_to_npt_by_face_id(char *dest_router, int face_id, int route_cost)
502{
503 printf("add_face_to_npt_by_face_id called\n");
504
505 printf("Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
506 int res,res1;
507 struct npt_entry *ne;
508
509 struct hashtb_enumerator ee;
510 struct hashtb_enumerator *e = &ee;
511
512 hashtb_start(nlsr->npt, e);
513 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
514
515 if ( res == HT_OLD_ENTRY )
516 {
517 printf("Dest Router Found \n");
518 ne=e->data;
519
520 struct hashtb_enumerator eef;
521 struct hashtb_enumerator *ef = &eef;
522
523 hashtb_start(ne->face_list, ef);
524 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
525
526 if ( res1 == HT_OLD_ENTRY )
527 {
528 printf("Face Found \n");
529 struct face_list_entry *fle;//=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
530 fle=ef->data;
531 fle->next_hop_face=face_id;
532 fle->route_cost=route_cost;
533 }
534 else if ( res1 == HT_NEW_ENTRY )
535 {
536 printf("Face Not Found \n");
537 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
538 fle=ef->data;
539 fle->next_hop_face=face_id;
540 fle->route_cost=route_cost;
541 //hashtb_delete(ef);
542 }
543 hashtb_end(ef);
544 }
545 else if (res == HT_NEW_ENTRY)
546 {
547 hashtb_delete(e);
548 }
549
550 hashtb_end(e);
551}
552
553
554void
akmhoque3cced642012-09-24 16:20:20 -0500555add_new_fib_entries_to_npt(void)
akmhoquede61ba92012-09-20 22:19:12 -0500556{
557 printf("add_new_fib_entries_to_npt called\n");
558 int i,j, rt_element,face_list_element;
559
560 struct routing_table_entry *rte;
561
562 struct hashtb_enumerator ee;
563 struct hashtb_enumerator *e = &ee;
564
565 hashtb_start(nlsr->routing_table, e);
566 rt_element=hashtb_n(nlsr->routing_table);
567
568 for(i=0;i<rt_element;i++)
569 {
akmhoquede61ba92012-09-20 22:19:12 -0500570 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500571
572 struct face_list_entry *fle;
573
574 struct hashtb_enumerator eef;
575 struct hashtb_enumerator *ef = &eef;
576
577 hashtb_start(rte->face_list, ef);
578 face_list_element=hashtb_n(rte->face_list);
579 if ( face_list_element <= 0 )
580 {
581 printf(" Face: No Face \n");
582 }
583 else
584 {
585 for(j=0;j<face_list_element;j++)
586 {
587 fle=ef->data;
akmhoquede61ba92012-09-20 22:19:12 -0500588 add_face_to_npt_by_face_id(rte->dest_router,fle->next_hop_face,fle->route_cost);
589 hashtb_next(ef);
590 }
591 }
592 hashtb_end(ef);
593
594 hashtb_next(e);
595 }
596
597 hashtb_end(e);
598
599}
600
601
602void
603delete_face_from_npt_by_face_id(char *dest_router, int face_id)
604{
605 printf("delete_face_from_npt_by_face_id called\n");
606
607 int res,res1;
608 struct npt_entry *ne;
609
610 struct hashtb_enumerator ee;
611 struct hashtb_enumerator *e = &ee;
612
613 hashtb_start(nlsr->npt, e);
614 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
615
616 if ( res == HT_OLD_ENTRY )
617 {
618 ne=e->data;
619
620 struct hashtb_enumerator eef;
621 struct hashtb_enumerator *ef = &eef;
622
623 hashtb_start(ne->face_list, ef);
624 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
625
626 if ( res1 == HT_OLD_ENTRY )
627 {
628 hashtb_delete(ef);
629 }
630 else if ( res1 == HT_NEW_ENTRY )
631 {
632 hashtb_delete(ef);
633 }
634 hashtb_end(ef);
635 }
636 else if (res == HT_NEW_ENTRY)
637 {
638 hashtb_delete(e);
639 }
640
641 hashtb_end(e);
642}
643
644int
645delete_old_face_from_npt(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
646{
647 if(flags == CCN_SCHEDULE_CANCEL)
648 {
649 return -1;
650 }
651
652 nlsr_lock();
653
654 printf("delete_old_face_from_npt called\n");
655 if ( ev->evdata != NULL )
656 {
657 printf("Event Data: %s \n",(char *)ev->evdata);
658 char *sep="|";
659 char *rem;
660 char *orig_router;
661 char *faceid;
662 int face_id;
663
664 char *face_data=(char *)malloc(strlen((char *)ev->evdata)+1);
665 memset(face_data,0,strlen((char *)ev->evdata)+1);
666 memcpy(face_data+strlen(face_data),(char *)ev->evdata,strlen((char *)ev->evdata));
667
668 orig_router=strtok_r(face_data,sep,&rem);
669 faceid=strtok_r(NULL,sep,&rem);
670 face_id=atoi(faceid);
671 printf("Orig Router: %s Face: %d \n",orig_router,face_id);
672
673 delete_face_from_npt_by_face_id(orig_router,face_id);
674 }
675
676 nlsr_unlock();
677
678 return 0;
679}
680
681void
akmhoque3cced642012-09-24 16:20:20 -0500682clean_old_fib_entries_from_npt(void)
akmhoquede61ba92012-09-20 22:19:12 -0500683{
684
685 printf("clean_old_fib_entries_from_npt called\n\n");
686 int i, npt_element;
687
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 npt_element=hashtb_n(nlsr->npt);
695
696 for(i=0;i<npt_element;i++)
697 {
698 ne=e->data;
699
700 int j,k, nl_element,face_list_element;
701 struct face_list_entry *fle;
702
703 struct hashtb_enumerator eef;
704 struct hashtb_enumerator *ef = &eef;
705
706 hashtb_start(ne->face_list, ef);
707 face_list_element=hashtb_n(ne->face_list);
708 if ( face_list_element <= 0 )
709 {
710 printf(" Face: No Face \n");
711 }
712 else
713 {
714 for(j=0;j<face_list_element;j++)
715 {
716 fle=ef->data;
717 int check=does_face_exist_for_router(ne->orig_router,fle->next_hop_face);
718 if ( check == 0 )
719 {
720 struct name_list_entry *nle;
721 struct hashtb_enumerator eenle;
722 struct hashtb_enumerator *enle = &eenle;
723
724 hashtb_start(ne->name_list, enle);
725 nl_element=hashtb_n(ne->name_list);
726
727 for (k=0;k<nl_element;k++)
728 {
729 nle=enle->data;
730
akmhoque3cced642012-09-24 16:20:20 -0500731 //delete all the fib entries here
732 if( is_neighbor(nle->name) == 0 )
733 {
akmhoque0820a2e2012-09-24 20:23:01 -0500734 printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
akmhoque3cced642012-09-24 16:20:20 -0500735 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, fle->next_hop_face);
736 }
737
akmhoquede61ba92012-09-20 22:19:12 -0500738
739 hashtb_next(enle);
740 }
741 hashtb_end(enle);
742
743 char faceid[20];
744 memset(faceid,0,20);
745 sprintf(faceid,"%d",fle->next_hop_face);
746 char *evdata=(char *)malloc(strlen(ne->orig_router)+strlen(faceid)+2);
747 memset(evdata,0,strlen(ne->orig_router)+strlen(faceid)+2);
748 memcpy(evdata+strlen(evdata),ne->orig_router,strlen(ne->orig_router));
749 memcpy(evdata+strlen(evdata),"|",1);
750 memcpy(evdata+strlen(evdata),faceid,strlen(faceid));
751
752 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_old_face_from_npt, (void *)evdata, 0);
753
754 }
755
756 hashtb_next(ef);
757 }
758 }
759 hashtb_end(ef);
760
761
762 hashtb_next(e);
763 }
764
765 hashtb_end(e);
766
767}
768
769void
akmhoque3cced642012-09-24 16:20:20 -0500770update_npt_with_new_route(void)
akmhoquede61ba92012-09-20 22:19:12 -0500771{
772 clean_old_fib_entries_from_npt();
773 add_new_fib_entries_to_npt();
akmhoque3cced642012-09-24 16:20:20 -0500774
775 int i, npt_element;
776
777 struct npt_entry *ne;
778
779 struct hashtb_enumerator ee;
780 struct hashtb_enumerator *e = &ee;
781
782 hashtb_start(nlsr->npt, e);
783 npt_element=hashtb_n(nlsr->npt);
784
785 for(i=0;i<npt_element;i++)
786 {
787
788 ne=e->data;
789 update_ccnd_fib_for_orig_router(ne->orig_router);
790 hashtb_next(e);
791 }
792
793 hashtb_end(e);
794}
795
796
797
798void
799sort_faces_by_distance(int *faces,int *route_costs,int start,int element)
800{
801 int i,j;
802 int temp_cost;
803 int temp_face;
804
805 for ( i=start ; i < element ; i ++)
806 {
807 for( j=i+1; j<element; j ++)
808 {
809 if (route_costs[j] < route_costs[i] )
810 {
811 temp_cost=route_costs[j];
812 route_costs[j]=route_costs[i];
813 route_costs[i]=temp_cost;
814
815 temp_face=faces[j];
816 faces[j]=faces[i];
817 faces[i]=temp_face;
818 }
819 }
820 }
821
822}
823
824void
825get_all_faces_for_orig_router_from_npt(char *orig_router, int *faces, int *route_costs, int num_faces)
826{
827
828 int res,face_list_element,j;
829 struct hashtb_enumerator ee;
830 struct hashtb_enumerator *e = &ee;
831
832
833 hashtb_start(nlsr->npt, e);
834 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
835
836 if(res == HT_NEW_ENTRY)
837 {
838 hashtb_delete(e);
839 }
840 else if ( res == HT_OLD_ENTRY )
841 {
842 struct npt_entry *ne;
843 ne=e->data;
844
845 struct face_list_entry *fle;
846
847 struct hashtb_enumerator eef;
848 struct hashtb_enumerator *ef = &eef;
849
850 hashtb_start(ne->face_list, ef);
851 face_list_element=hashtb_n(ne->face_list);
852 for(j=0;j<face_list_element;j++)
853 {
854 fle=ef->data;
855 faces[j]=fle->next_hop_face;
856 route_costs[j]=fle->route_cost;
857 hashtb_next(ef);
858 }
859 hashtb_end(ef);
860
861
862 }
863 hashtb_end(e);
864
865}
866
867void
868destroy_faces_by_orig_router(char *orig_router)
869{
870
871 int res;
872 struct hashtb_enumerator ee;
873 struct hashtb_enumerator *e = &ee;
874
875
876 hashtb_start(nlsr->npt, e);
877 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
878
879 if(res == HT_NEW_ENTRY)
880 {
881 hashtb_delete(e);
882 }
883 else if ( res == HT_OLD_ENTRY )
884 {
885 struct npt_entry *ne;
886 ne=e->data;
887 int num_face=hashtb_n(ne->face_list);
888 int last_face,first_face;
889
890 int *faces=(int *)malloc(num_face*sizeof(int));
891 int *route_costs=(int *)malloc(num_face*sizeof(int));
892
893 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
894 sort_faces_by_distance(faces,route_costs,0,num_face);
895
896
897 first_face=num_face-1;
898
899 if ( nlsr->multi_path_face_num == 0 )
900 {
901 last_face=first_face;
902 }
903 else
904 {
905 if ( num_face <= nlsr->multi_path_face_num)
906 {
907 last_face=0;
908 }
909 else if ( nlsr->multi_path_face_num == 0)
910 {
911 last_face=num_face-nlsr->multi_path_face_num;
912 }
913 }
914
915 int i,j, nl_element;
916 struct name_list_entry *nle;
917 struct hashtb_enumerator eenle;
918 struct hashtb_enumerator *enle = &eenle;
919
920 hashtb_start(ne->name_list, enle);
921 nl_element=hashtb_n(ne->name_list);
922
923 for (i=0;i<nl_element;i++)
924 {
925 nle=enle->data;
926
927 for( j=first_face; j>= last_face; j--)
928 {
929 if ( j == last_face )
930 {
931 if( is_neighbor(nle->name) == 0 )
932 {
akmhoque0820a2e2012-09-24 20:23:01 -0500933 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500934 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
935 }
936 }
937 else
938 {
939 if ( num_face-nlsr->multi_path_face_num > 0 && is_neighbor(orig_router) == 0 )
940 {
akmhoque0820a2e2012-09-24 20:23:01 -0500941 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500942 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
943 }
944 }
945 }
946
947 hashtb_next(enle);
948 }
949 hashtb_end(enle);
950
951
952
953 free(faces);
954 free(route_costs);
955
956 }
957 hashtb_end(e);
958
akmhoquede61ba92012-09-20 22:19:12 -0500959}
960
akmhoquefbfd0982012-09-09 20:59:03 -0500961void
962destroy_all_face_by_nlsr(void)
963{
964 int i, npt_element;
965
966 struct npt_entry *ne;
967
968 struct hashtb_enumerator ee;
969 struct hashtb_enumerator *e = &ee;
970
971 hashtb_start(nlsr->npt, e);
972 npt_element=hashtb_n(nlsr->npt);
973
974 for(i=0;i<npt_element;i++)
975 {
akmhoquefbfd0982012-09-09 20:59:03 -0500976 ne=e->data;
akmhoque3cced642012-09-24 16:20:20 -0500977 destroy_faces_by_orig_router(ne->orig_router);
akmhoquefbfd0982012-09-09 20:59:03 -0500978 hashtb_next(e);
979 }
980
981 hashtb_end(e);
982
983 printf("\n");
984}