blob: fb167f5f0e0c975619e6ee15045d2844e0116e02 [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
akmhoqueb77b95f2013-02-08 12:28:47 -0600180 if ( nlsr->debugging )
181 {
182 printf("update_ccnd_fib_for_orig_router called\n");
183 }
184
akmhoque3cced642012-09-24 16:20:20 -0500185 int res;
186 struct hashtb_enumerator ee;
187 struct hashtb_enumerator *e = &ee;
188
189
190 hashtb_start(nlsr->npt, e);
191 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
192
193 if(res == HT_NEW_ENTRY)
194 {
195 hashtb_delete(e);
196 }
197 else if ( res == HT_OLD_ENTRY )
198 {
199 struct npt_entry *ne;
200 ne=e->data;
201 int num_face=hashtb_n(ne->face_list);
202 int last_face,first_face;
203
204 int *faces=(int *)malloc(num_face*sizeof(int));
205 int *route_costs=(int *)malloc(num_face*sizeof(int));
206
207 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
208 sort_faces_by_distance(faces,route_costs,0,num_face);
209
akmhoqueb77b95f2013-02-08 12:28:47 -0600210 if ( nlsr->debugging )
akmhoque3cced642012-09-24 16:20:20 -0500211 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600212 int m;
213 for ( m =0 ; m< num_face ; m++)
214 {
215 printf("Dest_router: %s Next_Hop_Face: %d Route_cost: %d \n",orig_router,faces[m],route_costs[m]);
216 }
akmhoque3cced642012-09-24 16:20:20 -0500217 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600218
219 last_face=0;
220 if ( nlsr->max_faces_per_prefix == 0) // add all faces available in routing table
akmhoque3cced642012-09-24 16:20:20 -0500221 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600222 first_face=num_face-1;
223 }
224 else if( nlsr->max_faces_per_prefix > 0)
225 {
226 if ( nlsr->max_faces_per_prefix >= num_face)
akmhoque3cced642012-09-24 16:20:20 -0500227 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600228 first_face=num_face-1;
akmhoque3cced642012-09-24 16:20:20 -0500229 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600230 else if ( nlsr->max_faces_per_prefix < num_face)
akmhoque3cced642012-09-24 16:20:20 -0500231 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600232 first_face=nlsr->max_faces_per_prefix-1;
akmhoque3cced642012-09-24 16:20:20 -0500233 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600234
akmhoque3cced642012-09-24 16:20:20 -0500235 }
236
237 int i,j, nl_element;
238 struct name_list_entry *nle;
239 struct hashtb_enumerator eenle;
240 struct hashtb_enumerator *enle = &eenle;
241
242 hashtb_start(ne->name_list, enle);
243 nl_element=hashtb_n(ne->name_list);
244
245 for (i=0;i<nl_element;i++)
246 {
247 nle=enle->data;
248
249 for( j=first_face; j>= last_face; j--)
250 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600251 if ( nlsr->debugging )
252 {
253 printf("Possible FIB Entry name: %s face: %d route cost: %d \n",nle->name,faces[j],route_costs[j]);
254 }
255 if ( is_active_neighbor(orig_router) == 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]);
akmhoquea30cb772012-10-07 09:50:34 -0500261 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500262 }
263 else
264 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600265 if ( j == last_face && is_active_neighbor(nle->name)==0)
akmhoque3cced642012-09-24 16:20:20 -0500266 {
akmhoque1771c412012-11-09 13:06:08 -0600267 if ( nlsr->debugging )
268 printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
269 if ( nlsr->detailed_logging )
270 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500271 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]);
272 }
273 }
274 }
akmhoquea30cb772012-10-07 09:50:34 -0500275
276
akmhoque3cced642012-09-24 16:20:20 -0500277 hashtb_next(enle);
278 }
279 hashtb_end(enle);
280
281
282
283 free(faces);
284 free(route_costs);
285
286 }
287 hashtb_end(e);
288
289}
290
akmhoqueffacaa82012-09-13 17:48:30 -0500291int
akmhoque3cced642012-09-24 16:20:20 -0500292delete_npt_entry_by_router_and_name_prefix(char *orig_router, char *name_prefix)
akmhoqueffacaa82012-09-13 17:48:30 -0500293{
294 if ( strcmp(orig_router,nlsr->router_name)== 0)
295 {
296 return -1;
297 }
298
299 struct npt_entry *ne;
300
301 int res,res_nle;
302 struct hashtb_enumerator ee;
303 struct hashtb_enumerator *e = &ee;
304
305
306 hashtb_start(nlsr->npt, e);
307 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
308
309 if(res == HT_NEW_ENTRY)
310 {
311 hashtb_delete(e);
312 return -1;
313 }
314 else if (res == HT_OLD_ENTRY)
315 {
316 ne=e->data;
317
318 struct hashtb_enumerator eenle;
319 struct hashtb_enumerator *enle = &eenle;
320
321 hashtb_start(ne->name_list, enle);
322 res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
323
324 if(res_nle == HT_NEW_ENTRY )
325 {
326 hashtb_delete(enle);
327 }
328 else if(res_nle == HT_OLD_ENTRY )
329 {
akmhoque3cced642012-09-24 16:20:20 -0500330 struct name_list_entry *nle;
331
332 nle=enle->data;
333
334 int j;
335 int num_face=hashtb_n(ne->face_list);
336 int last_face,first_face;
337
338 int *faces=(int *)malloc(num_face*sizeof(int));
339 int *route_costs=(int *)malloc(num_face*sizeof(int));
340
341 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
342 sort_faces_by_distance(faces,route_costs,0,num_face);
343
344
akmhoqueb77b95f2013-02-08 12:28:47 -0600345 last_face=0;
346 if ( nlsr->max_faces_per_prefix == 0) // add all faces available in routing table
akmhoqueffacaa82012-09-13 17:48:30 -0500347 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600348 first_face=num_face-1;
akmhoqueffacaa82012-09-13 17:48:30 -0500349 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600350 else if( nlsr->max_faces_per_prefix > 0)
akmhoque3cced642012-09-24 16:20:20 -0500351 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600352 if ( nlsr->max_faces_per_prefix >= num_face)
akmhoque3cced642012-09-24 16:20:20 -0500353 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600354 first_face=num_face-1;
akmhoque3cced642012-09-24 16:20:20 -0500355 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600356 else if ( nlsr->max_faces_per_prefix < num_face)
akmhoque3cced642012-09-24 16:20:20 -0500357 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600358 first_face=nlsr->max_faces_per_prefix-1;
akmhoque3cced642012-09-24 16:20:20 -0500359 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600360
361 }
akmhoque3cced642012-09-24 16:20:20 -0500362 for( j=first_face; j>= last_face; j--)
363 {
akmhoquea30cb772012-10-07 09:50:34 -0500364
akmhoqueb77b95f2013-02-08 12:28:47 -0600365 if ( is_active_neighbor(orig_router) == 0 )
akmhoque3cced642012-09-24 16:20:20 -0500366 {
akmhoque1771c412012-11-09 13:06:08 -0600367 if ( nlsr->debugging )
368 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
369 if ( nlsr->detailed_logging )
370 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoquea30cb772012-10-07 09:50:34 -0500371 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500372 }
373 else
374 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600375 if ( j == last_face && is_active_neighbor(nle->name)==0)
akmhoque3cced642012-09-24 16:20:20 -0500376 {
akmhoque1771c412012-11-09 13:06:08 -0600377 if ( nlsr->debugging )
378 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
379 if ( nlsr->detailed_logging )
380 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoque3cced642012-09-24 16:20:20 -0500381 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
382 }
383 }
akmhoquea30cb772012-10-07 09:50:34 -0500384
akmhoque3cced642012-09-24 16:20:20 -0500385 }
386
akmhoqueffacaa82012-09-13 17:48:30 -0500387 }
388
389 hashtb_end(enle);
390 }
391
392 hashtb_end(e);
393
394 return 0;
395}
akmhoque3560cb62012-09-09 10:52:30 -0500396
397void
398print_npt(void)
399{
akmhoque1771c412012-11-09 13:06:08 -0600400
401 if ( nlsr->debugging )
402 {
403 printf("\n");
404 printf("print_npt called\n");
405 }
406 if ( nlsr->detailed_logging )
407 {
408 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
409 writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_npt called\n");
410 }
akmhoque3560cb62012-09-09 10:52:30 -0500411 int i, npt_element;
412
413 struct npt_entry *ne;
414
415 struct hashtb_enumerator ee;
416 struct hashtb_enumerator *e = &ee;
417
418 hashtb_start(nlsr->npt, e);
419 npt_element=hashtb_n(nlsr->npt);
420
421 for(i=0;i<npt_element;i++)
422 {
akmhoque1771c412012-11-09 13:06:08 -0600423 if ( nlsr->debugging )
424 {
425 printf("\n");
426 printf("----------NPT ENTRY %d------------------\n",i+1);
427 }
428 if ( nlsr->detailed_logging )
429 {
430 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
431 writeLogg(__FILE__,__FUNCTION__,__LINE__,"----------NPT ENTRY %d------------------\n",i+1);
432 }
akmhoque3560cb62012-09-09 10:52:30 -0500433 ne=e->data;
akmhoque1771c412012-11-09 13:06:08 -0600434 if ( nlsr->debugging )
435 printf(" Origination Router: %s \n",ne->orig_router);
436 if ( nlsr->detailed_logging )
437 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s \n",ne->orig_router);
akmhoque3560cb62012-09-09 10:52:30 -0500438
akmhoquede61ba92012-09-20 22:19:12 -0500439 int j, nl_element,face_list_element;
akmhoque810a5b52012-09-09 16:53:14 -0500440 struct name_list_entry *nle;
441 struct hashtb_enumerator eenle;
442 struct hashtb_enumerator *enle = &eenle;
akmhoque3560cb62012-09-09 10:52:30 -0500443
akmhoque810a5b52012-09-09 16:53:14 -0500444 hashtb_start(ne->name_list, enle);
445 nl_element=hashtb_n(ne->name_list);
akmhoque3560cb62012-09-09 10:52:30 -0500446
akmhoque810a5b52012-09-09 16:53:14 -0500447 for (j=0;j<nl_element;j++)
akmhoque3560cb62012-09-09 10:52:30 -0500448 {
akmhoque810a5b52012-09-09 16:53:14 -0500449 nle=enle->data;
akmhoque1771c412012-11-09 13:06:08 -0600450 if ( nlsr->debugging )
451 printf(" Name Prefix: %s \n",nle->name);
452 if ( nlsr->detailed_logging )
453 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix: %s \n",nle->name);
akmhoque810a5b52012-09-09 16:53:14 -0500454 hashtb_next(enle);
akmhoque3560cb62012-09-09 10:52:30 -0500455 }
akmhoque810a5b52012-09-09 16:53:14 -0500456 hashtb_end(enle);
akmhoquede61ba92012-09-20 22:19:12 -0500457
458 struct face_list_entry *fle;
459
460 struct hashtb_enumerator eef;
461 struct hashtb_enumerator *ef = &eef;
462
463 hashtb_start(ne->face_list, ef);
464 face_list_element=hashtb_n(ne->face_list);
465 if ( face_list_element <= 0 )
466 {
akmhoque1771c412012-11-09 13:06:08 -0600467 if ( nlsr->debugging )
468 printf(" Face: No Face \n");
469 if ( nlsr->detailed_logging )
470 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
471
akmhoquede61ba92012-09-20 22:19:12 -0500472 }
473 else
474 {
475 for(j=0;j<face_list_element;j++)
476 {
477 fle=ef->data;
akmhoque1771c412012-11-09 13:06:08 -0600478 if ( nlsr->debugging )
akmhoqueb77b95f2013-02-08 12:28:47 -0600479 printf(" Face: %d Route_Cost: %f \n",fle->next_hop_face,fle->route_cost);
akmhoque1771c412012-11-09 13:06:08 -0600480 if ( nlsr->detailed_logging )
481 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
akmhoquede61ba92012-09-20 22:19:12 -0500482 hashtb_next(ef);
483 }
484 }
485 hashtb_end(ef);
486
akmhoque3560cb62012-09-09 10:52:30 -0500487
488 hashtb_next(e);
489 }
490
491 hashtb_end(e);
492
akmhoque3171d652012-11-13 11:44:33 -0600493 if ( nlsr->debugging )
494 printf("\n");
495 if ( nlsr->detailed_logging )
496 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoque3560cb62012-09-09 10:52:30 -0500497}
akmhoque810a5b52012-09-09 16:53:14 -0500498
499void
akmhoquede61ba92012-09-20 22:19:12 -0500500delete_orig_router_from_npt(char *orig_router)
akmhoque810a5b52012-09-09 16:53:14 -0500501{
akmhoquede61ba92012-09-20 22:19:12 -0500502 int res,num_face,num_prefix;
akmhoque810a5b52012-09-09 16:53:14 -0500503 struct npt_entry *ne;
504
505 struct hashtb_enumerator ee;
506 struct hashtb_enumerator *e = &ee;
507
508 hashtb_start(nlsr->npt, e);
509 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
510
511 if ( res == HT_OLD_ENTRY )
512 {
513 ne=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500514 num_prefix=hashtb_n(ne->name_list);
515 if ( num_prefix > 0 )
akmhoque810a5b52012-09-09 16:53:14 -0500516 {
akmhoquede61ba92012-09-20 22:19:12 -0500517 num_face=hashtb_n(ne->face_list);
518
519 if ( num_face > 0 )
akmhoque810a5b52012-09-09 16:53:14 -0500520 {
521 int j, nl_element;
522 struct name_list_entry *nle;
523 struct hashtb_enumerator eenle;
524 struct hashtb_enumerator *enle = &eenle;
525
526 hashtb_start(ne->name_list, enle);
527 nl_element=hashtb_n(ne->name_list);
528
529 for (j=0;j<nl_element;j++)
530 {
531 nle=enle->data;
akmhoque3cced642012-09-24 16:20:20 -0500532 delete_npt_entry_by_router_and_name_prefix(ne->orig_router,nle->name);
akmhoque810a5b52012-09-09 16:53:14 -0500533 hashtb_next(enle);
534 }
535 hashtb_end(enle);
536
537 }
538
539 }
540 hashtb_destroy(&ne->name_list);
akmhoquede61ba92012-09-20 22:19:12 -0500541 hashtb_destroy(&ne->face_list);
akmhoque810a5b52012-09-09 16:53:14 -0500542 hashtb_delete(e);
543 }
544 else if ( res == HT_NEW_ENTRY )
545 {
546 hashtb_delete(e);
547 }
548 hashtb_end(e);
549}
akmhoquefbfd0982012-09-09 20:59:03 -0500550
akmhoquede61ba92012-09-20 22:19:12 -0500551
552void
553add_face_to_npt_by_face_id(char *dest_router, int face_id, int route_cost)
554{
akmhoque1771c412012-11-09 13:06:08 -0600555 if ( nlsr->debugging )
556 {
557 printf("add_face_to_npt_by_face_id called\n");
558 printf("Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
559 }
560 if ( nlsr->detailed_logging )
561 {
562 writeLogg(__FILE__,__FUNCTION__,__LINE__,"add_face_to_npt_by_face_id called\n");
563 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
564 }
akmhoquede61ba92012-09-20 22:19:12 -0500565
akmhoque1771c412012-11-09 13:06:08 -0600566
akmhoquede61ba92012-09-20 22:19:12 -0500567 int res,res1;
568 struct npt_entry *ne;
569
570 struct hashtb_enumerator ee;
571 struct hashtb_enumerator *e = &ee;
572
573 hashtb_start(nlsr->npt, e);
574 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
575
576 if ( res == HT_OLD_ENTRY )
577 {
akmhoque1771c412012-11-09 13:06:08 -0600578 if ( nlsr->debugging )
579 printf("Dest Router Found \n");
580 if ( nlsr->detailed_logging )
581 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest Router Found \n");
582
akmhoquede61ba92012-09-20 22:19:12 -0500583 ne=e->data;
584
585 struct hashtb_enumerator eef;
586 struct hashtb_enumerator *ef = &eef;
587
588 hashtb_start(ne->face_list, ef);
589 res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
590
591 if ( res1 == HT_OLD_ENTRY )
592 {
akmhoque1771c412012-11-09 13:06:08 -0600593 if ( nlsr->debugging )
594 printf("Face Found \n");
595 if ( nlsr->detailed_logging )
596 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face Found \n");
akmhoqueb77b95f2013-02-08 12:28:47 -0600597 struct face_list_entry *fle;
akmhoquede61ba92012-09-20 22:19:12 -0500598 fle=ef->data;
599 fle->next_hop_face=face_id;
600 fle->route_cost=route_cost;
601 }
602 else if ( res1 == HT_NEW_ENTRY )
603 {
akmhoque1771c412012-11-09 13:06:08 -0600604 if ( nlsr->debugging )
605 printf("Face Not Found \n");
606 if ( nlsr->detailed_logging )
607 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face Not Found \n");
akmhoquede61ba92012-09-20 22:19:12 -0500608 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
609 fle=ef->data;
610 fle->next_hop_face=face_id;
611 fle->route_cost=route_cost;
akmhoqueb77b95f2013-02-08 12:28:47 -0600612
613
614
615
akmhoquede61ba92012-09-20 22:19:12 -0500616 }
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 )
akmhoqueb77b95f2013-02-08 12:28:47 -0600659 printf(" Face: No Face \n");
akmhoque1771c412012-11-09 13:06:08 -0600660 if ( nlsr->detailed_logging )
akmhoqueb77b95f2013-02-08 12:28:47 -0600661 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);
akmhoquede61ba92012-09-20 22:19:12 -0500708 if ( res1 == HT_OLD_ENTRY )
709 {
710 hashtb_delete(ef);
711 }
712 else if ( res1 == HT_NEW_ENTRY )
713 {
714 hashtb_delete(ef);
715 }
716 hashtb_end(ef);
717 }
718 else if (res == HT_NEW_ENTRY)
719 {
720 hashtb_delete(e);
721 }
722
723 hashtb_end(e);
724}
725
akmhoquede61ba92012-09-20 22:19:12 -0500726
727void
akmhoque3cced642012-09-24 16:20:20 -0500728clean_old_fib_entries_from_npt(void)
akmhoquede61ba92012-09-20 22:19:12 -0500729{
730
akmhoque1771c412012-11-09 13:06:08 -0600731
732 if ( nlsr->debugging )
733 printf("clean_old_fib_entries_from_npt called\n\n");
734 if ( nlsr->detailed_logging )
735 writeLogg(__FILE__,__FUNCTION__,__LINE__,"clean_old_fib_entries_from_npt called\n\n");
akmhoquede61ba92012-09-20 22:19:12 -0500736 int i, npt_element;
737
738 struct npt_entry *ne;
739
740 struct hashtb_enumerator ee;
741 struct hashtb_enumerator *e = &ee;
742
743 hashtb_start(nlsr->npt, e);
744 npt_element=hashtb_n(nlsr->npt);
745
746 for(i=0;i<npt_element;i++)
747 {
748 ne=e->data;
749
750 int j,k, nl_element,face_list_element;
751 struct face_list_entry *fle;
752
753 struct hashtb_enumerator eef;
754 struct hashtb_enumerator *ef = &eef;
755
756 hashtb_start(ne->face_list, ef);
757 face_list_element=hashtb_n(ne->face_list);
758 if ( face_list_element <= 0 )
759 {
akmhoque1771c412012-11-09 13:06:08 -0600760 if ( nlsr->debugging )
akmhoqueb77b95f2013-02-08 12:28:47 -0600761 printf(" Face: No Face \n");
akmhoque1771c412012-11-09 13:06:08 -0600762 if ( nlsr->detailed_logging )
763 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
764
akmhoquede61ba92012-09-20 22:19:12 -0500765 }
766 else
767 {
768 for(j=0;j<face_list_element;j++)
769 {
770 fle=ef->data;
771 int check=does_face_exist_for_router(ne->orig_router,fle->next_hop_face);
772 if ( check == 0 )
773 {
774 struct name_list_entry *nle;
775 struct hashtb_enumerator eenle;
776 struct hashtb_enumerator *enle = &eenle;
777
778 hashtb_start(ne->name_list, enle);
779 nl_element=hashtb_n(ne->name_list);
780
781 for (k=0;k<nl_element;k++)
782 {
783 nle=enle->data;
akmhoque3cced642012-09-24 16:20:20 -0500784 if( is_neighbor(nle->name) == 0 )
785 {
akmhoque1771c412012-11-09 13:06:08 -0600786 if ( nlsr->debugging )
787 printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
788 if ( nlsr->detailed_logging )
akmhoqueb77b95f2013-02-08 12:28:47 -0600789 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
akmhoque3cced642012-09-24 16:20:20 -0500790 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, fle->next_hop_face);
791 }
792
akmhoquede61ba92012-09-20 22:19:12 -0500793
794 hashtb_next(enle);
795 }
796 hashtb_end(enle);
797
akmhoqueb77b95f2013-02-08 12:28:47 -0600798
799 hashtb_delete(ef);
800 j++;
801
akmhoquede61ba92012-09-20 22:19:12 -0500802 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600803 else
804 {
805 struct name_list_entry *nle;
806 struct hashtb_enumerator eenle;
807 struct hashtb_enumerator *enle = &eenle;
808
809 hashtb_start(ne->name_list, enle);
810 nl_element=hashtb_n(ne->name_list);
811
812 for (k=0;k<nl_element;k++)
813 {
814 nle=enle->data;
815 if( is_active_neighbor(ne->orig_router) && get_next_hop_face_from_adl( ne->orig_router ) != fle->next_hop_face )
816 {
817 if ( nlsr->debugging )
818 printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
819 if ( nlsr->detailed_logging )
820 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
821 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, fle->next_hop_face);
822 }
823
824
825 hashtb_next(enle);
826 }
827 hashtb_end(enle);
828
829 hashtb_next(ef);
830 }
akmhoquede61ba92012-09-20 22:19:12 -0500831 }
832 }
833 hashtb_end(ef);
834
835
836 hashtb_next(e);
837 }
838
839 hashtb_end(e);
840
841}
842
843void
akmhoque3cced642012-09-24 16:20:20 -0500844update_npt_with_new_route(void)
akmhoquede61ba92012-09-20 22:19:12 -0500845{
akmhoqueb77b95f2013-02-08 12:28:47 -0600846 if ( nlsr->debugging )
847 printf("update_npt_with_new_route called\n");
848
akmhoquede61ba92012-09-20 22:19:12 -0500849 clean_old_fib_entries_from_npt();
akmhoqueb77b95f2013-02-08 12:28:47 -0600850 print_npt();
akmhoquede61ba92012-09-20 22:19:12 -0500851 add_new_fib_entries_to_npt();
akmhoqueb77b95f2013-02-08 12:28:47 -0600852 print_npt();
853
akmhoque3cced642012-09-24 16:20:20 -0500854 int i, npt_element;
855
856 struct npt_entry *ne;
857
858 struct hashtb_enumerator ee;
859 struct hashtb_enumerator *e = &ee;
860
861 hashtb_start(nlsr->npt, e);
862 npt_element=hashtb_n(nlsr->npt);
863
864 for(i=0;i<npt_element;i++)
865 {
866
867 ne=e->data;
868 update_ccnd_fib_for_orig_router(ne->orig_router);
869 hashtb_next(e);
870 }
871
872 hashtb_end(e);
873}
874
875
876
877void
878sort_faces_by_distance(int *faces,int *route_costs,int start,int element)
879{
880 int i,j;
881 int temp_cost;
882 int temp_face;
883
884 for ( i=start ; i < element ; i ++)
885 {
886 for( j=i+1; j<element; j ++)
887 {
888 if (route_costs[j] < route_costs[i] )
889 {
890 temp_cost=route_costs[j];
891 route_costs[j]=route_costs[i];
892 route_costs[i]=temp_cost;
893
894 temp_face=faces[j];
895 faces[j]=faces[i];
896 faces[i]=temp_face;
897 }
898 }
899 }
900
901}
902
903void
904get_all_faces_for_orig_router_from_npt(char *orig_router, int *faces, int *route_costs, int num_faces)
905{
906
907 int res,face_list_element,j;
908 struct hashtb_enumerator ee;
909 struct hashtb_enumerator *e = &ee;
910
911
912 hashtb_start(nlsr->npt, e);
913 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
914
915 if(res == HT_NEW_ENTRY)
916 {
917 hashtb_delete(e);
918 }
919 else if ( res == HT_OLD_ENTRY )
920 {
921 struct npt_entry *ne;
922 ne=e->data;
923
924 struct face_list_entry *fle;
925
926 struct hashtb_enumerator eef;
927 struct hashtb_enumerator *ef = &eef;
928
929 hashtb_start(ne->face_list, ef);
930 face_list_element=hashtb_n(ne->face_list);
931 for(j=0;j<face_list_element;j++)
932 {
933 fle=ef->data;
934 faces[j]=fle->next_hop_face;
935 route_costs[j]=fle->route_cost;
936 hashtb_next(ef);
937 }
938 hashtb_end(ef);
939
940
941 }
942 hashtb_end(e);
943
944}
945
946void
947destroy_faces_by_orig_router(char *orig_router)
948{
949
950 int res;
951 struct hashtb_enumerator ee;
952 struct hashtb_enumerator *e = &ee;
953
954
955 hashtb_start(nlsr->npt, e);
956 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
957
958 if(res == HT_NEW_ENTRY)
959 {
960 hashtb_delete(e);
961 }
962 else if ( res == HT_OLD_ENTRY )
963 {
964 struct npt_entry *ne;
965 ne=e->data;
966 int num_face=hashtb_n(ne->face_list);
967 int last_face,first_face;
968
969 int *faces=(int *)malloc(num_face*sizeof(int));
970 int *route_costs=(int *)malloc(num_face*sizeof(int));
971
972 get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
973 sort_faces_by_distance(faces,route_costs,0,num_face);
974
akmhoqueb77b95f2013-02-08 12:28:47 -0600975 /*
akmhoque3cced642012-09-24 16:20:20 -0500976 first_face=num_face-1;
977
978 if ( nlsr->multi_path_face_num == 0 )
979 {
980 last_face=first_face;
981 }
982 else
983 {
984 if ( num_face <= nlsr->multi_path_face_num)
985 {
986 last_face=0;
987 }
988 else if ( nlsr->multi_path_face_num == 0)
989 {
990 last_face=num_face-nlsr->multi_path_face_num;
991 }
992 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600993 */
994
995 last_face=0;
996 if ( nlsr->max_faces_per_prefix == 0) // add all faces available in routing table
997 {
998 first_face=num_face-1;
999 }
1000 else if( nlsr->max_faces_per_prefix > 0)
1001 {
1002 if ( nlsr->max_faces_per_prefix >= num_face)
1003 {
1004 first_face=num_face-1;
1005 }
1006 else if ( nlsr->max_faces_per_prefix < num_face)
1007 {
1008 first_face=nlsr->max_faces_per_prefix-1;
1009 }
1010
1011 }
akmhoque3cced642012-09-24 16:20:20 -05001012
1013 int i,j, nl_element;
1014 struct name_list_entry *nle;
1015 struct hashtb_enumerator eenle;
1016 struct hashtb_enumerator *enle = &eenle;
1017
1018 hashtb_start(ne->name_list, enle);
1019 nl_element=hashtb_n(ne->name_list);
1020
1021 for (i=0;i<nl_element;i++)
1022 {
1023 nle=enle->data;
1024
1025 for( j=first_face; j>= last_face; j--)
1026 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001027 if ( is_active_neighbor(orig_router) == 0 )
akmhoque3cced642012-09-24 16:20:20 -05001028 {
akmhoque1771c412012-11-09 13:06:08 -06001029 if ( nlsr->debugging )
1030 printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
1031 if ( nlsr->detailed_logging )
1032 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
akmhoquea30cb772012-10-07 09:50:34 -05001033 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
akmhoque3cced642012-09-24 16:20:20 -05001034 }
1035 else
1036 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001037 if ( j == last_face && is_active_neighbor(nle->name)==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]);
akmhoque3cced642012-09-24 16:20:20 -05001043 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]);
1044 }
1045 }
1046 }
1047
1048 hashtb_next(enle);
1049 }
1050 hashtb_end(enle);
1051
1052
1053
1054 free(faces);
1055 free(route_costs);
1056
1057 }
1058 hashtb_end(e);
1059
akmhoquede61ba92012-09-20 22:19:12 -05001060}
1061
akmhoquefbfd0982012-09-09 20:59:03 -05001062void
1063destroy_all_face_by_nlsr(void)
1064{
1065 int i, npt_element;
1066
1067 struct npt_entry *ne;
1068
1069 struct hashtb_enumerator ee;
1070 struct hashtb_enumerator *e = &ee;
1071
1072 hashtb_start(nlsr->npt, e);
1073 npt_element=hashtb_n(nlsr->npt);
1074
1075 for(i=0;i<npt_element;i++)
1076 {
akmhoquefbfd0982012-09-09 20:59:03 -05001077 ne=e->data;
akmhoque3cced642012-09-24 16:20:20 -05001078 destroy_faces_by_orig_router(ne->orig_router);
akmhoquefbfd0982012-09-09 20:59:03 -05001079 hashtb_next(e);
1080 }
1081
1082 hashtb_end(e);
1083
akmhoque3171d652012-11-13 11:44:33 -06001084 if ( nlsr->debugging )
1085 printf("\n");
1086 if ( nlsr->detailed_logging )
1087 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoquefbfd0982012-09-09 20:59:03 -05001088}