blob: 25c3c38708f578f70db9f3fe475fd6111bab1899 [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];
akmhoquea30cb772012-10-07 09:50:34 -050094 if ( face != NO_FACE && face != ZERO_FACE)
akmhoquede61ba92012-09-20 22:19:12 -050095 {
akmhoquea30cb772012-10-07 09:50:34 -050096 res_fle = hashtb_seek(ef, &face, sizeof(face), 0);
97
98 if ( res_fle == HT_NEW_ENTRY )
99 {
100 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
101 fle=ef->data;
102 fle->next_hop_face=face;
103 fle->route_cost=route_costs[i];
104 }
akmhoquede61ba92012-09-20 22:19:12 -0500105 }
106
107 }
108 hashtb_end(ef);
109 }
110
akmhoque3560cb62012-09-09 10:52:30 -0500111 }
112 else if (res == HT_OLD_ENTRY)
113 {
114 free(ne);
115 struct npt_entry *one;
116
117 one=e->data;
118
akmhoque810a5b52012-09-09 16:53:14 -0500119 struct hashtb_enumerator eenle;
120 struct hashtb_enumerator *enle = &eenle;
akmhoque3560cb62012-09-09 10:52:30 -0500121
akmhoque810a5b52012-09-09 16:53:14 -0500122 hashtb_start(one->name_list, enle);
123 res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
124
125 if(res_nle == HT_NEW_ENTRY )
akmhoque3560cb62012-09-09 10:52:30 -0500126 {
akmhoquede61ba92012-09-20 22:19:12 -0500127 struct name_list_entry *nle=(struct name_list_entry *)malloc(sizeof(struct name_list_entry));
akmhoque810a5b52012-09-09 16:53:14 -0500128 nle=enle->data;
129 nle->name=(char *)malloc(strlen(name_prefix)+1);
130 memset(nle->name,0,strlen(name_prefix)+1);
131 memcpy(nle->name,name_prefix,strlen(name_prefix));
akmhoque3560cb62012-09-09 10:52:30 -0500132 }
akmhoque810a5b52012-09-09 16:53:14 -0500133 else if(res_nle == HT_OLD_ENTRY )
akmhoque3560cb62012-09-09 10:52:30 -0500134 {
akmhoquede61ba92012-09-20 22:19:12 -0500135
akmhoque3560cb62012-09-09 10:52:30 -0500136 }
akmhoque810a5b52012-09-09 16:53:14 -0500137 hashtb_end(enle);
138
akmhoquede61ba92012-09-20 22:19:12 -0500139 if ( num_face > 0 )
140 {
141 struct hashtb_enumerator eef;
142 struct hashtb_enumerator *ef = &eef;
143
144 hashtb_start(one->face_list, ef);
145 int i;
146
147 for ( i=0; i< num_face ; i ++)
148 {
149 int face=faces[i];
akmhoquea30cb772012-10-07 09:50:34 -0500150 if ( face != NO_FACE && face != ZERO_FACE)
akmhoquede61ba92012-09-20 22:19:12 -0500151 {
akmhoquea30cb772012-10-07 09:50:34 -0500152 res_fle = hashtb_seek(ef, &face, sizeof(face), 0);
153
154 if ( res_fle == HT_NEW_ENTRY )
155 {
156 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
157 fle=ef->data;
158 fle->next_hop_face=face;
159 fle->route_cost=route_costs[i];
160 }
akmhoquede61ba92012-09-20 22:19:12 -0500161 }
akmhoquede61ba92012-09-20 22:19:12 -0500162 }
163 hashtb_end(ef);
164 }
akmhoque810a5b52012-09-09 16:53:14 -0500165
akmhoque3560cb62012-09-09 10:52:30 -0500166
167 }
168 hashtb_end(e);
akmhoque3cced642012-09-24 16:20:20 -0500169
170 update_ccnd_fib_for_orig_router(orig_router);
171
akmhoque3560cb62012-09-09 10:52:30 -0500172 return res;
173}
174
akmhoque3cced642012-09-24 16:20:20 -0500175void
176update_ccnd_fib_for_orig_router(char *orig_router)
177{
178
179 int res;
180 struct hashtb_enumerator ee;
181 struct hashtb_enumerator *e = &ee;
182
183
184 hashtb_start(nlsr->npt, e);
185 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
186
187 if(res == HT_NEW_ENTRY)
188 {
189 hashtb_delete(e);
190 }
191 else if ( res == HT_OLD_ENTRY )
192 {
193 struct npt_entry *ne;
194 ne=e->data;
195 int num_face=hashtb_n(ne->face_list);
196 int last_face,first_face;
197
198 int *faces=(int *)malloc(num_face*sizeof(int));
199 int *route_costs=(int *)malloc(num_face*sizeof(int));
200
201 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
202 sort_faces_by_distance(faces,route_costs,0,num_face);
203
204
205 first_face=num_face-1;
206
207 if ( nlsr->multi_path_face_num == 0 )
208 {
209 last_face=first_face;
210 }
211 else
212 {
213 if ( num_face <= nlsr->multi_path_face_num)
214 {
215 last_face=0;
216 }
217 else if ( nlsr->multi_path_face_num == 0)
218 {
219 last_face=num_face-nlsr->multi_path_face_num;
220 }
221 }
222
223 int i,j, nl_element;
224 struct name_list_entry *nle;
225 struct hashtb_enumerator eenle;
226 struct hashtb_enumerator *enle = &eenle;
227
228 hashtb_start(ne->name_list, enle);
229 nl_element=hashtb_n(ne->name_list);
230
231 for (i=0;i<nl_element;i++)
232 {
233 nle=enle->data;
234
235 for( j=first_face; j>= last_face; j--)
236 {
akmhoquea30cb772012-10-07 09:50:34 -0500237 //printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
238 //printf("Orig Router: %s \n",orig_router);
239 //printf("Name Prefix: %s \n",nle->name);
240 //printf("Is neighbor Orig Router: %d \n",is_neighbor(orig_router));
241 //printf("Is neighbor Name Prefix: %d \n",is_neighbor(nle->name));
242
243 if ( is_neighbor(orig_router) == 0 )
akmhoque3cced642012-09-24 16:20:20 -0500244 {
akmhoquea30cb772012-10-07 09:50:34 -0500245 printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
246 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500247 }
248 else
249 {
akmhoquea30cb772012-10-07 09:50:34 -0500250 if ( j == last_face && is_neighbor(nle->name)==0)
akmhoque3cced642012-09-24 16:20:20 -0500251 {
akmhoque0820a2e2012-09-24 20:23:01 -0500252 printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500253 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);
254 }
255 }
256 }
akmhoquea30cb772012-10-07 09:50:34 -0500257
258
akmhoque3cced642012-09-24 16:20:20 -0500259 hashtb_next(enle);
260 }
261 hashtb_end(enle);
262
263
264
265 free(faces);
266 free(route_costs);
267
268 }
269 hashtb_end(e);
270
271}
272
akmhoqueffacaa82012-09-13 17:48:30 -0500273int
akmhoque3cced642012-09-24 16:20:20 -0500274delete_npt_entry_by_router_and_name_prefix(char *orig_router, char *name_prefix)
akmhoqueffacaa82012-09-13 17:48:30 -0500275{
276 if ( strcmp(orig_router,nlsr->router_name)== 0)
277 {
278 return -1;
279 }
280
281 struct npt_entry *ne;
282
283 int res,res_nle;
284 struct hashtb_enumerator ee;
285 struct hashtb_enumerator *e = &ee;
286
287
288 hashtb_start(nlsr->npt, e);
289 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
290
291 if(res == HT_NEW_ENTRY)
292 {
293 hashtb_delete(e);
294 return -1;
295 }
296 else if (res == HT_OLD_ENTRY)
297 {
298 ne=e->data;
299
300 struct hashtb_enumerator eenle;
301 struct hashtb_enumerator *enle = &eenle;
302
303 hashtb_start(ne->name_list, enle);
304 res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
305
306 if(res_nle == HT_NEW_ENTRY )
307 {
308 hashtb_delete(enle);
309 }
310 else if(res_nle == HT_OLD_ENTRY )
311 {
akmhoque3cced642012-09-24 16:20:20 -0500312 struct name_list_entry *nle;
313
314 nle=enle->data;
315
316 int j;
317 int num_face=hashtb_n(ne->face_list);
318 int last_face,first_face;
319
320 int *faces=(int *)malloc(num_face*sizeof(int));
321 int *route_costs=(int *)malloc(num_face*sizeof(int));
322
323 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
324 sort_faces_by_distance(faces,route_costs,0,num_face);
325
326
327 first_face=num_face-1;
328
329 if ( nlsr->multi_path_face_num == 0 )
akmhoqueffacaa82012-09-13 17:48:30 -0500330 {
akmhoque3cced642012-09-24 16:20:20 -0500331 last_face=first_face;
akmhoqueffacaa82012-09-13 17:48:30 -0500332 }
akmhoque3cced642012-09-24 16:20:20 -0500333 else
334 {
335 if ( num_face <= nlsr->multi_path_face_num)
336 {
337 last_face=0;
338 }
339 else if ( nlsr->multi_path_face_num == 0)
340 {
341 last_face=num_face-nlsr->multi_path_face_num;
342 }
343 }
344
345 for( j=first_face; j>= last_face; j--)
346 {
akmhoquea30cb772012-10-07 09:50:34 -0500347
348 if ( is_neighbor(orig_router) == 0 )
akmhoque3cced642012-09-24 16:20:20 -0500349 {
akmhoquea30cb772012-10-07 09:50:34 -0500350 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
351 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500352 }
353 else
354 {
akmhoquea30cb772012-10-07 09:50:34 -0500355 if ( j == last_face && is_neighbor(nle->name)==0)
akmhoque3cced642012-09-24 16:20:20 -0500356 {
akmhoque0820a2e2012-09-24 20:23:01 -0500357 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500358 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
359 }
360 }
akmhoquea30cb772012-10-07 09:50:34 -0500361
akmhoque3cced642012-09-24 16:20:20 -0500362 }
363
364
365
366
akmhoqueffacaa82012-09-13 17:48:30 -0500367 hashtb_delete(enle);
368 }
369
370 hashtb_end(enle);
akmhoqueda5b6832012-09-13 22:33:55 -0500371
372 if ( hashtb_n(ne->name_list) == 0 )
373 {
374 hashtb_delete(e);
375 }
akmhoqueffacaa82012-09-13 17:48:30 -0500376 }
377
378 hashtb_end(e);
379
380 return 0;
381}
akmhoque3560cb62012-09-09 10:52:30 -0500382
383void
384print_npt(void)
385{
386 printf("\n");
387 printf("print_npt called\n\n");
388 int i, npt_element;
389
390 struct npt_entry *ne;
391
392 struct hashtb_enumerator ee;
393 struct hashtb_enumerator *e = &ee;
394
395 hashtb_start(nlsr->npt, e);
396 npt_element=hashtb_n(nlsr->npt);
397
398 for(i=0;i<npt_element;i++)
399 {
400 printf("\n");
401 printf("----------NPT ENTRY %d------------------\n",i+1);
402 ne=e->data;
akmhoque810a5b52012-09-09 16:53:14 -0500403 printf(" Origination Router: %s \n",ne->orig_router);
akmhoquede61ba92012-09-20 22:19:12 -0500404 //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 -0500405
akmhoquede61ba92012-09-20 22:19:12 -0500406 int j, nl_element,face_list_element;
akmhoque810a5b52012-09-09 16:53:14 -0500407 struct name_list_entry *nle;
408 struct hashtb_enumerator eenle;
409 struct hashtb_enumerator *enle = &eenle;
akmhoque3560cb62012-09-09 10:52:30 -0500410
akmhoque810a5b52012-09-09 16:53:14 -0500411 hashtb_start(ne->name_list, enle);
412 nl_element=hashtb_n(ne->name_list);
akmhoque3560cb62012-09-09 10:52:30 -0500413
akmhoque810a5b52012-09-09 16:53:14 -0500414 for (j=0;j<nl_element;j++)
akmhoque3560cb62012-09-09 10:52:30 -0500415 {
akmhoque810a5b52012-09-09 16:53:14 -0500416 nle=enle->data;
417 printf(" Name Prefix: %s \n",nle->name);
418 hashtb_next(enle);
akmhoque3560cb62012-09-09 10:52:30 -0500419 }
akmhoque810a5b52012-09-09 16:53:14 -0500420 hashtb_end(enle);
akmhoquede61ba92012-09-20 22:19:12 -0500421
422 struct face_list_entry *fle;
423
424 struct hashtb_enumerator eef;
425 struct hashtb_enumerator *ef = &eef;
426
427 hashtb_start(ne->face_list, ef);
428 face_list_element=hashtb_n(ne->face_list);
429 if ( face_list_element <= 0 )
430 {
431 printf(" Face: No Face \n");
432 }
433 else
434 {
435 for(j=0;j<face_list_element;j++)
436 {
437 fle=ef->data;
438 printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
439 hashtb_next(ef);
440 }
441 }
442 hashtb_end(ef);
443
akmhoque3560cb62012-09-09 10:52:30 -0500444
445 hashtb_next(e);
446 }
447
448 hashtb_end(e);
449
450 printf("\n");
451}
akmhoque810a5b52012-09-09 16:53:14 -0500452
453void
akmhoquede61ba92012-09-20 22:19:12 -0500454delete_orig_router_from_npt(char *orig_router)
akmhoque810a5b52012-09-09 16:53:14 -0500455{
akmhoquede61ba92012-09-20 22:19:12 -0500456 int res,num_face,num_prefix;
akmhoque810a5b52012-09-09 16:53:14 -0500457 struct npt_entry *ne;
458
459 struct hashtb_enumerator ee;
460 struct hashtb_enumerator *e = &ee;
461
462 hashtb_start(nlsr->npt, e);
463 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
464
465 if ( res == HT_OLD_ENTRY )
466 {
467 ne=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500468 num_prefix=hashtb_n(ne->name_list);
469 if ( num_prefix > 0 )
akmhoque810a5b52012-09-09 16:53:14 -0500470 {
akmhoquede61ba92012-09-20 22:19:12 -0500471 num_face=hashtb_n(ne->face_list);
472
473 if ( num_face > 0 )
akmhoque810a5b52012-09-09 16:53:14 -0500474 {
475 int j, nl_element;
476 struct name_list_entry *nle;
477 struct hashtb_enumerator eenle;
478 struct hashtb_enumerator *enle = &eenle;
479
480 hashtb_start(ne->name_list, enle);
481 nl_element=hashtb_n(ne->name_list);
482
483 for (j=0;j<nl_element;j++)
484 {
485 nle=enle->data;
akmhoquede61ba92012-09-20 22:19:12 -0500486
akmhoque3cced642012-09-24 16:20:20 -0500487 delete_npt_entry_by_router_and_name_prefix(ne->orig_router,nle->name);
488
akmhoque810a5b52012-09-09 16:53:14 -0500489 hashtb_next(enle);
490 }
491 hashtb_end(enle);
492
493 }
494
495 }
496 hashtb_destroy(&ne->name_list);
akmhoquede61ba92012-09-20 22:19:12 -0500497 hashtb_destroy(&ne->face_list);
akmhoque810a5b52012-09-09 16:53:14 -0500498 hashtb_delete(e);
499 }
500 else if ( res == HT_NEW_ENTRY )
501 {
502 hashtb_delete(e);
503 }
504 hashtb_end(e);
505}
akmhoquefbfd0982012-09-09 20:59:03 -0500506
akmhoquede61ba92012-09-20 22:19:12 -0500507
508void
509add_face_to_npt_by_face_id(char *dest_router, int face_id, int route_cost)
510{
511 printf("add_face_to_npt_by_face_id called\n");
512
513 printf("Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
514 int res,res1;
515 struct npt_entry *ne;
516
517 struct hashtb_enumerator ee;
518 struct hashtb_enumerator *e = &ee;
519
520 hashtb_start(nlsr->npt, e);
521 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
522
523 if ( res == HT_OLD_ENTRY )
524 {
525 printf("Dest Router Found \n");
526 ne=e->data;
527
528 struct hashtb_enumerator eef;
529 struct hashtb_enumerator *ef = &eef;
530
531 hashtb_start(ne->face_list, ef);
532 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
533
534 if ( res1 == HT_OLD_ENTRY )
535 {
536 printf("Face 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 }
542 else if ( res1 == HT_NEW_ENTRY )
543 {
544 printf("Face Not Found \n");
545 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
546 fle=ef->data;
547 fle->next_hop_face=face_id;
548 fle->route_cost=route_cost;
549 //hashtb_delete(ef);
550 }
551 hashtb_end(ef);
552 }
553 else if (res == HT_NEW_ENTRY)
554 {
555 hashtb_delete(e);
556 }
557
558 hashtb_end(e);
559}
560
561
562void
akmhoque3cced642012-09-24 16:20:20 -0500563add_new_fib_entries_to_npt(void)
akmhoquede61ba92012-09-20 22:19:12 -0500564{
565 printf("add_new_fib_entries_to_npt called\n");
566 int i,j, rt_element,face_list_element;
567
568 struct routing_table_entry *rte;
569
570 struct hashtb_enumerator ee;
571 struct hashtb_enumerator *e = &ee;
572
573 hashtb_start(nlsr->routing_table, e);
574 rt_element=hashtb_n(nlsr->routing_table);
575
576 for(i=0;i<rt_element;i++)
577 {
akmhoquede61ba92012-09-20 22:19:12 -0500578 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500579
580 struct face_list_entry *fle;
581
582 struct hashtb_enumerator eef;
583 struct hashtb_enumerator *ef = &eef;
584
585 hashtb_start(rte->face_list, ef);
586 face_list_element=hashtb_n(rte->face_list);
587 if ( face_list_element <= 0 )
588 {
589 printf(" Face: No Face \n");
590 }
591 else
592 {
593 for(j=0;j<face_list_element;j++)
594 {
595 fle=ef->data;
akmhoquede61ba92012-09-20 22:19:12 -0500596 add_face_to_npt_by_face_id(rte->dest_router,fle->next_hop_face,fle->route_cost);
597 hashtb_next(ef);
598 }
599 }
600 hashtb_end(ef);
601
602 hashtb_next(e);
603 }
604
605 hashtb_end(e);
606
607}
608
609
610void
611delete_face_from_npt_by_face_id(char *dest_router, int face_id)
612{
613 printf("delete_face_from_npt_by_face_id called\n");
614
615 int res,res1;
616 struct npt_entry *ne;
617
618 struct hashtb_enumerator ee;
619 struct hashtb_enumerator *e = &ee;
620
621 hashtb_start(nlsr->npt, e);
622 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
623
624 if ( res == HT_OLD_ENTRY )
625 {
626 ne=e->data;
627
628 struct hashtb_enumerator eef;
629 struct hashtb_enumerator *ef = &eef;
630
631 hashtb_start(ne->face_list, ef);
632 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
633
634 if ( res1 == HT_OLD_ENTRY )
635 {
636 hashtb_delete(ef);
637 }
638 else if ( res1 == HT_NEW_ENTRY )
639 {
640 hashtb_delete(ef);
641 }
642 hashtb_end(ef);
643 }
644 else if (res == HT_NEW_ENTRY)
645 {
646 hashtb_delete(e);
647 }
648
649 hashtb_end(e);
650}
651
652int
653delete_old_face_from_npt(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
654{
655 if(flags == CCN_SCHEDULE_CANCEL)
656 {
657 return -1;
658 }
659
660 nlsr_lock();
661
662 printf("delete_old_face_from_npt called\n");
663 if ( ev->evdata != NULL )
664 {
665 printf("Event Data: %s \n",(char *)ev->evdata);
666 char *sep="|";
667 char *rem;
668 char *orig_router;
669 char *faceid;
670 int face_id;
671
672 char *face_data=(char *)malloc(strlen((char *)ev->evdata)+1);
673 memset(face_data,0,strlen((char *)ev->evdata)+1);
674 memcpy(face_data+strlen(face_data),(char *)ev->evdata,strlen((char *)ev->evdata));
675
676 orig_router=strtok_r(face_data,sep,&rem);
677 faceid=strtok_r(NULL,sep,&rem);
678 face_id=atoi(faceid);
679 printf("Orig Router: %s Face: %d \n",orig_router,face_id);
680
681 delete_face_from_npt_by_face_id(orig_router,face_id);
682 }
683
684 nlsr_unlock();
685
686 return 0;
687}
688
689void
akmhoque3cced642012-09-24 16:20:20 -0500690clean_old_fib_entries_from_npt(void)
akmhoquede61ba92012-09-20 22:19:12 -0500691{
692
693 printf("clean_old_fib_entries_from_npt called\n\n");
694 int i, npt_element;
695
696 struct npt_entry *ne;
697
698 struct hashtb_enumerator ee;
699 struct hashtb_enumerator *e = &ee;
700
701 hashtb_start(nlsr->npt, e);
702 npt_element=hashtb_n(nlsr->npt);
703
704 for(i=0;i<npt_element;i++)
705 {
706 ne=e->data;
707
708 int j,k, nl_element,face_list_element;
709 struct face_list_entry *fle;
710
711 struct hashtb_enumerator eef;
712 struct hashtb_enumerator *ef = &eef;
713
714 hashtb_start(ne->face_list, ef);
715 face_list_element=hashtb_n(ne->face_list);
716 if ( face_list_element <= 0 )
717 {
718 printf(" Face: No Face \n");
719 }
720 else
721 {
722 for(j=0;j<face_list_element;j++)
723 {
724 fle=ef->data;
725 int check=does_face_exist_for_router(ne->orig_router,fle->next_hop_face);
726 if ( check == 0 )
727 {
728 struct name_list_entry *nle;
729 struct hashtb_enumerator eenle;
730 struct hashtb_enumerator *enle = &eenle;
731
732 hashtb_start(ne->name_list, enle);
733 nl_element=hashtb_n(ne->name_list);
734
735 for (k=0;k<nl_element;k++)
736 {
737 nle=enle->data;
738
akmhoque3cced642012-09-24 16:20:20 -0500739 //delete all the fib entries here
akmhoquea30cb772012-10-07 09:50:34 -0500740 printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
741
742
akmhoque3cced642012-09-24 16:20:20 -0500743 if( is_neighbor(nle->name) == 0 )
744 {
akmhoque0820a2e2012-09-24 20:23:01 -0500745 printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
akmhoque3cced642012-09-24 16:20:20 -0500746 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, fle->next_hop_face);
747 }
748
akmhoquede61ba92012-09-20 22:19:12 -0500749
750 hashtb_next(enle);
751 }
752 hashtb_end(enle);
753
754 char faceid[20];
755 memset(faceid,0,20);
756 sprintf(faceid,"%d",fle->next_hop_face);
757 char *evdata=(char *)malloc(strlen(ne->orig_router)+strlen(faceid)+2);
758 memset(evdata,0,strlen(ne->orig_router)+strlen(faceid)+2);
759 memcpy(evdata+strlen(evdata),ne->orig_router,strlen(ne->orig_router));
760 memcpy(evdata+strlen(evdata),"|",1);
761 memcpy(evdata+strlen(evdata),faceid,strlen(faceid));
762
763 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_old_face_from_npt, (void *)evdata, 0);
764
765 }
766
767 hashtb_next(ef);
768 }
769 }
770 hashtb_end(ef);
771
772
773 hashtb_next(e);
774 }
775
776 hashtb_end(e);
777
778}
779
780void
akmhoque3cced642012-09-24 16:20:20 -0500781update_npt_with_new_route(void)
akmhoquede61ba92012-09-20 22:19:12 -0500782{
783 clean_old_fib_entries_from_npt();
784 add_new_fib_entries_to_npt();
akmhoque3cced642012-09-24 16:20:20 -0500785
786 int i, npt_element;
787
788 struct npt_entry *ne;
789
790 struct hashtb_enumerator ee;
791 struct hashtb_enumerator *e = &ee;
792
793 hashtb_start(nlsr->npt, e);
794 npt_element=hashtb_n(nlsr->npt);
795
796 for(i=0;i<npt_element;i++)
797 {
798
799 ne=e->data;
800 update_ccnd_fib_for_orig_router(ne->orig_router);
801 hashtb_next(e);
802 }
803
804 hashtb_end(e);
805}
806
807
808
809void
810sort_faces_by_distance(int *faces,int *route_costs,int start,int element)
811{
812 int i,j;
813 int temp_cost;
814 int temp_face;
815
816 for ( i=start ; i < element ; i ++)
817 {
818 for( j=i+1; j<element; j ++)
819 {
820 if (route_costs[j] < route_costs[i] )
821 {
822 temp_cost=route_costs[j];
823 route_costs[j]=route_costs[i];
824 route_costs[i]=temp_cost;
825
826 temp_face=faces[j];
827 faces[j]=faces[i];
828 faces[i]=temp_face;
829 }
830 }
831 }
832
833}
834
835void
836get_all_faces_for_orig_router_from_npt(char *orig_router, int *faces, int *route_costs, int num_faces)
837{
838
839 int res,face_list_element,j;
840 struct hashtb_enumerator ee;
841 struct hashtb_enumerator *e = &ee;
842
843
844 hashtb_start(nlsr->npt, e);
845 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
846
847 if(res == HT_NEW_ENTRY)
848 {
849 hashtb_delete(e);
850 }
851 else if ( res == HT_OLD_ENTRY )
852 {
853 struct npt_entry *ne;
854 ne=e->data;
855
856 struct face_list_entry *fle;
857
858 struct hashtb_enumerator eef;
859 struct hashtb_enumerator *ef = &eef;
860
861 hashtb_start(ne->face_list, ef);
862 face_list_element=hashtb_n(ne->face_list);
863 for(j=0;j<face_list_element;j++)
864 {
865 fle=ef->data;
866 faces[j]=fle->next_hop_face;
867 route_costs[j]=fle->route_cost;
868 hashtb_next(ef);
869 }
870 hashtb_end(ef);
871
872
873 }
874 hashtb_end(e);
875
876}
877
878void
879destroy_faces_by_orig_router(char *orig_router)
880{
881
882 int res;
883 struct hashtb_enumerator ee;
884 struct hashtb_enumerator *e = &ee;
885
886
887 hashtb_start(nlsr->npt, e);
888 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
889
890 if(res == HT_NEW_ENTRY)
891 {
892 hashtb_delete(e);
893 }
894 else if ( res == HT_OLD_ENTRY )
895 {
896 struct npt_entry *ne;
897 ne=e->data;
898 int num_face=hashtb_n(ne->face_list);
899 int last_face,first_face;
900
901 int *faces=(int *)malloc(num_face*sizeof(int));
902 int *route_costs=(int *)malloc(num_face*sizeof(int));
903
904 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
905 sort_faces_by_distance(faces,route_costs,0,num_face);
906
907
908 first_face=num_face-1;
909
910 if ( nlsr->multi_path_face_num == 0 )
911 {
912 last_face=first_face;
913 }
914 else
915 {
916 if ( num_face <= nlsr->multi_path_face_num)
917 {
918 last_face=0;
919 }
920 else if ( nlsr->multi_path_face_num == 0)
921 {
922 last_face=num_face-nlsr->multi_path_face_num;
923 }
924 }
925
926 int i,j, nl_element;
927 struct name_list_entry *nle;
928 struct hashtb_enumerator eenle;
929 struct hashtb_enumerator *enle = &eenle;
930
931 hashtb_start(ne->name_list, enle);
932 nl_element=hashtb_n(ne->name_list);
933
934 for (i=0;i<nl_element;i++)
935 {
936 nle=enle->data;
937
938 for( j=first_face; j>= last_face; j--)
939 {
akmhoquea30cb772012-10-07 09:50:34 -0500940 if ( is_neighbor(orig_router) == 0 )
akmhoque3cced642012-09-24 16:20:20 -0500941 {
akmhoquea30cb772012-10-07 09:50:34 -0500942 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
943 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500944 }
945 else
946 {
akmhoquea30cb772012-10-07 09:50:34 -0500947 if ( j == last_face && is_neighbor(nle->name)==0)
akmhoque3cced642012-09-24 16:20:20 -0500948 {
akmhoque0820a2e2012-09-24 20:23:01 -0500949 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500950 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
951 }
952 }
953 }
954
955 hashtb_next(enle);
956 }
957 hashtb_end(enle);
958
959
960
961 free(faces);
962 free(route_costs);
963
964 }
965 hashtb_end(e);
966
akmhoquede61ba92012-09-20 22:19:12 -0500967}
968
akmhoquefbfd0982012-09-09 20:59:03 -0500969void
970destroy_all_face_by_nlsr(void)
971{
972 int i, npt_element;
973
974 struct npt_entry *ne;
975
976 struct hashtb_enumerator ee;
977 struct hashtb_enumerator *e = &ee;
978
979 hashtb_start(nlsr->npt, e);
980 npt_element=hashtb_n(nlsr->npt);
981
982 for(i=0;i<npt_element;i++)
983 {
akmhoquefbfd0982012-09-09 20:59:03 -0500984 ne=e->data;
akmhoque3cced642012-09-24 16:20:20 -0500985 destroy_faces_by_orig_router(ne->orig_router);
akmhoquefbfd0982012-09-09 20:59:03 -0500986 hashtb_next(e);
987 }
988
989 hashtb_end(e);
990
991 printf("\n");
992}