blob: 9f690ffc67f7d919c0317a8a8fa3a782b64abb7d [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
akmhoqueffacaa82012-09-13 17:48:30 -0500382 hashtb_delete(enle);
383 }
384
385 hashtb_end(enle);
akmhoqueda5b6832012-09-13 22:33:55 -0500386
akmhoque83b313e2012-10-23 11:23:10 -0500387 /*
akmhoqueda5b6832012-09-13 22:33:55 -0500388 if ( hashtb_n(ne->name_list) == 0 )
389 {
390 hashtb_delete(e);
391 }
akmhoque83b313e2012-10-23 11:23:10 -0500392 */
akmhoqueffacaa82012-09-13 17:48:30 -0500393 }
394
395 hashtb_end(e);
396
397 return 0;
398}
akmhoque3560cb62012-09-09 10:52:30 -0500399
400void
401print_npt(void)
402{
akmhoque1771c412012-11-09 13:06:08 -0600403
404 if ( nlsr->debugging )
405 {
406 printf("\n");
407 printf("print_npt called\n");
408 }
409 if ( nlsr->detailed_logging )
410 {
411 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
412 writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_npt called\n");
413 }
akmhoque3560cb62012-09-09 10:52:30 -0500414 int i, npt_element;
415
416 struct npt_entry *ne;
417
418 struct hashtb_enumerator ee;
419 struct hashtb_enumerator *e = &ee;
420
421 hashtb_start(nlsr->npt, e);
422 npt_element=hashtb_n(nlsr->npt);
423
424 for(i=0;i<npt_element;i++)
425 {
akmhoque1771c412012-11-09 13:06:08 -0600426 if ( nlsr->debugging )
427 {
428 printf("\n");
429 printf("----------NPT ENTRY %d------------------\n",i+1);
430 }
431 if ( nlsr->detailed_logging )
432 {
433 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
434 writeLogg(__FILE__,__FUNCTION__,__LINE__,"----------NPT ENTRY %d------------------\n",i+1);
435 }
akmhoque3560cb62012-09-09 10:52:30 -0500436 ne=e->data;
akmhoque1771c412012-11-09 13:06:08 -0600437 if ( nlsr->debugging )
438 printf(" Origination Router: %s \n",ne->orig_router);
439 if ( nlsr->detailed_logging )
440 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s \n",ne->orig_router);
akmhoquede61ba92012-09-20 22:19:12 -0500441 //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 -0500442
akmhoquede61ba92012-09-20 22:19:12 -0500443 int j, nl_element,face_list_element;
akmhoque810a5b52012-09-09 16:53:14 -0500444 struct name_list_entry *nle;
445 struct hashtb_enumerator eenle;
446 struct hashtb_enumerator *enle = &eenle;
akmhoque3560cb62012-09-09 10:52:30 -0500447
akmhoque810a5b52012-09-09 16:53:14 -0500448 hashtb_start(ne->name_list, enle);
449 nl_element=hashtb_n(ne->name_list);
akmhoque3560cb62012-09-09 10:52:30 -0500450
akmhoque810a5b52012-09-09 16:53:14 -0500451 for (j=0;j<nl_element;j++)
akmhoque3560cb62012-09-09 10:52:30 -0500452 {
akmhoque810a5b52012-09-09 16:53:14 -0500453 nle=enle->data;
akmhoque1771c412012-11-09 13:06:08 -0600454 if ( nlsr->debugging )
455 printf(" Name Prefix: %s \n",nle->name);
456 if ( nlsr->detailed_logging )
457 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix: %s \n",nle->name);
akmhoque810a5b52012-09-09 16:53:14 -0500458 hashtb_next(enle);
akmhoque3560cb62012-09-09 10:52:30 -0500459 }
akmhoque810a5b52012-09-09 16:53:14 -0500460 hashtb_end(enle);
akmhoquede61ba92012-09-20 22:19:12 -0500461
462 struct face_list_entry *fle;
463
464 struct hashtb_enumerator eef;
465 struct hashtb_enumerator *ef = &eef;
466
467 hashtb_start(ne->face_list, ef);
468 face_list_element=hashtb_n(ne->face_list);
469 if ( face_list_element <= 0 )
470 {
akmhoque1771c412012-11-09 13:06:08 -0600471 if ( nlsr->debugging )
472 printf(" Face: No Face \n");
473 if ( nlsr->detailed_logging )
474 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
475
akmhoquede61ba92012-09-20 22:19:12 -0500476 }
477 else
478 {
479 for(j=0;j<face_list_element;j++)
480 {
481 fle=ef->data;
akmhoque1771c412012-11-09 13:06:08 -0600482 if ( nlsr->debugging )
483 printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
484 if ( nlsr->detailed_logging )
485 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
akmhoquede61ba92012-09-20 22:19:12 -0500486 hashtb_next(ef);
487 }
488 }
489 hashtb_end(ef);
490
akmhoque3560cb62012-09-09 10:52:30 -0500491
492 hashtb_next(e);
493 }
494
495 hashtb_end(e);
496
497 printf("\n");
498}
akmhoque810a5b52012-09-09 16:53:14 -0500499
500void
akmhoquede61ba92012-09-20 22:19:12 -0500501delete_orig_router_from_npt(char *orig_router)
akmhoque810a5b52012-09-09 16:53:14 -0500502{
akmhoquede61ba92012-09-20 22:19:12 -0500503 int res,num_face,num_prefix;
akmhoque810a5b52012-09-09 16:53:14 -0500504 struct npt_entry *ne;
505
506 struct hashtb_enumerator ee;
507 struct hashtb_enumerator *e = &ee;
508
509 hashtb_start(nlsr->npt, e);
510 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
511
512 if ( res == HT_OLD_ENTRY )
513 {
514 ne=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500515 num_prefix=hashtb_n(ne->name_list);
516 if ( num_prefix > 0 )
akmhoque810a5b52012-09-09 16:53:14 -0500517 {
akmhoquede61ba92012-09-20 22:19:12 -0500518 num_face=hashtb_n(ne->face_list);
519
520 if ( num_face > 0 )
akmhoque810a5b52012-09-09 16:53:14 -0500521 {
522 int j, nl_element;
523 struct name_list_entry *nle;
524 struct hashtb_enumerator eenle;
525 struct hashtb_enumerator *enle = &eenle;
526
527 hashtb_start(ne->name_list, enle);
528 nl_element=hashtb_n(ne->name_list);
529
530 for (j=0;j<nl_element;j++)
531 {
532 nle=enle->data;
akmhoquede61ba92012-09-20 22:19:12 -0500533
akmhoque3cced642012-09-24 16:20:20 -0500534 delete_npt_entry_by_router_and_name_prefix(ne->orig_router,nle->name);
535
akmhoque810a5b52012-09-09 16:53:14 -0500536 hashtb_next(enle);
537 }
538 hashtb_end(enle);
539
540 }
541
542 }
543 hashtb_destroy(&ne->name_list);
akmhoquede61ba92012-09-20 22:19:12 -0500544 hashtb_destroy(&ne->face_list);
akmhoque810a5b52012-09-09 16:53:14 -0500545 hashtb_delete(e);
546 }
547 else if ( res == HT_NEW_ENTRY )
548 {
549 hashtb_delete(e);
550 }
551 hashtb_end(e);
552}
akmhoquefbfd0982012-09-09 20:59:03 -0500553
akmhoquede61ba92012-09-20 22:19:12 -0500554
555void
556add_face_to_npt_by_face_id(char *dest_router, int face_id, int route_cost)
557{
akmhoque1771c412012-11-09 13:06:08 -0600558 if ( nlsr->debugging )
559 {
560 printf("add_face_to_npt_by_face_id called\n");
561 printf("Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
562 }
563 if ( nlsr->detailed_logging )
564 {
565 writeLogg(__FILE__,__FUNCTION__,__LINE__,"add_face_to_npt_by_face_id called\n");
566 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
567 }
akmhoquede61ba92012-09-20 22:19:12 -0500568
akmhoque1771c412012-11-09 13:06:08 -0600569
akmhoquede61ba92012-09-20 22:19:12 -0500570 int res,res1;
571 struct npt_entry *ne;
572
573 struct hashtb_enumerator ee;
574 struct hashtb_enumerator *e = &ee;
575
576 hashtb_start(nlsr->npt, e);
577 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
578
579 if ( res == HT_OLD_ENTRY )
580 {
akmhoque1771c412012-11-09 13:06:08 -0600581 if ( nlsr->debugging )
582 printf("Dest Router Found \n");
583 if ( nlsr->detailed_logging )
584 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest Router Found \n");
585
akmhoquede61ba92012-09-20 22:19:12 -0500586 ne=e->data;
587
588 struct hashtb_enumerator eef;
589 struct hashtb_enumerator *ef = &eef;
590
591 hashtb_start(ne->face_list, ef);
592 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
593
594 if ( res1 == HT_OLD_ENTRY )
595 {
akmhoque1771c412012-11-09 13:06:08 -0600596 if ( nlsr->debugging )
597 printf("Face Found \n");
598 if ( nlsr->detailed_logging )
599 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face Found \n");
akmhoquede61ba92012-09-20 22:19:12 -0500600 struct face_list_entry *fle;//=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
601 fle=ef->data;
602 fle->next_hop_face=face_id;
603 fle->route_cost=route_cost;
604 }
605 else if ( res1 == HT_NEW_ENTRY )
606 {
akmhoque1771c412012-11-09 13:06:08 -0600607 if ( nlsr->debugging )
608 printf("Face Not Found \n");
609 if ( nlsr->detailed_logging )
610 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face Not Found \n");
akmhoquede61ba92012-09-20 22:19:12 -0500611 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
612 fle=ef->data;
613 fle->next_hop_face=face_id;
614 fle->route_cost=route_cost;
615 //hashtb_delete(ef);
616 }
617 hashtb_end(ef);
618 }
619 else if (res == HT_NEW_ENTRY)
620 {
621 hashtb_delete(e);
622 }
623
624 hashtb_end(e);
625}
626
627
628void
akmhoque3cced642012-09-24 16:20:20 -0500629add_new_fib_entries_to_npt(void)
akmhoquede61ba92012-09-20 22:19:12 -0500630{
akmhoque1771c412012-11-09 13:06:08 -0600631 if ( nlsr->debugging )
632 printf("add_new_fib_entries_to_npt called\n");
633 if ( nlsr->detailed_logging )
634 writeLogg(__FILE__,__FUNCTION__,__LINE__,"add_new_fib_entries_to_npt called\n");
akmhoquede61ba92012-09-20 22:19:12 -0500635 int i,j, rt_element,face_list_element;
636
637 struct routing_table_entry *rte;
638
639 struct hashtb_enumerator ee;
640 struct hashtb_enumerator *e = &ee;
641
642 hashtb_start(nlsr->routing_table, e);
643 rt_element=hashtb_n(nlsr->routing_table);
644
645 for(i=0;i<rt_element;i++)
646 {
akmhoquede61ba92012-09-20 22:19:12 -0500647 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500648
649 struct face_list_entry *fle;
650
651 struct hashtb_enumerator eef;
652 struct hashtb_enumerator *ef = &eef;
653
654 hashtb_start(rte->face_list, ef);
655 face_list_element=hashtb_n(rte->face_list);
656 if ( face_list_element <= 0 )
657 {
akmhoque1771c412012-11-09 13:06:08 -0600658 if ( nlsr->debugging )
659 printf(" Face: No Face \n");
660 if ( nlsr->detailed_logging )
661 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
akmhoquede61ba92012-09-20 22:19:12 -0500662 }
663 else
664 {
665 for(j=0;j<face_list_element;j++)
666 {
667 fle=ef->data;
akmhoquede61ba92012-09-20 22:19:12 -0500668 add_face_to_npt_by_face_id(rte->dest_router,fle->next_hop_face,fle->route_cost);
669 hashtb_next(ef);
670 }
671 }
672 hashtb_end(ef);
673
674 hashtb_next(e);
675 }
676
677 hashtb_end(e);
678
679}
680
681
682void
683delete_face_from_npt_by_face_id(char *dest_router, int face_id)
684{
akmhoque1771c412012-11-09 13:06:08 -0600685 if ( nlsr->debugging )
686 printf("delete_face_from_npt_by_face_id\n");
687 if ( nlsr->detailed_logging )
688 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_face_from_npt_by_face_id\n");
akmhoquede61ba92012-09-20 22:19:12 -0500689
690 int res,res1;
691 struct npt_entry *ne;
692
693 struct hashtb_enumerator ee;
694 struct hashtb_enumerator *e = &ee;
695
696 hashtb_start(nlsr->npt, e);
697 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
698
699 if ( res == HT_OLD_ENTRY )
700 {
701 ne=e->data;
702
703 struct hashtb_enumerator eef;
704 struct hashtb_enumerator *ef = &eef;
705
706 hashtb_start(ne->face_list, ef);
707 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
708
709 if ( res1 == HT_OLD_ENTRY )
710 {
711 hashtb_delete(ef);
712 }
713 else if ( res1 == HT_NEW_ENTRY )
714 {
715 hashtb_delete(ef);
716 }
717 hashtb_end(ef);
718 }
719 else if (res == HT_NEW_ENTRY)
720 {
721 hashtb_delete(e);
722 }
723
724 hashtb_end(e);
725}
726
727int
728delete_old_face_from_npt(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
729{
730 if(flags == CCN_SCHEDULE_CANCEL)
731 {
732 return -1;
733 }
734
735 nlsr_lock();
736
akmhoque1771c412012-11-09 13:06:08 -0600737 if ( nlsr->debugging )
738 printf("delete_old_face_from_npt\n");
739 if ( nlsr->detailed_logging )
740 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_old_face_from_npt\n");
741
akmhoquede61ba92012-09-20 22:19:12 -0500742 if ( ev->evdata != NULL )
akmhoque1771c412012-11-09 13:06:08 -0600743 {
744 if ( nlsr->debugging )
745 printf("Event Data: %s \n",(char *)ev->evdata);
746 if ( nlsr->detailed_logging )
747 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Event Data: %s \n",(char *)ev->evdata);
akmhoquede61ba92012-09-20 22:19:12 -0500748 char *sep="|";
749 char *rem;
750 char *orig_router;
751 char *faceid;
752 int face_id;
753
754 char *face_data=(char *)malloc(strlen((char *)ev->evdata)+1);
755 memset(face_data,0,strlen((char *)ev->evdata)+1);
756 memcpy(face_data+strlen(face_data),(char *)ev->evdata,strlen((char *)ev->evdata));
757
758 orig_router=strtok_r(face_data,sep,&rem);
759 faceid=strtok_r(NULL,sep,&rem);
760 face_id=atoi(faceid);
akmhoque1771c412012-11-09 13:06:08 -0600761
762 if ( nlsr->debugging )
763 printf("Orig Router: %s Face: %d \n",orig_router,face_id);
764 if ( nlsr->detailed_logging )
765 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig Router: %s Face: %d \n",orig_router,face_id);
akmhoquede61ba92012-09-20 22:19:12 -0500766
767 delete_face_from_npt_by_face_id(orig_router,face_id);
768 }
769
770 nlsr_unlock();
771
772 return 0;
773}
774
775void
akmhoque3cced642012-09-24 16:20:20 -0500776clean_old_fib_entries_from_npt(void)
akmhoquede61ba92012-09-20 22:19:12 -0500777{
778
akmhoque1771c412012-11-09 13:06:08 -0600779
780 if ( nlsr->debugging )
781 printf("clean_old_fib_entries_from_npt called\n\n");
782 if ( nlsr->detailed_logging )
783 writeLogg(__FILE__,__FUNCTION__,__LINE__,"clean_old_fib_entries_from_npt called\n\n");
akmhoquede61ba92012-09-20 22:19:12 -0500784 int i, npt_element;
785
786 struct npt_entry *ne;
787
788 struct hashtb_enumerator ee;
789 struct hashtb_enumerator *e = &ee;
790
791 hashtb_start(nlsr->npt, e);
792 npt_element=hashtb_n(nlsr->npt);
793
794 for(i=0;i<npt_element;i++)
795 {
796 ne=e->data;
797
798 int j,k, nl_element,face_list_element;
799 struct face_list_entry *fle;
800
801 struct hashtb_enumerator eef;
802 struct hashtb_enumerator *ef = &eef;
803
804 hashtb_start(ne->face_list, ef);
805 face_list_element=hashtb_n(ne->face_list);
806 if ( face_list_element <= 0 )
807 {
akmhoque1771c412012-11-09 13:06:08 -0600808 if ( nlsr->debugging )
809 printf(" Face: No Face \n");
810 if ( nlsr->detailed_logging )
811 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
812
akmhoquede61ba92012-09-20 22:19:12 -0500813 }
814 else
815 {
816 for(j=0;j<face_list_element;j++)
817 {
818 fle=ef->data;
819 int check=does_face_exist_for_router(ne->orig_router,fle->next_hop_face);
820 if ( check == 0 )
821 {
822 struct name_list_entry *nle;
823 struct hashtb_enumerator eenle;
824 struct hashtb_enumerator *enle = &eenle;
825
826 hashtb_start(ne->name_list, enle);
827 nl_element=hashtb_n(ne->name_list);
828
829 for (k=0;k<nl_element;k++)
830 {
831 nle=enle->data;
832
akmhoque3cced642012-09-24 16:20:20 -0500833 //delete all the fib entries here
akmhoquea30cb772012-10-07 09:50:34 -0500834
akmhoque1771c412012-11-09 13:06:08 -0600835 //printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
836
akmhoque3cced642012-09-24 16:20:20 -0500837 if( is_neighbor(nle->name) == 0 )
838 {
akmhoque1771c412012-11-09 13:06:08 -0600839 if ( nlsr->debugging )
840 printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
841 if ( nlsr->detailed_logging )
842 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
akmhoque3cced642012-09-24 16:20:20 -0500843 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, fle->next_hop_face);
844 }
845
akmhoquede61ba92012-09-20 22:19:12 -0500846
847 hashtb_next(enle);
848 }
849 hashtb_end(enle);
850
851 char faceid[20];
852 memset(faceid,0,20);
853 sprintf(faceid,"%d",fle->next_hop_face);
854 char *evdata=(char *)malloc(strlen(ne->orig_router)+strlen(faceid)+2);
855 memset(evdata,0,strlen(ne->orig_router)+strlen(faceid)+2);
856 memcpy(evdata+strlen(evdata),ne->orig_router,strlen(ne->orig_router));
857 memcpy(evdata+strlen(evdata),"|",1);
858 memcpy(evdata+strlen(evdata),faceid,strlen(faceid));
859
860 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_old_face_from_npt, (void *)evdata, 0);
861
862 }
863
864 hashtb_next(ef);
865 }
866 }
867 hashtb_end(ef);
868
869
870 hashtb_next(e);
871 }
872
873 hashtb_end(e);
874
875}
876
877void
akmhoque3cced642012-09-24 16:20:20 -0500878update_npt_with_new_route(void)
akmhoquede61ba92012-09-20 22:19:12 -0500879{
880 clean_old_fib_entries_from_npt();
881 add_new_fib_entries_to_npt();
akmhoque3cced642012-09-24 16:20:20 -0500882
883 int i, npt_element;
884
885 struct npt_entry *ne;
886
887 struct hashtb_enumerator ee;
888 struct hashtb_enumerator *e = &ee;
889
890 hashtb_start(nlsr->npt, e);
891 npt_element=hashtb_n(nlsr->npt);
892
893 for(i=0;i<npt_element;i++)
894 {
895
896 ne=e->data;
897 update_ccnd_fib_for_orig_router(ne->orig_router);
898 hashtb_next(e);
899 }
900
901 hashtb_end(e);
902}
903
904
905
906void
907sort_faces_by_distance(int *faces,int *route_costs,int start,int element)
908{
909 int i,j;
910 int temp_cost;
911 int temp_face;
912
913 for ( i=start ; i < element ; i ++)
914 {
915 for( j=i+1; j<element; j ++)
916 {
917 if (route_costs[j] < route_costs[i] )
918 {
919 temp_cost=route_costs[j];
920 route_costs[j]=route_costs[i];
921 route_costs[i]=temp_cost;
922
923 temp_face=faces[j];
924 faces[j]=faces[i];
925 faces[i]=temp_face;
926 }
927 }
928 }
929
930}
931
932void
933get_all_faces_for_orig_router_from_npt(char *orig_router, int *faces, int *route_costs, int num_faces)
934{
935
936 int res,face_list_element,j;
937 struct hashtb_enumerator ee;
938 struct hashtb_enumerator *e = &ee;
939
940
941 hashtb_start(nlsr->npt, e);
942 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
943
944 if(res == HT_NEW_ENTRY)
945 {
946 hashtb_delete(e);
947 }
948 else if ( res == HT_OLD_ENTRY )
949 {
950 struct npt_entry *ne;
951 ne=e->data;
952
953 struct face_list_entry *fle;
954
955 struct hashtb_enumerator eef;
956 struct hashtb_enumerator *ef = &eef;
957
958 hashtb_start(ne->face_list, ef);
959 face_list_element=hashtb_n(ne->face_list);
960 for(j=0;j<face_list_element;j++)
961 {
962 fle=ef->data;
963 faces[j]=fle->next_hop_face;
964 route_costs[j]=fle->route_cost;
965 hashtb_next(ef);
966 }
967 hashtb_end(ef);
968
969
970 }
971 hashtb_end(e);
972
973}
974
975void
976destroy_faces_by_orig_router(char *orig_router)
977{
978
979 int res;
980 struct hashtb_enumerator ee;
981 struct hashtb_enumerator *e = &ee;
982
983
984 hashtb_start(nlsr->npt, e);
985 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
986
987 if(res == HT_NEW_ENTRY)
988 {
989 hashtb_delete(e);
990 }
991 else if ( res == HT_OLD_ENTRY )
992 {
993 struct npt_entry *ne;
994 ne=e->data;
995 int num_face=hashtb_n(ne->face_list);
996 int last_face,first_face;
997
998 int *faces=(int *)malloc(num_face*sizeof(int));
999 int *route_costs=(int *)malloc(num_face*sizeof(int));
1000
1001 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
1002 sort_faces_by_distance(faces,route_costs,0,num_face);
1003
1004
1005 first_face=num_face-1;
1006
1007 if ( nlsr->multi_path_face_num == 0 )
1008 {
1009 last_face=first_face;
1010 }
1011 else
1012 {
1013 if ( num_face <= nlsr->multi_path_face_num)
1014 {
1015 last_face=0;
1016 }
1017 else if ( nlsr->multi_path_face_num == 0)
1018 {
1019 last_face=num_face-nlsr->multi_path_face_num;
1020 }
1021 }
1022
1023 int i,j, nl_element;
1024 struct name_list_entry *nle;
1025 struct hashtb_enumerator eenle;
1026 struct hashtb_enumerator *enle = &eenle;
1027
1028 hashtb_start(ne->name_list, enle);
1029 nl_element=hashtb_n(ne->name_list);
1030
1031 for (i=0;i<nl_element;i++)
1032 {
1033 nle=enle->data;
1034
1035 for( j=first_face; j>= last_face; j--)
1036 {
akmhoquea30cb772012-10-07 09:50:34 -05001037 if ( is_neighbor(orig_router) == 0 )
akmhoque3cced642012-09-24 16:20:20 -05001038 {
akmhoque1771c412012-11-09 13:06:08 -06001039 if ( nlsr->debugging )
1040 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1041 if ( nlsr->detailed_logging )
1042 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoquea30cb772012-10-07 09:50:34 -05001043 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
akmhoque3cced642012-09-24 16:20:20 -05001044 }
1045 else
1046 {
akmhoquea30cb772012-10-07 09:50:34 -05001047 if ( j == last_face && is_neighbor(nle->name)==0)
akmhoque3cced642012-09-24 16:20:20 -05001048 {
akmhoque1771c412012-11-09 13:06:08 -06001049 if ( nlsr->debugging )
1050 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1051 if ( nlsr->detailed_logging )
1052 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoque3cced642012-09-24 16:20:20 -05001053 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
1054 }
1055 }
1056 }
1057
1058 hashtb_next(enle);
1059 }
1060 hashtb_end(enle);
1061
1062
1063
1064 free(faces);
1065 free(route_costs);
1066
1067 }
1068 hashtb_end(e);
1069
akmhoquede61ba92012-09-20 22:19:12 -05001070}
1071
akmhoquefbfd0982012-09-09 20:59:03 -05001072void
1073destroy_all_face_by_nlsr(void)
1074{
1075 int i, npt_element;
1076
1077 struct npt_entry *ne;
1078
1079 struct hashtb_enumerator ee;
1080 struct hashtb_enumerator *e = &ee;
1081
1082 hashtb_start(nlsr->npt, e);
1083 npt_element=hashtb_n(nlsr->npt);
1084
1085 for(i=0;i<npt_element;i++)
1086 {
akmhoquefbfd0982012-09-09 20:59:03 -05001087 ne=e->data;
akmhoque3cced642012-09-24 16:20:20 -05001088 destroy_faces_by_orig_router(ne->orig_router);
akmhoquefbfd0982012-09-09 20:59:03 -05001089 hashtb_next(e);
1090 }
1091
1092 hashtb_end(e);
1093
1094 printf("\n");
1095}