blob: e3c848355b03f2e233b5dbc9193cd253852d1233 [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"
akmhoque1771c412012-11-09 13:06:08 -060028#include "utility.h"
akmhoque3560cb62012-09-09 10:52:30 -050029
30int
akmhoquede61ba92012-09-20 22:19:12 -050031add_npt_entry(char *orig_router, char *name_prefix, int num_face, int *faces, int *route_costs)
akmhoque3560cb62012-09-09 10:52:30 -050032{
33 if ( strcmp(orig_router,nlsr->router_name)== 0)
34 {
35 return -1;
36 }
akmhoque3cced642012-09-24 16:20:20 -050037
akmhoque3560cb62012-09-09 10:52:30 -050038 struct npt_entry *ne=(struct npt_entry*)malloc(sizeof(struct npt_entry ));
39
akmhoquede61ba92012-09-20 22:19:12 -050040 int res,res_nle,res_fle;
akmhoque3560cb62012-09-09 10:52:30 -050041 struct hashtb_enumerator ee;
42 struct hashtb_enumerator *e = &ee;
akmhoque810a5b52012-09-09 16:53:14 -050043
akmhoque3560cb62012-09-09 10:52:30 -050044
45 hashtb_start(nlsr->npt, e);
akmhoque810a5b52012-09-09 16:53:14 -050046 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
akmhoque3560cb62012-09-09 10:52:30 -050047
48 if(res == HT_NEW_ENTRY)
49 {
50 ne=e->data;
51
akmhoque810a5b52012-09-09 16:53:14 -050052 ne->orig_router=(char *)malloc(strlen(orig_router)+1);
53 memset(ne->orig_router,0,strlen(orig_router)+1);
54 memcpy(ne->orig_router,orig_router,strlen(orig_router));
akmhoque3560cb62012-09-09 10:52:30 -050055
akmhoque810a5b52012-09-09 16:53:14 -050056
akmhoquede61ba92012-09-20 22:19:12 -050057
akmhoque3560cb62012-09-09 10:52:30 -050058
akmhoque810a5b52012-09-09 16:53:14 -050059 struct hashtb_param param_nle = {0};
60 ne->name_list= hashtb_create(sizeof(struct name_list_entry ), &param_nle);
akmhoque3560cb62012-09-09 10:52:30 -050061
akmhoque810a5b52012-09-09 16:53:14 -050062 struct hashtb_enumerator eenle;
63 struct hashtb_enumerator *enle = &eenle;
akmhoque3560cb62012-09-09 10:52:30 -050064
akmhoque810a5b52012-09-09 16:53:14 -050065 hashtb_start(ne->name_list, enle);
66 res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
akmhoque3560cb62012-09-09 10:52:30 -050067
akmhoque810a5b52012-09-09 16:53:14 -050068 if(res_nle == HT_NEW_ENTRY )
akmhoque3560cb62012-09-09 10:52:30 -050069 {
akmhoquede61ba92012-09-20 22:19:12 -050070 struct name_list_entry *nle=(struct name_list_entry *)malloc(sizeof(struct name_list_entry));
akmhoque810a5b52012-09-09 16:53:14 -050071 nle=enle->data;
72 nle->name=(char *)malloc(strlen(name_prefix)+1);
73 memset(nle->name,0,strlen(name_prefix)+1);
74 memcpy(nle->name,name_prefix,strlen(name_prefix));
akmhoque3560cb62012-09-09 10:52:30 -050075
akmhoque810a5b52012-09-09 16:53:14 -050076
akmhoque3560cb62012-09-09 10:52:30 -050077
78 }
akmhoque810a5b52012-09-09 16:53:14 -050079 hashtb_end(enle);
80
akmhoquede61ba92012-09-20 22:19:12 -050081 struct hashtb_param param_fle = {0};
82 ne->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
akmhoque3560cb62012-09-09 10:52:30 -050083
akmhoquede61ba92012-09-20 22:19:12 -050084 if ( num_face > 0 )
85 {
86 struct hashtb_enumerator eef;
87 struct hashtb_enumerator *ef = &eef;
88
89 hashtb_start(ne->face_list, ef);
90 int i;
91
akmhoque3cced642012-09-24 16:20:20 -050092 for ( i=0; i < num_face ; i++)
akmhoquede61ba92012-09-20 22:19:12 -050093 {
94 int face=faces[i];
akmhoquea30cb772012-10-07 09:50:34 -050095 if ( face != NO_FACE && face != ZERO_FACE)
akmhoquede61ba92012-09-20 22:19:12 -050096 {
akmhoquea30cb772012-10-07 09:50:34 -050097 res_fle = hashtb_seek(ef, &face, sizeof(face), 0);
98
99 if ( res_fle == HT_NEW_ENTRY )
100 {
101 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
102 fle=ef->data;
103 fle->next_hop_face=face;
104 fle->route_cost=route_costs[i];
105 }
akmhoquede61ba92012-09-20 22:19:12 -0500106 }
107
108 }
109 hashtb_end(ef);
110 }
111
akmhoque3560cb62012-09-09 10:52:30 -0500112 }
113 else if (res == HT_OLD_ENTRY)
114 {
115 free(ne);
116 struct npt_entry *one;
117
118 one=e->data;
119
akmhoque810a5b52012-09-09 16:53:14 -0500120 struct hashtb_enumerator eenle;
121 struct hashtb_enumerator *enle = &eenle;
akmhoque3560cb62012-09-09 10:52:30 -0500122
akmhoque810a5b52012-09-09 16:53:14 -0500123 hashtb_start(one->name_list, enle);
124 res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
125
126 if(res_nle == HT_NEW_ENTRY )
akmhoque3560cb62012-09-09 10:52:30 -0500127 {
akmhoquede61ba92012-09-20 22:19:12 -0500128 struct name_list_entry *nle=(struct name_list_entry *)malloc(sizeof(struct name_list_entry));
akmhoque810a5b52012-09-09 16:53:14 -0500129 nle=enle->data;
130 nle->name=(char *)malloc(strlen(name_prefix)+1);
131 memset(nle->name,0,strlen(name_prefix)+1);
132 memcpy(nle->name,name_prefix,strlen(name_prefix));
akmhoque3560cb62012-09-09 10:52:30 -0500133 }
akmhoque810a5b52012-09-09 16:53:14 -0500134 else if(res_nle == HT_OLD_ENTRY )
akmhoque3560cb62012-09-09 10:52:30 -0500135 {
akmhoquede61ba92012-09-20 22:19:12 -0500136
akmhoque3560cb62012-09-09 10:52:30 -0500137 }
akmhoque810a5b52012-09-09 16:53:14 -0500138 hashtb_end(enle);
139
akmhoquede61ba92012-09-20 22:19:12 -0500140 if ( num_face > 0 )
141 {
142 struct hashtb_enumerator eef;
143 struct hashtb_enumerator *ef = &eef;
144
145 hashtb_start(one->face_list, ef);
146 int i;
147
148 for ( i=0; i< num_face ; i ++)
149 {
150 int face=faces[i];
akmhoquea30cb772012-10-07 09:50:34 -0500151 if ( face != NO_FACE && face != ZERO_FACE)
akmhoquede61ba92012-09-20 22:19:12 -0500152 {
akmhoquea30cb772012-10-07 09:50:34 -0500153 res_fle = hashtb_seek(ef, &face, sizeof(face), 0);
154
155 if ( res_fle == HT_NEW_ENTRY )
156 {
157 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
158 fle=ef->data;
159 fle->next_hop_face=face;
160 fle->route_cost=route_costs[i];
161 }
akmhoquede61ba92012-09-20 22:19:12 -0500162 }
akmhoquede61ba92012-09-20 22:19:12 -0500163 }
164 hashtb_end(ef);
165 }
akmhoque810a5b52012-09-09 16:53:14 -0500166
akmhoque3560cb62012-09-09 10:52:30 -0500167
168 }
169 hashtb_end(e);
akmhoque3cced642012-09-24 16:20:20 -0500170
171 update_ccnd_fib_for_orig_router(orig_router);
172
akmhoque3560cb62012-09-09 10:52:30 -0500173 return res;
174}
175
akmhoque3cced642012-09-24 16:20:20 -0500176void
177update_ccnd_fib_for_orig_router(char *orig_router)
178{
179
180 int res;
181 struct hashtb_enumerator ee;
182 struct hashtb_enumerator *e = &ee;
183
184
185 hashtb_start(nlsr->npt, e);
186 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
187
188 if(res == HT_NEW_ENTRY)
189 {
190 hashtb_delete(e);
191 }
192 else if ( res == HT_OLD_ENTRY )
193 {
194 struct npt_entry *ne;
195 ne=e->data;
196 int num_face=hashtb_n(ne->face_list);
197 int last_face,first_face;
198
199 int *faces=(int *)malloc(num_face*sizeof(int));
200 int *route_costs=(int *)malloc(num_face*sizeof(int));
201
202 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
203 sort_faces_by_distance(faces,route_costs,0,num_face);
204
205
206 first_face=num_face-1;
207
208 if ( nlsr->multi_path_face_num == 0 )
209 {
210 last_face=first_face;
211 }
212 else
213 {
214 if ( num_face <= nlsr->multi_path_face_num)
215 {
216 last_face=0;
217 }
218 else if ( nlsr->multi_path_face_num == 0)
219 {
220 last_face=num_face-nlsr->multi_path_face_num;
221 }
222 }
223
224 int i,j, nl_element;
225 struct name_list_entry *nle;
226 struct hashtb_enumerator eenle;
227 struct hashtb_enumerator *enle = &eenle;
228
229 hashtb_start(ne->name_list, enle);
230 nl_element=hashtb_n(ne->name_list);
231
232 for (i=0;i<nl_element;i++)
233 {
234 nle=enle->data;
235
236 for( j=first_face; j>= last_face; j--)
237 {
akmhoquea30cb772012-10-07 09:50:34 -0500238 //printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
239 //printf("Orig Router: %s \n",orig_router);
240 //printf("Name Prefix: %s \n",nle->name);
241 //printf("Is neighbor Orig Router: %d \n",is_neighbor(orig_router));
242 //printf("Is neighbor Name Prefix: %d \n",is_neighbor(nle->name));
243
244 if ( is_neighbor(orig_router) == 0 )
akmhoque3cced642012-09-24 16:20:20 -0500245 {
akmhoque1771c412012-11-09 13:06:08 -0600246 if ( nlsr->debugging )
247 printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
248 if ( nlsr->detailed_logging )
249 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
250 //printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoquea30cb772012-10-07 09:50:34 -0500251 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500252 }
253 else
254 {
akmhoquea30cb772012-10-07 09:50:34 -0500255 if ( j == last_face && is_neighbor(nle->name)==0)
akmhoque3cced642012-09-24 16:20:20 -0500256 {
akmhoque1771c412012-11-09 13:06:08 -0600257 if ( nlsr->debugging )
258 printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
259 if ( nlsr->detailed_logging )
260 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
261 //printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500262 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);
263 }
264 }
265 }
akmhoquea30cb772012-10-07 09:50:34 -0500266
267
akmhoque3cced642012-09-24 16:20:20 -0500268 hashtb_next(enle);
269 }
270 hashtb_end(enle);
271
272
273
274 free(faces);
275 free(route_costs);
276
277 }
278 hashtb_end(e);
279
280}
281
akmhoqueffacaa82012-09-13 17:48:30 -0500282int
akmhoque3cced642012-09-24 16:20:20 -0500283delete_npt_entry_by_router_and_name_prefix(char *orig_router, char *name_prefix)
akmhoqueffacaa82012-09-13 17:48:30 -0500284{
285 if ( strcmp(orig_router,nlsr->router_name)== 0)
286 {
287 return -1;
288 }
289
290 struct npt_entry *ne;
291
292 int res,res_nle;
293 struct hashtb_enumerator ee;
294 struct hashtb_enumerator *e = &ee;
295
296
297 hashtb_start(nlsr->npt, e);
298 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
299
300 if(res == HT_NEW_ENTRY)
301 {
302 hashtb_delete(e);
303 return -1;
304 }
305 else if (res == HT_OLD_ENTRY)
306 {
307 ne=e->data;
308
309 struct hashtb_enumerator eenle;
310 struct hashtb_enumerator *enle = &eenle;
311
312 hashtb_start(ne->name_list, enle);
313 res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
314
315 if(res_nle == HT_NEW_ENTRY )
316 {
317 hashtb_delete(enle);
318 }
319 else if(res_nle == HT_OLD_ENTRY )
320 {
akmhoque3cced642012-09-24 16:20:20 -0500321 struct name_list_entry *nle;
322
323 nle=enle->data;
324
325 int j;
326 int num_face=hashtb_n(ne->face_list);
327 int last_face,first_face;
328
329 int *faces=(int *)malloc(num_face*sizeof(int));
330 int *route_costs=(int *)malloc(num_face*sizeof(int));
331
332 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
333 sort_faces_by_distance(faces,route_costs,0,num_face);
334
335
336 first_face=num_face-1;
337
338 if ( nlsr->multi_path_face_num == 0 )
akmhoqueffacaa82012-09-13 17:48:30 -0500339 {
akmhoque3cced642012-09-24 16:20:20 -0500340 last_face=first_face;
akmhoqueffacaa82012-09-13 17:48:30 -0500341 }
akmhoque3cced642012-09-24 16:20:20 -0500342 else
343 {
344 if ( num_face <= nlsr->multi_path_face_num)
345 {
346 last_face=0;
347 }
348 else if ( nlsr->multi_path_face_num == 0)
349 {
350 last_face=num_face-nlsr->multi_path_face_num;
351 }
352 }
353
354 for( j=first_face; j>= last_face; j--)
355 {
akmhoquea30cb772012-10-07 09:50:34 -0500356
357 if ( is_neighbor(orig_router) == 0 )
akmhoque3cced642012-09-24 16:20:20 -0500358 {
akmhoque1771c412012-11-09 13:06:08 -0600359 if ( nlsr->debugging )
360 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
361 if ( nlsr->detailed_logging )
362 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoquea30cb772012-10-07 09:50:34 -0500363 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500364 }
365 else
366 {
akmhoquea30cb772012-10-07 09:50:34 -0500367 if ( j == last_face && is_neighbor(nle->name)==0)
akmhoque3cced642012-09-24 16:20:20 -0500368 {
akmhoque1771c412012-11-09 13:06:08 -0600369 if ( nlsr->debugging )
370 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
371 if ( nlsr->detailed_logging )
372 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500373 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
374 }
375 }
akmhoquea30cb772012-10-07 09:50:34 -0500376
akmhoque3cced642012-09-24 16:20:20 -0500377 }
378
379
380
381
akmhoque5211dae2012-11-13 12:06:03 -0600382 //hashtb_delete(enle); has to delete later
akmhoqueffacaa82012-09-13 17:48:30 -0500383 }
384
385 hashtb_end(enle);
386 }
387
388 hashtb_end(e);
389
390 return 0;
391}
akmhoque3560cb62012-09-09 10:52:30 -0500392
393void
394print_npt(void)
395{
akmhoque1771c412012-11-09 13:06:08 -0600396
397 if ( nlsr->debugging )
398 {
399 printf("\n");
400 printf("print_npt called\n");
401 }
402 if ( nlsr->detailed_logging )
403 {
404 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
405 writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_npt called\n");
406 }
akmhoque3560cb62012-09-09 10:52:30 -0500407 int i, npt_element;
408
409 struct npt_entry *ne;
410
411 struct hashtb_enumerator ee;
412 struct hashtb_enumerator *e = &ee;
413
414 hashtb_start(nlsr->npt, e);
415 npt_element=hashtb_n(nlsr->npt);
416
417 for(i=0;i<npt_element;i++)
418 {
akmhoque1771c412012-11-09 13:06:08 -0600419 if ( nlsr->debugging )
420 {
421 printf("\n");
422 printf("----------NPT ENTRY %d------------------\n",i+1);
423 }
424 if ( nlsr->detailed_logging )
425 {
426 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
427 writeLogg(__FILE__,__FUNCTION__,__LINE__,"----------NPT ENTRY %d------------------\n",i+1);
428 }
akmhoque3560cb62012-09-09 10:52:30 -0500429 ne=e->data;
akmhoque1771c412012-11-09 13:06:08 -0600430 if ( nlsr->debugging )
431 printf(" Origination Router: %s \n",ne->orig_router);
432 if ( nlsr->detailed_logging )
433 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s \n",ne->orig_router);
akmhoquede61ba92012-09-20 22:19:12 -0500434 //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 -0500435
akmhoquede61ba92012-09-20 22:19:12 -0500436 int j, nl_element,face_list_element;
akmhoque810a5b52012-09-09 16:53:14 -0500437 struct name_list_entry *nle;
438 struct hashtb_enumerator eenle;
439 struct hashtb_enumerator *enle = &eenle;
akmhoque3560cb62012-09-09 10:52:30 -0500440
akmhoque810a5b52012-09-09 16:53:14 -0500441 hashtb_start(ne->name_list, enle);
442 nl_element=hashtb_n(ne->name_list);
akmhoque3560cb62012-09-09 10:52:30 -0500443
akmhoque810a5b52012-09-09 16:53:14 -0500444 for (j=0;j<nl_element;j++)
akmhoque3560cb62012-09-09 10:52:30 -0500445 {
akmhoque810a5b52012-09-09 16:53:14 -0500446 nle=enle->data;
akmhoque1771c412012-11-09 13:06:08 -0600447 if ( nlsr->debugging )
448 printf(" Name Prefix: %s \n",nle->name);
449 if ( nlsr->detailed_logging )
450 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix: %s \n",nle->name);
akmhoque810a5b52012-09-09 16:53:14 -0500451 hashtb_next(enle);
akmhoque3560cb62012-09-09 10:52:30 -0500452 }
akmhoque810a5b52012-09-09 16:53:14 -0500453 hashtb_end(enle);
akmhoquede61ba92012-09-20 22:19:12 -0500454
455 struct face_list_entry *fle;
456
457 struct hashtb_enumerator eef;
458 struct hashtb_enumerator *ef = &eef;
459
460 hashtb_start(ne->face_list, ef);
461 face_list_element=hashtb_n(ne->face_list);
462 if ( face_list_element <= 0 )
463 {
akmhoque1771c412012-11-09 13:06:08 -0600464 if ( nlsr->debugging )
465 printf(" Face: No Face \n");
466 if ( nlsr->detailed_logging )
467 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
468
akmhoquede61ba92012-09-20 22:19:12 -0500469 }
470 else
471 {
472 for(j=0;j<face_list_element;j++)
473 {
474 fle=ef->data;
akmhoque1771c412012-11-09 13:06:08 -0600475 if ( nlsr->debugging )
476 printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
477 if ( nlsr->detailed_logging )
478 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
akmhoquede61ba92012-09-20 22:19:12 -0500479 hashtb_next(ef);
480 }
481 }
482 hashtb_end(ef);
483
akmhoque3560cb62012-09-09 10:52:30 -0500484
485 hashtb_next(e);
486 }
487
488 hashtb_end(e);
489
akmhoque3171d652012-11-13 11:44:33 -0600490 if ( nlsr->debugging )
491 printf("\n");
492 if ( nlsr->detailed_logging )
493 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoque3560cb62012-09-09 10:52:30 -0500494}
akmhoque810a5b52012-09-09 16:53:14 -0500495
496void
akmhoquede61ba92012-09-20 22:19:12 -0500497delete_orig_router_from_npt(char *orig_router)
akmhoque810a5b52012-09-09 16:53:14 -0500498{
akmhoquede61ba92012-09-20 22:19:12 -0500499 int res,num_face,num_prefix;
akmhoque810a5b52012-09-09 16:53:14 -0500500 struct npt_entry *ne;
501
502 struct hashtb_enumerator ee;
503 struct hashtb_enumerator *e = &ee;
504
505 hashtb_start(nlsr->npt, e);
506 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
507
508 if ( res == HT_OLD_ENTRY )
509 {
510 ne=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500511 num_prefix=hashtb_n(ne->name_list);
512 if ( num_prefix > 0 )
akmhoque810a5b52012-09-09 16:53:14 -0500513 {
akmhoquede61ba92012-09-20 22:19:12 -0500514 num_face=hashtb_n(ne->face_list);
515
516 if ( num_face > 0 )
akmhoque810a5b52012-09-09 16:53:14 -0500517 {
518 int j, nl_element;
519 struct name_list_entry *nle;
520 struct hashtb_enumerator eenle;
521 struct hashtb_enumerator *enle = &eenle;
522
523 hashtb_start(ne->name_list, enle);
524 nl_element=hashtb_n(ne->name_list);
525
526 for (j=0;j<nl_element;j++)
527 {
528 nle=enle->data;
akmhoque3cced642012-09-24 16:20:20 -0500529 delete_npt_entry_by_router_and_name_prefix(ne->orig_router,nle->name);
akmhoque810a5b52012-09-09 16:53:14 -0500530 hashtb_next(enle);
531 }
532 hashtb_end(enle);
533
534 }
535
536 }
537 hashtb_destroy(&ne->name_list);
akmhoquede61ba92012-09-20 22:19:12 -0500538 hashtb_destroy(&ne->face_list);
akmhoque810a5b52012-09-09 16:53:14 -0500539 hashtb_delete(e);
540 }
541 else if ( res == HT_NEW_ENTRY )
542 {
543 hashtb_delete(e);
544 }
545 hashtb_end(e);
546}
akmhoquefbfd0982012-09-09 20:59:03 -0500547
akmhoquede61ba92012-09-20 22:19:12 -0500548
549void
550add_face_to_npt_by_face_id(char *dest_router, int face_id, int route_cost)
551{
akmhoque1771c412012-11-09 13:06:08 -0600552 if ( nlsr->debugging )
553 {
554 printf("add_face_to_npt_by_face_id called\n");
555 printf("Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
556 }
557 if ( nlsr->detailed_logging )
558 {
559 writeLogg(__FILE__,__FUNCTION__,__LINE__,"add_face_to_npt_by_face_id called\n");
560 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
561 }
akmhoquede61ba92012-09-20 22:19:12 -0500562
akmhoque1771c412012-11-09 13:06:08 -0600563
akmhoquede61ba92012-09-20 22:19:12 -0500564 int res,res1;
565 struct npt_entry *ne;
566
567 struct hashtb_enumerator ee;
568 struct hashtb_enumerator *e = &ee;
569
570 hashtb_start(nlsr->npt, e);
571 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
572
573 if ( res == HT_OLD_ENTRY )
574 {
akmhoque1771c412012-11-09 13:06:08 -0600575 if ( nlsr->debugging )
576 printf("Dest Router Found \n");
577 if ( nlsr->detailed_logging )
578 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest Router Found \n");
579
akmhoquede61ba92012-09-20 22:19:12 -0500580 ne=e->data;
581
582 struct hashtb_enumerator eef;
583 struct hashtb_enumerator *ef = &eef;
584
585 hashtb_start(ne->face_list, ef);
586 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
587
588 if ( res1 == HT_OLD_ENTRY )
589 {
akmhoque1771c412012-11-09 13:06:08 -0600590 if ( nlsr->debugging )
591 printf("Face Found \n");
592 if ( nlsr->detailed_logging )
593 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face Found \n");
akmhoquede61ba92012-09-20 22:19:12 -0500594 struct face_list_entry *fle;//=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
595 fle=ef->data;
596 fle->next_hop_face=face_id;
597 fle->route_cost=route_cost;
598 }
599 else if ( res1 == HT_NEW_ENTRY )
600 {
akmhoque1771c412012-11-09 13:06:08 -0600601 if ( nlsr->debugging )
602 printf("Face Not Found \n");
603 if ( nlsr->detailed_logging )
604 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face Not Found \n");
akmhoquede61ba92012-09-20 22:19:12 -0500605 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
606 fle=ef->data;
607 fle->next_hop_face=face_id;
608 fle->route_cost=route_cost;
609 //hashtb_delete(ef);
610 }
611 hashtb_end(ef);
612 }
613 else if (res == HT_NEW_ENTRY)
614 {
615 hashtb_delete(e);
616 }
617
618 hashtb_end(e);
619}
620
621
622void
akmhoque3cced642012-09-24 16:20:20 -0500623add_new_fib_entries_to_npt(void)
akmhoquede61ba92012-09-20 22:19:12 -0500624{
akmhoque1771c412012-11-09 13:06:08 -0600625 if ( nlsr->debugging )
626 printf("add_new_fib_entries_to_npt called\n");
627 if ( nlsr->detailed_logging )
628 writeLogg(__FILE__,__FUNCTION__,__LINE__,"add_new_fib_entries_to_npt called\n");
akmhoquede61ba92012-09-20 22:19:12 -0500629 int i,j, rt_element,face_list_element;
630
631 struct routing_table_entry *rte;
632
633 struct hashtb_enumerator ee;
634 struct hashtb_enumerator *e = &ee;
635
636 hashtb_start(nlsr->routing_table, e);
637 rt_element=hashtb_n(nlsr->routing_table);
638
639 for(i=0;i<rt_element;i++)
640 {
akmhoquede61ba92012-09-20 22:19:12 -0500641 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500642
643 struct face_list_entry *fle;
644
645 struct hashtb_enumerator eef;
646 struct hashtb_enumerator *ef = &eef;
647
648 hashtb_start(rte->face_list, ef);
649 face_list_element=hashtb_n(rte->face_list);
650 if ( face_list_element <= 0 )
651 {
akmhoque1771c412012-11-09 13:06:08 -0600652 if ( nlsr->debugging )
653 printf(" Face: No Face \n");
654 if ( nlsr->detailed_logging )
655 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
akmhoquede61ba92012-09-20 22:19:12 -0500656 }
657 else
658 {
659 for(j=0;j<face_list_element;j++)
660 {
661 fle=ef->data;
akmhoquede61ba92012-09-20 22:19:12 -0500662 add_face_to_npt_by_face_id(rte->dest_router,fle->next_hop_face,fle->route_cost);
663 hashtb_next(ef);
664 }
665 }
666 hashtb_end(ef);
667
668 hashtb_next(e);
669 }
670
671 hashtb_end(e);
672
673}
674
675
676void
677delete_face_from_npt_by_face_id(char *dest_router, int face_id)
678{
akmhoque1771c412012-11-09 13:06:08 -0600679 if ( nlsr->debugging )
680 printf("delete_face_from_npt_by_face_id\n");
681 if ( nlsr->detailed_logging )
682 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_face_from_npt_by_face_id\n");
akmhoquede61ba92012-09-20 22:19:12 -0500683
684 int res,res1;
685 struct npt_entry *ne;
686
687 struct hashtb_enumerator ee;
688 struct hashtb_enumerator *e = &ee;
689
690 hashtb_start(nlsr->npt, e);
691 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
692
693 if ( res == HT_OLD_ENTRY )
694 {
695 ne=e->data;
696
697 struct hashtb_enumerator eef;
698 struct hashtb_enumerator *ef = &eef;
699
700 hashtb_start(ne->face_list, ef);
701 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
702
703 if ( res1 == HT_OLD_ENTRY )
704 {
705 hashtb_delete(ef);
706 }
707 else if ( res1 == HT_NEW_ENTRY )
708 {
709 hashtb_delete(ef);
710 }
711 hashtb_end(ef);
712 }
713 else if (res == HT_NEW_ENTRY)
714 {
715 hashtb_delete(e);
716 }
717
718 hashtb_end(e);
719}
720
721int
722delete_old_face_from_npt(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
723{
724 if(flags == CCN_SCHEDULE_CANCEL)
725 {
726 return -1;
727 }
728
729 nlsr_lock();
730
akmhoque1771c412012-11-09 13:06:08 -0600731 if ( nlsr->debugging )
732 printf("delete_old_face_from_npt\n");
733 if ( nlsr->detailed_logging )
734 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_old_face_from_npt\n");
735
akmhoquede61ba92012-09-20 22:19:12 -0500736 if ( ev->evdata != NULL )
akmhoque1771c412012-11-09 13:06:08 -0600737 {
738 if ( nlsr->debugging )
739 printf("Event Data: %s \n",(char *)ev->evdata);
740 if ( nlsr->detailed_logging )
741 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Event Data: %s \n",(char *)ev->evdata);
akmhoquede61ba92012-09-20 22:19:12 -0500742 char *sep="|";
743 char *rem;
744 char *orig_router;
745 char *faceid;
746 int face_id;
747
748 char *face_data=(char *)malloc(strlen((char *)ev->evdata)+1);
749 memset(face_data,0,strlen((char *)ev->evdata)+1);
750 memcpy(face_data+strlen(face_data),(char *)ev->evdata,strlen((char *)ev->evdata));
751
752 orig_router=strtok_r(face_data,sep,&rem);
753 faceid=strtok_r(NULL,sep,&rem);
754 face_id=atoi(faceid);
akmhoque1771c412012-11-09 13:06:08 -0600755
756 if ( nlsr->debugging )
757 printf("Orig Router: %s Face: %d \n",orig_router,face_id);
758 if ( nlsr->detailed_logging )
759 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig Router: %s Face: %d \n",orig_router,face_id);
akmhoquede61ba92012-09-20 22:19:12 -0500760
761 delete_face_from_npt_by_face_id(orig_router,face_id);
762 }
763
764 nlsr_unlock();
765
766 return 0;
767}
768
769void
akmhoque3cced642012-09-24 16:20:20 -0500770clean_old_fib_entries_from_npt(void)
akmhoquede61ba92012-09-20 22:19:12 -0500771{
772
akmhoque1771c412012-11-09 13:06:08 -0600773
774 if ( nlsr->debugging )
775 printf("clean_old_fib_entries_from_npt called\n\n");
776 if ( nlsr->detailed_logging )
777 writeLogg(__FILE__,__FUNCTION__,__LINE__,"clean_old_fib_entries_from_npt called\n\n");
akmhoquede61ba92012-09-20 22:19:12 -0500778 int i, npt_element;
779
780 struct npt_entry *ne;
781
782 struct hashtb_enumerator ee;
783 struct hashtb_enumerator *e = &ee;
784
785 hashtb_start(nlsr->npt, e);
786 npt_element=hashtb_n(nlsr->npt);
787
788 for(i=0;i<npt_element;i++)
789 {
790 ne=e->data;
791
792 int j,k, nl_element,face_list_element;
793 struct face_list_entry *fle;
794
795 struct hashtb_enumerator eef;
796 struct hashtb_enumerator *ef = &eef;
797
798 hashtb_start(ne->face_list, ef);
799 face_list_element=hashtb_n(ne->face_list);
800 if ( face_list_element <= 0 )
801 {
akmhoque1771c412012-11-09 13:06:08 -0600802 if ( nlsr->debugging )
803 printf(" Face: No Face \n");
804 if ( nlsr->detailed_logging )
805 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
806
akmhoquede61ba92012-09-20 22:19:12 -0500807 }
808 else
809 {
810 for(j=0;j<face_list_element;j++)
811 {
812 fle=ef->data;
813 int check=does_face_exist_for_router(ne->orig_router,fle->next_hop_face);
814 if ( check == 0 )
815 {
816 struct name_list_entry *nle;
817 struct hashtb_enumerator eenle;
818 struct hashtb_enumerator *enle = &eenle;
819
820 hashtb_start(ne->name_list, enle);
821 nl_element=hashtb_n(ne->name_list);
822
823 for (k=0;k<nl_element;k++)
824 {
825 nle=enle->data;
826
akmhoque3cced642012-09-24 16:20:20 -0500827 //delete all the fib entries here
akmhoquea30cb772012-10-07 09:50:34 -0500828
akmhoque1771c412012-11-09 13:06:08 -0600829 //printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
830
akmhoque3cced642012-09-24 16:20:20 -0500831 if( is_neighbor(nle->name) == 0 )
832 {
akmhoque1771c412012-11-09 13:06:08 -0600833 if ( nlsr->debugging )
834 printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
835 if ( nlsr->detailed_logging )
836 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
akmhoque3cced642012-09-24 16:20:20 -0500837 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, fle->next_hop_face);
838 }
839
akmhoquede61ba92012-09-20 22:19:12 -0500840
841 hashtb_next(enle);
842 }
843 hashtb_end(enle);
844
845 char faceid[20];
846 memset(faceid,0,20);
847 sprintf(faceid,"%d",fle->next_hop_face);
848 char *evdata=(char *)malloc(strlen(ne->orig_router)+strlen(faceid)+2);
849 memset(evdata,0,strlen(ne->orig_router)+strlen(faceid)+2);
850 memcpy(evdata+strlen(evdata),ne->orig_router,strlen(ne->orig_router));
851 memcpy(evdata+strlen(evdata),"|",1);
852 memcpy(evdata+strlen(evdata),faceid,strlen(faceid));
853
854 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_old_face_from_npt, (void *)evdata, 0);
855
856 }
857
858 hashtb_next(ef);
859 }
860 }
861 hashtb_end(ef);
862
863
864 hashtb_next(e);
865 }
866
867 hashtb_end(e);
868
869}
870
871void
akmhoque3cced642012-09-24 16:20:20 -0500872update_npt_with_new_route(void)
akmhoquede61ba92012-09-20 22:19:12 -0500873{
874 clean_old_fib_entries_from_npt();
875 add_new_fib_entries_to_npt();
akmhoque3cced642012-09-24 16:20:20 -0500876
877 int i, npt_element;
878
879 struct npt_entry *ne;
880
881 struct hashtb_enumerator ee;
882 struct hashtb_enumerator *e = &ee;
883
884 hashtb_start(nlsr->npt, e);
885 npt_element=hashtb_n(nlsr->npt);
886
887 for(i=0;i<npt_element;i++)
888 {
889
890 ne=e->data;
891 update_ccnd_fib_for_orig_router(ne->orig_router);
892 hashtb_next(e);
893 }
894
895 hashtb_end(e);
896}
897
898
899
900void
901sort_faces_by_distance(int *faces,int *route_costs,int start,int element)
902{
903 int i,j;
904 int temp_cost;
905 int temp_face;
906
907 for ( i=start ; i < element ; i ++)
908 {
909 for( j=i+1; j<element; j ++)
910 {
911 if (route_costs[j] < route_costs[i] )
912 {
913 temp_cost=route_costs[j];
914 route_costs[j]=route_costs[i];
915 route_costs[i]=temp_cost;
916
917 temp_face=faces[j];
918 faces[j]=faces[i];
919 faces[i]=temp_face;
920 }
921 }
922 }
923
924}
925
926void
927get_all_faces_for_orig_router_from_npt(char *orig_router, int *faces, int *route_costs, int num_faces)
928{
929
930 int res,face_list_element,j;
931 struct hashtb_enumerator ee;
932 struct hashtb_enumerator *e = &ee;
933
934
935 hashtb_start(nlsr->npt, e);
936 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
937
938 if(res == HT_NEW_ENTRY)
939 {
940 hashtb_delete(e);
941 }
942 else if ( res == HT_OLD_ENTRY )
943 {
944 struct npt_entry *ne;
945 ne=e->data;
946
947 struct face_list_entry *fle;
948
949 struct hashtb_enumerator eef;
950 struct hashtb_enumerator *ef = &eef;
951
952 hashtb_start(ne->face_list, ef);
953 face_list_element=hashtb_n(ne->face_list);
954 for(j=0;j<face_list_element;j++)
955 {
956 fle=ef->data;
957 faces[j]=fle->next_hop_face;
958 route_costs[j]=fle->route_cost;
959 hashtb_next(ef);
960 }
961 hashtb_end(ef);
962
963
964 }
965 hashtb_end(e);
966
967}
968
969void
970destroy_faces_by_orig_router(char *orig_router)
971{
972
973 int res;
974 struct hashtb_enumerator ee;
975 struct hashtb_enumerator *e = &ee;
976
977
978 hashtb_start(nlsr->npt, e);
979 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
980
981 if(res == HT_NEW_ENTRY)
982 {
983 hashtb_delete(e);
984 }
985 else if ( res == HT_OLD_ENTRY )
986 {
987 struct npt_entry *ne;
988 ne=e->data;
989 int num_face=hashtb_n(ne->face_list);
990 int last_face,first_face;
991
992 int *faces=(int *)malloc(num_face*sizeof(int));
993 int *route_costs=(int *)malloc(num_face*sizeof(int));
994
995 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
996 sort_faces_by_distance(faces,route_costs,0,num_face);
997
998
999 first_face=num_face-1;
1000
1001 if ( nlsr->multi_path_face_num == 0 )
1002 {
1003 last_face=first_face;
1004 }
1005 else
1006 {
1007 if ( num_face <= nlsr->multi_path_face_num)
1008 {
1009 last_face=0;
1010 }
1011 else if ( nlsr->multi_path_face_num == 0)
1012 {
1013 last_face=num_face-nlsr->multi_path_face_num;
1014 }
1015 }
1016
1017 int i,j, nl_element;
1018 struct name_list_entry *nle;
1019 struct hashtb_enumerator eenle;
1020 struct hashtb_enumerator *enle = &eenle;
1021
1022 hashtb_start(ne->name_list, enle);
1023 nl_element=hashtb_n(ne->name_list);
1024
1025 for (i=0;i<nl_element;i++)
1026 {
1027 nle=enle->data;
1028
1029 for( j=first_face; j>= last_face; j--)
1030 {
akmhoquea30cb772012-10-07 09:50:34 -05001031 if ( is_neighbor(orig_router) == 0 )
akmhoque3cced642012-09-24 16:20:20 -05001032 {
akmhoque1771c412012-11-09 13:06:08 -06001033 if ( nlsr->debugging )
1034 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1035 if ( nlsr->detailed_logging )
1036 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoquea30cb772012-10-07 09:50:34 -05001037 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
akmhoque3cced642012-09-24 16:20:20 -05001038 }
1039 else
1040 {
akmhoquea30cb772012-10-07 09:50:34 -05001041 if ( j == last_face && is_neighbor(nle->name)==0)
akmhoque3cced642012-09-24 16:20:20 -05001042 {
akmhoque1771c412012-11-09 13:06:08 -06001043 if ( nlsr->debugging )
1044 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1045 if ( nlsr->detailed_logging )
1046 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoque3cced642012-09-24 16:20:20 -05001047 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
1048 }
1049 }
1050 }
1051
1052 hashtb_next(enle);
1053 }
1054 hashtb_end(enle);
1055
1056
1057
1058 free(faces);
1059 free(route_costs);
1060
1061 }
1062 hashtb_end(e);
1063
akmhoquede61ba92012-09-20 22:19:12 -05001064}
1065
akmhoquefbfd0982012-09-09 20:59:03 -05001066void
1067destroy_all_face_by_nlsr(void)
1068{
1069 int i, npt_element;
1070
1071 struct npt_entry *ne;
1072
1073 struct hashtb_enumerator ee;
1074 struct hashtb_enumerator *e = &ee;
1075
1076 hashtb_start(nlsr->npt, e);
1077 npt_element=hashtb_n(nlsr->npt);
1078
1079 for(i=0;i<npt_element;i++)
1080 {
akmhoquefbfd0982012-09-09 20:59:03 -05001081 ne=e->data;
akmhoque3cced642012-09-24 16:20:20 -05001082 destroy_faces_by_orig_router(ne->orig_router);
akmhoquefbfd0982012-09-09 20:59:03 -05001083 hashtb_next(e);
1084 }
1085
1086 hashtb_end(e);
1087
akmhoque3171d652012-11-13 11:44:33 -06001088 if ( nlsr->debugging )
1089 printf("\n");
1090 if ( nlsr->detailed_logging )
1091 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoquefbfd0982012-09-09 20:59:03 -05001092}