blob: 81879b94288412d38b97b2129aa7f8e2c7206d31 [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
akmhoque3171d652012-11-13 11:44:33 -0600497 if ( nlsr->debugging )
498 printf("\n");
499 if ( nlsr->detailed_logging )
500 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoque3560cb62012-09-09 10:52:30 -0500501}
akmhoque810a5b52012-09-09 16:53:14 -0500502
503void
akmhoquede61ba92012-09-20 22:19:12 -0500504delete_orig_router_from_npt(char *orig_router)
akmhoque810a5b52012-09-09 16:53:14 -0500505{
akmhoquede61ba92012-09-20 22:19:12 -0500506 int res,num_face,num_prefix;
akmhoque810a5b52012-09-09 16:53:14 -0500507 struct npt_entry *ne;
508
509 struct hashtb_enumerator ee;
510 struct hashtb_enumerator *e = &ee;
511
512 hashtb_start(nlsr->npt, e);
513 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
514
515 if ( res == HT_OLD_ENTRY )
516 {
517 ne=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500518 num_prefix=hashtb_n(ne->name_list);
519 if ( num_prefix > 0 )
akmhoque810a5b52012-09-09 16:53:14 -0500520 {
akmhoquede61ba92012-09-20 22:19:12 -0500521 num_face=hashtb_n(ne->face_list);
522
523 if ( num_face > 0 )
akmhoque810a5b52012-09-09 16:53:14 -0500524 {
525 int j, nl_element;
526 struct name_list_entry *nle;
527 struct hashtb_enumerator eenle;
528 struct hashtb_enumerator *enle = &eenle;
529
530 hashtb_start(ne->name_list, enle);
531 nl_element=hashtb_n(ne->name_list);
532
533 for (j=0;j<nl_element;j++)
534 {
535 nle=enle->data;
akmhoquede61ba92012-09-20 22:19:12 -0500536
akmhoque3cced642012-09-24 16:20:20 -0500537 delete_npt_entry_by_router_and_name_prefix(ne->orig_router,nle->name);
538
akmhoque810a5b52012-09-09 16:53:14 -0500539 hashtb_next(enle);
540 }
541 hashtb_end(enle);
542
543 }
544
545 }
546 hashtb_destroy(&ne->name_list);
akmhoquede61ba92012-09-20 22:19:12 -0500547 hashtb_destroy(&ne->face_list);
akmhoque810a5b52012-09-09 16:53:14 -0500548 hashtb_delete(e);
549 }
550 else if ( res == HT_NEW_ENTRY )
551 {
552 hashtb_delete(e);
553 }
554 hashtb_end(e);
555}
akmhoquefbfd0982012-09-09 20:59:03 -0500556
akmhoquede61ba92012-09-20 22:19:12 -0500557
558void
559add_face_to_npt_by_face_id(char *dest_router, int face_id, int route_cost)
560{
akmhoque1771c412012-11-09 13:06:08 -0600561 if ( nlsr->debugging )
562 {
563 printf("add_face_to_npt_by_face_id called\n");
564 printf("Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
565 }
566 if ( nlsr->detailed_logging )
567 {
568 writeLogg(__FILE__,__FUNCTION__,__LINE__,"add_face_to_npt_by_face_id called\n");
569 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
570 }
akmhoquede61ba92012-09-20 22:19:12 -0500571
akmhoque1771c412012-11-09 13:06:08 -0600572
akmhoquede61ba92012-09-20 22:19:12 -0500573 int res,res1;
574 struct npt_entry *ne;
575
576 struct hashtb_enumerator ee;
577 struct hashtb_enumerator *e = &ee;
578
579 hashtb_start(nlsr->npt, e);
580 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
581
582 if ( res == HT_OLD_ENTRY )
583 {
akmhoque1771c412012-11-09 13:06:08 -0600584 if ( nlsr->debugging )
585 printf("Dest Router Found \n");
586 if ( nlsr->detailed_logging )
587 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest Router Found \n");
588
akmhoquede61ba92012-09-20 22:19:12 -0500589 ne=e->data;
590
591 struct hashtb_enumerator eef;
592 struct hashtb_enumerator *ef = &eef;
593
594 hashtb_start(ne->face_list, ef);
595 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
596
597 if ( res1 == HT_OLD_ENTRY )
598 {
akmhoque1771c412012-11-09 13:06:08 -0600599 if ( nlsr->debugging )
600 printf("Face Found \n");
601 if ( nlsr->detailed_logging )
602 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face Found \n");
akmhoquede61ba92012-09-20 22:19:12 -0500603 struct face_list_entry *fle;//=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
604 fle=ef->data;
605 fle->next_hop_face=face_id;
606 fle->route_cost=route_cost;
607 }
608 else if ( res1 == HT_NEW_ENTRY )
609 {
akmhoque1771c412012-11-09 13:06:08 -0600610 if ( nlsr->debugging )
611 printf("Face Not Found \n");
612 if ( nlsr->detailed_logging )
613 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face Not Found \n");
akmhoquede61ba92012-09-20 22:19:12 -0500614 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
615 fle=ef->data;
616 fle->next_hop_face=face_id;
617 fle->route_cost=route_cost;
618 //hashtb_delete(ef);
619 }
620 hashtb_end(ef);
621 }
622 else if (res == HT_NEW_ENTRY)
623 {
624 hashtb_delete(e);
625 }
626
627 hashtb_end(e);
628}
629
630
631void
akmhoque3cced642012-09-24 16:20:20 -0500632add_new_fib_entries_to_npt(void)
akmhoquede61ba92012-09-20 22:19:12 -0500633{
akmhoque1771c412012-11-09 13:06:08 -0600634 if ( nlsr->debugging )
635 printf("add_new_fib_entries_to_npt called\n");
636 if ( nlsr->detailed_logging )
637 writeLogg(__FILE__,__FUNCTION__,__LINE__,"add_new_fib_entries_to_npt called\n");
akmhoquede61ba92012-09-20 22:19:12 -0500638 int i,j, rt_element,face_list_element;
639
640 struct routing_table_entry *rte;
641
642 struct hashtb_enumerator ee;
643 struct hashtb_enumerator *e = &ee;
644
645 hashtb_start(nlsr->routing_table, e);
646 rt_element=hashtb_n(nlsr->routing_table);
647
648 for(i=0;i<rt_element;i++)
649 {
akmhoquede61ba92012-09-20 22:19:12 -0500650 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500651
652 struct face_list_entry *fle;
653
654 struct hashtb_enumerator eef;
655 struct hashtb_enumerator *ef = &eef;
656
657 hashtb_start(rte->face_list, ef);
658 face_list_element=hashtb_n(rte->face_list);
659 if ( face_list_element <= 0 )
660 {
akmhoque1771c412012-11-09 13:06:08 -0600661 if ( nlsr->debugging )
662 printf(" Face: No Face \n");
663 if ( nlsr->detailed_logging )
664 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
akmhoquede61ba92012-09-20 22:19:12 -0500665 }
666 else
667 {
668 for(j=0;j<face_list_element;j++)
669 {
670 fle=ef->data;
akmhoquede61ba92012-09-20 22:19:12 -0500671 add_face_to_npt_by_face_id(rte->dest_router,fle->next_hop_face,fle->route_cost);
672 hashtb_next(ef);
673 }
674 }
675 hashtb_end(ef);
676
677 hashtb_next(e);
678 }
679
680 hashtb_end(e);
681
682}
683
684
685void
686delete_face_from_npt_by_face_id(char *dest_router, int face_id)
687{
akmhoque1771c412012-11-09 13:06:08 -0600688 if ( nlsr->debugging )
689 printf("delete_face_from_npt_by_face_id\n");
690 if ( nlsr->detailed_logging )
691 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_face_from_npt_by_face_id\n");
akmhoquede61ba92012-09-20 22:19:12 -0500692
693 int res,res1;
694 struct npt_entry *ne;
695
696 struct hashtb_enumerator ee;
697 struct hashtb_enumerator *e = &ee;
698
699 hashtb_start(nlsr->npt, e);
700 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
701
702 if ( res == HT_OLD_ENTRY )
703 {
704 ne=e->data;
705
706 struct hashtb_enumerator eef;
707 struct hashtb_enumerator *ef = &eef;
708
709 hashtb_start(ne->face_list, ef);
710 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
711
712 if ( res1 == HT_OLD_ENTRY )
713 {
714 hashtb_delete(ef);
715 }
716 else if ( res1 == HT_NEW_ENTRY )
717 {
718 hashtb_delete(ef);
719 }
720 hashtb_end(ef);
721 }
722 else if (res == HT_NEW_ENTRY)
723 {
724 hashtb_delete(e);
725 }
726
727 hashtb_end(e);
728}
729
730int
731delete_old_face_from_npt(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
732{
733 if(flags == CCN_SCHEDULE_CANCEL)
734 {
735 return -1;
736 }
737
738 nlsr_lock();
739
akmhoque1771c412012-11-09 13:06:08 -0600740 if ( nlsr->debugging )
741 printf("delete_old_face_from_npt\n");
742 if ( nlsr->detailed_logging )
743 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_old_face_from_npt\n");
744
akmhoquede61ba92012-09-20 22:19:12 -0500745 if ( ev->evdata != NULL )
akmhoque1771c412012-11-09 13:06:08 -0600746 {
747 if ( nlsr->debugging )
748 printf("Event Data: %s \n",(char *)ev->evdata);
749 if ( nlsr->detailed_logging )
750 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Event Data: %s \n",(char *)ev->evdata);
akmhoquede61ba92012-09-20 22:19:12 -0500751 char *sep="|";
752 char *rem;
753 char *orig_router;
754 char *faceid;
755 int face_id;
756
757 char *face_data=(char *)malloc(strlen((char *)ev->evdata)+1);
758 memset(face_data,0,strlen((char *)ev->evdata)+1);
759 memcpy(face_data+strlen(face_data),(char *)ev->evdata,strlen((char *)ev->evdata));
760
761 orig_router=strtok_r(face_data,sep,&rem);
762 faceid=strtok_r(NULL,sep,&rem);
763 face_id=atoi(faceid);
akmhoque1771c412012-11-09 13:06:08 -0600764
765 if ( nlsr->debugging )
766 printf("Orig Router: %s Face: %d \n",orig_router,face_id);
767 if ( nlsr->detailed_logging )
768 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig Router: %s Face: %d \n",orig_router,face_id);
akmhoquede61ba92012-09-20 22:19:12 -0500769
770 delete_face_from_npt_by_face_id(orig_router,face_id);
771 }
772
773 nlsr_unlock();
774
775 return 0;
776}
777
778void
akmhoque3cced642012-09-24 16:20:20 -0500779clean_old_fib_entries_from_npt(void)
akmhoquede61ba92012-09-20 22:19:12 -0500780{
781
akmhoque1771c412012-11-09 13:06:08 -0600782
783 if ( nlsr->debugging )
784 printf("clean_old_fib_entries_from_npt called\n\n");
785 if ( nlsr->detailed_logging )
786 writeLogg(__FILE__,__FUNCTION__,__LINE__,"clean_old_fib_entries_from_npt called\n\n");
akmhoquede61ba92012-09-20 22:19:12 -0500787 int i, npt_element;
788
789 struct npt_entry *ne;
790
791 struct hashtb_enumerator ee;
792 struct hashtb_enumerator *e = &ee;
793
794 hashtb_start(nlsr->npt, e);
795 npt_element=hashtb_n(nlsr->npt);
796
797 for(i=0;i<npt_element;i++)
798 {
799 ne=e->data;
800
801 int j,k, nl_element,face_list_element;
802 struct face_list_entry *fle;
803
804 struct hashtb_enumerator eef;
805 struct hashtb_enumerator *ef = &eef;
806
807 hashtb_start(ne->face_list, ef);
808 face_list_element=hashtb_n(ne->face_list);
809 if ( face_list_element <= 0 )
810 {
akmhoque1771c412012-11-09 13:06:08 -0600811 if ( nlsr->debugging )
812 printf(" Face: No Face \n");
813 if ( nlsr->detailed_logging )
814 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
815
akmhoquede61ba92012-09-20 22:19:12 -0500816 }
817 else
818 {
819 for(j=0;j<face_list_element;j++)
820 {
821 fle=ef->data;
822 int check=does_face_exist_for_router(ne->orig_router,fle->next_hop_face);
823 if ( check == 0 )
824 {
825 struct name_list_entry *nle;
826 struct hashtb_enumerator eenle;
827 struct hashtb_enumerator *enle = &eenle;
828
829 hashtb_start(ne->name_list, enle);
830 nl_element=hashtb_n(ne->name_list);
831
832 for (k=0;k<nl_element;k++)
833 {
834 nle=enle->data;
835
akmhoque3cced642012-09-24 16:20:20 -0500836 //delete all the fib entries here
akmhoquea30cb772012-10-07 09:50:34 -0500837
akmhoque1771c412012-11-09 13:06:08 -0600838 //printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
839
akmhoque3cced642012-09-24 16:20:20 -0500840 if( is_neighbor(nle->name) == 0 )
841 {
akmhoque1771c412012-11-09 13:06:08 -0600842 if ( nlsr->debugging )
843 printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
844 if ( nlsr->detailed_logging )
845 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
akmhoque3cced642012-09-24 16:20:20 -0500846 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, fle->next_hop_face);
847 }
848
akmhoquede61ba92012-09-20 22:19:12 -0500849
850 hashtb_next(enle);
851 }
852 hashtb_end(enle);
853
854 char faceid[20];
855 memset(faceid,0,20);
856 sprintf(faceid,"%d",fle->next_hop_face);
857 char *evdata=(char *)malloc(strlen(ne->orig_router)+strlen(faceid)+2);
858 memset(evdata,0,strlen(ne->orig_router)+strlen(faceid)+2);
859 memcpy(evdata+strlen(evdata),ne->orig_router,strlen(ne->orig_router));
860 memcpy(evdata+strlen(evdata),"|",1);
861 memcpy(evdata+strlen(evdata),faceid,strlen(faceid));
862
863 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_old_face_from_npt, (void *)evdata, 0);
864
865 }
866
867 hashtb_next(ef);
868 }
869 }
870 hashtb_end(ef);
871
872
873 hashtb_next(e);
874 }
875
876 hashtb_end(e);
877
878}
879
880void
akmhoque3cced642012-09-24 16:20:20 -0500881update_npt_with_new_route(void)
akmhoquede61ba92012-09-20 22:19:12 -0500882{
883 clean_old_fib_entries_from_npt();
884 add_new_fib_entries_to_npt();
akmhoque3cced642012-09-24 16:20:20 -0500885
886 int i, npt_element;
887
888 struct npt_entry *ne;
889
890 struct hashtb_enumerator ee;
891 struct hashtb_enumerator *e = &ee;
892
893 hashtb_start(nlsr->npt, e);
894 npt_element=hashtb_n(nlsr->npt);
895
896 for(i=0;i<npt_element;i++)
897 {
898
899 ne=e->data;
900 update_ccnd_fib_for_orig_router(ne->orig_router);
901 hashtb_next(e);
902 }
903
904 hashtb_end(e);
905}
906
907
908
909void
910sort_faces_by_distance(int *faces,int *route_costs,int start,int element)
911{
912 int i,j;
913 int temp_cost;
914 int temp_face;
915
916 for ( i=start ; i < element ; i ++)
917 {
918 for( j=i+1; j<element; j ++)
919 {
920 if (route_costs[j] < route_costs[i] )
921 {
922 temp_cost=route_costs[j];
923 route_costs[j]=route_costs[i];
924 route_costs[i]=temp_cost;
925
926 temp_face=faces[j];
927 faces[j]=faces[i];
928 faces[i]=temp_face;
929 }
930 }
931 }
932
933}
934
935void
936get_all_faces_for_orig_router_from_npt(char *orig_router, int *faces, int *route_costs, int num_faces)
937{
938
939 int res,face_list_element,j;
940 struct hashtb_enumerator ee;
941 struct hashtb_enumerator *e = &ee;
942
943
944 hashtb_start(nlsr->npt, e);
945 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
946
947 if(res == HT_NEW_ENTRY)
948 {
949 hashtb_delete(e);
950 }
951 else if ( res == HT_OLD_ENTRY )
952 {
953 struct npt_entry *ne;
954 ne=e->data;
955
956 struct face_list_entry *fle;
957
958 struct hashtb_enumerator eef;
959 struct hashtb_enumerator *ef = &eef;
960
961 hashtb_start(ne->face_list, ef);
962 face_list_element=hashtb_n(ne->face_list);
963 for(j=0;j<face_list_element;j++)
964 {
965 fle=ef->data;
966 faces[j]=fle->next_hop_face;
967 route_costs[j]=fle->route_cost;
968 hashtb_next(ef);
969 }
970 hashtb_end(ef);
971
972
973 }
974 hashtb_end(e);
975
976}
977
978void
979destroy_faces_by_orig_router(char *orig_router)
980{
981
982 int res;
983 struct hashtb_enumerator ee;
984 struct hashtb_enumerator *e = &ee;
985
986
987 hashtb_start(nlsr->npt, e);
988 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
989
990 if(res == HT_NEW_ENTRY)
991 {
992 hashtb_delete(e);
993 }
994 else if ( res == HT_OLD_ENTRY )
995 {
996 struct npt_entry *ne;
997 ne=e->data;
998 int num_face=hashtb_n(ne->face_list);
999 int last_face,first_face;
1000
1001 int *faces=(int *)malloc(num_face*sizeof(int));
1002 int *route_costs=(int *)malloc(num_face*sizeof(int));
1003
1004 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
1005 sort_faces_by_distance(faces,route_costs,0,num_face);
1006
1007
1008 first_face=num_face-1;
1009
1010 if ( nlsr->multi_path_face_num == 0 )
1011 {
1012 last_face=first_face;
1013 }
1014 else
1015 {
1016 if ( num_face <= nlsr->multi_path_face_num)
1017 {
1018 last_face=0;
1019 }
1020 else if ( nlsr->multi_path_face_num == 0)
1021 {
1022 last_face=num_face-nlsr->multi_path_face_num;
1023 }
1024 }
1025
1026 int i,j, nl_element;
1027 struct name_list_entry *nle;
1028 struct hashtb_enumerator eenle;
1029 struct hashtb_enumerator *enle = &eenle;
1030
1031 hashtb_start(ne->name_list, enle);
1032 nl_element=hashtb_n(ne->name_list);
1033
1034 for (i=0;i<nl_element;i++)
1035 {
1036 nle=enle->data;
1037
1038 for( j=first_face; j>= last_face; j--)
1039 {
akmhoquea30cb772012-10-07 09:50:34 -05001040 if ( is_neighbor(orig_router) == 0 )
akmhoque3cced642012-09-24 16:20:20 -05001041 {
akmhoque1771c412012-11-09 13:06:08 -06001042 if ( nlsr->debugging )
1043 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1044 if ( nlsr->detailed_logging )
1045 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoquea30cb772012-10-07 09:50:34 -05001046 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
akmhoque3cced642012-09-24 16:20:20 -05001047 }
1048 else
1049 {
akmhoquea30cb772012-10-07 09:50:34 -05001050 if ( j == last_face && is_neighbor(nle->name)==0)
akmhoque3cced642012-09-24 16:20:20 -05001051 {
akmhoque1771c412012-11-09 13:06:08 -06001052 if ( nlsr->debugging )
1053 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1054 if ( nlsr->detailed_logging )
1055 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoque3cced642012-09-24 16:20:20 -05001056 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
1057 }
1058 }
1059 }
1060
1061 hashtb_next(enle);
1062 }
1063 hashtb_end(enle);
1064
1065
1066
1067 free(faces);
1068 free(route_costs);
1069
1070 }
1071 hashtb_end(e);
1072
akmhoquede61ba92012-09-20 22:19:12 -05001073}
1074
akmhoquefbfd0982012-09-09 20:59:03 -05001075void
1076destroy_all_face_by_nlsr(void)
1077{
1078 int i, npt_element;
1079
1080 struct npt_entry *ne;
1081
1082 struct hashtb_enumerator ee;
1083 struct hashtb_enumerator *e = &ee;
1084
1085 hashtb_start(nlsr->npt, e);
1086 npt_element=hashtb_n(nlsr->npt);
1087
1088 for(i=0;i<npt_element;i++)
1089 {
akmhoquefbfd0982012-09-09 20:59:03 -05001090 ne=e->data;
akmhoque3cced642012-09-24 16:20:20 -05001091 destroy_faces_by_orig_router(ne->orig_router);
akmhoquefbfd0982012-09-09 20:59:03 -05001092 hashtb_next(e);
1093 }
1094
1095 hashtb_end(e);
1096
akmhoque3171d652012-11-13 11:44:33 -06001097 if ( nlsr->debugging )
1098 printf("\n");
1099 if ( nlsr->detailed_logging )
1100 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoquefbfd0982012-09-09 20:59:03 -05001101}