blob: 443e644fadd3fd41c3e77babea47808edea1f5cf [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");
akmhoqueffacaa82012-09-13 17:48:30 -0500201 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
202 set_new_lsdb_version();
203 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
204
akmhoque9e9fc722012-09-26 14:03:25 -0500205
akmhoquede61ba92012-09-20 22:19:12 -0500206 int num_next_hop=get_number_of_next_hop(new_name_lsa->header->orig_router->name);
akmhoquede61ba92012-09-20 22:19:12 -0500207 if ( num_next_hop < 0 )
akmhoque3560cb62012-09-09 10:52:30 -0500208 {
akmhoquede61ba92012-09-20 22:19:12 -0500209 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 -0500210 if ( check == HT_NEW_ENTRY )
211 {
212 printf("Added in npt \n");
213 }
akmhoque3560cb62012-09-09 10:52:30 -0500214 }
akmhoqueffacaa82012-09-13 17:48:30 -0500215 else
akmhoque3560cb62012-09-09 10:52:30 -0500216 {
akmhoquede61ba92012-09-20 22:19:12 -0500217 int *faces=malloc(num_next_hop*sizeof(int));
218 int *route_costs=malloc(num_next_hop*sizeof(int));
219 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name,faces,route_costs);
220 printf("Printing from install_name_lsa \n");
221 int j;
222 for(j=0;j<num_next_hop;j++)
223 printf("Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
224 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 -0500225 if ( check == HT_NEW_ENTRY )
226 {
227 printf("Added in npt \n");
228 }
akmhoquede61ba92012-09-20 22:19:12 -0500229 free(faces);
230 free(route_costs);
akmhoqueffacaa82012-09-13 17:48:30 -0500231
akmhoque3560cb62012-09-09 10:52:30 -0500232 }
akmhoquede61ba92012-09-20 22:19:12 -0500233
akmhoque9e9fc722012-09-26 14:03:25 -0500234 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
235 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
236 write_log_for_name_lsa(new_name_lsa);
237 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500238
akmhoquede61ba92012-09-20 22:19:12 -0500239 free(time_stamp);
akmhoqueffacaa82012-09-13 17:48:30 -0500240
241 }
242 else if(res == HT_OLD_ENTRY)
243 {
244 new_name_lsa=e->data;
akmhoque14b3f342012-09-14 10:39:02 -0500245 if(strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time)<0)
akmhoqueffacaa82012-09-13 17:48:30 -0500246 {
akmhoque14b3f342012-09-14 10:39:02 -0500247 printf("Older Adj LSA. Discarded...\n");
248 }
249 else if( strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time) == 0 )
250 {
251 printf("Duplicate Adj LSA. Discarded...\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500252 }
253 else
254 {
255 if ( name_lsa->header->isValid == 0 )
256 {
257 // have to call to delete npt table entry
akmhoque3cced642012-09-24 16:20:20 -0500258 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 -0500259
260 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name)!= 0)
261 {
akmhoque9e9fc722012-09-26 14:03:25 -0500262 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
263 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
264 write_log_for_name_lsa(new_name_lsa);
265 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
266
akmhoqueffacaa82012-09-13 17:48:30 -0500267 hashtb_delete(e);
268 printf("isValid bit not set for Router %s so LSA Deleted from LSDB\n",name_lsa->header->orig_router->name);
269 }
270 else
271 {
272 new_name_lsa->header->isValid=name_lsa->header->isValid;
273 free(new_name_lsa->header->orig_time);
274 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
275 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
276 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
277 }
278 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
279 set_new_lsdb_version();
280 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
281 }
282 else
283 {
284 int is_npt_update=0;
285 if ( strcmp(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name) != 0 )
286 {
287 is_npt_update=1;
akmhoque3cced642012-09-24 16:20:20 -0500288 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 -0500289 }
290
291 // copying LSA content with header
akmhoque9e9fc722012-09-26 14:03:25 -0500292 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
293 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
294 write_log_for_name_lsa(new_name_lsa);
295 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
296
akmhoqueffacaa82012-09-13 17:48:30 -0500297
298 free(new_name_lsa->header->orig_time);
299 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
300 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
301 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
302
303 new_name_lsa->header->isValid=name_lsa->header->isValid;
304
305 free(new_name_lsa->name_prefix->name);
306 free(new_name_lsa->name_prefix);
307 new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
308 new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
309 memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
310 new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
311
akmhoque9e9fc722012-09-26 14:03:25 -0500312
313 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
314 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
315 write_log_for_name_lsa(new_name_lsa);
316 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
317
akmhoqueffacaa82012-09-13 17:48:30 -0500318 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
319 set_new_lsdb_version();
320 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
321
322 if( is_npt_update == 1 )
323 {
akmhoquede61ba92012-09-20 22:19:12 -0500324 //struct hashtb *face_list;
325 int num_next_hop=get_number_of_next_hop(new_name_lsa->header->orig_router->name);
326 if ( num_next_hop < 0 )
akmhoqueffacaa82012-09-13 17:48:30 -0500327 {
akmhoquede61ba92012-09-20 22:19:12 -0500328
329 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 -0500330 if ( check == HT_NEW_ENTRY )
331 {
332 printf("Added in npt \n");
333 }
334 }
335 else
336 {
akmhoquede61ba92012-09-20 22:19:12 -0500337 int *faces=malloc(num_next_hop*sizeof(int));
338 int *route_costs=malloc(num_next_hop*sizeof(int));
339 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name,faces,route_costs);
340 printf("Printing from install_name_lsa \n");
341 int j;
342 for(j=0;j<num_next_hop;j++)
343 printf("Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
344
345 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 -0500346 if ( check == HT_NEW_ENTRY )
347 {
348 printf("Added in npt \n");
349 }
akmhoquede61ba92012-09-20 22:19:12 -0500350 free(faces);
351 free(route_costs);
352
akmhoqueffacaa82012-09-13 17:48:30 -0500353 }
akmhoquede61ba92012-09-20 22:19:12 -0500354
akmhoqueffacaa82012-09-13 17:48:30 -0500355 }
356 }
357 }
358
akmhoque3560cb62012-09-09 10:52:30 -0500359 }
akmhoque53f64222012-09-05 13:57:51 -0500360
akmhoqueffacaa82012-09-13 17:48:30 -0500361 hashtb_end(e);
akmhoque53f64222012-09-05 13:57:51 -0500362
akmhoqueffacaa82012-09-13 17:48:30 -0500363 free(key);
akmhoque53f64222012-09-05 13:57:51 -0500364}
365
akmhoque9e9fc722012-09-26 14:03:25 -0500366void
367write_log_for_name_lsa(struct nlsa *name_lsa)
368{
369
370 writeLogg(__FILE__,__FUNCTION__,__LINE__,"-----------Name LSA Content---------------\n");
371 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s\n",name_lsa->header->orig_router->name);
372 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router Length: %d\n",name_lsa->header->orig_router->length);
akmhoque34b99f92012-09-27 12:24:27 -0500373 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Type: %d\n",name_lsa->header->ls_type);
374 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Id: %ld\n",name_lsa->header->ls_id);
375 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Time: %s\n",name_lsa->header->orig_time);
376 writeLogg(__FILE__,__FUNCTION__,__LINE__," Is Valid: %d\n",name_lsa->header->isValid);
akmhoque9e9fc722012-09-26 14:03:25 -0500377 writeLogg(__FILE__,__FUNCTION__,__LINE__," LSA Data \n");
378 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix: %s\n",name_lsa->name_prefix->name);
379 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix Length: %d\n",name_lsa->name_prefix->length);
380 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
381}
akmhoque53f64222012-09-05 13:57:51 -0500382
akmhoque03004e62012-09-06 01:12:28 -0500383void
384print_name_lsa(struct nlsa *name_lsa)
akmhoque53f64222012-09-05 13:57:51 -0500385{
386
akmhoque03004e62012-09-06 01:12:28 -0500387 printf("-----------Name LSA Content---------------\n");
388 printf(" Origination Router : %s\n",name_lsa->header->orig_router->name);
389 printf(" Origination Router Length: %d\n",name_lsa->header->orig_router->length);
390 printf(" LS Type : %d\n",name_lsa->header->ls_type);
391 printf(" LS Id : %ld\n",name_lsa->header->ls_id);
392 printf(" Origination Time : %s\n",name_lsa->header->orig_time);
393 printf(" Is Valid : %d\n",name_lsa->header->isValid);
394 printf(" LSA Data \n");
395 printf(" Name Prefix: : %s\n",name_lsa->name_prefix->name);
396 printf(" Name Prefix Length : %d\n",name_lsa->name_prefix->length);
akmhoque53f64222012-09-05 13:57:51 -0500397
akmhoque03004e62012-09-06 01:12:28 -0500398 printf("\n");
akmhoquef71d9082012-08-22 12:51:53 -0400399}
400
401void
402print_name_lsdb(void)
403{
404 printf("print_name_lsdb called \n");
akmhoque53f64222012-09-05 13:57:51 -0500405 int i, name_lsdb_element;
akmhoquef71d9082012-08-22 12:51:53 -0400406 struct nlsa *name_lsa;
407
408 struct hashtb_enumerator ee;
409 struct hashtb_enumerator *e = &ee;
410
411 hashtb_start(nlsr->lsdb->name_lsdb, e);
akmhoque53f64222012-09-05 13:57:51 -0500412 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
akmhoquef71d9082012-08-22 12:51:53 -0400413
akmhoque53f64222012-09-05 13:57:51 -0500414 for(i=0;i<name_lsdb_element;i++)
akmhoquef71d9082012-08-22 12:51:53 -0400415 {
akmhoque53f64222012-09-05 13:57:51 -0500416 printf("-----------Name LSA (%d)---------------\n",i+1);
akmhoquef71d9082012-08-22 12:51:53 -0400417 name_lsa=e->data;
akmhoque53f64222012-09-05 13:57:51 -0500418 print_name_lsa(name_lsa);
akmhoquef71d9082012-08-22 12:51:53 -0400419 hashtb_next(e);
420 }
421
422 hashtb_end(e);
423
akmhoque53f64222012-09-05 13:57:51 -0500424 printf("\n");
425}
426
akmhoque03004e62012-09-06 01:12:28 -0500427
428void
429build_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 -0500430{
akmhoque03004e62012-09-06 01:12:28 -0500431 printf("build_and_install_others_name_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500432
akmhoque03004e62012-09-06 01:12:28 -0500433 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
434 build_others_name_lsa(name_lsa,orig_router,ls_type,ls_id,orig_time, isValid,np);
435 print_name_lsa(name_lsa);
436 install_name_lsa(name_lsa);
437 print_name_lsdb();
akmhoquede61ba92012-09-20 22:19:12 -0500438 print_npt();
akmhoque53f64222012-09-05 13:57:51 -0500439
akmhoque03004e62012-09-06 01:12:28 -0500440 free(name_lsa->header->orig_router->name);
441 free(name_lsa->header->orig_router);
442 free(name_lsa->header);
443 free(name_lsa->name_prefix->name);
444 free(name_lsa->name_prefix);
445 free(name_lsa);
akmhoque53f64222012-09-05 13:57:51 -0500446
447}
448
449void
akmhoque03004e62012-09-06 01:12:28 -0500450build_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 -0500451{
akmhoque03004e62012-09-06 01:12:28 -0500452 printf("build_others_name_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500453
akmhoque03004e62012-09-06 01:12:28 -0500454 name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header ));
455 name_lsa->header->ls_type=LS_TYPE_NAME;
akmhoque53f64222012-09-05 13:57:51 -0500456
akmhoque03004e62012-09-06 01:12:28 -0500457 name_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
458 memset(name_lsa->header->orig_time,0,strlen(orig_time)+1);
459 memcpy(name_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
akmhoque53f64222012-09-05 13:57:51 -0500460
akmhoque03004e62012-09-06 01:12:28 -0500461 name_lsa->header->ls_id=ls_id;
462 name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
463 name_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
464 memset(name_lsa->header->orig_router->name,0,strlen(orig_router)+1);
465 memcpy(name_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
466 name_lsa->header->orig_router->length=strlen(orig_router)+1;
467 name_lsa->header->isValid=isValid;
akmhoque53f64222012-09-05 13:57:51 -0500468
akmhoque03004e62012-09-06 01:12:28 -0500469 name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
470 name_lsa->name_prefix->name=(char *)malloc(strlen(np)+1);
471 memset(name_lsa->name_prefix->name,0,strlen(np)+1);
472 memcpy(name_lsa->name_prefix->name,np,strlen(np)+1);
473 name_lsa->name_prefix->length=strlen(np)+1;
akmhoquef71d9082012-08-22 12:51:53 -0400474}
akmhoqued79438d2012-08-27 13:31:42 -0500475
476
akmhoqueffacaa82012-09-13 17:48:30 -0500477
478
akmhoque53f64222012-09-05 13:57:51 -0500479void
480make_adj_lsa_key(char *key,struct alsa *adj_lsa)
akmhoqued79438d2012-08-27 13:31:42 -0500481{
akmhoque53f64222012-09-05 13:57:51 -0500482 memcpy(key+strlen(key),adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
483 memcpy(key+strlen(key),"/",1);
484 char ls_type[2];
485 sprintf(ls_type,"%d",adj_lsa->header->ls_type);
486 memcpy(key+strlen(key),ls_type,strlen(ls_type));
487 key[strlen(key)]='\0';
488}
489
akmhoque03004e62012-09-06 01:12:28 -0500490int
491build_and_install_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
492{
akmhoqueffacaa82012-09-13 17:48:30 -0500493 if(flags == CCN_SCHEDULE_CANCEL)
494 {
495 return -1;
496 }
497
498 nlsr_lock();
499
akmhoque03004e62012-09-06 01:12:28 -0500500 printf("build_and_install_adj_lsa called \n");
501
502 printf("adj_build_flag = %d \n",nlsr->adj_build_flag);
503
504 if(nlsr->adj_build_flag > 0)
505 {
506 printf("is_adj_lsa_build = %d \n",is_adj_lsa_build());
507 if ( is_adj_lsa_build()> 0)
508 {
509 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
510 build_adj_lsa(adj_lsa);
511 install_adj_lsa(adj_lsa);
512
513 free(adj_lsa->header->orig_router->name);
514 free(adj_lsa->header->orig_router);
515 free(adj_lsa->header->orig_time);
516 free(adj_lsa->header);
517 free(adj_lsa->body);
518 free(adj_lsa);
519 nlsr->adj_build_flag=0;
520 print_adj_lsdb();
521 }
522 else
523 {
524 printf("Can not build adj LSA now\n");
525 }
526 }
527 nlsr->is_build_adj_lsa_sheduled=0;
akmhoqueffacaa82012-09-13 17:48:30 -0500528
529 nlsr_unlock();
530
akmhoque03004e62012-09-06 01:12:28 -0500531 return 0;
532}
533
534
535void
536build_adj_lsa(struct alsa * adj_lsa)
537{
538 printf("build_adj_lsa called \n");
539
540 int no_link=no_active_nbr();
541 printf("Number of link in Adjacent LSA: %d\n",no_link);
542
543 /*Filling Up Header Data */
544 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
545 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
546 adj_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1);
547 memset(adj_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
548 memcpy(adj_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
549 adj_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
550
551 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
552
553 char *time_stamp=(char *)malloc(20);
554 memset(time_stamp,0,20);
555 get_current_timestamp_micro(time_stamp);
556
557 adj_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1);
558 memset(adj_lsa->header->orig_time,0,strlen(time_stamp)+1);
559 memcpy(adj_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
560 free(time_stamp);
561
562
563 /* Filling Up Body Data */
564
565 adj_lsa->no_link=no_link;
akmhoque03004e62012-09-06 01:12:28 -0500566
567
568 struct ccn_charbuf *c=ccn_charbuf_create();
569 get_active_nbr_adj_data(c);
570 char *data=ccn_charbuf_as_string(c);
571
572 adj_lsa->body=(char *)malloc(strlen(data)+1);
573 memset(adj_lsa->body,0,strlen(data)+1);
574 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
575 ccn_charbuf_destroy(&c);
576
577
578
579 if( !nlsr->is_send_lsdb_interest_scheduled )
580 {
581 nlsr->event_send_lsdb_interest= ccn_schedule_event(nlsr->sched, 1000, &send_lsdb_interest, NULL, 0);
582 nlsr->is_send_lsdb_interest_scheduled=1;
583 }
584
585 nlsr->adj_build_count++;
586
587
588}
589
590
akmhoque53f64222012-09-05 13:57:51 -0500591void
592install_adj_lsa(struct alsa * adj_lsa)
593{
akmhoqued79438d2012-08-27 13:31:42 -0500594 printf("install_adj_lsa called \n");
595
akmhoque03004e62012-09-06 01:12:28 -0500596
akmhoqueffacaa82012-09-13 17:48:30 -0500597 char *time_stamp=(char *)malloc(20);
598 memset(time_stamp,0,20);
599 get_current_timestamp_micro(time_stamp);
600 long int lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
akmhoque53f64222012-09-05 13:57:51 -0500601
akmhoqueffacaa82012-09-13 17:48:30 -0500602 printf("time difference: %ld \n",lsa_life_time);
akmhoque53f64222012-09-05 13:57:51 -0500603
akmhoqueffacaa82012-09-13 17:48:30 -0500604
605 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
606 memset(key,0,adj_lsa->header->orig_router->length+2);
607 make_adj_lsa_key(key,adj_lsa);
608 printf("Adjacent LSA key: %s \n",key);
609
610 struct alsa *new_adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
611
612 struct hashtb_enumerator ee;
613 struct hashtb_enumerator *e = &ee;
614 int res;
615
616 hashtb_start(nlsr->lsdb->adj_lsdb, e);
617 res = hashtb_seek(e, key, strlen(key), 0);
akmhoque53f64222012-09-05 13:57:51 -0500618
akmhoque03004e62012-09-06 01:12:28 -0500619
akmhoque53f64222012-09-05 13:57:51 -0500620
akmhoque62c0c192012-09-24 07:49:25 -0500621 if(res == HT_NEW_ENTRY)
akmhoque53f64222012-09-05 13:57:51 -0500622 {
akmhoque62c0c192012-09-24 07:49:25 -0500623 if ( adj_lsa->no_link > 0)
624 {
625 printf("New ADJ LSA... Adding to LSDB\n");
626 new_adj_lsa = e->data;
akmhoque53f64222012-09-05 13:57:51 -0500627
akmhoque62c0c192012-09-24 07:49:25 -0500628 new_adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
629 new_adj_lsa->header->ls_type=adj_lsa->header->ls_type;
630 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
631 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 -0500632
akmhoque62c0c192012-09-24 07:49:25 -0500633 new_adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
634 new_adj_lsa->header->orig_router->name=(char *)malloc(adj_lsa->header->orig_router->length);
635 memcpy(new_adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
636 new_adj_lsa->header->orig_router->length=adj_lsa->header->orig_router->length;
akmhoque53f64222012-09-05 13:57:51 -0500637
akmhoque62c0c192012-09-24 07:49:25 -0500638 new_adj_lsa->no_link=adj_lsa->no_link;
akmhoqueffacaa82012-09-13 17:48:30 -0500639
akmhoque62c0c192012-09-24 07:49:25 -0500640 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
641 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
642 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
643
644 add_next_hop_router(new_adj_lsa->header->orig_router->name);
645 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
akmhoque9e9fc722012-09-26 14:03:25 -0500646
647 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
648 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
649 write_log_for_adj_lsa(new_adj_lsa);
650 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoque62c0c192012-09-24 07:49:25 -0500651 }
652 else
653 {
654 hashtb_delete(e);
655 }
akmhoque53f64222012-09-05 13:57:51 -0500656
akmhoque03004e62012-09-06 01:12:28 -0500657 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500658 set_new_lsdb_version();
akmhoque03004e62012-09-06 01:12:28 -0500659 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500660 }
661 else if(res == HT_OLD_ENTRY)
662 {
663 new_adj_lsa = e->data;
664 if(strcmp(adj_lsa->header->orig_time,new_adj_lsa->header->orig_time)<=0)
665 {
666 printf("Older/Duplicate Adj LSA. Discarded...\n");
667 }
668 else
669 {
akmhoqueffacaa82012-09-13 17:48:30 -0500670
akmhoque62c0c192012-09-24 07:49:25 -0500671 if ( adj_lsa->no_link > 0)
672 {
akmhoque9e9fc722012-09-26 14:03:25 -0500673 //new_adj_lsa = e->data;
674
675 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
676 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
677 write_log_for_adj_lsa(new_adj_lsa);
678 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500679
akmhoque62c0c192012-09-24 07:49:25 -0500680 free(new_adj_lsa->header->orig_time);
681 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
682 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
683
684 new_adj_lsa->no_link=adj_lsa->no_link;
akmhoqueffacaa82012-09-13 17:48:30 -0500685
akmhoque62c0c192012-09-24 07:49:25 -0500686 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
687 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
688 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
akmhoqueffacaa82012-09-13 17:48:30 -0500689
akmhoque62c0c192012-09-24 07:49:25 -0500690 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
akmhoque9e9fc722012-09-26 14:03:25 -0500691
692 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
693 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
694 write_log_for_adj_lsa(new_adj_lsa);
695 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoque62c0c192012-09-24 07:49:25 -0500696 }
697 else
698 {
akmhoque9e9fc722012-09-26 14:03:25 -0500699 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
700 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
701 write_log_for_adj_lsa(new_adj_lsa);
702 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
703
akmhoque62c0c192012-09-24 07:49:25 -0500704 hashtb_delete(e);
705 }
akmhoqueffacaa82012-09-13 17:48:30 -0500706 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
707 set_new_lsdb_version();
708 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500709 }
710
711 }
712 hashtb_end(e);
713
714 if ( !nlsr->is_route_calculation_scheduled )
715 {
716 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
717 nlsr->is_route_calculation_scheduled=1;
akmhoque53f64222012-09-05 13:57:51 -0500718 }
719
akmhoque03004e62012-09-06 01:12:28 -0500720
akmhoqueffacaa82012-09-13 17:48:30 -0500721 free(key);
722
723 free(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -0500724}
725
726void
akmhoque9e9fc722012-09-26 14:03:25 -0500727write_log_for_adj_lsa_body(const char *body, int no_link)
728{
729 int i=0;
730 char *lsa_data=(char *)malloc(strlen(body)+1);
731 memset( lsa_data,0,strlen(body)+1);
732 memcpy(lsa_data,body,strlen(body)+1);
733 char *sep="|";
734 char *rem;
735 char *rtr_id;
736 char *length;
737 char *face;
738 char *metric;
739
740 if(no_link >0 )
741 {
742 rtr_id=strtok_r(lsa_data,sep,&rem);
743 length=strtok_r(NULL,sep,&rem);
744 face=strtok_r(NULL,sep,&rem);
745 metric=strtok_r(NULL,sep,&rem);
746
747 writeLogg(__FILE__,__FUNCTION__,__LINE__," Link %d \n",i+1);
akmhoque34b99f92012-09-27 12:24:27 -0500748 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router: %s \n",rtr_id);
749 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router Length: %s \n",length);
750 writeLogg(__FILE__,__FUNCTION__,__LINE__," Connecting Face: %s \n",face);
751 writeLogg(__FILE__,__FUNCTION__,__LINE__," Metric: %s \n",metric);
akmhoque9e9fc722012-09-26 14:03:25 -0500752
753
754 for(i=1;i<no_link;i++)
755 {
756 rtr_id=strtok_r(NULL,sep,&rem);
757 length=strtok_r(NULL,sep,&rem);
758 face=strtok_r(NULL,sep,&rem);
759 metric=strtok_r(NULL,sep,&rem);
760 writeLogg(__FILE__,__FUNCTION__,__LINE__," Link %d \n",i+1);
akmhoque34b99f92012-09-27 12:24:27 -0500761 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router: %s \n",rtr_id);
762 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router Length: %s \n",length);
763 writeLogg(__FILE__,__FUNCTION__,__LINE__," Connecting Face: %s \n",face);
764 writeLogg(__FILE__,__FUNCTION__,__LINE__," Metric: %s \n",metric);
akmhoque9e9fc722012-09-26 14:03:25 -0500765
766 }
767 }
768
769 free(lsa_data);
770}
771
772
773void
774write_log_for_adj_lsa(struct alsa * adj_lsa)
775{
776 writeLogg(__FILE__,__FUNCTION__,__LINE__,"-----------Adj LSA Content---------------\n");
akmhoque34b99f92012-09-27 12:24:27 -0500777 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s\n",adj_lsa->header->orig_router->name);
778 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
779 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Type: %d\n",adj_lsa->header->ls_type);
780 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Time: %s\n",adj_lsa->header->orig_time);
akmhoque9e9fc722012-09-26 14:03:25 -0500781 writeLogg(__FILE__,__FUNCTION__,__LINE__," Lsa Data:\n");
akmhoque34b99f92012-09-27 12:24:27 -0500782 writeLogg(__FILE__,__FUNCTION__,__LINE__," No of Link: %d\n",adj_lsa->no_link);
akmhoque9e9fc722012-09-26 14:03:25 -0500783
784 write_log_for_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
785
786 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
787
788}
789
790void
akmhoque53f64222012-09-05 13:57:51 -0500791print_adj_lsa_body(const char *body, int no_link)
792{
793 int i=0;
794 char *lsa_data=(char *)malloc(strlen(body)+1);
795 memset( lsa_data,0,strlen(body)+1);
796 memcpy(lsa_data,body,strlen(body)+1);
797 char *sep="|";
798 char *rem;
799 char *rtr_id;
800 char *length;
801 char *face;
802 char *metric;
803
akmhoque53f64222012-09-05 13:57:51 -0500804 if(no_link >0 )
805 {
806 rtr_id=strtok_r(lsa_data,sep,&rem);
807 length=strtok_r(NULL,sep,&rem);
808 face=strtok_r(NULL,sep,&rem);
809 metric=strtok_r(NULL,sep,&rem);
810
811 printf(" Link %d \n",i+1);
812 printf(" Neighbor : %s \n",rtr_id);
813 printf(" Neighbor Length : %s \n",length);
814 printf(" Connecting Face : %s \n",face);
815 printf(" Metric : %s \n",metric);
akmhoque03004e62012-09-06 01:12:28 -0500816
akmhoque53f64222012-09-05 13:57:51 -0500817
818 for(i=1;i<no_link;i++)
819 {
820 rtr_id=strtok_r(NULL,sep,&rem);
821 length=strtok_r(NULL,sep,&rem);
822 face=strtok_r(NULL,sep,&rem);
823 metric=strtok_r(NULL,sep,&rem);
824 printf(" Link %d \n",i+1);
825 printf(" Neighbor : %s \n",rtr_id);
826 printf(" Neighbor Length : %s \n",length);
827 printf(" Connecting Face : %s \n",face);
828 printf(" Metric : %s \n",metric);
829
830 }
831 }
832
833 free(lsa_data);
834}
835
836void
837print_adj_lsa(struct alsa * adj_lsa)
838{
akmhoque53f64222012-09-05 13:57:51 -0500839 printf("-----------ADJ LSA Content---------------\n");
840 printf(" Origination Router : %s\n",adj_lsa->header->orig_router->name);
841 printf(" Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
842 printf(" LS Type : %d\n",adj_lsa->header->ls_type);
843 printf(" Origination Time : %s\n",adj_lsa->header->orig_time);
akmhoque03004e62012-09-06 01:12:28 -0500844 printf(" Lsa Data:\n");
845 printf(" No of Link : %d\n",adj_lsa->no_link);
akmhoque53f64222012-09-05 13:57:51 -0500846
847 print_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
akmhoque53f64222012-09-05 13:57:51 -0500848 printf("\n");
849
850}
851
852void
853print_adj_lsdb(void)
854{
855 printf("print_name_lsdb called \n");
856 int i, adj_lsdb_element;
857 struct alsa *adj_lsa;
858
859 struct hashtb_enumerator ee;
860 struct hashtb_enumerator *e = &ee;
861
862 hashtb_start(nlsr->lsdb->adj_lsdb, e);
863 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
864
865 for(i=0;i<adj_lsdb_element;i++)
866 {
867 printf("-----------Adj LSA (%d)---------------\n",i+1);
868 adj_lsa=e->data;
869 print_adj_lsa(adj_lsa);
870 hashtb_next(e);
871 }
872
873 hashtb_end(e);
874
875 printf("\n");
akmhoque53f64222012-09-05 13:57:51 -0500876}
877
878void
akmhoque03004e62012-09-06 01:12:28 -0500879build_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 -0500880{
akmhoque03004e62012-09-06 01:12:28 -0500881 printf("build_and_install_others_adj_lsa called \n");
882 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
883 build_others_adj_lsa(adj_lsa,orig_router,ls_type,orig_time,no_link,data);
884 //print_adj_lsa(adj_lsa);
885 install_adj_lsa(adj_lsa);
886
akmhoque53f64222012-09-05 13:57:51 -0500887
akmhoque03004e62012-09-06 01:12:28 -0500888 free(adj_lsa->header->orig_router->name);
889 free(adj_lsa->header->orig_router);
890 free(adj_lsa->header->orig_time);
891 free(adj_lsa->header);
892 free(adj_lsa->body);
893 free(adj_lsa);
akmhoque53f64222012-09-05 13:57:51 -0500894
akmhoque03004e62012-09-06 01:12:28 -0500895 print_adj_lsdb();
akmhoque53f64222012-09-05 13:57:51 -0500896
akmhoque53f64222012-09-05 13:57:51 -0500897}
898
akmhoque03004e62012-09-06 01:12:28 -0500899
akmhoque53f64222012-09-05 13:57:51 -0500900void
akmhoque03004e62012-09-06 01:12:28 -0500901build_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 -0500902{
akmhoque03004e62012-09-06 01:12:28 -0500903 printf("build_others_adj_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500904
akmhoque03004e62012-09-06 01:12:28 -0500905 /*Filling Up Header Data */
906 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
907 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
908 adj_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
909 memset(adj_lsa->header->orig_router->name,0,strlen(orig_router)+1);
910 memcpy(adj_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
akmhoque53f64222012-09-05 13:57:51 -0500911
akmhoque03004e62012-09-06 01:12:28 -0500912 adj_lsa->header->orig_router->length=strlen(orig_router)+1;
akmhoque53f64222012-09-05 13:57:51 -0500913
akmhoque53f64222012-09-05 13:57:51 -0500914
akmhoque03004e62012-09-06 01:12:28 -0500915 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
akmhoque53f64222012-09-05 13:57:51 -0500916
akmhoque03004e62012-09-06 01:12:28 -0500917 adj_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
918 memset(adj_lsa->header->orig_time,0,strlen(orig_time)+1);
919 memcpy(adj_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
akmhoque53f64222012-09-05 13:57:51 -0500920
akmhoque03004e62012-09-06 01:12:28 -0500921 adj_lsa->no_link=no_link;
akmhoque53f64222012-09-05 13:57:51 -0500922
akmhoque03004e62012-09-06 01:12:28 -0500923 adj_lsa->body=(char *)malloc(strlen(data)+1);
924 memset(adj_lsa->body,0,strlen(data)+1);
925 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
akmhoque53f64222012-09-05 13:57:51 -0500926
akmhoque53f64222012-09-05 13:57:51 -0500927}
928
akmhoque03004e62012-09-06 01:12:28 -0500929
akmhoque53f64222012-09-05 13:57:51 -0500930long int
931get_name_lsdb_num_element(void)
932{
933 long int num_element;
934
935
936 struct hashtb_enumerator ee;
937 struct hashtb_enumerator *e = &ee;
938
939 hashtb_start(nlsr->lsdb->name_lsdb, e);
940 num_element=hashtb_n(nlsr->lsdb->name_lsdb);
941 hashtb_end(e);
942
943 return num_element;
944}
945
946long int
947get_adj_lsdb_num_element(void)
948{
949 long int num_element;
950
951
952 struct hashtb_enumerator ee;
953 struct hashtb_enumerator *e = &ee;
954
955 hashtb_start(nlsr->lsdb->adj_lsdb, e);
956 num_element=hashtb_n(nlsr->lsdb->adj_lsdb);
957 hashtb_end(e);
958
959 return num_element;
akmhoqued79438d2012-08-27 13:31:42 -0500960}
akmhoque03004e62012-09-06 01:12:28 -0500961
962void
963get_name_lsdb_summary(struct ccn_charbuf *name_lsdb_data)
964{
965 printf("get_name_lsdb_summary called \n");
966 int i, name_lsdb_element;
967
968 struct nlsa *name_lsa;
969 struct hashtb_enumerator ee;
970 struct hashtb_enumerator *e = &ee;
971
972 hashtb_start(nlsr->lsdb->name_lsdb, e);
973 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
974
975 for(i=0;i<name_lsdb_element;i++)
976 {
977 name_lsa=e->data;
978
979 ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_router->name);
980 ccn_charbuf_append_string(name_lsdb_data,"|");
981
982 char *lst=(char *)malloc(20);
983 memset(lst,0,20);
984 sprintf(lst,"%d",name_lsa->header->ls_type);
985 ccn_charbuf_append_string(name_lsdb_data,lst);
986 free(lst);
987 ccn_charbuf_append_string(name_lsdb_data,"|");
988
989 char *lsid=(char *)malloc(20);
990 memset(lsid,0,20);
991 sprintf(lsid,"%ld",name_lsa->header->ls_id);
992 ccn_charbuf_append_string(name_lsdb_data,lsid);
993 free(lsid);
994 ccn_charbuf_append_string(name_lsdb_data,"|");
995
996 ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_time);
997 ccn_charbuf_append_string(name_lsdb_data,"|");
998
999 hashtb_next(e);
1000 }
1001
1002 hashtb_end(e);
1003
1004}
1005
1006
1007void
1008get_adj_lsdb_summary(struct ccn_charbuf *adj_lsdb_data)
1009{
1010 printf("get_adj_lsdb_summary called \n");
1011 int i, adj_lsdb_element;
1012 struct alsa *adj_lsa;
1013
1014 struct hashtb_enumerator ee;
1015 struct hashtb_enumerator *e = &ee;
1016
1017 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1018 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1019
1020 for(i=0;i<adj_lsdb_element;i++)
1021 {
1022 adj_lsa=e->data;
1023
1024 ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_router->name);
1025 ccn_charbuf_append_string(adj_lsdb_data,"|");
1026
1027 char *lst=(char *)malloc(20);
1028 memset(lst,0,20);
1029 sprintf(lst,"%d",adj_lsa->header->ls_type);
1030 ccn_charbuf_append_string(adj_lsdb_data,lst);
1031 free(lst);
1032 ccn_charbuf_append_string(adj_lsdb_data,"|");
1033
1034 ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_time);
1035 ccn_charbuf_append_string(adj_lsdb_data,"|");
1036
1037 hashtb_next(e);
1038 }
1039
1040 hashtb_end(e);
1041}
1042
1043
1044void
1045get_lsdb_summary(struct ccn_charbuf *lsdb_data)
1046{
1047 struct ccn_charbuf *name_lsdb_data=ccn_charbuf_create();
1048 struct ccn_charbuf *adj_lsdb_data=ccn_charbuf_create();
1049
1050 get_name_lsdb_summary(name_lsdb_data);
1051 get_adj_lsdb_summary(adj_lsdb_data);
1052
1053 long int num_lsa=get_name_lsdb_num_element() + get_adj_lsdb_num_element();
1054 char *num_element=(char *)malloc(15);
1055 memset(num_element,0,15);
1056 sprintf(num_element,"%ld",num_lsa);
1057
1058 if( num_lsa > 0)
1059 {
1060 ccn_charbuf_append_string(lsdb_data,num_element);
1061 ccn_charbuf_append_string(lsdb_data,"|");
1062 }
1063 if(name_lsdb_data->length>0)
1064 {
1065 char *data1=ccn_charbuf_as_string(name_lsdb_data);
1066 ccn_charbuf_append_string(lsdb_data,(char *)data1);
1067 }
1068 if(adj_lsdb_data->length>0)
1069 {
1070 char *data2=ccn_charbuf_as_string(adj_lsdb_data);
1071 ccn_charbuf_append_string(lsdb_data,(char *)data2);
1072 }
1073 ccn_charbuf_destroy(&name_lsdb_data);
1074 ccn_charbuf_destroy(&adj_lsdb_data);
1075 free(num_element);
1076
1077}
1078
1079int
1080check_is_new_name_lsa(char *orig_router,char *lst,char *lsid,char *orig_time)
1081{
1082 int ret=0;
1083 struct ccn_charbuf *key=ccn_charbuf_create();
1084 ccn_charbuf_append_string(key,orig_router);
1085 ccn_charbuf_append_string(key,"/");
1086 ccn_charbuf_append_string(key,lst);
1087 ccn_charbuf_append_string(key,"/");
1088 ccn_charbuf_append_string(key,lsid);
1089
1090 int res;
1091 struct nlsa *name_lsa;
1092
1093 struct hashtb_enumerator ee;
1094 struct hashtb_enumerator *e = &ee;
1095
1096 hashtb_start(nlsr->lsdb->name_lsdb, e);
1097 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1098
1099 if( res == HT_NEW_ENTRY )
1100 {
1101 hashtb_delete(e);
1102 ret=1;
1103
1104 }
1105 else if(res == HT_OLD_ENTRY)
1106 {
1107 name_lsa=e->data;
1108 if( strcmp ( orig_time , name_lsa->header->orig_time ) > 0 )
1109 {
1110 ret=1;
1111 }
1112 }
1113
1114 hashtb_end(e);
1115
1116 ccn_charbuf_destroy(&key);
1117
1118 return ret;
1119}
1120
1121int
1122check_is_new_adj_lsa(char *orig_router,char *lst,char *orig_time)
1123{
1124 int ret=0;
1125 struct ccn_charbuf *key=ccn_charbuf_create();
1126 ccn_charbuf_append_string(key,orig_router);
1127 ccn_charbuf_append_string(key,"/");
1128 ccn_charbuf_append_string(key,lst);
1129
1130 int res;
1131 struct alsa *adj_lsa;
1132
1133 struct hashtb_enumerator ee;
1134 struct hashtb_enumerator *e = &ee;
1135
1136 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1137 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1138
1139 if( res == HT_NEW_ENTRY )
1140 {
1141 hashtb_delete(e);
1142 ret=1;
1143
1144 }
1145 else if(res == HT_OLD_ENTRY)
1146 {
1147 adj_lsa=e->data;
1148 if( strcmp ( orig_time , adj_lsa->header->orig_time ) > 0 )
1149 {
1150 ret=1;
1151 }
1152 }
1153
1154 hashtb_end(e);
1155
1156 ccn_charbuf_destroy(&key);
1157
1158 return ret;
1159}
1160
1161void
1162get_name_lsa_data(struct ccn_charbuf *lsa_data, struct name_prefix *lsaId)
1163{
1164 printf("get_name_lsa_data called \n");
1165
1166 struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa ));
1167
1168 struct hashtb_enumerator ee;
1169 struct hashtb_enumerator *e = &ee;
1170 int res;
1171
1172 hashtb_start(nlsr->lsdb->name_lsdb, e);
1173 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1174
1175 if( res == HT_OLD_ENTRY )
1176 {
1177 name_lsa=e->data;
1178 printf("NAME LSA found\n");
1179
1180 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_router->name);
1181 ccn_charbuf_append_string(lsa_data,"|");
1182
1183 char *temp_length=(char *)malloc(20);
1184 memset(temp_length,0,20);
1185 sprintf(temp_length,"%d",name_lsa->header->orig_router->length);
1186 ccn_charbuf_append_string(lsa_data,temp_length);
1187 free(temp_length);
1188 ccn_charbuf_append_string(lsa_data,"|");
1189
1190 char *temp_ltype=(char *)malloc(20);
1191 memset(temp_ltype,0,20);
1192 sprintf(temp_ltype,"%d",name_lsa->header->ls_type);
1193 ccn_charbuf_append_string(lsa_data,temp_ltype);
1194 free(temp_ltype);
1195 ccn_charbuf_append_string(lsa_data,"|");
1196
1197 char *temp_lsid=(char *)malloc(20);
1198 memset(temp_lsid,0,20);
1199 sprintf(temp_lsid,"%ld",name_lsa->header->ls_id);
1200 ccn_charbuf_append_string(lsa_data,temp_lsid);
1201 free(temp_lsid);
1202 ccn_charbuf_append_string(lsa_data,"|");
1203
1204 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_time);
1205 ccn_charbuf_append_string(lsa_data,"|");
1206
1207 char *temp_valid=(char *)malloc(20);
1208 memset(temp_valid,0,20);
1209 sprintf(temp_valid,"%d",name_lsa->header->isValid);
1210 ccn_charbuf_append_string(lsa_data,temp_valid);
1211 free(temp_valid);
1212 ccn_charbuf_append_string(lsa_data,"|");
1213
1214 ccn_charbuf_append_string(lsa_data,name_lsa->name_prefix->name);
1215 ccn_charbuf_append_string(lsa_data,"|");
1216
1217 char *temp_npl=(char *)malloc(20);
1218 memset(temp_npl,0,20);
1219 sprintf(temp_npl,"%d",name_lsa->name_prefix->length);
1220 ccn_charbuf_append_string(lsa_data,temp_npl);
1221 free(temp_npl);
1222 ccn_charbuf_append_string(lsa_data,"|");
1223
1224 }
1225 else if(res == HT_NEW_ENTRY)
1226 {
1227 hashtb_delete(e);
1228 }
1229
1230 hashtb_end(e);
1231}
1232
1233void
1234get_adj_lsa_data(struct ccn_charbuf *lsa_data,struct name_prefix *lsaId)
1235{
1236 printf("get_adj_lsa_data called \n");
1237
1238 struct alsa *adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
1239
1240 struct hashtb_enumerator ee;
1241 struct hashtb_enumerator *e = &ee;
1242 int res;
1243
1244 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1245 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1246
1247 if( res == HT_OLD_ENTRY )
1248 {
1249 adj_lsa=e->data;
1250 printf("NAME LSA found\n");
1251
1252 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_router->name);
1253 ccn_charbuf_append_string(lsa_data,"|");
1254
1255 char *temp_length=(char *)malloc(20);
1256 memset(temp_length,0,20);
1257 sprintf(temp_length,"%d",adj_lsa->header->orig_router->length);
1258 ccn_charbuf_append_string(lsa_data,temp_length);
1259 free(temp_length);
1260 ccn_charbuf_append_string(lsa_data,"|");
1261
1262 char *temp_ltype=(char *)malloc(20);
1263 memset(temp_ltype,0,20);
1264 sprintf(temp_ltype,"%d",adj_lsa->header->ls_type);
1265 ccn_charbuf_append_string(lsa_data,temp_ltype);
1266 free(temp_ltype);
1267 ccn_charbuf_append_string(lsa_data,"|");
1268
1269 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_time);
1270 ccn_charbuf_append_string(lsa_data,"|");
1271
1272 char *temp_nl=(char *)malloc(20);
1273 memset(temp_nl,0,20);
1274 sprintf(temp_nl,"%d",adj_lsa->no_link);
1275 ccn_charbuf_append_string(lsa_data,temp_nl);
1276 free(temp_nl);
1277 ccn_charbuf_append_string(lsa_data,"|");
1278
1279 ccn_charbuf_append_string(lsa_data,adj_lsa->body);
1280
1281
1282 }
1283 else if(res == HT_NEW_ENTRY)
1284 {
1285 hashtb_delete(e);
1286 }
1287
1288 hashtb_end(e);
1289}
akmhoqueffacaa82012-09-13 17:48:30 -05001290
akmhoqueda5b6832012-09-13 22:33:55 -05001291int
1292delete_name_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1293{
1294 printf("delete_name_lsa called \n");
1295
1296 if(flags == CCN_SCHEDULE_CANCEL)
1297 {
1298 return -1;
1299 }
1300
1301
1302
1303 nlsr_lock();
1304
1305 printf("LSA Key: %s \n",(char *)ev->evdata);
1306
1307 struct nlsa *nlsa;
1308
1309 struct hashtb_enumerator ee;
1310 struct hashtb_enumerator *e = &ee;
1311
1312 int res;
1313
1314 hashtb_start(nlsr->lsdb->name_lsdb, e);
1315 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1316
1317 if( res == HT_OLD_ENTRY )
1318 {
1319 nlsa=e->data;
akmhoque3cced642012-09-24 16:20:20 -05001320 delete_npt_entry_by_router_and_name_prefix(nlsa->header->orig_router->name, nlsa->name_prefix->name);
akmhoque9e9fc722012-09-26 14:03:25 -05001321
1322 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1323 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1324 write_log_for_name_lsa(nlsa);
1325 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1326
akmhoqueda5b6832012-09-13 22:33:55 -05001327 hashtb_delete(e);
1328 }
akmhoque3cced642012-09-24 16:20:20 -05001329 else if( res == HT_NEW_ENTRY )
akmhoqueda5b6832012-09-13 22:33:55 -05001330 {
akmhoqueda5b6832012-09-13 22:33:55 -05001331 hashtb_delete(e);
1332 }
1333 hashtb_end(e);
1334
1335
1336 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1337 set_new_lsdb_version();
1338 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1339
1340 //print_name_lsdb();
1341
1342 nlsr_unlock();
1343
1344 return 0;
1345}
1346
1347int
1348delete_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1349{
1350 printf("delete_adj_lsa called \n");
1351
1352 if(flags == CCN_SCHEDULE_CANCEL)
1353 {
1354 return -1;
1355 }
1356 nlsr_lock();
1357
1358 printf("LSA Key: %s \n",(char *)ev->evdata);
1359
akmhoque9e9fc722012-09-26 14:03:25 -05001360 struct alsa *alsa;
akmhoqueda5b6832012-09-13 22:33:55 -05001361 struct hashtb_enumerator ee;
1362 struct hashtb_enumerator *e = &ee;
1363 int res;
1364
1365 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1366 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1367
1368 if( res == HT_OLD_ENTRY )
1369 {
akmhoque9e9fc722012-09-26 14:03:25 -05001370 alsa=e->data;
1371 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1372 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
1373 write_log_for_adj_lsa(alsa);
1374 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1375
akmhoqueda5b6832012-09-13 22:33:55 -05001376 hashtb_delete(e);
1377 }
1378 else if( res == HT_OLD_ENTRY )
1379 {
1380 hashtb_delete(e);
1381 }
1382 hashtb_end(e);
1383
1384 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1385 set_new_lsdb_version();
1386 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1387
1388 if ( !nlsr->is_route_calculation_scheduled)
1389 {
1390 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
1391 nlsr->is_route_calculation_scheduled=1;
1392 }
1393
1394 //print_adj_lsdb();
1395
1396 nlsr_unlock();
1397
1398 return 0;
1399}
1400
akmhoqueffacaa82012-09-13 17:48:30 -05001401void
1402refresh_name_lsdb(void)
1403{
1404 printf("refresh_name_lsdb called \n");
1405
akmhoqueda5b6832012-09-13 22:33:55 -05001406 //int lsa_change_count=0;
1407
akmhoqueffacaa82012-09-13 17:48:30 -05001408 char *time_stamp=(char *)malloc(20);
1409 memset(time_stamp,0,20);
1410 get_current_timestamp_micro(time_stamp);
1411
1412 long int lsa_life_time;
1413
1414 int i, name_lsdb_element;
1415 struct nlsa *name_lsa;
1416
1417 struct hashtb_enumerator ee;
1418 struct hashtb_enumerator *e = &ee;
1419
1420 hashtb_start(nlsr->lsdb->name_lsdb, e);
1421 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
1422
1423 for(i=0;i<name_lsdb_element;i++)
1424 {
1425 name_lsa=e->data;
1426
1427 lsa_life_time=get_time_diff(time_stamp,name_lsa->header->orig_time);
akmhoqueda5b6832012-09-13 22:33:55 -05001428 printf("LSA Life Time: %ld \n",lsa_life_time);
1429
1430 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name) == 0)
1431 {
1432 if ( lsa_life_time > nlsr->lsa_refresh_time )
1433 {
akmhoque14b3f342012-09-14 10:39:02 -05001434 if ( name_lsa->header->isValid == NAME_LSA_VALID )
1435 {
1436 printf("Own Name LSA need to be refrshed\n");
1437 char *current_time_stamp=(char *)malloc(20);
1438 memset(current_time_stamp,0,20);
1439 get_current_timestamp_micro(current_time_stamp);
akmhoqueda5b6832012-09-13 22:33:55 -05001440
akmhoque14b3f342012-09-14 10:39:02 -05001441 free(name_lsa->header->orig_time);
1442 name_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1443 memset(name_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1444 memcpy(name_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
akmhoqueda5b6832012-09-13 22:33:55 -05001445
akmhoque14b3f342012-09-14 10:39:02 -05001446 free(current_time_stamp);
1447 }
1448 else
1449 {
1450 char lst[2];
1451 memset(lst,0,2);
1452 sprintf(lst,"%d",name_lsa->header->ls_type);
1453
1454 char lsid[10];
1455 memset(lsid,0,10);
1456 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1457
1458
1459 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1460 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1461
1462
1463 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
1464 printf("Key:%s Length:%d\n",key,(int)strlen(key));
1465
1466 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
1467 }
akmhoqueda5b6832012-09-13 22:33:55 -05001468
1469 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1470 set_new_lsdb_version();
1471 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1472
1473 print_name_lsdb();
1474 //lsa_change_count++;
1475 }
1476 }
1477 else
1478 {
1479 if ( lsa_life_time > nlsr->router_dead_interval )
1480 {
1481 printf("Others Name LSA need to be deleted\n");
1482
1483 char lst[2];
1484 memset(lst,0,2);
1485 sprintf(lst,"%d",name_lsa->header->ls_type);
1486
1487 char lsid[10];
1488 memset(lsid,0,10);
1489 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1490
1491
1492 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1493 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1494
1495
1496 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
1497 printf("Key:%s Length:%d\n",key,(int)strlen(key));
1498
1499 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
1500 //lsa_change_count++;
1501 }
1502 }
akmhoqueffacaa82012-09-13 17:48:30 -05001503
1504 hashtb_next(e);
1505 }
1506
1507 hashtb_end(e);
1508
1509 free(time_stamp);
akmhoqueda5b6832012-09-13 22:33:55 -05001510
akmhoqueffacaa82012-09-13 17:48:30 -05001511
1512}
1513
akmhoqueda5b6832012-09-13 22:33:55 -05001514void
akmhoqueffacaa82012-09-13 17:48:30 -05001515refresh_adj_lsdb(void)
1516{
1517 printf("refresh_adj_lsdb called \n");
1518
akmhoqueda5b6832012-09-13 22:33:55 -05001519
akmhoqueffacaa82012-09-13 17:48:30 -05001520 char *time_stamp=(char *)malloc(20);
1521 memset(time_stamp,0,20);
1522 get_current_timestamp_micro(time_stamp);
1523
1524 long int lsa_life_time;
1525
1526 int i, adj_lsdb_element;
1527 struct alsa *adj_lsa;
1528
1529 struct hashtb_enumerator ee;
1530 struct hashtb_enumerator *e = &ee;
1531
1532 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1533 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1534
1535 for(i=0;i<adj_lsdb_element;i++)
1536 {
1537 adj_lsa=e->data;
1538
1539 lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
1540 printf("LSA Life Time: %ld \n",lsa_life_time);
1541
akmhoqueda5b6832012-09-13 22:33:55 -05001542 if ( strcmp(adj_lsa->header->orig_router->name,nlsr->router_name) == 0)
1543 {
1544 if ( lsa_life_time > nlsr->lsa_refresh_time )
1545 {
1546 printf("Own Adj LSA need to be refrshed\n");
1547
1548 char *current_time_stamp=(char *)malloc(20);
1549 memset(current_time_stamp,0,20);
1550 get_current_timestamp_micro(current_time_stamp);
1551
1552 free(adj_lsa->header->orig_time);
1553 adj_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1554 memset(adj_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1555 memcpy(adj_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1556
1557 free(current_time_stamp);
1558
1559
1560 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1561 set_new_lsdb_version();
1562 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1563
1564 print_adj_lsdb();
1565 }
1566 }
1567 else
1568 {
1569 if ( lsa_life_time > nlsr->router_dead_interval )
1570 {
1571 printf("Others Adj LSA need to be deleted\n");
1572
1573 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
1574 memset(key,0,adj_lsa->header->orig_router->length+2);
1575 make_adj_lsa_key(key,adj_lsa);
1576 printf("Adjacent LSA key: %s \n",key);
akmhoqueda5b6832012-09-13 22:33:55 -05001577 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_adj_lsa, (void *)key, 0);
1578 }
1579 }
1580
1581
1582
akmhoqueffacaa82012-09-13 17:48:30 -05001583 hashtb_next(e);
1584 }
1585
1586 hashtb_end(e);
1587
1588 free(time_stamp);
akmhoqueffacaa82012-09-13 17:48:30 -05001589}
1590
1591int
1592refresh_lsdb(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1593{
1594 if(flags == CCN_SCHEDULE_CANCEL)
1595 {
1596 return -1;
1597 }
1598
1599 nlsr_lock();
akmhoqueda5b6832012-09-13 22:33:55 -05001600
akmhoqueffacaa82012-09-13 17:48:30 -05001601 printf("refresh_lsdb called \n");
1602
1603 refresh_name_lsdb();
1604 refresh_adj_lsdb();
1605
akmhoqueffacaa82012-09-13 17:48:30 -05001606 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoqueda5b6832012-09-13 22:33:55 -05001607
akmhoqueffacaa82012-09-13 17:48:30 -05001608 nlsr_unlock();
1609 return 0;
1610}