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