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