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