blob: e36a92291465da17103f149fd1f6a368c8d3e136 [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
akmhoqueed418f32013-01-30 12:25:04 -060089make_cor_lsa_prefix_for_repo(char *key, char *orig_router, int ls_type, char *orig_time,char *slice_prefix)
90{
91
92 sprintf(key,"%s/%d/%s%s",slice_prefix,ls_type, orig_time, orig_router);
93
94 if ( nlsr->debugging )
95 printf("Cor LSA prefix for repo content:%s\n",key);
96}
97
98void
akmhoque8fdd6412012-12-04 15:05:33 -060099build_and_install_name_lsas(void)
100{
101 if ( nlsr->debugging )
102 printf("build_and_install_name_lsas called \n");
103 if ( nlsr->detailed_logging )
104 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_name_lsas called\n");
105
106 int i, npl_element;
akmhoque8fdd6412012-12-04 15:05:33 -0600107 struct name_prefix_list_entry *npe;
108
109 struct hashtb_enumerator ee;
110 struct hashtb_enumerator *e = &ee;
111
112 hashtb_start(nlsr->npl, e);
113 npl_element=hashtb_n(nlsr->npl);
114
115 for(i=0;i<npl_element;i++)
116 {
117 npe=e->data;
118 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
119 build_name_lsa(name_lsa,npe->np);
120
121 install_name_lsa(name_lsa);
122 update_nlsa_id_for_name_in_npl(npe->np,name_lsa->header->ls_id);
123 free(name_lsa->header->orig_router->name);
124 free(name_lsa->header->orig_router);
125 free(name_lsa->header);
126 free(name_lsa->name_prefix->name);
127 free(name_lsa->name_prefix);
128 free(name_lsa);
129 hashtb_next(e);
130 }
131
132 hashtb_end(e);
133
134 print_name_prefix_from_npl();
135
136}
137
138void
139build_and_install_single_name_lsa(struct name_prefix *np)
140{
141 if ( nlsr->debugging )
142 printf("build_and_install_single_name_lsa called \n");
143 if ( nlsr->detailed_logging )
144 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_single_name_lsa called\n");
145
146 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
147 build_name_lsa(name_lsa,np);
148
149 install_name_lsa(name_lsa);
150 update_nlsa_id_for_name_in_npl(np,name_lsa->header->ls_id);
151
152 free(name_lsa->header->orig_router->name);
153 free(name_lsa->header->orig_router);
154 free(name_lsa->header);
155 free(name_lsa->name_prefix->name);
156 free(name_lsa->name_prefix);
157 free(name_lsa);
158
159 print_name_prefix_from_npl();
160
161}
162
163void
164build_name_lsa(struct nlsa *name_lsa, struct name_prefix *np)
165{
166 name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header ));
167 name_lsa->header->ls_type=LS_TYPE_NAME;
168
169 char *time_stamp=(char *)malloc(20);
170 memset(time_stamp,0,20);
171 get_current_timestamp_micro(time_stamp);
172
173 name_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1); //free
174 memset(name_lsa->header->orig_time,0,strlen(time_stamp)+1);
175 memcpy(name_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
176
177 free(time_stamp);
178
179 name_lsa->header->ls_id=++nlsr->nlsa_id;
180 name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
181 name_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1);
182 memset(name_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
183 memcpy(name_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
184 name_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
185 name_lsa->header->isValid=1;
186
187
188 name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
189 name_lsa->name_prefix->name=(char *)malloc(np->length);
190 memcpy(name_lsa->name_prefix->name,np->name,np->length);
191 name_lsa->name_prefix->length=np->length;
192
193}
194
195void
196install_name_lsa(struct nlsa *name_lsa)
197{
198
199 char *time_stamp=(char *)malloc(20);
200 memset(time_stamp,0,20);
201 get_current_timestamp_micro(time_stamp);
akmhoque8fdd6412012-12-04 15:05:33 -0600202
203
204 char lst[2];
205 memset(lst,0,2);
206 sprintf(lst,"%d",name_lsa->header->ls_type);
207
208 char lsid[10];
209 memset(lsid,0,10);
210 sprintf(lsid,"%ld",name_lsa->header->ls_id);
211
212
213 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
214 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
215
216
217 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
218 if ( nlsr->debugging )
219 printf("Key:%s Length:%d\n",key,(int)strlen(key));
220 if ( nlsr->detailed_logging )
221 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Key:%s Length:%d\n",key,(int)strlen(key));
222
223
224 struct nlsa *new_name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa )); //free
225
226 struct hashtb_enumerator ee;
227 struct hashtb_enumerator *e = &ee;
228 int res;
229
230 hashtb_start(nlsr->lsdb->name_lsdb, e);
231 res = hashtb_seek(e, key, strlen(key), 0);
232
233 if(res == HT_NEW_ENTRY )
234 {
235
236 if ( nlsr->debugging )
237 printf("New Name LSA... Adding to LSDB\n");
238 if ( nlsr->detailed_logging )
239 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Name LSA... Adding to LSDB\n");
240
241
242 new_name_lsa = e->data;
243
244 new_name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header )); //free
245 new_name_lsa->header->ls_type=name_lsa->header->ls_type;
246
247 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
248 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
249 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
250
251 new_name_lsa->header->ls_id=name_lsa->header->ls_id;
252 new_name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free
253 new_name_lsa->header->orig_router->name=(char *)malloc(name_lsa->header->orig_router->length);
254 memcpy(new_name_lsa->header->orig_router->name,name_lsa->header->orig_router->name,name_lsa->header->orig_router->length);
255 new_name_lsa->header->orig_router->length=name_lsa->header->orig_router->length;
256 new_name_lsa->header->isValid=name_lsa->header->isValid;
257
258
259 new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free
260 new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
261 memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
262 new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
263
264 if ( nlsr->debugging )
265 {
266 printf("New Name LSA Added....\n");
267 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
268 }
269 if ( nlsr->detailed_logging )
270 {
271 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Name LSA Added....\n");
272 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
273 }
274 set_new_lsdb_version();
275 if ( nlsr->debugging )
276 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
277 if ( nlsr->detailed_logging )
278 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
279
280
281 int num_next_hop=get_number_of_next_hop(new_name_lsa->header->orig_router->name);
282 if ( num_next_hop < 0 )
283 {
284 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,NO_NEXT_HOP,NULL,NULL);
285 if ( check == HT_NEW_ENTRY )
286 {
287 if ( nlsr->debugging )
288 printf("Added in npt \n");
289 if ( nlsr->detailed_logging )
290 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
291 }
292 }
293 else
294 {
295 int *faces=malloc(num_next_hop*sizeof(int));
296 int *route_costs=malloc(num_next_hop*sizeof(int));
297 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name,faces,route_costs);
298 if ( nlsr->debugging )
299 {
300 printf("Printing from install_name_lsa \n");
301 int j;
302 for(j=0;j<num_next_hop;j++)
303 printf("Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
304 }
305 if ( nlsr->detailed_logging )
306 {
307 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Printing from install_name_lsa \n");
308 int j;
309 for(j=0;j<num_next_hop;j++)
310 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
311 }
312 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,next_hop,faces,route_costs);
313 if ( check == HT_NEW_ENTRY )
314 {
315 if ( nlsr->debugging )
316 printf("Added in npt \n");
317 if ( nlsr->detailed_logging )
318 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
319 }
320 free(faces);
321 free(route_costs);
322
323 }
324
325 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
326 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
327 write_log_for_name_lsa(new_name_lsa);
328 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
329
330 free(time_stamp);
331
332 }
333 else if(res == HT_OLD_ENTRY)
334 {
335 new_name_lsa=e->data;
336 if(strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time)<0)
337 {
338 if ( nlsr->debugging )
339 printf("Older Adj LSA. Discarded... \n");
340 if ( nlsr->detailed_logging )
341 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Older Adj LSA. Discarded...\n");
342 }
343 else if( strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time) == 0 )
344 {
345 if ( nlsr->debugging )
346 printf("Duplicate Adj LSA. Discarded... \n");
347 if ( nlsr->detailed_logging )
348 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Duplicate Adj LSA. Discarded...\n");
349 }
350 else
351 {
352 if ( name_lsa->header->isValid == 0 )
353 {
354 // have to call to delete npt table entry
355 delete_npt_entry_by_router_and_name_prefix(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name);
356
357 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name)!= 0)
358 {
359 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
360 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
361 write_log_for_name_lsa(new_name_lsa);
362 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
363
364 hashtb_delete(e);
365 if ( nlsr->debugging )
366 printf("isValid bit not set for Router %s so LSA Deleted from LSDB\n",name_lsa->header->orig_router->name);
367 if ( nlsr->detailed_logging )
368 writeLogg(__FILE__,__FUNCTION__,__LINE__,"isValid bit not set for Router %s so LSA Deleted from LSDB\n",name_lsa->header->orig_router->name);
369 }
370 else
371 {
372 new_name_lsa->header->isValid=name_lsa->header->isValid;
373 free(new_name_lsa->header->orig_time);
374 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
375 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
376 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
377 }
378 if ( nlsr->debugging )
379 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
380 if ( nlsr->detailed_logging )
381 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
382 set_new_lsdb_version();
383 if ( nlsr->debugging )
384 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
385 if ( nlsr->detailed_logging )
386 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
387 }
388 else
389 {
390 int is_npt_update=0;
391 if ( strcmp(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name) != 0 )
392 {
393 is_npt_update=1;
394 delete_npt_entry_by_router_and_name_prefix(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name);
395 }
396
397 // copying LSA content with header
398 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
399 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
400 write_log_for_name_lsa(new_name_lsa);
401 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
402
403
404 free(new_name_lsa->header->orig_time);
405 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
406 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
407 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
408
409 new_name_lsa->header->isValid=name_lsa->header->isValid;
410
411 free(new_name_lsa->name_prefix->name);
412 free(new_name_lsa->name_prefix);
413 new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
414 new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
415 memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
416 new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
417
418
419 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
420 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
421 write_log_for_name_lsa(new_name_lsa);
422 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
423
424 if ( nlsr->debugging )
425 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
426 if ( nlsr->detailed_logging )
427 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
428
429 set_new_lsdb_version();
430
431 if ( nlsr->debugging )
432 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
433 if ( nlsr->detailed_logging )
434 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
435
436
437 if( is_npt_update == 1 )
438 {
439 //struct hashtb *face_list;
440 int num_next_hop=get_number_of_next_hop(new_name_lsa->header->orig_router->name);
441 if ( num_next_hop < 0 )
442 {
443
444 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,NO_NEXT_HOP,NULL,NULL);
445 if ( check == HT_NEW_ENTRY )
446 {
447 if ( nlsr->debugging )
448 printf("Added in npt \n");
449 if ( nlsr->detailed_logging )
450 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
451 }
452 }
453 else
454 {
455 int *faces=malloc(num_next_hop*sizeof(int));
456 int *route_costs=malloc(num_next_hop*sizeof(int));
457 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name,faces,route_costs);
458
459 if ( nlsr->debugging )
460 {
461 printf("Printing from install_name_lsa \n");
462 int j;
463 for(j=0;j<num_next_hop;j++)
464 printf("Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
465 }
466 if ( nlsr->detailed_logging )
467 {
468 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Printing from install_name_lsa \n");
469 int j;
470 for(j=0;j<num_next_hop;j++)
471 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
472 }
473
474
475 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,next_hop,faces,route_costs);
476 if ( check == HT_NEW_ENTRY )
477 {
akmhoque8fdd6412012-12-04 15:05:33 -0600478 if ( nlsr->debugging )
479 printf("Added in npt \n");
480 if ( nlsr->detailed_logging )
481 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
482 }
483 free(faces);
484 free(route_costs);
485
486 }
487
488 }
489 }
490 }
491
492 }
493
494 hashtb_end(e);
495
496 free(key);
497}
498
499void
500write_log_for_name_lsa(struct nlsa *name_lsa)
501{
502
503 writeLogg(__FILE__,__FUNCTION__,__LINE__,"-----------Name LSA Content---------------\n");
504 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s\n",name_lsa->header->orig_router->name);
505 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router Length: %d\n",name_lsa->header->orig_router->length);
506 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Type: %d\n",name_lsa->header->ls_type);
507 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Id: %ld\n",name_lsa->header->ls_id);
508 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Time: %s\n",name_lsa->header->orig_time);
509 writeLogg(__FILE__,__FUNCTION__,__LINE__," Is Valid: %d\n",name_lsa->header->isValid);
510 writeLogg(__FILE__,__FUNCTION__,__LINE__," LSA Data \n");
511 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix: %s\n",name_lsa->name_prefix->name);
512 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix Length: %d\n",name_lsa->name_prefix->length);
513 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
514}
515
516void
517print_name_lsa(struct nlsa *name_lsa)
518{
519
520 if ( nlsr->debugging )
521 {
522 printf("-----------Name LSA Content---------------\n");
523 printf(" Origination Router : %s\n",name_lsa->header->orig_router->name);
524 printf(" Origination Router Length: %d\n",name_lsa->header->orig_router->length);
525 printf(" LS Type : %d\n",name_lsa->header->ls_type);
526 printf(" LS Id : %ld\n",name_lsa->header->ls_id);
527 printf(" Origination Time : %s\n",name_lsa->header->orig_time);
528 printf(" Is Valid : %d\n",name_lsa->header->isValid);
529 printf(" LSA Data \n");
530 printf(" Name Prefix: : %s\n",name_lsa->name_prefix->name);
531 printf(" Name Prefix Length : %d\n",name_lsa->name_prefix->length);
532
533 printf("\n");
534 }
535}
536
537void
538print_name_lsdb(void)
539{
540 if ( nlsr->debugging )
541 printf("print_name_lsdb called \n");
542 int i, name_lsdb_element;
543 struct nlsa *name_lsa;
544
545 struct hashtb_enumerator ee;
546 struct hashtb_enumerator *e = &ee;
547
548 hashtb_start(nlsr->lsdb->name_lsdb, e);
549 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
550
551 for(i=0;i<name_lsdb_element;i++)
552 {
553 if ( nlsr->debugging )
554 printf("-----------Name LSA (%d)---------------\n",i+1);
555 name_lsa=e->data;
556 print_name_lsa(name_lsa);
557 hashtb_next(e);
558 }
559
560 hashtb_end(e);
561
562 if ( nlsr->debugging )
563 printf("\n");
564 if ( nlsr->detailed_logging )
565 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
566}
567
568
569void
570build_and_install_others_name_lsa(char *orig_router,int ls_type,long int ls_id,char *orig_time, int isValid,char *np)
571{
572 if ( nlsr->debugging )
573 printf("build_and_install_others_name_lsa called \n");
574 if ( nlsr->detailed_logging )
575 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_others_name_lsa called \n");
576
577 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
578 build_others_name_lsa(name_lsa,orig_router,ls_type,ls_id,orig_time, isValid,np);
579 print_name_lsa(name_lsa);
580 install_name_lsa(name_lsa);
581 print_name_lsdb();
582 print_npt();
583
584 free(name_lsa->header->orig_router->name);
585 free(name_lsa->header->orig_router);
586 free(name_lsa->header);
587 free(name_lsa->name_prefix->name);
588 free(name_lsa->name_prefix);
589 free(name_lsa);
590
591}
592
593void
594build_others_name_lsa(struct nlsa *name_lsa, char *orig_router,int ls_type,long int ls_id,char *orig_time, int isValid,char *np)
595{
596 if ( nlsr->debugging )
597 printf("build_others_name_lsa called\n");
598 if ( nlsr->detailed_logging )
599 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_others_name_lsa called \n");
600
601 name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header ));
602 name_lsa->header->ls_type=LS_TYPE_NAME;
603
604 name_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
605 memset(name_lsa->header->orig_time,0,strlen(orig_time)+1);
606 memcpy(name_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
607
608 name_lsa->header->ls_id=ls_id;
609 name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
610 name_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
611 memset(name_lsa->header->orig_router->name,0,strlen(orig_router)+1);
612 memcpy(name_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
613 name_lsa->header->orig_router->length=strlen(orig_router)+1;
614 name_lsa->header->isValid=isValid;
615
616 name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
617 name_lsa->name_prefix->name=(char *)malloc(strlen(np)+1);
618 memset(name_lsa->name_prefix->name,0,strlen(np)+1);
619 memcpy(name_lsa->name_prefix->name,np,strlen(np)+1);
620 name_lsa->name_prefix->length=strlen(np)+1;
621}
622
623
akmhoqueed418f32013-01-30 12:25:04 -0600624void
625make_cor_lsa_key(char *key,struct clsa *cor_lsa)
626{
627 memcpy(key+strlen(key),cor_lsa->header->orig_router->name,cor_lsa->header->orig_router->length);
628 memcpy(key+strlen(key),"/",1);
629 char ls_type[2];
630 sprintf(ls_type,"%d",cor_lsa->header->ls_type);
631 memcpy(key+strlen(key),ls_type,strlen(ls_type));
632 key[strlen(key)]='\0';
633}
akmhoque8fdd6412012-12-04 15:05:33 -0600634
635
636void
637make_adj_lsa_key(char *key,struct alsa *adj_lsa)
638{
639 memcpy(key+strlen(key),adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
640 memcpy(key+strlen(key),"/",1);
641 char ls_type[2];
642 sprintf(ls_type,"%d",adj_lsa->header->ls_type);
643 memcpy(key+strlen(key),ls_type,strlen(ls_type));
644 key[strlen(key)]='\0';
645}
646
647int
648build_and_install_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
649{
650 if(flags == CCN_SCHEDULE_CANCEL)
651 {
652 return -1;
653 }
654
655 nlsr_lock();
656
657 if ( nlsr->debugging )
658 printf("build_and_install_adj_lsa called \n");
659 if ( nlsr->detailed_logging )
660 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_adj_lsa called \n");
661
662 if ( nlsr->debugging )
663 printf("adj_build_flag = %d \n",nlsr->adj_build_flag);
664 if ( nlsr->detailed_logging )
665 writeLogg(__FILE__,__FUNCTION__,__LINE__,"adj_build_flag = %d \n",nlsr->adj_build_flag);
666
667 if(nlsr->adj_build_flag > 0)
668 {
669 if ( nlsr->debugging )
670 printf("is_adj_lsa_build = %d \n",is_adj_lsa_build());
671 if ( nlsr->detailed_logging )
672 writeLogg(__FILE__,__FUNCTION__,__LINE__,"is_adj_lsa_build = %d \n",is_adj_lsa_build());
673
674 if ( is_adj_lsa_build()> 0)
675 {
676 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
677 build_adj_lsa(adj_lsa);
678 install_adj_lsa(adj_lsa);
679
akmhoque4ae16942012-12-10 11:50:43 -0600680 char lst[2];
Syed Obaid Amin4c959562012-12-21 16:43:21 -0600681 memset(lst,0,2);
akmhoque4ae16942012-12-10 11:50:43 -0600682 sprintf(lst,"%d",LS_TYPE_ADJ);
683
akmhoquedecdf5b2013-01-17 03:34:29 -0600684 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);
685 memset(repo_key, 0, strlen(nlsr->slice_prefix)+strlen(adj_lsa->header->orig_time)+strlen(adj_lsa->header->orig_router->name) + strlen(lst) + 5);
686 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);
687
688 if ( nlsr->debugging )
689 printf("Adj LSA Repo Key: %s \n",repo_key);
690
691 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
692 memset(key,0,adj_lsa->header->orig_router->length+2+2);
693 make_adj_lsa_key(key,adj_lsa);
694 if ( nlsr->debugging )
695 printf("Adj LSA: %s \n",key);
696
697 struct name_prefix *lsaid=(struct name_prefix *)malloc(sizeof(struct name_prefix));
698 lsaid->name=(char *)malloc(strlen(key)+1);
699 memset(lsaid->name, 0, strlen(key)+1);
700 memcpy(lsaid->name,key,strlen(key));
701 lsaid->length=strlen(key)+1;
702
703
akmhoque559efac2013-01-17 03:43:13 -0600704 write_adj_lsa_to_repo(repo_key, lsaid);
akmhoquedecdf5b2013-01-17 03:34:29 -0600705
706 free(key);
707 free(repo_key);
708 free(lsaid->name);
709 free(lsaid);
710
711
akmhoqueea2fb632013-01-16 17:18:01 -0600712
akmhoque8fdd6412012-12-04 15:05:33 -0600713 free(adj_lsa->header->orig_router->name);
714 free(adj_lsa->header->orig_router);
715 free(adj_lsa->header->orig_time);
716 free(adj_lsa->header);
717 free(adj_lsa->body);
718 free(adj_lsa);
719 nlsr->adj_build_flag=0;
720 print_adj_lsdb();
721 }
722 else
723 {
724 if ( nlsr->debugging )
725 printf("Can not build adj LSA now\n");
726 if ( nlsr->detailed_logging )
727 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Can not build adj LSA now\n");
728 }
729 }
730 nlsr->is_build_adj_lsa_sheduled=0;
731
732 nlsr_unlock();
733
734 return 0;
735}
736
737
738void
739build_adj_lsa(struct alsa * adj_lsa)
740{
741 if ( nlsr->debugging )
742 printf("build_adj_lsa called \n");
743 if ( nlsr->detailed_logging )
744 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_adj_lsa called \n");
745
746 int no_link=no_active_nbr();
747
akmhoque8fdd6412012-12-04 15:05:33 -0600748 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
749 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
750 adj_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1);
751 memset(adj_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
752 memcpy(adj_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
753 adj_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
754
755 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
756
757 char *time_stamp=(char *)malloc(20);
758 memset(time_stamp,0,20);
759 get_current_timestamp_micro(time_stamp);
760
761 adj_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1);
762 memset(adj_lsa->header->orig_time,0,strlen(time_stamp)+1);
763 memcpy(adj_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
764 free(time_stamp);
765
akmhoque8fdd6412012-12-04 15:05:33 -0600766 adj_lsa->no_link=no_link;
767
768
769 struct ccn_charbuf *c=ccn_charbuf_create();
770 get_active_nbr_adj_data(c);
771 char *data=ccn_charbuf_as_string(c);
772
773 adj_lsa->body=(char *)malloc(strlen(data)+1);
774 memset(adj_lsa->body,0,strlen(data)+1);
775 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
776 ccn_charbuf_destroy(&c);
777
akmhoque8fdd6412012-12-04 15:05:33 -0600778 nlsr->adj_build_count++;
779
780
781}
782
783
784void
785install_adj_lsa(struct alsa * adj_lsa)
786{
787
788 if ( nlsr->debugging )
789 printf("install_adj_lsa called \n");
790 if ( nlsr->detailed_logging )
791 writeLogg(__FILE__,__FUNCTION__,__LINE__,"install_adj_lsa called \n");
792
793
794 char *time_stamp=(char *)malloc(20);
795 memset(time_stamp,0,20);
796 get_current_timestamp_micro(time_stamp);
akmhoque8fdd6412012-12-04 15:05:33 -0600797
798
799 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
800 memset(key,0,adj_lsa->header->orig_router->length+2);
801 make_adj_lsa_key(key,adj_lsa);
akmhoque8fdd6412012-12-04 15:05:33 -0600802
803 struct alsa *new_adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
804
805 struct hashtb_enumerator ee;
806 struct hashtb_enumerator *e = &ee;
807 int res;
808
809 hashtb_start(nlsr->lsdb->adj_lsdb, e);
810 res = hashtb_seek(e, key, strlen(key), 0);
811
812
813
814 if(res == HT_NEW_ENTRY)
815 {
816 if ( adj_lsa->no_link > 0)
817 {
818 if ( nlsr->debugging )
819 printf("New ADJ LSA... Adding to LSDB\n");
820 if ( nlsr->detailed_logging )
821 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New ADJ LSA... Adding to LSDB\n");
822 new_adj_lsa = e->data;
823
824 new_adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
825 new_adj_lsa->header->ls_type=adj_lsa->header->ls_type;
826 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
827 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
828
829 new_adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
830 new_adj_lsa->header->orig_router->name=(char *)malloc(adj_lsa->header->orig_router->length);
831 memcpy(new_adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
832 new_adj_lsa->header->orig_router->length=adj_lsa->header->orig_router->length;
833
834 new_adj_lsa->no_link=adj_lsa->no_link;
835
836 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
837 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
838 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
839
840 add_next_hop_router(new_adj_lsa->header->orig_router->name);
841 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
842
843 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
844 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
845 write_log_for_adj_lsa(new_adj_lsa);
846 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
847 }
848 else
849 {
850 hashtb_delete(e);
851 }
852
853 if ( nlsr->debugging )
854 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
855 if ( nlsr->detailed_logging )
856 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
857
858 set_new_lsdb_version();
859
860 if ( nlsr->debugging )
861 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
862 if ( nlsr->detailed_logging )
863 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
864
865 }
866 else if(res == HT_OLD_ENTRY)
867 {
868 new_adj_lsa = e->data;
869 if(strcmp(adj_lsa->header->orig_time,new_adj_lsa->header->orig_time)<=0)
870 {
871 if ( nlsr->debugging )
872 printf("Older/Duplicate Adj LSA. Discarded...\n");
873 if ( nlsr->detailed_logging )
874 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Older/Duplicate Adj LSA. Discarded...\n");
875 }
876 else
877 {
878
879 if ( adj_lsa->no_link > 0)
880 {
akmhoque8fdd6412012-12-04 15:05:33 -0600881 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
882 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
883 write_log_for_adj_lsa(new_adj_lsa);
884 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
885
886 free(new_adj_lsa->header->orig_time);
887 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
888 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
889
890 new_adj_lsa->no_link=adj_lsa->no_link;
891
892 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
893 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
894 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
895
896 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
897
898 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
899 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
900 write_log_for_adj_lsa(new_adj_lsa);
901 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
902 }
903 else
904 {
905 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
906 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
907 write_log_for_adj_lsa(new_adj_lsa);
908 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
909
910 hashtb_delete(e);
911 }
912
913 if ( nlsr->debugging )
914 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
915 if ( nlsr->detailed_logging )
916 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
917
918 set_new_lsdb_version();
919
920 if ( nlsr->debugging )
921 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
922 if ( nlsr->detailed_logging )
923 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
924 }
925
926 }
927 hashtb_end(e);
928
929 if ( !nlsr->is_route_calculation_scheduled )
930 {
931 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
932 nlsr->is_route_calculation_scheduled=1;
933 }
934
935
936 free(key);
937
938 free(time_stamp);
939}
940
941void
942write_log_for_adj_lsa_body(const char *body, int no_link)
943{
944 int i=0;
945 char *lsa_data=(char *)malloc(strlen(body)+1);
946 memset( lsa_data,0,strlen(body)+1);
947 memcpy(lsa_data,body,strlen(body)+1);
948 char *sep="|";
949 char *rem;
950 char *rtr_id;
951 char *length;
952 char *face;
953 char *metric;
954
955 if(no_link >0 )
956 {
957 rtr_id=strtok_r(lsa_data,sep,&rem);
958 length=strtok_r(NULL,sep,&rem);
959 face=strtok_r(NULL,sep,&rem);
960 metric=strtok_r(NULL,sep,&rem);
961
962 writeLogg(__FILE__,__FUNCTION__,__LINE__," Link %d \n",i+1);
963 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router: %s \n",rtr_id);
964 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router Length: %s \n",length);
965 writeLogg(__FILE__,__FUNCTION__,__LINE__," Connecting Face: %s \n",face);
966 writeLogg(__FILE__,__FUNCTION__,__LINE__," Metric: %s \n",metric);
967
968
969 for(i=1;i<no_link;i++)
970 {
971 rtr_id=strtok_r(NULL,sep,&rem);
972 length=strtok_r(NULL,sep,&rem);
973 face=strtok_r(NULL,sep,&rem);
974 metric=strtok_r(NULL,sep,&rem);
975 writeLogg(__FILE__,__FUNCTION__,__LINE__," Link %d \n",i+1);
976 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router: %s \n",rtr_id);
977 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router Length: %s \n",length);
978 writeLogg(__FILE__,__FUNCTION__,__LINE__," Connecting Face: %s \n",face);
979 writeLogg(__FILE__,__FUNCTION__,__LINE__," Metric: %s \n",metric);
980
981 }
982 }
983
984 free(lsa_data);
985}
986
987
988void
989write_log_for_adj_lsa(struct alsa * adj_lsa)
990{
991 writeLogg(__FILE__,__FUNCTION__,__LINE__,"-----------Adj LSA Content---------------\n");
992 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s\n",adj_lsa->header->orig_router->name);
993 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
994 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Type: %d\n",adj_lsa->header->ls_type);
995 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Time: %s\n",adj_lsa->header->orig_time);
996 writeLogg(__FILE__,__FUNCTION__,__LINE__," Lsa Data:\n");
997 writeLogg(__FILE__,__FUNCTION__,__LINE__," No of Link: %d\n",adj_lsa->no_link);
998
999 write_log_for_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
1000
1001 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
1002
1003}
1004
1005void
1006print_adj_lsa_body(const char *body, int no_link)
1007{
1008 int i=0;
1009 char *lsa_data=(char *)malloc(strlen(body)+1);
1010 memset( lsa_data,0,strlen(body)+1);
1011 memcpy(lsa_data,body,strlen(body)+1);
1012 char *sep="|";
1013 char *rem;
1014 char *rtr_id;
1015 char *length;
1016 char *face;
1017 char *metric;
1018
1019 if(no_link >0 )
1020 {
1021 rtr_id=strtok_r(lsa_data,sep,&rem);
1022 length=strtok_r(NULL,sep,&rem);
1023 face=strtok_r(NULL,sep,&rem);
1024 metric=strtok_r(NULL,sep,&rem);
1025
a64adam04be6482013-01-17 11:29:32 -06001026 if ( nlsr->debugging ) {
1027 printf(" Link %d \n",i+1);
1028 printf(" Neighbor : %s \n",rtr_id);
1029 printf(" Neighbor Length : %s \n",length);
1030 printf(" Connecting Face : %s \n",face);
1031 printf(" Metric : %s \n",metric);
1032 }
akmhoque8fdd6412012-12-04 15:05:33 -06001033
1034 for(i=1;i<no_link;i++)
1035 {
1036 rtr_id=strtok_r(NULL,sep,&rem);
1037 length=strtok_r(NULL,sep,&rem);
1038 face=strtok_r(NULL,sep,&rem);
1039 metric=strtok_r(NULL,sep,&rem);
1040 printf(" Link %d \n",i+1);
1041 printf(" Neighbor : %s \n",rtr_id);
1042 printf(" Neighbor Length : %s \n",length);
1043 printf(" Connecting Face : %s \n",face);
1044 printf(" Metric : %s \n",metric);
1045
1046 }
1047 }
1048
1049 free(lsa_data);
1050}
1051
1052void
1053print_adj_lsa(struct alsa * adj_lsa)
1054{
1055 if ( nlsr->debugging )
1056 {
1057 printf("-----------ADJ LSA Content---------------\n");
1058 printf(" Origination Router : %s\n",adj_lsa->header->orig_router->name);
1059 printf(" Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
1060 printf(" LS Type : %d\n",adj_lsa->header->ls_type);
1061 printf(" Origination Time : %s\n",adj_lsa->header->orig_time);
1062 printf(" Lsa Data:\n");
1063 printf(" No of Link : %d\n",adj_lsa->no_link);
1064
1065 print_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
1066 printf("\n");
1067 }
1068
1069}
1070
1071void
1072print_adj_lsdb(void)
1073{
1074 if ( nlsr->debugging )
1075 printf("print_name_lsdb called \n");
1076 int i, adj_lsdb_element;
1077 struct alsa *adj_lsa;
1078
1079 struct hashtb_enumerator ee;
1080 struct hashtb_enumerator *e = &ee;
1081
1082 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1083 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1084
1085 for(i=0;i<adj_lsdb_element;i++)
1086 {
1087 if ( nlsr->debugging )
1088 printf("-----------Adj LSA (%d)---------------\n",i+1);
1089 adj_lsa=e->data;
1090 print_adj_lsa(adj_lsa);
1091 hashtb_next(e);
1092 }
1093
1094 hashtb_end(e);
1095
1096 if ( nlsr->debugging )
1097 printf("\n");
1098 if ( nlsr->detailed_logging )
1099 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
1100}
1101
1102void
1103build_and_install_others_adj_lsa(char *orig_router,int ls_type,char *orig_time, int no_link,char *data)
1104{
1105 if ( nlsr->debugging )
1106 printf("build_and_install_others_adj_lsa called \n");
1107 if ( nlsr->detailed_logging )
1108 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_others_adj_lsa called \n");
1109 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
1110 build_others_adj_lsa(adj_lsa,orig_router,ls_type,orig_time,no_link,data);
akmhoque8fdd6412012-12-04 15:05:33 -06001111 install_adj_lsa(adj_lsa);
1112
1113
1114 free(adj_lsa->header->orig_router->name);
1115 free(adj_lsa->header->orig_router);
1116 free(adj_lsa->header->orig_time);
1117 free(adj_lsa->header);
1118 free(adj_lsa->body);
1119 free(adj_lsa);
1120
1121 print_adj_lsdb();
1122
1123}
1124
1125
1126void
1127build_others_adj_lsa(struct alsa *adj_lsa,char *orig_router,int ls_type,char *orig_time,int no_link,char *data)
1128{
akmhoque8fdd6412012-12-04 15:05:33 -06001129 if ( nlsr->debugging )
1130 printf("build_others_adj_lsa called \n");
1131 if ( nlsr->detailed_logging )
1132 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_others_adj_lsa called \n");
1133
1134 /*Filling Up Header Data */
1135 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
1136 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
1137 adj_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
1138 memset(adj_lsa->header->orig_router->name,0,strlen(orig_router)+1);
1139 memcpy(adj_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
1140
1141 adj_lsa->header->orig_router->length=strlen(orig_router)+1;
1142
1143
1144 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
1145
1146 adj_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
1147 memset(adj_lsa->header->orig_time,0,strlen(orig_time)+1);
1148 memcpy(adj_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
1149
1150 adj_lsa->no_link=no_link;
1151
1152 adj_lsa->body=(char *)malloc(strlen(data)+1);
1153 memset(adj_lsa->body,0,strlen(data)+1);
1154 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
1155
1156}
1157
1158
1159long int
1160get_name_lsdb_num_element(void)
1161{
1162 long int num_element;
1163
1164
1165 struct hashtb_enumerator ee;
1166 struct hashtb_enumerator *e = &ee;
1167
1168 hashtb_start(nlsr->lsdb->name_lsdb, e);
1169 num_element=hashtb_n(nlsr->lsdb->name_lsdb);
1170 hashtb_end(e);
1171
1172 return num_element;
1173}
1174
1175long int
1176get_adj_lsdb_num_element(void)
1177{
1178 long int num_element;
1179
1180
1181 struct hashtb_enumerator ee;
1182 struct hashtb_enumerator *e = &ee;
1183
1184 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1185 num_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1186 hashtb_end(e);
1187
1188 return num_element;
1189}
1190
akmhoque8fdd6412012-12-04 15:05:33 -06001191
1192int
1193check_is_new_name_lsa(char *orig_router,char *lst,char *lsid,char *orig_time)
1194{
1195 int ret=0;
1196 struct ccn_charbuf *key=ccn_charbuf_create();
1197 ccn_charbuf_append_string(key,orig_router);
1198 ccn_charbuf_append_string(key,"/");
1199 ccn_charbuf_append_string(key,lst);
1200 ccn_charbuf_append_string(key,"/");
1201 ccn_charbuf_append_string(key,lsid);
1202
1203 int res;
1204 struct nlsa *name_lsa;
1205
1206 struct hashtb_enumerator ee;
1207 struct hashtb_enumerator *e = &ee;
1208
1209 hashtb_start(nlsr->lsdb->name_lsdb, e);
1210 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1211
1212 if( res == HT_NEW_ENTRY )
1213 {
1214 hashtb_delete(e);
1215 ret=1;
1216
1217 }
1218 else if(res == HT_OLD_ENTRY)
1219 {
1220 name_lsa=e->data;
1221 if( strcmp ( orig_time , name_lsa->header->orig_time ) > 0 )
1222 {
1223 ret=1;
1224 }
1225 }
1226
1227 hashtb_end(e);
1228
1229 ccn_charbuf_destroy(&key);
1230
1231 return ret;
1232}
1233
1234int
1235check_is_new_adj_lsa(char *orig_router,char *lst,char *orig_time)
1236{
1237 int ret=0;
1238 struct ccn_charbuf *key=ccn_charbuf_create();
1239 ccn_charbuf_append_string(key,orig_router);
1240 ccn_charbuf_append_string(key,"/");
1241 ccn_charbuf_append_string(key,lst);
1242
1243 int res;
1244 struct alsa *adj_lsa;
1245
1246 struct hashtb_enumerator ee;
1247 struct hashtb_enumerator *e = &ee;
1248
1249 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1250 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1251
1252 if( res == HT_NEW_ENTRY )
1253 {
1254 hashtb_delete(e);
1255 ret=1;
1256
1257 }
1258 else if(res == HT_OLD_ENTRY)
1259 {
1260 adj_lsa=e->data;
1261 if( strcmp ( orig_time , adj_lsa->header->orig_time ) > 0 )
1262 {
1263 ret=1;
1264 }
1265 }
1266
1267 hashtb_end(e);
1268
1269 ccn_charbuf_destroy(&key);
1270
1271 return ret;
1272}
1273
akmhoqueed418f32013-01-30 12:25:04 -06001274int
1275check_is_new_cor_lsa(char *orig_router,char *lst,char *orig_time)
1276{
1277 int ret=0;
1278 struct ccn_charbuf *key=ccn_charbuf_create();
1279 ccn_charbuf_append_string(key,orig_router);
1280 ccn_charbuf_append_string(key,"/");
1281 ccn_charbuf_append_string(key,lst);
1282
1283 int res;
1284 struct clsa *cor_lsa;
1285
1286 struct hashtb_enumerator ee;
1287 struct hashtb_enumerator *e = &ee;
1288
1289 hashtb_start(nlsr->lsdb->cor_lsdb, e);
1290 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1291
1292 if( res == HT_NEW_ENTRY )
1293 {
1294 hashtb_delete(e);
1295 ret=1;
1296
1297 }
1298 else if(res == HT_OLD_ENTRY)
1299 {
1300 cor_lsa=e->data;
1301 if( strcmp ( orig_time , cor_lsa->header->orig_time ) > 0 )
1302 {
1303 ret=1;
1304 }
1305 }
1306
1307 hashtb_end(e);
1308
1309 ccn_charbuf_destroy(&key);
1310
1311 return ret;
1312}
1313
akmhoque8fdd6412012-12-04 15:05:33 -06001314void
1315get_name_lsa_data(struct ccn_charbuf *lsa_data, struct name_prefix *lsaId)
1316{
akmhoque8fdd6412012-12-04 15:05:33 -06001317 if ( nlsr->debugging )
1318 printf("get_name_lsa_data called \n");
1319 if ( nlsr->detailed_logging )
1320 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_name_lsa_data called \n");
1321
1322 struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa ));
1323
1324 struct hashtb_enumerator ee;
1325 struct hashtb_enumerator *e = &ee;
1326 int res;
1327
1328 hashtb_start(nlsr->lsdb->name_lsdb, e);
1329 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1330
1331 if( res == HT_OLD_ENTRY )
1332 {
1333 name_lsa=e->data;
akmhoquefc5176d2013-01-18 09:50:12 -06001334
akmhoque8fdd6412012-12-04 15:05:33 -06001335 if ( nlsr->debugging )
1336 printf("NAME LSA found \n");
1337 if ( nlsr->detailed_logging )
1338 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Name LSA found \n");
1339
1340 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_router->name);
1341 ccn_charbuf_append_string(lsa_data,"|");
1342
1343 char *temp_length=(char *)malloc(20);
1344 memset(temp_length,0,20);
1345 sprintf(temp_length,"%d",name_lsa->header->orig_router->length);
1346 ccn_charbuf_append_string(lsa_data,temp_length);
1347 free(temp_length);
1348 ccn_charbuf_append_string(lsa_data,"|");
1349
1350 char *temp_ltype=(char *)malloc(20);
1351 memset(temp_ltype,0,20);
1352 sprintf(temp_ltype,"%d",name_lsa->header->ls_type);
1353 ccn_charbuf_append_string(lsa_data,temp_ltype);
1354 free(temp_ltype);
1355 ccn_charbuf_append_string(lsa_data,"|");
1356
1357 char *temp_lsid=(char *)malloc(20);
1358 memset(temp_lsid,0,20);
1359 sprintf(temp_lsid,"%ld",name_lsa->header->ls_id);
1360 ccn_charbuf_append_string(lsa_data,temp_lsid);
1361 free(temp_lsid);
1362 ccn_charbuf_append_string(lsa_data,"|");
1363
1364 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_time);
1365 ccn_charbuf_append_string(lsa_data,"|");
1366
1367 char *temp_valid=(char *)malloc(20);
1368 memset(temp_valid,0,20);
1369 sprintf(temp_valid,"%d",name_lsa->header->isValid);
1370 ccn_charbuf_append_string(lsa_data,temp_valid);
1371 free(temp_valid);
1372 ccn_charbuf_append_string(lsa_data,"|");
1373
1374 ccn_charbuf_append_string(lsa_data,name_lsa->name_prefix->name);
1375 ccn_charbuf_append_string(lsa_data,"|");
1376
1377 char *temp_npl=(char *)malloc(20);
1378 memset(temp_npl,0,20);
1379 sprintf(temp_npl,"%d",name_lsa->name_prefix->length);
1380 ccn_charbuf_append_string(lsa_data,temp_npl);
1381 free(temp_npl);
1382 ccn_charbuf_append_string(lsa_data,"|");
1383
1384 }
1385 else if(res == HT_NEW_ENTRY)
1386 {
1387 hashtb_delete(e);
1388 }
1389
1390 hashtb_end(e);
1391}
1392
1393void
1394get_adj_lsa_data(struct ccn_charbuf *lsa_data,struct name_prefix *lsaId)
1395{
1396 if ( nlsr->debugging )
1397 printf("get_adj_lsa_data called \n");
1398 if ( nlsr->detailed_logging )
1399 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_adj_lsa_data called \n");
1400
1401 struct alsa *adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
1402
1403 struct hashtb_enumerator ee;
1404 struct hashtb_enumerator *e = &ee;
1405 int res;
1406
1407 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1408 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1409
1410 if( res == HT_OLD_ENTRY )
1411 {
1412 adj_lsa=e->data;
akmhoque8fdd6412012-12-04 15:05:33 -06001413
1414 if ( nlsr->debugging )
1415 printf("Adj LSA found \n");
1416 if ( nlsr->detailed_logging )
1417 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adj LSA found \n");
1418
1419 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_router->name);
1420 ccn_charbuf_append_string(lsa_data,"|");
1421
1422 char *temp_length=(char *)malloc(20);
1423 memset(temp_length,0,20);
1424 sprintf(temp_length,"%d",adj_lsa->header->orig_router->length);
1425 ccn_charbuf_append_string(lsa_data,temp_length);
akmhoque8fdd6412012-12-04 15:05:33 -06001426 ccn_charbuf_append_string(lsa_data,"|");
akmhoquefc5176d2013-01-18 09:50:12 -06001427 free(temp_length);
akmhoque8fdd6412012-12-04 15:05:33 -06001428
1429 char *temp_ltype=(char *)malloc(20);
1430 memset(temp_ltype,0,20);
1431 sprintf(temp_ltype,"%d",adj_lsa->header->ls_type);
1432 ccn_charbuf_append_string(lsa_data,temp_ltype);
akmhoque8fdd6412012-12-04 15:05:33 -06001433 ccn_charbuf_append_string(lsa_data,"|");
akmhoquefc5176d2013-01-18 09:50:12 -06001434 free(temp_ltype);
akmhoque8fdd6412012-12-04 15:05:33 -06001435
1436 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_time);
1437 ccn_charbuf_append_string(lsa_data,"|");
1438
1439 char *temp_nl=(char *)malloc(20);
1440 memset(temp_nl,0,20);
1441 sprintf(temp_nl,"%d",adj_lsa->no_link);
1442 ccn_charbuf_append_string(lsa_data,temp_nl);
akmhoque8fdd6412012-12-04 15:05:33 -06001443 ccn_charbuf_append_string(lsa_data,"|");
akmhoquefc5176d2013-01-18 09:50:12 -06001444 free(temp_nl);
akmhoque8fdd6412012-12-04 15:05:33 -06001445
1446 ccn_charbuf_append_string(lsa_data,adj_lsa->body);
1447
1448
1449 }
1450 else if(res == HT_NEW_ENTRY)
1451 {
1452 hashtb_delete(e);
1453 }
1454
1455 hashtb_end(e);
1456}
1457
1458void
1459make_name_lsa_invalid(struct name_prefix *np,int ls_type, long int ls_id)
1460{
1461
1462 if ( nlsr->debugging )
1463 printf("make_name_lsa_invalid called \n");
1464 if ( nlsr->detailed_logging )
1465 writeLogg(__FILE__,__FUNCTION__,__LINE__,"make_name_lsa_invalid called \n");
1466
1467
1468 char lst[2];
1469 memset(lst,0,2);
1470 sprintf(lst,"%d",ls_type);
1471
1472 char lsid[10];
1473 memset(lsid,0,10);
1474 sprintf(lsid,"%ld",ls_id);
1475
1476
1477 char *key=(char *)malloc(strlen(np->name)+1+strlen(lst)+1+strlen(lsid)+1);
1478 memset(key,0,strlen(np->name)+1+strlen(lst)+1+strlen(lsid)+1);
1479
1480
1481 make_name_lsa_key(key, np->name,ls_type,ls_id);
a64adam04be6482013-01-17 11:29:32 -06001482
1483 if ( nlsr->debugging )
1484 printf("Key:%s Length:%d\n",key,(int)strlen(key));
akmhoque8fdd6412012-12-04 15:05:33 -06001485
1486 struct nlsa *nlsa;
1487
1488 struct hashtb_enumerator ee;
1489 struct hashtb_enumerator *e = &ee;
1490
1491 int res;
1492
1493 hashtb_start(nlsr->lsdb->name_lsdb, e);
1494 res = hashtb_seek(e, key,strlen(key) , 0);
1495
1496 if( res == HT_OLD_ENTRY )
1497 {
1498 nlsa=e->data;
1499
1500 nlsa->header->isValid=0;
1501
1502 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1503 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1504 write_log_for_name_lsa(nlsa);
1505 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1506
1507 hashtb_delete(e);
1508 }
1509 else if( res == HT_NEW_ENTRY )
1510 {
1511 hashtb_delete(e);
1512 }
1513 hashtb_end(e);
1514
1515 if ( nlsr->debugging )
1516 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1517 if ( nlsr->detailed_logging )
1518 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1519
1520 set_new_lsdb_version();
1521
1522 if ( nlsr->debugging )
1523 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1524 if ( nlsr->detailed_logging )
1525 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1526
1527}
1528
1529int
1530delete_name_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1531{
akmhoquefd255f42013-01-29 10:41:31 -06001532
akmhoque8fdd6412012-12-04 15:05:33 -06001533 if ( nlsr->debugging )
1534 printf("delete_name_lsa called \n");
1535 if ( nlsr->detailed_logging )
1536 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_name_lsa called \n");
1537
1538 if(flags == CCN_SCHEDULE_CANCEL)
1539 {
1540 return -1;
1541 }
1542
1543
1544
1545 nlsr_lock();
1546
1547 if ( nlsr->debugging )
1548 printf("LSA Key: %s \n",(char *)ev->evdata);
1549 if ( nlsr->detailed_logging )
1550 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Key: %s \n",(char *)ev->evdata);
1551
1552 struct nlsa *nlsa;
1553
1554 struct hashtb_enumerator ee;
1555 struct hashtb_enumerator *e = &ee;
1556
1557 int res;
1558
1559 hashtb_start(nlsr->lsdb->name_lsdb, e);
1560 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1561
1562 if( res == HT_OLD_ENTRY )
1563 {
1564 nlsa=e->data;
1565 delete_npt_entry_by_router_and_name_prefix(nlsa->header->orig_router->name, nlsa->name_prefix->name);
1566
1567 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1568 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1569 write_log_for_name_lsa(nlsa);
1570 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1571
1572 hashtb_delete(e);
1573 }
1574 else if( res == HT_NEW_ENTRY )
1575 {
1576 hashtb_delete(e);
1577 }
1578 hashtb_end(e);
1579
1580 if ( nlsr->debugging )
1581 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1582 if ( nlsr->detailed_logging )
1583 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1584
1585 set_new_lsdb_version();
1586
1587 if ( nlsr->debugging )
1588 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1589 if ( nlsr->detailed_logging )
1590 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1591
akmhoque8fdd6412012-12-04 15:05:33 -06001592 nlsr_unlock();
1593
1594 return 0;
1595}
1596
1597int
1598delete_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1599{
akmhoque8fdd6412012-12-04 15:05:33 -06001600 if ( nlsr->debugging )
1601 printf("delete_adj_lsa called \n");
1602 if ( nlsr->detailed_logging )
1603 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_adj_lsa called \n");
1604
1605 if(flags == CCN_SCHEDULE_CANCEL)
1606 {
1607 return -1;
1608 }
1609 nlsr_lock();
1610
akmhoque8fdd6412012-12-04 15:05:33 -06001611 if ( nlsr->debugging )
1612 printf("LSA Key: %s \n",(char *)ev->evdata);
1613 if ( nlsr->detailed_logging )
1614 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Key: %s \n",(char *)ev->evdata);
1615
1616 struct alsa *alsa;
1617 struct hashtb_enumerator ee;
1618 struct hashtb_enumerator *e = &ee;
1619 int res;
1620
1621 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1622 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1623
1624 if( res == HT_OLD_ENTRY )
1625 {
1626 alsa=e->data;
1627 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1628 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
1629 write_log_for_adj_lsa(alsa);
1630 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1631
1632 hashtb_delete(e);
1633 }
1634 else if( res == HT_OLD_ENTRY )
1635 {
1636 hashtb_delete(e);
1637 }
1638 hashtb_end(e);
1639
1640 if ( nlsr->debugging )
1641 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1642 if ( nlsr->detailed_logging )
1643 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1644
1645 set_new_lsdb_version();
1646
1647 if ( nlsr->debugging )
1648 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1649 if ( nlsr->detailed_logging )
1650 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1651
1652
1653 if ( !nlsr->is_route_calculation_scheduled)
1654 {
1655 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
1656 nlsr->is_route_calculation_scheduled=1;
1657 }
1658
akmhoque8fdd6412012-12-04 15:05:33 -06001659 nlsr_unlock();
1660
1661 return 0;
1662}
1663
1664void
1665refresh_name_lsdb(void)
1666{
akmhoque8fdd6412012-12-04 15:05:33 -06001667 if ( nlsr->debugging )
1668 printf("refresh_name_lsdb called \n");
1669 if ( nlsr->detailed_logging )
1670 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_name_lsdb called \n");
1671
akmhoque8fdd6412012-12-04 15:05:33 -06001672 char *time_stamp=(char *)malloc(20);
1673 memset(time_stamp,0,20);
1674 get_current_timestamp_micro(time_stamp);
1675
1676 long int lsa_life_time;
1677
1678 int i, name_lsdb_element;
1679 struct nlsa *name_lsa;
1680
1681 struct hashtb_enumerator ee;
1682 struct hashtb_enumerator *e = &ee;
1683
1684 hashtb_start(nlsr->lsdb->name_lsdb, e);
1685 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
1686
1687 for(i=0;i<name_lsdb_element;i++)
1688 {
1689 name_lsa=e->data;
1690
1691 lsa_life_time=get_time_diff(time_stamp,name_lsa->header->orig_time);
1692 if ( nlsr->debugging )
1693 printf("LSA Life Time: %ld \n",lsa_life_time);
1694 if ( nlsr->detailed_logging )
1695 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Life Time: %ld \n",lsa_life_time);
1696
1697 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name) == 0)
1698 {
1699 if ( lsa_life_time > nlsr->lsa_refresh_time )
1700 {
1701 if ( name_lsa->header->isValid == NAME_LSA_VALID )
1702 {
1703 if ( nlsr->debugging )
1704 printf("Own Name LSA need to be refrshed\n");
1705 if ( nlsr->detailed_logging )
1706 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Own Name LSA need to be refrshed\n");
1707
1708 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1709 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1710 write_log_for_name_lsa(name_lsa);
1711 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1712
1713
1714 char *current_time_stamp=(char *)malloc(20);
1715 memset(current_time_stamp,0,20);
1716 get_current_timestamp_micro(current_time_stamp);
1717
1718 free(name_lsa->header->orig_time);
1719 name_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1720 memset(name_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1721 memcpy(name_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1722
1723 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1724 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
1725 write_log_for_name_lsa(name_lsa);
1726 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1727
1728 free(current_time_stamp);
1729 }
1730 else
1731 {
1732 char lst[2];
1733 memset(lst,0,2);
1734 sprintf(lst,"%d",name_lsa->header->ls_type);
1735
1736 char lsid[10];
1737 memset(lsid,0,10);
1738 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1739
1740
1741 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1742 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1743
1744
1745 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 -06001746
1747 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
1748 }
1749
1750 if ( nlsr->debugging )
1751 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1752 if ( nlsr->detailed_logging )
1753 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1754
1755 set_new_lsdb_version();
1756
1757 if ( nlsr->debugging )
1758 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1759 if ( nlsr->detailed_logging )
1760 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1761
1762
1763
1764 print_name_lsdb();
akmhoque8fdd6412012-12-04 15:05:33 -06001765 }
1766 }
1767 else
1768 {
1769 if ( lsa_life_time > nlsr->router_dead_interval )
1770 {
1771 if ( nlsr->debugging )
1772 printf("Others Name LSA need to be deleted\n");
1773 if ( nlsr->detailed_logging )
1774 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Others Name LSA need to be deleted\n");
1775
1776 char lst[2];
1777 memset(lst,0,2);
1778 sprintf(lst,"%d",name_lsa->header->ls_type);
1779
1780 char lsid[10];
1781 memset(lsid,0,10);
1782 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1783
1784
1785 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1786 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1787
1788
1789 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 -06001790
1791 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
akmhoque8fdd6412012-12-04 15:05:33 -06001792 }
1793 }
1794
1795 hashtb_next(e);
1796 }
1797
1798 hashtb_end(e);
1799
1800 free(time_stamp);
1801
1802
1803}
1804
1805void
1806refresh_adj_lsdb(void)
1807{
akmhoque8fdd6412012-12-04 15:05:33 -06001808
1809 if ( nlsr->debugging )
1810 printf("refresh_adj_lsdb called \n");
1811 if ( nlsr->detailed_logging )
1812 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_adj_lsdb called \n");
1813
1814 char *time_stamp=(char *)malloc(20);
1815 memset(time_stamp,0,20);
1816 get_current_timestamp_micro(time_stamp);
1817
1818 long int lsa_life_time;
1819
1820 int i, adj_lsdb_element;
1821 struct alsa *adj_lsa;
1822
1823 struct hashtb_enumerator ee;
1824 struct hashtb_enumerator *e = &ee;
1825
1826 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1827 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1828
1829 for(i=0;i<adj_lsdb_element;i++)
1830 {
1831 adj_lsa=e->data;
1832
akmhoquefd255f42013-01-29 10:41:31 -06001833 lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
akmhoque8fdd6412012-12-04 15:05:33 -06001834
1835 if ( nlsr->debugging )
1836 printf("LSA Life Time: %ld \n",lsa_life_time);
1837 if ( nlsr->detailed_logging )
1838 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Life Time: %ld \n",lsa_life_time);
1839
1840 if ( strcmp(adj_lsa->header->orig_router->name,nlsr->router_name) == 0)
1841 {
1842 if ( lsa_life_time > nlsr->lsa_refresh_time )
1843 {
akmhoque8fdd6412012-12-04 15:05:33 -06001844 if ( nlsr->debugging )
1845 printf("Own Adj LSA need to be refrshed\n");
1846 if ( nlsr->detailed_logging )
1847 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Own Adj LSA need to be refrshed\n");
1848
1849 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1850 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
1851 write_log_for_adj_lsa(adj_lsa);
1852 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1853
1854 char *current_time_stamp=(char *)malloc(20);
1855 memset(current_time_stamp,0,20);
1856 get_current_timestamp_micro(current_time_stamp);
1857
1858 free(adj_lsa->header->orig_time);
1859 adj_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1860 memset(adj_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1861 memcpy(adj_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1862
1863 free(current_time_stamp);
1864
1865 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1866 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
1867 write_log_for_adj_lsa(adj_lsa);
1868 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1869
1870 if ( nlsr->debugging )
1871 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1872 if ( nlsr->detailed_logging )
1873 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1874
1875 set_new_lsdb_version();
1876
1877 if ( nlsr->debugging )
1878 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1879 if ( nlsr->detailed_logging )
1880 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1881
1882 print_adj_lsdb();
1883 }
1884 }
1885 else
1886 {
1887 if ( lsa_life_time > nlsr->router_dead_interval )
1888 {
akmhoque8fdd6412012-12-04 15:05:33 -06001889
1890 if ( nlsr->debugging )
1891 printf("Others Adj LSA need to be deleted\n");
1892 if ( nlsr->detailed_logging )
1893 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Others Adj LSA need to be deleted\n");
1894
1895 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
1896 memset(key,0,adj_lsa->header->orig_router->length+2);
1897 make_adj_lsa_key(key,adj_lsa);
akmhoquefd255f42013-01-29 10:41:31 -06001898
akmhoque8fdd6412012-12-04 15:05:33 -06001899 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_adj_lsa, (void *)key, 0);
1900 }
1901 }
1902
1903
1904
1905 hashtb_next(e);
1906 }
1907
1908 hashtb_end(e);
1909
1910 free(time_stamp);
1911}
1912
akmhoquecffe69e2013-01-30 12:33:36 -06001913
1914void
1915refresh_cor_lsdb(void)
1916{
1917
1918 if ( nlsr->debugging )
1919 printf("refresh_cor_lsdb called \n");
1920 if ( nlsr->detailed_logging )
1921 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_cor_lsdb called \n");
1922
1923 char *time_stamp=(char *)malloc(20);
1924 memset(time_stamp,0,20);
1925 get_current_timestamp_micro(time_stamp);
1926
1927 long int lsa_life_time;
1928
1929 int i, cor_lsdb_element;
1930 struct clsa *cor_lsa;
1931
1932 struct hashtb_enumerator ee;
1933 struct hashtb_enumerator *e = &ee;
1934
1935 hashtb_start(nlsr->lsdb->cor_lsdb, e);
1936 cor_lsdb_element=hashtb_n(nlsr->lsdb->cor_lsdb);
1937
1938 for(i=0;i<cor_lsdb_element;i++)
1939 {
1940 cor_lsa=e->data;
1941
1942 lsa_life_time=get_time_diff(time_stamp,cor_lsa->header->orig_time);
1943
1944 if ( nlsr->debugging )
1945 printf("LSA Life Time: %ld \n",lsa_life_time);
1946 if ( nlsr->detailed_logging )
1947 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Life Time: %ld \n",lsa_life_time);
1948
1949 if ( strcmp(cor_lsa->header->orig_router->name,nlsr->router_name) == 0)
1950 {
1951 if ( lsa_life_time > nlsr->lsa_refresh_time )
1952 {
1953 if ( nlsr->debugging )
1954 printf("Own Cor LSA need to be refrshed\n");
1955 if ( nlsr->detailed_logging )
1956 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Own Cor LSA need to be refrshed\n");
1957
1958 //writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1959 //writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
1960 //write_log_for_adj_lsa(adj_lsa);
1961 //writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1962
1963 char *current_time_stamp=(char *)malloc(20);
1964 memset(current_time_stamp,0,20);
1965 get_current_timestamp_micro(current_time_stamp);
1966
1967 free(cor_lsa->header->orig_time);
1968 cor_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1969 memset(cor_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1970 memcpy(cor_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1971
1972 free(current_time_stamp);
1973
1974 //writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1975 //writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
1976 //write_log_for_adj_lsa(adj_lsa);
1977 //writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1978
1979 if ( nlsr->debugging )
1980 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1981 if ( nlsr->detailed_logging )
1982 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1983
1984 set_new_lsdb_version();
1985
1986 if ( nlsr->debugging )
1987 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1988 if ( nlsr->detailed_logging )
1989 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1990
1991 print_adj_lsdb();
1992 }
1993 }
1994 else
1995 {
1996 if ( lsa_life_time > nlsr->router_dead_interval )
1997 {
1998
1999 if ( nlsr->debugging )
2000 printf("Others Adj LSA need to be deleted\n");
2001 if ( nlsr->detailed_logging )
2002 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Others Adj LSA need to be deleted\n");
2003
akmhoquecffe69e2013-01-30 12:33:36 -06002004 hashtb_delete(e);
2005 i++;
akmhoquecffe69e2013-01-30 12:33:36 -06002006 }
2007 }
2008
2009
2010
2011 hashtb_next(e);
2012 }
2013
2014 hashtb_end(e);
2015
2016 free(time_stamp);
2017}
2018
akmhoque8fdd6412012-12-04 15:05:33 -06002019int
2020refresh_lsdb(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
2021{
2022 if(flags == CCN_SCHEDULE_CANCEL)
2023 {
2024 return -1;
2025 }
2026
2027 nlsr_lock();
2028
akmhoque8fdd6412012-12-04 15:05:33 -06002029 if ( nlsr->debugging )
2030 printf("refresh_lsdb called\n");
2031 if ( nlsr->detailed_logging )
2032 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_lsdb called\n");
2033
2034 refresh_name_lsdb();
2035 refresh_adj_lsdb();
akmhoquecffe69e2013-01-30 12:33:36 -06002036 refresh_cor_lsdb();
akmhoque8fdd6412012-12-04 15:05:33 -06002037
2038 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
2039
2040 nlsr_unlock();
2041 return 0;
2042}
2043
2044void
akmhoque4ae16942012-12-10 11:50:43 -06002045write_adj_lsa_to_repo(char *repo_content_prefix, struct name_prefix *lsa_id)
2046{
akmhoque5a511c62013-01-16 17:31:46 -06002047 if ( nlsr->debugging )
akmhoquedecdf5b2013-01-17 03:34:29 -06002048 printf("write_adj_lsa_to_repo called\n");
akmhoque5a511c62013-01-16 17:31:46 -06002049 if ( nlsr->debugging )
2050 printf("Content Prefix: %s\n",repo_content_prefix);
akmhoque4ae16942012-12-10 11:50:43 -06002051
2052 struct ccn_charbuf *lsa_data=ccn_charbuf_create();
2053 get_adj_lsa_data(lsa_data,lsa_id);
akmhoque5a511c62013-01-16 17:31:46 -06002054 if ( nlsr->debugging )
akmhoquedecdf5b2013-01-17 03:34:29 -06002055 printf("Adj LSA Data: %s \n",ccn_charbuf_as_string(lsa_data));
akmhoque4ae16942012-12-10 11:50:43 -06002056
2057 write_data_to_repo(ccn_charbuf_as_string(lsa_data), repo_content_prefix);
2058
2059 ccn_charbuf_destroy(&lsa_data);
2060}
2061
2062void
akmhoque8fdd6412012-12-04 15:05:33 -06002063write_name_lsa_to_repo(char *repo_content_prefix, struct name_prefix *lsa_id)
2064{
akmhoquedecdf5b2013-01-17 03:34:29 -06002065 if ( nlsr->debugging )
2066 printf("write_name_lsa_to_repo called\n");
2067 if ( nlsr->debugging )
2068 printf("Content Prefix: %s\n",repo_content_prefix);
akmhoque8fdd6412012-12-04 15:05:33 -06002069
2070 struct ccn_charbuf *lsa_data=ccn_charbuf_create();
2071 get_name_lsa_data(lsa_data,lsa_id);
akmhoquedecdf5b2013-01-17 03:34:29 -06002072
2073 if ( nlsr->debugging )
2074 printf("Name LSA Data: %s \n",ccn_charbuf_as_string(lsa_data));
akmhoque8fdd6412012-12-04 15:05:33 -06002075
2076 write_data_to_repo(ccn_charbuf_as_string(lsa_data), repo_content_prefix);
2077
2078 ccn_charbuf_destroy(&lsa_data);
2079}
2080
2081
2082void
2083write_name_lsdb_to_repo(char *slice_prefix)
2084{
2085 int i, name_lsdb_element;
2086
2087 struct nlsa *name_lsa;
2088 struct hashtb_enumerator ee;
2089 struct hashtb_enumerator *e = &ee;
2090
2091 hashtb_start(nlsr->lsdb->name_lsdb, e);
2092 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
2093
2094 for(i=0;i<name_lsdb_element;i++)
2095 {
2096 name_lsa=e->data;
2097
2098 char lst[2];
2099 memset(lst,0,2);
2100 sprintf(lst,"%d",name_lsa->header->ls_type);
2101
2102 char lsid[10];
2103 memset(lsid,0,10);
2104 sprintf(lsid,"%ld",name_lsa->header->ls_id);
2105
2106
2107 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
2108 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
2109
2110
2111 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 -06002112
2113 if ( nlsr->debugging )
2114 printf("Name LSA Key: %s \n",key);
akmhoque8fdd6412012-12-04 15:05:33 -06002115
2116
2117 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);
2118 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);
2119 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);
2120
a64adam04be6482013-01-17 11:29:32 -06002121 if ( nlsr->debugging )
2122 printf("Name LSA Repo Key: %s \n",repo_key);
akmhoque8fdd6412012-12-04 15:05:33 -06002123
2124 struct name_prefix *lsaid=(struct name_prefix *)malloc(sizeof(struct name_prefix));
2125 lsaid->name=(char *)malloc(strlen(key)+1);
Syed Obaid Amin4c959562012-12-21 16:43:21 -06002126 memset(lsaid->name, 0, strlen(key)+1);
akmhoque8fdd6412012-12-04 15:05:33 -06002127 memcpy(lsaid->name,key,strlen(key));
2128 lsaid->length=strlen(key)+1;
2129
2130
2131 write_name_lsa_to_repo(repo_key, lsaid);
2132
2133 free(key);
2134 free(repo_key);
2135 free(lsaid->name);
2136 free(lsaid);
2137
2138 hashtb_next(e);
2139 }
2140
2141 hashtb_end(e);
2142
2143
2144}
akmhoqueed418f32013-01-30 12:25:04 -06002145
2146void
2147print_cor_lsa(struct clsa *cor_lsa)
2148{
2149 if ( nlsr->debugging )
2150 {
2151 printf("-----------Cor LSA Content---------------\n");
2152 printf(" Origination Router : %s\n",cor_lsa->header->orig_router->name);
2153 printf(" Origination Router Length: %d\n",cor_lsa->header->orig_router->length);
2154 printf(" LS Type : %d\n",cor_lsa->header->ls_type);
2155 printf(" Origination Time : %s\n",cor_lsa->header->orig_time);
2156 printf(" LSA Data \n");
2157 printf(" Cor R: : %f\n",cor_lsa->cor_r);
2158 printf(" Cor Theta : %f\n",cor_lsa->cor_theta);
2159
2160 printf("\n");
2161 }
2162}
2163
2164void
2165print_cor_lsdb()
2166{
2167
2168 if ( nlsr->debugging )
2169 printf("print_cor_lsdb called \n");
2170
2171 struct hashtb_enumerator ee;
2172 struct hashtb_enumerator *e = &ee;
2173
2174 int i=1;
2175
2176 for (hashtb_start(nlsr->lsdb->cor_lsdb, e); e->key != NULL; hashtb_next(e))
2177 {
2178 if ( nlsr->debugging )
2179 printf("-----------Cor LSA (%d)---------------\n",i);
2180 struct clsa *cor_lsa=e->data;
2181 print_cor_lsa(cor_lsa);
2182 i++;
2183 }
2184 hashtb_end(e);
2185
2186 if ( nlsr->debugging )
2187 printf("\n");
2188 if ( nlsr->detailed_logging )
2189 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
2190}
2191
2192void
2193install_cor_lsa(struct clsa *cor_lsa)
2194{
2195 if ( nlsr->debugging )
2196 printf("install_cor_lsa called \n");
2197 if ( nlsr->detailed_logging )
2198 writeLogg(__FILE__,__FUNCTION__,__LINE__,"install_cor_lsa called \n");
2199
2200
2201 char *time_stamp=(char *)malloc(20);
2202 memset(time_stamp,0,20);
2203 get_current_timestamp_micro(time_stamp);
2204
2205
2206 char *key=(char *)malloc(cor_lsa->header->orig_router->length+2+2);
2207 memset(key,0,cor_lsa->header->orig_router->length+2);
2208 make_cor_lsa_key(key,cor_lsa);
2209
2210 if ( nlsr->debugging )
2211 printf("Cor LSA key: %s \n",key);
2212
2213 struct hashtb_enumerator ee;
2214 struct hashtb_enumerator *e = &ee;
2215 int res;
2216
2217 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2218 res = hashtb_seek(e, key, strlen(key), 0);
2219
2220 if ( res == HT_NEW_ENTRY )
2221 {
2222 if ( nlsr->debugging )
2223 printf("New Cor LSA... \n");
2224
2225 struct clsa *new_cor_lsa=(struct clsa *)malloc(sizeof( struct clsa ));
2226 new_cor_lsa=e->data;
2227 new_cor_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
2228
2229 new_cor_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
2230 new_cor_lsa->header->orig_router->name=(char *)malloc(strlen(cor_lsa->header->orig_router->name)+1);
2231 memset(new_cor_lsa->header->orig_router->name,0,strlen(cor_lsa->header->orig_router->name)+1);
2232 memcpy(new_cor_lsa->header->orig_router->name,cor_lsa->header->orig_router->name,strlen(cor_lsa->header->orig_router->name)+1);
2233 new_cor_lsa->header->orig_router->length=cor_lsa->header->orig_router->length;
2234
2235 new_cor_lsa->header->orig_time=(char *)malloc(strlen(cor_lsa->header->orig_time)+1); //free
2236 memset(new_cor_lsa->header->orig_time,0,strlen(cor_lsa->header->orig_time)+1);
2237 memcpy(new_cor_lsa->header->orig_time,cor_lsa->header->orig_time,strlen(cor_lsa->header->orig_time)+1);
2238
2239 new_cor_lsa->header->ls_type=cor_lsa->header->ls_type;
2240
2241 new_cor_lsa->cor_r=cor_lsa->cor_r;
2242 new_cor_lsa->cor_theta=cor_lsa->cor_theta;
2243 }
2244 else if ( res == HT_OLD_ENTRY )
2245 {
2246 if ( nlsr->debugging )
2247 printf("Cor LSA exists (Old)... \n");
2248 }
2249 hashtb_end(e);
2250
2251 free(key);
2252
2253}
2254
2255void
2256build_cor_lsa(struct clsa *cor_lsa, double cor_r, double cor_theta)
2257{
2258 cor_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
2259 cor_lsa->header->ls_type=LS_TYPE_COR;
2260
2261 char *time_stamp=(char *)malloc(20);
2262 memset(time_stamp,0,20);
2263 get_current_timestamp_micro(time_stamp);
2264
2265 cor_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1); //free
2266 memset(cor_lsa->header->orig_time,0,strlen(time_stamp)+1);
2267 memcpy(cor_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
2268 free(time_stamp);
2269
2270 cor_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
2271 cor_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1);
2272 memset(cor_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
2273 memcpy(cor_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
2274 cor_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
2275
2276 cor_lsa->cor_r=cor_r;
2277 cor_lsa->cor_theta=cor_theta;
2278
2279}
2280
2281void
2282build_others_cor_lsa(struct clsa *cor_lsa,char *orig_router, int ls_type,char *orig_time, double cor_r, double cor_theta)
2283{
2284 cor_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
2285 cor_lsa->header->ls_type=ls_type;
2286
2287 cor_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
2288 memset(cor_lsa->header->orig_time,0,strlen(orig_time)+1);
2289 memcpy(cor_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
2290
2291 cor_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
2292 cor_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
2293 memset(cor_lsa->header->orig_router->name,0,strlen(orig_router)+1);
2294 memcpy(cor_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
2295 cor_lsa->header->orig_router->length=strlen(orig_router)+1;
2296
2297 cor_lsa->cor_r=cor_r;
2298 cor_lsa->cor_theta=cor_theta;
2299
2300}
2301
2302void
2303build_and_install_others_cor_lsa(char *orig_router,int ls_type,char *orig_time, double cor_r, double cor_theta)
2304{
2305 struct clsa *cor_lsa=(struct clsa *)malloc(sizeof( struct clsa ));
2306 build_others_cor_lsa(cor_lsa,orig_router,ls_type, orig_time, cor_r, cor_theta);
2307 install_cor_lsa(cor_lsa);
2308
2309 print_cor_lsdb();
2310
2311 free(cor_lsa->header->orig_router);
2312 free(cor_lsa->header->orig_time);
2313 free(cor_lsa->header);
2314 free(cor_lsa);
2315
2316}
2317
2318
2319void
2320build_and_install_cor_lsa()
2321{
2322
2323 struct clsa *cor_lsa=(struct clsa *)malloc(sizeof( struct clsa ));
2324
2325 build_cor_lsa(cor_lsa,nlsr->cor_r,nlsr->cor_theta);
2326 install_cor_lsa(cor_lsa);
2327
2328 write_cor_lsa_to_repo(cor_lsa);
2329
2330 print_cor_lsdb();
2331
2332 free(cor_lsa->header->orig_router);
2333 free(cor_lsa->header->orig_time);
2334 free(cor_lsa->header);
2335 free(cor_lsa);
2336
2337}
2338
2339void
2340get_cor_lsa_data(struct ccn_charbuf *lsa_data,char *cor_lsa_key)
2341{
2342 if ( nlsr->debugging )
2343 printf("get_cor_lsa_data called \n");
2344 if ( nlsr->detailed_logging )
2345 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_adj_lsa_data called \n");
2346
2347 struct clsa *cor_lsa=(struct clsa*)malloc(sizeof(struct clsa ));
2348
2349 struct hashtb_enumerator ee;
2350 struct hashtb_enumerator *e = &ee;
2351 int res;
2352
2353 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2354 res = hashtb_seek(e, cor_lsa_key, strlen(cor_lsa_key), 0);
2355
2356 if( res == HT_OLD_ENTRY )
2357 {
2358 cor_lsa=e->data;
2359
2360 if ( nlsr->debugging )
2361 printf("Cor LSA found \n");
2362 if ( nlsr->detailed_logging )
2363 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Cor LSA found \n");
2364
2365 ccn_charbuf_append_string(lsa_data,cor_lsa->header->orig_router->name);
2366 ccn_charbuf_append_string(lsa_data,"|");
2367
2368 char *temp_length=(char *)malloc(20);
2369 memset(temp_length,0,20);
2370 sprintf(temp_length,"%d",cor_lsa->header->orig_router->length);
2371 ccn_charbuf_append_string(lsa_data,temp_length);
2372 ccn_charbuf_append_string(lsa_data,"|");
2373 free(temp_length);
2374
2375 char *temp_ltype=(char *)malloc(20);
2376 memset(temp_ltype,0,20);
2377 sprintf(temp_ltype,"%d",cor_lsa->header->ls_type);
2378 ccn_charbuf_append_string(lsa_data,temp_ltype);
2379 ccn_charbuf_append_string(lsa_data,"|");
2380 free(temp_ltype);
2381
2382 ccn_charbuf_append_string(lsa_data,cor_lsa->header->orig_time);
2383 ccn_charbuf_append_string(lsa_data,"|");
2384
2385 char *cor_r=(char *)malloc(20);
2386 memset(cor_r,0,20);
2387 sprintf(cor_r,"%f",cor_lsa->cor_r);
2388 ccn_charbuf_append_string(lsa_data,cor_r);
2389 ccn_charbuf_append_string(lsa_data,"|");
2390 free(cor_r);
2391
2392 char *cor_theta=(char *)malloc(20);
2393 memset(cor_theta,0,20);
2394 sprintf(cor_theta,"%f",cor_lsa->cor_theta);
2395 ccn_charbuf_append_string(lsa_data,cor_theta);
2396 ccn_charbuf_append_string(lsa_data,"|");
2397 free(cor_theta);
2398
2399 }
2400 else if(res == HT_NEW_ENTRY)
2401 {
2402 hashtb_delete(e);
2403 }
2404
2405 hashtb_end(e);
2406}
2407
2408void
2409write_cor_lsa_to_repo(struct clsa *cor_lsa)
2410{
2411
2412
2413 if ( nlsr->debugging )
2414 printf("write_cor_lsa_to_repo called\n");
2415
2416
2417 char *key=(char *)malloc(cor_lsa->header->orig_router->length+2+2);
akmhoquef06586e2013-02-05 12:09:38 -06002418 memset(key,0,cor_lsa->header->orig_router->length+2+2);
akmhoqueed418f32013-01-30 12:25:04 -06002419 make_cor_lsa_key(key,cor_lsa);
2420
2421 struct ccn_charbuf *lsa_data=ccn_charbuf_create();
2422 get_cor_lsa_data(lsa_data,key);
2423
2424 if ( nlsr->debugging )
2425 printf("Cor LSA Data: %s \n",ccn_charbuf_as_string(lsa_data));
2426 char *lst=(char *)malloc(20);
2427 memset(lst,0,20);
2428 sprintf(lst,"%d",cor_lsa->header->ls_type);
2429 char *repo_key=(char *)malloc(strlen(nlsr->slice_prefix)+strlen(cor_lsa->header->orig_time)+strlen(cor_lsa->header->orig_router->name) + strlen(lst) + 5);
2430 memset(repo_key, 0, strlen(nlsr->slice_prefix)+strlen(cor_lsa->header->orig_time)+strlen(cor_lsa->header->orig_router->name) + strlen(lst) + 5);
2431 make_cor_lsa_prefix_for_repo(repo_key, cor_lsa->header->orig_router->name,LS_TYPE_COR,cor_lsa->header->orig_time,nlsr->slice_prefix);
2432
2433 if ( nlsr->debugging )
2434 printf("Cor LSA Repo Key: %s \n",repo_key);
2435
2436 write_data_to_repo(ccn_charbuf_as_string(lsa_data), repo_key);
2437
2438
2439
2440 free(lst);
2441 free(key);
2442 free(repo_key);
2443 ccn_charbuf_destroy(&lsa_data);
2444}
2445
akmhoquef06586e2013-02-05 12:09:38 -06002446void
2447make_cor_lsa_key_by_router_name(char *key,char *router_name)
2448{
2449 memcpy(key+strlen(key),router_name,strlen(router_name));
2450 memcpy(key+strlen(key),"/",1);
2451 char ls_type[2];
2452 sprintf(ls_type,"%d",LS_TYPE_COR);
2453 memcpy(key+strlen(key),ls_type,strlen(ls_type));
2454 key[strlen(key)]='\0';
2455}
2456
2457
2458double
2459get_hyperbolic_r(char *router)
2460{
2461 double ret=-1.0;
2462 char *cor_lsa_key=(char *)calloc(strlen(router)+4,sizeof(char));
2463 make_cor_lsa_key_by_router_name(cor_lsa_key,router);
2464
2465
2466 struct clsa *cor_lsa;
2467 struct hashtb_enumerator ee;
2468 struct hashtb_enumerator *e = &ee;
2469 int res;
2470
2471 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2472 res = hashtb_seek(e, cor_lsa_key, strlen(cor_lsa_key), 0);
2473
2474 if ( res == HT_OLD_ENTRY)
2475 {
2476 cor_lsa=e->data;
2477 ret=cor_lsa->cor_r;
2478 }
2479 else if(res == HT_NEW_ENTRY)
2480 {
2481 hashtb_delete(e);
2482 }
2483
2484 hashtb_end(e);
2485
2486 free(cor_lsa_key);
2487 return ret;
2488}
2489
2490double
2491get_hyperbolic_theta(char *router)
2492{
2493 double ret=-1.0;
2494 char *cor_lsa_key=(char *)calloc(strlen(router)+4,sizeof(char));
2495 make_cor_lsa_key_by_router_name(cor_lsa_key,router);
2496
2497 struct clsa *cor_lsa;
2498 struct hashtb_enumerator ee;
2499 struct hashtb_enumerator *e = &ee;
2500 int res;
2501
2502 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2503 res = hashtb_seek(e, cor_lsa_key, strlen(cor_lsa_key), 0);
2504
2505 if ( res == HT_OLD_ENTRY)
2506 {
2507 cor_lsa=e->data;
2508 ret=cor_lsa->cor_theta;
2509 }
2510 else if(res == HT_NEW_ENTRY)
2511 {
2512 hashtb_delete(e);
2513 }
2514
2515 hashtb_end(e);
2516
2517 free(cor_lsa_key);
2518 return ret;
2519}