blob: 982fef4b608739303dedd8a128a8032465d2e3e2 [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
akmhoque3cced642012-09-24 16:20:20 -0500259 delete_npt_entry_by_router_and_name_prefix(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name);
akmhoqueffacaa82012-09-13 17:48:30 -0500260
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;
akmhoque3cced642012-09-24 16:20:20 -0500284 delete_npt_entry_by_router_and_name_prefix(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name);
akmhoqueffacaa82012-09-13 17:48:30 -0500285 }
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
akmhoque62c0c192012-09-24 07:49:25 -0500590 if(res == HT_NEW_ENTRY)
akmhoque53f64222012-09-05 13:57:51 -0500591 {
akmhoque62c0c192012-09-24 07:49:25 -0500592 if ( adj_lsa->no_link > 0)
593 {
594 printf("New ADJ LSA... Adding to LSDB\n");
595 new_adj_lsa = e->data;
akmhoque53f64222012-09-05 13:57:51 -0500596
akmhoque62c0c192012-09-24 07:49:25 -0500597 new_adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
598 new_adj_lsa->header->ls_type=adj_lsa->header->ls_type;
599 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
600 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
akmhoqueffacaa82012-09-13 17:48:30 -0500601
akmhoque62c0c192012-09-24 07:49:25 -0500602 new_adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
603 new_adj_lsa->header->orig_router->name=(char *)malloc(adj_lsa->header->orig_router->length);
604 memcpy(new_adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
605 new_adj_lsa->header->orig_router->length=adj_lsa->header->orig_router->length;
akmhoque53f64222012-09-05 13:57:51 -0500606
akmhoque62c0c192012-09-24 07:49:25 -0500607 new_adj_lsa->no_link=adj_lsa->no_link;
akmhoqueffacaa82012-09-13 17:48:30 -0500608
akmhoque62c0c192012-09-24 07:49:25 -0500609 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
610 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
611 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
612
613 add_next_hop_router(new_adj_lsa->header->orig_router->name);
614 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
615 }
616 else
617 {
618 hashtb_delete(e);
619 }
akmhoque53f64222012-09-05 13:57:51 -0500620
akmhoque03004e62012-09-06 01:12:28 -0500621 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500622 set_new_lsdb_version();
akmhoque03004e62012-09-06 01:12:28 -0500623 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500624 }
625 else if(res == HT_OLD_ENTRY)
626 {
627 new_adj_lsa = e->data;
628 if(strcmp(adj_lsa->header->orig_time,new_adj_lsa->header->orig_time)<=0)
629 {
630 printf("Older/Duplicate Adj LSA. Discarded...\n");
631 }
632 else
633 {
akmhoqueffacaa82012-09-13 17:48:30 -0500634
akmhoque62c0c192012-09-24 07:49:25 -0500635 if ( adj_lsa->no_link > 0)
636 {
637 new_adj_lsa = e->data;
akmhoqueffacaa82012-09-13 17:48:30 -0500638
akmhoque62c0c192012-09-24 07:49:25 -0500639 free(new_adj_lsa->header->orig_time);
640 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
641 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
642
643 new_adj_lsa->no_link=adj_lsa->no_link;
akmhoqueffacaa82012-09-13 17:48:30 -0500644
akmhoque62c0c192012-09-24 07:49:25 -0500645 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
646 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
647 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
akmhoqueffacaa82012-09-13 17:48:30 -0500648
akmhoque62c0c192012-09-24 07:49:25 -0500649 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
650 }
651 else
652 {
653 hashtb_delete(e);
654 }
akmhoqueffacaa82012-09-13 17:48:30 -0500655 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
656 set_new_lsdb_version();
657 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500658 }
659
660 }
661 hashtb_end(e);
662
663 if ( !nlsr->is_route_calculation_scheduled )
664 {
665 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
666 nlsr->is_route_calculation_scheduled=1;
akmhoque53f64222012-09-05 13:57:51 -0500667 }
668
akmhoque03004e62012-09-06 01:12:28 -0500669
akmhoqueffacaa82012-09-13 17:48:30 -0500670 free(key);
671
672 free(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -0500673}
674
675void
676print_adj_lsa_body(const char *body, int no_link)
677{
678 int i=0;
679 char *lsa_data=(char *)malloc(strlen(body)+1);
680 memset( lsa_data,0,strlen(body)+1);
681 memcpy(lsa_data,body,strlen(body)+1);
682 char *sep="|";
683 char *rem;
684 char *rtr_id;
685 char *length;
686 char *face;
687 char *metric;
688
akmhoque53f64222012-09-05 13:57:51 -0500689 if(no_link >0 )
690 {
691 rtr_id=strtok_r(lsa_data,sep,&rem);
692 length=strtok_r(NULL,sep,&rem);
693 face=strtok_r(NULL,sep,&rem);
694 metric=strtok_r(NULL,sep,&rem);
695
696 printf(" Link %d \n",i+1);
697 printf(" Neighbor : %s \n",rtr_id);
698 printf(" Neighbor Length : %s \n",length);
699 printf(" Connecting Face : %s \n",face);
700 printf(" Metric : %s \n",metric);
akmhoque03004e62012-09-06 01:12:28 -0500701
akmhoque53f64222012-09-05 13:57:51 -0500702
703 for(i=1;i<no_link;i++)
704 {
705 rtr_id=strtok_r(NULL,sep,&rem);
706 length=strtok_r(NULL,sep,&rem);
707 face=strtok_r(NULL,sep,&rem);
708 metric=strtok_r(NULL,sep,&rem);
709 printf(" Link %d \n",i+1);
710 printf(" Neighbor : %s \n",rtr_id);
711 printf(" Neighbor Length : %s \n",length);
712 printf(" Connecting Face : %s \n",face);
713 printf(" Metric : %s \n",metric);
714
715 }
716 }
717
718 free(lsa_data);
719}
720
721void
722print_adj_lsa(struct alsa * adj_lsa)
723{
akmhoque03004e62012-09-06 01:12:28 -0500724
akmhoque53f64222012-09-05 13:57:51 -0500725 printf("print_adj_lsa called \n");
akmhoque03004e62012-09-06 01:12:28 -0500726
akmhoque53f64222012-09-05 13:57:51 -0500727 printf("-----------ADJ LSA Content---------------\n");
728 printf(" Origination Router : %s\n",adj_lsa->header->orig_router->name);
729 printf(" Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
730 printf(" LS Type : %d\n",adj_lsa->header->ls_type);
731 printf(" Origination Time : %s\n",adj_lsa->header->orig_time);
akmhoque03004e62012-09-06 01:12:28 -0500732 printf(" Lsa Data:\n");
733 printf(" No of Link : %d\n",adj_lsa->no_link);
akmhoque53f64222012-09-05 13:57:51 -0500734
735 print_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
akmhoque53f64222012-09-05 13:57:51 -0500736
akmhoque03004e62012-09-06 01:12:28 -0500737/*
738 struct link *templ=adj_lsa->links;
739 int i;
740
741 for(i=0 ; i< adj_lsa->no_link ; i++)
742 {
743 printf(" Link %d \n",i+1);
744 printf(" Neighbor : %s \n",templ->nbr->name);
745 printf(" Neighbor Length : %d \n",templ->nbr->length);
746 printf(" Connecting Face : %d \n",templ->face);
747 printf(" Metric : %d \n",templ->metric);
748
749 templ++;
750 }
751*/
akmhoque53f64222012-09-05 13:57:51 -0500752 printf("\n");
753
754}
755
756void
757print_adj_lsdb(void)
758{
759 printf("print_name_lsdb called \n");
760 int i, adj_lsdb_element;
761 struct alsa *adj_lsa;
762
763 struct hashtb_enumerator ee;
764 struct hashtb_enumerator *e = &ee;
765
766 hashtb_start(nlsr->lsdb->adj_lsdb, e);
767 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
768
769 for(i=0;i<adj_lsdb_element;i++)
770 {
771 printf("-----------Adj LSA (%d)---------------\n",i+1);
772 adj_lsa=e->data;
773 print_adj_lsa(adj_lsa);
774 hashtb_next(e);
775 }
776
777 hashtb_end(e);
778
779 printf("\n");
akmhoque53f64222012-09-05 13:57:51 -0500780}
781
782void
akmhoque03004e62012-09-06 01:12:28 -0500783build_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 -0500784{
akmhoque03004e62012-09-06 01:12:28 -0500785 printf("build_and_install_others_adj_lsa called \n");
786 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
787 build_others_adj_lsa(adj_lsa,orig_router,ls_type,orig_time,no_link,data);
788 //print_adj_lsa(adj_lsa);
789 install_adj_lsa(adj_lsa);
790
akmhoque53f64222012-09-05 13:57:51 -0500791
akmhoque03004e62012-09-06 01:12:28 -0500792 free(adj_lsa->header->orig_router->name);
793 free(adj_lsa->header->orig_router);
794 free(adj_lsa->header->orig_time);
795 free(adj_lsa->header);
796 free(adj_lsa->body);
797 free(adj_lsa);
akmhoque53f64222012-09-05 13:57:51 -0500798
akmhoque03004e62012-09-06 01:12:28 -0500799 print_adj_lsdb();
akmhoque53f64222012-09-05 13:57:51 -0500800
akmhoque53f64222012-09-05 13:57:51 -0500801}
802
akmhoque03004e62012-09-06 01:12:28 -0500803
akmhoque53f64222012-09-05 13:57:51 -0500804void
akmhoque03004e62012-09-06 01:12:28 -0500805build_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 -0500806{
akmhoque03004e62012-09-06 01:12:28 -0500807 printf("build_others_adj_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500808
akmhoque03004e62012-09-06 01:12:28 -0500809 /*Filling Up Header Data */
810 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
811 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
812 adj_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
813 memset(adj_lsa->header->orig_router->name,0,strlen(orig_router)+1);
814 memcpy(adj_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
akmhoque53f64222012-09-05 13:57:51 -0500815
akmhoque03004e62012-09-06 01:12:28 -0500816 adj_lsa->header->orig_router->length=strlen(orig_router)+1;
akmhoque53f64222012-09-05 13:57:51 -0500817
akmhoque53f64222012-09-05 13:57:51 -0500818
akmhoque03004e62012-09-06 01:12:28 -0500819 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
akmhoque53f64222012-09-05 13:57:51 -0500820
akmhoque03004e62012-09-06 01:12:28 -0500821 adj_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
822 memset(adj_lsa->header->orig_time,0,strlen(orig_time)+1);
823 memcpy(adj_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
akmhoque53f64222012-09-05 13:57:51 -0500824
akmhoque03004e62012-09-06 01:12:28 -0500825 adj_lsa->no_link=no_link;
akmhoque53f64222012-09-05 13:57:51 -0500826
akmhoque03004e62012-09-06 01:12:28 -0500827 adj_lsa->body=(char *)malloc(strlen(data)+1);
828 memset(adj_lsa->body,0,strlen(data)+1);
829 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
akmhoque53f64222012-09-05 13:57:51 -0500830
akmhoque53f64222012-09-05 13:57:51 -0500831}
832
akmhoque03004e62012-09-06 01:12:28 -0500833
akmhoque53f64222012-09-05 13:57:51 -0500834long int
835get_name_lsdb_num_element(void)
836{
837 long int num_element;
838
839
840 struct hashtb_enumerator ee;
841 struct hashtb_enumerator *e = &ee;
842
843 hashtb_start(nlsr->lsdb->name_lsdb, e);
844 num_element=hashtb_n(nlsr->lsdb->name_lsdb);
845 hashtb_end(e);
846
847 return num_element;
848}
849
850long int
851get_adj_lsdb_num_element(void)
852{
853 long int num_element;
854
855
856 struct hashtb_enumerator ee;
857 struct hashtb_enumerator *e = &ee;
858
859 hashtb_start(nlsr->lsdb->adj_lsdb, e);
860 num_element=hashtb_n(nlsr->lsdb->adj_lsdb);
861 hashtb_end(e);
862
863 return num_element;
akmhoqued79438d2012-08-27 13:31:42 -0500864}
akmhoque03004e62012-09-06 01:12:28 -0500865
866void
867get_name_lsdb_summary(struct ccn_charbuf *name_lsdb_data)
868{
869 printf("get_name_lsdb_summary called \n");
870 int i, name_lsdb_element;
871
872 struct nlsa *name_lsa;
873 struct hashtb_enumerator ee;
874 struct hashtb_enumerator *e = &ee;
875
876 hashtb_start(nlsr->lsdb->name_lsdb, e);
877 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
878
879 for(i=0;i<name_lsdb_element;i++)
880 {
881 name_lsa=e->data;
882
883 ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_router->name);
884 ccn_charbuf_append_string(name_lsdb_data,"|");
885
886 char *lst=(char *)malloc(20);
887 memset(lst,0,20);
888 sprintf(lst,"%d",name_lsa->header->ls_type);
889 ccn_charbuf_append_string(name_lsdb_data,lst);
890 free(lst);
891 ccn_charbuf_append_string(name_lsdb_data,"|");
892
893 char *lsid=(char *)malloc(20);
894 memset(lsid,0,20);
895 sprintf(lsid,"%ld",name_lsa->header->ls_id);
896 ccn_charbuf_append_string(name_lsdb_data,lsid);
897 free(lsid);
898 ccn_charbuf_append_string(name_lsdb_data,"|");
899
900 ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_time);
901 ccn_charbuf_append_string(name_lsdb_data,"|");
902
903 hashtb_next(e);
904 }
905
906 hashtb_end(e);
907
908}
909
910
911void
912get_adj_lsdb_summary(struct ccn_charbuf *adj_lsdb_data)
913{
914 printf("get_adj_lsdb_summary called \n");
915 int i, adj_lsdb_element;
916 struct alsa *adj_lsa;
917
918 struct hashtb_enumerator ee;
919 struct hashtb_enumerator *e = &ee;
920
921 hashtb_start(nlsr->lsdb->adj_lsdb, e);
922 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
923
924 for(i=0;i<adj_lsdb_element;i++)
925 {
926 adj_lsa=e->data;
927
928 ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_router->name);
929 ccn_charbuf_append_string(adj_lsdb_data,"|");
930
931 char *lst=(char *)malloc(20);
932 memset(lst,0,20);
933 sprintf(lst,"%d",adj_lsa->header->ls_type);
934 ccn_charbuf_append_string(adj_lsdb_data,lst);
935 free(lst);
936 ccn_charbuf_append_string(adj_lsdb_data,"|");
937
938 ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_time);
939 ccn_charbuf_append_string(adj_lsdb_data,"|");
940
941 hashtb_next(e);
942 }
943
944 hashtb_end(e);
945}
946
947
948void
949get_lsdb_summary(struct ccn_charbuf *lsdb_data)
950{
951 struct ccn_charbuf *name_lsdb_data=ccn_charbuf_create();
952 struct ccn_charbuf *adj_lsdb_data=ccn_charbuf_create();
953
954 get_name_lsdb_summary(name_lsdb_data);
955 get_adj_lsdb_summary(adj_lsdb_data);
956
957 long int num_lsa=get_name_lsdb_num_element() + get_adj_lsdb_num_element();
958 char *num_element=(char *)malloc(15);
959 memset(num_element,0,15);
960 sprintf(num_element,"%ld",num_lsa);
961
962 if( num_lsa > 0)
963 {
964 ccn_charbuf_append_string(lsdb_data,num_element);
965 ccn_charbuf_append_string(lsdb_data,"|");
966 }
967 if(name_lsdb_data->length>0)
968 {
969 char *data1=ccn_charbuf_as_string(name_lsdb_data);
970 ccn_charbuf_append_string(lsdb_data,(char *)data1);
971 }
972 if(adj_lsdb_data->length>0)
973 {
974 char *data2=ccn_charbuf_as_string(adj_lsdb_data);
975 ccn_charbuf_append_string(lsdb_data,(char *)data2);
976 }
977 ccn_charbuf_destroy(&name_lsdb_data);
978 ccn_charbuf_destroy(&adj_lsdb_data);
979 free(num_element);
980
981}
982
983int
984check_is_new_name_lsa(char *orig_router,char *lst,char *lsid,char *orig_time)
985{
986 int ret=0;
987 struct ccn_charbuf *key=ccn_charbuf_create();
988 ccn_charbuf_append_string(key,orig_router);
989 ccn_charbuf_append_string(key,"/");
990 ccn_charbuf_append_string(key,lst);
991 ccn_charbuf_append_string(key,"/");
992 ccn_charbuf_append_string(key,lsid);
993
994 int res;
995 struct nlsa *name_lsa;
996
997 struct hashtb_enumerator ee;
998 struct hashtb_enumerator *e = &ee;
999
1000 hashtb_start(nlsr->lsdb->name_lsdb, e);
1001 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1002
1003 if( res == HT_NEW_ENTRY )
1004 {
1005 hashtb_delete(e);
1006 ret=1;
1007
1008 }
1009 else if(res == HT_OLD_ENTRY)
1010 {
1011 name_lsa=e->data;
1012 if( strcmp ( orig_time , name_lsa->header->orig_time ) > 0 )
1013 {
1014 ret=1;
1015 }
1016 }
1017
1018 hashtb_end(e);
1019
1020 ccn_charbuf_destroy(&key);
1021
1022 return ret;
1023}
1024
1025int
1026check_is_new_adj_lsa(char *orig_router,char *lst,char *orig_time)
1027{
1028 int ret=0;
1029 struct ccn_charbuf *key=ccn_charbuf_create();
1030 ccn_charbuf_append_string(key,orig_router);
1031 ccn_charbuf_append_string(key,"/");
1032 ccn_charbuf_append_string(key,lst);
1033
1034 int res;
1035 struct alsa *adj_lsa;
1036
1037 struct hashtb_enumerator ee;
1038 struct hashtb_enumerator *e = &ee;
1039
1040 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1041 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1042
1043 if( res == HT_NEW_ENTRY )
1044 {
1045 hashtb_delete(e);
1046 ret=1;
1047
1048 }
1049 else if(res == HT_OLD_ENTRY)
1050 {
1051 adj_lsa=e->data;
1052 if( strcmp ( orig_time , adj_lsa->header->orig_time ) > 0 )
1053 {
1054 ret=1;
1055 }
1056 }
1057
1058 hashtb_end(e);
1059
1060 ccn_charbuf_destroy(&key);
1061
1062 return ret;
1063}
1064
1065void
1066get_name_lsa_data(struct ccn_charbuf *lsa_data, struct name_prefix *lsaId)
1067{
1068 printf("get_name_lsa_data called \n");
1069
1070 struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa ));
1071
1072 struct hashtb_enumerator ee;
1073 struct hashtb_enumerator *e = &ee;
1074 int res;
1075
1076 hashtb_start(nlsr->lsdb->name_lsdb, e);
1077 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1078
1079 if( res == HT_OLD_ENTRY )
1080 {
1081 name_lsa=e->data;
1082 printf("NAME LSA found\n");
1083
1084 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_router->name);
1085 ccn_charbuf_append_string(lsa_data,"|");
1086
1087 char *temp_length=(char *)malloc(20);
1088 memset(temp_length,0,20);
1089 sprintf(temp_length,"%d",name_lsa->header->orig_router->length);
1090 ccn_charbuf_append_string(lsa_data,temp_length);
1091 free(temp_length);
1092 ccn_charbuf_append_string(lsa_data,"|");
1093
1094 char *temp_ltype=(char *)malloc(20);
1095 memset(temp_ltype,0,20);
1096 sprintf(temp_ltype,"%d",name_lsa->header->ls_type);
1097 ccn_charbuf_append_string(lsa_data,temp_ltype);
1098 free(temp_ltype);
1099 ccn_charbuf_append_string(lsa_data,"|");
1100
1101 char *temp_lsid=(char *)malloc(20);
1102 memset(temp_lsid,0,20);
1103 sprintf(temp_lsid,"%ld",name_lsa->header->ls_id);
1104 ccn_charbuf_append_string(lsa_data,temp_lsid);
1105 free(temp_lsid);
1106 ccn_charbuf_append_string(lsa_data,"|");
1107
1108 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_time);
1109 ccn_charbuf_append_string(lsa_data,"|");
1110
1111 char *temp_valid=(char *)malloc(20);
1112 memset(temp_valid,0,20);
1113 sprintf(temp_valid,"%d",name_lsa->header->isValid);
1114 ccn_charbuf_append_string(lsa_data,temp_valid);
1115 free(temp_valid);
1116 ccn_charbuf_append_string(lsa_data,"|");
1117
1118 ccn_charbuf_append_string(lsa_data,name_lsa->name_prefix->name);
1119 ccn_charbuf_append_string(lsa_data,"|");
1120
1121 char *temp_npl=(char *)malloc(20);
1122 memset(temp_npl,0,20);
1123 sprintf(temp_npl,"%d",name_lsa->name_prefix->length);
1124 ccn_charbuf_append_string(lsa_data,temp_npl);
1125 free(temp_npl);
1126 ccn_charbuf_append_string(lsa_data,"|");
1127
1128 }
1129 else if(res == HT_NEW_ENTRY)
1130 {
1131 hashtb_delete(e);
1132 }
1133
1134 hashtb_end(e);
1135}
1136
1137void
1138get_adj_lsa_data(struct ccn_charbuf *lsa_data,struct name_prefix *lsaId)
1139{
1140 printf("get_adj_lsa_data called \n");
1141
1142 struct alsa *adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
1143
1144 struct hashtb_enumerator ee;
1145 struct hashtb_enumerator *e = &ee;
1146 int res;
1147
1148 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1149 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1150
1151 if( res == HT_OLD_ENTRY )
1152 {
1153 adj_lsa=e->data;
1154 printf("NAME LSA found\n");
1155
1156 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_router->name);
1157 ccn_charbuf_append_string(lsa_data,"|");
1158
1159 char *temp_length=(char *)malloc(20);
1160 memset(temp_length,0,20);
1161 sprintf(temp_length,"%d",adj_lsa->header->orig_router->length);
1162 ccn_charbuf_append_string(lsa_data,temp_length);
1163 free(temp_length);
1164 ccn_charbuf_append_string(lsa_data,"|");
1165
1166 char *temp_ltype=(char *)malloc(20);
1167 memset(temp_ltype,0,20);
1168 sprintf(temp_ltype,"%d",adj_lsa->header->ls_type);
1169 ccn_charbuf_append_string(lsa_data,temp_ltype);
1170 free(temp_ltype);
1171 ccn_charbuf_append_string(lsa_data,"|");
1172
1173 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_time);
1174 ccn_charbuf_append_string(lsa_data,"|");
1175
1176 char *temp_nl=(char *)malloc(20);
1177 memset(temp_nl,0,20);
1178 sprintf(temp_nl,"%d",adj_lsa->no_link);
1179 ccn_charbuf_append_string(lsa_data,temp_nl);
1180 free(temp_nl);
1181 ccn_charbuf_append_string(lsa_data,"|");
1182
1183 ccn_charbuf_append_string(lsa_data,adj_lsa->body);
1184
1185
1186 }
1187 else if(res == HT_NEW_ENTRY)
1188 {
1189 hashtb_delete(e);
1190 }
1191
1192 hashtb_end(e);
1193}
akmhoqueffacaa82012-09-13 17:48:30 -05001194
akmhoqueda5b6832012-09-13 22:33:55 -05001195int
1196delete_name_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1197{
1198 printf("delete_name_lsa called \n");
1199
1200 if(flags == CCN_SCHEDULE_CANCEL)
1201 {
1202 return -1;
1203 }
1204
1205
1206
1207 nlsr_lock();
1208
1209 printf("LSA Key: %s \n",(char *)ev->evdata);
1210
1211 struct nlsa *nlsa;
1212
1213 struct hashtb_enumerator ee;
1214 struct hashtb_enumerator *e = &ee;
1215
1216 int res;
1217
1218 hashtb_start(nlsr->lsdb->name_lsdb, e);
1219 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1220
1221 if( res == HT_OLD_ENTRY )
1222 {
1223 nlsa=e->data;
akmhoque3cced642012-09-24 16:20:20 -05001224 delete_npt_entry_by_router_and_name_prefix(nlsa->header->orig_router->name, nlsa->name_prefix->name);
akmhoqueda5b6832012-09-13 22:33:55 -05001225 hashtb_delete(e);
1226 }
akmhoque3cced642012-09-24 16:20:20 -05001227 else if( res == HT_NEW_ENTRY )
akmhoqueda5b6832012-09-13 22:33:55 -05001228 {
akmhoqueda5b6832012-09-13 22:33:55 -05001229 hashtb_delete(e);
1230 }
1231 hashtb_end(e);
1232
1233
1234 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1235 set_new_lsdb_version();
1236 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1237
1238 //print_name_lsdb();
1239
1240 nlsr_unlock();
1241
1242 return 0;
1243}
1244
1245int
1246delete_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1247{
1248 printf("delete_adj_lsa called \n");
1249
1250 if(flags == CCN_SCHEDULE_CANCEL)
1251 {
1252 return -1;
1253 }
1254 nlsr_lock();
1255
1256 printf("LSA Key: %s \n",(char *)ev->evdata);
1257
1258 struct hashtb_enumerator ee;
1259 struct hashtb_enumerator *e = &ee;
1260 int res;
1261
1262 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1263 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1264
1265 if( res == HT_OLD_ENTRY )
1266 {
1267 hashtb_delete(e);
1268 }
1269 else if( res == HT_OLD_ENTRY )
1270 {
1271 hashtb_delete(e);
1272 }
1273 hashtb_end(e);
1274
1275 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1276 set_new_lsdb_version();
1277 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1278
1279 if ( !nlsr->is_route_calculation_scheduled)
1280 {
1281 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
1282 nlsr->is_route_calculation_scheduled=1;
1283 }
1284
1285 //print_adj_lsdb();
1286
1287 nlsr_unlock();
1288
1289 return 0;
1290}
1291
akmhoqueffacaa82012-09-13 17:48:30 -05001292void
1293refresh_name_lsdb(void)
1294{
1295 printf("refresh_name_lsdb called \n");
1296
akmhoqueda5b6832012-09-13 22:33:55 -05001297 //int lsa_change_count=0;
1298
akmhoqueffacaa82012-09-13 17:48:30 -05001299 char *time_stamp=(char *)malloc(20);
1300 memset(time_stamp,0,20);
1301 get_current_timestamp_micro(time_stamp);
1302
1303 long int lsa_life_time;
1304
1305 int i, name_lsdb_element;
1306 struct nlsa *name_lsa;
1307
1308 struct hashtb_enumerator ee;
1309 struct hashtb_enumerator *e = &ee;
1310
1311 hashtb_start(nlsr->lsdb->name_lsdb, e);
1312 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
1313
1314 for(i=0;i<name_lsdb_element;i++)
1315 {
1316 name_lsa=e->data;
1317
1318 lsa_life_time=get_time_diff(time_stamp,name_lsa->header->orig_time);
akmhoqueda5b6832012-09-13 22:33:55 -05001319 printf("LSA Life Time: %ld \n",lsa_life_time);
1320
1321 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name) == 0)
1322 {
1323 if ( lsa_life_time > nlsr->lsa_refresh_time )
1324 {
akmhoque14b3f342012-09-14 10:39:02 -05001325 if ( name_lsa->header->isValid == NAME_LSA_VALID )
1326 {
1327 printf("Own Name LSA need to be refrshed\n");
1328 char *current_time_stamp=(char *)malloc(20);
1329 memset(current_time_stamp,0,20);
1330 get_current_timestamp_micro(current_time_stamp);
akmhoqueda5b6832012-09-13 22:33:55 -05001331
akmhoque14b3f342012-09-14 10:39:02 -05001332 free(name_lsa->header->orig_time);
1333 name_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1334 memset(name_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1335 memcpy(name_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
akmhoqueda5b6832012-09-13 22:33:55 -05001336
akmhoque14b3f342012-09-14 10:39:02 -05001337 free(current_time_stamp);
1338 }
1339 else
1340 {
1341 char lst[2];
1342 memset(lst,0,2);
1343 sprintf(lst,"%d",name_lsa->header->ls_type);
1344
1345 char lsid[10];
1346 memset(lsid,0,10);
1347 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1348
1349
1350 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1351 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1352
1353
1354 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
1355 printf("Key:%s Length:%d\n",key,(int)strlen(key));
1356
1357 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
1358 }
akmhoqueda5b6832012-09-13 22:33:55 -05001359
1360 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1361 set_new_lsdb_version();
1362 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1363
1364 print_name_lsdb();
1365 //lsa_change_count++;
1366 }
1367 }
1368 else
1369 {
1370 if ( lsa_life_time > nlsr->router_dead_interval )
1371 {
1372 printf("Others Name LSA need to be deleted\n");
1373
1374 char lst[2];
1375 memset(lst,0,2);
1376 sprintf(lst,"%d",name_lsa->header->ls_type);
1377
1378 char lsid[10];
1379 memset(lsid,0,10);
1380 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1381
1382
1383 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1384 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1385
1386
1387 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
1388 printf("Key:%s Length:%d\n",key,(int)strlen(key));
1389
1390 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
1391 //lsa_change_count++;
1392 }
1393 }
akmhoqueffacaa82012-09-13 17:48:30 -05001394
1395 hashtb_next(e);
1396 }
1397
1398 hashtb_end(e);
1399
1400 free(time_stamp);
akmhoqueda5b6832012-09-13 22:33:55 -05001401
akmhoqueffacaa82012-09-13 17:48:30 -05001402
1403}
1404
akmhoqueda5b6832012-09-13 22:33:55 -05001405void
akmhoqueffacaa82012-09-13 17:48:30 -05001406refresh_adj_lsdb(void)
1407{
1408 printf("refresh_adj_lsdb called \n");
1409
akmhoqueda5b6832012-09-13 22:33:55 -05001410
akmhoqueffacaa82012-09-13 17:48:30 -05001411 char *time_stamp=(char *)malloc(20);
1412 memset(time_stamp,0,20);
1413 get_current_timestamp_micro(time_stamp);
1414
1415 long int lsa_life_time;
1416
1417 int i, adj_lsdb_element;
1418 struct alsa *adj_lsa;
1419
1420 struct hashtb_enumerator ee;
1421 struct hashtb_enumerator *e = &ee;
1422
1423 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1424 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1425
1426 for(i=0;i<adj_lsdb_element;i++)
1427 {
1428 adj_lsa=e->data;
1429
1430 lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
1431 printf("LSA Life Time: %ld \n",lsa_life_time);
1432
akmhoqueda5b6832012-09-13 22:33:55 -05001433 if ( strcmp(adj_lsa->header->orig_router->name,nlsr->router_name) == 0)
1434 {
1435 if ( lsa_life_time > nlsr->lsa_refresh_time )
1436 {
1437 printf("Own Adj LSA need to be refrshed\n");
1438
1439 char *current_time_stamp=(char *)malloc(20);
1440 memset(current_time_stamp,0,20);
1441 get_current_timestamp_micro(current_time_stamp);
1442
1443 free(adj_lsa->header->orig_time);
1444 adj_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1445 memset(adj_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1446 memcpy(adj_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1447
1448 free(current_time_stamp);
1449
1450
1451 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1452 set_new_lsdb_version();
1453 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1454
1455 print_adj_lsdb();
1456 }
1457 }
1458 else
1459 {
1460 if ( lsa_life_time > nlsr->router_dead_interval )
1461 {
1462 printf("Others Adj LSA need to be deleted\n");
1463
1464 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
1465 memset(key,0,adj_lsa->header->orig_router->length+2);
1466 make_adj_lsa_key(key,adj_lsa);
1467 printf("Adjacent LSA key: %s \n",key);
akmhoqueda5b6832012-09-13 22:33:55 -05001468 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_adj_lsa, (void *)key, 0);
1469 }
1470 }
1471
1472
1473
akmhoqueffacaa82012-09-13 17:48:30 -05001474 hashtb_next(e);
1475 }
1476
1477 hashtb_end(e);
1478
1479 free(time_stamp);
akmhoqueffacaa82012-09-13 17:48:30 -05001480}
1481
1482int
1483refresh_lsdb(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1484{
1485 if(flags == CCN_SCHEDULE_CANCEL)
1486 {
1487 return -1;
1488 }
1489
1490 nlsr_lock();
akmhoqueda5b6832012-09-13 22:33:55 -05001491
akmhoqueffacaa82012-09-13 17:48:30 -05001492 printf("refresh_lsdb called \n");
1493
1494 refresh_name_lsdb();
1495 refresh_adj_lsdb();
1496
akmhoqueffacaa82012-09-13 17:48:30 -05001497 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoqueda5b6832012-09-13 22:33:55 -05001498
akmhoqueffacaa82012-09-13 17:48:30 -05001499 nlsr_unlock();
1500 return 0;
1501}