blob: 0a88b5efecd8492c66eeae30d713fc6540806788 [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);
akmhoqued5152122012-09-19 06:44:23 -0500207
208 //printf("Next hop: %d \n",next_hop);
209
akmhoqueffacaa82012-09-13 17:48:30 -0500210 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;
akmhoque14b3f342012-09-14 10:39:02 -0500235 if(strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time)<0)
akmhoqueffacaa82012-09-13 17:48:30 -0500236 {
akmhoque14b3f342012-09-14 10:39:02 -0500237 printf("Older Adj LSA. Discarded...\n");
238 }
239 else if( strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time) == 0 )
240 {
241 printf("Duplicate Adj LSA. Discarded...\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500242 }
243 else
244 {
245 if ( name_lsa->header->isValid == 0 )
246 {
247 // have to call to delete npt table entry
248 delete_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name);
249
250 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name)!= 0)
251 {
252 hashtb_delete(e);
253 printf("isValid bit not set for Router %s so LSA Deleted from LSDB\n",name_lsa->header->orig_router->name);
254 }
255 else
256 {
257 new_name_lsa->header->isValid=name_lsa->header->isValid;
258 free(new_name_lsa->header->orig_time);
259 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
260 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
261 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
262 }
263 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
264 set_new_lsdb_version();
265 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
266 }
267 else
268 {
269 int is_npt_update=0;
270 if ( strcmp(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name) != 0 )
271 {
272 is_npt_update=1;
273 delete_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name);
274 }
275
276 // copying LSA content with header
277
278 free(new_name_lsa->header->orig_time);
279 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
280 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
281 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
282
283 new_name_lsa->header->isValid=name_lsa->header->isValid;
284
285 free(new_name_lsa->name_prefix->name);
286 free(new_name_lsa->name_prefix);
287 new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
288 new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
289 memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
290 new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
291
292 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
293 set_new_lsdb_version();
294 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
295
296 if( is_npt_update == 1 )
297 {
298 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name);
299 if ( next_hop == NO_NEXT_HOP )
300 {
301 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,NO_FACE);
302 if ( check == HT_NEW_ENTRY )
303 {
304 printf("Added in npt \n");
305 }
306 }
307 else
308 {
309 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,next_hop);
310 if ( check == HT_NEW_ENTRY )
311 {
312 printf("Added in npt \n");
313 }
314 }
315 }
316 }
317 }
318
akmhoque3560cb62012-09-09 10:52:30 -0500319 }
akmhoque53f64222012-09-05 13:57:51 -0500320
akmhoqueffacaa82012-09-13 17:48:30 -0500321 hashtb_end(e);
akmhoque53f64222012-09-05 13:57:51 -0500322
akmhoqueffacaa82012-09-13 17:48:30 -0500323 free(key);
akmhoque53f64222012-09-05 13:57:51 -0500324}
325
326
akmhoque03004e62012-09-06 01:12:28 -0500327void
328print_name_lsa(struct nlsa *name_lsa)
akmhoque53f64222012-09-05 13:57:51 -0500329{
330
akmhoque03004e62012-09-06 01:12:28 -0500331 printf("-----------Name LSA Content---------------\n");
332 printf(" Origination Router : %s\n",name_lsa->header->orig_router->name);
333 printf(" Origination Router Length: %d\n",name_lsa->header->orig_router->length);
334 printf(" LS Type : %d\n",name_lsa->header->ls_type);
335 printf(" LS Id : %ld\n",name_lsa->header->ls_id);
336 printf(" Origination Time : %s\n",name_lsa->header->orig_time);
337 printf(" Is Valid : %d\n",name_lsa->header->isValid);
338 printf(" LSA Data \n");
339 printf(" Name Prefix: : %s\n",name_lsa->name_prefix->name);
340 printf(" Name Prefix Length : %d\n",name_lsa->name_prefix->length);
akmhoque53f64222012-09-05 13:57:51 -0500341
akmhoque03004e62012-09-06 01:12:28 -0500342 printf("\n");
akmhoquef71d9082012-08-22 12:51:53 -0400343}
344
345void
346print_name_lsdb(void)
347{
348 printf("print_name_lsdb called \n");
akmhoque53f64222012-09-05 13:57:51 -0500349 int i, name_lsdb_element;
akmhoquef71d9082012-08-22 12:51:53 -0400350 struct nlsa *name_lsa;
351
352 struct hashtb_enumerator ee;
353 struct hashtb_enumerator *e = &ee;
354
355 hashtb_start(nlsr->lsdb->name_lsdb, e);
akmhoque53f64222012-09-05 13:57:51 -0500356 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
akmhoquef71d9082012-08-22 12:51:53 -0400357
akmhoque53f64222012-09-05 13:57:51 -0500358 for(i=0;i<name_lsdb_element;i++)
akmhoquef71d9082012-08-22 12:51:53 -0400359 {
akmhoque53f64222012-09-05 13:57:51 -0500360 printf("-----------Name LSA (%d)---------------\n",i+1);
akmhoquef71d9082012-08-22 12:51:53 -0400361 name_lsa=e->data;
akmhoque53f64222012-09-05 13:57:51 -0500362 print_name_lsa(name_lsa);
akmhoquef71d9082012-08-22 12:51:53 -0400363 hashtb_next(e);
364 }
365
366 hashtb_end(e);
367
akmhoque53f64222012-09-05 13:57:51 -0500368 printf("\n");
369}
370
akmhoque03004e62012-09-06 01:12:28 -0500371
372void
373build_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 -0500374{
akmhoque03004e62012-09-06 01:12:28 -0500375 printf("build_and_install_others_name_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500376
akmhoque03004e62012-09-06 01:12:28 -0500377 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
378 build_others_name_lsa(name_lsa,orig_router,ls_type,ls_id,orig_time, isValid,np);
379 print_name_lsa(name_lsa);
380 install_name_lsa(name_lsa);
381 print_name_lsdb();
akmhoque53f64222012-09-05 13:57:51 -0500382
akmhoque53f64222012-09-05 13:57:51 -0500383
akmhoque03004e62012-09-06 01:12:28 -0500384 free(name_lsa->header->orig_router->name);
385 free(name_lsa->header->orig_router);
386 free(name_lsa->header);
387 free(name_lsa->name_prefix->name);
388 free(name_lsa->name_prefix);
389 free(name_lsa);
akmhoque53f64222012-09-05 13:57:51 -0500390
391}
392
393void
akmhoque03004e62012-09-06 01:12:28 -0500394build_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 -0500395{
akmhoque03004e62012-09-06 01:12:28 -0500396 printf("build_others_name_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500397
akmhoque03004e62012-09-06 01:12:28 -0500398 name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header ));
399 name_lsa->header->ls_type=LS_TYPE_NAME;
akmhoque53f64222012-09-05 13:57:51 -0500400
akmhoque03004e62012-09-06 01:12:28 -0500401 name_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
402 memset(name_lsa->header->orig_time,0,strlen(orig_time)+1);
403 memcpy(name_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
akmhoque53f64222012-09-05 13:57:51 -0500404
akmhoque03004e62012-09-06 01:12:28 -0500405 name_lsa->header->ls_id=ls_id;
406 name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
407 name_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
408 memset(name_lsa->header->orig_router->name,0,strlen(orig_router)+1);
409 memcpy(name_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
410 name_lsa->header->orig_router->length=strlen(orig_router)+1;
411 name_lsa->header->isValid=isValid;
akmhoque53f64222012-09-05 13:57:51 -0500412
akmhoque03004e62012-09-06 01:12:28 -0500413 name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
414 name_lsa->name_prefix->name=(char *)malloc(strlen(np)+1);
415 memset(name_lsa->name_prefix->name,0,strlen(np)+1);
416 memcpy(name_lsa->name_prefix->name,np,strlen(np)+1);
417 name_lsa->name_prefix->length=strlen(np)+1;
akmhoquef71d9082012-08-22 12:51:53 -0400418}
akmhoqued79438d2012-08-27 13:31:42 -0500419
420
akmhoqueffacaa82012-09-13 17:48:30 -0500421
422
akmhoque53f64222012-09-05 13:57:51 -0500423void
424make_adj_lsa_key(char *key,struct alsa *adj_lsa)
akmhoqued79438d2012-08-27 13:31:42 -0500425{
akmhoque53f64222012-09-05 13:57:51 -0500426 memcpy(key+strlen(key),adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
427 memcpy(key+strlen(key),"/",1);
428 char ls_type[2];
429 sprintf(ls_type,"%d",adj_lsa->header->ls_type);
430 memcpy(key+strlen(key),ls_type,strlen(ls_type));
431 key[strlen(key)]='\0';
432}
433
akmhoque03004e62012-09-06 01:12:28 -0500434int
435build_and_install_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
436{
akmhoqueffacaa82012-09-13 17:48:30 -0500437 if(flags == CCN_SCHEDULE_CANCEL)
438 {
439 return -1;
440 }
441
442 nlsr_lock();
443
akmhoque03004e62012-09-06 01:12:28 -0500444 printf("build_and_install_adj_lsa called \n");
445
446 printf("adj_build_flag = %d \n",nlsr->adj_build_flag);
447
448 if(nlsr->adj_build_flag > 0)
449 {
450 printf("is_adj_lsa_build = %d \n",is_adj_lsa_build());
451 if ( is_adj_lsa_build()> 0)
452 {
453 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
454 build_adj_lsa(adj_lsa);
455 install_adj_lsa(adj_lsa);
456
457 free(adj_lsa->header->orig_router->name);
458 free(adj_lsa->header->orig_router);
459 free(adj_lsa->header->orig_time);
460 free(adj_lsa->header);
461 free(adj_lsa->body);
462 free(adj_lsa);
463 nlsr->adj_build_flag=0;
464 print_adj_lsdb();
465 }
466 else
467 {
468 printf("Can not build adj LSA now\n");
469 }
470 }
471 nlsr->is_build_adj_lsa_sheduled=0;
akmhoqueffacaa82012-09-13 17:48:30 -0500472
473 nlsr_unlock();
474
akmhoque03004e62012-09-06 01:12:28 -0500475 return 0;
476}
477
478
479void
480build_adj_lsa(struct alsa * adj_lsa)
481{
482 printf("build_adj_lsa called \n");
483
484 int no_link=no_active_nbr();
485 printf("Number of link in Adjacent LSA: %d\n",no_link);
486
487 /*Filling Up Header Data */
488 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
489 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
490 adj_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1);
491 memset(adj_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
492 memcpy(adj_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
493 adj_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
494
495 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
496
497 char *time_stamp=(char *)malloc(20);
498 memset(time_stamp,0,20);
499 get_current_timestamp_micro(time_stamp);
500
501 adj_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1);
502 memset(adj_lsa->header->orig_time,0,strlen(time_stamp)+1);
503 memcpy(adj_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
504 free(time_stamp);
505
506
507 /* Filling Up Body Data */
508
509 adj_lsa->no_link=no_link;
akmhoque03004e62012-09-06 01:12:28 -0500510
511
512 struct ccn_charbuf *c=ccn_charbuf_create();
513 get_active_nbr_adj_data(c);
514 char *data=ccn_charbuf_as_string(c);
515
516 adj_lsa->body=(char *)malloc(strlen(data)+1);
517 memset(adj_lsa->body,0,strlen(data)+1);
518 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
519 ccn_charbuf_destroy(&c);
520
521
522
523 if( !nlsr->is_send_lsdb_interest_scheduled )
524 {
525 nlsr->event_send_lsdb_interest= ccn_schedule_event(nlsr->sched, 1000, &send_lsdb_interest, NULL, 0);
526 nlsr->is_send_lsdb_interest_scheduled=1;
527 }
528
529 nlsr->adj_build_count++;
530
531
532}
533
534
akmhoque53f64222012-09-05 13:57:51 -0500535void
536install_adj_lsa(struct alsa * adj_lsa)
537{
akmhoqued79438d2012-08-27 13:31:42 -0500538 printf("install_adj_lsa called \n");
539
akmhoque03004e62012-09-06 01:12:28 -0500540
akmhoqueffacaa82012-09-13 17:48:30 -0500541 char *time_stamp=(char *)malloc(20);
542 memset(time_stamp,0,20);
543 get_current_timestamp_micro(time_stamp);
544 long int lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
akmhoque53f64222012-09-05 13:57:51 -0500545
akmhoqueffacaa82012-09-13 17:48:30 -0500546 printf("time difference: %ld \n",lsa_life_time);
akmhoque53f64222012-09-05 13:57:51 -0500547
akmhoqueffacaa82012-09-13 17:48:30 -0500548
549 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
550 memset(key,0,adj_lsa->header->orig_router->length+2);
551 make_adj_lsa_key(key,adj_lsa);
552 printf("Adjacent LSA key: %s \n",key);
553
554 struct alsa *new_adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
555
556 struct hashtb_enumerator ee;
557 struct hashtb_enumerator *e = &ee;
558 int res;
559
560 hashtb_start(nlsr->lsdb->adj_lsdb, e);
561 res = hashtb_seek(e, key, strlen(key), 0);
akmhoque53f64222012-09-05 13:57:51 -0500562
akmhoque03004e62012-09-06 01:12:28 -0500563
akmhoque53f64222012-09-05 13:57:51 -0500564
akmhoqueffacaa82012-09-13 17:48:30 -0500565 if(res == HT_NEW_ENTRY )
akmhoque53f64222012-09-05 13:57:51 -0500566 {
akmhoqueffacaa82012-09-13 17:48:30 -0500567 printf("New ADJ LSA... Adding to LSDB\n");
akmhoque53f64222012-09-05 13:57:51 -0500568 new_adj_lsa = e->data;
569
akmhoqueffacaa82012-09-13 17:48:30 -0500570 new_adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
571 new_adj_lsa->header->ls_type=adj_lsa->header->ls_type;
akmhoque53f64222012-09-05 13:57:51 -0500572 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
akmhoqueffacaa82012-09-13 17:48:30 -0500573 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
574
575 new_adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
576 new_adj_lsa->header->orig_router->name=(char *)malloc(adj_lsa->header->orig_router->length);
577 memcpy(new_adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
578 new_adj_lsa->header->orig_router->length=adj_lsa->header->orig_router->length;
akmhoque53f64222012-09-05 13:57:51 -0500579
akmhoque03004e62012-09-06 01:12:28 -0500580 new_adj_lsa->no_link=adj_lsa->no_link;
akmhoqueffacaa82012-09-13 17:48:30 -0500581
akmhoque53f64222012-09-05 13:57:51 -0500582 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
583 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
584 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
585
akmhoque03004e62012-09-06 01:12:28 -0500586 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500587 set_new_lsdb_version();
akmhoque03004e62012-09-06 01:12:28 -0500588 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500589
akmhoqueffacaa82012-09-13 17:48:30 -0500590 add_next_hop_router(new_adj_lsa->header->orig_router->name);
akmhoque3560cb62012-09-09 10:52:30 -0500591
akmhoqueffacaa82012-09-13 17:48:30 -0500592 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
593 }
594 else if(res == HT_OLD_ENTRY)
595 {
596 new_adj_lsa = e->data;
597 if(strcmp(adj_lsa->header->orig_time,new_adj_lsa->header->orig_time)<=0)
598 {
599 printf("Older/Duplicate Adj LSA. Discarded...\n");
600 }
601 else
602 {
603 new_adj_lsa = e->data;
604
605 free(new_adj_lsa->header->orig_time);
606 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
607 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
608
609 new_adj_lsa->no_link=adj_lsa->no_link;
610
611 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
612 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
613 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
614
615 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
616 set_new_lsdb_version();
617 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
618
619 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
620
621 }
622
623 }
624 hashtb_end(e);
625
626 if ( !nlsr->is_route_calculation_scheduled )
627 {
628 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
629 nlsr->is_route_calculation_scheduled=1;
akmhoque53f64222012-09-05 13:57:51 -0500630 }
631
akmhoque03004e62012-09-06 01:12:28 -0500632
akmhoqueffacaa82012-09-13 17:48:30 -0500633 free(key);
634
635 free(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -0500636}
637
638void
639print_adj_lsa_body(const char *body, int no_link)
640{
641 int i=0;
642 char *lsa_data=(char *)malloc(strlen(body)+1);
643 memset( lsa_data,0,strlen(body)+1);
644 memcpy(lsa_data,body,strlen(body)+1);
645 char *sep="|";
646 char *rem;
647 char *rtr_id;
648 char *length;
649 char *face;
650 char *metric;
651
akmhoque53f64222012-09-05 13:57:51 -0500652 if(no_link >0 )
653 {
654 rtr_id=strtok_r(lsa_data,sep,&rem);
655 length=strtok_r(NULL,sep,&rem);
656 face=strtok_r(NULL,sep,&rem);
657 metric=strtok_r(NULL,sep,&rem);
658
659 printf(" Link %d \n",i+1);
660 printf(" Neighbor : %s \n",rtr_id);
661 printf(" Neighbor Length : %s \n",length);
662 printf(" Connecting Face : %s \n",face);
663 printf(" Metric : %s \n",metric);
akmhoque03004e62012-09-06 01:12:28 -0500664
akmhoque53f64222012-09-05 13:57:51 -0500665
666 for(i=1;i<no_link;i++)
667 {
668 rtr_id=strtok_r(NULL,sep,&rem);
669 length=strtok_r(NULL,sep,&rem);
670 face=strtok_r(NULL,sep,&rem);
671 metric=strtok_r(NULL,sep,&rem);
672 printf(" Link %d \n",i+1);
673 printf(" Neighbor : %s \n",rtr_id);
674 printf(" Neighbor Length : %s \n",length);
675 printf(" Connecting Face : %s \n",face);
676 printf(" Metric : %s \n",metric);
677
678 }
679 }
680
681 free(lsa_data);
682}
683
684void
685print_adj_lsa(struct alsa * adj_lsa)
686{
akmhoque03004e62012-09-06 01:12:28 -0500687
akmhoque53f64222012-09-05 13:57:51 -0500688 printf("print_adj_lsa called \n");
akmhoque03004e62012-09-06 01:12:28 -0500689
akmhoque53f64222012-09-05 13:57:51 -0500690 printf("-----------ADJ LSA Content---------------\n");
691 printf(" Origination Router : %s\n",adj_lsa->header->orig_router->name);
692 printf(" Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
693 printf(" LS Type : %d\n",adj_lsa->header->ls_type);
694 printf(" Origination Time : %s\n",adj_lsa->header->orig_time);
akmhoque03004e62012-09-06 01:12:28 -0500695 printf(" Lsa Data:\n");
696 printf(" No of Link : %d\n",adj_lsa->no_link);
akmhoque53f64222012-09-05 13:57:51 -0500697
698 print_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
akmhoque53f64222012-09-05 13:57:51 -0500699
akmhoque03004e62012-09-06 01:12:28 -0500700/*
701 struct link *templ=adj_lsa->links;
702 int i;
703
704 for(i=0 ; i< adj_lsa->no_link ; i++)
705 {
706 printf(" Link %d \n",i+1);
707 printf(" Neighbor : %s \n",templ->nbr->name);
708 printf(" Neighbor Length : %d \n",templ->nbr->length);
709 printf(" Connecting Face : %d \n",templ->face);
710 printf(" Metric : %d \n",templ->metric);
711
712 templ++;
713 }
714*/
akmhoque53f64222012-09-05 13:57:51 -0500715 printf("\n");
716
717}
718
719void
720print_adj_lsdb(void)
721{
722 printf("print_name_lsdb called \n");
723 int i, adj_lsdb_element;
724 struct alsa *adj_lsa;
725
726 struct hashtb_enumerator ee;
727 struct hashtb_enumerator *e = &ee;
728
729 hashtb_start(nlsr->lsdb->adj_lsdb, e);
730 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
731
732 for(i=0;i<adj_lsdb_element;i++)
733 {
734 printf("-----------Adj LSA (%d)---------------\n",i+1);
735 adj_lsa=e->data;
736 print_adj_lsa(adj_lsa);
737 hashtb_next(e);
738 }
739
740 hashtb_end(e);
741
742 printf("\n");
akmhoque53f64222012-09-05 13:57:51 -0500743}
744
745void
akmhoque03004e62012-09-06 01:12:28 -0500746build_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 -0500747{
akmhoque03004e62012-09-06 01:12:28 -0500748 printf("build_and_install_others_adj_lsa called \n");
749 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
750 build_others_adj_lsa(adj_lsa,orig_router,ls_type,orig_time,no_link,data);
751 //print_adj_lsa(adj_lsa);
752 install_adj_lsa(adj_lsa);
753
akmhoque53f64222012-09-05 13:57:51 -0500754
akmhoque03004e62012-09-06 01:12:28 -0500755 free(adj_lsa->header->orig_router->name);
756 free(adj_lsa->header->orig_router);
757 free(adj_lsa->header->orig_time);
758 free(adj_lsa->header);
759 free(adj_lsa->body);
760 free(adj_lsa);
akmhoque53f64222012-09-05 13:57:51 -0500761
akmhoque03004e62012-09-06 01:12:28 -0500762 print_adj_lsdb();
akmhoque53f64222012-09-05 13:57:51 -0500763
akmhoque53f64222012-09-05 13:57:51 -0500764}
765
akmhoque03004e62012-09-06 01:12:28 -0500766
akmhoque53f64222012-09-05 13:57:51 -0500767void
akmhoque03004e62012-09-06 01:12:28 -0500768build_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 -0500769{
akmhoque03004e62012-09-06 01:12:28 -0500770 printf("build_others_adj_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500771
akmhoque03004e62012-09-06 01:12:28 -0500772 /*Filling Up Header Data */
773 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
774 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
775 adj_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
776 memset(adj_lsa->header->orig_router->name,0,strlen(orig_router)+1);
777 memcpy(adj_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
akmhoque53f64222012-09-05 13:57:51 -0500778
akmhoque03004e62012-09-06 01:12:28 -0500779 adj_lsa->header->orig_router->length=strlen(orig_router)+1;
akmhoque53f64222012-09-05 13:57:51 -0500780
akmhoque53f64222012-09-05 13:57:51 -0500781
akmhoque03004e62012-09-06 01:12:28 -0500782 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
akmhoque53f64222012-09-05 13:57:51 -0500783
akmhoque03004e62012-09-06 01:12:28 -0500784 adj_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
785 memset(adj_lsa->header->orig_time,0,strlen(orig_time)+1);
786 memcpy(adj_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
akmhoque53f64222012-09-05 13:57:51 -0500787
akmhoque03004e62012-09-06 01:12:28 -0500788 adj_lsa->no_link=no_link;
akmhoque53f64222012-09-05 13:57:51 -0500789
akmhoque03004e62012-09-06 01:12:28 -0500790 adj_lsa->body=(char *)malloc(strlen(data)+1);
791 memset(adj_lsa->body,0,strlen(data)+1);
792 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
akmhoque53f64222012-09-05 13:57:51 -0500793
akmhoque53f64222012-09-05 13:57:51 -0500794}
795
akmhoque03004e62012-09-06 01:12:28 -0500796
akmhoque53f64222012-09-05 13:57:51 -0500797long int
798get_name_lsdb_num_element(void)
799{
800 long int num_element;
801
802
803 struct hashtb_enumerator ee;
804 struct hashtb_enumerator *e = &ee;
805
806 hashtb_start(nlsr->lsdb->name_lsdb, e);
807 num_element=hashtb_n(nlsr->lsdb->name_lsdb);
808 hashtb_end(e);
809
810 return num_element;
811}
812
813long int
814get_adj_lsdb_num_element(void)
815{
816 long int num_element;
817
818
819 struct hashtb_enumerator ee;
820 struct hashtb_enumerator *e = &ee;
821
822 hashtb_start(nlsr->lsdb->adj_lsdb, e);
823 num_element=hashtb_n(nlsr->lsdb->adj_lsdb);
824 hashtb_end(e);
825
826 return num_element;
akmhoqued79438d2012-08-27 13:31:42 -0500827}
akmhoque03004e62012-09-06 01:12:28 -0500828
829void
830get_name_lsdb_summary(struct ccn_charbuf *name_lsdb_data)
831{
832 printf("get_name_lsdb_summary called \n");
833 int i, name_lsdb_element;
834
835 struct nlsa *name_lsa;
836 struct hashtb_enumerator ee;
837 struct hashtb_enumerator *e = &ee;
838
839 hashtb_start(nlsr->lsdb->name_lsdb, e);
840 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
841
842 for(i=0;i<name_lsdb_element;i++)
843 {
844 name_lsa=e->data;
845
846 ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_router->name);
847 ccn_charbuf_append_string(name_lsdb_data,"|");
848
849 char *lst=(char *)malloc(20);
850 memset(lst,0,20);
851 sprintf(lst,"%d",name_lsa->header->ls_type);
852 ccn_charbuf_append_string(name_lsdb_data,lst);
853 free(lst);
854 ccn_charbuf_append_string(name_lsdb_data,"|");
855
856 char *lsid=(char *)malloc(20);
857 memset(lsid,0,20);
858 sprintf(lsid,"%ld",name_lsa->header->ls_id);
859 ccn_charbuf_append_string(name_lsdb_data,lsid);
860 free(lsid);
861 ccn_charbuf_append_string(name_lsdb_data,"|");
862
863 ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_time);
864 ccn_charbuf_append_string(name_lsdb_data,"|");
865
866 hashtb_next(e);
867 }
868
869 hashtb_end(e);
870
871}
872
873
874void
875get_adj_lsdb_summary(struct ccn_charbuf *adj_lsdb_data)
876{
877 printf("get_adj_lsdb_summary called \n");
878 int i, adj_lsdb_element;
879 struct alsa *adj_lsa;
880
881 struct hashtb_enumerator ee;
882 struct hashtb_enumerator *e = &ee;
883
884 hashtb_start(nlsr->lsdb->adj_lsdb, e);
885 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
886
887 for(i=0;i<adj_lsdb_element;i++)
888 {
889 adj_lsa=e->data;
890
891 ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_router->name);
892 ccn_charbuf_append_string(adj_lsdb_data,"|");
893
894 char *lst=(char *)malloc(20);
895 memset(lst,0,20);
896 sprintf(lst,"%d",adj_lsa->header->ls_type);
897 ccn_charbuf_append_string(adj_lsdb_data,lst);
898 free(lst);
899 ccn_charbuf_append_string(adj_lsdb_data,"|");
900
901 ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_time);
902 ccn_charbuf_append_string(adj_lsdb_data,"|");
903
904 hashtb_next(e);
905 }
906
907 hashtb_end(e);
908}
909
910
911void
912get_lsdb_summary(struct ccn_charbuf *lsdb_data)
913{
914 struct ccn_charbuf *name_lsdb_data=ccn_charbuf_create();
915 struct ccn_charbuf *adj_lsdb_data=ccn_charbuf_create();
916
917 get_name_lsdb_summary(name_lsdb_data);
918 get_adj_lsdb_summary(adj_lsdb_data);
919
920 long int num_lsa=get_name_lsdb_num_element() + get_adj_lsdb_num_element();
921 char *num_element=(char *)malloc(15);
922 memset(num_element,0,15);
923 sprintf(num_element,"%ld",num_lsa);
924
925 if( num_lsa > 0)
926 {
927 ccn_charbuf_append_string(lsdb_data,num_element);
928 ccn_charbuf_append_string(lsdb_data,"|");
929 }
930 if(name_lsdb_data->length>0)
931 {
932 char *data1=ccn_charbuf_as_string(name_lsdb_data);
933 ccn_charbuf_append_string(lsdb_data,(char *)data1);
934 }
935 if(adj_lsdb_data->length>0)
936 {
937 char *data2=ccn_charbuf_as_string(adj_lsdb_data);
938 ccn_charbuf_append_string(lsdb_data,(char *)data2);
939 }
940 ccn_charbuf_destroy(&name_lsdb_data);
941 ccn_charbuf_destroy(&adj_lsdb_data);
942 free(num_element);
943
944}
945
946int
947check_is_new_name_lsa(char *orig_router,char *lst,char *lsid,char *orig_time)
948{
949 int ret=0;
950 struct ccn_charbuf *key=ccn_charbuf_create();
951 ccn_charbuf_append_string(key,orig_router);
952 ccn_charbuf_append_string(key,"/");
953 ccn_charbuf_append_string(key,lst);
954 ccn_charbuf_append_string(key,"/");
955 ccn_charbuf_append_string(key,lsid);
956
957 int res;
958 struct nlsa *name_lsa;
959
960 struct hashtb_enumerator ee;
961 struct hashtb_enumerator *e = &ee;
962
963 hashtb_start(nlsr->lsdb->name_lsdb, e);
964 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
965
966 if( res == HT_NEW_ENTRY )
967 {
968 hashtb_delete(e);
969 ret=1;
970
971 }
972 else if(res == HT_OLD_ENTRY)
973 {
974 name_lsa=e->data;
975 if( strcmp ( orig_time , name_lsa->header->orig_time ) > 0 )
976 {
977 ret=1;
978 }
979 }
980
981 hashtb_end(e);
982
983 ccn_charbuf_destroy(&key);
984
985 return ret;
986}
987
988int
989check_is_new_adj_lsa(char *orig_router,char *lst,char *orig_time)
990{
991 int ret=0;
992 struct ccn_charbuf *key=ccn_charbuf_create();
993 ccn_charbuf_append_string(key,orig_router);
994 ccn_charbuf_append_string(key,"/");
995 ccn_charbuf_append_string(key,lst);
996
997 int res;
998 struct alsa *adj_lsa;
999
1000 struct hashtb_enumerator ee;
1001 struct hashtb_enumerator *e = &ee;
1002
1003 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1004 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1005
1006 if( res == HT_NEW_ENTRY )
1007 {
1008 hashtb_delete(e);
1009 ret=1;
1010
1011 }
1012 else if(res == HT_OLD_ENTRY)
1013 {
1014 adj_lsa=e->data;
1015 if( strcmp ( orig_time , adj_lsa->header->orig_time ) > 0 )
1016 {
1017 ret=1;
1018 }
1019 }
1020
1021 hashtb_end(e);
1022
1023 ccn_charbuf_destroy(&key);
1024
1025 return ret;
1026}
1027
1028void
1029get_name_lsa_data(struct ccn_charbuf *lsa_data, struct name_prefix *lsaId)
1030{
1031 printf("get_name_lsa_data called \n");
1032
1033 struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa ));
1034
1035 struct hashtb_enumerator ee;
1036 struct hashtb_enumerator *e = &ee;
1037 int res;
1038
1039 hashtb_start(nlsr->lsdb->name_lsdb, e);
1040 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1041
1042 if( res == HT_OLD_ENTRY )
1043 {
1044 name_lsa=e->data;
1045 printf("NAME LSA found\n");
1046
1047 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_router->name);
1048 ccn_charbuf_append_string(lsa_data,"|");
1049
1050 char *temp_length=(char *)malloc(20);
1051 memset(temp_length,0,20);
1052 sprintf(temp_length,"%d",name_lsa->header->orig_router->length);
1053 ccn_charbuf_append_string(lsa_data,temp_length);
1054 free(temp_length);
1055 ccn_charbuf_append_string(lsa_data,"|");
1056
1057 char *temp_ltype=(char *)malloc(20);
1058 memset(temp_ltype,0,20);
1059 sprintf(temp_ltype,"%d",name_lsa->header->ls_type);
1060 ccn_charbuf_append_string(lsa_data,temp_ltype);
1061 free(temp_ltype);
1062 ccn_charbuf_append_string(lsa_data,"|");
1063
1064 char *temp_lsid=(char *)malloc(20);
1065 memset(temp_lsid,0,20);
1066 sprintf(temp_lsid,"%ld",name_lsa->header->ls_id);
1067 ccn_charbuf_append_string(lsa_data,temp_lsid);
1068 free(temp_lsid);
1069 ccn_charbuf_append_string(lsa_data,"|");
1070
1071 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_time);
1072 ccn_charbuf_append_string(lsa_data,"|");
1073
1074 char *temp_valid=(char *)malloc(20);
1075 memset(temp_valid,0,20);
1076 sprintf(temp_valid,"%d",name_lsa->header->isValid);
1077 ccn_charbuf_append_string(lsa_data,temp_valid);
1078 free(temp_valid);
1079 ccn_charbuf_append_string(lsa_data,"|");
1080
1081 ccn_charbuf_append_string(lsa_data,name_lsa->name_prefix->name);
1082 ccn_charbuf_append_string(lsa_data,"|");
1083
1084 char *temp_npl=(char *)malloc(20);
1085 memset(temp_npl,0,20);
1086 sprintf(temp_npl,"%d",name_lsa->name_prefix->length);
1087 ccn_charbuf_append_string(lsa_data,temp_npl);
1088 free(temp_npl);
1089 ccn_charbuf_append_string(lsa_data,"|");
1090
1091 }
1092 else if(res == HT_NEW_ENTRY)
1093 {
1094 hashtb_delete(e);
1095 }
1096
1097 hashtb_end(e);
1098}
1099
1100void
1101get_adj_lsa_data(struct ccn_charbuf *lsa_data,struct name_prefix *lsaId)
1102{
1103 printf("get_adj_lsa_data called \n");
1104
1105 struct alsa *adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
1106
1107 struct hashtb_enumerator ee;
1108 struct hashtb_enumerator *e = &ee;
1109 int res;
1110
1111 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1112 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1113
1114 if( res == HT_OLD_ENTRY )
1115 {
1116 adj_lsa=e->data;
1117 printf("NAME LSA found\n");
1118
1119 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_router->name);
1120 ccn_charbuf_append_string(lsa_data,"|");
1121
1122 char *temp_length=(char *)malloc(20);
1123 memset(temp_length,0,20);
1124 sprintf(temp_length,"%d",adj_lsa->header->orig_router->length);
1125 ccn_charbuf_append_string(lsa_data,temp_length);
1126 free(temp_length);
1127 ccn_charbuf_append_string(lsa_data,"|");
1128
1129 char *temp_ltype=(char *)malloc(20);
1130 memset(temp_ltype,0,20);
1131 sprintf(temp_ltype,"%d",adj_lsa->header->ls_type);
1132 ccn_charbuf_append_string(lsa_data,temp_ltype);
1133 free(temp_ltype);
1134 ccn_charbuf_append_string(lsa_data,"|");
1135
1136 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_time);
1137 ccn_charbuf_append_string(lsa_data,"|");
1138
1139 char *temp_nl=(char *)malloc(20);
1140 memset(temp_nl,0,20);
1141 sprintf(temp_nl,"%d",adj_lsa->no_link);
1142 ccn_charbuf_append_string(lsa_data,temp_nl);
1143 free(temp_nl);
1144 ccn_charbuf_append_string(lsa_data,"|");
1145
1146 ccn_charbuf_append_string(lsa_data,adj_lsa->body);
1147
1148
1149 }
1150 else if(res == HT_NEW_ENTRY)
1151 {
1152 hashtb_delete(e);
1153 }
1154
1155 hashtb_end(e);
1156}
akmhoqueffacaa82012-09-13 17:48:30 -05001157
akmhoqueda5b6832012-09-13 22:33:55 -05001158int
1159delete_name_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1160{
1161 printf("delete_name_lsa called \n");
1162
1163 if(flags == CCN_SCHEDULE_CANCEL)
1164 {
1165 return -1;
1166 }
1167
1168
1169
1170 nlsr_lock();
1171
1172 printf("LSA Key: %s \n",(char *)ev->evdata);
1173
1174 struct nlsa *nlsa;
1175
1176 struct hashtb_enumerator ee;
1177 struct hashtb_enumerator *e = &ee;
1178
1179 int res;
1180
1181 hashtb_start(nlsr->lsdb->name_lsdb, e);
1182 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1183
1184 if( res == HT_OLD_ENTRY )
1185 {
1186 nlsa=e->data;
1187 delete_npt_entry(nlsa->header->orig_router->name, nlsa->name_prefix->name);
1188 hashtb_delete(e);
1189 }
1190 else if( res == HT_OLD_ENTRY )
1191 {
1192 nlsa=e->data;
1193 delete_npt_entry(nlsa->header->orig_router->name, nlsa->name_prefix->name);
1194 hashtb_delete(e);
1195 }
1196 hashtb_end(e);
1197
1198
1199 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1200 set_new_lsdb_version();
1201 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1202
1203 //print_name_lsdb();
1204
1205 nlsr_unlock();
1206
1207 return 0;
1208}
1209
1210int
1211delete_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1212{
1213 printf("delete_adj_lsa called \n");
1214
1215 if(flags == CCN_SCHEDULE_CANCEL)
1216 {
1217 return -1;
1218 }
1219 nlsr_lock();
1220
1221 printf("LSA Key: %s \n",(char *)ev->evdata);
1222
1223 struct hashtb_enumerator ee;
1224 struct hashtb_enumerator *e = &ee;
1225 int res;
1226
1227 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1228 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1229
1230 if( res == HT_OLD_ENTRY )
1231 {
1232 hashtb_delete(e);
1233 }
1234 else if( res == HT_OLD_ENTRY )
1235 {
1236 hashtb_delete(e);
1237 }
1238 hashtb_end(e);
1239
1240 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1241 set_new_lsdb_version();
1242 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1243
1244 if ( !nlsr->is_route_calculation_scheduled)
1245 {
1246 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
1247 nlsr->is_route_calculation_scheduled=1;
1248 }
1249
1250 //print_adj_lsdb();
1251
1252 nlsr_unlock();
1253
1254 return 0;
1255}
1256
akmhoqueffacaa82012-09-13 17:48:30 -05001257void
1258refresh_name_lsdb(void)
1259{
1260 printf("refresh_name_lsdb called \n");
1261
akmhoqueda5b6832012-09-13 22:33:55 -05001262 //int lsa_change_count=0;
1263
akmhoqueffacaa82012-09-13 17:48:30 -05001264 char *time_stamp=(char *)malloc(20);
1265 memset(time_stamp,0,20);
1266 get_current_timestamp_micro(time_stamp);
1267
1268 long int lsa_life_time;
1269
1270 int i, name_lsdb_element;
1271 struct nlsa *name_lsa;
1272
1273 struct hashtb_enumerator ee;
1274 struct hashtb_enumerator *e = &ee;
1275
1276 hashtb_start(nlsr->lsdb->name_lsdb, e);
1277 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
1278
1279 for(i=0;i<name_lsdb_element;i++)
1280 {
1281 name_lsa=e->data;
1282
1283 lsa_life_time=get_time_diff(time_stamp,name_lsa->header->orig_time);
akmhoqueda5b6832012-09-13 22:33:55 -05001284 printf("LSA Life Time: %ld \n",lsa_life_time);
1285
1286 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name) == 0)
1287 {
1288 if ( lsa_life_time > nlsr->lsa_refresh_time )
1289 {
akmhoque14b3f342012-09-14 10:39:02 -05001290 if ( name_lsa->header->isValid == NAME_LSA_VALID )
1291 {
1292 printf("Own Name LSA need to be refrshed\n");
1293 char *current_time_stamp=(char *)malloc(20);
1294 memset(current_time_stamp,0,20);
1295 get_current_timestamp_micro(current_time_stamp);
akmhoqueda5b6832012-09-13 22:33:55 -05001296
akmhoque14b3f342012-09-14 10:39:02 -05001297 free(name_lsa->header->orig_time);
1298 name_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1299 memset(name_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1300 memcpy(name_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
akmhoqueda5b6832012-09-13 22:33:55 -05001301
akmhoque14b3f342012-09-14 10:39:02 -05001302 free(current_time_stamp);
1303 }
1304 else
1305 {
1306 char lst[2];
1307 memset(lst,0,2);
1308 sprintf(lst,"%d",name_lsa->header->ls_type);
1309
1310 char lsid[10];
1311 memset(lsid,0,10);
1312 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1313
1314
1315 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1316 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1317
1318
1319 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
1320 printf("Key:%s Length:%d\n",key,(int)strlen(key));
1321
1322 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
1323 }
akmhoqueda5b6832012-09-13 22:33:55 -05001324
1325 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1326 set_new_lsdb_version();
1327 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1328
1329 print_name_lsdb();
1330 //lsa_change_count++;
1331 }
1332 }
1333 else
1334 {
1335 if ( lsa_life_time > nlsr->router_dead_interval )
1336 {
1337 printf("Others Name LSA need to be deleted\n");
1338
1339 char lst[2];
1340 memset(lst,0,2);
1341 sprintf(lst,"%d",name_lsa->header->ls_type);
1342
1343 char lsid[10];
1344 memset(lsid,0,10);
1345 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1346
1347
1348 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1349 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1350
1351
1352 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
1353 printf("Key:%s Length:%d\n",key,(int)strlen(key));
1354
1355 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
1356 //lsa_change_count++;
1357 }
1358 }
akmhoqueffacaa82012-09-13 17:48:30 -05001359
1360 hashtb_next(e);
1361 }
1362
1363 hashtb_end(e);
1364
1365 free(time_stamp);
akmhoqueda5b6832012-09-13 22:33:55 -05001366
akmhoqueffacaa82012-09-13 17:48:30 -05001367
1368}
1369
akmhoqueda5b6832012-09-13 22:33:55 -05001370void
akmhoqueffacaa82012-09-13 17:48:30 -05001371refresh_adj_lsdb(void)
1372{
1373 printf("refresh_adj_lsdb called \n");
1374
akmhoqueda5b6832012-09-13 22:33:55 -05001375
akmhoqueffacaa82012-09-13 17:48:30 -05001376 char *time_stamp=(char *)malloc(20);
1377 memset(time_stamp,0,20);
1378 get_current_timestamp_micro(time_stamp);
1379
1380 long int lsa_life_time;
1381
1382 int i, adj_lsdb_element;
1383 struct alsa *adj_lsa;
1384
1385 struct hashtb_enumerator ee;
1386 struct hashtb_enumerator *e = &ee;
1387
1388 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1389 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1390
1391 for(i=0;i<adj_lsdb_element;i++)
1392 {
1393 adj_lsa=e->data;
1394
1395 lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
1396 printf("LSA Life Time: %ld \n",lsa_life_time);
1397
akmhoqueda5b6832012-09-13 22:33:55 -05001398 if ( strcmp(adj_lsa->header->orig_router->name,nlsr->router_name) == 0)
1399 {
1400 if ( lsa_life_time > nlsr->lsa_refresh_time )
1401 {
1402 printf("Own Adj LSA need to be refrshed\n");
1403
1404 char *current_time_stamp=(char *)malloc(20);
1405 memset(current_time_stamp,0,20);
1406 get_current_timestamp_micro(current_time_stamp);
1407
1408 free(adj_lsa->header->orig_time);
1409 adj_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1410 memset(adj_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1411 memcpy(adj_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1412
1413 free(current_time_stamp);
1414
1415
1416 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1417 set_new_lsdb_version();
1418 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1419
1420 print_adj_lsdb();
1421 }
1422 }
1423 else
1424 {
1425 if ( lsa_life_time > nlsr->router_dead_interval )
1426 {
1427 printf("Others Adj LSA need to be deleted\n");
1428
1429 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
1430 memset(key,0,adj_lsa->header->orig_router->length+2);
1431 make_adj_lsa_key(key,adj_lsa);
1432 printf("Adjacent LSA key: %s \n",key);
akmhoqueda5b6832012-09-13 22:33:55 -05001433 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_adj_lsa, (void *)key, 0);
1434 }
1435 }
1436
1437
1438
akmhoqueffacaa82012-09-13 17:48:30 -05001439 hashtb_next(e);
1440 }
1441
1442 hashtb_end(e);
1443
1444 free(time_stamp);
akmhoqueffacaa82012-09-13 17:48:30 -05001445}
1446
1447int
1448refresh_lsdb(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1449{
1450 if(flags == CCN_SCHEDULE_CANCEL)
1451 {
1452 return -1;
1453 }
1454
1455 nlsr_lock();
akmhoqueda5b6832012-09-13 22:33:55 -05001456
akmhoqueffacaa82012-09-13 17:48:30 -05001457 printf("refresh_lsdb called \n");
1458
1459 refresh_name_lsdb();
1460 refresh_adj_lsdb();
1461
akmhoqueffacaa82012-09-13 17:48:30 -05001462 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoqueda5b6832012-09-13 22:33:55 -05001463
akmhoqueffacaa82012-09-13 17:48:30 -05001464 nlsr_unlock();
1465 return 0;
1466}