blob: a2995d33038afb70a6a24c595be5c0d9747ae88d [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);
679
680 printf("Adj LSA Repo Key: %s \n",repo_con_name);
681
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);
Syed Obaid Amin78e9aa92012-12-21 03:57:13 -0600685 printf("Adj LSA: %s \n",key);
akmhoque4ae16942012-12-10 11:50:43 -0600686 struct name_prefix *lsaid=(struct name_prefix *)malloc(sizeof(struct name_prefix));
687 lsaid->name=(char *)malloc(strlen(key)+1);
Syed Obaid Amin4c959562012-12-21 16:43:21 -0600688 memset(lsaid->name, 0, strlen(key)+1);
akmhoque4ae16942012-12-10 11:50:43 -0600689 memcpy(lsaid->name,key,strlen(key));
690 lsaid->length=strlen(key)+1;
691
692
693 //write_adj_lsa_to_repo(repo_key, lsaid);
694
695 //free(key);
696 //free(repo_con_name);
697
akmhoque8fdd6412012-12-04 15:05:33 -0600698 free(adj_lsa->header->orig_router->name);
699 free(adj_lsa->header->orig_router);
700 free(adj_lsa->header->orig_time);
701 free(adj_lsa->header);
702 free(adj_lsa->body);
703 free(adj_lsa);
704 nlsr->adj_build_flag=0;
705 print_adj_lsdb();
706 }
707 else
708 {
709 if ( nlsr->debugging )
710 printf("Can not build adj LSA now\n");
711 if ( nlsr->detailed_logging )
712 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Can not build adj LSA now\n");
713 }
714 }
715 nlsr->is_build_adj_lsa_sheduled=0;
716
717 nlsr_unlock();
718
719 return 0;
720}
721
722
723void
724build_adj_lsa(struct alsa * adj_lsa)
725{
726 if ( nlsr->debugging )
727 printf("build_adj_lsa called \n");
728 if ( nlsr->detailed_logging )
729 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_adj_lsa called \n");
730
731 int no_link=no_active_nbr();
732
733 //printf("Number of link in Adjacent LSA: %d\n",no_link);
734
735 /*Filling Up Header Data */
736 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
737 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
738 adj_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1);
739 memset(adj_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
740 memcpy(adj_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
741 adj_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
742
743 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
744
745 char *time_stamp=(char *)malloc(20);
746 memset(time_stamp,0,20);
747 get_current_timestamp_micro(time_stamp);
748
749 adj_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1);
750 memset(adj_lsa->header->orig_time,0,strlen(time_stamp)+1);
751 memcpy(adj_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
752 free(time_stamp);
753
754
755 /* Filling Up Body Data */
756
757 adj_lsa->no_link=no_link;
758
759
760 struct ccn_charbuf *c=ccn_charbuf_create();
761 get_active_nbr_adj_data(c);
762 char *data=ccn_charbuf_as_string(c);
763
764 adj_lsa->body=(char *)malloc(strlen(data)+1);
765 memset(adj_lsa->body,0,strlen(data)+1);
766 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
767 ccn_charbuf_destroy(&c);
768
769
770
akmhoque4ae16942012-12-10 11:50:43 -0600771 /*if( !nlsr->is_send_lsdb_interest_scheduled )
akmhoque8fdd6412012-12-04 15:05:33 -0600772 {
773 nlsr->event_send_lsdb_interest= ccn_schedule_event(nlsr->sched, 1000, &send_lsdb_interest, NULL, 0);
774 nlsr->is_send_lsdb_interest_scheduled=1;
akmhoque4ae16942012-12-10 11:50:43 -0600775 }*/
akmhoque8fdd6412012-12-04 15:05:33 -0600776
777 nlsr->adj_build_count++;
778
779
780}
781
782
783void
784install_adj_lsa(struct alsa * adj_lsa)
785{
786
787 if ( nlsr->debugging )
788 printf("install_adj_lsa called \n");
789 if ( nlsr->detailed_logging )
790 writeLogg(__FILE__,__FUNCTION__,__LINE__,"install_adj_lsa called \n");
791
792
793 char *time_stamp=(char *)malloc(20);
794 memset(time_stamp,0,20);
795 get_current_timestamp_micro(time_stamp);
796 //long int lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
797
798 //printf("time difference: %ld \n",lsa_life_time);
799
800
801 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
802 memset(key,0,adj_lsa->header->orig_router->length+2);
803 make_adj_lsa_key(key,adj_lsa);
804 //printf("Adjacent LSA key: %s \n",key);
805
806 struct alsa *new_adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
807
808 struct hashtb_enumerator ee;
809 struct hashtb_enumerator *e = &ee;
810 int res;
811
812 hashtb_start(nlsr->lsdb->adj_lsdb, e);
813 res = hashtb_seek(e, key, strlen(key), 0);
814
815
816
817 if(res == HT_NEW_ENTRY)
818 {
819 if ( adj_lsa->no_link > 0)
820 {
821 if ( nlsr->debugging )
822 printf("New ADJ LSA... Adding to LSDB\n");
823 if ( nlsr->detailed_logging )
824 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New ADJ LSA... Adding to LSDB\n");
825 new_adj_lsa = e->data;
826
827 new_adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
828 new_adj_lsa->header->ls_type=adj_lsa->header->ls_type;
829 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
830 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
831
832 new_adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
833 new_adj_lsa->header->orig_router->name=(char *)malloc(adj_lsa->header->orig_router->length);
834 memcpy(new_adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
835 new_adj_lsa->header->orig_router->length=adj_lsa->header->orig_router->length;
836
837 new_adj_lsa->no_link=adj_lsa->no_link;
838
839 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
840 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
841 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
842
843 add_next_hop_router(new_adj_lsa->header->orig_router->name);
844 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
845
846 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
847 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
848 write_log_for_adj_lsa(new_adj_lsa);
849 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
850 }
851 else
852 {
853 hashtb_delete(e);
854 }
855
856 if ( nlsr->debugging )
857 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
858 if ( nlsr->detailed_logging )
859 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
860
861 set_new_lsdb_version();
862
863 if ( nlsr->debugging )
864 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
865 if ( nlsr->detailed_logging )
866 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
867
868 }
869 else if(res == HT_OLD_ENTRY)
870 {
871 new_adj_lsa = e->data;
872 if(strcmp(adj_lsa->header->orig_time,new_adj_lsa->header->orig_time)<=0)
873 {
874 if ( nlsr->debugging )
875 printf("Older/Duplicate Adj LSA. Discarded...\n");
876 if ( nlsr->detailed_logging )
877 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Older/Duplicate Adj LSA. Discarded...\n");
878 }
879 else
880 {
881
882 if ( adj_lsa->no_link > 0)
883 {
884 //new_adj_lsa = e->data;
885
886 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
887 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
888 write_log_for_adj_lsa(new_adj_lsa);
889 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
890
891 free(new_adj_lsa->header->orig_time);
892 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
893 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
894
895 new_adj_lsa->no_link=adj_lsa->no_link;
896
897 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
898 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
899 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
900
901 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
902
903 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
904 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
905 write_log_for_adj_lsa(new_adj_lsa);
906 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
907 }
908 else
909 {
910 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
911 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
912 write_log_for_adj_lsa(new_adj_lsa);
913 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
914
915 hashtb_delete(e);
916 }
917
918 if ( nlsr->debugging )
919 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
920 if ( nlsr->detailed_logging )
921 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
922
923 set_new_lsdb_version();
924
925 if ( nlsr->debugging )
926 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
927 if ( nlsr->detailed_logging )
928 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
929 }
930
931 }
932 hashtb_end(e);
933
934 if ( !nlsr->is_route_calculation_scheduled )
935 {
936 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
937 nlsr->is_route_calculation_scheduled=1;
938 }
939
940
941 free(key);
942
943 free(time_stamp);
944}
945
946void
947write_log_for_adj_lsa_body(const char *body, int no_link)
948{
949 int i=0;
950 char *lsa_data=(char *)malloc(strlen(body)+1);
951 memset( lsa_data,0,strlen(body)+1);
952 memcpy(lsa_data,body,strlen(body)+1);
953 char *sep="|";
954 char *rem;
955 char *rtr_id;
956 char *length;
957 char *face;
958 char *metric;
959
960 if(no_link >0 )
961 {
962 rtr_id=strtok_r(lsa_data,sep,&rem);
963 length=strtok_r(NULL,sep,&rem);
964 face=strtok_r(NULL,sep,&rem);
965 metric=strtok_r(NULL,sep,&rem);
966
967 writeLogg(__FILE__,__FUNCTION__,__LINE__," Link %d \n",i+1);
968 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router: %s \n",rtr_id);
969 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router Length: %s \n",length);
970 writeLogg(__FILE__,__FUNCTION__,__LINE__," Connecting Face: %s \n",face);
971 writeLogg(__FILE__,__FUNCTION__,__LINE__," Metric: %s \n",metric);
972
973
974 for(i=1;i<no_link;i++)
975 {
976 rtr_id=strtok_r(NULL,sep,&rem);
977 length=strtok_r(NULL,sep,&rem);
978 face=strtok_r(NULL,sep,&rem);
979 metric=strtok_r(NULL,sep,&rem);
980 writeLogg(__FILE__,__FUNCTION__,__LINE__," Link %d \n",i+1);
981 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router: %s \n",rtr_id);
982 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router Length: %s \n",length);
983 writeLogg(__FILE__,__FUNCTION__,__LINE__," Connecting Face: %s \n",face);
984 writeLogg(__FILE__,__FUNCTION__,__LINE__," Metric: %s \n",metric);
985
986 }
987 }
988
989 free(lsa_data);
990}
991
992
993void
994write_log_for_adj_lsa(struct alsa * adj_lsa)
995{
996 writeLogg(__FILE__,__FUNCTION__,__LINE__,"-----------Adj LSA Content---------------\n");
997 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s\n",adj_lsa->header->orig_router->name);
998 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
999 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Type: %d\n",adj_lsa->header->ls_type);
1000 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Time: %s\n",adj_lsa->header->orig_time);
1001 writeLogg(__FILE__,__FUNCTION__,__LINE__," Lsa Data:\n");
1002 writeLogg(__FILE__,__FUNCTION__,__LINE__," No of Link: %d\n",adj_lsa->no_link);
1003
1004 write_log_for_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
1005
1006 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
1007
1008}
1009
1010void
1011print_adj_lsa_body(const char *body, int no_link)
1012{
1013 int i=0;
1014 char *lsa_data=(char *)malloc(strlen(body)+1);
1015 memset( lsa_data,0,strlen(body)+1);
1016 memcpy(lsa_data,body,strlen(body)+1);
1017 char *sep="|";
1018 char *rem;
1019 char *rtr_id;
1020 char *length;
1021 char *face;
1022 char *metric;
1023
1024 if(no_link >0 )
1025 {
1026 rtr_id=strtok_r(lsa_data,sep,&rem);
1027 length=strtok_r(NULL,sep,&rem);
1028 face=strtok_r(NULL,sep,&rem);
1029 metric=strtok_r(NULL,sep,&rem);
1030
1031 printf(" Link %d \n",i+1);
1032 printf(" Neighbor : %s \n",rtr_id);
1033 printf(" Neighbor Length : %s \n",length);
1034 printf(" Connecting Face : %s \n",face);
1035 printf(" Metric : %s \n",metric);
1036
1037
1038 for(i=1;i<no_link;i++)
1039 {
1040 rtr_id=strtok_r(NULL,sep,&rem);
1041 length=strtok_r(NULL,sep,&rem);
1042 face=strtok_r(NULL,sep,&rem);
1043 metric=strtok_r(NULL,sep,&rem);
1044 printf(" Link %d \n",i+1);
1045 printf(" Neighbor : %s \n",rtr_id);
1046 printf(" Neighbor Length : %s \n",length);
1047 printf(" Connecting Face : %s \n",face);
1048 printf(" Metric : %s \n",metric);
1049
1050 }
1051 }
1052
1053 free(lsa_data);
1054}
1055
1056void
1057print_adj_lsa(struct alsa * adj_lsa)
1058{
1059 if ( nlsr->debugging )
1060 {
1061 printf("-----------ADJ LSA Content---------------\n");
1062 printf(" Origination Router : %s\n",adj_lsa->header->orig_router->name);
1063 printf(" Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
1064 printf(" LS Type : %d\n",adj_lsa->header->ls_type);
1065 printf(" Origination Time : %s\n",adj_lsa->header->orig_time);
1066 printf(" Lsa Data:\n");
1067 printf(" No of Link : %d\n",adj_lsa->no_link);
1068
1069 print_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
1070 printf("\n");
1071 }
1072
1073}
1074
1075void
1076print_adj_lsdb(void)
1077{
1078 if ( nlsr->debugging )
1079 printf("print_name_lsdb called \n");
1080 int i, adj_lsdb_element;
1081 struct alsa *adj_lsa;
1082
1083 struct hashtb_enumerator ee;
1084 struct hashtb_enumerator *e = &ee;
1085
1086 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1087 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1088
1089 for(i=0;i<adj_lsdb_element;i++)
1090 {
1091 if ( nlsr->debugging )
1092 printf("-----------Adj LSA (%d)---------------\n",i+1);
1093 adj_lsa=e->data;
1094 print_adj_lsa(adj_lsa);
1095 hashtb_next(e);
1096 }
1097
1098 hashtb_end(e);
1099
1100 if ( nlsr->debugging )
1101 printf("\n");
1102 if ( nlsr->detailed_logging )
1103 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
1104}
1105
1106void
1107build_and_install_others_adj_lsa(char *orig_router,int ls_type,char *orig_time, int no_link,char *data)
1108{
1109 if ( nlsr->debugging )
1110 printf("build_and_install_others_adj_lsa called \n");
1111 if ( nlsr->detailed_logging )
1112 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_others_adj_lsa called \n");
1113 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
1114 build_others_adj_lsa(adj_lsa,orig_router,ls_type,orig_time,no_link,data);
1115 //print_adj_lsa(adj_lsa);
1116 install_adj_lsa(adj_lsa);
1117
1118
1119 free(adj_lsa->header->orig_router->name);
1120 free(adj_lsa->header->orig_router);
1121 free(adj_lsa->header->orig_time);
1122 free(adj_lsa->header);
1123 free(adj_lsa->body);
1124 free(adj_lsa);
1125
1126 print_adj_lsdb();
1127
1128}
1129
1130
1131void
1132build_others_adj_lsa(struct alsa *adj_lsa,char *orig_router,int ls_type,char *orig_time,int no_link,char *data)
1133{
1134 //printf("build_others_adj_lsa called \n");
1135 if ( nlsr->debugging )
1136 printf("build_others_adj_lsa called \n");
1137 if ( nlsr->detailed_logging )
1138 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_others_adj_lsa called \n");
1139
1140 /*Filling Up Header Data */
1141 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
1142 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
1143 adj_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
1144 memset(adj_lsa->header->orig_router->name,0,strlen(orig_router)+1);
1145 memcpy(adj_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
1146
1147 adj_lsa->header->orig_router->length=strlen(orig_router)+1;
1148
1149
1150 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
1151
1152 adj_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
1153 memset(adj_lsa->header->orig_time,0,strlen(orig_time)+1);
1154 memcpy(adj_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
1155
1156 adj_lsa->no_link=no_link;
1157
1158 adj_lsa->body=(char *)malloc(strlen(data)+1);
1159 memset(adj_lsa->body,0,strlen(data)+1);
1160 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
1161
1162}
1163
1164
1165long int
1166get_name_lsdb_num_element(void)
1167{
1168 long int num_element;
1169
1170
1171 struct hashtb_enumerator ee;
1172 struct hashtb_enumerator *e = &ee;
1173
1174 hashtb_start(nlsr->lsdb->name_lsdb, e);
1175 num_element=hashtb_n(nlsr->lsdb->name_lsdb);
1176 hashtb_end(e);
1177
1178 return num_element;
1179}
1180
1181long int
1182get_adj_lsdb_num_element(void)
1183{
1184 long int num_element;
1185
1186
1187 struct hashtb_enumerator ee;
1188 struct hashtb_enumerator *e = &ee;
1189
1190 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1191 num_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1192 hashtb_end(e);
1193
1194 return num_element;
1195}
1196
1197void
1198get_name_lsdb_summary(struct ccn_charbuf *name_lsdb_data)
1199{
1200 //printf("get_name_lsdb_summary called \n");
1201 if ( nlsr->debugging )
1202 printf("get_name_lsdb_summary called \n");
1203 if ( nlsr->detailed_logging )
1204 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_name_lsdb_summary called \n");
1205
1206 int i, name_lsdb_element;
1207
1208 struct nlsa *name_lsa;
1209 struct hashtb_enumerator ee;
1210 struct hashtb_enumerator *e = &ee;
1211
1212 hashtb_start(nlsr->lsdb->name_lsdb, e);
1213 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
1214
1215 for(i=0;i<name_lsdb_element;i++)
1216 {
1217 name_lsa=e->data;
1218
1219 ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_router->name);
1220 ccn_charbuf_append_string(name_lsdb_data,"|");
1221
1222 char *lst=(char *)malloc(20);
1223 memset(lst,0,20);
1224 sprintf(lst,"%d",name_lsa->header->ls_type);
1225 ccn_charbuf_append_string(name_lsdb_data,lst);
1226 free(lst);
1227 ccn_charbuf_append_string(name_lsdb_data,"|");
1228
1229 char *lsid=(char *)malloc(20);
1230 memset(lsid,0,20);
1231 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1232 ccn_charbuf_append_string(name_lsdb_data,lsid);
1233 free(lsid);
1234 ccn_charbuf_append_string(name_lsdb_data,"|");
1235
1236 ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_time);
1237 ccn_charbuf_append_string(name_lsdb_data,"|");
1238
1239 hashtb_next(e);
1240 }
1241
1242 hashtb_end(e);
1243
1244}
1245
1246
1247void
1248get_adj_lsdb_summary(struct ccn_charbuf *adj_lsdb_data)
1249{
1250 if ( nlsr->debugging )
1251 printf("get_adj_lsdb_summary called \n");
1252 if ( nlsr->detailed_logging )
1253 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_adj_lsdb_summary called \n");
1254 int i, adj_lsdb_element;
1255 struct alsa *adj_lsa;
1256
1257 struct hashtb_enumerator ee;
1258 struct hashtb_enumerator *e = &ee;
1259
1260 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1261 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1262
1263 for(i=0;i<adj_lsdb_element;i++)
1264 {
1265 adj_lsa=e->data;
1266
1267 ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_router->name);
1268 ccn_charbuf_append_string(adj_lsdb_data,"|");
1269
1270 char *lst=(char *)malloc(20);
1271 memset(lst,0,20);
1272 sprintf(lst,"%d",adj_lsa->header->ls_type);
1273 ccn_charbuf_append_string(adj_lsdb_data,lst);
1274 free(lst);
1275 ccn_charbuf_append_string(adj_lsdb_data,"|");
1276
1277 ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_time);
1278 ccn_charbuf_append_string(adj_lsdb_data,"|");
1279
1280 hashtb_next(e);
1281 }
1282
1283 hashtb_end(e);
1284}
1285
1286
1287void
1288get_lsdb_summary(struct ccn_charbuf *lsdb_data)
1289{
1290 struct ccn_charbuf *name_lsdb_data=ccn_charbuf_create();
1291 struct ccn_charbuf *adj_lsdb_data=ccn_charbuf_create();
1292
1293 get_name_lsdb_summary(name_lsdb_data);
1294 get_adj_lsdb_summary(adj_lsdb_data);
1295
1296 long int num_lsa=get_name_lsdb_num_element() + get_adj_lsdb_num_element();
1297 char *num_element=(char *)malloc(15);
1298 memset(num_element,0,15);
1299 sprintf(num_element,"%ld",num_lsa);
1300
1301 if( num_lsa > 0)
1302 {
1303 ccn_charbuf_append_string(lsdb_data,num_element);
1304 ccn_charbuf_append_string(lsdb_data,"|");
1305 }
1306 if(name_lsdb_data->length>0)
1307 {
1308 char *data1=ccn_charbuf_as_string(name_lsdb_data);
1309 ccn_charbuf_append_string(lsdb_data,(char *)data1);
1310 }
1311 if(adj_lsdb_data->length>0)
1312 {
1313 char *data2=ccn_charbuf_as_string(adj_lsdb_data);
1314 ccn_charbuf_append_string(lsdb_data,(char *)data2);
1315 }
1316 ccn_charbuf_destroy(&name_lsdb_data);
1317 ccn_charbuf_destroy(&adj_lsdb_data);
1318 free(num_element);
1319
1320}
1321
1322int
1323check_is_new_name_lsa(char *orig_router,char *lst,char *lsid,char *orig_time)
1324{
1325 int ret=0;
1326 struct ccn_charbuf *key=ccn_charbuf_create();
1327 ccn_charbuf_append_string(key,orig_router);
1328 ccn_charbuf_append_string(key,"/");
1329 ccn_charbuf_append_string(key,lst);
1330 ccn_charbuf_append_string(key,"/");
1331 ccn_charbuf_append_string(key,lsid);
1332
1333 int res;
1334 struct nlsa *name_lsa;
1335
1336 struct hashtb_enumerator ee;
1337 struct hashtb_enumerator *e = &ee;
1338
1339 hashtb_start(nlsr->lsdb->name_lsdb, e);
1340 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1341
1342 if( res == HT_NEW_ENTRY )
1343 {
1344 hashtb_delete(e);
1345 ret=1;
1346
1347 }
1348 else if(res == HT_OLD_ENTRY)
1349 {
1350 name_lsa=e->data;
1351 if( strcmp ( orig_time , name_lsa->header->orig_time ) > 0 )
1352 {
1353 ret=1;
1354 }
1355 }
1356
1357 hashtb_end(e);
1358
1359 ccn_charbuf_destroy(&key);
1360
1361 return ret;
1362}
1363
1364int
1365check_is_new_adj_lsa(char *orig_router,char *lst,char *orig_time)
1366{
1367 int ret=0;
1368 struct ccn_charbuf *key=ccn_charbuf_create();
1369 ccn_charbuf_append_string(key,orig_router);
1370 ccn_charbuf_append_string(key,"/");
1371 ccn_charbuf_append_string(key,lst);
1372
1373 int res;
1374 struct alsa *adj_lsa;
1375
1376 struct hashtb_enumerator ee;
1377 struct hashtb_enumerator *e = &ee;
1378
1379 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1380 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1381
1382 if( res == HT_NEW_ENTRY )
1383 {
1384 hashtb_delete(e);
1385 ret=1;
1386
1387 }
1388 else if(res == HT_OLD_ENTRY)
1389 {
1390 adj_lsa=e->data;
1391 if( strcmp ( orig_time , adj_lsa->header->orig_time ) > 0 )
1392 {
1393 ret=1;
1394 }
1395 }
1396
1397 hashtb_end(e);
1398
1399 ccn_charbuf_destroy(&key);
1400
1401 return ret;
1402}
1403
1404void
1405get_name_lsa_data(struct ccn_charbuf *lsa_data, struct name_prefix *lsaId)
1406{
1407 //printf("get_name_lsa_data called \n");
1408 if ( nlsr->debugging )
1409 printf("get_name_lsa_data called \n");
1410 if ( nlsr->detailed_logging )
1411 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_name_lsa_data called \n");
1412
1413 struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa ));
1414
1415 struct hashtb_enumerator ee;
1416 struct hashtb_enumerator *e = &ee;
1417 int res;
1418
1419 hashtb_start(nlsr->lsdb->name_lsdb, e);
1420 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1421
1422 if( res == HT_OLD_ENTRY )
1423 {
1424 name_lsa=e->data;
1425 //printf("NAME LSA found\n");
1426
1427 if ( nlsr->debugging )
1428 printf("NAME LSA found \n");
1429 if ( nlsr->detailed_logging )
1430 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Name LSA found \n");
1431
1432 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_router->name);
1433 ccn_charbuf_append_string(lsa_data,"|");
1434
1435 char *temp_length=(char *)malloc(20);
1436 memset(temp_length,0,20);
1437 sprintf(temp_length,"%d",name_lsa->header->orig_router->length);
1438 ccn_charbuf_append_string(lsa_data,temp_length);
1439 free(temp_length);
1440 ccn_charbuf_append_string(lsa_data,"|");
1441
1442 char *temp_ltype=(char *)malloc(20);
1443 memset(temp_ltype,0,20);
1444 sprintf(temp_ltype,"%d",name_lsa->header->ls_type);
1445 ccn_charbuf_append_string(lsa_data,temp_ltype);
1446 free(temp_ltype);
1447 ccn_charbuf_append_string(lsa_data,"|");
1448
1449 char *temp_lsid=(char *)malloc(20);
1450 memset(temp_lsid,0,20);
1451 sprintf(temp_lsid,"%ld",name_lsa->header->ls_id);
1452 ccn_charbuf_append_string(lsa_data,temp_lsid);
1453 free(temp_lsid);
1454 ccn_charbuf_append_string(lsa_data,"|");
1455
1456 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_time);
1457 ccn_charbuf_append_string(lsa_data,"|");
1458
1459 char *temp_valid=(char *)malloc(20);
1460 memset(temp_valid,0,20);
1461 sprintf(temp_valid,"%d",name_lsa->header->isValid);
1462 ccn_charbuf_append_string(lsa_data,temp_valid);
1463 free(temp_valid);
1464 ccn_charbuf_append_string(lsa_data,"|");
1465
1466 ccn_charbuf_append_string(lsa_data,name_lsa->name_prefix->name);
1467 ccn_charbuf_append_string(lsa_data,"|");
1468
1469 char *temp_npl=(char *)malloc(20);
1470 memset(temp_npl,0,20);
1471 sprintf(temp_npl,"%d",name_lsa->name_prefix->length);
1472 ccn_charbuf_append_string(lsa_data,temp_npl);
1473 free(temp_npl);
1474 ccn_charbuf_append_string(lsa_data,"|");
1475
1476 }
1477 else if(res == HT_NEW_ENTRY)
1478 {
1479 hashtb_delete(e);
1480 }
1481
1482 hashtb_end(e);
1483}
1484
1485void
1486get_adj_lsa_data(struct ccn_charbuf *lsa_data,struct name_prefix *lsaId)
1487{
1488 if ( nlsr->debugging )
1489 printf("get_adj_lsa_data called \n");
1490 if ( nlsr->detailed_logging )
1491 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_adj_lsa_data called \n");
1492
1493 struct alsa *adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
1494
1495 struct hashtb_enumerator ee;
1496 struct hashtb_enumerator *e = &ee;
1497 int res;
1498
1499 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1500 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1501
1502 if( res == HT_OLD_ENTRY )
1503 {
1504 adj_lsa=e->data;
1505 //printf("Adj LSA found\n");
1506
1507 if ( nlsr->debugging )
1508 printf("Adj LSA found \n");
1509 if ( nlsr->detailed_logging )
1510 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adj LSA found \n");
1511
1512 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_router->name);
1513 ccn_charbuf_append_string(lsa_data,"|");
1514
1515 char *temp_length=(char *)malloc(20);
1516 memset(temp_length,0,20);
1517 sprintf(temp_length,"%d",adj_lsa->header->orig_router->length);
1518 ccn_charbuf_append_string(lsa_data,temp_length);
1519 free(temp_length);
1520 ccn_charbuf_append_string(lsa_data,"|");
1521
1522 char *temp_ltype=(char *)malloc(20);
1523 memset(temp_ltype,0,20);
1524 sprintf(temp_ltype,"%d",adj_lsa->header->ls_type);
1525 ccn_charbuf_append_string(lsa_data,temp_ltype);
1526 free(temp_ltype);
1527 ccn_charbuf_append_string(lsa_data,"|");
1528
1529 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_time);
1530 ccn_charbuf_append_string(lsa_data,"|");
1531
1532 char *temp_nl=(char *)malloc(20);
1533 memset(temp_nl,0,20);
1534 sprintf(temp_nl,"%d",adj_lsa->no_link);
1535 ccn_charbuf_append_string(lsa_data,temp_nl);
1536 free(temp_nl);
1537 ccn_charbuf_append_string(lsa_data,"|");
1538
1539 ccn_charbuf_append_string(lsa_data,adj_lsa->body);
1540
1541
1542 }
1543 else if(res == HT_NEW_ENTRY)
1544 {
1545 hashtb_delete(e);
1546 }
1547
1548 hashtb_end(e);
1549}
1550
1551void
1552make_name_lsa_invalid(struct name_prefix *np,int ls_type, long int ls_id)
1553{
1554
1555 if ( nlsr->debugging )
1556 printf("make_name_lsa_invalid called \n");
1557 if ( nlsr->detailed_logging )
1558 writeLogg(__FILE__,__FUNCTION__,__LINE__,"make_name_lsa_invalid called \n");
1559
1560
1561 char lst[2];
1562 memset(lst,0,2);
1563 sprintf(lst,"%d",ls_type);
1564
1565 char lsid[10];
1566 memset(lsid,0,10);
1567 sprintf(lsid,"%ld",ls_id);
1568
1569
1570 char *key=(char *)malloc(strlen(np->name)+1+strlen(lst)+1+strlen(lsid)+1);
1571 memset(key,0,strlen(np->name)+1+strlen(lst)+1+strlen(lsid)+1);
1572
1573
1574 make_name_lsa_key(key, np->name,ls_type,ls_id);
1575 printf("Key:%s Length:%d\n",key,(int)strlen(key));
1576
1577 struct nlsa *nlsa;
1578
1579 struct hashtb_enumerator ee;
1580 struct hashtb_enumerator *e = &ee;
1581
1582 int res;
1583
1584 hashtb_start(nlsr->lsdb->name_lsdb, e);
1585 res = hashtb_seek(e, key,strlen(key) , 0);
1586
1587 if( res == HT_OLD_ENTRY )
1588 {
1589 nlsa=e->data;
1590
1591 nlsa->header->isValid=0;
1592
1593 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1594 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1595 write_log_for_name_lsa(nlsa);
1596 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1597
1598 hashtb_delete(e);
1599 }
1600 else if( res == HT_NEW_ENTRY )
1601 {
1602 hashtb_delete(e);
1603 }
1604 hashtb_end(e);
1605
1606 if ( nlsr->debugging )
1607 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1608 if ( nlsr->detailed_logging )
1609 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1610
1611 set_new_lsdb_version();
1612
1613 if ( nlsr->debugging )
1614 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1615 if ( nlsr->detailed_logging )
1616 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1617
1618}
1619
1620int
1621delete_name_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1622{
1623 //printf("delete_name_lsa called \n");
1624
1625 if ( nlsr->debugging )
1626 printf("delete_name_lsa called \n");
1627 if ( nlsr->detailed_logging )
1628 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_name_lsa called \n");
1629
1630 if(flags == CCN_SCHEDULE_CANCEL)
1631 {
1632 return -1;
1633 }
1634
1635
1636
1637 nlsr_lock();
1638
1639 if ( nlsr->debugging )
1640 printf("LSA Key: %s \n",(char *)ev->evdata);
1641 if ( nlsr->detailed_logging )
1642 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Key: %s \n",(char *)ev->evdata);
1643
1644 struct nlsa *nlsa;
1645
1646 struct hashtb_enumerator ee;
1647 struct hashtb_enumerator *e = &ee;
1648
1649 int res;
1650
1651 hashtb_start(nlsr->lsdb->name_lsdb, e);
1652 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1653
1654 if( res == HT_OLD_ENTRY )
1655 {
1656 nlsa=e->data;
1657 delete_npt_entry_by_router_and_name_prefix(nlsa->header->orig_router->name, nlsa->name_prefix->name);
1658
1659 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1660 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1661 write_log_for_name_lsa(nlsa);
1662 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1663
1664 hashtb_delete(e);
1665 }
1666 else if( res == HT_NEW_ENTRY )
1667 {
1668 hashtb_delete(e);
1669 }
1670 hashtb_end(e);
1671
1672 if ( nlsr->debugging )
1673 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1674 if ( nlsr->detailed_logging )
1675 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1676
1677 set_new_lsdb_version();
1678
1679 if ( nlsr->debugging )
1680 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1681 if ( nlsr->detailed_logging )
1682 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1683
1684 //print_name_lsdb();
1685
1686 nlsr_unlock();
1687
1688 return 0;
1689}
1690
1691int
1692delete_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1693{
1694 //printf("delete_adj_lsa called \n");
1695
1696 if ( nlsr->debugging )
1697 printf("delete_adj_lsa called \n");
1698 if ( nlsr->detailed_logging )
1699 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_adj_lsa called \n");
1700
1701 if(flags == CCN_SCHEDULE_CANCEL)
1702 {
1703 return -1;
1704 }
1705 nlsr_lock();
1706
1707 //printf("LSA Key: %s \n",(char *)ev->evdata);
1708
1709 if ( nlsr->debugging )
1710 printf("LSA Key: %s \n",(char *)ev->evdata);
1711 if ( nlsr->detailed_logging )
1712 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Key: %s \n",(char *)ev->evdata);
1713
1714 struct alsa *alsa;
1715 struct hashtb_enumerator ee;
1716 struct hashtb_enumerator *e = &ee;
1717 int res;
1718
1719 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1720 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1721
1722 if( res == HT_OLD_ENTRY )
1723 {
1724 alsa=e->data;
1725 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1726 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
1727 write_log_for_adj_lsa(alsa);
1728 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1729
1730 hashtb_delete(e);
1731 }
1732 else if( res == HT_OLD_ENTRY )
1733 {
1734 hashtb_delete(e);
1735 }
1736 hashtb_end(e);
1737
1738 if ( nlsr->debugging )
1739 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1740 if ( nlsr->detailed_logging )
1741 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1742
1743 set_new_lsdb_version();
1744
1745 if ( nlsr->debugging )
1746 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1747 if ( nlsr->detailed_logging )
1748 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1749
1750
1751 if ( !nlsr->is_route_calculation_scheduled)
1752 {
1753 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
1754 nlsr->is_route_calculation_scheduled=1;
1755 }
1756
1757 //print_adj_lsdb();
1758
1759 nlsr_unlock();
1760
1761 return 0;
1762}
1763
1764void
1765refresh_name_lsdb(void)
1766{
1767 //printf("refresh_name_lsdb called \n");
1768
1769 if ( nlsr->debugging )
1770 printf("refresh_name_lsdb called \n");
1771 if ( nlsr->detailed_logging )
1772 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_name_lsdb called \n");
1773
1774 //int lsa_change_count=0;
1775
1776 char *time_stamp=(char *)malloc(20);
1777 memset(time_stamp,0,20);
1778 get_current_timestamp_micro(time_stamp);
1779
1780 long int lsa_life_time;
1781
1782 int i, name_lsdb_element;
1783 struct nlsa *name_lsa;
1784
1785 struct hashtb_enumerator ee;
1786 struct hashtb_enumerator *e = &ee;
1787
1788 hashtb_start(nlsr->lsdb->name_lsdb, e);
1789 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
1790
1791 for(i=0;i<name_lsdb_element;i++)
1792 {
1793 name_lsa=e->data;
1794
1795 lsa_life_time=get_time_diff(time_stamp,name_lsa->header->orig_time);
1796 if ( nlsr->debugging )
1797 printf("LSA Life Time: %ld \n",lsa_life_time);
1798 if ( nlsr->detailed_logging )
1799 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Life Time: %ld \n",lsa_life_time);
1800
1801 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name) == 0)
1802 {
1803 if ( lsa_life_time > nlsr->lsa_refresh_time )
1804 {
1805 if ( name_lsa->header->isValid == NAME_LSA_VALID )
1806 {
1807 if ( nlsr->debugging )
1808 printf("Own Name LSA need to be refrshed\n");
1809 if ( nlsr->detailed_logging )
1810 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Own Name LSA need to be refrshed\n");
1811
1812 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1813 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1814 write_log_for_name_lsa(name_lsa);
1815 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1816
1817
1818 char *current_time_stamp=(char *)malloc(20);
1819 memset(current_time_stamp,0,20);
1820 get_current_timestamp_micro(current_time_stamp);
1821
1822 free(name_lsa->header->orig_time);
1823 name_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1824 memset(name_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1825 memcpy(name_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1826
1827 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1828 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
1829 write_log_for_name_lsa(name_lsa);
1830 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1831
1832 free(current_time_stamp);
1833 }
1834 else
1835 {
1836 char lst[2];
1837 memset(lst,0,2);
1838 sprintf(lst,"%d",name_lsa->header->ls_type);
1839
1840 char lsid[10];
1841 memset(lsid,0,10);
1842 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1843
1844
1845 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1846 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1847
1848
1849 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
1850 //printf("Key:%s Length:%d\n",key,(int)strlen(key));
1851
1852 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
1853 }
1854
1855 if ( nlsr->debugging )
1856 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1857 if ( nlsr->detailed_logging )
1858 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1859
1860 set_new_lsdb_version();
1861
1862 if ( nlsr->debugging )
1863 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1864 if ( nlsr->detailed_logging )
1865 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1866
1867
1868
1869 print_name_lsdb();
1870 //lsa_change_count++;
1871 }
1872 }
1873 else
1874 {
1875 if ( lsa_life_time > nlsr->router_dead_interval )
1876 {
1877 if ( nlsr->debugging )
1878 printf("Others Name LSA need to be deleted\n");
1879 if ( nlsr->detailed_logging )
1880 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Others Name LSA need to be deleted\n");
1881
1882 char lst[2];
1883 memset(lst,0,2);
1884 sprintf(lst,"%d",name_lsa->header->ls_type);
1885
1886 char lsid[10];
1887 memset(lsid,0,10);
1888 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1889
1890
1891 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1892 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1893
1894
1895 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
1896 //printf("Key:%s Length:%d\n",key,(int)strlen(key));
1897
1898 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
1899 //lsa_change_count++;
1900 }
1901 }
1902
1903 hashtb_next(e);
1904 }
1905
1906 hashtb_end(e);
1907
1908 free(time_stamp);
1909
1910
1911}
1912
1913void
1914refresh_adj_lsdb(void)
1915{
1916 //printf("refresh_adj_lsdb called \n");
1917
1918 if ( nlsr->debugging )
1919 printf("refresh_adj_lsdb called \n");
1920 if ( nlsr->detailed_logging )
1921 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_adj_lsdb called \n");
1922
1923 char *time_stamp=(char *)malloc(20);
1924 memset(time_stamp,0,20);
1925 get_current_timestamp_micro(time_stamp);
1926
1927 long int lsa_life_time;
1928
1929 int i, adj_lsdb_element;
1930 struct alsa *adj_lsa;
1931
1932 struct hashtb_enumerator ee;
1933 struct hashtb_enumerator *e = &ee;
1934
1935 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1936 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1937
1938 for(i=0;i<adj_lsdb_element;i++)
1939 {
1940 adj_lsa=e->data;
1941
1942 lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
1943 //printf("LSA Life Time: %ld \n",lsa_life_time);
1944
1945 if ( nlsr->debugging )
1946 printf("LSA Life Time: %ld \n",lsa_life_time);
1947 if ( nlsr->detailed_logging )
1948 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Life Time: %ld \n",lsa_life_time);
1949
1950 if ( strcmp(adj_lsa->header->orig_router->name,nlsr->router_name) == 0)
1951 {
1952 if ( lsa_life_time > nlsr->lsa_refresh_time )
1953 {
1954 //printf("Own Adj LSA need to be refrshed\n");
1955 if ( nlsr->debugging )
1956 printf("Own Adj LSA need to be refrshed\n");
1957 if ( nlsr->detailed_logging )
1958 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Own Adj LSA need to be refrshed\n");
1959
1960 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1961 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
1962 write_log_for_adj_lsa(adj_lsa);
1963 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1964
1965 char *current_time_stamp=(char *)malloc(20);
1966 memset(current_time_stamp,0,20);
1967 get_current_timestamp_micro(current_time_stamp);
1968
1969 free(adj_lsa->header->orig_time);
1970 adj_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1971 memset(adj_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1972 memcpy(adj_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1973
1974 free(current_time_stamp);
1975
1976 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1977 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
1978 write_log_for_adj_lsa(adj_lsa);
1979 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1980
1981 if ( nlsr->debugging )
1982 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1983 if ( nlsr->detailed_logging )
1984 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1985
1986 set_new_lsdb_version();
1987
1988 if ( nlsr->debugging )
1989 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1990 if ( nlsr->detailed_logging )
1991 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1992
1993 print_adj_lsdb();
1994 }
1995 }
1996 else
1997 {
1998 if ( lsa_life_time > nlsr->router_dead_interval )
1999 {
2000 //printf("Others Adj LSA need to be deleted\n");
2001
2002 if ( nlsr->debugging )
2003 printf("Others Adj LSA need to be deleted\n");
2004 if ( nlsr->detailed_logging )
2005 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Others Adj LSA need to be deleted\n");
2006
2007 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
2008 memset(key,0,adj_lsa->header->orig_router->length+2);
2009 make_adj_lsa_key(key,adj_lsa);
2010 //printf("Adjacent LSA key: %s \n",key);
2011 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_adj_lsa, (void *)key, 0);
2012 }
2013 }
2014
2015
2016
2017 hashtb_next(e);
2018 }
2019
2020 hashtb_end(e);
2021
2022 free(time_stamp);
2023}
2024
2025int
2026refresh_lsdb(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
2027{
2028 if(flags == CCN_SCHEDULE_CANCEL)
2029 {
2030 return -1;
2031 }
2032
2033 nlsr_lock();
2034
2035 //printf("refresh_lsdb called \n");
2036
2037 if ( nlsr->debugging )
2038 printf("refresh_lsdb called\n");
2039 if ( nlsr->detailed_logging )
2040 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_lsdb called\n");
2041
2042 refresh_name_lsdb();
2043 refresh_adj_lsdb();
2044
2045 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
2046
2047 nlsr_unlock();
2048 return 0;
2049}
2050
2051void
akmhoque4ae16942012-12-10 11:50:43 -06002052write_adj_lsa_to_repo(char *repo_content_prefix, struct name_prefix *lsa_id)
2053{
Syed Obaid Amin83eea122013-01-08 12:21:37 -06002054
2055 printf("Content Prefix: %s\n",repo_content_prefix);
akmhoque4ae16942012-12-10 11:50:43 -06002056
2057 struct ccn_charbuf *lsa_data=ccn_charbuf_create();
2058 get_adj_lsa_data(lsa_data,lsa_id);
2059 printf("Name LSA Data: %s \n",ccn_charbuf_as_string(lsa_data));
2060
2061 write_data_to_repo(ccn_charbuf_as_string(lsa_data), repo_content_prefix);
2062
2063 ccn_charbuf_destroy(&lsa_data);
2064}
2065
2066void
akmhoque8fdd6412012-12-04 15:05:33 -06002067write_name_lsa_to_repo(char *repo_content_prefix, struct name_prefix *lsa_id)
2068{
akmhoqueb29edd82013-01-14 20:54:11 -06002069 printf("Content Prefix: %s\n",repo_content_prefix);
akmhoque8fdd6412012-12-04 15:05:33 -06002070
2071 struct ccn_charbuf *lsa_data=ccn_charbuf_create();
2072 get_name_lsa_data(lsa_data,lsa_id);
2073 printf("Name LSA Data: %s \n",ccn_charbuf_as_string(lsa_data));
2074
2075 write_data_to_repo(ccn_charbuf_as_string(lsa_data), repo_content_prefix);
2076
2077 ccn_charbuf_destroy(&lsa_data);
2078}
2079
2080
2081void
2082write_name_lsdb_to_repo(char *slice_prefix)
2083{
2084 int i, name_lsdb_element;
2085
2086 struct nlsa *name_lsa;
2087 struct hashtb_enumerator ee;
2088 struct hashtb_enumerator *e = &ee;
2089
2090 hashtb_start(nlsr->lsdb->name_lsdb, e);
2091 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
2092
2093 for(i=0;i<name_lsdb_element;i++)
2094 {
2095 name_lsa=e->data;
2096
2097 char lst[2];
2098 memset(lst,0,2);
2099 sprintf(lst,"%d",name_lsa->header->ls_type);
2100
2101 char lsid[10];
2102 memset(lsid,0,10);
2103 sprintf(lsid,"%ld",name_lsa->header->ls_id);
2104
2105
2106 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
2107 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
2108
2109
2110 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
2111 printf("Name LSA Key: %s \n",key);
2112
2113
2114 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);
2115 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);
2116 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);
2117
2118 printf("Name LSA Repo Key: %s \n",repo_key);
2119
2120 struct name_prefix *lsaid=(struct name_prefix *)malloc(sizeof(struct name_prefix));
2121 lsaid->name=(char *)malloc(strlen(key)+1);
Syed Obaid Amin4c959562012-12-21 16:43:21 -06002122 memset(lsaid->name, 0, strlen(key)+1);
akmhoque8fdd6412012-12-04 15:05:33 -06002123 memcpy(lsaid->name,key,strlen(key));
2124 lsaid->length=strlen(key)+1;
2125
2126
2127 write_name_lsa_to_repo(repo_key, lsaid);
2128
2129 free(key);
2130 free(repo_key);
2131 free(lsaid->name);
2132 free(lsaid);
2133
2134 hashtb_next(e);
2135 }
2136
2137 hashtb_end(e);
2138
2139
2140}