blob: e0f6ee40bae3124b20a1e6a092015e666dabf1d8 [file] [log] [blame]
akmhoque8fdd6412012-12-04 15:05:33 -06001#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
12
13#include <ccn/ccn.h>
14#include <ccn/uri.h>
15#include <ccn/keystore.h>
16#include <ccn/signing.h>
17#include <ccn/schedule.h>
18#include <ccn/hashtb.h>
19
20#include "nlsr.h"
21#include "nlsr_ndn.h"
22#include "nlsr_lsdb.h"
23#include "utility.h"
24#include "nlsr_npl.h"
25#include "nlsr_adl.h"
26#include "nlsr_route.h"
27#include "nlsr_npt.h"
28#include "nlsr_sync.h"
29
30void
31set_new_lsdb_version(void)
32{
33
34 char *time_stamp=(char *)malloc(20);
35 memset(time_stamp,0,20);
36 get_current_timestamp_micro(time_stamp);
37
38 free(nlsr->lsdb->lsdb_version);
39 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
40 memset(nlsr->lsdb->lsdb_version,0,strlen(time_stamp)+1);
41 memcpy(nlsr->lsdb->lsdb_version,time_stamp,strlen(time_stamp)+1);
42
43 free(time_stamp);
44
45}
46
47void
48make_name_lsa_key(char *key, char *orig_router, int ls_type, long int ls_id)
49{
50
51 char lst[2];
52 memset(lst,0,2);
53 sprintf(lst,"%d",ls_type);
54
55 char lsid[10];
56 memset(lsid,0,10);
57 sprintf(lsid,"%ld",ls_id);
58
59 memcpy(key+strlen(key),orig_router,strlen(orig_router));
60 memcpy(key+strlen(key),"/",1);
61 memcpy(key+strlen(key),lst,strlen(lst));
62 memcpy(key+strlen(key),"/",1);
63 memcpy(key+strlen(key),lsid,strlen(lsid));
64
65}
66
67
68void
69make_name_lsa_prefix_for_repo(char *key, char *orig_router, int ls_type, long int ls_id,char *orig_time,char *slice_prefix)
70{
71
akmhoque8fdd6412012-12-04 15:05:33 -060072 char lst[2];
73 memset(lst,0,2);
74 sprintf(lst,"%d",ls_type);
75
76 char lsid[10];
77 memset(lsid,0,10);
78 sprintf(lsid,"%ld",ls_id);
79 memcpy(key+strlen(key),slice_prefix,strlen(slice_prefix));
akmhoque8fdd6412012-12-04 15:05:33 -060080 memcpy(key+strlen(key),lst,strlen(lst));
81 memcpy(key+strlen(key),"/",1);
82 memcpy(key+strlen(key),lsid,strlen(lsid));
83 memcpy(key+strlen(key),"/",1);
84 memcpy(key+strlen(key),orig_time,strlen(orig_time));
85 memcpy(key+strlen(key),orig_router,strlen(orig_router));
86
akmhoque4ae16942012-12-10 11:50:43 -060087}
88
89void
90make_adj_lsa_prefix_for_repo(char *key, char *orig_router, int ls_type, char *orig_time,char *slice_prefix)
91{
92
Syed Obaid Amin78e9aa92012-12-21 03:57:13 -060093/* char lst[2];
akmhoque4ae16942012-12-10 11:50:43 -060094 memset(lst,0,2);
Syed Obaid Amin78e9aa92012-12-21 03:57:13 -060095 sprintf(lst,"%d",ls_type);
96
97
akmhoque4ae16942012-12-10 11:50:43 -060098 memcpy(key+strlen(key),slice_prefix,strlen(slice_prefix));
99 memcpy(key+strlen(key),"/",1);
100 memcpy(key+strlen(key),lst,strlen(lst));
101 memcpy(key+strlen(key),"/",1);
102 memcpy(key+strlen(key),orig_time,strlen(orig_time));
103 memcpy(key+strlen(key),orig_router,strlen(orig_router));
Syed Obaid Amin78e9aa92012-12-21 03:57:13 -0600104*/
105 sprintf(key,"%s%d/%s%s",slice_prefix,ls_type, orig_time, orig_router);
106 printf("Key:%s\n",key);
akmhoque8fdd6412012-12-04 15:05:33 -0600107}
108
109void
110build_and_install_name_lsas(void)
111{
112 if ( nlsr->debugging )
113 printf("build_and_install_name_lsas called \n");
114 if ( nlsr->detailed_logging )
115 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_name_lsas called\n");
116
117 int i, npl_element;
118 //struct name_prefix *np;
119 struct name_prefix_list_entry *npe;
120
121 struct hashtb_enumerator ee;
122 struct hashtb_enumerator *e = &ee;
123
124 hashtb_start(nlsr->npl, e);
125 npl_element=hashtb_n(nlsr->npl);
126
127 for(i=0;i<npl_element;i++)
128 {
129 npe=e->data;
130 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
131 build_name_lsa(name_lsa,npe->np);
132
133 install_name_lsa(name_lsa);
134 update_nlsa_id_for_name_in_npl(npe->np,name_lsa->header->ls_id);
135 free(name_lsa->header->orig_router->name);
136 free(name_lsa->header->orig_router);
137 free(name_lsa->header);
138 free(name_lsa->name_prefix->name);
139 free(name_lsa->name_prefix);
140 free(name_lsa);
141 hashtb_next(e);
142 }
143
144 hashtb_end(e);
145
146 print_name_prefix_from_npl();
147
148}
149
150void
151build_and_install_single_name_lsa(struct name_prefix *np)
152{
153 if ( nlsr->debugging )
154 printf("build_and_install_single_name_lsa called \n");
155 if ( nlsr->detailed_logging )
156 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_single_name_lsa called\n");
157
158 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
159 build_name_lsa(name_lsa,np);
160
161 install_name_lsa(name_lsa);
162 update_nlsa_id_for_name_in_npl(np,name_lsa->header->ls_id);
163
164 free(name_lsa->header->orig_router->name);
165 free(name_lsa->header->orig_router);
166 free(name_lsa->header);
167 free(name_lsa->name_prefix->name);
168 free(name_lsa->name_prefix);
169 free(name_lsa);
170
171 print_name_prefix_from_npl();
172
173}
174
175void
176build_name_lsa(struct nlsa *name_lsa, struct name_prefix *np)
177{
178 name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header ));
179 name_lsa->header->ls_type=LS_TYPE_NAME;
180
181 char *time_stamp=(char *)malloc(20);
182 memset(time_stamp,0,20);
183 get_current_timestamp_micro(time_stamp);
184
185 name_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1); //free
186 memset(name_lsa->header->orig_time,0,strlen(time_stamp)+1);
187 memcpy(name_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
188
189 free(time_stamp);
190
191 name_lsa->header->ls_id=++nlsr->nlsa_id;
192 name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
193 name_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1);
194 memset(name_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
195 memcpy(name_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
196 name_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
197 name_lsa->header->isValid=1;
198
199
200 name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
201 name_lsa->name_prefix->name=(char *)malloc(np->length);
202 memcpy(name_lsa->name_prefix->name,np->name,np->length);
203 name_lsa->name_prefix->length=np->length;
204
205}
206
207void
208install_name_lsa(struct nlsa *name_lsa)
209{
210
211 char *time_stamp=(char *)malloc(20);
212 memset(time_stamp,0,20);
213 get_current_timestamp_micro(time_stamp);
214 //long int lsa_life_time=get_time_diff(time_stamp,name_lsa->header->orig_time);
215
216 //printf("time difference: %ld \n",lsa_life_time);
217
218
219 char lst[2];
220 memset(lst,0,2);
221 sprintf(lst,"%d",name_lsa->header->ls_type);
222
223 char lsid[10];
224 memset(lsid,0,10);
225 sprintf(lsid,"%ld",name_lsa->header->ls_id);
226
227
228 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
229 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
230
231
232 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
233 if ( nlsr->debugging )
234 printf("Key:%s Length:%d\n",key,(int)strlen(key));
235 if ( nlsr->detailed_logging )
236 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Key:%s Length:%d\n",key,(int)strlen(key));
237
238
239 struct nlsa *new_name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa )); //free
240
241 struct hashtb_enumerator ee;
242 struct hashtb_enumerator *e = &ee;
243 int res;
244
245 hashtb_start(nlsr->lsdb->name_lsdb, e);
246 res = hashtb_seek(e, key, strlen(key), 0);
247
248 if(res == HT_NEW_ENTRY )
249 {
250
251 if ( nlsr->debugging )
252 printf("New Name LSA... Adding to LSDB\n");
253 if ( nlsr->detailed_logging )
254 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Name LSA... Adding to LSDB\n");
255
256
257 new_name_lsa = e->data;
258
259 new_name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header )); //free
260 new_name_lsa->header->ls_type=name_lsa->header->ls_type;
261
262 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
263 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
264 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
265
266 new_name_lsa->header->ls_id=name_lsa->header->ls_id;
267 new_name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free
268 new_name_lsa->header->orig_router->name=(char *)malloc(name_lsa->header->orig_router->length);
269 memcpy(new_name_lsa->header->orig_router->name,name_lsa->header->orig_router->name,name_lsa->header->orig_router->length);
270 new_name_lsa->header->orig_router->length=name_lsa->header->orig_router->length;
271 new_name_lsa->header->isValid=name_lsa->header->isValid;
272
273
274 new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free
275 new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
276 memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
277 new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
278
279 if ( nlsr->debugging )
280 {
281 printf("New Name LSA Added....\n");
282 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
283 }
284 if ( nlsr->detailed_logging )
285 {
286 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Name LSA Added....\n");
287 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
288 }
289 set_new_lsdb_version();
290 if ( nlsr->debugging )
291 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
292 if ( nlsr->detailed_logging )
293 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
294
295
296 int num_next_hop=get_number_of_next_hop(new_name_lsa->header->orig_router->name);
297 if ( num_next_hop < 0 )
298 {
299 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,NO_NEXT_HOP,NULL,NULL);
300 if ( check == HT_NEW_ENTRY )
301 {
302 if ( nlsr->debugging )
303 printf("Added in npt \n");
304 if ( nlsr->detailed_logging )
305 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
306 }
307 }
308 else
309 {
310 int *faces=malloc(num_next_hop*sizeof(int));
311 int *route_costs=malloc(num_next_hop*sizeof(int));
312 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name,faces,route_costs);
313 if ( nlsr->debugging )
314 {
315 printf("Printing from install_name_lsa \n");
316 int j;
317 for(j=0;j<num_next_hop;j++)
318 printf("Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
319 }
320 if ( nlsr->detailed_logging )
321 {
322 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Printing from install_name_lsa \n");
323 int j;
324 for(j=0;j<num_next_hop;j++)
325 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
326 }
327 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,next_hop,faces,route_costs);
328 if ( check == HT_NEW_ENTRY )
329 {
330 if ( nlsr->debugging )
331 printf("Added in npt \n");
332 if ( nlsr->detailed_logging )
333 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
334 }
335 free(faces);
336 free(route_costs);
337
338 }
339
340 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
341 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
342 write_log_for_name_lsa(new_name_lsa);
343 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
344
345 free(time_stamp);
346
347 }
348 else if(res == HT_OLD_ENTRY)
349 {
350 new_name_lsa=e->data;
351 if(strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time)<0)
352 {
353 if ( nlsr->debugging )
354 printf("Older Adj LSA. Discarded... \n");
355 if ( nlsr->detailed_logging )
356 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Older Adj LSA. Discarded...\n");
357 }
358 else if( strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time) == 0 )
359 {
360 if ( nlsr->debugging )
361 printf("Duplicate Adj LSA. Discarded... \n");
362 if ( nlsr->detailed_logging )
363 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Duplicate Adj LSA. Discarded...\n");
364 }
365 else
366 {
367 if ( name_lsa->header->isValid == 0 )
368 {
369 // have to call to delete npt table entry
370 delete_npt_entry_by_router_and_name_prefix(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name);
371
372 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name)!= 0)
373 {
374 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
375 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
376 write_log_for_name_lsa(new_name_lsa);
377 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
378
379 hashtb_delete(e);
380 if ( nlsr->debugging )
381 printf("isValid bit not set for Router %s so LSA Deleted from LSDB\n",name_lsa->header->orig_router->name);
382 if ( nlsr->detailed_logging )
383 writeLogg(__FILE__,__FUNCTION__,__LINE__,"isValid bit not set for Router %s so LSA Deleted from LSDB\n",name_lsa->header->orig_router->name);
384 }
385 else
386 {
387 new_name_lsa->header->isValid=name_lsa->header->isValid;
388 free(new_name_lsa->header->orig_time);
389 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
390 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
391 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
392 }
393 if ( nlsr->debugging )
394 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
395 if ( nlsr->detailed_logging )
396 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
397 set_new_lsdb_version();
398 if ( nlsr->debugging )
399 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
400 if ( nlsr->detailed_logging )
401 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
402 }
403 else
404 {
405 int is_npt_update=0;
406 if ( strcmp(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name) != 0 )
407 {
408 is_npt_update=1;
409 delete_npt_entry_by_router_and_name_prefix(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name);
410 }
411
412 // copying LSA content with header
413 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
414 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
415 write_log_for_name_lsa(new_name_lsa);
416 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
417
418
419 free(new_name_lsa->header->orig_time);
420 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
421 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
422 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
423
424 new_name_lsa->header->isValid=name_lsa->header->isValid;
425
426 free(new_name_lsa->name_prefix->name);
427 free(new_name_lsa->name_prefix);
428 new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
429 new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
430 memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
431 new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
432
433
434 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
435 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
436 write_log_for_name_lsa(new_name_lsa);
437 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
438
439 if ( nlsr->debugging )
440 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
441 if ( nlsr->detailed_logging )
442 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
443
444 set_new_lsdb_version();
445
446 if ( nlsr->debugging )
447 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
448 if ( nlsr->detailed_logging )
449 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
450
451
452 if( is_npt_update == 1 )
453 {
454 //struct hashtb *face_list;
455 int num_next_hop=get_number_of_next_hop(new_name_lsa->header->orig_router->name);
456 if ( num_next_hop < 0 )
457 {
458
459 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,NO_NEXT_HOP,NULL,NULL);
460 if ( check == HT_NEW_ENTRY )
461 {
462 if ( nlsr->debugging )
463 printf("Added in npt \n");
464 if ( nlsr->detailed_logging )
465 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
466 }
467 }
468 else
469 {
470 int *faces=malloc(num_next_hop*sizeof(int));
471 int *route_costs=malloc(num_next_hop*sizeof(int));
472 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name,faces,route_costs);
473
474 if ( nlsr->debugging )
475 {
476 printf("Printing from install_name_lsa \n");
477 int j;
478 for(j=0;j<num_next_hop;j++)
479 printf("Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
480 }
481 if ( nlsr->detailed_logging )
482 {
483 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Printing from install_name_lsa \n");
484 int j;
485 for(j=0;j<num_next_hop;j++)
486 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
487 }
488
489
490 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,next_hop,faces,route_costs);
491 if ( check == HT_NEW_ENTRY )
492 {
493 //printf("Added in npt \n");
494 if ( nlsr->debugging )
495 printf("Added in npt \n");
496 if ( nlsr->detailed_logging )
497 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
498 }
499 free(faces);
500 free(route_costs);
501
502 }
503
504 }
505 }
506 }
507
508 }
509
510 hashtb_end(e);
511
512 free(key);
513}
514
515void
516write_log_for_name_lsa(struct nlsa *name_lsa)
517{
518
519 writeLogg(__FILE__,__FUNCTION__,__LINE__,"-----------Name LSA Content---------------\n");
520 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s\n",name_lsa->header->orig_router->name);
521 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router Length: %d\n",name_lsa->header->orig_router->length);
522 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Type: %d\n",name_lsa->header->ls_type);
523 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Id: %ld\n",name_lsa->header->ls_id);
524 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Time: %s\n",name_lsa->header->orig_time);
525 writeLogg(__FILE__,__FUNCTION__,__LINE__," Is Valid: %d\n",name_lsa->header->isValid);
526 writeLogg(__FILE__,__FUNCTION__,__LINE__," LSA Data \n");
527 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix: %s\n",name_lsa->name_prefix->name);
528 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix Length: %d\n",name_lsa->name_prefix->length);
529 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
530}
531
532void
533print_name_lsa(struct nlsa *name_lsa)
534{
535
536 if ( nlsr->debugging )
537 {
538 printf("-----------Name LSA Content---------------\n");
539 printf(" Origination Router : %s\n",name_lsa->header->orig_router->name);
540 printf(" Origination Router Length: %d\n",name_lsa->header->orig_router->length);
541 printf(" LS Type : %d\n",name_lsa->header->ls_type);
542 printf(" LS Id : %ld\n",name_lsa->header->ls_id);
543 printf(" Origination Time : %s\n",name_lsa->header->orig_time);
544 printf(" Is Valid : %d\n",name_lsa->header->isValid);
545 printf(" LSA Data \n");
546 printf(" Name Prefix: : %s\n",name_lsa->name_prefix->name);
547 printf(" Name Prefix Length : %d\n",name_lsa->name_prefix->length);
548
549 printf("\n");
550 }
551}
552
553void
554print_name_lsdb(void)
555{
556 if ( nlsr->debugging )
557 printf("print_name_lsdb called \n");
558 int i, name_lsdb_element;
559 struct nlsa *name_lsa;
560
561 struct hashtb_enumerator ee;
562 struct hashtb_enumerator *e = &ee;
563
564 hashtb_start(nlsr->lsdb->name_lsdb, e);
565 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
566
567 for(i=0;i<name_lsdb_element;i++)
568 {
569 if ( nlsr->debugging )
570 printf("-----------Name LSA (%d)---------------\n",i+1);
571 name_lsa=e->data;
572 print_name_lsa(name_lsa);
573 hashtb_next(e);
574 }
575
576 hashtb_end(e);
577
578 if ( nlsr->debugging )
579 printf("\n");
580 if ( nlsr->detailed_logging )
581 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
582}
583
584
585void
586build_and_install_others_name_lsa(char *orig_router,int ls_type,long int ls_id,char *orig_time, int isValid,char *np)
587{
588 if ( nlsr->debugging )
589 printf("build_and_install_others_name_lsa called \n");
590 if ( nlsr->detailed_logging )
591 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_others_name_lsa called \n");
592
593 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
594 build_others_name_lsa(name_lsa,orig_router,ls_type,ls_id,orig_time, isValid,np);
595 print_name_lsa(name_lsa);
596 install_name_lsa(name_lsa);
597 print_name_lsdb();
598 print_npt();
599
600 free(name_lsa->header->orig_router->name);
601 free(name_lsa->header->orig_router);
602 free(name_lsa->header);
603 free(name_lsa->name_prefix->name);
604 free(name_lsa->name_prefix);
605 free(name_lsa);
606
607}
608
609void
610build_others_name_lsa(struct nlsa *name_lsa, char *orig_router,int ls_type,long int ls_id,char *orig_time, int isValid,char *np)
611{
612 if ( nlsr->debugging )
613 printf("build_others_name_lsa called\n");
614 if ( nlsr->detailed_logging )
615 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_others_name_lsa called \n");
616
617 name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header ));
618 name_lsa->header->ls_type=LS_TYPE_NAME;
619
620 name_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
621 memset(name_lsa->header->orig_time,0,strlen(orig_time)+1);
622 memcpy(name_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
623
624 name_lsa->header->ls_id=ls_id;
625 name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
626 name_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
627 memset(name_lsa->header->orig_router->name,0,strlen(orig_router)+1);
628 memcpy(name_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
629 name_lsa->header->orig_router->length=strlen(orig_router)+1;
630 name_lsa->header->isValid=isValid;
631
632 name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
633 name_lsa->name_prefix->name=(char *)malloc(strlen(np)+1);
634 memset(name_lsa->name_prefix->name,0,strlen(np)+1);
635 memcpy(name_lsa->name_prefix->name,np,strlen(np)+1);
636 name_lsa->name_prefix->length=strlen(np)+1;
637}
638
639
640
641
642void
643make_adj_lsa_key(char *key,struct alsa *adj_lsa)
644{
645 memcpy(key+strlen(key),adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
646 memcpy(key+strlen(key),"/",1);
647 char ls_type[2];
648 sprintf(ls_type,"%d",adj_lsa->header->ls_type);
649 memcpy(key+strlen(key),ls_type,strlen(ls_type));
650 key[strlen(key)]='\0';
651}
652
653int
654build_and_install_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
655{
656 if(flags == CCN_SCHEDULE_CANCEL)
657 {
658 return -1;
659 }
660
661 nlsr_lock();
662
663 if ( nlsr->debugging )
664 printf("build_and_install_adj_lsa called \n");
665 if ( nlsr->detailed_logging )
666 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_adj_lsa called \n");
667
668 if ( nlsr->debugging )
669 printf("adj_build_flag = %d \n",nlsr->adj_build_flag);
670 if ( nlsr->detailed_logging )
671 writeLogg(__FILE__,__FUNCTION__,__LINE__,"adj_build_flag = %d \n",nlsr->adj_build_flag);
672
673 if(nlsr->adj_build_flag > 0)
674 {
675 if ( nlsr->debugging )
676 printf("is_adj_lsa_build = %d \n",is_adj_lsa_build());
677 if ( nlsr->detailed_logging )
678 writeLogg(__FILE__,__FUNCTION__,__LINE__,"is_adj_lsa_build = %d \n",is_adj_lsa_build());
679
680 if ( is_adj_lsa_build()> 0)
681 {
682 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
683 build_adj_lsa(adj_lsa);
684 install_adj_lsa(adj_lsa);
685
akmhoque4ae16942012-12-10 11:50:43 -0600686 char lst[2];
687 memset(lst,2,0);
688 sprintf(lst,"%d",LS_TYPE_ADJ);
689
690 char *repo_con_name=(char *)malloc(strlen(nlsr->slice_prefix)+strlen(adj_lsa->header->orig_time)+strlen(adj_lsa->header->orig_router->name) + strlen(lst) + 5);
691 memset(repo_con_name,strlen(nlsr->slice_prefix)+strlen(adj_lsa->header->orig_time)+strlen(adj_lsa->header->orig_router->name) + strlen(lst) + 5,0);
692 make_adj_lsa_prefix_for_repo(repo_con_name, adj_lsa->header->orig_router->name,LS_TYPE_ADJ,adj_lsa->header->orig_time,nlsr->slice_prefix);
693
694 printf("Adj LSA Repo Key: %s \n",repo_con_name);
695
696 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
697 memset(key,0,adj_lsa->header->orig_router->length+2);
698 make_adj_lsa_key(key,adj_lsa);
Syed Obaid Amin78e9aa92012-12-21 03:57:13 -0600699 printf("Adj LSA: %s \n",key);
akmhoque4ae16942012-12-10 11:50:43 -0600700 struct name_prefix *lsaid=(struct name_prefix *)malloc(sizeof(struct name_prefix));
701 lsaid->name=(char *)malloc(strlen(key)+1);
702 memset(lsaid->name,strlen(key)+1,0);
703 memcpy(lsaid->name,key,strlen(key));
704 lsaid->length=strlen(key)+1;
705
706
707 //write_adj_lsa_to_repo(repo_key, lsaid);
708
709 //free(key);
710 //free(repo_con_name);
711
akmhoque8fdd6412012-12-04 15:05:33 -0600712 free(adj_lsa->header->orig_router->name);
713 free(adj_lsa->header->orig_router);
714 free(adj_lsa->header->orig_time);
715 free(adj_lsa->header);
716 free(adj_lsa->body);
717 free(adj_lsa);
718 nlsr->adj_build_flag=0;
719 print_adj_lsdb();
720 }
721 else
722 {
723 if ( nlsr->debugging )
724 printf("Can not build adj LSA now\n");
725 if ( nlsr->detailed_logging )
726 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Can not build adj LSA now\n");
727 }
728 }
729 nlsr->is_build_adj_lsa_sheduled=0;
730
731 nlsr_unlock();
732
733 return 0;
734}
735
736
737void
738build_adj_lsa(struct alsa * adj_lsa)
739{
740 if ( nlsr->debugging )
741 printf("build_adj_lsa called \n");
742 if ( nlsr->detailed_logging )
743 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_adj_lsa called \n");
744
745 int no_link=no_active_nbr();
746
747 //printf("Number of link in Adjacent LSA: %d\n",no_link);
748
749 /*Filling Up Header Data */
750 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
751 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
752 adj_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1);
753 memset(adj_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
754 memcpy(adj_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
755 adj_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
756
757 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
758
759 char *time_stamp=(char *)malloc(20);
760 memset(time_stamp,0,20);
761 get_current_timestamp_micro(time_stamp);
762
763 adj_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1);
764 memset(adj_lsa->header->orig_time,0,strlen(time_stamp)+1);
765 memcpy(adj_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
766 free(time_stamp);
767
768
769 /* Filling Up Body Data */
770
771 adj_lsa->no_link=no_link;
772
773
774 struct ccn_charbuf *c=ccn_charbuf_create();
775 get_active_nbr_adj_data(c);
776 char *data=ccn_charbuf_as_string(c);
777
778 adj_lsa->body=(char *)malloc(strlen(data)+1);
779 memset(adj_lsa->body,0,strlen(data)+1);
780 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
781 ccn_charbuf_destroy(&c);
782
783
784
akmhoque4ae16942012-12-10 11:50:43 -0600785 /*if( !nlsr->is_send_lsdb_interest_scheduled )
akmhoque8fdd6412012-12-04 15:05:33 -0600786 {
787 nlsr->event_send_lsdb_interest= ccn_schedule_event(nlsr->sched, 1000, &send_lsdb_interest, NULL, 0);
788 nlsr->is_send_lsdb_interest_scheduled=1;
akmhoque4ae16942012-12-10 11:50:43 -0600789 }*/
akmhoque8fdd6412012-12-04 15:05:33 -0600790
791 nlsr->adj_build_count++;
792
793
794}
795
796
797void
798install_adj_lsa(struct alsa * adj_lsa)
799{
800
801 if ( nlsr->debugging )
802 printf("install_adj_lsa called \n");
803 if ( nlsr->detailed_logging )
804 writeLogg(__FILE__,__FUNCTION__,__LINE__,"install_adj_lsa called \n");
805
806
807 char *time_stamp=(char *)malloc(20);
808 memset(time_stamp,0,20);
809 get_current_timestamp_micro(time_stamp);
810 //long int lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
811
812 //printf("time difference: %ld \n",lsa_life_time);
813
814
815 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
816 memset(key,0,adj_lsa->header->orig_router->length+2);
817 make_adj_lsa_key(key,adj_lsa);
818 //printf("Adjacent LSA key: %s \n",key);
819
820 struct alsa *new_adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
821
822 struct hashtb_enumerator ee;
823 struct hashtb_enumerator *e = &ee;
824 int res;
825
826 hashtb_start(nlsr->lsdb->adj_lsdb, e);
827 res = hashtb_seek(e, key, strlen(key), 0);
828
829
830
831 if(res == HT_NEW_ENTRY)
832 {
833 if ( adj_lsa->no_link > 0)
834 {
835 if ( nlsr->debugging )
836 printf("New ADJ LSA... Adding to LSDB\n");
837 if ( nlsr->detailed_logging )
838 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New ADJ LSA... Adding to LSDB\n");
839 new_adj_lsa = e->data;
840
841 new_adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
842 new_adj_lsa->header->ls_type=adj_lsa->header->ls_type;
843 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
844 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
845
846 new_adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
847 new_adj_lsa->header->orig_router->name=(char *)malloc(adj_lsa->header->orig_router->length);
848 memcpy(new_adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
849 new_adj_lsa->header->orig_router->length=adj_lsa->header->orig_router->length;
850
851 new_adj_lsa->no_link=adj_lsa->no_link;
852
853 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
854 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
855 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
856
857 add_next_hop_router(new_adj_lsa->header->orig_router->name);
858 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
859
860 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
861 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
862 write_log_for_adj_lsa(new_adj_lsa);
863 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
864 }
865 else
866 {
867 hashtb_delete(e);
868 }
869
870 if ( nlsr->debugging )
871 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
872 if ( nlsr->detailed_logging )
873 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
874
875 set_new_lsdb_version();
876
877 if ( nlsr->debugging )
878 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
879 if ( nlsr->detailed_logging )
880 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
881
882 }
883 else if(res == HT_OLD_ENTRY)
884 {
885 new_adj_lsa = e->data;
886 if(strcmp(adj_lsa->header->orig_time,new_adj_lsa->header->orig_time)<=0)
887 {
888 if ( nlsr->debugging )
889 printf("Older/Duplicate Adj LSA. Discarded...\n");
890 if ( nlsr->detailed_logging )
891 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Older/Duplicate Adj LSA. Discarded...\n");
892 }
893 else
894 {
895
896 if ( adj_lsa->no_link > 0)
897 {
898 //new_adj_lsa = e->data;
899
900 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
901 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
902 write_log_for_adj_lsa(new_adj_lsa);
903 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
904
905 free(new_adj_lsa->header->orig_time);
906 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
907 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
908
909 new_adj_lsa->no_link=adj_lsa->no_link;
910
911 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
912 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
913 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
914
915 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
916
917 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
918 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
919 write_log_for_adj_lsa(new_adj_lsa);
920 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
921 }
922 else
923 {
924 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
925 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
926 write_log_for_adj_lsa(new_adj_lsa);
927 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
928
929 hashtb_delete(e);
930 }
931
932 if ( nlsr->debugging )
933 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
934 if ( nlsr->detailed_logging )
935 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
936
937 set_new_lsdb_version();
938
939 if ( nlsr->debugging )
940 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
941 if ( nlsr->detailed_logging )
942 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
943 }
944
945 }
946 hashtb_end(e);
947
948 if ( !nlsr->is_route_calculation_scheduled )
949 {
950 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
951 nlsr->is_route_calculation_scheduled=1;
952 }
953
954
955 free(key);
956
957 free(time_stamp);
958}
959
960void
961write_log_for_adj_lsa_body(const char *body, int no_link)
962{
963 int i=0;
964 char *lsa_data=(char *)malloc(strlen(body)+1);
965 memset( lsa_data,0,strlen(body)+1);
966 memcpy(lsa_data,body,strlen(body)+1);
967 char *sep="|";
968 char *rem;
969 char *rtr_id;
970 char *length;
971 char *face;
972 char *metric;
973
974 if(no_link >0 )
975 {
976 rtr_id=strtok_r(lsa_data,sep,&rem);
977 length=strtok_r(NULL,sep,&rem);
978 face=strtok_r(NULL,sep,&rem);
979 metric=strtok_r(NULL,sep,&rem);
980
981 writeLogg(__FILE__,__FUNCTION__,__LINE__," Link %d \n",i+1);
982 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router: %s \n",rtr_id);
983 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router Length: %s \n",length);
984 writeLogg(__FILE__,__FUNCTION__,__LINE__," Connecting Face: %s \n",face);
985 writeLogg(__FILE__,__FUNCTION__,__LINE__," Metric: %s \n",metric);
986
987
988 for(i=1;i<no_link;i++)
989 {
990 rtr_id=strtok_r(NULL,sep,&rem);
991 length=strtok_r(NULL,sep,&rem);
992 face=strtok_r(NULL,sep,&rem);
993 metric=strtok_r(NULL,sep,&rem);
994 writeLogg(__FILE__,__FUNCTION__,__LINE__," Link %d \n",i+1);
995 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router: %s \n",rtr_id);
996 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router Length: %s \n",length);
997 writeLogg(__FILE__,__FUNCTION__,__LINE__," Connecting Face: %s \n",face);
998 writeLogg(__FILE__,__FUNCTION__,__LINE__," Metric: %s \n",metric);
999
1000 }
1001 }
1002
1003 free(lsa_data);
1004}
1005
1006
1007void
1008write_log_for_adj_lsa(struct alsa * adj_lsa)
1009{
1010 writeLogg(__FILE__,__FUNCTION__,__LINE__,"-----------Adj LSA Content---------------\n");
1011 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s\n",adj_lsa->header->orig_router->name);
1012 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
1013 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Type: %d\n",adj_lsa->header->ls_type);
1014 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Time: %s\n",adj_lsa->header->orig_time);
1015 writeLogg(__FILE__,__FUNCTION__,__LINE__," Lsa Data:\n");
1016 writeLogg(__FILE__,__FUNCTION__,__LINE__," No of Link: %d\n",adj_lsa->no_link);
1017
1018 write_log_for_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
1019
1020 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
1021
1022}
1023
1024void
1025print_adj_lsa_body(const char *body, int no_link)
1026{
1027 int i=0;
1028 char *lsa_data=(char *)malloc(strlen(body)+1);
1029 memset( lsa_data,0,strlen(body)+1);
1030 memcpy(lsa_data,body,strlen(body)+1);
1031 char *sep="|";
1032 char *rem;
1033 char *rtr_id;
1034 char *length;
1035 char *face;
1036 char *metric;
1037
1038 if(no_link >0 )
1039 {
1040 rtr_id=strtok_r(lsa_data,sep,&rem);
1041 length=strtok_r(NULL,sep,&rem);
1042 face=strtok_r(NULL,sep,&rem);
1043 metric=strtok_r(NULL,sep,&rem);
1044
1045 printf(" Link %d \n",i+1);
1046 printf(" Neighbor : %s \n",rtr_id);
1047 printf(" Neighbor Length : %s \n",length);
1048 printf(" Connecting Face : %s \n",face);
1049 printf(" Metric : %s \n",metric);
1050
1051
1052 for(i=1;i<no_link;i++)
1053 {
1054 rtr_id=strtok_r(NULL,sep,&rem);
1055 length=strtok_r(NULL,sep,&rem);
1056 face=strtok_r(NULL,sep,&rem);
1057 metric=strtok_r(NULL,sep,&rem);
1058 printf(" Link %d \n",i+1);
1059 printf(" Neighbor : %s \n",rtr_id);
1060 printf(" Neighbor Length : %s \n",length);
1061 printf(" Connecting Face : %s \n",face);
1062 printf(" Metric : %s \n",metric);
1063
1064 }
1065 }
1066
1067 free(lsa_data);
1068}
1069
1070void
1071print_adj_lsa(struct alsa * adj_lsa)
1072{
1073 if ( nlsr->debugging )
1074 {
1075 printf("-----------ADJ LSA Content---------------\n");
1076 printf(" Origination Router : %s\n",adj_lsa->header->orig_router->name);
1077 printf(" Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
1078 printf(" LS Type : %d\n",adj_lsa->header->ls_type);
1079 printf(" Origination Time : %s\n",adj_lsa->header->orig_time);
1080 printf(" Lsa Data:\n");
1081 printf(" No of Link : %d\n",adj_lsa->no_link);
1082
1083 print_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
1084 printf("\n");
1085 }
1086
1087}
1088
1089void
1090print_adj_lsdb(void)
1091{
1092 if ( nlsr->debugging )
1093 printf("print_name_lsdb called \n");
1094 int i, adj_lsdb_element;
1095 struct alsa *adj_lsa;
1096
1097 struct hashtb_enumerator ee;
1098 struct hashtb_enumerator *e = &ee;
1099
1100 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1101 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1102
1103 for(i=0;i<adj_lsdb_element;i++)
1104 {
1105 if ( nlsr->debugging )
1106 printf("-----------Adj LSA (%d)---------------\n",i+1);
1107 adj_lsa=e->data;
1108 print_adj_lsa(adj_lsa);
1109 hashtb_next(e);
1110 }
1111
1112 hashtb_end(e);
1113
1114 if ( nlsr->debugging )
1115 printf("\n");
1116 if ( nlsr->detailed_logging )
1117 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
1118}
1119
1120void
1121build_and_install_others_adj_lsa(char *orig_router,int ls_type,char *orig_time, int no_link,char *data)
1122{
1123 if ( nlsr->debugging )
1124 printf("build_and_install_others_adj_lsa called \n");
1125 if ( nlsr->detailed_logging )
1126 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_others_adj_lsa called \n");
1127 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
1128 build_others_adj_lsa(adj_lsa,orig_router,ls_type,orig_time,no_link,data);
1129 //print_adj_lsa(adj_lsa);
1130 install_adj_lsa(adj_lsa);
1131
1132
1133 free(adj_lsa->header->orig_router->name);
1134 free(adj_lsa->header->orig_router);
1135 free(adj_lsa->header->orig_time);
1136 free(adj_lsa->header);
1137 free(adj_lsa->body);
1138 free(adj_lsa);
1139
1140 print_adj_lsdb();
1141
1142}
1143
1144
1145void
1146build_others_adj_lsa(struct alsa *adj_lsa,char *orig_router,int ls_type,char *orig_time,int no_link,char *data)
1147{
1148 //printf("build_others_adj_lsa called \n");
1149 if ( nlsr->debugging )
1150 printf("build_others_adj_lsa called \n");
1151 if ( nlsr->detailed_logging )
1152 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_others_adj_lsa called \n");
1153
1154 /*Filling Up Header Data */
1155 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
1156 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
1157 adj_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
1158 memset(adj_lsa->header->orig_router->name,0,strlen(orig_router)+1);
1159 memcpy(adj_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
1160
1161 adj_lsa->header->orig_router->length=strlen(orig_router)+1;
1162
1163
1164 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
1165
1166 adj_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
1167 memset(adj_lsa->header->orig_time,0,strlen(orig_time)+1);
1168 memcpy(adj_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
1169
1170 adj_lsa->no_link=no_link;
1171
1172 adj_lsa->body=(char *)malloc(strlen(data)+1);
1173 memset(adj_lsa->body,0,strlen(data)+1);
1174 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
1175
1176}
1177
1178
1179long int
1180get_name_lsdb_num_element(void)
1181{
1182 long int num_element;
1183
1184
1185 struct hashtb_enumerator ee;
1186 struct hashtb_enumerator *e = &ee;
1187
1188 hashtb_start(nlsr->lsdb->name_lsdb, e);
1189 num_element=hashtb_n(nlsr->lsdb->name_lsdb);
1190 hashtb_end(e);
1191
1192 return num_element;
1193}
1194
1195long int
1196get_adj_lsdb_num_element(void)
1197{
1198 long int num_element;
1199
1200
1201 struct hashtb_enumerator ee;
1202 struct hashtb_enumerator *e = &ee;
1203
1204 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1205 num_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1206 hashtb_end(e);
1207
1208 return num_element;
1209}
1210
1211void
1212get_name_lsdb_summary(struct ccn_charbuf *name_lsdb_data)
1213{
1214 //printf("get_name_lsdb_summary called \n");
1215 if ( nlsr->debugging )
1216 printf("get_name_lsdb_summary called \n");
1217 if ( nlsr->detailed_logging )
1218 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_name_lsdb_summary called \n");
1219
1220 int i, name_lsdb_element;
1221
1222 struct nlsa *name_lsa;
1223 struct hashtb_enumerator ee;
1224 struct hashtb_enumerator *e = &ee;
1225
1226 hashtb_start(nlsr->lsdb->name_lsdb, e);
1227 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
1228
1229 for(i=0;i<name_lsdb_element;i++)
1230 {
1231 name_lsa=e->data;
1232
1233 ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_router->name);
1234 ccn_charbuf_append_string(name_lsdb_data,"|");
1235
1236 char *lst=(char *)malloc(20);
1237 memset(lst,0,20);
1238 sprintf(lst,"%d",name_lsa->header->ls_type);
1239 ccn_charbuf_append_string(name_lsdb_data,lst);
1240 free(lst);
1241 ccn_charbuf_append_string(name_lsdb_data,"|");
1242
1243 char *lsid=(char *)malloc(20);
1244 memset(lsid,0,20);
1245 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1246 ccn_charbuf_append_string(name_lsdb_data,lsid);
1247 free(lsid);
1248 ccn_charbuf_append_string(name_lsdb_data,"|");
1249
1250 ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_time);
1251 ccn_charbuf_append_string(name_lsdb_data,"|");
1252
1253 hashtb_next(e);
1254 }
1255
1256 hashtb_end(e);
1257
1258}
1259
1260
1261void
1262get_adj_lsdb_summary(struct ccn_charbuf *adj_lsdb_data)
1263{
1264 if ( nlsr->debugging )
1265 printf("get_adj_lsdb_summary called \n");
1266 if ( nlsr->detailed_logging )
1267 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_adj_lsdb_summary called \n");
1268 int i, adj_lsdb_element;
1269 struct alsa *adj_lsa;
1270
1271 struct hashtb_enumerator ee;
1272 struct hashtb_enumerator *e = &ee;
1273
1274 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1275 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1276
1277 for(i=0;i<adj_lsdb_element;i++)
1278 {
1279 adj_lsa=e->data;
1280
1281 ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_router->name);
1282 ccn_charbuf_append_string(adj_lsdb_data,"|");
1283
1284 char *lst=(char *)malloc(20);
1285 memset(lst,0,20);
1286 sprintf(lst,"%d",adj_lsa->header->ls_type);
1287 ccn_charbuf_append_string(adj_lsdb_data,lst);
1288 free(lst);
1289 ccn_charbuf_append_string(adj_lsdb_data,"|");
1290
1291 ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_time);
1292 ccn_charbuf_append_string(adj_lsdb_data,"|");
1293
1294 hashtb_next(e);
1295 }
1296
1297 hashtb_end(e);
1298}
1299
1300
1301void
1302get_lsdb_summary(struct ccn_charbuf *lsdb_data)
1303{
1304 struct ccn_charbuf *name_lsdb_data=ccn_charbuf_create();
1305 struct ccn_charbuf *adj_lsdb_data=ccn_charbuf_create();
1306
1307 get_name_lsdb_summary(name_lsdb_data);
1308 get_adj_lsdb_summary(adj_lsdb_data);
1309
1310 long int num_lsa=get_name_lsdb_num_element() + get_adj_lsdb_num_element();
1311 char *num_element=(char *)malloc(15);
1312 memset(num_element,0,15);
1313 sprintf(num_element,"%ld",num_lsa);
1314
1315 if( num_lsa > 0)
1316 {
1317 ccn_charbuf_append_string(lsdb_data,num_element);
1318 ccn_charbuf_append_string(lsdb_data,"|");
1319 }
1320 if(name_lsdb_data->length>0)
1321 {
1322 char *data1=ccn_charbuf_as_string(name_lsdb_data);
1323 ccn_charbuf_append_string(lsdb_data,(char *)data1);
1324 }
1325 if(adj_lsdb_data->length>0)
1326 {
1327 char *data2=ccn_charbuf_as_string(adj_lsdb_data);
1328 ccn_charbuf_append_string(lsdb_data,(char *)data2);
1329 }
1330 ccn_charbuf_destroy(&name_lsdb_data);
1331 ccn_charbuf_destroy(&adj_lsdb_data);
1332 free(num_element);
1333
1334}
1335
1336int
1337check_is_new_name_lsa(char *orig_router,char *lst,char *lsid,char *orig_time)
1338{
1339 int ret=0;
1340 struct ccn_charbuf *key=ccn_charbuf_create();
1341 ccn_charbuf_append_string(key,orig_router);
1342 ccn_charbuf_append_string(key,"/");
1343 ccn_charbuf_append_string(key,lst);
1344 ccn_charbuf_append_string(key,"/");
1345 ccn_charbuf_append_string(key,lsid);
1346
1347 int res;
1348 struct nlsa *name_lsa;
1349
1350 struct hashtb_enumerator ee;
1351 struct hashtb_enumerator *e = &ee;
1352
1353 hashtb_start(nlsr->lsdb->name_lsdb, e);
1354 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1355
1356 if( res == HT_NEW_ENTRY )
1357 {
1358 hashtb_delete(e);
1359 ret=1;
1360
1361 }
1362 else if(res == HT_OLD_ENTRY)
1363 {
1364 name_lsa=e->data;
1365 if( strcmp ( orig_time , name_lsa->header->orig_time ) > 0 )
1366 {
1367 ret=1;
1368 }
1369 }
1370
1371 hashtb_end(e);
1372
1373 ccn_charbuf_destroy(&key);
1374
1375 return ret;
1376}
1377
1378int
1379check_is_new_adj_lsa(char *orig_router,char *lst,char *orig_time)
1380{
1381 int ret=0;
1382 struct ccn_charbuf *key=ccn_charbuf_create();
1383 ccn_charbuf_append_string(key,orig_router);
1384 ccn_charbuf_append_string(key,"/");
1385 ccn_charbuf_append_string(key,lst);
1386
1387 int res;
1388 struct alsa *adj_lsa;
1389
1390 struct hashtb_enumerator ee;
1391 struct hashtb_enumerator *e = &ee;
1392
1393 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1394 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1395
1396 if( res == HT_NEW_ENTRY )
1397 {
1398 hashtb_delete(e);
1399 ret=1;
1400
1401 }
1402 else if(res == HT_OLD_ENTRY)
1403 {
1404 adj_lsa=e->data;
1405 if( strcmp ( orig_time , adj_lsa->header->orig_time ) > 0 )
1406 {
1407 ret=1;
1408 }
1409 }
1410
1411 hashtb_end(e);
1412
1413 ccn_charbuf_destroy(&key);
1414
1415 return ret;
1416}
1417
1418void
1419get_name_lsa_data(struct ccn_charbuf *lsa_data, struct name_prefix *lsaId)
1420{
1421 //printf("get_name_lsa_data called \n");
1422 if ( nlsr->debugging )
1423 printf("get_name_lsa_data called \n");
1424 if ( nlsr->detailed_logging )
1425 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_name_lsa_data called \n");
1426
1427 struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa ));
1428
1429 struct hashtb_enumerator ee;
1430 struct hashtb_enumerator *e = &ee;
1431 int res;
1432
1433 hashtb_start(nlsr->lsdb->name_lsdb, e);
1434 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1435
1436 if( res == HT_OLD_ENTRY )
1437 {
1438 name_lsa=e->data;
1439 //printf("NAME LSA found\n");
1440
1441 if ( nlsr->debugging )
1442 printf("NAME LSA found \n");
1443 if ( nlsr->detailed_logging )
1444 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Name LSA found \n");
1445
1446 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_router->name);
1447 ccn_charbuf_append_string(lsa_data,"|");
1448
1449 char *temp_length=(char *)malloc(20);
1450 memset(temp_length,0,20);
1451 sprintf(temp_length,"%d",name_lsa->header->orig_router->length);
1452 ccn_charbuf_append_string(lsa_data,temp_length);
1453 free(temp_length);
1454 ccn_charbuf_append_string(lsa_data,"|");
1455
1456 char *temp_ltype=(char *)malloc(20);
1457 memset(temp_ltype,0,20);
1458 sprintf(temp_ltype,"%d",name_lsa->header->ls_type);
1459 ccn_charbuf_append_string(lsa_data,temp_ltype);
1460 free(temp_ltype);
1461 ccn_charbuf_append_string(lsa_data,"|");
1462
1463 char *temp_lsid=(char *)malloc(20);
1464 memset(temp_lsid,0,20);
1465 sprintf(temp_lsid,"%ld",name_lsa->header->ls_id);
1466 ccn_charbuf_append_string(lsa_data,temp_lsid);
1467 free(temp_lsid);
1468 ccn_charbuf_append_string(lsa_data,"|");
1469
1470 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_time);
1471 ccn_charbuf_append_string(lsa_data,"|");
1472
1473 char *temp_valid=(char *)malloc(20);
1474 memset(temp_valid,0,20);
1475 sprintf(temp_valid,"%d",name_lsa->header->isValid);
1476 ccn_charbuf_append_string(lsa_data,temp_valid);
1477 free(temp_valid);
1478 ccn_charbuf_append_string(lsa_data,"|");
1479
1480 ccn_charbuf_append_string(lsa_data,name_lsa->name_prefix->name);
1481 ccn_charbuf_append_string(lsa_data,"|");
1482
1483 char *temp_npl=(char *)malloc(20);
1484 memset(temp_npl,0,20);
1485 sprintf(temp_npl,"%d",name_lsa->name_prefix->length);
1486 ccn_charbuf_append_string(lsa_data,temp_npl);
1487 free(temp_npl);
1488 ccn_charbuf_append_string(lsa_data,"|");
1489
1490 }
1491 else if(res == HT_NEW_ENTRY)
1492 {
1493 hashtb_delete(e);
1494 }
1495
1496 hashtb_end(e);
1497}
1498
1499void
1500get_adj_lsa_data(struct ccn_charbuf *lsa_data,struct name_prefix *lsaId)
1501{
1502 if ( nlsr->debugging )
1503 printf("get_adj_lsa_data called \n");
1504 if ( nlsr->detailed_logging )
1505 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_adj_lsa_data called \n");
1506
1507 struct alsa *adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
1508
1509 struct hashtb_enumerator ee;
1510 struct hashtb_enumerator *e = &ee;
1511 int res;
1512
1513 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1514 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1515
1516 if( res == HT_OLD_ENTRY )
1517 {
1518 adj_lsa=e->data;
1519 //printf("Adj LSA found\n");
1520
1521 if ( nlsr->debugging )
1522 printf("Adj LSA found \n");
1523 if ( nlsr->detailed_logging )
1524 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adj LSA found \n");
1525
1526 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_router->name);
1527 ccn_charbuf_append_string(lsa_data,"|");
1528
1529 char *temp_length=(char *)malloc(20);
1530 memset(temp_length,0,20);
1531 sprintf(temp_length,"%d",adj_lsa->header->orig_router->length);
1532 ccn_charbuf_append_string(lsa_data,temp_length);
1533 free(temp_length);
1534 ccn_charbuf_append_string(lsa_data,"|");
1535
1536 char *temp_ltype=(char *)malloc(20);
1537 memset(temp_ltype,0,20);
1538 sprintf(temp_ltype,"%d",adj_lsa->header->ls_type);
1539 ccn_charbuf_append_string(lsa_data,temp_ltype);
1540 free(temp_ltype);
1541 ccn_charbuf_append_string(lsa_data,"|");
1542
1543 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_time);
1544 ccn_charbuf_append_string(lsa_data,"|");
1545
1546 char *temp_nl=(char *)malloc(20);
1547 memset(temp_nl,0,20);
1548 sprintf(temp_nl,"%d",adj_lsa->no_link);
1549 ccn_charbuf_append_string(lsa_data,temp_nl);
1550 free(temp_nl);
1551 ccn_charbuf_append_string(lsa_data,"|");
1552
1553 ccn_charbuf_append_string(lsa_data,adj_lsa->body);
1554
1555
1556 }
1557 else if(res == HT_NEW_ENTRY)
1558 {
1559 hashtb_delete(e);
1560 }
1561
1562 hashtb_end(e);
1563}
1564
1565void
1566make_name_lsa_invalid(struct name_prefix *np,int ls_type, long int ls_id)
1567{
1568
1569 if ( nlsr->debugging )
1570 printf("make_name_lsa_invalid called \n");
1571 if ( nlsr->detailed_logging )
1572 writeLogg(__FILE__,__FUNCTION__,__LINE__,"make_name_lsa_invalid called \n");
1573
1574
1575 char lst[2];
1576 memset(lst,0,2);
1577 sprintf(lst,"%d",ls_type);
1578
1579 char lsid[10];
1580 memset(lsid,0,10);
1581 sprintf(lsid,"%ld",ls_id);
1582
1583
1584 char *key=(char *)malloc(strlen(np->name)+1+strlen(lst)+1+strlen(lsid)+1);
1585 memset(key,0,strlen(np->name)+1+strlen(lst)+1+strlen(lsid)+1);
1586
1587
1588 make_name_lsa_key(key, np->name,ls_type,ls_id);
1589 printf("Key:%s Length:%d\n",key,(int)strlen(key));
1590
1591 struct nlsa *nlsa;
1592
1593 struct hashtb_enumerator ee;
1594 struct hashtb_enumerator *e = &ee;
1595
1596 int res;
1597
1598 hashtb_start(nlsr->lsdb->name_lsdb, e);
1599 res = hashtb_seek(e, key,strlen(key) , 0);
1600
1601 if( res == HT_OLD_ENTRY )
1602 {
1603 nlsa=e->data;
1604
1605 nlsa->header->isValid=0;
1606
1607 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1608 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1609 write_log_for_name_lsa(nlsa);
1610 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1611
1612 hashtb_delete(e);
1613 }
1614 else if( res == HT_NEW_ENTRY )
1615 {
1616 hashtb_delete(e);
1617 }
1618 hashtb_end(e);
1619
1620 if ( nlsr->debugging )
1621 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1622 if ( nlsr->detailed_logging )
1623 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1624
1625 set_new_lsdb_version();
1626
1627 if ( nlsr->debugging )
1628 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1629 if ( nlsr->detailed_logging )
1630 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1631
1632}
1633
1634int
1635delete_name_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1636{
1637 //printf("delete_name_lsa called \n");
1638
1639 if ( nlsr->debugging )
1640 printf("delete_name_lsa called \n");
1641 if ( nlsr->detailed_logging )
1642 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_name_lsa called \n");
1643
1644 if(flags == CCN_SCHEDULE_CANCEL)
1645 {
1646 return -1;
1647 }
1648
1649
1650
1651 nlsr_lock();
1652
1653 if ( nlsr->debugging )
1654 printf("LSA Key: %s \n",(char *)ev->evdata);
1655 if ( nlsr->detailed_logging )
1656 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Key: %s \n",(char *)ev->evdata);
1657
1658 struct nlsa *nlsa;
1659
1660 struct hashtb_enumerator ee;
1661 struct hashtb_enumerator *e = &ee;
1662
1663 int res;
1664
1665 hashtb_start(nlsr->lsdb->name_lsdb, e);
1666 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1667
1668 if( res == HT_OLD_ENTRY )
1669 {
1670 nlsa=e->data;
1671 delete_npt_entry_by_router_and_name_prefix(nlsa->header->orig_router->name, nlsa->name_prefix->name);
1672
1673 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1674 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1675 write_log_for_name_lsa(nlsa);
1676 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1677
1678 hashtb_delete(e);
1679 }
1680 else if( res == HT_NEW_ENTRY )
1681 {
1682 hashtb_delete(e);
1683 }
1684 hashtb_end(e);
1685
1686 if ( nlsr->debugging )
1687 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1688 if ( nlsr->detailed_logging )
1689 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1690
1691 set_new_lsdb_version();
1692
1693 if ( nlsr->debugging )
1694 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1695 if ( nlsr->detailed_logging )
1696 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1697
1698 //print_name_lsdb();
1699
1700 nlsr_unlock();
1701
1702 return 0;
1703}
1704
1705int
1706delete_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1707{
1708 //printf("delete_adj_lsa called \n");
1709
1710 if ( nlsr->debugging )
1711 printf("delete_adj_lsa called \n");
1712 if ( nlsr->detailed_logging )
1713 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_adj_lsa called \n");
1714
1715 if(flags == CCN_SCHEDULE_CANCEL)
1716 {
1717 return -1;
1718 }
1719 nlsr_lock();
1720
1721 //printf("LSA Key: %s \n",(char *)ev->evdata);
1722
1723 if ( nlsr->debugging )
1724 printf("LSA Key: %s \n",(char *)ev->evdata);
1725 if ( nlsr->detailed_logging )
1726 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Key: %s \n",(char *)ev->evdata);
1727
1728 struct alsa *alsa;
1729 struct hashtb_enumerator ee;
1730 struct hashtb_enumerator *e = &ee;
1731 int res;
1732
1733 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1734 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1735
1736 if( res == HT_OLD_ENTRY )
1737 {
1738 alsa=e->data;
1739 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1740 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
1741 write_log_for_adj_lsa(alsa);
1742 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1743
1744 hashtb_delete(e);
1745 }
1746 else if( res == HT_OLD_ENTRY )
1747 {
1748 hashtb_delete(e);
1749 }
1750 hashtb_end(e);
1751
1752 if ( nlsr->debugging )
1753 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1754 if ( nlsr->detailed_logging )
1755 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1756
1757 set_new_lsdb_version();
1758
1759 if ( nlsr->debugging )
1760 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1761 if ( nlsr->detailed_logging )
1762 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1763
1764
1765 if ( !nlsr->is_route_calculation_scheduled)
1766 {
1767 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
1768 nlsr->is_route_calculation_scheduled=1;
1769 }
1770
1771 //print_adj_lsdb();
1772
1773 nlsr_unlock();
1774
1775 return 0;
1776}
1777
1778void
1779refresh_name_lsdb(void)
1780{
1781 //printf("refresh_name_lsdb called \n");
1782
1783 if ( nlsr->debugging )
1784 printf("refresh_name_lsdb called \n");
1785 if ( nlsr->detailed_logging )
1786 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_name_lsdb called \n");
1787
1788 //int lsa_change_count=0;
1789
1790 char *time_stamp=(char *)malloc(20);
1791 memset(time_stamp,0,20);
1792 get_current_timestamp_micro(time_stamp);
1793
1794 long int lsa_life_time;
1795
1796 int i, name_lsdb_element;
1797 struct nlsa *name_lsa;
1798
1799 struct hashtb_enumerator ee;
1800 struct hashtb_enumerator *e = &ee;
1801
1802 hashtb_start(nlsr->lsdb->name_lsdb, e);
1803 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
1804
1805 for(i=0;i<name_lsdb_element;i++)
1806 {
1807 name_lsa=e->data;
1808
1809 lsa_life_time=get_time_diff(time_stamp,name_lsa->header->orig_time);
1810 if ( nlsr->debugging )
1811 printf("LSA Life Time: %ld \n",lsa_life_time);
1812 if ( nlsr->detailed_logging )
1813 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Life Time: %ld \n",lsa_life_time);
1814
1815 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name) == 0)
1816 {
1817 if ( lsa_life_time > nlsr->lsa_refresh_time )
1818 {
1819 if ( name_lsa->header->isValid == NAME_LSA_VALID )
1820 {
1821 if ( nlsr->debugging )
1822 printf("Own Name LSA need to be refrshed\n");
1823 if ( nlsr->detailed_logging )
1824 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Own Name LSA need to be refrshed\n");
1825
1826 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1827 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1828 write_log_for_name_lsa(name_lsa);
1829 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1830
1831
1832 char *current_time_stamp=(char *)malloc(20);
1833 memset(current_time_stamp,0,20);
1834 get_current_timestamp_micro(current_time_stamp);
1835
1836 free(name_lsa->header->orig_time);
1837 name_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1838 memset(name_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1839 memcpy(name_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1840
1841 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1842 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
1843 write_log_for_name_lsa(name_lsa);
1844 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1845
1846 free(current_time_stamp);
1847 }
1848 else
1849 {
1850 char lst[2];
1851 memset(lst,0,2);
1852 sprintf(lst,"%d",name_lsa->header->ls_type);
1853
1854 char lsid[10];
1855 memset(lsid,0,10);
1856 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1857
1858
1859 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1860 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1861
1862
1863 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
1864 //printf("Key:%s Length:%d\n",key,(int)strlen(key));
1865
1866 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
1867 }
1868
1869 if ( nlsr->debugging )
1870 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1871 if ( nlsr->detailed_logging )
1872 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1873
1874 set_new_lsdb_version();
1875
1876 if ( nlsr->debugging )
1877 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1878 if ( nlsr->detailed_logging )
1879 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1880
1881
1882
1883 print_name_lsdb();
1884 //lsa_change_count++;
1885 }
1886 }
1887 else
1888 {
1889 if ( lsa_life_time > nlsr->router_dead_interval )
1890 {
1891 if ( nlsr->debugging )
1892 printf("Others Name LSA need to be deleted\n");
1893 if ( nlsr->detailed_logging )
1894 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Others Name LSA need to be deleted\n");
1895
1896 char lst[2];
1897 memset(lst,0,2);
1898 sprintf(lst,"%d",name_lsa->header->ls_type);
1899
1900 char lsid[10];
1901 memset(lsid,0,10);
1902 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1903
1904
1905 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1906 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1907
1908
1909 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
1910 //printf("Key:%s Length:%d\n",key,(int)strlen(key));
1911
1912 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
1913 //lsa_change_count++;
1914 }
1915 }
1916
1917 hashtb_next(e);
1918 }
1919
1920 hashtb_end(e);
1921
1922 free(time_stamp);
1923
1924
1925}
1926
1927void
1928refresh_adj_lsdb(void)
1929{
1930 //printf("refresh_adj_lsdb called \n");
1931
1932 if ( nlsr->debugging )
1933 printf("refresh_adj_lsdb called \n");
1934 if ( nlsr->detailed_logging )
1935 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_adj_lsdb called \n");
1936
1937 char *time_stamp=(char *)malloc(20);
1938 memset(time_stamp,0,20);
1939 get_current_timestamp_micro(time_stamp);
1940
1941 long int lsa_life_time;
1942
1943 int i, adj_lsdb_element;
1944 struct alsa *adj_lsa;
1945
1946 struct hashtb_enumerator ee;
1947 struct hashtb_enumerator *e = &ee;
1948
1949 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1950 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1951
1952 for(i=0;i<adj_lsdb_element;i++)
1953 {
1954 adj_lsa=e->data;
1955
1956 lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
1957 //printf("LSA Life Time: %ld \n",lsa_life_time);
1958
1959 if ( nlsr->debugging )
1960 printf("LSA Life Time: %ld \n",lsa_life_time);
1961 if ( nlsr->detailed_logging )
1962 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Life Time: %ld \n",lsa_life_time);
1963
1964 if ( strcmp(adj_lsa->header->orig_router->name,nlsr->router_name) == 0)
1965 {
1966 if ( lsa_life_time > nlsr->lsa_refresh_time )
1967 {
1968 //printf("Own Adj LSA need to be refrshed\n");
1969 if ( nlsr->debugging )
1970 printf("Own Adj LSA need to be refrshed\n");
1971 if ( nlsr->detailed_logging )
1972 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Own Adj LSA need to be refrshed\n");
1973
1974 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1975 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
1976 write_log_for_adj_lsa(adj_lsa);
1977 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1978
1979 char *current_time_stamp=(char *)malloc(20);
1980 memset(current_time_stamp,0,20);
1981 get_current_timestamp_micro(current_time_stamp);
1982
1983 free(adj_lsa->header->orig_time);
1984 adj_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1985 memset(adj_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1986 memcpy(adj_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1987
1988 free(current_time_stamp);
1989
1990 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1991 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
1992 write_log_for_adj_lsa(adj_lsa);
1993 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1994
1995 if ( nlsr->debugging )
1996 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1997 if ( nlsr->detailed_logging )
1998 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1999
2000 set_new_lsdb_version();
2001
2002 if ( nlsr->debugging )
2003 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
2004 if ( nlsr->detailed_logging )
2005 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
2006
2007 print_adj_lsdb();
2008 }
2009 }
2010 else
2011 {
2012 if ( lsa_life_time > nlsr->router_dead_interval )
2013 {
2014 //printf("Others Adj LSA need to be deleted\n");
2015
2016 if ( nlsr->debugging )
2017 printf("Others Adj LSA need to be deleted\n");
2018 if ( nlsr->detailed_logging )
2019 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Others Adj LSA need to be deleted\n");
2020
2021 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
2022 memset(key,0,adj_lsa->header->orig_router->length+2);
2023 make_adj_lsa_key(key,adj_lsa);
2024 //printf("Adjacent LSA key: %s \n",key);
2025 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_adj_lsa, (void *)key, 0);
2026 }
2027 }
2028
2029
2030
2031 hashtb_next(e);
2032 }
2033
2034 hashtb_end(e);
2035
2036 free(time_stamp);
2037}
2038
2039int
2040refresh_lsdb(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
2041{
2042 if(flags == CCN_SCHEDULE_CANCEL)
2043 {
2044 return -1;
2045 }
2046
2047 nlsr_lock();
2048
2049 //printf("refresh_lsdb called \n");
2050
2051 if ( nlsr->debugging )
2052 printf("refresh_lsdb called\n");
2053 if ( nlsr->detailed_logging )
2054 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_lsdb called\n");
2055
2056 refresh_name_lsdb();
2057 refresh_adj_lsdb();
2058
2059 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
2060
2061 nlsr_unlock();
2062 return 0;
2063}
2064
2065void
akmhoque4ae16942012-12-10 11:50:43 -06002066write_adj_lsa_to_repo(char *repo_content_prefix, struct name_prefix *lsa_id)
2067{
2068 printf("Content Preifx: %s\n",repo_content_prefix);
2069
2070
2071 struct ccn_charbuf *lsa_data=ccn_charbuf_create();
2072 get_adj_lsa_data(lsa_data,lsa_id);
2073 printf("Name LSA Data: %s \n",ccn_charbuf_as_string(lsa_data));
2074
2075 write_data_to_repo(ccn_charbuf_as_string(lsa_data), repo_content_prefix);
2076
2077 ccn_charbuf_destroy(&lsa_data);
2078}
2079
2080void
akmhoque8fdd6412012-12-04 15:05:33 -06002081write_name_lsa_to_repo(char *repo_content_prefix, struct name_prefix *lsa_id)
2082{
2083 printf("Content Preifx: %s\n",repo_content_prefix);
2084
2085
2086 struct ccn_charbuf *lsa_data=ccn_charbuf_create();
2087 get_name_lsa_data(lsa_data,lsa_id);
2088 printf("Name LSA Data: %s \n",ccn_charbuf_as_string(lsa_data));
2089
2090 write_data_to_repo(ccn_charbuf_as_string(lsa_data), repo_content_prefix);
2091
2092 ccn_charbuf_destroy(&lsa_data);
2093}
2094
2095
2096void
2097write_name_lsdb_to_repo(char *slice_prefix)
2098{
2099 int i, name_lsdb_element;
2100
2101 struct nlsa *name_lsa;
2102 struct hashtb_enumerator ee;
2103 struct hashtb_enumerator *e = &ee;
2104
2105 hashtb_start(nlsr->lsdb->name_lsdb, e);
2106 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
2107
2108 for(i=0;i<name_lsdb_element;i++)
2109 {
2110 name_lsa=e->data;
2111
2112 char lst[2];
2113 memset(lst,0,2);
2114 sprintf(lst,"%d",name_lsa->header->ls_type);
2115
2116 char lsid[10];
2117 memset(lsid,0,10);
2118 sprintf(lsid,"%ld",name_lsa->header->ls_id);
2119
2120
2121 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
2122 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
2123
2124
2125 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
2126 printf("Name LSA Key: %s \n",key);
2127
2128
2129 char *repo_key=(char *)malloc(strlen(slice_prefix)+1+strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1+strlen(name_lsa->header->orig_time)+1);
2130 memset(repo_key,0,strlen(slice_prefix)+1+strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1+strlen(name_lsa->header->orig_time)+1);
2131 make_name_lsa_prefix_for_repo(repo_key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id,name_lsa->header->orig_time,slice_prefix);
2132
2133 printf("Name LSA Repo Key: %s \n",repo_key);
2134
2135 struct name_prefix *lsaid=(struct name_prefix *)malloc(sizeof(struct name_prefix));
2136 lsaid->name=(char *)malloc(strlen(key)+1);
2137 memset(lsaid->name,strlen(key)+1,0);
2138 memcpy(lsaid->name,key,strlen(key));
2139 lsaid->length=strlen(key)+1;
2140
2141
2142 write_name_lsa_to_repo(repo_key, lsaid);
2143
2144 free(key);
2145 free(repo_key);
2146 free(lsaid->name);
2147 free(lsaid);
2148
2149 hashtb_next(e);
2150 }
2151
2152 hashtb_end(e);
2153
2154
2155}