blob: 49b033f401326b19facafd93fca69d93e066cb1b [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 printf("New Name LSA... Adding to LSDB\n");
178 new_name_lsa = e->data;
179
180 new_name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header )); //free
181 new_name_lsa->header->ls_type=name_lsa->header->ls_type;
182
183 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
184 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
185 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
186
187 new_name_lsa->header->ls_id=name_lsa->header->ls_id;
188 new_name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free
189 new_name_lsa->header->orig_router->name=(char *)malloc(name_lsa->header->orig_router->length);
190 memcpy(new_name_lsa->header->orig_router->name,name_lsa->header->orig_router->name,name_lsa->header->orig_router->length);
191 new_name_lsa->header->orig_router->length=name_lsa->header->orig_router->length;
192 new_name_lsa->header->isValid=name_lsa->header->isValid;
193
194
195 new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free
196 new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
197 memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
198 new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
199
200 printf("New Name LSA Added....\n");
201
202 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
203 set_new_lsdb_version();
204 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
205
206 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name);
207 if ( next_hop == NO_NEXT_HOP )
akmhoque3560cb62012-09-09 10:52:30 -0500208 {
akmhoqueffacaa82012-09-13 17:48:30 -0500209 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,NO_FACE);
210 if ( check == HT_NEW_ENTRY )
211 {
212 printf("Added in npt \n");
213 }
akmhoque3560cb62012-09-09 10:52:30 -0500214 }
akmhoqueffacaa82012-09-13 17:48:30 -0500215 else
akmhoque3560cb62012-09-09 10:52:30 -0500216 {
akmhoqueffacaa82012-09-13 17:48:30 -0500217 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,next_hop);
218 if ( check == HT_NEW_ENTRY )
219 {
220 printf("Added in npt \n");
221 }
222
akmhoque3560cb62012-09-09 10:52:30 -0500223 }
224
akmhoqueffacaa82012-09-13 17:48:30 -0500225
226 free(time_stamp);
227
228 }
229 else if(res == HT_OLD_ENTRY)
230 {
231 new_name_lsa=e->data;
akmhoque14b3f342012-09-14 10:39:02 -0500232 if(strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time)<0)
akmhoqueffacaa82012-09-13 17:48:30 -0500233 {
akmhoque14b3f342012-09-14 10:39:02 -0500234 printf("Older Adj LSA. Discarded...\n");
235 }
236 else if( strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time) == 0 )
237 {
238 printf("Duplicate Adj LSA. Discarded...\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500239 }
240 else
241 {
242 if ( name_lsa->header->isValid == 0 )
243 {
244 // have to call to delete npt table entry
245 delete_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name);
246
247 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name)!= 0)
248 {
249 hashtb_delete(e);
250 printf("isValid bit not set for Router %s so LSA Deleted from LSDB\n",name_lsa->header->orig_router->name);
251 }
252 else
253 {
254 new_name_lsa->header->isValid=name_lsa->header->isValid;
255 free(new_name_lsa->header->orig_time);
256 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
257 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
258 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
259 }
260 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
261 set_new_lsdb_version();
262 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
263 }
264 else
265 {
266 int is_npt_update=0;
267 if ( strcmp(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name) != 0 )
268 {
269 is_npt_update=1;
270 delete_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name);
271 }
272
273 // copying LSA content with header
274
275 free(new_name_lsa->header->orig_time);
276 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
277 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
278 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
279
280 new_name_lsa->header->isValid=name_lsa->header->isValid;
281
282 free(new_name_lsa->name_prefix->name);
283 free(new_name_lsa->name_prefix);
284 new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
285 new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
286 memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
287 new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
288
289 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
290 set_new_lsdb_version();
291 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
292
293 if( is_npt_update == 1 )
294 {
295 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name);
296 if ( next_hop == NO_NEXT_HOP )
297 {
298 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,NO_FACE);
299 if ( check == HT_NEW_ENTRY )
300 {
301 printf("Added in npt \n");
302 }
303 }
304 else
305 {
306 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,next_hop);
307 if ( check == HT_NEW_ENTRY )
308 {
309 printf("Added in npt \n");
310 }
311 }
312 }
313 }
314 }
315
akmhoque3560cb62012-09-09 10:52:30 -0500316 }
akmhoque53f64222012-09-05 13:57:51 -0500317
akmhoqueffacaa82012-09-13 17:48:30 -0500318 hashtb_end(e);
akmhoque53f64222012-09-05 13:57:51 -0500319
akmhoqueffacaa82012-09-13 17:48:30 -0500320 free(key);
akmhoque53f64222012-09-05 13:57:51 -0500321}
322
323
akmhoque03004e62012-09-06 01:12:28 -0500324void
325print_name_lsa(struct nlsa *name_lsa)
akmhoque53f64222012-09-05 13:57:51 -0500326{
327
akmhoque03004e62012-09-06 01:12:28 -0500328 printf("-----------Name LSA Content---------------\n");
329 printf(" Origination Router : %s\n",name_lsa->header->orig_router->name);
330 printf(" Origination Router Length: %d\n",name_lsa->header->orig_router->length);
331 printf(" LS Type : %d\n",name_lsa->header->ls_type);
332 printf(" LS Id : %ld\n",name_lsa->header->ls_id);
333 printf(" Origination Time : %s\n",name_lsa->header->orig_time);
334 printf(" Is Valid : %d\n",name_lsa->header->isValid);
335 printf(" LSA Data \n");
336 printf(" Name Prefix: : %s\n",name_lsa->name_prefix->name);
337 printf(" Name Prefix Length : %d\n",name_lsa->name_prefix->length);
akmhoque53f64222012-09-05 13:57:51 -0500338
akmhoque03004e62012-09-06 01:12:28 -0500339 printf("\n");
akmhoquef71d9082012-08-22 12:51:53 -0400340}
341
342void
343print_name_lsdb(void)
344{
345 printf("print_name_lsdb called \n");
akmhoque53f64222012-09-05 13:57:51 -0500346 int i, name_lsdb_element;
akmhoquef71d9082012-08-22 12:51:53 -0400347 struct nlsa *name_lsa;
348
349 struct hashtb_enumerator ee;
350 struct hashtb_enumerator *e = &ee;
351
352 hashtb_start(nlsr->lsdb->name_lsdb, e);
akmhoque53f64222012-09-05 13:57:51 -0500353 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
akmhoquef71d9082012-08-22 12:51:53 -0400354
akmhoque53f64222012-09-05 13:57:51 -0500355 for(i=0;i<name_lsdb_element;i++)
akmhoquef71d9082012-08-22 12:51:53 -0400356 {
akmhoque53f64222012-09-05 13:57:51 -0500357 printf("-----------Name LSA (%d)---------------\n",i+1);
akmhoquef71d9082012-08-22 12:51:53 -0400358 name_lsa=e->data;
akmhoque53f64222012-09-05 13:57:51 -0500359 print_name_lsa(name_lsa);
akmhoquef71d9082012-08-22 12:51:53 -0400360 hashtb_next(e);
361 }
362
363 hashtb_end(e);
364
akmhoque53f64222012-09-05 13:57:51 -0500365 printf("\n");
366}
367
akmhoque03004e62012-09-06 01:12:28 -0500368
369void
370build_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 -0500371{
akmhoque03004e62012-09-06 01:12:28 -0500372 printf("build_and_install_others_name_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500373
akmhoque03004e62012-09-06 01:12:28 -0500374 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
375 build_others_name_lsa(name_lsa,orig_router,ls_type,ls_id,orig_time, isValid,np);
376 print_name_lsa(name_lsa);
377 install_name_lsa(name_lsa);
378 print_name_lsdb();
akmhoque53f64222012-09-05 13:57:51 -0500379
akmhoque53f64222012-09-05 13:57:51 -0500380
akmhoque03004e62012-09-06 01:12:28 -0500381 free(name_lsa->header->orig_router->name);
382 free(name_lsa->header->orig_router);
383 free(name_lsa->header);
384 free(name_lsa->name_prefix->name);
385 free(name_lsa->name_prefix);
386 free(name_lsa);
akmhoque53f64222012-09-05 13:57:51 -0500387
388}
389
390void
akmhoque03004e62012-09-06 01:12:28 -0500391build_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 -0500392{
akmhoque03004e62012-09-06 01:12:28 -0500393 printf("build_others_name_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500394
akmhoque03004e62012-09-06 01:12:28 -0500395 name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header ));
396 name_lsa->header->ls_type=LS_TYPE_NAME;
akmhoque53f64222012-09-05 13:57:51 -0500397
akmhoque03004e62012-09-06 01:12:28 -0500398 name_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
399 memset(name_lsa->header->orig_time,0,strlen(orig_time)+1);
400 memcpy(name_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
akmhoque53f64222012-09-05 13:57:51 -0500401
akmhoque03004e62012-09-06 01:12:28 -0500402 name_lsa->header->ls_id=ls_id;
403 name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
404 name_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
405 memset(name_lsa->header->orig_router->name,0,strlen(orig_router)+1);
406 memcpy(name_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
407 name_lsa->header->orig_router->length=strlen(orig_router)+1;
408 name_lsa->header->isValid=isValid;
akmhoque53f64222012-09-05 13:57:51 -0500409
akmhoque03004e62012-09-06 01:12:28 -0500410 name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
411 name_lsa->name_prefix->name=(char *)malloc(strlen(np)+1);
412 memset(name_lsa->name_prefix->name,0,strlen(np)+1);
413 memcpy(name_lsa->name_prefix->name,np,strlen(np)+1);
414 name_lsa->name_prefix->length=strlen(np)+1;
akmhoquef71d9082012-08-22 12:51:53 -0400415}
akmhoqued79438d2012-08-27 13:31:42 -0500416
417
akmhoqueffacaa82012-09-13 17:48:30 -0500418
419
akmhoque53f64222012-09-05 13:57:51 -0500420void
421make_adj_lsa_key(char *key,struct alsa *adj_lsa)
akmhoqued79438d2012-08-27 13:31:42 -0500422{
akmhoque53f64222012-09-05 13:57:51 -0500423 memcpy(key+strlen(key),adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
424 memcpy(key+strlen(key),"/",1);
425 char ls_type[2];
426 sprintf(ls_type,"%d",adj_lsa->header->ls_type);
427 memcpy(key+strlen(key),ls_type,strlen(ls_type));
428 key[strlen(key)]='\0';
429}
430
akmhoque03004e62012-09-06 01:12:28 -0500431int
432build_and_install_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
433{
akmhoqueffacaa82012-09-13 17:48:30 -0500434 if(flags == CCN_SCHEDULE_CANCEL)
435 {
436 return -1;
437 }
438
439 nlsr_lock();
440
akmhoque03004e62012-09-06 01:12:28 -0500441 printf("build_and_install_adj_lsa called \n");
442
443 printf("adj_build_flag = %d \n",nlsr->adj_build_flag);
444
445 if(nlsr->adj_build_flag > 0)
446 {
447 printf("is_adj_lsa_build = %d \n",is_adj_lsa_build());
448 if ( is_adj_lsa_build()> 0)
449 {
450 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
451 build_adj_lsa(adj_lsa);
452 install_adj_lsa(adj_lsa);
453
454 free(adj_lsa->header->orig_router->name);
455 free(adj_lsa->header->orig_router);
456 free(adj_lsa->header->orig_time);
457 free(adj_lsa->header);
458 free(adj_lsa->body);
459 free(adj_lsa);
460 nlsr->adj_build_flag=0;
461 print_adj_lsdb();
462 }
463 else
464 {
465 printf("Can not build adj LSA now\n");
466 }
467 }
468 nlsr->is_build_adj_lsa_sheduled=0;
akmhoqueffacaa82012-09-13 17:48:30 -0500469
470 nlsr_unlock();
471
akmhoque03004e62012-09-06 01:12:28 -0500472 return 0;
473}
474
475
476void
477build_adj_lsa(struct alsa * adj_lsa)
478{
479 printf("build_adj_lsa called \n");
480
481 int no_link=no_active_nbr();
482 printf("Number of link in Adjacent LSA: %d\n",no_link);
483
484 /*Filling Up Header Data */
485 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
486 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
487 adj_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1);
488 memset(adj_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
489 memcpy(adj_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
490 adj_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
491
492 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
493
494 char *time_stamp=(char *)malloc(20);
495 memset(time_stamp,0,20);
496 get_current_timestamp_micro(time_stamp);
497
498 adj_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1);
499 memset(adj_lsa->header->orig_time,0,strlen(time_stamp)+1);
500 memcpy(adj_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
501 free(time_stamp);
502
503
504 /* Filling Up Body Data */
505
506 adj_lsa->no_link=no_link;
akmhoque03004e62012-09-06 01:12:28 -0500507
508
509 struct ccn_charbuf *c=ccn_charbuf_create();
510 get_active_nbr_adj_data(c);
511 char *data=ccn_charbuf_as_string(c);
512
513 adj_lsa->body=(char *)malloc(strlen(data)+1);
514 memset(adj_lsa->body,0,strlen(data)+1);
515 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
516 ccn_charbuf_destroy(&c);
517
518
519
520 if( !nlsr->is_send_lsdb_interest_scheduled )
521 {
522 nlsr->event_send_lsdb_interest= ccn_schedule_event(nlsr->sched, 1000, &send_lsdb_interest, NULL, 0);
523 nlsr->is_send_lsdb_interest_scheduled=1;
524 }
525
526 nlsr->adj_build_count++;
527
528
529}
530
531
akmhoque53f64222012-09-05 13:57:51 -0500532void
533install_adj_lsa(struct alsa * adj_lsa)
534{
akmhoqued79438d2012-08-27 13:31:42 -0500535 printf("install_adj_lsa called \n");
536
akmhoque03004e62012-09-06 01:12:28 -0500537
akmhoqueffacaa82012-09-13 17:48:30 -0500538 char *time_stamp=(char *)malloc(20);
539 memset(time_stamp,0,20);
540 get_current_timestamp_micro(time_stamp);
541 long int lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
akmhoque53f64222012-09-05 13:57:51 -0500542
akmhoqueffacaa82012-09-13 17:48:30 -0500543 printf("time difference: %ld \n",lsa_life_time);
akmhoque53f64222012-09-05 13:57:51 -0500544
akmhoqueffacaa82012-09-13 17:48:30 -0500545
546 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
547 memset(key,0,adj_lsa->header->orig_router->length+2);
548 make_adj_lsa_key(key,adj_lsa);
549 printf("Adjacent LSA key: %s \n",key);
550
551 struct alsa *new_adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
552
553 struct hashtb_enumerator ee;
554 struct hashtb_enumerator *e = &ee;
555 int res;
556
557 hashtb_start(nlsr->lsdb->adj_lsdb, e);
558 res = hashtb_seek(e, key, strlen(key), 0);
akmhoque53f64222012-09-05 13:57:51 -0500559
akmhoque03004e62012-09-06 01:12:28 -0500560
akmhoque53f64222012-09-05 13:57:51 -0500561
akmhoqueffacaa82012-09-13 17:48:30 -0500562 if(res == HT_NEW_ENTRY )
akmhoque53f64222012-09-05 13:57:51 -0500563 {
akmhoqueffacaa82012-09-13 17:48:30 -0500564 printf("New ADJ LSA... Adding to LSDB\n");
akmhoque53f64222012-09-05 13:57:51 -0500565 new_adj_lsa = e->data;
566
akmhoqueffacaa82012-09-13 17:48:30 -0500567 new_adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
568 new_adj_lsa->header->ls_type=adj_lsa->header->ls_type;
akmhoque53f64222012-09-05 13:57:51 -0500569 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
akmhoqueffacaa82012-09-13 17:48:30 -0500570 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
571
572 new_adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
573 new_adj_lsa->header->orig_router->name=(char *)malloc(adj_lsa->header->orig_router->length);
574 memcpy(new_adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
575 new_adj_lsa->header->orig_router->length=adj_lsa->header->orig_router->length;
akmhoque53f64222012-09-05 13:57:51 -0500576
akmhoque03004e62012-09-06 01:12:28 -0500577 new_adj_lsa->no_link=adj_lsa->no_link;
akmhoqueffacaa82012-09-13 17:48:30 -0500578
akmhoque53f64222012-09-05 13:57:51 -0500579 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
580 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
581 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
582
akmhoque03004e62012-09-06 01:12:28 -0500583 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500584 set_new_lsdb_version();
akmhoque03004e62012-09-06 01:12:28 -0500585 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500586
akmhoqueffacaa82012-09-13 17:48:30 -0500587 add_next_hop_router(new_adj_lsa->header->orig_router->name);
akmhoque3560cb62012-09-09 10:52:30 -0500588
akmhoqueffacaa82012-09-13 17:48:30 -0500589 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
590 }
591 else if(res == HT_OLD_ENTRY)
592 {
593 new_adj_lsa = e->data;
594 if(strcmp(adj_lsa->header->orig_time,new_adj_lsa->header->orig_time)<=0)
595 {
596 printf("Older/Duplicate Adj LSA. Discarded...\n");
597 }
598 else
599 {
600 new_adj_lsa = e->data;
601
602 free(new_adj_lsa->header->orig_time);
603 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
604 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
605
606 new_adj_lsa->no_link=adj_lsa->no_link;
607
608 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
609 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
610 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
611
612 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
613 set_new_lsdb_version();
614 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
615
616 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
617
618 }
619
620 }
621 hashtb_end(e);
622
623 if ( !nlsr->is_route_calculation_scheduled )
624 {
625 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
626 nlsr->is_route_calculation_scheduled=1;
akmhoque53f64222012-09-05 13:57:51 -0500627 }
628
akmhoque03004e62012-09-06 01:12:28 -0500629
akmhoqueffacaa82012-09-13 17:48:30 -0500630 free(key);
631
632 free(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -0500633}
634
635void
636print_adj_lsa_body(const char *body, int no_link)
637{
638 int i=0;
639 char *lsa_data=(char *)malloc(strlen(body)+1);
640 memset( lsa_data,0,strlen(body)+1);
641 memcpy(lsa_data,body,strlen(body)+1);
642 char *sep="|";
643 char *rem;
644 char *rtr_id;
645 char *length;
646 char *face;
647 char *metric;
648
akmhoque53f64222012-09-05 13:57:51 -0500649 if(no_link >0 )
650 {
651 rtr_id=strtok_r(lsa_data,sep,&rem);
652 length=strtok_r(NULL,sep,&rem);
653 face=strtok_r(NULL,sep,&rem);
654 metric=strtok_r(NULL,sep,&rem);
655
656 printf(" Link %d \n",i+1);
657 printf(" Neighbor : %s \n",rtr_id);
658 printf(" Neighbor Length : %s \n",length);
659 printf(" Connecting Face : %s \n",face);
660 printf(" Metric : %s \n",metric);
akmhoque03004e62012-09-06 01:12:28 -0500661
akmhoque53f64222012-09-05 13:57:51 -0500662
663 for(i=1;i<no_link;i++)
664 {
665 rtr_id=strtok_r(NULL,sep,&rem);
666 length=strtok_r(NULL,sep,&rem);
667 face=strtok_r(NULL,sep,&rem);
668 metric=strtok_r(NULL,sep,&rem);
669 printf(" Link %d \n",i+1);
670 printf(" Neighbor : %s \n",rtr_id);
671 printf(" Neighbor Length : %s \n",length);
672 printf(" Connecting Face : %s \n",face);
673 printf(" Metric : %s \n",metric);
674
675 }
676 }
677
678 free(lsa_data);
679}
680
681void
682print_adj_lsa(struct alsa * adj_lsa)
683{
akmhoque03004e62012-09-06 01:12:28 -0500684
akmhoque53f64222012-09-05 13:57:51 -0500685 printf("print_adj_lsa called \n");
akmhoque03004e62012-09-06 01:12:28 -0500686
akmhoque53f64222012-09-05 13:57:51 -0500687 printf("-----------ADJ LSA Content---------------\n");
688 printf(" Origination Router : %s\n",adj_lsa->header->orig_router->name);
689 printf(" Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
690 printf(" LS Type : %d\n",adj_lsa->header->ls_type);
691 printf(" Origination Time : %s\n",adj_lsa->header->orig_time);
akmhoque03004e62012-09-06 01:12:28 -0500692 printf(" Lsa Data:\n");
693 printf(" No of Link : %d\n",adj_lsa->no_link);
akmhoque53f64222012-09-05 13:57:51 -0500694
695 print_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
akmhoque53f64222012-09-05 13:57:51 -0500696
akmhoque03004e62012-09-06 01:12:28 -0500697/*
698 struct link *templ=adj_lsa->links;
699 int i;
700
701 for(i=0 ; i< adj_lsa->no_link ; i++)
702 {
703 printf(" Link %d \n",i+1);
704 printf(" Neighbor : %s \n",templ->nbr->name);
705 printf(" Neighbor Length : %d \n",templ->nbr->length);
706 printf(" Connecting Face : %d \n",templ->face);
707 printf(" Metric : %d \n",templ->metric);
708
709 templ++;
710 }
711*/
akmhoque53f64222012-09-05 13:57:51 -0500712 printf("\n");
713
714}
715
716void
717print_adj_lsdb(void)
718{
719 printf("print_name_lsdb called \n");
720 int i, adj_lsdb_element;
721 struct alsa *adj_lsa;
722
723 struct hashtb_enumerator ee;
724 struct hashtb_enumerator *e = &ee;
725
726 hashtb_start(nlsr->lsdb->adj_lsdb, e);
727 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
728
729 for(i=0;i<adj_lsdb_element;i++)
730 {
731 printf("-----------Adj LSA (%d)---------------\n",i+1);
732 adj_lsa=e->data;
733 print_adj_lsa(adj_lsa);
734 hashtb_next(e);
735 }
736
737 hashtb_end(e);
738
739 printf("\n");
akmhoque53f64222012-09-05 13:57:51 -0500740}
741
742void
akmhoque03004e62012-09-06 01:12:28 -0500743build_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 -0500744{
akmhoque03004e62012-09-06 01:12:28 -0500745 printf("build_and_install_others_adj_lsa called \n");
746 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
747 build_others_adj_lsa(adj_lsa,orig_router,ls_type,orig_time,no_link,data);
748 //print_adj_lsa(adj_lsa);
749 install_adj_lsa(adj_lsa);
750
akmhoque53f64222012-09-05 13:57:51 -0500751
akmhoque03004e62012-09-06 01:12:28 -0500752 free(adj_lsa->header->orig_router->name);
753 free(adj_lsa->header->orig_router);
754 free(adj_lsa->header->orig_time);
755 free(adj_lsa->header);
756 free(adj_lsa->body);
757 free(adj_lsa);
akmhoque53f64222012-09-05 13:57:51 -0500758
akmhoque03004e62012-09-06 01:12:28 -0500759 print_adj_lsdb();
akmhoque53f64222012-09-05 13:57:51 -0500760
akmhoque53f64222012-09-05 13:57:51 -0500761}
762
akmhoque03004e62012-09-06 01:12:28 -0500763
akmhoque53f64222012-09-05 13:57:51 -0500764void
akmhoque03004e62012-09-06 01:12:28 -0500765build_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 -0500766{
akmhoque03004e62012-09-06 01:12:28 -0500767 printf("build_others_adj_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500768
akmhoque03004e62012-09-06 01:12:28 -0500769 /*Filling Up Header Data */
770 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
771 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
772 adj_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
773 memset(adj_lsa->header->orig_router->name,0,strlen(orig_router)+1);
774 memcpy(adj_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
akmhoque53f64222012-09-05 13:57:51 -0500775
akmhoque03004e62012-09-06 01:12:28 -0500776 adj_lsa->header->orig_router->length=strlen(orig_router)+1;
akmhoque53f64222012-09-05 13:57:51 -0500777
akmhoque53f64222012-09-05 13:57:51 -0500778
akmhoque03004e62012-09-06 01:12:28 -0500779 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
akmhoque53f64222012-09-05 13:57:51 -0500780
akmhoque03004e62012-09-06 01:12:28 -0500781 adj_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
782 memset(adj_lsa->header->orig_time,0,strlen(orig_time)+1);
783 memcpy(adj_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
akmhoque53f64222012-09-05 13:57:51 -0500784
akmhoque03004e62012-09-06 01:12:28 -0500785 adj_lsa->no_link=no_link;
akmhoque53f64222012-09-05 13:57:51 -0500786
akmhoque03004e62012-09-06 01:12:28 -0500787 adj_lsa->body=(char *)malloc(strlen(data)+1);
788 memset(adj_lsa->body,0,strlen(data)+1);
789 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
akmhoque53f64222012-09-05 13:57:51 -0500790
akmhoque53f64222012-09-05 13:57:51 -0500791}
792
akmhoque03004e62012-09-06 01:12:28 -0500793
akmhoque53f64222012-09-05 13:57:51 -0500794long int
795get_name_lsdb_num_element(void)
796{
797 long int num_element;
798
799
800 struct hashtb_enumerator ee;
801 struct hashtb_enumerator *e = &ee;
802
803 hashtb_start(nlsr->lsdb->name_lsdb, e);
804 num_element=hashtb_n(nlsr->lsdb->name_lsdb);
805 hashtb_end(e);
806
807 return num_element;
808}
809
810long int
811get_adj_lsdb_num_element(void)
812{
813 long int num_element;
814
815
816 struct hashtb_enumerator ee;
817 struct hashtb_enumerator *e = &ee;
818
819 hashtb_start(nlsr->lsdb->adj_lsdb, e);
820 num_element=hashtb_n(nlsr->lsdb->adj_lsdb);
821 hashtb_end(e);
822
823 return num_element;
akmhoqued79438d2012-08-27 13:31:42 -0500824}
akmhoque03004e62012-09-06 01:12:28 -0500825
826void
827get_name_lsdb_summary(struct ccn_charbuf *name_lsdb_data)
828{
829 printf("get_name_lsdb_summary called \n");
830 int i, name_lsdb_element;
831
832 struct nlsa *name_lsa;
833 struct hashtb_enumerator ee;
834 struct hashtb_enumerator *e = &ee;
835
836 hashtb_start(nlsr->lsdb->name_lsdb, e);
837 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
838
839 for(i=0;i<name_lsdb_element;i++)
840 {
841 name_lsa=e->data;
842
843 ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_router->name);
844 ccn_charbuf_append_string(name_lsdb_data,"|");
845
846 char *lst=(char *)malloc(20);
847 memset(lst,0,20);
848 sprintf(lst,"%d",name_lsa->header->ls_type);
849 ccn_charbuf_append_string(name_lsdb_data,lst);
850 free(lst);
851 ccn_charbuf_append_string(name_lsdb_data,"|");
852
853 char *lsid=(char *)malloc(20);
854 memset(lsid,0,20);
855 sprintf(lsid,"%ld",name_lsa->header->ls_id);
856 ccn_charbuf_append_string(name_lsdb_data,lsid);
857 free(lsid);
858 ccn_charbuf_append_string(name_lsdb_data,"|");
859
860 ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_time);
861 ccn_charbuf_append_string(name_lsdb_data,"|");
862
863 hashtb_next(e);
864 }
865
866 hashtb_end(e);
867
868}
869
870
871void
872get_adj_lsdb_summary(struct ccn_charbuf *adj_lsdb_data)
873{
874 printf("get_adj_lsdb_summary called \n");
875 int i, adj_lsdb_element;
876 struct alsa *adj_lsa;
877
878 struct hashtb_enumerator ee;
879 struct hashtb_enumerator *e = &ee;
880
881 hashtb_start(nlsr->lsdb->adj_lsdb, e);
882 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
883
884 for(i=0;i<adj_lsdb_element;i++)
885 {
886 adj_lsa=e->data;
887
888 ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_router->name);
889 ccn_charbuf_append_string(adj_lsdb_data,"|");
890
891 char *lst=(char *)malloc(20);
892 memset(lst,0,20);
893 sprintf(lst,"%d",adj_lsa->header->ls_type);
894 ccn_charbuf_append_string(adj_lsdb_data,lst);
895 free(lst);
896 ccn_charbuf_append_string(adj_lsdb_data,"|");
897
898 ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_time);
899 ccn_charbuf_append_string(adj_lsdb_data,"|");
900
901 hashtb_next(e);
902 }
903
904 hashtb_end(e);
905}
906
907
908void
909get_lsdb_summary(struct ccn_charbuf *lsdb_data)
910{
911 struct ccn_charbuf *name_lsdb_data=ccn_charbuf_create();
912 struct ccn_charbuf *adj_lsdb_data=ccn_charbuf_create();
913
914 get_name_lsdb_summary(name_lsdb_data);
915 get_adj_lsdb_summary(adj_lsdb_data);
916
917 long int num_lsa=get_name_lsdb_num_element() + get_adj_lsdb_num_element();
918 char *num_element=(char *)malloc(15);
919 memset(num_element,0,15);
920 sprintf(num_element,"%ld",num_lsa);
921
922 if( num_lsa > 0)
923 {
924 ccn_charbuf_append_string(lsdb_data,num_element);
925 ccn_charbuf_append_string(lsdb_data,"|");
926 }
927 if(name_lsdb_data->length>0)
928 {
929 char *data1=ccn_charbuf_as_string(name_lsdb_data);
930 ccn_charbuf_append_string(lsdb_data,(char *)data1);
931 }
932 if(adj_lsdb_data->length>0)
933 {
934 char *data2=ccn_charbuf_as_string(adj_lsdb_data);
935 ccn_charbuf_append_string(lsdb_data,(char *)data2);
936 }
937 ccn_charbuf_destroy(&name_lsdb_data);
938 ccn_charbuf_destroy(&adj_lsdb_data);
939 free(num_element);
940
941}
942
943int
944check_is_new_name_lsa(char *orig_router,char *lst,char *lsid,char *orig_time)
945{
946 int ret=0;
947 struct ccn_charbuf *key=ccn_charbuf_create();
948 ccn_charbuf_append_string(key,orig_router);
949 ccn_charbuf_append_string(key,"/");
950 ccn_charbuf_append_string(key,lst);
951 ccn_charbuf_append_string(key,"/");
952 ccn_charbuf_append_string(key,lsid);
953
954 int res;
955 struct nlsa *name_lsa;
956
957 struct hashtb_enumerator ee;
958 struct hashtb_enumerator *e = &ee;
959
960 hashtb_start(nlsr->lsdb->name_lsdb, e);
961 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
962
963 if( res == HT_NEW_ENTRY )
964 {
965 hashtb_delete(e);
966 ret=1;
967
968 }
969 else if(res == HT_OLD_ENTRY)
970 {
971 name_lsa=e->data;
972 if( strcmp ( orig_time , name_lsa->header->orig_time ) > 0 )
973 {
974 ret=1;
975 }
976 }
977
978 hashtb_end(e);
979
980 ccn_charbuf_destroy(&key);
981
982 return ret;
983}
984
985int
986check_is_new_adj_lsa(char *orig_router,char *lst,char *orig_time)
987{
988 int ret=0;
989 struct ccn_charbuf *key=ccn_charbuf_create();
990 ccn_charbuf_append_string(key,orig_router);
991 ccn_charbuf_append_string(key,"/");
992 ccn_charbuf_append_string(key,lst);
993
994 int res;
995 struct alsa *adj_lsa;
996
997 struct hashtb_enumerator ee;
998 struct hashtb_enumerator *e = &ee;
999
1000 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1001 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1002
1003 if( res == HT_NEW_ENTRY )
1004 {
1005 hashtb_delete(e);
1006 ret=1;
1007
1008 }
1009 else if(res == HT_OLD_ENTRY)
1010 {
1011 adj_lsa=e->data;
1012 if( strcmp ( orig_time , adj_lsa->header->orig_time ) > 0 )
1013 {
1014 ret=1;
1015 }
1016 }
1017
1018 hashtb_end(e);
1019
1020 ccn_charbuf_destroy(&key);
1021
1022 return ret;
1023}
1024
1025void
1026get_name_lsa_data(struct ccn_charbuf *lsa_data, struct name_prefix *lsaId)
1027{
1028 printf("get_name_lsa_data called \n");
1029
1030 struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa ));
1031
1032 struct hashtb_enumerator ee;
1033 struct hashtb_enumerator *e = &ee;
1034 int res;
1035
1036 hashtb_start(nlsr->lsdb->name_lsdb, e);
1037 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1038
1039 if( res == HT_OLD_ENTRY )
1040 {
1041 name_lsa=e->data;
1042 printf("NAME LSA found\n");
1043
1044 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_router->name);
1045 ccn_charbuf_append_string(lsa_data,"|");
1046
1047 char *temp_length=(char *)malloc(20);
1048 memset(temp_length,0,20);
1049 sprintf(temp_length,"%d",name_lsa->header->orig_router->length);
1050 ccn_charbuf_append_string(lsa_data,temp_length);
1051 free(temp_length);
1052 ccn_charbuf_append_string(lsa_data,"|");
1053
1054 char *temp_ltype=(char *)malloc(20);
1055 memset(temp_ltype,0,20);
1056 sprintf(temp_ltype,"%d",name_lsa->header->ls_type);
1057 ccn_charbuf_append_string(lsa_data,temp_ltype);
1058 free(temp_ltype);
1059 ccn_charbuf_append_string(lsa_data,"|");
1060
1061 char *temp_lsid=(char *)malloc(20);
1062 memset(temp_lsid,0,20);
1063 sprintf(temp_lsid,"%ld",name_lsa->header->ls_id);
1064 ccn_charbuf_append_string(lsa_data,temp_lsid);
1065 free(temp_lsid);
1066 ccn_charbuf_append_string(lsa_data,"|");
1067
1068 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_time);
1069 ccn_charbuf_append_string(lsa_data,"|");
1070
1071 char *temp_valid=(char *)malloc(20);
1072 memset(temp_valid,0,20);
1073 sprintf(temp_valid,"%d",name_lsa->header->isValid);
1074 ccn_charbuf_append_string(lsa_data,temp_valid);
1075 free(temp_valid);
1076 ccn_charbuf_append_string(lsa_data,"|");
1077
1078 ccn_charbuf_append_string(lsa_data,name_lsa->name_prefix->name);
1079 ccn_charbuf_append_string(lsa_data,"|");
1080
1081 char *temp_npl=(char *)malloc(20);
1082 memset(temp_npl,0,20);
1083 sprintf(temp_npl,"%d",name_lsa->name_prefix->length);
1084 ccn_charbuf_append_string(lsa_data,temp_npl);
1085 free(temp_npl);
1086 ccn_charbuf_append_string(lsa_data,"|");
1087
1088 }
1089 else if(res == HT_NEW_ENTRY)
1090 {
1091 hashtb_delete(e);
1092 }
1093
1094 hashtb_end(e);
1095}
1096
1097void
1098get_adj_lsa_data(struct ccn_charbuf *lsa_data,struct name_prefix *lsaId)
1099{
1100 printf("get_adj_lsa_data called \n");
1101
1102 struct alsa *adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
1103
1104 struct hashtb_enumerator ee;
1105 struct hashtb_enumerator *e = &ee;
1106 int res;
1107
1108 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1109 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1110
1111 if( res == HT_OLD_ENTRY )
1112 {
1113 adj_lsa=e->data;
1114 printf("NAME LSA found\n");
1115
1116 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_router->name);
1117 ccn_charbuf_append_string(lsa_data,"|");
1118
1119 char *temp_length=(char *)malloc(20);
1120 memset(temp_length,0,20);
1121 sprintf(temp_length,"%d",adj_lsa->header->orig_router->length);
1122 ccn_charbuf_append_string(lsa_data,temp_length);
1123 free(temp_length);
1124 ccn_charbuf_append_string(lsa_data,"|");
1125
1126 char *temp_ltype=(char *)malloc(20);
1127 memset(temp_ltype,0,20);
1128 sprintf(temp_ltype,"%d",adj_lsa->header->ls_type);
1129 ccn_charbuf_append_string(lsa_data,temp_ltype);
1130 free(temp_ltype);
1131 ccn_charbuf_append_string(lsa_data,"|");
1132
1133 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_time);
1134 ccn_charbuf_append_string(lsa_data,"|");
1135
1136 char *temp_nl=(char *)malloc(20);
1137 memset(temp_nl,0,20);
1138 sprintf(temp_nl,"%d",adj_lsa->no_link);
1139 ccn_charbuf_append_string(lsa_data,temp_nl);
1140 free(temp_nl);
1141 ccn_charbuf_append_string(lsa_data,"|");
1142
1143 ccn_charbuf_append_string(lsa_data,adj_lsa->body);
1144
1145
1146 }
1147 else if(res == HT_NEW_ENTRY)
1148 {
1149 hashtb_delete(e);
1150 }
1151
1152 hashtb_end(e);
1153}
akmhoqueffacaa82012-09-13 17:48:30 -05001154
akmhoqueda5b6832012-09-13 22:33:55 -05001155int
1156delete_name_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1157{
1158 printf("delete_name_lsa called \n");
1159
1160 if(flags == CCN_SCHEDULE_CANCEL)
1161 {
1162 return -1;
1163 }
1164
1165
1166
1167 nlsr_lock();
1168
1169 printf("LSA Key: %s \n",(char *)ev->evdata);
1170
1171 struct nlsa *nlsa;
1172
1173 struct hashtb_enumerator ee;
1174 struct hashtb_enumerator *e = &ee;
1175
1176 int res;
1177
1178 hashtb_start(nlsr->lsdb->name_lsdb, e);
1179 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1180
1181 if( res == HT_OLD_ENTRY )
1182 {
1183 nlsa=e->data;
1184 delete_npt_entry(nlsa->header->orig_router->name, nlsa->name_prefix->name);
1185 hashtb_delete(e);
1186 }
1187 else if( res == HT_OLD_ENTRY )
1188 {
1189 nlsa=e->data;
1190 delete_npt_entry(nlsa->header->orig_router->name, nlsa->name_prefix->name);
1191 hashtb_delete(e);
1192 }
1193 hashtb_end(e);
1194
1195
1196 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1197 set_new_lsdb_version();
1198 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1199
1200 //print_name_lsdb();
1201
1202 nlsr_unlock();
1203
1204 return 0;
1205}
1206
1207int
1208delete_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1209{
1210 printf("delete_adj_lsa called \n");
1211
1212 if(flags == CCN_SCHEDULE_CANCEL)
1213 {
1214 return -1;
1215 }
1216 nlsr_lock();
1217
1218 printf("LSA Key: %s \n",(char *)ev->evdata);
1219
1220 struct hashtb_enumerator ee;
1221 struct hashtb_enumerator *e = &ee;
1222 int res;
1223
1224 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1225 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1226
1227 if( res == HT_OLD_ENTRY )
1228 {
1229 hashtb_delete(e);
1230 }
1231 else if( res == HT_OLD_ENTRY )
1232 {
1233 hashtb_delete(e);
1234 }
1235 hashtb_end(e);
1236
1237 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1238 set_new_lsdb_version();
1239 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1240
1241 if ( !nlsr->is_route_calculation_scheduled)
1242 {
1243 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
1244 nlsr->is_route_calculation_scheduled=1;
1245 }
1246
1247 //print_adj_lsdb();
1248
1249 nlsr_unlock();
1250
1251 return 0;
1252}
1253
akmhoqueffacaa82012-09-13 17:48:30 -05001254void
1255refresh_name_lsdb(void)
1256{
1257 printf("refresh_name_lsdb called \n");
1258
akmhoqueda5b6832012-09-13 22:33:55 -05001259 //int lsa_change_count=0;
1260
akmhoqueffacaa82012-09-13 17:48:30 -05001261 char *time_stamp=(char *)malloc(20);
1262 memset(time_stamp,0,20);
1263 get_current_timestamp_micro(time_stamp);
1264
1265 long int lsa_life_time;
1266
1267 int i, name_lsdb_element;
1268 struct nlsa *name_lsa;
1269
1270 struct hashtb_enumerator ee;
1271 struct hashtb_enumerator *e = &ee;
1272
1273 hashtb_start(nlsr->lsdb->name_lsdb, e);
1274 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
1275
1276 for(i=0;i<name_lsdb_element;i++)
1277 {
1278 name_lsa=e->data;
1279
1280 lsa_life_time=get_time_diff(time_stamp,name_lsa->header->orig_time);
akmhoqueda5b6832012-09-13 22:33:55 -05001281 printf("LSA Life Time: %ld \n",lsa_life_time);
1282
1283 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name) == 0)
1284 {
1285 if ( lsa_life_time > nlsr->lsa_refresh_time )
1286 {
akmhoque14b3f342012-09-14 10:39:02 -05001287 if ( name_lsa->header->isValid == NAME_LSA_VALID )
1288 {
1289 printf("Own Name LSA need to be refrshed\n");
1290 char *current_time_stamp=(char *)malloc(20);
1291 memset(current_time_stamp,0,20);
1292 get_current_timestamp_micro(current_time_stamp);
akmhoqueda5b6832012-09-13 22:33:55 -05001293
akmhoque14b3f342012-09-14 10:39:02 -05001294 free(name_lsa->header->orig_time);
1295 name_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1296 memset(name_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1297 memcpy(name_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
akmhoqueda5b6832012-09-13 22:33:55 -05001298
akmhoque14b3f342012-09-14 10:39:02 -05001299 free(current_time_stamp);
1300 }
1301 else
1302 {
1303 char lst[2];
1304 memset(lst,0,2);
1305 sprintf(lst,"%d",name_lsa->header->ls_type);
1306
1307 char lsid[10];
1308 memset(lsid,0,10);
1309 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1310
1311
1312 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1313 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1314
1315
1316 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
1317 printf("Key:%s Length:%d\n",key,(int)strlen(key));
1318
1319 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
1320 }
akmhoqueda5b6832012-09-13 22:33:55 -05001321
1322 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1323 set_new_lsdb_version();
1324 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1325
1326 print_name_lsdb();
1327 //lsa_change_count++;
1328 }
1329 }
1330 else
1331 {
1332 if ( lsa_life_time > nlsr->router_dead_interval )
1333 {
1334 printf("Others Name LSA need to be deleted\n");
1335
1336 char lst[2];
1337 memset(lst,0,2);
1338 sprintf(lst,"%d",name_lsa->header->ls_type);
1339
1340 char lsid[10];
1341 memset(lsid,0,10);
1342 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1343
1344
1345 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1346 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1347
1348
1349 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
1350 printf("Key:%s Length:%d\n",key,(int)strlen(key));
1351
1352 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
1353 //lsa_change_count++;
1354 }
1355 }
akmhoqueffacaa82012-09-13 17:48:30 -05001356
1357 hashtb_next(e);
1358 }
1359
1360 hashtb_end(e);
1361
1362 free(time_stamp);
akmhoqueda5b6832012-09-13 22:33:55 -05001363
akmhoqueffacaa82012-09-13 17:48:30 -05001364
1365}
1366
akmhoqueda5b6832012-09-13 22:33:55 -05001367void
akmhoqueffacaa82012-09-13 17:48:30 -05001368refresh_adj_lsdb(void)
1369{
1370 printf("refresh_adj_lsdb called \n");
1371
akmhoqueda5b6832012-09-13 22:33:55 -05001372
akmhoqueffacaa82012-09-13 17:48:30 -05001373 char *time_stamp=(char *)malloc(20);
1374 memset(time_stamp,0,20);
1375 get_current_timestamp_micro(time_stamp);
1376
1377 long int lsa_life_time;
1378
1379 int i, adj_lsdb_element;
1380 struct alsa *adj_lsa;
1381
1382 struct hashtb_enumerator ee;
1383 struct hashtb_enumerator *e = &ee;
1384
1385 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1386 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1387
1388 for(i=0;i<adj_lsdb_element;i++)
1389 {
1390 adj_lsa=e->data;
1391
1392 lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
1393 printf("LSA Life Time: %ld \n",lsa_life_time);
1394
akmhoqueda5b6832012-09-13 22:33:55 -05001395 if ( strcmp(adj_lsa->header->orig_router->name,nlsr->router_name) == 0)
1396 {
1397 if ( lsa_life_time > nlsr->lsa_refresh_time )
1398 {
1399 printf("Own Adj LSA need to be refrshed\n");
1400
1401 char *current_time_stamp=(char *)malloc(20);
1402 memset(current_time_stamp,0,20);
1403 get_current_timestamp_micro(current_time_stamp);
1404
1405 free(adj_lsa->header->orig_time);
1406 adj_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1407 memset(adj_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1408 memcpy(adj_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1409
1410 free(current_time_stamp);
1411
1412
1413 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1414 set_new_lsdb_version();
1415 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1416
1417 print_adj_lsdb();
1418 }
1419 }
1420 else
1421 {
1422 if ( lsa_life_time > nlsr->router_dead_interval )
1423 {
1424 printf("Others Adj LSA need to be deleted\n");
1425
1426 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
1427 memset(key,0,adj_lsa->header->orig_router->length+2);
1428 make_adj_lsa_key(key,adj_lsa);
1429 printf("Adjacent LSA key: %s \n",key);
1430
1431 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_adj_lsa, (void *)key, 0);
1432 }
1433 }
1434
1435
1436
akmhoqueffacaa82012-09-13 17:48:30 -05001437 hashtb_next(e);
1438 }
1439
1440 hashtb_end(e);
1441
1442 free(time_stamp);
akmhoqueffacaa82012-09-13 17:48:30 -05001443}
1444
1445int
1446refresh_lsdb(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1447{
1448 if(flags == CCN_SCHEDULE_CANCEL)
1449 {
1450 return -1;
1451 }
1452
1453 nlsr_lock();
akmhoqueda5b6832012-09-13 22:33:55 -05001454
akmhoqueffacaa82012-09-13 17:48:30 -05001455 printf("refresh_lsdb called \n");
1456
1457 refresh_name_lsdb();
1458 refresh_adj_lsdb();
1459
akmhoqueffacaa82012-09-13 17:48:30 -05001460 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoqueda5b6832012-09-13 22:33:55 -05001461
akmhoqueffacaa82012-09-13 17:48:30 -05001462 nlsr_unlock();
1463 return 0;
1464}