blob: b1455861cd452691aa7fc7bc12179b111910b734 [file] [log] [blame]
akmhoquef1161eb2012-08-21 09:37:21 -04001#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"
akmhoque03004e62012-09-06 01:12:28 -050021#include "nlsr_ndn.h"
akmhoquef1161eb2012-08-21 09:37:21 -040022#include "nlsr_lsdb.h"
akmhoquef71d9082012-08-22 12:51:53 -040023#include "utility.h"
akmhoque03004e62012-09-06 01:12:28 -050024#include "nlsr_npl.h"
akmhoque53f64222012-09-05 13:57:51 -050025#include "nlsr_adl.h"
akmhoque29c1db52012-09-07 14:47:43 -050026#include "nlsr_route.h"
akmhoque3560cb62012-09-09 10:52:30 -050027#include "nlsr_npt.h"
akmhoque438b07e2012-08-21 10:13:57 -040028
akmhoquef71d9082012-08-22 12:51:53 -040029void
akmhoque53f64222012-09-05 13:57:51 -050030set_new_lsdb_version(void)
akmhoquef71d9082012-08-22 12:51:53 -040031{
akmhoquef71d9082012-08-22 12:51:53 -040032
akmhoque03004e62012-09-06 01:12:28 -050033 char *time_stamp=(char *)malloc(20);
34 memset(time_stamp,0,20);
35 get_current_timestamp_micro(time_stamp);
akmhoquef71d9082012-08-22 12:51:53 -040036
akmhoque03004e62012-09-06 01:12:28 -050037 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);
akmhoque53f64222012-09-05 13:57:51 -050043
akmhoquef71d9082012-08-22 12:51:53 -040044}
45
akmhoque03004e62012-09-06 01:12:28 -050046void
47make_name_lsa_key(char *key, char *orig_router, int ls_type, long int ls_id)
48{
49
50
51 printf("Orig Router: %s LS Type: %d LS Id: %ld\n",orig_router,ls_type,ls_id);
52
53 char lst[2];
54 memset(lst,0,2);
55 sprintf(lst,"%d",ls_type);
56
57 char lsid[10];
58 memset(lsid,0,10);
59 sprintf(lsid,"%ld",ls_id);
60
61 memcpy(key+strlen(key),orig_router,strlen(orig_router));
62 memcpy(key+strlen(key),"/",1);
63 memcpy(key+strlen(key),lst,strlen(lst));
64 memcpy(key+strlen(key),"/",1);
65 memcpy(key+strlen(key),lsid,strlen(lsid));
66
67 printf("Key: %s\n",key);
68
69}
akmhoque53f64222012-09-05 13:57:51 -050070
akmhoquef71d9082012-08-22 12:51:53 -040071void
akmhoque53f64222012-09-05 13:57:51 -050072build_and_install_name_lsas(void)
akmhoquef71d9082012-08-22 12:51:53 -040073{
akmhoque53f64222012-09-05 13:57:51 -050074 printf("build_and_install_name_lsas called \n");
akmhoquef71d9082012-08-22 12:51:53 -040075
akmhoquef71d9082012-08-22 12:51:53 -040076 int i, npl_element;
77 struct name_prefix *np;
78
79 struct hashtb_enumerator ee;
80 struct hashtb_enumerator *e = &ee;
81
82 hashtb_start(nlsr->npl, e);
83 npl_element=hashtb_n(nlsr->npl);
84
85 for(i=0;i<npl_element;i++)
86 {
87 np=e->data;
akmhoque53f64222012-09-05 13:57:51 -050088 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
89 build_name_lsa(name_lsa,np);
akmhoque03004e62012-09-06 01:12:28 -050090
akmhoquef71d9082012-08-22 12:51:53 -040091 install_name_lsa(name_lsa);
akmhoque03004e62012-09-06 01:12:28 -050092 free(name_lsa->header->orig_router->name);
93 free(name_lsa->header->orig_router);
94 free(name_lsa->header);
95 free(name_lsa->name_prefix->name);
96 free(name_lsa->name_prefix);
akmhoquef71d9082012-08-22 12:51:53 -040097 free(name_lsa);
akmhoquef71d9082012-08-22 12:51:53 -040098 hashtb_next(e);
99 }
100
akmhoque53f64222012-09-05 13:57:51 -0500101 hashtb_end(e);
akmhoquef71d9082012-08-22 12:51:53 -0400102
akmhoquef71d9082012-08-22 12:51:53 -0400103}
104
akmhoque53f64222012-09-05 13:57:51 -0500105void
106build_name_lsa(struct nlsa *name_lsa, struct name_prefix *np)
107{
akmhoque53f64222012-09-05 13:57:51 -0500108 name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header ));
109 name_lsa->header->ls_type=LS_TYPE_NAME;
110
akmhoque03004e62012-09-06 01:12:28 -0500111 char *time_stamp=(char *)malloc(20);
112 memset(time_stamp,0,20);
113 get_current_timestamp_micro(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -0500114
akmhoque03004e62012-09-06 01:12:28 -0500115 name_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1); //free
akmhoque53f64222012-09-05 13:57:51 -0500116 memset(name_lsa->header->orig_time,0,strlen(time_stamp)+1);
117 memcpy(name_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
akmhoque03004e62012-09-06 01:12:28 -0500118
119 free(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -0500120
121 name_lsa->header->ls_id=++nlsr->nlsa_id;
122 name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoque03004e62012-09-06 01:12:28 -0500123 name_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1);
124 memset(name_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
125 memcpy(name_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
126 name_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
akmhoque53f64222012-09-05 13:57:51 -0500127 name_lsa->header->isValid=1;
128
129
130 name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
131 name_lsa->name_prefix->name=(char *)malloc(np->length);
132 memcpy(name_lsa->name_prefix->name,np->name,np->length);
133 name_lsa->name_prefix->length=np->length;
134
135}
136
akmhoque53f64222012-09-05 13:57:51 -0500137void
138install_name_lsa(struct nlsa *name_lsa)
139{
akmhoque53f64222012-09-05 13:57:51 -0500140
akmhoqueffacaa82012-09-13 17:48:30 -0500141 char *time_stamp=(char *)malloc(20);
142 memset(time_stamp,0,20);
143 get_current_timestamp_micro(time_stamp);
144 long int lsa_life_time=get_time_diff(time_stamp,name_lsa->header->orig_time);
145
146 printf("time difference: %ld \n",lsa_life_time);
147
148
149 char lst[2];
150 memset(lst,0,2);
151 sprintf(lst,"%d",name_lsa->header->ls_type);
152
153 char lsid[10];
154 memset(lsid,0,10);
155 sprintf(lsid,"%ld",name_lsa->header->ls_id);
akmhoque03004e62012-09-06 01:12:28 -0500156
157
akmhoqueffacaa82012-09-13 17:48:30 -0500158 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
159 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
akmhoque03004e62012-09-06 01:12:28 -0500160
161
akmhoqueffacaa82012-09-13 17:48:30 -0500162 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
163 printf("Key:%s Length:%d\n",key,(int)strlen(key));
akmhoque53f64222012-09-05 13:57:51 -0500164
akmhoqueffacaa82012-09-13 17:48:30 -0500165 struct nlsa *new_name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa )); //free
akmhoque53f64222012-09-05 13:57:51 -0500166
akmhoqueffacaa82012-09-13 17:48:30 -0500167 struct hashtb_enumerator ee;
168 struct hashtb_enumerator *e = &ee;
169 int res;
akmhoque53f64222012-09-05 13:57:51 -0500170
akmhoqueffacaa82012-09-13 17:48:30 -0500171 hashtb_start(nlsr->lsdb->name_lsdb, e);
172 res = hashtb_seek(e, key, strlen(key), 0);
akmhoque53f64222012-09-05 13:57:51 -0500173
akmhoqueffacaa82012-09-13 17:48:30 -0500174 if(res == HT_NEW_ENTRY )
akmhoque3560cb62012-09-09 10:52:30 -0500175 {
akmhoqueffacaa82012-09-13 17:48:30 -0500176
177
178
179
180 printf("New Name LSA... Adding to LSDB\n");
181 new_name_lsa = e->data;
182
183 new_name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header )); //free
184 new_name_lsa->header->ls_type=name_lsa->header->ls_type;
185
186 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
187 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
188 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
189
190 new_name_lsa->header->ls_id=name_lsa->header->ls_id;
191 new_name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free
192 new_name_lsa->header->orig_router->name=(char *)malloc(name_lsa->header->orig_router->length);
193 memcpy(new_name_lsa->header->orig_router->name,name_lsa->header->orig_router->name,name_lsa->header->orig_router->length);
194 new_name_lsa->header->orig_router->length=name_lsa->header->orig_router->length;
195 new_name_lsa->header->isValid=name_lsa->header->isValid;
196
197
198 new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free
199 new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
200 memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
201 new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
202
203 printf("New Name LSA Added....\n");
204
205 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
206 set_new_lsdb_version();
207 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
208
209 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name);
210 if ( next_hop == NO_NEXT_HOP )
akmhoque3560cb62012-09-09 10:52:30 -0500211 {
akmhoqueffacaa82012-09-13 17:48:30 -0500212 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,NO_FACE);
213 if ( check == HT_NEW_ENTRY )
214 {
215 printf("Added in npt \n");
216 }
akmhoque3560cb62012-09-09 10:52:30 -0500217 }
akmhoqueffacaa82012-09-13 17:48:30 -0500218 else
akmhoque3560cb62012-09-09 10:52:30 -0500219 {
akmhoqueffacaa82012-09-13 17:48:30 -0500220 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,next_hop);
221 if ( check == HT_NEW_ENTRY )
222 {
223 printf("Added in npt \n");
224 }
225
akmhoque3560cb62012-09-09 10:52:30 -0500226 }
227
akmhoqueffacaa82012-09-13 17:48:30 -0500228
229 free(time_stamp);
230
231 }
232 else if(res == HT_OLD_ENTRY)
233 {
234 new_name_lsa=e->data;
235 if(strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time)<=0)
236 {
237 printf("Older/Duplicate Adj LSA. Discarded...\n");
238 }
239 else
240 {
241 if ( name_lsa->header->isValid == 0 )
242 {
243 // have to call to delete npt table entry
244 delete_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name);
245
246 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name)!= 0)
247 {
248 hashtb_delete(e);
249 printf("isValid bit not set for Router %s so LSA Deleted from LSDB\n",name_lsa->header->orig_router->name);
250 }
251 else
252 {
253 new_name_lsa->header->isValid=name_lsa->header->isValid;
254 free(new_name_lsa->header->orig_time);
255 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
256 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
257 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
258 }
259 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
260 set_new_lsdb_version();
261 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
262 }
263 else
264 {
265 int is_npt_update=0;
266 if ( strcmp(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name) != 0 )
267 {
268 is_npt_update=1;
269 delete_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name);
270 }
271
272 // copying LSA content with header
273
274 free(new_name_lsa->header->orig_time);
275 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
276 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
277 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
278
279 new_name_lsa->header->isValid=name_lsa->header->isValid;
280
281 free(new_name_lsa->name_prefix->name);
282 free(new_name_lsa->name_prefix);
283 new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
284 new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
285 memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
286 new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
287
288 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
289 set_new_lsdb_version();
290 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
291
292 if( is_npt_update == 1 )
293 {
294 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name);
295 if ( next_hop == NO_NEXT_HOP )
296 {
297 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,NO_FACE);
298 if ( check == HT_NEW_ENTRY )
299 {
300 printf("Added in npt \n");
301 }
302 }
303 else
304 {
305 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,next_hop);
306 if ( check == HT_NEW_ENTRY )
307 {
308 printf("Added in npt \n");
309 }
310 }
311 }
312 }
313 }
314
akmhoque3560cb62012-09-09 10:52:30 -0500315 }
akmhoque53f64222012-09-05 13:57:51 -0500316
akmhoqueffacaa82012-09-13 17:48:30 -0500317 hashtb_end(e);
akmhoque53f64222012-09-05 13:57:51 -0500318
akmhoqueffacaa82012-09-13 17:48:30 -0500319 free(key);
akmhoque53f64222012-09-05 13:57:51 -0500320}
321
322
akmhoque03004e62012-09-06 01:12:28 -0500323void
324print_name_lsa(struct nlsa *name_lsa)
akmhoque53f64222012-09-05 13:57:51 -0500325{
326
akmhoque03004e62012-09-06 01:12:28 -0500327 printf("-----------Name LSA Content---------------\n");
328 printf(" Origination Router : %s\n",name_lsa->header->orig_router->name);
329 printf(" Origination Router Length: %d\n",name_lsa->header->orig_router->length);
330 printf(" LS Type : %d\n",name_lsa->header->ls_type);
331 printf(" LS Id : %ld\n",name_lsa->header->ls_id);
332 printf(" Origination Time : %s\n",name_lsa->header->orig_time);
333 printf(" Is Valid : %d\n",name_lsa->header->isValid);
334 printf(" LSA Data \n");
335 printf(" Name Prefix: : %s\n",name_lsa->name_prefix->name);
336 printf(" Name Prefix Length : %d\n",name_lsa->name_prefix->length);
akmhoque53f64222012-09-05 13:57:51 -0500337
akmhoque03004e62012-09-06 01:12:28 -0500338 printf("\n");
akmhoquef71d9082012-08-22 12:51:53 -0400339}
340
341void
342print_name_lsdb(void)
343{
344 printf("print_name_lsdb called \n");
akmhoque53f64222012-09-05 13:57:51 -0500345 int i, name_lsdb_element;
akmhoquef71d9082012-08-22 12:51:53 -0400346 struct nlsa *name_lsa;
347
348 struct hashtb_enumerator ee;
349 struct hashtb_enumerator *e = &ee;
350
351 hashtb_start(nlsr->lsdb->name_lsdb, e);
akmhoque53f64222012-09-05 13:57:51 -0500352 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
akmhoquef71d9082012-08-22 12:51:53 -0400353
akmhoque53f64222012-09-05 13:57:51 -0500354 for(i=0;i<name_lsdb_element;i++)
akmhoquef71d9082012-08-22 12:51:53 -0400355 {
akmhoque53f64222012-09-05 13:57:51 -0500356 printf("-----------Name LSA (%d)---------------\n",i+1);
akmhoquef71d9082012-08-22 12:51:53 -0400357 name_lsa=e->data;
akmhoque53f64222012-09-05 13:57:51 -0500358 print_name_lsa(name_lsa);
akmhoquef71d9082012-08-22 12:51:53 -0400359 hashtb_next(e);
360 }
361
362 hashtb_end(e);
363
akmhoque53f64222012-09-05 13:57:51 -0500364 printf("\n");
365}
366
akmhoque03004e62012-09-06 01:12:28 -0500367
368void
369build_and_install_others_name_lsa(char *orig_router,int ls_type,long int ls_id,char *orig_time, int isValid,char *np)
akmhoque53f64222012-09-05 13:57:51 -0500370{
akmhoque03004e62012-09-06 01:12:28 -0500371 printf("build_and_install_others_name_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500372
akmhoque03004e62012-09-06 01:12:28 -0500373 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
374 build_others_name_lsa(name_lsa,orig_router,ls_type,ls_id,orig_time, isValid,np);
375 print_name_lsa(name_lsa);
376 install_name_lsa(name_lsa);
377 print_name_lsdb();
akmhoque53f64222012-09-05 13:57:51 -0500378
akmhoque53f64222012-09-05 13:57:51 -0500379
akmhoque03004e62012-09-06 01:12:28 -0500380 free(name_lsa->header->orig_router->name);
381 free(name_lsa->header->orig_router);
382 free(name_lsa->header);
383 free(name_lsa->name_prefix->name);
384 free(name_lsa->name_prefix);
385 free(name_lsa);
akmhoque53f64222012-09-05 13:57:51 -0500386
387}
388
389void
akmhoque03004e62012-09-06 01:12:28 -0500390build_others_name_lsa(struct nlsa *name_lsa, char *orig_router,int ls_type,long int ls_id,char *orig_time, int isValid,char *np)
akmhoque53f64222012-09-05 13:57:51 -0500391{
akmhoque03004e62012-09-06 01:12:28 -0500392 printf("build_others_name_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500393
akmhoque03004e62012-09-06 01:12:28 -0500394 name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header ));
395 name_lsa->header->ls_type=LS_TYPE_NAME;
akmhoque53f64222012-09-05 13:57:51 -0500396
akmhoque03004e62012-09-06 01:12:28 -0500397 name_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
398 memset(name_lsa->header->orig_time,0,strlen(orig_time)+1);
399 memcpy(name_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
akmhoque53f64222012-09-05 13:57:51 -0500400
akmhoque03004e62012-09-06 01:12:28 -0500401 name_lsa->header->ls_id=ls_id;
402 name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
403 name_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
404 memset(name_lsa->header->orig_router->name,0,strlen(orig_router)+1);
405 memcpy(name_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
406 name_lsa->header->orig_router->length=strlen(orig_router)+1;
407 name_lsa->header->isValid=isValid;
akmhoque53f64222012-09-05 13:57:51 -0500408
akmhoque03004e62012-09-06 01:12:28 -0500409 name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
410 name_lsa->name_prefix->name=(char *)malloc(strlen(np)+1);
411 memset(name_lsa->name_prefix->name,0,strlen(np)+1);
412 memcpy(name_lsa->name_prefix->name,np,strlen(np)+1);
413 name_lsa->name_prefix->length=strlen(np)+1;
akmhoquef71d9082012-08-22 12:51:53 -0400414}
akmhoqued79438d2012-08-27 13:31:42 -0500415
416
akmhoqueffacaa82012-09-13 17:48:30 -0500417
418
akmhoque53f64222012-09-05 13:57:51 -0500419void
420make_adj_lsa_key(char *key,struct alsa *adj_lsa)
akmhoqued79438d2012-08-27 13:31:42 -0500421{
akmhoque53f64222012-09-05 13:57:51 -0500422 memcpy(key+strlen(key),adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
423 memcpy(key+strlen(key),"/",1);
424 char ls_type[2];
425 sprintf(ls_type,"%d",adj_lsa->header->ls_type);
426 memcpy(key+strlen(key),ls_type,strlen(ls_type));
427 key[strlen(key)]='\0';
428}
429
akmhoque03004e62012-09-06 01:12:28 -0500430int
431build_and_install_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
432{
akmhoqueffacaa82012-09-13 17:48:30 -0500433 if(flags == CCN_SCHEDULE_CANCEL)
434 {
435 return -1;
436 }
437
438 nlsr_lock();
439
akmhoque03004e62012-09-06 01:12:28 -0500440 printf("build_and_install_adj_lsa called \n");
441
442 printf("adj_build_flag = %d \n",nlsr->adj_build_flag);
443
444 if(nlsr->adj_build_flag > 0)
445 {
446 printf("is_adj_lsa_build = %d \n",is_adj_lsa_build());
447 if ( is_adj_lsa_build()> 0)
448 {
449 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
450 build_adj_lsa(adj_lsa);
451 install_adj_lsa(adj_lsa);
452
453 free(adj_lsa->header->orig_router->name);
454 free(adj_lsa->header->orig_router);
455 free(adj_lsa->header->orig_time);
456 free(adj_lsa->header);
457 free(adj_lsa->body);
458 free(adj_lsa);
459 nlsr->adj_build_flag=0;
460 print_adj_lsdb();
461 }
462 else
463 {
464 printf("Can not build adj LSA now\n");
465 }
466 }
467 nlsr->is_build_adj_lsa_sheduled=0;
akmhoqueffacaa82012-09-13 17:48:30 -0500468
469 nlsr_unlock();
470
akmhoque03004e62012-09-06 01:12:28 -0500471 return 0;
472}
473
474
475void
476build_adj_lsa(struct alsa * adj_lsa)
477{
478 printf("build_adj_lsa called \n");
479
480 int no_link=no_active_nbr();
481 printf("Number of link in Adjacent LSA: %d\n",no_link);
482
483 /*Filling Up Header Data */
484 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
485 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
486 adj_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1);
487 memset(adj_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
488 memcpy(adj_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
489 adj_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
490
491 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
492
493 char *time_stamp=(char *)malloc(20);
494 memset(time_stamp,0,20);
495 get_current_timestamp_micro(time_stamp);
496
497 adj_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1);
498 memset(adj_lsa->header->orig_time,0,strlen(time_stamp)+1);
499 memcpy(adj_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
500 free(time_stamp);
501
502
503 /* Filling Up Body Data */
504
505 adj_lsa->no_link=no_link;
akmhoque03004e62012-09-06 01:12:28 -0500506
507
508 struct ccn_charbuf *c=ccn_charbuf_create();
509 get_active_nbr_adj_data(c);
510 char *data=ccn_charbuf_as_string(c);
511
512 adj_lsa->body=(char *)malloc(strlen(data)+1);
513 memset(adj_lsa->body,0,strlen(data)+1);
514 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
515 ccn_charbuf_destroy(&c);
516
517
518
519 if( !nlsr->is_send_lsdb_interest_scheduled )
520 {
521 nlsr->event_send_lsdb_interest= ccn_schedule_event(nlsr->sched, 1000, &send_lsdb_interest, NULL, 0);
522 nlsr->is_send_lsdb_interest_scheduled=1;
523 }
524
525 nlsr->adj_build_count++;
526
527
528}
529
530
akmhoque53f64222012-09-05 13:57:51 -0500531void
532install_adj_lsa(struct alsa * adj_lsa)
533{
akmhoqued79438d2012-08-27 13:31:42 -0500534 printf("install_adj_lsa called \n");
535
akmhoque03004e62012-09-06 01:12:28 -0500536
akmhoqueffacaa82012-09-13 17:48:30 -0500537 char *time_stamp=(char *)malloc(20);
538 memset(time_stamp,0,20);
539 get_current_timestamp_micro(time_stamp);
540 long int lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
akmhoque53f64222012-09-05 13:57:51 -0500541
akmhoqueffacaa82012-09-13 17:48:30 -0500542 printf("time difference: %ld \n",lsa_life_time);
akmhoque53f64222012-09-05 13:57:51 -0500543
akmhoqueffacaa82012-09-13 17:48:30 -0500544
545 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
546 memset(key,0,adj_lsa->header->orig_router->length+2);
547 make_adj_lsa_key(key,adj_lsa);
548 printf("Adjacent LSA key: %s \n",key);
549
550 struct alsa *new_adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
551
552 struct hashtb_enumerator ee;
553 struct hashtb_enumerator *e = &ee;
554 int res;
555
556 hashtb_start(nlsr->lsdb->adj_lsdb, e);
557 res = hashtb_seek(e, key, strlen(key), 0);
akmhoque53f64222012-09-05 13:57:51 -0500558
akmhoque03004e62012-09-06 01:12:28 -0500559
akmhoque53f64222012-09-05 13:57:51 -0500560
akmhoqueffacaa82012-09-13 17:48:30 -0500561 if(res == HT_NEW_ENTRY )
akmhoque53f64222012-09-05 13:57:51 -0500562 {
akmhoqueffacaa82012-09-13 17:48:30 -0500563 printf("New ADJ LSA... Adding to LSDB\n");
akmhoque53f64222012-09-05 13:57:51 -0500564 new_adj_lsa = e->data;
565
akmhoqueffacaa82012-09-13 17:48:30 -0500566 new_adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
567 new_adj_lsa->header->ls_type=adj_lsa->header->ls_type;
akmhoque53f64222012-09-05 13:57:51 -0500568 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
akmhoqueffacaa82012-09-13 17:48:30 -0500569 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
570
571 new_adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
572 new_adj_lsa->header->orig_router->name=(char *)malloc(adj_lsa->header->orig_router->length);
573 memcpy(new_adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
574 new_adj_lsa->header->orig_router->length=adj_lsa->header->orig_router->length;
akmhoque53f64222012-09-05 13:57:51 -0500575
akmhoque03004e62012-09-06 01:12:28 -0500576 new_adj_lsa->no_link=adj_lsa->no_link;
akmhoqueffacaa82012-09-13 17:48:30 -0500577
akmhoque53f64222012-09-05 13:57:51 -0500578 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
579 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
580 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
581
akmhoque03004e62012-09-06 01:12:28 -0500582 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500583 set_new_lsdb_version();
akmhoque03004e62012-09-06 01:12:28 -0500584 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500585
akmhoqueffacaa82012-09-13 17:48:30 -0500586 add_next_hop_router(new_adj_lsa->header->orig_router->name);
akmhoque3560cb62012-09-09 10:52:30 -0500587
akmhoqueffacaa82012-09-13 17:48:30 -0500588 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
589 }
590 else if(res == HT_OLD_ENTRY)
591 {
592 new_adj_lsa = e->data;
593 if(strcmp(adj_lsa->header->orig_time,new_adj_lsa->header->orig_time)<=0)
594 {
595 printf("Older/Duplicate Adj LSA. Discarded...\n");
596 }
597 else
598 {
599 new_adj_lsa = e->data;
600
601 free(new_adj_lsa->header->orig_time);
602 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
603 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
604
605 new_adj_lsa->no_link=adj_lsa->no_link;
606
607 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
608 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
609 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
610
611 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
612 set_new_lsdb_version();
613 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
614
615 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
616
617 }
618
619 }
620 hashtb_end(e);
621
622 if ( !nlsr->is_route_calculation_scheduled )
623 {
624 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
625 nlsr->is_route_calculation_scheduled=1;
akmhoque53f64222012-09-05 13:57:51 -0500626 }
627
akmhoque03004e62012-09-06 01:12:28 -0500628
akmhoqueffacaa82012-09-13 17:48:30 -0500629 free(key);
630
631 free(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -0500632}
633
634void
635print_adj_lsa_body(const char *body, int no_link)
636{
637 int i=0;
638 char *lsa_data=(char *)malloc(strlen(body)+1);
639 memset( lsa_data,0,strlen(body)+1);
640 memcpy(lsa_data,body,strlen(body)+1);
641 char *sep="|";
642 char *rem;
643 char *rtr_id;
644 char *length;
645 char *face;
646 char *metric;
647
akmhoque53f64222012-09-05 13:57:51 -0500648 if(no_link >0 )
649 {
650 rtr_id=strtok_r(lsa_data,sep,&rem);
651 length=strtok_r(NULL,sep,&rem);
652 face=strtok_r(NULL,sep,&rem);
653 metric=strtok_r(NULL,sep,&rem);
654
655 printf(" Link %d \n",i+1);
656 printf(" Neighbor : %s \n",rtr_id);
657 printf(" Neighbor Length : %s \n",length);
658 printf(" Connecting Face : %s \n",face);
659 printf(" Metric : %s \n",metric);
akmhoque03004e62012-09-06 01:12:28 -0500660
akmhoque53f64222012-09-05 13:57:51 -0500661
662 for(i=1;i<no_link;i++)
663 {
664 rtr_id=strtok_r(NULL,sep,&rem);
665 length=strtok_r(NULL,sep,&rem);
666 face=strtok_r(NULL,sep,&rem);
667 metric=strtok_r(NULL,sep,&rem);
668 printf(" Link %d \n",i+1);
669 printf(" Neighbor : %s \n",rtr_id);
670 printf(" Neighbor Length : %s \n",length);
671 printf(" Connecting Face : %s \n",face);
672 printf(" Metric : %s \n",metric);
673
674 }
675 }
676
677 free(lsa_data);
678}
679
680void
681print_adj_lsa(struct alsa * adj_lsa)
682{
akmhoque03004e62012-09-06 01:12:28 -0500683
akmhoque53f64222012-09-05 13:57:51 -0500684 printf("print_adj_lsa called \n");
akmhoque03004e62012-09-06 01:12:28 -0500685
akmhoque53f64222012-09-05 13:57:51 -0500686 printf("-----------ADJ LSA Content---------------\n");
687 printf(" Origination Router : %s\n",adj_lsa->header->orig_router->name);
688 printf(" Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
689 printf(" LS Type : %d\n",adj_lsa->header->ls_type);
690 printf(" Origination Time : %s\n",adj_lsa->header->orig_time);
akmhoque03004e62012-09-06 01:12:28 -0500691 printf(" Lsa Data:\n");
692 printf(" No of Link : %d\n",adj_lsa->no_link);
akmhoque53f64222012-09-05 13:57:51 -0500693
694 print_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
akmhoque53f64222012-09-05 13:57:51 -0500695
akmhoque03004e62012-09-06 01:12:28 -0500696/*
697 struct link *templ=adj_lsa->links;
698 int i;
699
700 for(i=0 ; i< adj_lsa->no_link ; i++)
701 {
702 printf(" Link %d \n",i+1);
703 printf(" Neighbor : %s \n",templ->nbr->name);
704 printf(" Neighbor Length : %d \n",templ->nbr->length);
705 printf(" Connecting Face : %d \n",templ->face);
706 printf(" Metric : %d \n",templ->metric);
707
708 templ++;
709 }
710*/
akmhoque53f64222012-09-05 13:57:51 -0500711 printf("\n");
712
713}
714
715void
716print_adj_lsdb(void)
717{
718 printf("print_name_lsdb called \n");
719 int i, adj_lsdb_element;
720 struct alsa *adj_lsa;
721
722 struct hashtb_enumerator ee;
723 struct hashtb_enumerator *e = &ee;
724
725 hashtb_start(nlsr->lsdb->adj_lsdb, e);
726 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
727
728 for(i=0;i<adj_lsdb_element;i++)
729 {
730 printf("-----------Adj LSA (%d)---------------\n",i+1);
731 adj_lsa=e->data;
732 print_adj_lsa(adj_lsa);
733 hashtb_next(e);
734 }
735
736 hashtb_end(e);
737
738 printf("\n");
akmhoque53f64222012-09-05 13:57:51 -0500739}
740
741void
akmhoque03004e62012-09-06 01:12:28 -0500742build_and_install_others_adj_lsa(char *orig_router,int ls_type,char *orig_time, int no_link,char *data)
akmhoque53f64222012-09-05 13:57:51 -0500743{
akmhoque03004e62012-09-06 01:12:28 -0500744 printf("build_and_install_others_adj_lsa called \n");
745 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
746 build_others_adj_lsa(adj_lsa,orig_router,ls_type,orig_time,no_link,data);
747 //print_adj_lsa(adj_lsa);
748 install_adj_lsa(adj_lsa);
749
akmhoque53f64222012-09-05 13:57:51 -0500750
akmhoque03004e62012-09-06 01:12:28 -0500751 free(adj_lsa->header->orig_router->name);
752 free(adj_lsa->header->orig_router);
753 free(adj_lsa->header->orig_time);
754 free(adj_lsa->header);
755 free(adj_lsa->body);
756 free(adj_lsa);
akmhoque53f64222012-09-05 13:57:51 -0500757
akmhoque03004e62012-09-06 01:12:28 -0500758 print_adj_lsdb();
akmhoque53f64222012-09-05 13:57:51 -0500759
akmhoque53f64222012-09-05 13:57:51 -0500760}
761
akmhoque03004e62012-09-06 01:12:28 -0500762
akmhoque53f64222012-09-05 13:57:51 -0500763void
akmhoque03004e62012-09-06 01:12:28 -0500764build_others_adj_lsa(struct alsa *adj_lsa,char *orig_router,int ls_type,char *orig_time,int no_link,char *data)
akmhoque53f64222012-09-05 13:57:51 -0500765{
akmhoque03004e62012-09-06 01:12:28 -0500766 printf("build_others_adj_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500767
akmhoque03004e62012-09-06 01:12:28 -0500768 /*Filling Up Header Data */
769 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
770 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
771 adj_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
772 memset(adj_lsa->header->orig_router->name,0,strlen(orig_router)+1);
773 memcpy(adj_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
akmhoque53f64222012-09-05 13:57:51 -0500774
akmhoque03004e62012-09-06 01:12:28 -0500775 adj_lsa->header->orig_router->length=strlen(orig_router)+1;
akmhoque53f64222012-09-05 13:57:51 -0500776
akmhoque53f64222012-09-05 13:57:51 -0500777
akmhoque03004e62012-09-06 01:12:28 -0500778 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
akmhoque53f64222012-09-05 13:57:51 -0500779
akmhoque03004e62012-09-06 01:12:28 -0500780 adj_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
781 memset(adj_lsa->header->orig_time,0,strlen(orig_time)+1);
782 memcpy(adj_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
akmhoque53f64222012-09-05 13:57:51 -0500783
akmhoque03004e62012-09-06 01:12:28 -0500784 adj_lsa->no_link=no_link;
akmhoque53f64222012-09-05 13:57:51 -0500785
akmhoque03004e62012-09-06 01:12:28 -0500786 adj_lsa->body=(char *)malloc(strlen(data)+1);
787 memset(adj_lsa->body,0,strlen(data)+1);
788 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
akmhoque53f64222012-09-05 13:57:51 -0500789
akmhoque53f64222012-09-05 13:57:51 -0500790}
791
akmhoque03004e62012-09-06 01:12:28 -0500792
akmhoque53f64222012-09-05 13:57:51 -0500793long int
794get_name_lsdb_num_element(void)
795{
796 long int num_element;
797
798
799 struct hashtb_enumerator ee;
800 struct hashtb_enumerator *e = &ee;
801
802 hashtb_start(nlsr->lsdb->name_lsdb, e);
803 num_element=hashtb_n(nlsr->lsdb->name_lsdb);
804 hashtb_end(e);
805
806 return num_element;
807}
808
809long int
810get_adj_lsdb_num_element(void)
811{
812 long int num_element;
813
814
815 struct hashtb_enumerator ee;
816 struct hashtb_enumerator *e = &ee;
817
818 hashtb_start(nlsr->lsdb->adj_lsdb, e);
819 num_element=hashtb_n(nlsr->lsdb->adj_lsdb);
820 hashtb_end(e);
821
822 return num_element;
akmhoqued79438d2012-08-27 13:31:42 -0500823}
akmhoque03004e62012-09-06 01:12:28 -0500824
825void
826get_name_lsdb_summary(struct ccn_charbuf *name_lsdb_data)
827{
828 printf("get_name_lsdb_summary called \n");
829 int i, name_lsdb_element;
830
831 struct nlsa *name_lsa;
832 struct hashtb_enumerator ee;
833 struct hashtb_enumerator *e = &ee;
834
835 hashtb_start(nlsr->lsdb->name_lsdb, e);
836 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
837
838 for(i=0;i<name_lsdb_element;i++)
839 {
840 name_lsa=e->data;
841
842 ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_router->name);
843 ccn_charbuf_append_string(name_lsdb_data,"|");
844
845 char *lst=(char *)malloc(20);
846 memset(lst,0,20);
847 sprintf(lst,"%d",name_lsa->header->ls_type);
848 ccn_charbuf_append_string(name_lsdb_data,lst);
849 free(lst);
850 ccn_charbuf_append_string(name_lsdb_data,"|");
851
852 char *lsid=(char *)malloc(20);
853 memset(lsid,0,20);
854 sprintf(lsid,"%ld",name_lsa->header->ls_id);
855 ccn_charbuf_append_string(name_lsdb_data,lsid);
856 free(lsid);
857 ccn_charbuf_append_string(name_lsdb_data,"|");
858
859 ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_time);
860 ccn_charbuf_append_string(name_lsdb_data,"|");
861
862 hashtb_next(e);
863 }
864
865 hashtb_end(e);
866
867}
868
869
870void
871get_adj_lsdb_summary(struct ccn_charbuf *adj_lsdb_data)
872{
873 printf("get_adj_lsdb_summary called \n");
874 int i, adj_lsdb_element;
875 struct alsa *adj_lsa;
876
877 struct hashtb_enumerator ee;
878 struct hashtb_enumerator *e = &ee;
879
880 hashtb_start(nlsr->lsdb->adj_lsdb, e);
881 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
882
883 for(i=0;i<adj_lsdb_element;i++)
884 {
885 adj_lsa=e->data;
886
887 ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_router->name);
888 ccn_charbuf_append_string(adj_lsdb_data,"|");
889
890 char *lst=(char *)malloc(20);
891 memset(lst,0,20);
892 sprintf(lst,"%d",adj_lsa->header->ls_type);
893 ccn_charbuf_append_string(adj_lsdb_data,lst);
894 free(lst);
895 ccn_charbuf_append_string(adj_lsdb_data,"|");
896
897 ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_time);
898 ccn_charbuf_append_string(adj_lsdb_data,"|");
899
900 hashtb_next(e);
901 }
902
903 hashtb_end(e);
904}
905
906
907void
908get_lsdb_summary(struct ccn_charbuf *lsdb_data)
909{
910 struct ccn_charbuf *name_lsdb_data=ccn_charbuf_create();
911 struct ccn_charbuf *adj_lsdb_data=ccn_charbuf_create();
912
913 get_name_lsdb_summary(name_lsdb_data);
914 get_adj_lsdb_summary(adj_lsdb_data);
915
916 long int num_lsa=get_name_lsdb_num_element() + get_adj_lsdb_num_element();
917 char *num_element=(char *)malloc(15);
918 memset(num_element,0,15);
919 sprintf(num_element,"%ld",num_lsa);
920
921 if( num_lsa > 0)
922 {
923 ccn_charbuf_append_string(lsdb_data,num_element);
924 ccn_charbuf_append_string(lsdb_data,"|");
925 }
926 if(name_lsdb_data->length>0)
927 {
928 char *data1=ccn_charbuf_as_string(name_lsdb_data);
929 ccn_charbuf_append_string(lsdb_data,(char *)data1);
930 }
931 if(adj_lsdb_data->length>0)
932 {
933 char *data2=ccn_charbuf_as_string(adj_lsdb_data);
934 ccn_charbuf_append_string(lsdb_data,(char *)data2);
935 }
936 ccn_charbuf_destroy(&name_lsdb_data);
937 ccn_charbuf_destroy(&adj_lsdb_data);
938 free(num_element);
939
940}
941
942int
943check_is_new_name_lsa(char *orig_router,char *lst,char *lsid,char *orig_time)
944{
945 int ret=0;
946 struct ccn_charbuf *key=ccn_charbuf_create();
947 ccn_charbuf_append_string(key,orig_router);
948 ccn_charbuf_append_string(key,"/");
949 ccn_charbuf_append_string(key,lst);
950 ccn_charbuf_append_string(key,"/");
951 ccn_charbuf_append_string(key,lsid);
952
953 int res;
954 struct nlsa *name_lsa;
955
956 struct hashtb_enumerator ee;
957 struct hashtb_enumerator *e = &ee;
958
959 hashtb_start(nlsr->lsdb->name_lsdb, e);
960 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
961
962 if( res == HT_NEW_ENTRY )
963 {
964 hashtb_delete(e);
965 ret=1;
966
967 }
968 else if(res == HT_OLD_ENTRY)
969 {
970 name_lsa=e->data;
971 if( strcmp ( orig_time , name_lsa->header->orig_time ) > 0 )
972 {
973 ret=1;
974 }
975 }
976
977 hashtb_end(e);
978
979 ccn_charbuf_destroy(&key);
980
981 return ret;
982}
983
984int
985check_is_new_adj_lsa(char *orig_router,char *lst,char *orig_time)
986{
987 int ret=0;
988 struct ccn_charbuf *key=ccn_charbuf_create();
989 ccn_charbuf_append_string(key,orig_router);
990 ccn_charbuf_append_string(key,"/");
991 ccn_charbuf_append_string(key,lst);
992
993 int res;
994 struct alsa *adj_lsa;
995
996 struct hashtb_enumerator ee;
997 struct hashtb_enumerator *e = &ee;
998
999 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1000 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1001
1002 if( res == HT_NEW_ENTRY )
1003 {
1004 hashtb_delete(e);
1005 ret=1;
1006
1007 }
1008 else if(res == HT_OLD_ENTRY)
1009 {
1010 adj_lsa=e->data;
1011 if( strcmp ( orig_time , adj_lsa->header->orig_time ) > 0 )
1012 {
1013 ret=1;
1014 }
1015 }
1016
1017 hashtb_end(e);
1018
1019 ccn_charbuf_destroy(&key);
1020
1021 return ret;
1022}
1023
1024void
1025get_name_lsa_data(struct ccn_charbuf *lsa_data, struct name_prefix *lsaId)
1026{
1027 printf("get_name_lsa_data called \n");
1028
1029 struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa ));
1030
1031 struct hashtb_enumerator ee;
1032 struct hashtb_enumerator *e = &ee;
1033 int res;
1034
1035 hashtb_start(nlsr->lsdb->name_lsdb, e);
1036 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1037
1038 if( res == HT_OLD_ENTRY )
1039 {
1040 name_lsa=e->data;
1041 printf("NAME LSA found\n");
1042
1043 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_router->name);
1044 ccn_charbuf_append_string(lsa_data,"|");
1045
1046 char *temp_length=(char *)malloc(20);
1047 memset(temp_length,0,20);
1048 sprintf(temp_length,"%d",name_lsa->header->orig_router->length);
1049 ccn_charbuf_append_string(lsa_data,temp_length);
1050 free(temp_length);
1051 ccn_charbuf_append_string(lsa_data,"|");
1052
1053 char *temp_ltype=(char *)malloc(20);
1054 memset(temp_ltype,0,20);
1055 sprintf(temp_ltype,"%d",name_lsa->header->ls_type);
1056 ccn_charbuf_append_string(lsa_data,temp_ltype);
1057 free(temp_ltype);
1058 ccn_charbuf_append_string(lsa_data,"|");
1059
1060 char *temp_lsid=(char *)malloc(20);
1061 memset(temp_lsid,0,20);
1062 sprintf(temp_lsid,"%ld",name_lsa->header->ls_id);
1063 ccn_charbuf_append_string(lsa_data,temp_lsid);
1064 free(temp_lsid);
1065 ccn_charbuf_append_string(lsa_data,"|");
1066
1067 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_time);
1068 ccn_charbuf_append_string(lsa_data,"|");
1069
1070 char *temp_valid=(char *)malloc(20);
1071 memset(temp_valid,0,20);
1072 sprintf(temp_valid,"%d",name_lsa->header->isValid);
1073 ccn_charbuf_append_string(lsa_data,temp_valid);
1074 free(temp_valid);
1075 ccn_charbuf_append_string(lsa_data,"|");
1076
1077 ccn_charbuf_append_string(lsa_data,name_lsa->name_prefix->name);
1078 ccn_charbuf_append_string(lsa_data,"|");
1079
1080 char *temp_npl=(char *)malloc(20);
1081 memset(temp_npl,0,20);
1082 sprintf(temp_npl,"%d",name_lsa->name_prefix->length);
1083 ccn_charbuf_append_string(lsa_data,temp_npl);
1084 free(temp_npl);
1085 ccn_charbuf_append_string(lsa_data,"|");
1086
1087 }
1088 else if(res == HT_NEW_ENTRY)
1089 {
1090 hashtb_delete(e);
1091 }
1092
1093 hashtb_end(e);
1094}
1095
1096void
1097get_adj_lsa_data(struct ccn_charbuf *lsa_data,struct name_prefix *lsaId)
1098{
1099 printf("get_adj_lsa_data called \n");
1100
1101 struct alsa *adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
1102
1103 struct hashtb_enumerator ee;
1104 struct hashtb_enumerator *e = &ee;
1105 int res;
1106
1107 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1108 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1109
1110 if( res == HT_OLD_ENTRY )
1111 {
1112 adj_lsa=e->data;
1113 printf("NAME LSA found\n");
1114
1115 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_router->name);
1116 ccn_charbuf_append_string(lsa_data,"|");
1117
1118 char *temp_length=(char *)malloc(20);
1119 memset(temp_length,0,20);
1120 sprintf(temp_length,"%d",adj_lsa->header->orig_router->length);
1121 ccn_charbuf_append_string(lsa_data,temp_length);
1122 free(temp_length);
1123 ccn_charbuf_append_string(lsa_data,"|");
1124
1125 char *temp_ltype=(char *)malloc(20);
1126 memset(temp_ltype,0,20);
1127 sprintf(temp_ltype,"%d",adj_lsa->header->ls_type);
1128 ccn_charbuf_append_string(lsa_data,temp_ltype);
1129 free(temp_ltype);
1130 ccn_charbuf_append_string(lsa_data,"|");
1131
1132 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_time);
1133 ccn_charbuf_append_string(lsa_data,"|");
1134
1135 char *temp_nl=(char *)malloc(20);
1136 memset(temp_nl,0,20);
1137 sprintf(temp_nl,"%d",adj_lsa->no_link);
1138 ccn_charbuf_append_string(lsa_data,temp_nl);
1139 free(temp_nl);
1140 ccn_charbuf_append_string(lsa_data,"|");
1141
1142 ccn_charbuf_append_string(lsa_data,adj_lsa->body);
1143
1144
1145 }
1146 else if(res == HT_NEW_ENTRY)
1147 {
1148 hashtb_delete(e);
1149 }
1150
1151 hashtb_end(e);
1152}
akmhoqueffacaa82012-09-13 17:48:30 -05001153
akmhoqueda5b6832012-09-13 22:33:55 -05001154int
1155delete_name_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1156{
1157 printf("delete_name_lsa called \n");
1158
1159 if(flags == CCN_SCHEDULE_CANCEL)
1160 {
1161 return -1;
1162 }
1163
1164
1165
1166 nlsr_lock();
1167
1168 printf("LSA Key: %s \n",(char *)ev->evdata);
1169
1170 struct nlsa *nlsa;
1171
1172 struct hashtb_enumerator ee;
1173 struct hashtb_enumerator *e = &ee;
1174
1175 int res;
1176
1177 hashtb_start(nlsr->lsdb->name_lsdb, e);
1178 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1179
1180 if( res == HT_OLD_ENTRY )
1181 {
1182 nlsa=e->data;
1183 delete_npt_entry(nlsa->header->orig_router->name, nlsa->name_prefix->name);
1184 hashtb_delete(e);
1185 }
1186 else if( res == HT_OLD_ENTRY )
1187 {
1188 nlsa=e->data;
1189 delete_npt_entry(nlsa->header->orig_router->name, nlsa->name_prefix->name);
1190 hashtb_delete(e);
1191 }
1192 hashtb_end(e);
1193
1194
1195 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1196 set_new_lsdb_version();
1197 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1198
1199 //print_name_lsdb();
1200
1201 nlsr_unlock();
1202
1203 return 0;
1204}
1205
1206int
1207delete_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1208{
1209 printf("delete_adj_lsa called \n");
1210
1211 if(flags == CCN_SCHEDULE_CANCEL)
1212 {
1213 return -1;
1214 }
1215 nlsr_lock();
1216
1217 printf("LSA Key: %s \n",(char *)ev->evdata);
1218
1219 struct hashtb_enumerator ee;
1220 struct hashtb_enumerator *e = &ee;
1221 int res;
1222
1223 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1224 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1225
1226 if( res == HT_OLD_ENTRY )
1227 {
1228 hashtb_delete(e);
1229 }
1230 else if( res == HT_OLD_ENTRY )
1231 {
1232 hashtb_delete(e);
1233 }
1234 hashtb_end(e);
1235
1236 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1237 set_new_lsdb_version();
1238 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1239
1240 if ( !nlsr->is_route_calculation_scheduled)
1241 {
1242 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
1243 nlsr->is_route_calculation_scheduled=1;
1244 }
1245
1246 //print_adj_lsdb();
1247
1248 nlsr_unlock();
1249
1250 return 0;
1251}
1252
akmhoqueffacaa82012-09-13 17:48:30 -05001253void
1254refresh_name_lsdb(void)
1255{
1256 printf("refresh_name_lsdb called \n");
1257
akmhoqueda5b6832012-09-13 22:33:55 -05001258 //int lsa_change_count=0;
1259
akmhoqueffacaa82012-09-13 17:48:30 -05001260 char *time_stamp=(char *)malloc(20);
1261 memset(time_stamp,0,20);
1262 get_current_timestamp_micro(time_stamp);
1263
1264 long int lsa_life_time;
1265
1266 int i, name_lsdb_element;
1267 struct nlsa *name_lsa;
1268
1269 struct hashtb_enumerator ee;
1270 struct hashtb_enumerator *e = &ee;
1271
1272 hashtb_start(nlsr->lsdb->name_lsdb, e);
1273 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
1274
1275 for(i=0;i<name_lsdb_element;i++)
1276 {
1277 name_lsa=e->data;
1278
1279 lsa_life_time=get_time_diff(time_stamp,name_lsa->header->orig_time);
akmhoqueda5b6832012-09-13 22:33:55 -05001280 printf("LSA Life Time: %ld \n",lsa_life_time);
1281
1282 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name) == 0)
1283 {
1284 if ( lsa_life_time > nlsr->lsa_refresh_time )
1285 {
1286 printf("Own Name LSA need to be refrshed\n");
1287 char *current_time_stamp=(char *)malloc(20);
1288 memset(current_time_stamp,0,20);
1289 get_current_timestamp_micro(current_time_stamp);
1290
1291 free(name_lsa->header->orig_time);
1292 name_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1293 memset(name_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1294 memcpy(name_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1295
1296 free(current_time_stamp);
1297
1298 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1299 set_new_lsdb_version();
1300 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1301
1302 print_name_lsdb();
1303 //lsa_change_count++;
1304 }
1305 }
1306 else
1307 {
1308 if ( lsa_life_time > nlsr->router_dead_interval )
1309 {
1310 printf("Others Name LSA need to be deleted\n");
1311
1312 char lst[2];
1313 memset(lst,0,2);
1314 sprintf(lst,"%d",name_lsa->header->ls_type);
1315
1316 char lsid[10];
1317 memset(lsid,0,10);
1318 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1319
1320
1321 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1322 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1323
1324
1325 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
1326 printf("Key:%s Length:%d\n",key,(int)strlen(key));
1327
1328 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
1329 //lsa_change_count++;
1330 }
1331 }
akmhoqueffacaa82012-09-13 17:48:30 -05001332
1333 hashtb_next(e);
1334 }
1335
1336 hashtb_end(e);
1337
1338 free(time_stamp);
akmhoqueda5b6832012-09-13 22:33:55 -05001339
akmhoqueffacaa82012-09-13 17:48:30 -05001340
1341}
1342
akmhoqueda5b6832012-09-13 22:33:55 -05001343void
akmhoqueffacaa82012-09-13 17:48:30 -05001344refresh_adj_lsdb(void)
1345{
1346 printf("refresh_adj_lsdb called \n");
1347
akmhoqueda5b6832012-09-13 22:33:55 -05001348
akmhoqueffacaa82012-09-13 17:48:30 -05001349 char *time_stamp=(char *)malloc(20);
1350 memset(time_stamp,0,20);
1351 get_current_timestamp_micro(time_stamp);
1352
1353 long int lsa_life_time;
1354
1355 int i, adj_lsdb_element;
1356 struct alsa *adj_lsa;
1357
1358 struct hashtb_enumerator ee;
1359 struct hashtb_enumerator *e = &ee;
1360
1361 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1362 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1363
1364 for(i=0;i<adj_lsdb_element;i++)
1365 {
1366 adj_lsa=e->data;
1367
1368 lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
1369 printf("LSA Life Time: %ld \n",lsa_life_time);
1370
akmhoqueda5b6832012-09-13 22:33:55 -05001371 if ( strcmp(adj_lsa->header->orig_router->name,nlsr->router_name) == 0)
1372 {
1373 if ( lsa_life_time > nlsr->lsa_refresh_time )
1374 {
1375 printf("Own Adj LSA need to be refrshed\n");
1376
1377 char *current_time_stamp=(char *)malloc(20);
1378 memset(current_time_stamp,0,20);
1379 get_current_timestamp_micro(current_time_stamp);
1380
1381 free(adj_lsa->header->orig_time);
1382 adj_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1383 memset(adj_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1384 memcpy(adj_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1385
1386 free(current_time_stamp);
1387
1388
1389 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1390 set_new_lsdb_version();
1391 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1392
1393 print_adj_lsdb();
1394 }
1395 }
1396 else
1397 {
1398 if ( lsa_life_time > nlsr->router_dead_interval )
1399 {
1400 printf("Others Adj LSA need to be deleted\n");
1401
1402 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
1403 memset(key,0,adj_lsa->header->orig_router->length+2);
1404 make_adj_lsa_key(key,adj_lsa);
1405 printf("Adjacent LSA key: %s \n",key);
1406
1407 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_adj_lsa, (void *)key, 0);
1408 }
1409 }
1410
1411
1412
akmhoqueffacaa82012-09-13 17:48:30 -05001413 hashtb_next(e);
1414 }
1415
1416 hashtb_end(e);
1417
1418 free(time_stamp);
akmhoqueffacaa82012-09-13 17:48:30 -05001419}
1420
1421int
1422refresh_lsdb(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1423{
1424 if(flags == CCN_SCHEDULE_CANCEL)
1425 {
1426 return -1;
1427 }
1428
1429 nlsr_lock();
akmhoqueda5b6832012-09-13 22:33:55 -05001430
akmhoqueffacaa82012-09-13 17:48:30 -05001431 printf("refresh_lsdb called \n");
1432
1433 refresh_name_lsdb();
1434 refresh_adj_lsdb();
1435
akmhoqueffacaa82012-09-13 17:48:30 -05001436 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoqueda5b6832012-09-13 22:33:55 -05001437
akmhoqueffacaa82012-09-13 17:48:30 -05001438 nlsr_unlock();
1439 return 0;
1440}