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