blob: a83e2fe80ac2a8625a127c347ee4bc38a5d14209 [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
akmhoque83b313e2012-10-23 11:23:10 -0500372 /*
akmhoqueda5b6832012-09-13 22:33:55 -0500373 if ( hashtb_n(ne->name_list) == 0 )
374 {
375 hashtb_delete(e);
376 }
akmhoque83b313e2012-10-23 11:23:10 -0500377 */
akmhoqueffacaa82012-09-13 17:48:30 -0500378 }
379
380 hashtb_end(e);
381
382 return 0;
383}
akmhoque3560cb62012-09-09 10:52:30 -0500384
385void
386print_npt(void)
387{
388 printf("\n");
389 printf("print_npt called\n\n");
390 int i, npt_element;
391
392 struct npt_entry *ne;
393
394 struct hashtb_enumerator ee;
395 struct hashtb_enumerator *e = &ee;
396
397 hashtb_start(nlsr->npt, e);
398 npt_element=hashtb_n(nlsr->npt);
399
400 for(i=0;i<npt_element;i++)
401 {
402 printf("\n");
403 printf("----------NPT ENTRY %d------------------\n",i+1);
404 ne=e->data;
akmhoque810a5b52012-09-09 16:53:14 -0500405 printf(" Origination Router: %s \n",ne->orig_router);
akmhoquede61ba92012-09-20 22:19:12 -0500406 //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 -0500407
akmhoquede61ba92012-09-20 22:19:12 -0500408 int j, nl_element,face_list_element;
akmhoque810a5b52012-09-09 16:53:14 -0500409 struct name_list_entry *nle;
410 struct hashtb_enumerator eenle;
411 struct hashtb_enumerator *enle = &eenle;
akmhoque3560cb62012-09-09 10:52:30 -0500412
akmhoque810a5b52012-09-09 16:53:14 -0500413 hashtb_start(ne->name_list, enle);
414 nl_element=hashtb_n(ne->name_list);
akmhoque3560cb62012-09-09 10:52:30 -0500415
akmhoque810a5b52012-09-09 16:53:14 -0500416 for (j=0;j<nl_element;j++)
akmhoque3560cb62012-09-09 10:52:30 -0500417 {
akmhoque810a5b52012-09-09 16:53:14 -0500418 nle=enle->data;
419 printf(" Name Prefix: %s \n",nle->name);
420 hashtb_next(enle);
akmhoque3560cb62012-09-09 10:52:30 -0500421 }
akmhoque810a5b52012-09-09 16:53:14 -0500422 hashtb_end(enle);
akmhoquede61ba92012-09-20 22:19:12 -0500423
424 struct face_list_entry *fle;
425
426 struct hashtb_enumerator eef;
427 struct hashtb_enumerator *ef = &eef;
428
429 hashtb_start(ne->face_list, ef);
430 face_list_element=hashtb_n(ne->face_list);
431 if ( face_list_element <= 0 )
432 {
433 printf(" Face: No Face \n");
434 }
435 else
436 {
437 for(j=0;j<face_list_element;j++)
438 {
439 fle=ef->data;
440 printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
441 hashtb_next(ef);
442 }
443 }
444 hashtb_end(ef);
445
akmhoque3560cb62012-09-09 10:52:30 -0500446
447 hashtb_next(e);
448 }
449
450 hashtb_end(e);
451
452 printf("\n");
453}
akmhoque810a5b52012-09-09 16:53:14 -0500454
455void
akmhoquede61ba92012-09-20 22:19:12 -0500456delete_orig_router_from_npt(char *orig_router)
akmhoque810a5b52012-09-09 16:53:14 -0500457{
akmhoquede61ba92012-09-20 22:19:12 -0500458 int res,num_face,num_prefix;
akmhoque810a5b52012-09-09 16:53:14 -0500459 struct npt_entry *ne;
460
461 struct hashtb_enumerator ee;
462 struct hashtb_enumerator *e = &ee;
463
464 hashtb_start(nlsr->npt, e);
465 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
466
467 if ( res == HT_OLD_ENTRY )
468 {
469 ne=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500470 num_prefix=hashtb_n(ne->name_list);
471 if ( num_prefix > 0 )
akmhoque810a5b52012-09-09 16:53:14 -0500472 {
akmhoquede61ba92012-09-20 22:19:12 -0500473 num_face=hashtb_n(ne->face_list);
474
475 if ( num_face > 0 )
akmhoque810a5b52012-09-09 16:53:14 -0500476 {
477 int j, nl_element;
478 struct name_list_entry *nle;
479 struct hashtb_enumerator eenle;
480 struct hashtb_enumerator *enle = &eenle;
481
482 hashtb_start(ne->name_list, enle);
483 nl_element=hashtb_n(ne->name_list);
484
485 for (j=0;j<nl_element;j++)
486 {
487 nle=enle->data;
akmhoquede61ba92012-09-20 22:19:12 -0500488
akmhoque3cced642012-09-24 16:20:20 -0500489 delete_npt_entry_by_router_and_name_prefix(ne->orig_router,nle->name);
490
akmhoque810a5b52012-09-09 16:53:14 -0500491 hashtb_next(enle);
492 }
493 hashtb_end(enle);
494
495 }
496
497 }
498 hashtb_destroy(&ne->name_list);
akmhoquede61ba92012-09-20 22:19:12 -0500499 hashtb_destroy(&ne->face_list);
akmhoque810a5b52012-09-09 16:53:14 -0500500 hashtb_delete(e);
501 }
502 else if ( res == HT_NEW_ENTRY )
503 {
504 hashtb_delete(e);
505 }
506 hashtb_end(e);
507}
akmhoquefbfd0982012-09-09 20:59:03 -0500508
akmhoquede61ba92012-09-20 22:19:12 -0500509
510void
511add_face_to_npt_by_face_id(char *dest_router, int face_id, int route_cost)
512{
513 printf("add_face_to_npt_by_face_id called\n");
514
515 printf("Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
516 int res,res1;
517 struct npt_entry *ne;
518
519 struct hashtb_enumerator ee;
520 struct hashtb_enumerator *e = &ee;
521
522 hashtb_start(nlsr->npt, e);
523 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
524
525 if ( res == HT_OLD_ENTRY )
526 {
527 printf("Dest Router Found \n");
528 ne=e->data;
529
530 struct hashtb_enumerator eef;
531 struct hashtb_enumerator *ef = &eef;
532
533 hashtb_start(ne->face_list, ef);
534 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
535
536 if ( res1 == HT_OLD_ENTRY )
537 {
538 printf("Face Found \n");
539 struct face_list_entry *fle;//=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
540 fle=ef->data;
541 fle->next_hop_face=face_id;
542 fle->route_cost=route_cost;
543 }
544 else if ( res1 == HT_NEW_ENTRY )
545 {
546 printf("Face Not Found \n");
547 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
548 fle=ef->data;
549 fle->next_hop_face=face_id;
550 fle->route_cost=route_cost;
551 //hashtb_delete(ef);
552 }
553 hashtb_end(ef);
554 }
555 else if (res == HT_NEW_ENTRY)
556 {
557 hashtb_delete(e);
558 }
559
560 hashtb_end(e);
561}
562
563
564void
akmhoque3cced642012-09-24 16:20:20 -0500565add_new_fib_entries_to_npt(void)
akmhoquede61ba92012-09-20 22:19:12 -0500566{
567 printf("add_new_fib_entries_to_npt called\n");
568 int i,j, rt_element,face_list_element;
569
570 struct routing_table_entry *rte;
571
572 struct hashtb_enumerator ee;
573 struct hashtb_enumerator *e = &ee;
574
575 hashtb_start(nlsr->routing_table, e);
576 rt_element=hashtb_n(nlsr->routing_table);
577
578 for(i=0;i<rt_element;i++)
579 {
akmhoquede61ba92012-09-20 22:19:12 -0500580 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500581
582 struct face_list_entry *fle;
583
584 struct hashtb_enumerator eef;
585 struct hashtb_enumerator *ef = &eef;
586
587 hashtb_start(rte->face_list, ef);
588 face_list_element=hashtb_n(rte->face_list);
589 if ( face_list_element <= 0 )
590 {
591 printf(" Face: No Face \n");
592 }
593 else
594 {
595 for(j=0;j<face_list_element;j++)
596 {
597 fle=ef->data;
akmhoquede61ba92012-09-20 22:19:12 -0500598 add_face_to_npt_by_face_id(rte->dest_router,fle->next_hop_face,fle->route_cost);
599 hashtb_next(ef);
600 }
601 }
602 hashtb_end(ef);
603
604 hashtb_next(e);
605 }
606
607 hashtb_end(e);
608
609}
610
611
612void
613delete_face_from_npt_by_face_id(char *dest_router, int face_id)
614{
615 printf("delete_face_from_npt_by_face_id called\n");
616
617 int res,res1;
618 struct npt_entry *ne;
619
620 struct hashtb_enumerator ee;
621 struct hashtb_enumerator *e = &ee;
622
623 hashtb_start(nlsr->npt, e);
624 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
625
626 if ( res == HT_OLD_ENTRY )
627 {
628 ne=e->data;
629
630 struct hashtb_enumerator eef;
631 struct hashtb_enumerator *ef = &eef;
632
633 hashtb_start(ne->face_list, ef);
634 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
635
636 if ( res1 == HT_OLD_ENTRY )
637 {
638 hashtb_delete(ef);
639 }
640 else if ( res1 == HT_NEW_ENTRY )
641 {
642 hashtb_delete(ef);
643 }
644 hashtb_end(ef);
645 }
646 else if (res == HT_NEW_ENTRY)
647 {
648 hashtb_delete(e);
649 }
650
651 hashtb_end(e);
652}
653
654int
655delete_old_face_from_npt(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
656{
657 if(flags == CCN_SCHEDULE_CANCEL)
658 {
659 return -1;
660 }
661
662 nlsr_lock();
663
664 printf("delete_old_face_from_npt called\n");
665 if ( ev->evdata != NULL )
666 {
667 printf("Event Data: %s \n",(char *)ev->evdata);
668 char *sep="|";
669 char *rem;
670 char *orig_router;
671 char *faceid;
672 int face_id;
673
674 char *face_data=(char *)malloc(strlen((char *)ev->evdata)+1);
675 memset(face_data,0,strlen((char *)ev->evdata)+1);
676 memcpy(face_data+strlen(face_data),(char *)ev->evdata,strlen((char *)ev->evdata));
677
678 orig_router=strtok_r(face_data,sep,&rem);
679 faceid=strtok_r(NULL,sep,&rem);
680 face_id=atoi(faceid);
681 printf("Orig Router: %s Face: %d \n",orig_router,face_id);
682
683 delete_face_from_npt_by_face_id(orig_router,face_id);
684 }
685
686 nlsr_unlock();
687
688 return 0;
689}
690
691void
akmhoque3cced642012-09-24 16:20:20 -0500692clean_old_fib_entries_from_npt(void)
akmhoquede61ba92012-09-20 22:19:12 -0500693{
694
695 printf("clean_old_fib_entries_from_npt called\n\n");
696 int i, npt_element;
697
698 struct npt_entry *ne;
699
700 struct hashtb_enumerator ee;
701 struct hashtb_enumerator *e = &ee;
702
703 hashtb_start(nlsr->npt, e);
704 npt_element=hashtb_n(nlsr->npt);
705
706 for(i=0;i<npt_element;i++)
707 {
708 ne=e->data;
709
710 int j,k, nl_element,face_list_element;
711 struct face_list_entry *fle;
712
713 struct hashtb_enumerator eef;
714 struct hashtb_enumerator *ef = &eef;
715
716 hashtb_start(ne->face_list, ef);
717 face_list_element=hashtb_n(ne->face_list);
718 if ( face_list_element <= 0 )
719 {
720 printf(" Face: No Face \n");
721 }
722 else
723 {
724 for(j=0;j<face_list_element;j++)
725 {
726 fle=ef->data;
727 int check=does_face_exist_for_router(ne->orig_router,fle->next_hop_face);
728 if ( check == 0 )
729 {
730 struct name_list_entry *nle;
731 struct hashtb_enumerator eenle;
732 struct hashtb_enumerator *enle = &eenle;
733
734 hashtb_start(ne->name_list, enle);
735 nl_element=hashtb_n(ne->name_list);
736
737 for (k=0;k<nl_element;k++)
738 {
739 nle=enle->data;
740
akmhoque3cced642012-09-24 16:20:20 -0500741 //delete all the fib entries here
akmhoquea30cb772012-10-07 09:50:34 -0500742 printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
743
744
akmhoque3cced642012-09-24 16:20:20 -0500745 if( is_neighbor(nle->name) == 0 )
746 {
akmhoque0820a2e2012-09-24 20:23:01 -0500747 printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
akmhoque3cced642012-09-24 16:20:20 -0500748 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, fle->next_hop_face);
749 }
750
akmhoquede61ba92012-09-20 22:19:12 -0500751
752 hashtb_next(enle);
753 }
754 hashtb_end(enle);
755
756 char faceid[20];
757 memset(faceid,0,20);
758 sprintf(faceid,"%d",fle->next_hop_face);
759 char *evdata=(char *)malloc(strlen(ne->orig_router)+strlen(faceid)+2);
760 memset(evdata,0,strlen(ne->orig_router)+strlen(faceid)+2);
761 memcpy(evdata+strlen(evdata),ne->orig_router,strlen(ne->orig_router));
762 memcpy(evdata+strlen(evdata),"|",1);
763 memcpy(evdata+strlen(evdata),faceid,strlen(faceid));
764
765 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_old_face_from_npt, (void *)evdata, 0);
766
767 }
768
769 hashtb_next(ef);
770 }
771 }
772 hashtb_end(ef);
773
774
775 hashtb_next(e);
776 }
777
778 hashtb_end(e);
779
780}
781
782void
akmhoque3cced642012-09-24 16:20:20 -0500783update_npt_with_new_route(void)
akmhoquede61ba92012-09-20 22:19:12 -0500784{
785 clean_old_fib_entries_from_npt();
786 add_new_fib_entries_to_npt();
akmhoque3cced642012-09-24 16:20:20 -0500787
788 int i, npt_element;
789
790 struct npt_entry *ne;
791
792 struct hashtb_enumerator ee;
793 struct hashtb_enumerator *e = &ee;
794
795 hashtb_start(nlsr->npt, e);
796 npt_element=hashtb_n(nlsr->npt);
797
798 for(i=0;i<npt_element;i++)
799 {
800
801 ne=e->data;
802 update_ccnd_fib_for_orig_router(ne->orig_router);
803 hashtb_next(e);
804 }
805
806 hashtb_end(e);
807}
808
809
810
811void
812sort_faces_by_distance(int *faces,int *route_costs,int start,int element)
813{
814 int i,j;
815 int temp_cost;
816 int temp_face;
817
818 for ( i=start ; i < element ; i ++)
819 {
820 for( j=i+1; j<element; j ++)
821 {
822 if (route_costs[j] < route_costs[i] )
823 {
824 temp_cost=route_costs[j];
825 route_costs[j]=route_costs[i];
826 route_costs[i]=temp_cost;
827
828 temp_face=faces[j];
829 faces[j]=faces[i];
830 faces[i]=temp_face;
831 }
832 }
833 }
834
835}
836
837void
838get_all_faces_for_orig_router_from_npt(char *orig_router, int *faces, int *route_costs, int num_faces)
839{
840
841 int res,face_list_element,j;
842 struct hashtb_enumerator ee;
843 struct hashtb_enumerator *e = &ee;
844
845
846 hashtb_start(nlsr->npt, e);
847 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
848
849 if(res == HT_NEW_ENTRY)
850 {
851 hashtb_delete(e);
852 }
853 else if ( res == HT_OLD_ENTRY )
854 {
855 struct npt_entry *ne;
856 ne=e->data;
857
858 struct face_list_entry *fle;
859
860 struct hashtb_enumerator eef;
861 struct hashtb_enumerator *ef = &eef;
862
863 hashtb_start(ne->face_list, ef);
864 face_list_element=hashtb_n(ne->face_list);
865 for(j=0;j<face_list_element;j++)
866 {
867 fle=ef->data;
868 faces[j]=fle->next_hop_face;
869 route_costs[j]=fle->route_cost;
870 hashtb_next(ef);
871 }
872 hashtb_end(ef);
873
874
875 }
876 hashtb_end(e);
877
878}
879
880void
881destroy_faces_by_orig_router(char *orig_router)
882{
883
884 int res;
885 struct hashtb_enumerator ee;
886 struct hashtb_enumerator *e = &ee;
887
888
889 hashtb_start(nlsr->npt, e);
890 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
891
892 if(res == HT_NEW_ENTRY)
893 {
894 hashtb_delete(e);
895 }
896 else if ( res == HT_OLD_ENTRY )
897 {
898 struct npt_entry *ne;
899 ne=e->data;
900 int num_face=hashtb_n(ne->face_list);
901 int last_face,first_face;
902
903 int *faces=(int *)malloc(num_face*sizeof(int));
904 int *route_costs=(int *)malloc(num_face*sizeof(int));
905
906 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
907 sort_faces_by_distance(faces,route_costs,0,num_face);
908
909
910 first_face=num_face-1;
911
912 if ( nlsr->multi_path_face_num == 0 )
913 {
914 last_face=first_face;
915 }
916 else
917 {
918 if ( num_face <= nlsr->multi_path_face_num)
919 {
920 last_face=0;
921 }
922 else if ( nlsr->multi_path_face_num == 0)
923 {
924 last_face=num_face-nlsr->multi_path_face_num;
925 }
926 }
927
928 int i,j, nl_element;
929 struct name_list_entry *nle;
930 struct hashtb_enumerator eenle;
931 struct hashtb_enumerator *enle = &eenle;
932
933 hashtb_start(ne->name_list, enle);
934 nl_element=hashtb_n(ne->name_list);
935
936 for (i=0;i<nl_element;i++)
937 {
938 nle=enle->data;
939
940 for( j=first_face; j>= last_face; j--)
941 {
akmhoquea30cb772012-10-07 09:50:34 -0500942 if ( is_neighbor(orig_router) == 0 )
akmhoque3cced642012-09-24 16:20:20 -0500943 {
akmhoquea30cb772012-10-07 09:50:34 -0500944 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
945 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500946 }
947 else
948 {
akmhoquea30cb772012-10-07 09:50:34 -0500949 if ( j == last_face && is_neighbor(nle->name)==0)
akmhoque3cced642012-09-24 16:20:20 -0500950 {
akmhoque0820a2e2012-09-24 20:23:01 -0500951 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500952 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
953 }
954 }
955 }
956
957 hashtb_next(enle);
958 }
959 hashtb_end(enle);
960
961
962
963 free(faces);
964 free(route_costs);
965
966 }
967 hashtb_end(e);
968
akmhoquede61ba92012-09-20 22:19:12 -0500969}
970
akmhoquefbfd0982012-09-09 20:59:03 -0500971void
972destroy_all_face_by_nlsr(void)
973{
974 int i, npt_element;
975
976 struct npt_entry *ne;
977
978 struct hashtb_enumerator ee;
979 struct hashtb_enumerator *e = &ee;
980
981 hashtb_start(nlsr->npt, e);
982 npt_element=hashtb_n(nlsr->npt);
983
984 for(i=0;i<npt_element;i++)
985 {
akmhoquefbfd0982012-09-09 20:59:03 -0500986 ne=e->data;
akmhoque3cced642012-09-24 16:20:20 -0500987 destroy_faces_by_orig_router(ne->orig_router);
akmhoquefbfd0982012-09-09 20:59:03 -0500988 hashtb_next(e);
989 }
990
991 hashtb_end(e);
992
993 printf("\n");
994}