blob: 16a7f30feba90a3eda1ed35d6d4adc03b0fbe445 [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
akmhoquede61ba92012-09-20 22:19:12 -0500206 //struct hashtb *face_list;
207 int num_next_hop=get_number_of_next_hop(new_name_lsa->header->orig_router->name);
akmhoqued5152122012-09-19 06:44:23 -0500208
209 //printf("Next hop: %d \n",next_hop);
210
akmhoquede61ba92012-09-20 22:19:12 -0500211 if ( num_next_hop < 0 )
akmhoque3560cb62012-09-09 10:52:30 -0500212 {
akmhoquede61ba92012-09-20 22:19:12 -0500213 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,NO_NEXT_HOP,NULL,NULL);
akmhoqueffacaa82012-09-13 17:48:30 -0500214 if ( check == HT_NEW_ENTRY )
215 {
216 printf("Added in npt \n");
217 }
akmhoque3560cb62012-09-09 10:52:30 -0500218 }
akmhoqueffacaa82012-09-13 17:48:30 -0500219 else
akmhoque3560cb62012-09-09 10:52:30 -0500220 {
akmhoquede61ba92012-09-20 22:19:12 -0500221 int *faces=malloc(num_next_hop*sizeof(int));
222 int *route_costs=malloc(num_next_hop*sizeof(int));
223 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name,faces,route_costs);
224 printf("Printing from install_name_lsa \n");
225 int j;
226 for(j=0;j<num_next_hop;j++)
227 printf("Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
228 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,next_hop,faces,route_costs);
akmhoqueffacaa82012-09-13 17:48:30 -0500229 if ( check == HT_NEW_ENTRY )
230 {
231 printf("Added in npt \n");
232 }
akmhoquede61ba92012-09-20 22:19:12 -0500233 free(faces);
234 free(route_costs);
akmhoqueffacaa82012-09-13 17:48:30 -0500235
akmhoque3560cb62012-09-09 10:52:30 -0500236 }
akmhoquede61ba92012-09-20 22:19:12 -0500237
akmhoque3560cb62012-09-09 10:52:30 -0500238
akmhoqueffacaa82012-09-13 17:48:30 -0500239
akmhoquede61ba92012-09-20 22:19:12 -0500240 free(time_stamp);
akmhoqueffacaa82012-09-13 17:48:30 -0500241
242 }
243 else if(res == HT_OLD_ENTRY)
244 {
245 new_name_lsa=e->data;
akmhoque14b3f342012-09-14 10:39:02 -0500246 if(strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time)<0)
akmhoqueffacaa82012-09-13 17:48:30 -0500247 {
akmhoque14b3f342012-09-14 10:39:02 -0500248 printf("Older Adj LSA. Discarded...\n");
249 }
250 else if( strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time) == 0 )
251 {
252 printf("Duplicate Adj LSA. Discarded...\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500253 }
254 else
255 {
256 if ( name_lsa->header->isValid == 0 )
257 {
258 // have to call to delete npt table entry
259 delete_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name);
260
261 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name)!= 0)
262 {
263 hashtb_delete(e);
264 printf("isValid bit not set for Router %s so LSA Deleted from LSDB\n",name_lsa->header->orig_router->name);
265 }
266 else
267 {
268 new_name_lsa->header->isValid=name_lsa->header->isValid;
269 free(new_name_lsa->header->orig_time);
270 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
271 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
272 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
273 }
274 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
275 set_new_lsdb_version();
276 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
277 }
278 else
279 {
280 int is_npt_update=0;
281 if ( strcmp(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name) != 0 )
282 {
283 is_npt_update=1;
284 delete_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name);
285 }
286
287 // copying LSA content with header
288
289 free(new_name_lsa->header->orig_time);
290 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
291 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
292 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
293
294 new_name_lsa->header->isValid=name_lsa->header->isValid;
295
296 free(new_name_lsa->name_prefix->name);
297 free(new_name_lsa->name_prefix);
298 new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
299 new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
300 memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
301 new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
302
303 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
304 set_new_lsdb_version();
305 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
306
307 if( is_npt_update == 1 )
308 {
akmhoquede61ba92012-09-20 22:19:12 -0500309 //struct hashtb *face_list;
310 int num_next_hop=get_number_of_next_hop(new_name_lsa->header->orig_router->name);
311 if ( num_next_hop < 0 )
akmhoqueffacaa82012-09-13 17:48:30 -0500312 {
akmhoquede61ba92012-09-20 22:19:12 -0500313
314 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,NO_NEXT_HOP,NULL,NULL);
akmhoqueffacaa82012-09-13 17:48:30 -0500315 if ( check == HT_NEW_ENTRY )
316 {
317 printf("Added in npt \n");
318 }
319 }
320 else
321 {
akmhoquede61ba92012-09-20 22:19:12 -0500322 int *faces=malloc(num_next_hop*sizeof(int));
323 int *route_costs=malloc(num_next_hop*sizeof(int));
324 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name,faces,route_costs);
325 printf("Printing from install_name_lsa \n");
326 int j;
327 for(j=0;j<num_next_hop;j++)
328 printf("Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
329
330 int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,next_hop,faces,route_costs);
akmhoqueffacaa82012-09-13 17:48:30 -0500331 if ( check == HT_NEW_ENTRY )
332 {
333 printf("Added in npt \n");
334 }
akmhoquede61ba92012-09-20 22:19:12 -0500335 free(faces);
336 free(route_costs);
337
akmhoqueffacaa82012-09-13 17:48:30 -0500338 }
akmhoquede61ba92012-09-20 22:19:12 -0500339
akmhoqueffacaa82012-09-13 17:48:30 -0500340 }
341 }
342 }
343
akmhoque3560cb62012-09-09 10:52:30 -0500344 }
akmhoque53f64222012-09-05 13:57:51 -0500345
akmhoqueffacaa82012-09-13 17:48:30 -0500346 hashtb_end(e);
akmhoque53f64222012-09-05 13:57:51 -0500347
akmhoqueffacaa82012-09-13 17:48:30 -0500348 free(key);
akmhoque53f64222012-09-05 13:57:51 -0500349}
350
351
akmhoque03004e62012-09-06 01:12:28 -0500352void
353print_name_lsa(struct nlsa *name_lsa)
akmhoque53f64222012-09-05 13:57:51 -0500354{
355
akmhoque03004e62012-09-06 01:12:28 -0500356 printf("-----------Name LSA Content---------------\n");
357 printf(" Origination Router : %s\n",name_lsa->header->orig_router->name);
358 printf(" Origination Router Length: %d\n",name_lsa->header->orig_router->length);
359 printf(" LS Type : %d\n",name_lsa->header->ls_type);
360 printf(" LS Id : %ld\n",name_lsa->header->ls_id);
361 printf(" Origination Time : %s\n",name_lsa->header->orig_time);
362 printf(" Is Valid : %d\n",name_lsa->header->isValid);
363 printf(" LSA Data \n");
364 printf(" Name Prefix: : %s\n",name_lsa->name_prefix->name);
365 printf(" Name Prefix Length : %d\n",name_lsa->name_prefix->length);
akmhoque53f64222012-09-05 13:57:51 -0500366
akmhoque03004e62012-09-06 01:12:28 -0500367 printf("\n");
akmhoquef71d9082012-08-22 12:51:53 -0400368}
369
370void
371print_name_lsdb(void)
372{
373 printf("print_name_lsdb called \n");
akmhoque53f64222012-09-05 13:57:51 -0500374 int i, name_lsdb_element;
akmhoquef71d9082012-08-22 12:51:53 -0400375 struct nlsa *name_lsa;
376
377 struct hashtb_enumerator ee;
378 struct hashtb_enumerator *e = &ee;
379
380 hashtb_start(nlsr->lsdb->name_lsdb, e);
akmhoque53f64222012-09-05 13:57:51 -0500381 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
akmhoquef71d9082012-08-22 12:51:53 -0400382
akmhoque53f64222012-09-05 13:57:51 -0500383 for(i=0;i<name_lsdb_element;i++)
akmhoquef71d9082012-08-22 12:51:53 -0400384 {
akmhoque53f64222012-09-05 13:57:51 -0500385 printf("-----------Name LSA (%d)---------------\n",i+1);
akmhoquef71d9082012-08-22 12:51:53 -0400386 name_lsa=e->data;
akmhoque53f64222012-09-05 13:57:51 -0500387 print_name_lsa(name_lsa);
akmhoquef71d9082012-08-22 12:51:53 -0400388 hashtb_next(e);
389 }
390
391 hashtb_end(e);
392
akmhoque53f64222012-09-05 13:57:51 -0500393 printf("\n");
394}
395
akmhoque03004e62012-09-06 01:12:28 -0500396
397void
398build_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 -0500399{
akmhoque03004e62012-09-06 01:12:28 -0500400 printf("build_and_install_others_name_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500401
akmhoque03004e62012-09-06 01:12:28 -0500402 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
403 build_others_name_lsa(name_lsa,orig_router,ls_type,ls_id,orig_time, isValid,np);
404 print_name_lsa(name_lsa);
405 install_name_lsa(name_lsa);
406 print_name_lsdb();
akmhoquede61ba92012-09-20 22:19:12 -0500407 print_npt();
akmhoque53f64222012-09-05 13:57:51 -0500408
akmhoque03004e62012-09-06 01:12:28 -0500409 free(name_lsa->header->orig_router->name);
410 free(name_lsa->header->orig_router);
411 free(name_lsa->header);
412 free(name_lsa->name_prefix->name);
413 free(name_lsa->name_prefix);
414 free(name_lsa);
akmhoque53f64222012-09-05 13:57:51 -0500415
416}
417
418void
akmhoque03004e62012-09-06 01:12:28 -0500419build_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 -0500420{
akmhoque03004e62012-09-06 01:12:28 -0500421 printf("build_others_name_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500422
akmhoque03004e62012-09-06 01:12:28 -0500423 name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header ));
424 name_lsa->header->ls_type=LS_TYPE_NAME;
akmhoque53f64222012-09-05 13:57:51 -0500425
akmhoque03004e62012-09-06 01:12:28 -0500426 name_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
427 memset(name_lsa->header->orig_time,0,strlen(orig_time)+1);
428 memcpy(name_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
akmhoque53f64222012-09-05 13:57:51 -0500429
akmhoque03004e62012-09-06 01:12:28 -0500430 name_lsa->header->ls_id=ls_id;
431 name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
432 name_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
433 memset(name_lsa->header->orig_router->name,0,strlen(orig_router)+1);
434 memcpy(name_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
435 name_lsa->header->orig_router->length=strlen(orig_router)+1;
436 name_lsa->header->isValid=isValid;
akmhoque53f64222012-09-05 13:57:51 -0500437
akmhoque03004e62012-09-06 01:12:28 -0500438 name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
439 name_lsa->name_prefix->name=(char *)malloc(strlen(np)+1);
440 memset(name_lsa->name_prefix->name,0,strlen(np)+1);
441 memcpy(name_lsa->name_prefix->name,np,strlen(np)+1);
442 name_lsa->name_prefix->length=strlen(np)+1;
akmhoquef71d9082012-08-22 12:51:53 -0400443}
akmhoqued79438d2012-08-27 13:31:42 -0500444
445
akmhoqueffacaa82012-09-13 17:48:30 -0500446
447
akmhoque53f64222012-09-05 13:57:51 -0500448void
449make_adj_lsa_key(char *key,struct alsa *adj_lsa)
akmhoqued79438d2012-08-27 13:31:42 -0500450{
akmhoque53f64222012-09-05 13:57:51 -0500451 memcpy(key+strlen(key),adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
452 memcpy(key+strlen(key),"/",1);
453 char ls_type[2];
454 sprintf(ls_type,"%d",adj_lsa->header->ls_type);
455 memcpy(key+strlen(key),ls_type,strlen(ls_type));
456 key[strlen(key)]='\0';
457}
458
akmhoque03004e62012-09-06 01:12:28 -0500459int
460build_and_install_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
461{
akmhoqueffacaa82012-09-13 17:48:30 -0500462 if(flags == CCN_SCHEDULE_CANCEL)
463 {
464 return -1;
465 }
466
467 nlsr_lock();
468
akmhoque03004e62012-09-06 01:12:28 -0500469 printf("build_and_install_adj_lsa called \n");
470
471 printf("adj_build_flag = %d \n",nlsr->adj_build_flag);
472
473 if(nlsr->adj_build_flag > 0)
474 {
475 printf("is_adj_lsa_build = %d \n",is_adj_lsa_build());
476 if ( is_adj_lsa_build()> 0)
477 {
478 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
479 build_adj_lsa(adj_lsa);
480 install_adj_lsa(adj_lsa);
481
482 free(adj_lsa->header->orig_router->name);
483 free(adj_lsa->header->orig_router);
484 free(adj_lsa->header->orig_time);
485 free(adj_lsa->header);
486 free(adj_lsa->body);
487 free(adj_lsa);
488 nlsr->adj_build_flag=0;
489 print_adj_lsdb();
490 }
491 else
492 {
493 printf("Can not build adj LSA now\n");
494 }
495 }
496 nlsr->is_build_adj_lsa_sheduled=0;
akmhoqueffacaa82012-09-13 17:48:30 -0500497
498 nlsr_unlock();
499
akmhoque03004e62012-09-06 01:12:28 -0500500 return 0;
501}
502
503
504void
505build_adj_lsa(struct alsa * adj_lsa)
506{
507 printf("build_adj_lsa called \n");
508
509 int no_link=no_active_nbr();
510 printf("Number of link in Adjacent LSA: %d\n",no_link);
511
512 /*Filling Up Header Data */
513 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
514 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
515 adj_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1);
516 memset(adj_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
517 memcpy(adj_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
518 adj_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
519
520 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
521
522 char *time_stamp=(char *)malloc(20);
523 memset(time_stamp,0,20);
524 get_current_timestamp_micro(time_stamp);
525
526 adj_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1);
527 memset(adj_lsa->header->orig_time,0,strlen(time_stamp)+1);
528 memcpy(adj_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
529 free(time_stamp);
530
531
532 /* Filling Up Body Data */
533
534 adj_lsa->no_link=no_link;
akmhoque03004e62012-09-06 01:12:28 -0500535
536
537 struct ccn_charbuf *c=ccn_charbuf_create();
538 get_active_nbr_adj_data(c);
539 char *data=ccn_charbuf_as_string(c);
540
541 adj_lsa->body=(char *)malloc(strlen(data)+1);
542 memset(adj_lsa->body,0,strlen(data)+1);
543 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
544 ccn_charbuf_destroy(&c);
545
546
547
548 if( !nlsr->is_send_lsdb_interest_scheduled )
549 {
550 nlsr->event_send_lsdb_interest= ccn_schedule_event(nlsr->sched, 1000, &send_lsdb_interest, NULL, 0);
551 nlsr->is_send_lsdb_interest_scheduled=1;
552 }
553
554 nlsr->adj_build_count++;
555
556
557}
558
559
akmhoque53f64222012-09-05 13:57:51 -0500560void
561install_adj_lsa(struct alsa * adj_lsa)
562{
akmhoqued79438d2012-08-27 13:31:42 -0500563 printf("install_adj_lsa called \n");
564
akmhoque03004e62012-09-06 01:12:28 -0500565
akmhoqueffacaa82012-09-13 17:48:30 -0500566 char *time_stamp=(char *)malloc(20);
567 memset(time_stamp,0,20);
568 get_current_timestamp_micro(time_stamp);
569 long int lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
akmhoque53f64222012-09-05 13:57:51 -0500570
akmhoqueffacaa82012-09-13 17:48:30 -0500571 printf("time difference: %ld \n",lsa_life_time);
akmhoque53f64222012-09-05 13:57:51 -0500572
akmhoqueffacaa82012-09-13 17:48:30 -0500573
574 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
575 memset(key,0,adj_lsa->header->orig_router->length+2);
576 make_adj_lsa_key(key,adj_lsa);
577 printf("Adjacent LSA key: %s \n",key);
578
579 struct alsa *new_adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
580
581 struct hashtb_enumerator ee;
582 struct hashtb_enumerator *e = &ee;
583 int res;
584
585 hashtb_start(nlsr->lsdb->adj_lsdb, e);
586 res = hashtb_seek(e, key, strlen(key), 0);
akmhoque53f64222012-09-05 13:57:51 -0500587
akmhoque03004e62012-09-06 01:12:28 -0500588
akmhoque53f64222012-09-05 13:57:51 -0500589
akmhoqueffacaa82012-09-13 17:48:30 -0500590 if(res == HT_NEW_ENTRY )
akmhoque53f64222012-09-05 13:57:51 -0500591 {
akmhoqueffacaa82012-09-13 17:48:30 -0500592 printf("New ADJ LSA... Adding to LSDB\n");
akmhoque53f64222012-09-05 13:57:51 -0500593 new_adj_lsa = e->data;
594
akmhoqueffacaa82012-09-13 17:48:30 -0500595 new_adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
596 new_adj_lsa->header->ls_type=adj_lsa->header->ls_type;
akmhoque53f64222012-09-05 13:57:51 -0500597 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
akmhoqueffacaa82012-09-13 17:48:30 -0500598 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
599
600 new_adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
601 new_adj_lsa->header->orig_router->name=(char *)malloc(adj_lsa->header->orig_router->length);
602 memcpy(new_adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
603 new_adj_lsa->header->orig_router->length=adj_lsa->header->orig_router->length;
akmhoque53f64222012-09-05 13:57:51 -0500604
akmhoque03004e62012-09-06 01:12:28 -0500605 new_adj_lsa->no_link=adj_lsa->no_link;
akmhoqueffacaa82012-09-13 17:48:30 -0500606
akmhoque53f64222012-09-05 13:57:51 -0500607 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
608 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
609 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
610
akmhoque03004e62012-09-06 01:12:28 -0500611 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500612 set_new_lsdb_version();
akmhoque03004e62012-09-06 01:12:28 -0500613 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500614
akmhoquede61ba92012-09-20 22:19:12 -0500615
akmhoqueffacaa82012-09-13 17:48:30 -0500616 add_next_hop_router(new_adj_lsa->header->orig_router->name);
akmhoqueffacaa82012-09-13 17:48:30 -0500617 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
618 }
619 else if(res == HT_OLD_ENTRY)
620 {
621 new_adj_lsa = e->data;
622 if(strcmp(adj_lsa->header->orig_time,new_adj_lsa->header->orig_time)<=0)
623 {
624 printf("Older/Duplicate Adj LSA. Discarded...\n");
625 }
626 else
627 {
628 new_adj_lsa = e->data;
629
630 free(new_adj_lsa->header->orig_time);
631 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
632 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
633
634 new_adj_lsa->no_link=adj_lsa->no_link;
635
636 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
637 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
638 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
639
640 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
641 set_new_lsdb_version();
642 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
643
644 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
645
646 }
647
648 }
649 hashtb_end(e);
650
651 if ( !nlsr->is_route_calculation_scheduled )
652 {
653 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
654 nlsr->is_route_calculation_scheduled=1;
akmhoque53f64222012-09-05 13:57:51 -0500655 }
656
akmhoque03004e62012-09-06 01:12:28 -0500657
akmhoqueffacaa82012-09-13 17:48:30 -0500658 free(key);
659
660 free(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -0500661}
662
663void
664print_adj_lsa_body(const char *body, int no_link)
665{
666 int i=0;
667 char *lsa_data=(char *)malloc(strlen(body)+1);
668 memset( lsa_data,0,strlen(body)+1);
669 memcpy(lsa_data,body,strlen(body)+1);
670 char *sep="|";
671 char *rem;
672 char *rtr_id;
673 char *length;
674 char *face;
675 char *metric;
676
akmhoque53f64222012-09-05 13:57:51 -0500677 if(no_link >0 )
678 {
679 rtr_id=strtok_r(lsa_data,sep,&rem);
680 length=strtok_r(NULL,sep,&rem);
681 face=strtok_r(NULL,sep,&rem);
682 metric=strtok_r(NULL,sep,&rem);
683
684 printf(" Link %d \n",i+1);
685 printf(" Neighbor : %s \n",rtr_id);
686 printf(" Neighbor Length : %s \n",length);
687 printf(" Connecting Face : %s \n",face);
688 printf(" Metric : %s \n",metric);
akmhoque03004e62012-09-06 01:12:28 -0500689
akmhoque53f64222012-09-05 13:57:51 -0500690
691 for(i=1;i<no_link;i++)
692 {
693 rtr_id=strtok_r(NULL,sep,&rem);
694 length=strtok_r(NULL,sep,&rem);
695 face=strtok_r(NULL,sep,&rem);
696 metric=strtok_r(NULL,sep,&rem);
697 printf(" Link %d \n",i+1);
698 printf(" Neighbor : %s \n",rtr_id);
699 printf(" Neighbor Length : %s \n",length);
700 printf(" Connecting Face : %s \n",face);
701 printf(" Metric : %s \n",metric);
702
703 }
704 }
705
706 free(lsa_data);
707}
708
709void
710print_adj_lsa(struct alsa * adj_lsa)
711{
akmhoque03004e62012-09-06 01:12:28 -0500712
akmhoque53f64222012-09-05 13:57:51 -0500713 printf("print_adj_lsa called \n");
akmhoque03004e62012-09-06 01:12:28 -0500714
akmhoque53f64222012-09-05 13:57:51 -0500715 printf("-----------ADJ LSA Content---------------\n");
716 printf(" Origination Router : %s\n",adj_lsa->header->orig_router->name);
717 printf(" Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
718 printf(" LS Type : %d\n",adj_lsa->header->ls_type);
719 printf(" Origination Time : %s\n",adj_lsa->header->orig_time);
akmhoque03004e62012-09-06 01:12:28 -0500720 printf(" Lsa Data:\n");
721 printf(" No of Link : %d\n",adj_lsa->no_link);
akmhoque53f64222012-09-05 13:57:51 -0500722
723 print_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
akmhoque53f64222012-09-05 13:57:51 -0500724
akmhoque03004e62012-09-06 01:12:28 -0500725/*
726 struct link *templ=adj_lsa->links;
727 int i;
728
729 for(i=0 ; i< adj_lsa->no_link ; i++)
730 {
731 printf(" Link %d \n",i+1);
732 printf(" Neighbor : %s \n",templ->nbr->name);
733 printf(" Neighbor Length : %d \n",templ->nbr->length);
734 printf(" Connecting Face : %d \n",templ->face);
735 printf(" Metric : %d \n",templ->metric);
736
737 templ++;
738 }
739*/
akmhoque53f64222012-09-05 13:57:51 -0500740 printf("\n");
741
742}
743
744void
745print_adj_lsdb(void)
746{
747 printf("print_name_lsdb called \n");
748 int i, adj_lsdb_element;
749 struct alsa *adj_lsa;
750
751 struct hashtb_enumerator ee;
752 struct hashtb_enumerator *e = &ee;
753
754 hashtb_start(nlsr->lsdb->adj_lsdb, e);
755 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
756
757 for(i=0;i<adj_lsdb_element;i++)
758 {
759 printf("-----------Adj LSA (%d)---------------\n",i+1);
760 adj_lsa=e->data;
761 print_adj_lsa(adj_lsa);
762 hashtb_next(e);
763 }
764
765 hashtb_end(e);
766
767 printf("\n");
akmhoque53f64222012-09-05 13:57:51 -0500768}
769
770void
akmhoque03004e62012-09-06 01:12:28 -0500771build_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 -0500772{
akmhoque03004e62012-09-06 01:12:28 -0500773 printf("build_and_install_others_adj_lsa called \n");
774 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
775 build_others_adj_lsa(adj_lsa,orig_router,ls_type,orig_time,no_link,data);
776 //print_adj_lsa(adj_lsa);
777 install_adj_lsa(adj_lsa);
778
akmhoque53f64222012-09-05 13:57:51 -0500779
akmhoque03004e62012-09-06 01:12:28 -0500780 free(adj_lsa->header->orig_router->name);
781 free(adj_lsa->header->orig_router);
782 free(adj_lsa->header->orig_time);
783 free(adj_lsa->header);
784 free(adj_lsa->body);
785 free(adj_lsa);
akmhoque53f64222012-09-05 13:57:51 -0500786
akmhoque03004e62012-09-06 01:12:28 -0500787 print_adj_lsdb();
akmhoque53f64222012-09-05 13:57:51 -0500788
akmhoque53f64222012-09-05 13:57:51 -0500789}
790
akmhoque03004e62012-09-06 01:12:28 -0500791
akmhoque53f64222012-09-05 13:57:51 -0500792void
akmhoque03004e62012-09-06 01:12:28 -0500793build_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 -0500794{
akmhoque03004e62012-09-06 01:12:28 -0500795 printf("build_others_adj_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500796
akmhoque03004e62012-09-06 01:12:28 -0500797 /*Filling Up Header Data */
798 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
799 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
800 adj_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
801 memset(adj_lsa->header->orig_router->name,0,strlen(orig_router)+1);
802 memcpy(adj_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
akmhoque53f64222012-09-05 13:57:51 -0500803
akmhoque03004e62012-09-06 01:12:28 -0500804 adj_lsa->header->orig_router->length=strlen(orig_router)+1;
akmhoque53f64222012-09-05 13:57:51 -0500805
akmhoque53f64222012-09-05 13:57:51 -0500806
akmhoque03004e62012-09-06 01:12:28 -0500807 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
akmhoque53f64222012-09-05 13:57:51 -0500808
akmhoque03004e62012-09-06 01:12:28 -0500809 adj_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
810 memset(adj_lsa->header->orig_time,0,strlen(orig_time)+1);
811 memcpy(adj_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
akmhoque53f64222012-09-05 13:57:51 -0500812
akmhoque03004e62012-09-06 01:12:28 -0500813 adj_lsa->no_link=no_link;
akmhoque53f64222012-09-05 13:57:51 -0500814
akmhoque03004e62012-09-06 01:12:28 -0500815 adj_lsa->body=(char *)malloc(strlen(data)+1);
816 memset(adj_lsa->body,0,strlen(data)+1);
817 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
akmhoque53f64222012-09-05 13:57:51 -0500818
akmhoque53f64222012-09-05 13:57:51 -0500819}
820
akmhoque03004e62012-09-06 01:12:28 -0500821
akmhoque53f64222012-09-05 13:57:51 -0500822long int
823get_name_lsdb_num_element(void)
824{
825 long int num_element;
826
827
828 struct hashtb_enumerator ee;
829 struct hashtb_enumerator *e = &ee;
830
831 hashtb_start(nlsr->lsdb->name_lsdb, e);
832 num_element=hashtb_n(nlsr->lsdb->name_lsdb);
833 hashtb_end(e);
834
835 return num_element;
836}
837
838long int
839get_adj_lsdb_num_element(void)
840{
841 long int num_element;
842
843
844 struct hashtb_enumerator ee;
845 struct hashtb_enumerator *e = &ee;
846
847 hashtb_start(nlsr->lsdb->adj_lsdb, e);
848 num_element=hashtb_n(nlsr->lsdb->adj_lsdb);
849 hashtb_end(e);
850
851 return num_element;
akmhoqued79438d2012-08-27 13:31:42 -0500852}
akmhoque03004e62012-09-06 01:12:28 -0500853
854void
855get_name_lsdb_summary(struct ccn_charbuf *name_lsdb_data)
856{
857 printf("get_name_lsdb_summary called \n");
858 int i, name_lsdb_element;
859
860 struct nlsa *name_lsa;
861 struct hashtb_enumerator ee;
862 struct hashtb_enumerator *e = &ee;
863
864 hashtb_start(nlsr->lsdb->name_lsdb, e);
865 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
866
867 for(i=0;i<name_lsdb_element;i++)
868 {
869 name_lsa=e->data;
870
871 ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_router->name);
872 ccn_charbuf_append_string(name_lsdb_data,"|");
873
874 char *lst=(char *)malloc(20);
875 memset(lst,0,20);
876 sprintf(lst,"%d",name_lsa->header->ls_type);
877 ccn_charbuf_append_string(name_lsdb_data,lst);
878 free(lst);
879 ccn_charbuf_append_string(name_lsdb_data,"|");
880
881 char *lsid=(char *)malloc(20);
882 memset(lsid,0,20);
883 sprintf(lsid,"%ld",name_lsa->header->ls_id);
884 ccn_charbuf_append_string(name_lsdb_data,lsid);
885 free(lsid);
886 ccn_charbuf_append_string(name_lsdb_data,"|");
887
888 ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_time);
889 ccn_charbuf_append_string(name_lsdb_data,"|");
890
891 hashtb_next(e);
892 }
893
894 hashtb_end(e);
895
896}
897
898
899void
900get_adj_lsdb_summary(struct ccn_charbuf *adj_lsdb_data)
901{
902 printf("get_adj_lsdb_summary called \n");
903 int i, adj_lsdb_element;
904 struct alsa *adj_lsa;
905
906 struct hashtb_enumerator ee;
907 struct hashtb_enumerator *e = &ee;
908
909 hashtb_start(nlsr->lsdb->adj_lsdb, e);
910 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
911
912 for(i=0;i<adj_lsdb_element;i++)
913 {
914 adj_lsa=e->data;
915
916 ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_router->name);
917 ccn_charbuf_append_string(adj_lsdb_data,"|");
918
919 char *lst=(char *)malloc(20);
920 memset(lst,0,20);
921 sprintf(lst,"%d",adj_lsa->header->ls_type);
922 ccn_charbuf_append_string(adj_lsdb_data,lst);
923 free(lst);
924 ccn_charbuf_append_string(adj_lsdb_data,"|");
925
926 ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_time);
927 ccn_charbuf_append_string(adj_lsdb_data,"|");
928
929 hashtb_next(e);
930 }
931
932 hashtb_end(e);
933}
934
935
936void
937get_lsdb_summary(struct ccn_charbuf *lsdb_data)
938{
939 struct ccn_charbuf *name_lsdb_data=ccn_charbuf_create();
940 struct ccn_charbuf *adj_lsdb_data=ccn_charbuf_create();
941
942 get_name_lsdb_summary(name_lsdb_data);
943 get_adj_lsdb_summary(adj_lsdb_data);
944
945 long int num_lsa=get_name_lsdb_num_element() + get_adj_lsdb_num_element();
946 char *num_element=(char *)malloc(15);
947 memset(num_element,0,15);
948 sprintf(num_element,"%ld",num_lsa);
949
950 if( num_lsa > 0)
951 {
952 ccn_charbuf_append_string(lsdb_data,num_element);
953 ccn_charbuf_append_string(lsdb_data,"|");
954 }
955 if(name_lsdb_data->length>0)
956 {
957 char *data1=ccn_charbuf_as_string(name_lsdb_data);
958 ccn_charbuf_append_string(lsdb_data,(char *)data1);
959 }
960 if(adj_lsdb_data->length>0)
961 {
962 char *data2=ccn_charbuf_as_string(adj_lsdb_data);
963 ccn_charbuf_append_string(lsdb_data,(char *)data2);
964 }
965 ccn_charbuf_destroy(&name_lsdb_data);
966 ccn_charbuf_destroy(&adj_lsdb_data);
967 free(num_element);
968
969}
970
971int
972check_is_new_name_lsa(char *orig_router,char *lst,char *lsid,char *orig_time)
973{
974 int ret=0;
975 struct ccn_charbuf *key=ccn_charbuf_create();
976 ccn_charbuf_append_string(key,orig_router);
977 ccn_charbuf_append_string(key,"/");
978 ccn_charbuf_append_string(key,lst);
979 ccn_charbuf_append_string(key,"/");
980 ccn_charbuf_append_string(key,lsid);
981
982 int res;
983 struct nlsa *name_lsa;
984
985 struct hashtb_enumerator ee;
986 struct hashtb_enumerator *e = &ee;
987
988 hashtb_start(nlsr->lsdb->name_lsdb, e);
989 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
990
991 if( res == HT_NEW_ENTRY )
992 {
993 hashtb_delete(e);
994 ret=1;
995
996 }
997 else if(res == HT_OLD_ENTRY)
998 {
999 name_lsa=e->data;
1000 if( strcmp ( orig_time , name_lsa->header->orig_time ) > 0 )
1001 {
1002 ret=1;
1003 }
1004 }
1005
1006 hashtb_end(e);
1007
1008 ccn_charbuf_destroy(&key);
1009
1010 return ret;
1011}
1012
1013int
1014check_is_new_adj_lsa(char *orig_router,char *lst,char *orig_time)
1015{
1016 int ret=0;
1017 struct ccn_charbuf *key=ccn_charbuf_create();
1018 ccn_charbuf_append_string(key,orig_router);
1019 ccn_charbuf_append_string(key,"/");
1020 ccn_charbuf_append_string(key,lst);
1021
1022 int res;
1023 struct alsa *adj_lsa;
1024
1025 struct hashtb_enumerator ee;
1026 struct hashtb_enumerator *e = &ee;
1027
1028 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1029 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1030
1031 if( res == HT_NEW_ENTRY )
1032 {
1033 hashtb_delete(e);
1034 ret=1;
1035
1036 }
1037 else if(res == HT_OLD_ENTRY)
1038 {
1039 adj_lsa=e->data;
1040 if( strcmp ( orig_time , adj_lsa->header->orig_time ) > 0 )
1041 {
1042 ret=1;
1043 }
1044 }
1045
1046 hashtb_end(e);
1047
1048 ccn_charbuf_destroy(&key);
1049
1050 return ret;
1051}
1052
1053void
1054get_name_lsa_data(struct ccn_charbuf *lsa_data, struct name_prefix *lsaId)
1055{
1056 printf("get_name_lsa_data called \n");
1057
1058 struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa ));
1059
1060 struct hashtb_enumerator ee;
1061 struct hashtb_enumerator *e = &ee;
1062 int res;
1063
1064 hashtb_start(nlsr->lsdb->name_lsdb, e);
1065 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1066
1067 if( res == HT_OLD_ENTRY )
1068 {
1069 name_lsa=e->data;
1070 printf("NAME LSA found\n");
1071
1072 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_router->name);
1073 ccn_charbuf_append_string(lsa_data,"|");
1074
1075 char *temp_length=(char *)malloc(20);
1076 memset(temp_length,0,20);
1077 sprintf(temp_length,"%d",name_lsa->header->orig_router->length);
1078 ccn_charbuf_append_string(lsa_data,temp_length);
1079 free(temp_length);
1080 ccn_charbuf_append_string(lsa_data,"|");
1081
1082 char *temp_ltype=(char *)malloc(20);
1083 memset(temp_ltype,0,20);
1084 sprintf(temp_ltype,"%d",name_lsa->header->ls_type);
1085 ccn_charbuf_append_string(lsa_data,temp_ltype);
1086 free(temp_ltype);
1087 ccn_charbuf_append_string(lsa_data,"|");
1088
1089 char *temp_lsid=(char *)malloc(20);
1090 memset(temp_lsid,0,20);
1091 sprintf(temp_lsid,"%ld",name_lsa->header->ls_id);
1092 ccn_charbuf_append_string(lsa_data,temp_lsid);
1093 free(temp_lsid);
1094 ccn_charbuf_append_string(lsa_data,"|");
1095
1096 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_time);
1097 ccn_charbuf_append_string(lsa_data,"|");
1098
1099 char *temp_valid=(char *)malloc(20);
1100 memset(temp_valid,0,20);
1101 sprintf(temp_valid,"%d",name_lsa->header->isValid);
1102 ccn_charbuf_append_string(lsa_data,temp_valid);
1103 free(temp_valid);
1104 ccn_charbuf_append_string(lsa_data,"|");
1105
1106 ccn_charbuf_append_string(lsa_data,name_lsa->name_prefix->name);
1107 ccn_charbuf_append_string(lsa_data,"|");
1108
1109 char *temp_npl=(char *)malloc(20);
1110 memset(temp_npl,0,20);
1111 sprintf(temp_npl,"%d",name_lsa->name_prefix->length);
1112 ccn_charbuf_append_string(lsa_data,temp_npl);
1113 free(temp_npl);
1114 ccn_charbuf_append_string(lsa_data,"|");
1115
1116 }
1117 else if(res == HT_NEW_ENTRY)
1118 {
1119 hashtb_delete(e);
1120 }
1121
1122 hashtb_end(e);
1123}
1124
1125void
1126get_adj_lsa_data(struct ccn_charbuf *lsa_data,struct name_prefix *lsaId)
1127{
1128 printf("get_adj_lsa_data called \n");
1129
1130 struct alsa *adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
1131
1132 struct hashtb_enumerator ee;
1133 struct hashtb_enumerator *e = &ee;
1134 int res;
1135
1136 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1137 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1138
1139 if( res == HT_OLD_ENTRY )
1140 {
1141 adj_lsa=e->data;
1142 printf("NAME LSA found\n");
1143
1144 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_router->name);
1145 ccn_charbuf_append_string(lsa_data,"|");
1146
1147 char *temp_length=(char *)malloc(20);
1148 memset(temp_length,0,20);
1149 sprintf(temp_length,"%d",adj_lsa->header->orig_router->length);
1150 ccn_charbuf_append_string(lsa_data,temp_length);
1151 free(temp_length);
1152 ccn_charbuf_append_string(lsa_data,"|");
1153
1154 char *temp_ltype=(char *)malloc(20);
1155 memset(temp_ltype,0,20);
1156 sprintf(temp_ltype,"%d",adj_lsa->header->ls_type);
1157 ccn_charbuf_append_string(lsa_data,temp_ltype);
1158 free(temp_ltype);
1159 ccn_charbuf_append_string(lsa_data,"|");
1160
1161 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_time);
1162 ccn_charbuf_append_string(lsa_data,"|");
1163
1164 char *temp_nl=(char *)malloc(20);
1165 memset(temp_nl,0,20);
1166 sprintf(temp_nl,"%d",adj_lsa->no_link);
1167 ccn_charbuf_append_string(lsa_data,temp_nl);
1168 free(temp_nl);
1169 ccn_charbuf_append_string(lsa_data,"|");
1170
1171 ccn_charbuf_append_string(lsa_data,adj_lsa->body);
1172
1173
1174 }
1175 else if(res == HT_NEW_ENTRY)
1176 {
1177 hashtb_delete(e);
1178 }
1179
1180 hashtb_end(e);
1181}
akmhoqueffacaa82012-09-13 17:48:30 -05001182
akmhoqueda5b6832012-09-13 22:33:55 -05001183int
1184delete_name_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1185{
1186 printf("delete_name_lsa called \n");
1187
1188 if(flags == CCN_SCHEDULE_CANCEL)
1189 {
1190 return -1;
1191 }
1192
1193
1194
1195 nlsr_lock();
1196
1197 printf("LSA Key: %s \n",(char *)ev->evdata);
1198
1199 struct nlsa *nlsa;
1200
1201 struct hashtb_enumerator ee;
1202 struct hashtb_enumerator *e = &ee;
1203
1204 int res;
1205
1206 hashtb_start(nlsr->lsdb->name_lsdb, e);
1207 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1208
1209 if( res == HT_OLD_ENTRY )
1210 {
1211 nlsa=e->data;
1212 delete_npt_entry(nlsa->header->orig_router->name, nlsa->name_prefix->name);
1213 hashtb_delete(e);
1214 }
1215 else if( res == HT_OLD_ENTRY )
1216 {
1217 nlsa=e->data;
1218 delete_npt_entry(nlsa->header->orig_router->name, nlsa->name_prefix->name);
1219 hashtb_delete(e);
1220 }
1221 hashtb_end(e);
1222
1223
1224 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1225 set_new_lsdb_version();
1226 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1227
1228 //print_name_lsdb();
1229
1230 nlsr_unlock();
1231
1232 return 0;
1233}
1234
1235int
1236delete_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1237{
1238 printf("delete_adj_lsa called \n");
1239
1240 if(flags == CCN_SCHEDULE_CANCEL)
1241 {
1242 return -1;
1243 }
1244 nlsr_lock();
1245
1246 printf("LSA Key: %s \n",(char *)ev->evdata);
1247
1248 struct hashtb_enumerator ee;
1249 struct hashtb_enumerator *e = &ee;
1250 int res;
1251
1252 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1253 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1254
1255 if( res == HT_OLD_ENTRY )
1256 {
1257 hashtb_delete(e);
1258 }
1259 else if( res == HT_OLD_ENTRY )
1260 {
1261 hashtb_delete(e);
1262 }
1263 hashtb_end(e);
1264
1265 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1266 set_new_lsdb_version();
1267 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1268
1269 if ( !nlsr->is_route_calculation_scheduled)
1270 {
1271 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
1272 nlsr->is_route_calculation_scheduled=1;
1273 }
1274
1275 //print_adj_lsdb();
1276
1277 nlsr_unlock();
1278
1279 return 0;
1280}
1281
akmhoqueffacaa82012-09-13 17:48:30 -05001282void
1283refresh_name_lsdb(void)
1284{
1285 printf("refresh_name_lsdb called \n");
1286
akmhoqueda5b6832012-09-13 22:33:55 -05001287 //int lsa_change_count=0;
1288
akmhoqueffacaa82012-09-13 17:48:30 -05001289 char *time_stamp=(char *)malloc(20);
1290 memset(time_stamp,0,20);
1291 get_current_timestamp_micro(time_stamp);
1292
1293 long int lsa_life_time;
1294
1295 int i, name_lsdb_element;
1296 struct nlsa *name_lsa;
1297
1298 struct hashtb_enumerator ee;
1299 struct hashtb_enumerator *e = &ee;
1300
1301 hashtb_start(nlsr->lsdb->name_lsdb, e);
1302 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
1303
1304 for(i=0;i<name_lsdb_element;i++)
1305 {
1306 name_lsa=e->data;
1307
1308 lsa_life_time=get_time_diff(time_stamp,name_lsa->header->orig_time);
akmhoqueda5b6832012-09-13 22:33:55 -05001309 printf("LSA Life Time: %ld \n",lsa_life_time);
1310
1311 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name) == 0)
1312 {
1313 if ( lsa_life_time > nlsr->lsa_refresh_time )
1314 {
akmhoque14b3f342012-09-14 10:39:02 -05001315 if ( name_lsa->header->isValid == NAME_LSA_VALID )
1316 {
1317 printf("Own Name LSA need to be refrshed\n");
1318 char *current_time_stamp=(char *)malloc(20);
1319 memset(current_time_stamp,0,20);
1320 get_current_timestamp_micro(current_time_stamp);
akmhoqueda5b6832012-09-13 22:33:55 -05001321
akmhoque14b3f342012-09-14 10:39:02 -05001322 free(name_lsa->header->orig_time);
1323 name_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1324 memset(name_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1325 memcpy(name_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
akmhoqueda5b6832012-09-13 22:33:55 -05001326
akmhoque14b3f342012-09-14 10:39:02 -05001327 free(current_time_stamp);
1328 }
1329 else
1330 {
1331 char lst[2];
1332 memset(lst,0,2);
1333 sprintf(lst,"%d",name_lsa->header->ls_type);
1334
1335 char lsid[10];
1336 memset(lsid,0,10);
1337 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1338
1339
1340 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1341 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1342
1343
1344 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
1345 printf("Key:%s Length:%d\n",key,(int)strlen(key));
1346
1347 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
1348 }
akmhoqueda5b6832012-09-13 22:33:55 -05001349
1350 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1351 set_new_lsdb_version();
1352 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1353
1354 print_name_lsdb();
1355 //lsa_change_count++;
1356 }
1357 }
1358 else
1359 {
1360 if ( lsa_life_time > nlsr->router_dead_interval )
1361 {
1362 printf("Others Name LSA need to be deleted\n");
1363
1364 char lst[2];
1365 memset(lst,0,2);
1366 sprintf(lst,"%d",name_lsa->header->ls_type);
1367
1368 char lsid[10];
1369 memset(lsid,0,10);
1370 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1371
1372
1373 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1374 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1375
1376
1377 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
1378 printf("Key:%s Length:%d\n",key,(int)strlen(key));
1379
1380 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
1381 //lsa_change_count++;
1382 }
1383 }
akmhoqueffacaa82012-09-13 17:48:30 -05001384
1385 hashtb_next(e);
1386 }
1387
1388 hashtb_end(e);
1389
1390 free(time_stamp);
akmhoqueda5b6832012-09-13 22:33:55 -05001391
akmhoqueffacaa82012-09-13 17:48:30 -05001392
1393}
1394
akmhoqueda5b6832012-09-13 22:33:55 -05001395void
akmhoqueffacaa82012-09-13 17:48:30 -05001396refresh_adj_lsdb(void)
1397{
1398 printf("refresh_adj_lsdb called \n");
1399
akmhoqueda5b6832012-09-13 22:33:55 -05001400
akmhoqueffacaa82012-09-13 17:48:30 -05001401 char *time_stamp=(char *)malloc(20);
1402 memset(time_stamp,0,20);
1403 get_current_timestamp_micro(time_stamp);
1404
1405 long int lsa_life_time;
1406
1407 int i, adj_lsdb_element;
1408 struct alsa *adj_lsa;
1409
1410 struct hashtb_enumerator ee;
1411 struct hashtb_enumerator *e = &ee;
1412
1413 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1414 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1415
1416 for(i=0;i<adj_lsdb_element;i++)
1417 {
1418 adj_lsa=e->data;
1419
1420 lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
1421 printf("LSA Life Time: %ld \n",lsa_life_time);
1422
akmhoqueda5b6832012-09-13 22:33:55 -05001423 if ( strcmp(adj_lsa->header->orig_router->name,nlsr->router_name) == 0)
1424 {
1425 if ( lsa_life_time > nlsr->lsa_refresh_time )
1426 {
1427 printf("Own Adj LSA need to be refrshed\n");
1428
1429 char *current_time_stamp=(char *)malloc(20);
1430 memset(current_time_stamp,0,20);
1431 get_current_timestamp_micro(current_time_stamp);
1432
1433 free(adj_lsa->header->orig_time);
1434 adj_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1435 memset(adj_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1436 memcpy(adj_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1437
1438 free(current_time_stamp);
1439
1440
1441 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1442 set_new_lsdb_version();
1443 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1444
1445 print_adj_lsdb();
1446 }
1447 }
1448 else
1449 {
1450 if ( lsa_life_time > nlsr->router_dead_interval )
1451 {
1452 printf("Others Adj LSA need to be deleted\n");
1453
1454 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
1455 memset(key,0,adj_lsa->header->orig_router->length+2);
1456 make_adj_lsa_key(key,adj_lsa);
1457 printf("Adjacent LSA key: %s \n",key);
akmhoqueda5b6832012-09-13 22:33:55 -05001458 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_adj_lsa, (void *)key, 0);
1459 }
1460 }
1461
1462
1463
akmhoqueffacaa82012-09-13 17:48:30 -05001464 hashtb_next(e);
1465 }
1466
1467 hashtb_end(e);
1468
1469 free(time_stamp);
akmhoqueffacaa82012-09-13 17:48:30 -05001470}
1471
1472int
1473refresh_lsdb(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1474{
1475 if(flags == CCN_SCHEDULE_CANCEL)
1476 {
1477 return -1;
1478 }
1479
1480 nlsr_lock();
akmhoqueda5b6832012-09-13 22:33:55 -05001481
akmhoqueffacaa82012-09-13 17:48:30 -05001482 printf("refresh_lsdb called \n");
1483
1484 refresh_name_lsdb();
1485 refresh_adj_lsdb();
1486
akmhoqueffacaa82012-09-13 17:48:30 -05001487 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoqueda5b6832012-09-13 22:33:55 -05001488
akmhoqueffacaa82012-09-13 17:48:30 -05001489 nlsr_unlock();
1490 return 0;
1491}