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