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