blob: edf5728d98d039aa44f297d39dbe217c801af451 [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"
akmhoqueb77b95f2013-02-08 12:28:47 -060028#include "nlsr_sync.h"
akmhoque438b07e2012-08-21 10:13:57 -040029
akmhoque0ab71642013-02-21 10:10:33 -060030/**
31* LSDB version is updated to last updated timestamp
32*/
33
akmhoquef71d9082012-08-22 12:51:53 -040034void
akmhoque53f64222012-09-05 13:57:51 -050035set_new_lsdb_version(void)
akmhoquef71d9082012-08-22 12:51:53 -040036{
akmhoque03004e62012-09-06 01:12:28 -050037 char *time_stamp=(char *)malloc(20);
38 memset(time_stamp,0,20);
39 get_current_timestamp_micro(time_stamp);
akmhoquef71d9082012-08-22 12:51:53 -040040
akmhoque03004e62012-09-06 01:12:28 -050041 free(nlsr->lsdb->lsdb_version);
42 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
43 memset(nlsr->lsdb->lsdb_version,0,strlen(time_stamp)+1);
44 memcpy(nlsr->lsdb->lsdb_version,time_stamp,strlen(time_stamp)+1);
45
46 free(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -050047
akmhoquef71d9082012-08-22 12:51:53 -040048}
49
akmhoque0ab71642013-02-21 10:10:33 -060050/**
51* Make key for storing Name LSA in Name LSDB
52*/
53
akmhoque03004e62012-09-06 01:12:28 -050054void
55make_name_lsa_key(char *key, char *orig_router, int ls_type, long int ls_id)
56{
57
akmhoque03004e62012-09-06 01:12:28 -050058 char lst[2];
59 memset(lst,0,2);
60 sprintf(lst,"%d",ls_type);
61
62 char lsid[10];
63 memset(lsid,0,10);
64 sprintf(lsid,"%ld",ls_id);
65
66 memcpy(key+strlen(key),orig_router,strlen(orig_router));
67 memcpy(key+strlen(key),"/",1);
68 memcpy(key+strlen(key),lst,strlen(lst));
69 memcpy(key+strlen(key),"/",1);
70 memcpy(key+strlen(key),lsid,strlen(lsid));
akmhoque03004e62012-09-06 01:12:28 -050071
akmhoqueb77b95f2013-02-08 12:28:47 -060072 if ( nlsr->debugging )
73 printf("name LSA Key: %s\n", key);
74}
75
akmhoque0ab71642013-02-21 10:10:33 -060076/**
77* Make content name prefix for Name LSA to store in repo
78*/
akmhoqueb77b95f2013-02-08 12:28:47 -060079
80void
akmhoque0ab71642013-02-21 10:10:33 -060081make_name_lsa_prefix_for_repo(char *key, char *orig_router, int ls_type,
82 long int ls_id,char *orig_time,char *slice_prefix)
akmhoqueb77b95f2013-02-08 12:28:47 -060083{
akmhoque0ab71642013-02-21 10:10:33 -060084 sprintf(key,"%s%s/lsType.%d/lsId.%ld/%s",slice_prefix, orig_router, ls_type,
85 ls_id, orig_time);
akmhoqueb77b95f2013-02-08 12:28:47 -060086
87 if ( nlsr->debugging )
88 printf("Name LSA prefix for repo content: %s\n",key);
89}
90
akmhoque0ab71642013-02-21 10:10:33 -060091/**
92* Make content name prefix for Adj LSA to store in repo
93*/
94
akmhoqueb77b95f2013-02-08 12:28:47 -060095void
akmhoque0ab71642013-02-21 10:10:33 -060096make_adj_lsa_prefix_for_repo(char *key, char *orig_router, int ls_type,
97 char *orig_time,char *slice_prefix)
akmhoqueb77b95f2013-02-08 12:28:47 -060098{
99
100 sprintf(key,"%s%s/lsType.%d/%s",slice_prefix,orig_router,ls_type, orig_time );
101
102 if ( nlsr->debugging )
103 printf("Name LSA prefix for repo content:%s\n",key);
104}
105
akmhoque0ab71642013-02-21 10:10:33 -0600106/**
107* Make content name prefix for Cor LSA to store in repo
108*/
109
akmhoqueb77b95f2013-02-08 12:28:47 -0600110void
akmhoque0ab71642013-02-21 10:10:33 -0600111make_cor_lsa_prefix_for_repo(char *key, char *orig_router, int ls_type,
112 char *orig_time,char *slice_prefix)
akmhoqueb77b95f2013-02-08 12:28:47 -0600113{
114
115 sprintf(key,"%s%s/lsType.%d/%s",slice_prefix,orig_router,ls_type, orig_time );
116
117 if ( nlsr->debugging )
118 printf("Cor LSA prefix for repo content:%s\n",key);
akmhoque03004e62012-09-06 01:12:28 -0500119}
akmhoque53f64222012-09-05 13:57:51 -0500120
akmhoque0ab71642013-02-21 10:10:33 -0600121/**
122* Build name lsa for all name prefixes in Name Prefix List (NPL). Intsall Name
123* LSA in Name LSDB for router itself.
124*/
125
126
akmhoquef71d9082012-08-22 12:51:53 -0400127void
akmhoque53f64222012-09-05 13:57:51 -0500128build_and_install_name_lsas(void)
akmhoquef71d9082012-08-22 12:51:53 -0400129{
akmhoque7b791452012-10-30 11:24:56 -0500130 if ( nlsr->debugging )
131 printf("build_and_install_name_lsas called \n");
132 if ( nlsr->detailed_logging )
133 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_name_lsas called\n");
akmhoquef71d9082012-08-22 12:51:53 -0400134
akmhoquef71d9082012-08-22 12:51:53 -0400135 int i, npl_element;
akmhoque3171d652012-11-13 11:44:33 -0600136 struct name_prefix_list_entry *npe;
akmhoquef71d9082012-08-22 12:51:53 -0400137
138 struct hashtb_enumerator ee;
139 struct hashtb_enumerator *e = &ee;
140
141 hashtb_start(nlsr->npl, e);
142 npl_element=hashtb_n(nlsr->npl);
143
144 for(i=0;i<npl_element;i++)
145 {
akmhoque3171d652012-11-13 11:44:33 -0600146 npe=e->data;
akmhoque53f64222012-09-05 13:57:51 -0500147 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
akmhoque3171d652012-11-13 11:44:33 -0600148 build_name_lsa(name_lsa,npe->np);
akmhoque03004e62012-09-06 01:12:28 -0500149
akmhoquef71d9082012-08-22 12:51:53 -0400150 install_name_lsa(name_lsa);
akmhoque3171d652012-11-13 11:44:33 -0600151 update_nlsa_id_for_name_in_npl(npe->np,name_lsa->header->ls_id);
akmhoque03004e62012-09-06 01:12:28 -0500152 free(name_lsa->header->orig_router->name);
153 free(name_lsa->header->orig_router);
154 free(name_lsa->header);
155 free(name_lsa->name_prefix->name);
156 free(name_lsa->name_prefix);
akmhoquef71d9082012-08-22 12:51:53 -0400157 free(name_lsa);
akmhoquef71d9082012-08-22 12:51:53 -0400158 hashtb_next(e);
159 }
160
akmhoque53f64222012-09-05 13:57:51 -0500161 hashtb_end(e);
akmhoque3171d652012-11-13 11:44:33 -0600162
163 print_name_prefix_from_npl();
164
165}
166
akmhoque0ab71642013-02-21 10:10:33 -0600167/**
168* Build and Install one Name LSA Use ful for API
169*/
170
akmhoque3171d652012-11-13 11:44:33 -0600171void
172build_and_install_single_name_lsa(struct name_prefix *np)
173{
174 if ( nlsr->debugging )
175 printf("build_and_install_single_name_lsa called \n");
176 if ( nlsr->detailed_logging )
177 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_single_name_lsa called\n");
178
179 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
180 build_name_lsa(name_lsa,np);
181
182 install_name_lsa(name_lsa);
183 update_nlsa_id_for_name_in_npl(np,name_lsa->header->ls_id);
184
185 free(name_lsa->header->orig_router->name);
186 free(name_lsa->header->orig_router);
187 free(name_lsa->header);
188 free(name_lsa->name_prefix->name);
189 free(name_lsa->name_prefix);
190 free(name_lsa);
191
192 print_name_prefix_from_npl();
akmhoquef71d9082012-08-22 12:51:53 -0400193
akmhoquef71d9082012-08-22 12:51:53 -0400194}
195
akmhoque53f64222012-09-05 13:57:51 -0500196void
197build_name_lsa(struct nlsa *name_lsa, struct name_prefix *np)
198{
akmhoque53f64222012-09-05 13:57:51 -0500199 name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header ));
200 name_lsa->header->ls_type=LS_TYPE_NAME;
201
akmhoque03004e62012-09-06 01:12:28 -0500202 char *time_stamp=(char *)malloc(20);
203 memset(time_stamp,0,20);
204 get_current_timestamp_micro(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -0500205
akmhoque03004e62012-09-06 01:12:28 -0500206 name_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1); //free
akmhoque53f64222012-09-05 13:57:51 -0500207 memset(name_lsa->header->orig_time,0,strlen(time_stamp)+1);
208 memcpy(name_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
akmhoque03004e62012-09-06 01:12:28 -0500209
210 free(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -0500211
212 name_lsa->header->ls_id=++nlsr->nlsa_id;
213 name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoque03004e62012-09-06 01:12:28 -0500214 name_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1);
215 memset(name_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
216 memcpy(name_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
217 name_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
akmhoque53f64222012-09-05 13:57:51 -0500218 name_lsa->header->isValid=1;
219
220
221 name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
222 name_lsa->name_prefix->name=(char *)malloc(np->length);
223 memcpy(name_lsa->name_prefix->name,np->name,np->length);
224 name_lsa->name_prefix->length=np->length;
225
226}
227
akmhoque53f64222012-09-05 13:57:51 -0500228void
229install_name_lsa(struct nlsa *name_lsa)
230{
akmhoque53f64222012-09-05 13:57:51 -0500231
akmhoqueffacaa82012-09-13 17:48:30 -0500232 char *time_stamp=(char *)malloc(20);
233 memset(time_stamp,0,20);
234 get_current_timestamp_micro(time_stamp);
akmhoqueffacaa82012-09-13 17:48:30 -0500235
236
akmhoque0ab71642013-02-21 10:10:33 -0600237 char lst[2];
238 memset(lst,0,2);
239 sprintf(lst,"%d",name_lsa->header->ls_type);
akmhoqueffacaa82012-09-13 17:48:30 -0500240
akmhoque0ab71642013-02-21 10:10:33 -0600241 char lsid[10];
242 memset(lsid,0,10);
243 sprintf(lsid,"%ld",name_lsa->header->ls_id);
akmhoque3171d652012-11-13 11:44:33 -0600244
akmhoque0ab71642013-02-21 10:10:33 -0600245 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
246 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
247 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
akmhoque53f64222012-09-05 13:57:51 -0500248
akmhoque0ab71642013-02-21 10:10:33 -0600249 if ( nlsr->debugging )
250 printf("Key:%s Length:%d\n",key,(int)strlen(key));
251 if ( nlsr->detailed_logging )
252 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Key:%s Length:%d\n",key,(int)strlen(key));
253
254 struct nlsa *new_name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa ));
akmhoque53f64222012-09-05 13:57:51 -0500255
akmhoque0ab71642013-02-21 10:10:33 -0600256 struct hashtb_enumerator ee;
257 struct hashtb_enumerator *e = &ee;
258 int res;
akmhoque53f64222012-09-05 13:57:51 -0500259
akmhoque0ab71642013-02-21 10:10:33 -0600260 hashtb_start(nlsr->lsdb->name_lsdb, e);
261 res = hashtb_seek(e, key, strlen(key), 0);
akmhoque53f64222012-09-05 13:57:51 -0500262
akmhoque0ab71642013-02-21 10:10:33 -0600263 if(res == HT_NEW_ENTRY )
akmhoque3560cb62012-09-09 10:52:30 -0500264 {
akmhoqueffacaa82012-09-13 17:48:30 -0500265
akmhoque7b791452012-10-30 11:24:56 -0500266 if ( nlsr->debugging )
267 printf("New Name LSA... Adding to LSDB\n");
268 if ( nlsr->detailed_logging )
269 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Name LSA... Adding to LSDB\n");
270
271
akmhoqueffacaa82012-09-13 17:48:30 -0500272 new_name_lsa = e->data;
273
akmhoque0ab71642013-02-21 10:10:33 -0600274 new_name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header ));
akmhoqueffacaa82012-09-13 17:48:30 -0500275 new_name_lsa->header->ls_type=name_lsa->header->ls_type;
276
277 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
278 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
akmhoque0ab71642013-02-21 10:10:33 -0600279 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time));
akmhoqueffacaa82012-09-13 17:48:30 -0500280
281 new_name_lsa->header->ls_id=name_lsa->header->ls_id;
akmhoque0ab71642013-02-21 10:10:33 -0600282 new_name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoqueffacaa82012-09-13 17:48:30 -0500283 new_name_lsa->header->orig_router->name=(char *)malloc(name_lsa->header->orig_router->length);
284 memcpy(new_name_lsa->header->orig_router->name,name_lsa->header->orig_router->name,name_lsa->header->orig_router->length);
285 new_name_lsa->header->orig_router->length=name_lsa->header->orig_router->length;
286 new_name_lsa->header->isValid=name_lsa->header->isValid;
287
288
akmhoque0ab71642013-02-21 10:10:33 -0600289 new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoqueffacaa82012-09-13 17:48:30 -0500290 new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
291 memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
292 new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
293
akmhoque7b791452012-10-30 11:24:56 -0500294 if ( nlsr->debugging )
295 {
296 printf("New Name LSA Added....\n");
297 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
298 }
299 if ( nlsr->detailed_logging )
300 {
301 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Name LSA Added....\n");
302 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
303 }
akmhoqueffacaa82012-09-13 17:48:30 -0500304 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500305 if ( nlsr->debugging )
306 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
307 if ( nlsr->detailed_logging )
308 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500309
akmhoque9e9fc722012-09-26 14:03:25 -0500310
akmhoquede61ba92012-09-20 22:19:12 -0500311 int num_next_hop=get_number_of_next_hop(new_name_lsa->header->orig_router->name);
akmhoquede61ba92012-09-20 22:19:12 -0500312 if ( num_next_hop < 0 )
akmhoque3560cb62012-09-09 10:52:30 -0500313 {
akmhoquede61ba92012-09-20 22:19:12 -0500314 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 {
akmhoque7b791452012-10-30 11:24:56 -0500317 if ( nlsr->debugging )
318 printf("Added in npt \n");
319 if ( nlsr->detailed_logging )
320 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
akmhoqueffacaa82012-09-13 17:48:30 -0500321 }
akmhoque3560cb62012-09-09 10:52:30 -0500322 }
akmhoqueffacaa82012-09-13 17:48:30 -0500323 else
akmhoque3560cb62012-09-09 10:52:30 -0500324 {
akmhoquede61ba92012-09-20 22:19:12 -0500325 int *faces=malloc(num_next_hop*sizeof(int));
326 int *route_costs=malloc(num_next_hop*sizeof(int));
327 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name,faces,route_costs);
akmhoque7b791452012-10-30 11:24:56 -0500328 if ( nlsr->debugging )
329 {
330 printf("Printing from install_name_lsa \n");
331 int j;
332 for(j=0;j<num_next_hop;j++)
333 printf("Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
334 }
335 if ( nlsr->detailed_logging )
336 {
337 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Printing from install_name_lsa \n");
338 int j;
339 for(j=0;j<num_next_hop;j++)
340 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
341 }
akmhoquede61ba92012-09-20 22:19:12 -0500342 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 -0500343 if ( check == HT_NEW_ENTRY )
344 {
akmhoque7b791452012-10-30 11:24:56 -0500345 if ( nlsr->debugging )
346 printf("Added in npt \n");
347 if ( nlsr->detailed_logging )
348 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
akmhoqueffacaa82012-09-13 17:48:30 -0500349 }
akmhoquede61ba92012-09-20 22:19:12 -0500350 free(faces);
351 free(route_costs);
akmhoqueffacaa82012-09-13 17:48:30 -0500352
akmhoque3560cb62012-09-09 10:52:30 -0500353 }
akmhoquede61ba92012-09-20 22:19:12 -0500354
akmhoque9e9fc722012-09-26 14:03:25 -0500355 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
356 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
357 write_log_for_name_lsa(new_name_lsa);
358 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500359
akmhoquede61ba92012-09-20 22:19:12 -0500360 free(time_stamp);
akmhoqueffacaa82012-09-13 17:48:30 -0500361
362 }
363 else if(res == HT_OLD_ENTRY)
364 {
365 new_name_lsa=e->data;
akmhoque14b3f342012-09-14 10:39:02 -0500366 if(strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time)<0)
akmhoqueffacaa82012-09-13 17:48:30 -0500367 {
akmhoque7b791452012-10-30 11:24:56 -0500368 if ( nlsr->debugging )
369 printf("Older Adj LSA. Discarded... \n");
370 if ( nlsr->detailed_logging )
371 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Older Adj LSA. Discarded...\n");
akmhoque14b3f342012-09-14 10:39:02 -0500372 }
373 else if( strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time) == 0 )
374 {
akmhoque7b791452012-10-30 11:24:56 -0500375 if ( nlsr->debugging )
376 printf("Duplicate Adj LSA. Discarded... \n");
377 if ( nlsr->detailed_logging )
378 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Duplicate Adj LSA. Discarded...\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500379 }
380 else
381 {
382 if ( name_lsa->header->isValid == 0 )
383 {
384 // have to call to delete npt table entry
akmhoque3cced642012-09-24 16:20:20 -0500385 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 -0500386
387 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name)!= 0)
388 {
akmhoque9e9fc722012-09-26 14:03:25 -0500389 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
390 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
391 write_log_for_name_lsa(new_name_lsa);
392 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
393
akmhoqueffacaa82012-09-13 17:48:30 -0500394 hashtb_delete(e);
akmhoque7b791452012-10-30 11:24:56 -0500395 if ( nlsr->debugging )
396 printf("isValid bit not set for Router %s so LSA Deleted from LSDB\n",name_lsa->header->orig_router->name);
397 if ( nlsr->detailed_logging )
398 writeLogg(__FILE__,__FUNCTION__,__LINE__,"isValid bit not set for Router %s so LSA Deleted from LSDB\n",name_lsa->header->orig_router->name);
akmhoqueffacaa82012-09-13 17:48:30 -0500399 }
400 else
401 {
402 new_name_lsa->header->isValid=name_lsa->header->isValid;
403 free(new_name_lsa->header->orig_time);
404 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
405 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
406 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
407 }
akmhoque7b791452012-10-30 11:24:56 -0500408 if ( nlsr->debugging )
409 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
410 if ( nlsr->detailed_logging )
411 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500412 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500413 if ( nlsr->debugging )
414 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
415 if ( nlsr->detailed_logging )
416 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500417 }
418 else
419 {
420 int is_npt_update=0;
421 if ( strcmp(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name) != 0 )
422 {
423 is_npt_update=1;
akmhoque3cced642012-09-24 16:20:20 -0500424 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 -0500425 }
426
427 // copying LSA content with header
akmhoque9e9fc722012-09-26 14:03:25 -0500428 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
429 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
430 write_log_for_name_lsa(new_name_lsa);
431 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
432
akmhoqueffacaa82012-09-13 17:48:30 -0500433
434 free(new_name_lsa->header->orig_time);
435 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
436 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
akmhoque0ab71642013-02-21 10:10:33 -0600437 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time));
akmhoqueffacaa82012-09-13 17:48:30 -0500438
439 new_name_lsa->header->isValid=name_lsa->header->isValid;
440
441 free(new_name_lsa->name_prefix->name);
442 free(new_name_lsa->name_prefix);
443 new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
444 new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
445 memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
446 new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
447
akmhoque9e9fc722012-09-26 14:03:25 -0500448
449 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
450 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
451 write_log_for_name_lsa(new_name_lsa);
452 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
453
akmhoque7b791452012-10-30 11:24:56 -0500454 if ( nlsr->debugging )
455 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
456 if ( nlsr->detailed_logging )
457 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
458
akmhoqueffacaa82012-09-13 17:48:30 -0500459 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500460
461 if ( nlsr->debugging )
462 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
463 if ( nlsr->detailed_logging )
464 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
465
akmhoqueffacaa82012-09-13 17:48:30 -0500466
467 if( is_npt_update == 1 )
468 {
akmhoquede61ba92012-09-20 22:19:12 -0500469 //struct hashtb *face_list;
470 int num_next_hop=get_number_of_next_hop(new_name_lsa->header->orig_router->name);
471 if ( num_next_hop < 0 )
akmhoqueffacaa82012-09-13 17:48:30 -0500472 {
akmhoquede61ba92012-09-20 22:19:12 -0500473
474 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 -0500475 if ( check == HT_NEW_ENTRY )
476 {
akmhoque7b791452012-10-30 11:24:56 -0500477 if ( nlsr->debugging )
478 printf("Added in npt \n");
479 if ( nlsr->detailed_logging )
480 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
akmhoqueffacaa82012-09-13 17:48:30 -0500481 }
482 }
483 else
484 {
akmhoquede61ba92012-09-20 22:19:12 -0500485 int *faces=malloc(num_next_hop*sizeof(int));
486 int *route_costs=malloc(num_next_hop*sizeof(int));
487 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name,faces,route_costs);
akmhoque7b791452012-10-30 11:24:56 -0500488
489 if ( nlsr->debugging )
490 {
491 printf("Printing from install_name_lsa \n");
492 int j;
493 for(j=0;j<num_next_hop;j++)
akmhoquede61ba92012-09-20 22:19:12 -0500494 printf("Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
akmhoque7b791452012-10-30 11:24:56 -0500495 }
496 if ( nlsr->detailed_logging )
497 {
498 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Printing from install_name_lsa \n");
499 int j;
500 for(j=0;j<num_next_hop;j++)
501 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
502 }
503
akmhoquede61ba92012-09-20 22:19:12 -0500504
505 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 -0500506 if ( check == HT_NEW_ENTRY )
507 {
akmhoque7b791452012-10-30 11:24:56 -0500508 if ( nlsr->debugging )
509 printf("Added in npt \n");
510 if ( nlsr->detailed_logging )
511 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
akmhoqueffacaa82012-09-13 17:48:30 -0500512 }
akmhoquede61ba92012-09-20 22:19:12 -0500513 free(faces);
514 free(route_costs);
515
akmhoqueffacaa82012-09-13 17:48:30 -0500516 }
akmhoquede61ba92012-09-20 22:19:12 -0500517
akmhoqueffacaa82012-09-13 17:48:30 -0500518 }
519 }
520 }
521
akmhoque3560cb62012-09-09 10:52:30 -0500522 }
akmhoque53f64222012-09-05 13:57:51 -0500523
akmhoqueffacaa82012-09-13 17:48:30 -0500524 hashtb_end(e);
akmhoque53f64222012-09-05 13:57:51 -0500525
akmhoque569a93d2013-02-21 10:19:54 -0600526 //free(key);
akmhoque53f64222012-09-05 13:57:51 -0500527}
528
akmhoque9e9fc722012-09-26 14:03:25 -0500529void
530write_log_for_name_lsa(struct nlsa *name_lsa)
531{
532
533 writeLogg(__FILE__,__FUNCTION__,__LINE__,"-----------Name LSA Content---------------\n");
534 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s\n",name_lsa->header->orig_router->name);
535 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router Length: %d\n",name_lsa->header->orig_router->length);
akmhoque34b99f92012-09-27 12:24:27 -0500536 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Type: %d\n",name_lsa->header->ls_type);
537 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Id: %ld\n",name_lsa->header->ls_id);
538 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Time: %s\n",name_lsa->header->orig_time);
539 writeLogg(__FILE__,__FUNCTION__,__LINE__," Is Valid: %d\n",name_lsa->header->isValid);
akmhoque9e9fc722012-09-26 14:03:25 -0500540 writeLogg(__FILE__,__FUNCTION__,__LINE__," LSA Data \n");
541 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix: %s\n",name_lsa->name_prefix->name);
542 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix Length: %d\n",name_lsa->name_prefix->length);
543 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
544}
akmhoque53f64222012-09-05 13:57:51 -0500545
akmhoque03004e62012-09-06 01:12:28 -0500546void
547print_name_lsa(struct nlsa *name_lsa)
akmhoque53f64222012-09-05 13:57:51 -0500548{
akmhoque53f64222012-09-05 13:57:51 -0500549
akmhoque7b791452012-10-30 11:24:56 -0500550 if ( nlsr->debugging )
551 {
552 printf("-----------Name LSA Content---------------\n");
553 printf(" Origination Router : %s\n",name_lsa->header->orig_router->name);
554 printf(" Origination Router Length: %d\n",name_lsa->header->orig_router->length);
555 printf(" LS Type : %d\n",name_lsa->header->ls_type);
556 printf(" LS Id : %ld\n",name_lsa->header->ls_id);
557 printf(" Origination Time : %s\n",name_lsa->header->orig_time);
558 printf(" Is Valid : %d\n",name_lsa->header->isValid);
559 printf(" LSA Data \n");
560 printf(" Name Prefix: : %s\n",name_lsa->name_prefix->name);
561 printf(" Name Prefix Length : %d\n",name_lsa->name_prefix->length);
562
563 printf("\n");
564 }
akmhoquef71d9082012-08-22 12:51:53 -0400565}
566
567void
568print_name_lsdb(void)
569{
akmhoque7b791452012-10-30 11:24:56 -0500570 if ( nlsr->debugging )
571 printf("print_name_lsdb called \n");
akmhoque53f64222012-09-05 13:57:51 -0500572 int i, name_lsdb_element;
akmhoquef71d9082012-08-22 12:51:53 -0400573 struct nlsa *name_lsa;
574
575 struct hashtb_enumerator ee;
576 struct hashtb_enumerator *e = &ee;
577
578 hashtb_start(nlsr->lsdb->name_lsdb, e);
akmhoque53f64222012-09-05 13:57:51 -0500579 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
akmhoquef71d9082012-08-22 12:51:53 -0400580
akmhoque53f64222012-09-05 13:57:51 -0500581 for(i=0;i<name_lsdb_element;i++)
akmhoquef71d9082012-08-22 12:51:53 -0400582 {
akmhoque7b791452012-10-30 11:24:56 -0500583 if ( nlsr->debugging )
584 printf("-----------Name LSA (%d)---------------\n",i+1);
akmhoquef71d9082012-08-22 12:51:53 -0400585 name_lsa=e->data;
akmhoque53f64222012-09-05 13:57:51 -0500586 print_name_lsa(name_lsa);
akmhoquef71d9082012-08-22 12:51:53 -0400587 hashtb_next(e);
588 }
589
590 hashtb_end(e);
591
akmhoque3171d652012-11-13 11:44:33 -0600592 if ( nlsr->debugging )
593 printf("\n");
594 if ( nlsr->detailed_logging )
595 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoque53f64222012-09-05 13:57:51 -0500596}
597
akmhoque03004e62012-09-06 01:12:28 -0500598
599void
600build_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 -0500601{
akmhoque7b791452012-10-30 11:24:56 -0500602 if ( nlsr->debugging )
603 printf("build_and_install_others_name_lsa called \n");
604 if ( nlsr->detailed_logging )
605 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_others_name_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500606
akmhoque03004e62012-09-06 01:12:28 -0500607 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
608 build_others_name_lsa(name_lsa,orig_router,ls_type,ls_id,orig_time, isValid,np);
609 print_name_lsa(name_lsa);
610 install_name_lsa(name_lsa);
611 print_name_lsdb();
akmhoquede61ba92012-09-20 22:19:12 -0500612 print_npt();
akmhoque53f64222012-09-05 13:57:51 -0500613
akmhoque03004e62012-09-06 01:12:28 -0500614 free(name_lsa->header->orig_router->name);
615 free(name_lsa->header->orig_router);
616 free(name_lsa->header);
617 free(name_lsa->name_prefix->name);
618 free(name_lsa->name_prefix);
619 free(name_lsa);
akmhoque53f64222012-09-05 13:57:51 -0500620
621}
622
623void
akmhoque03004e62012-09-06 01:12:28 -0500624build_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 -0500625{
akmhoque7b791452012-10-30 11:24:56 -0500626 if ( nlsr->debugging )
627 printf("build_others_name_lsa called\n");
628 if ( nlsr->detailed_logging )
629 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_others_name_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500630
akmhoque03004e62012-09-06 01:12:28 -0500631 name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header ));
632 name_lsa->header->ls_type=LS_TYPE_NAME;
akmhoque53f64222012-09-05 13:57:51 -0500633
akmhoque03004e62012-09-06 01:12:28 -0500634 name_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
635 memset(name_lsa->header->orig_time,0,strlen(orig_time)+1);
akmhoque0ab71642013-02-21 10:10:33 -0600636 memcpy(name_lsa->header->orig_time,orig_time,strlen(orig_time));
akmhoque53f64222012-09-05 13:57:51 -0500637
akmhoque03004e62012-09-06 01:12:28 -0500638 name_lsa->header->ls_id=ls_id;
639 name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
640 name_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
641 memset(name_lsa->header->orig_router->name,0,strlen(orig_router)+1);
akmhoque0ab71642013-02-21 10:10:33 -0600642 memcpy(name_lsa->header->orig_router->name,orig_router,strlen(orig_router));
akmhoque03004e62012-09-06 01:12:28 -0500643 name_lsa->header->orig_router->length=strlen(orig_router)+1;
644 name_lsa->header->isValid=isValid;
akmhoque53f64222012-09-05 13:57:51 -0500645
akmhoque03004e62012-09-06 01:12:28 -0500646 name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
647 name_lsa->name_prefix->name=(char *)malloc(strlen(np)+1);
648 memset(name_lsa->name_prefix->name,0,strlen(np)+1);
akmhoque0ab71642013-02-21 10:10:33 -0600649 memcpy(name_lsa->name_prefix->name,np,strlen(np));
akmhoque03004e62012-09-06 01:12:28 -0500650 name_lsa->name_prefix->length=strlen(np)+1;
akmhoquef71d9082012-08-22 12:51:53 -0400651}
akmhoqued79438d2012-08-27 13:31:42 -0500652
653
akmhoqueb77b95f2013-02-08 12:28:47 -0600654void
655make_cor_lsa_key(char *key,struct clsa *cor_lsa)
656{
657 memcpy(key+strlen(key),cor_lsa->header->orig_router->name,cor_lsa->header->orig_router->length);
658 memcpy(key+strlen(key),"/",1);
659 char ls_type[2];
660 sprintf(ls_type,"%d",cor_lsa->header->ls_type);
661 memcpy(key+strlen(key),ls_type,strlen(ls_type));
662 key[strlen(key)]='\0';
663}
akmhoqueffacaa82012-09-13 17:48:30 -0500664
665
akmhoque53f64222012-09-05 13:57:51 -0500666void
667make_adj_lsa_key(char *key,struct alsa *adj_lsa)
akmhoqued79438d2012-08-27 13:31:42 -0500668{
akmhoque53f64222012-09-05 13:57:51 -0500669 memcpy(key+strlen(key),adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
670 memcpy(key+strlen(key),"/",1);
671 char ls_type[2];
672 sprintf(ls_type,"%d",adj_lsa->header->ls_type);
673 memcpy(key+strlen(key),ls_type,strlen(ls_type));
674 key[strlen(key)]='\0';
675}
676
akmhoque03004e62012-09-06 01:12:28 -0500677int
678build_and_install_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
679{
akmhoqueffacaa82012-09-13 17:48:30 -0500680 if(flags == CCN_SCHEDULE_CANCEL)
681 {
682 return -1;
683 }
684
685 nlsr_lock();
686
akmhoque7b791452012-10-30 11:24:56 -0500687 if ( nlsr->debugging )
688 printf("build_and_install_adj_lsa called \n");
689 if ( nlsr->detailed_logging )
690 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_adj_lsa called \n");
691
692 if ( nlsr->debugging )
693 printf("adj_build_flag = %d \n",nlsr->adj_build_flag);
694 if ( nlsr->detailed_logging )
695 writeLogg(__FILE__,__FUNCTION__,__LINE__,"adj_build_flag = %d \n",nlsr->adj_build_flag);
akmhoque03004e62012-09-06 01:12:28 -0500696
697 if(nlsr->adj_build_flag > 0)
698 {
akmhoque7b791452012-10-30 11:24:56 -0500699 if ( nlsr->debugging )
700 printf("is_adj_lsa_build = %d \n",is_adj_lsa_build());
701 if ( nlsr->detailed_logging )
702 writeLogg(__FILE__,__FUNCTION__,__LINE__,"is_adj_lsa_build = %d \n",is_adj_lsa_build());
703
akmhoque03004e62012-09-06 01:12:28 -0500704 if ( is_adj_lsa_build()> 0)
705 {
706 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
707 build_adj_lsa(adj_lsa);
708 install_adj_lsa(adj_lsa);
709
akmhoqueb77b95f2013-02-08 12:28:47 -0600710 char lst[2];
711 memset(lst,0,2);
712 sprintf(lst,"%d",LS_TYPE_ADJ);
713
714 char *repo_key=(char *)malloc(strlen(nlsr->slice_prefix)+strlen(adj_lsa->header->orig_time)+strlen(adj_lsa->header->orig_router->name) + strlen(lst) + 5+15);
715 memset(repo_key, 0, strlen(nlsr->slice_prefix)+strlen(adj_lsa->header->orig_time)+strlen(adj_lsa->header->orig_router->name) + strlen(lst) + 5+15);
716 make_adj_lsa_prefix_for_repo(repo_key, adj_lsa->header->orig_router->name,LS_TYPE_ADJ,adj_lsa->header->orig_time,nlsr->slice_prefix);
717
718 if ( nlsr->debugging )
719 printf("Adj LSA Repo Key: %s \n",repo_key);
720
721 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
722 memset(key,0,adj_lsa->header->orig_router->length+2+2);
723 make_adj_lsa_key(key,adj_lsa);
724 if ( nlsr->debugging )
725 printf("Adj LSA: %s \n",key);
726
727 struct name_prefix *lsaid=(struct name_prefix *)malloc(sizeof(struct name_prefix));
728 lsaid->name=(char *)malloc(strlen(key)+1);
729 memset(lsaid->name, 0, strlen(key)+1);
730 memcpy(lsaid->name,key,strlen(key));
731 lsaid->length=strlen(key)+1;
732
733
734 write_adj_lsa_to_repo(repo_key, lsaid);
735
akmhoque569a93d2013-02-21 10:19:54 -0600736 //free(key);
akmhoqueb77b95f2013-02-08 12:28:47 -0600737 free(repo_key);
738 free(lsaid->name);
739 free(lsaid);
740
741
742
akmhoque03004e62012-09-06 01:12:28 -0500743 free(adj_lsa->header->orig_router->name);
744 free(adj_lsa->header->orig_router);
745 free(adj_lsa->header->orig_time);
746 free(adj_lsa->header);
747 free(adj_lsa->body);
748 free(adj_lsa);
749 nlsr->adj_build_flag=0;
750 print_adj_lsdb();
751 }
752 else
753 {
akmhoque7b791452012-10-30 11:24:56 -0500754 if ( nlsr->debugging )
755 printf("Can not build adj LSA now\n");
756 if ( nlsr->detailed_logging )
757 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Can not build adj LSA now\n");
akmhoque03004e62012-09-06 01:12:28 -0500758 }
759 }
760 nlsr->is_build_adj_lsa_sheduled=0;
akmhoqueffacaa82012-09-13 17:48:30 -0500761
762 nlsr_unlock();
763
akmhoque03004e62012-09-06 01:12:28 -0500764 return 0;
765}
766
767
768void
769build_adj_lsa(struct alsa * adj_lsa)
770{
akmhoque7b791452012-10-30 11:24:56 -0500771 if ( nlsr->debugging )
772 printf("build_adj_lsa called \n");
773 if ( nlsr->detailed_logging )
774 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_adj_lsa called \n");
akmhoque03004e62012-09-06 01:12:28 -0500775
776 int no_link=no_active_nbr();
akmhoque7b791452012-10-30 11:24:56 -0500777
akmhoque03004e62012-09-06 01:12:28 -0500778 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
779 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
780 adj_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1);
781 memset(adj_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
782 memcpy(adj_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
783 adj_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
784
785 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
786
787 char *time_stamp=(char *)malloc(20);
788 memset(time_stamp,0,20);
789 get_current_timestamp_micro(time_stamp);
790
791 adj_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1);
792 memset(adj_lsa->header->orig_time,0,strlen(time_stamp)+1);
793 memcpy(adj_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
794 free(time_stamp);
795
akmhoque03004e62012-09-06 01:12:28 -0500796 adj_lsa->no_link=no_link;
akmhoque03004e62012-09-06 01:12:28 -0500797
798
799 struct ccn_charbuf *c=ccn_charbuf_create();
800 get_active_nbr_adj_data(c);
801 char *data=ccn_charbuf_as_string(c);
802
803 adj_lsa->body=(char *)malloc(strlen(data)+1);
804 memset(adj_lsa->body,0,strlen(data)+1);
805 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
806 ccn_charbuf_destroy(&c);
807
akmhoque03004e62012-09-06 01:12:28 -0500808 nlsr->adj_build_count++;
809
810
811}
812
813
akmhoque53f64222012-09-05 13:57:51 -0500814void
815install_adj_lsa(struct alsa * adj_lsa)
816{
akmhoqued79438d2012-08-27 13:31:42 -0500817
akmhoque7b791452012-10-30 11:24:56 -0500818 if ( nlsr->debugging )
819 printf("install_adj_lsa called \n");
820 if ( nlsr->detailed_logging )
821 writeLogg(__FILE__,__FUNCTION__,__LINE__,"install_adj_lsa called \n");
822
akmhoque03004e62012-09-06 01:12:28 -0500823
akmhoqueffacaa82012-09-13 17:48:30 -0500824 char *time_stamp=(char *)malloc(20);
825 memset(time_stamp,0,20);
826 get_current_timestamp_micro(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -0500827
akmhoqueffacaa82012-09-13 17:48:30 -0500828
829 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
830 memset(key,0,adj_lsa->header->orig_router->length+2);
831 make_adj_lsa_key(key,adj_lsa);
akmhoqueffacaa82012-09-13 17:48:30 -0500832
833 struct alsa *new_adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
834
835 struct hashtb_enumerator ee;
836 struct hashtb_enumerator *e = &ee;
837 int res;
838
839 hashtb_start(nlsr->lsdb->adj_lsdb, e);
840 res = hashtb_seek(e, key, strlen(key), 0);
akmhoque53f64222012-09-05 13:57:51 -0500841
akmhoque03004e62012-09-06 01:12:28 -0500842
akmhoque53f64222012-09-05 13:57:51 -0500843
akmhoque62c0c192012-09-24 07:49:25 -0500844 if(res == HT_NEW_ENTRY)
akmhoque53f64222012-09-05 13:57:51 -0500845 {
akmhoque62c0c192012-09-24 07:49:25 -0500846 if ( adj_lsa->no_link > 0)
847 {
akmhoque3171d652012-11-13 11:44:33 -0600848 if ( nlsr->debugging )
849 printf("New ADJ LSA... Adding to LSDB\n");
850 if ( nlsr->detailed_logging )
851 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New ADJ LSA... Adding to LSDB\n");
akmhoque62c0c192012-09-24 07:49:25 -0500852 new_adj_lsa = e->data;
akmhoque53f64222012-09-05 13:57:51 -0500853
akmhoque62c0c192012-09-24 07:49:25 -0500854 new_adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
855 new_adj_lsa->header->ls_type=adj_lsa->header->ls_type;
856 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
857 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 -0500858
akmhoque62c0c192012-09-24 07:49:25 -0500859 new_adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
860 new_adj_lsa->header->orig_router->name=(char *)malloc(adj_lsa->header->orig_router->length);
861 memcpy(new_adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
862 new_adj_lsa->header->orig_router->length=adj_lsa->header->orig_router->length;
akmhoque53f64222012-09-05 13:57:51 -0500863
akmhoque62c0c192012-09-24 07:49:25 -0500864 new_adj_lsa->no_link=adj_lsa->no_link;
akmhoqueffacaa82012-09-13 17:48:30 -0500865
akmhoque62c0c192012-09-24 07:49:25 -0500866 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
867 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
868 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
869
870 add_next_hop_router(new_adj_lsa->header->orig_router->name);
871 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
akmhoque9e9fc722012-09-26 14:03:25 -0500872
873 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
874 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
875 write_log_for_adj_lsa(new_adj_lsa);
876 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoque62c0c192012-09-24 07:49:25 -0500877 }
878 else
879 {
880 hashtb_delete(e);
881 }
akmhoque53f64222012-09-05 13:57:51 -0500882
akmhoque7b791452012-10-30 11:24:56 -0500883 if ( nlsr->debugging )
884 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
885 if ( nlsr->detailed_logging )
886 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
887
akmhoque53f64222012-09-05 13:57:51 -0500888 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500889
890 if ( nlsr->debugging )
891 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
892 if ( nlsr->detailed_logging )
893 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
894
akmhoqueffacaa82012-09-13 17:48:30 -0500895 }
896 else if(res == HT_OLD_ENTRY)
897 {
898 new_adj_lsa = e->data;
899 if(strcmp(adj_lsa->header->orig_time,new_adj_lsa->header->orig_time)<=0)
900 {
akmhoque7b791452012-10-30 11:24:56 -0500901 if ( nlsr->debugging )
902 printf("Older/Duplicate Adj LSA. Discarded...\n");
903 if ( nlsr->detailed_logging )
904 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Older/Duplicate Adj LSA. Discarded...\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500905 }
906 else
907 {
akmhoqueffacaa82012-09-13 17:48:30 -0500908
akmhoque62c0c192012-09-24 07:49:25 -0500909 if ( adj_lsa->no_link > 0)
910 {
akmhoque9e9fc722012-09-26 14:03:25 -0500911 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
912 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
913 write_log_for_adj_lsa(new_adj_lsa);
914 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500915
akmhoque62c0c192012-09-24 07:49:25 -0500916 free(new_adj_lsa->header->orig_time);
917 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
918 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
919
920 new_adj_lsa->no_link=adj_lsa->no_link;
akmhoqueffacaa82012-09-13 17:48:30 -0500921
akmhoque62c0c192012-09-24 07:49:25 -0500922 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
923 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
924 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
akmhoqueffacaa82012-09-13 17:48:30 -0500925
akmhoque62c0c192012-09-24 07:49:25 -0500926 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
akmhoque9e9fc722012-09-26 14:03:25 -0500927
928 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
929 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
930 write_log_for_adj_lsa(new_adj_lsa);
931 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoque62c0c192012-09-24 07:49:25 -0500932 }
933 else
934 {
akmhoque9e9fc722012-09-26 14:03:25 -0500935 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
936 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
937 write_log_for_adj_lsa(new_adj_lsa);
938 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
939
akmhoque62c0c192012-09-24 07:49:25 -0500940 hashtb_delete(e);
941 }
akmhoque7b791452012-10-30 11:24:56 -0500942
943 if ( nlsr->debugging )
944 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
945 if ( nlsr->detailed_logging )
946 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
947
akmhoqueffacaa82012-09-13 17:48:30 -0500948 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500949
950 if ( nlsr->debugging )
951 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
952 if ( nlsr->detailed_logging )
953 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500954 }
955
956 }
957 hashtb_end(e);
958
959 if ( !nlsr->is_route_calculation_scheduled )
960 {
961 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
962 nlsr->is_route_calculation_scheduled=1;
akmhoque53f64222012-09-05 13:57:51 -0500963 }
964
akmhoque03004e62012-09-06 01:12:28 -0500965
akmhoque569a93d2013-02-21 10:19:54 -0600966 //free(key);
akmhoqueffacaa82012-09-13 17:48:30 -0500967
968 free(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -0500969}
970
971void
akmhoque9e9fc722012-09-26 14:03:25 -0500972write_log_for_adj_lsa_body(const char *body, int no_link)
973{
974 int i=0;
975 char *lsa_data=(char *)malloc(strlen(body)+1);
976 memset( lsa_data,0,strlen(body)+1);
977 memcpy(lsa_data,body,strlen(body)+1);
978 char *sep="|";
979 char *rem;
980 char *rtr_id;
981 char *length;
akmhoqueb28579d2013-02-12 11:15:52 -0600982 //char *face;
akmhoque9e9fc722012-09-26 14:03:25 -0500983 char *metric;
984
985 if(no_link >0 )
986 {
987 rtr_id=strtok_r(lsa_data,sep,&rem);
988 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -0600989 //face=strtok_r(NULL,sep,&rem);
akmhoque9e9fc722012-09-26 14:03:25 -0500990 metric=strtok_r(NULL,sep,&rem);
991
992 writeLogg(__FILE__,__FUNCTION__,__LINE__," Link %d \n",i+1);
akmhoque34b99f92012-09-27 12:24:27 -0500993 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router: %s \n",rtr_id);
994 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router Length: %s \n",length);
akmhoque866c2222013-02-12 10:49:33 -0600995 //writeLogg(__FILE__,__FUNCTION__,__LINE__," Connecting Face: %s \n",face);
akmhoque34b99f92012-09-27 12:24:27 -0500996 writeLogg(__FILE__,__FUNCTION__,__LINE__," Metric: %s \n",metric);
akmhoque9e9fc722012-09-26 14:03:25 -0500997
998
999 for(i=1;i<no_link;i++)
1000 {
1001 rtr_id=strtok_r(NULL,sep,&rem);
1002 length=strtok_r(NULL,sep,&rem);
akmhoqueb28579d2013-02-12 11:15:52 -06001003 //face=strtok_r(NULL,sep,&rem);
akmhoque9e9fc722012-09-26 14:03:25 -05001004 metric=strtok_r(NULL,sep,&rem);
1005 writeLogg(__FILE__,__FUNCTION__,__LINE__," Link %d \n",i+1);
akmhoque34b99f92012-09-27 12:24:27 -05001006 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router: %s \n",rtr_id);
1007 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router Length: %s \n",length);
akmhoque866c2222013-02-12 10:49:33 -06001008 //writeLogg(__FILE__,__FUNCTION__,__LINE__," Connecting Face: %s \n",face);
akmhoque34b99f92012-09-27 12:24:27 -05001009 writeLogg(__FILE__,__FUNCTION__,__LINE__," Metric: %s \n",metric);
akmhoque9e9fc722012-09-26 14:03:25 -05001010
1011 }
1012 }
1013
1014 free(lsa_data);
1015}
1016
1017
1018void
1019write_log_for_adj_lsa(struct alsa * adj_lsa)
1020{
1021 writeLogg(__FILE__,__FUNCTION__,__LINE__,"-----------Adj LSA Content---------------\n");
akmhoque34b99f92012-09-27 12:24:27 -05001022 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s\n",adj_lsa->header->orig_router->name);
1023 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
1024 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Type: %d\n",adj_lsa->header->ls_type);
1025 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Time: %s\n",adj_lsa->header->orig_time);
akmhoque9e9fc722012-09-26 14:03:25 -05001026 writeLogg(__FILE__,__FUNCTION__,__LINE__," Lsa Data:\n");
akmhoque34b99f92012-09-27 12:24:27 -05001027 writeLogg(__FILE__,__FUNCTION__,__LINE__," No of Link: %d\n",adj_lsa->no_link);
akmhoque9e9fc722012-09-26 14:03:25 -05001028
1029 write_log_for_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
1030
1031 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
1032
1033}
1034
1035void
akmhoque53f64222012-09-05 13:57:51 -05001036print_adj_lsa_body(const char *body, int no_link)
1037{
1038 int i=0;
1039 char *lsa_data=(char *)malloc(strlen(body)+1);
1040 memset( lsa_data,0,strlen(body)+1);
1041 memcpy(lsa_data,body,strlen(body)+1);
1042 char *sep="|";
1043 char *rem;
1044 char *rtr_id;
1045 char *length;
akmhoqueb28579d2013-02-12 11:15:52 -06001046 //char *face;
akmhoque53f64222012-09-05 13:57:51 -05001047 char *metric;
1048
akmhoque53f64222012-09-05 13:57:51 -05001049 if(no_link >0 )
1050 {
1051 rtr_id=strtok_r(lsa_data,sep,&rem);
1052 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -06001053 //face=strtok_r(NULL,sep,&rem);
akmhoque53f64222012-09-05 13:57:51 -05001054 metric=strtok_r(NULL,sep,&rem);
1055
akmhoqueb77b95f2013-02-08 12:28:47 -06001056 if ( nlsr->debugging ) {
1057 printf(" Link %d \n",i+1);
1058 printf(" Neighbor : %s \n",rtr_id);
1059 printf(" Neighbor Length : %s \n",length);
akmhoque866c2222013-02-12 10:49:33 -06001060 //printf(" Connecting Face : %s \n",face);
akmhoqueb77b95f2013-02-08 12:28:47 -06001061 printf(" Metric : %s \n",metric);
1062 }
akmhoque53f64222012-09-05 13:57:51 -05001063
1064 for(i=1;i<no_link;i++)
1065 {
1066 rtr_id=strtok_r(NULL,sep,&rem);
1067 length=strtok_r(NULL,sep,&rem);
akmhoqueb28579d2013-02-12 11:15:52 -06001068 //face=strtok_r(NULL,sep,&rem);
akmhoque53f64222012-09-05 13:57:51 -05001069 metric=strtok_r(NULL,sep,&rem);
1070 printf(" Link %d \n",i+1);
1071 printf(" Neighbor : %s \n",rtr_id);
1072 printf(" Neighbor Length : %s \n",length);
akmhoque866c2222013-02-12 10:49:33 -06001073 //printf(" Connecting Face : %s \n",face);
akmhoque53f64222012-09-05 13:57:51 -05001074 printf(" Metric : %s \n",metric);
1075
1076 }
1077 }
1078
1079 free(lsa_data);
1080}
1081
1082void
1083print_adj_lsa(struct alsa * adj_lsa)
1084{
akmhoque7b791452012-10-30 11:24:56 -05001085 if ( nlsr->debugging )
1086 {
1087 printf("-----------ADJ LSA Content---------------\n");
1088 printf(" Origination Router : %s\n",adj_lsa->header->orig_router->name);
1089 printf(" Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
1090 printf(" LS Type : %d\n",adj_lsa->header->ls_type);
1091 printf(" Origination Time : %s\n",adj_lsa->header->orig_time);
1092 printf(" Lsa Data:\n");
1093 printf(" No of Link : %d\n",adj_lsa->no_link);
akmhoque53f64222012-09-05 13:57:51 -05001094
akmhoque7b791452012-10-30 11:24:56 -05001095 print_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
1096 printf("\n");
1097 }
akmhoque53f64222012-09-05 13:57:51 -05001098
1099}
1100
1101void
1102print_adj_lsdb(void)
1103{
akmhoque7b791452012-10-30 11:24:56 -05001104 if ( nlsr->debugging )
1105 printf("print_name_lsdb called \n");
akmhoque53f64222012-09-05 13:57:51 -05001106 int i, adj_lsdb_element;
1107 struct alsa *adj_lsa;
1108
1109 struct hashtb_enumerator ee;
1110 struct hashtb_enumerator *e = &ee;
1111
1112 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1113 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1114
1115 for(i=0;i<adj_lsdb_element;i++)
1116 {
akmhoque7b791452012-10-30 11:24:56 -05001117 if ( nlsr->debugging )
1118 printf("-----------Adj LSA (%d)---------------\n",i+1);
akmhoque53f64222012-09-05 13:57:51 -05001119 adj_lsa=e->data;
1120 print_adj_lsa(adj_lsa);
1121 hashtb_next(e);
1122 }
1123
1124 hashtb_end(e);
1125
akmhoque3171d652012-11-13 11:44:33 -06001126 if ( nlsr->debugging )
1127 printf("\n");
1128 if ( nlsr->detailed_logging )
1129 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoque53f64222012-09-05 13:57:51 -05001130}
1131
1132void
akmhoque03004e62012-09-06 01:12:28 -05001133build_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 -05001134{
akmhoque7b791452012-10-30 11:24:56 -05001135 if ( nlsr->debugging )
1136 printf("build_and_install_others_adj_lsa called \n");
1137 if ( nlsr->detailed_logging )
1138 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_others_adj_lsa called \n");
akmhoque03004e62012-09-06 01:12:28 -05001139 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
1140 build_others_adj_lsa(adj_lsa,orig_router,ls_type,orig_time,no_link,data);
akmhoque03004e62012-09-06 01:12:28 -05001141 install_adj_lsa(adj_lsa);
1142
akmhoque53f64222012-09-05 13:57:51 -05001143
akmhoque03004e62012-09-06 01:12:28 -05001144 free(adj_lsa->header->orig_router->name);
1145 free(adj_lsa->header->orig_router);
1146 free(adj_lsa->header->orig_time);
1147 free(adj_lsa->header);
1148 free(adj_lsa->body);
1149 free(adj_lsa);
akmhoque53f64222012-09-05 13:57:51 -05001150
akmhoque03004e62012-09-06 01:12:28 -05001151 print_adj_lsdb();
akmhoque53f64222012-09-05 13:57:51 -05001152
akmhoque53f64222012-09-05 13:57:51 -05001153}
1154
akmhoque03004e62012-09-06 01:12:28 -05001155
akmhoque53f64222012-09-05 13:57:51 -05001156void
akmhoque03004e62012-09-06 01:12:28 -05001157build_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 -05001158{
akmhoque7b791452012-10-30 11:24:56 -05001159 if ( nlsr->debugging )
1160 printf("build_others_adj_lsa called \n");
1161 if ( nlsr->detailed_logging )
1162 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_others_adj_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -05001163
akmhoque03004e62012-09-06 01:12:28 -05001164 /*Filling Up Header Data */
1165 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
1166 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
1167 adj_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
1168 memset(adj_lsa->header->orig_router->name,0,strlen(orig_router)+1);
1169 memcpy(adj_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
akmhoque53f64222012-09-05 13:57:51 -05001170
akmhoque03004e62012-09-06 01:12:28 -05001171 adj_lsa->header->orig_router->length=strlen(orig_router)+1;
akmhoque53f64222012-09-05 13:57:51 -05001172
akmhoque53f64222012-09-05 13:57:51 -05001173
akmhoque03004e62012-09-06 01:12:28 -05001174 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
akmhoque53f64222012-09-05 13:57:51 -05001175
akmhoque03004e62012-09-06 01:12:28 -05001176 adj_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
1177 memset(adj_lsa->header->orig_time,0,strlen(orig_time)+1);
1178 memcpy(adj_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
akmhoque53f64222012-09-05 13:57:51 -05001179
akmhoque03004e62012-09-06 01:12:28 -05001180 adj_lsa->no_link=no_link;
akmhoque53f64222012-09-05 13:57:51 -05001181
akmhoque03004e62012-09-06 01:12:28 -05001182 adj_lsa->body=(char *)malloc(strlen(data)+1);
1183 memset(adj_lsa->body,0,strlen(data)+1);
1184 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
akmhoque53f64222012-09-05 13:57:51 -05001185
akmhoque53f64222012-09-05 13:57:51 -05001186}
1187
akmhoque03004e62012-09-06 01:12:28 -05001188
akmhoque53f64222012-09-05 13:57:51 -05001189long int
1190get_name_lsdb_num_element(void)
1191{
1192 long int num_element;
1193
1194
1195 struct hashtb_enumerator ee;
1196 struct hashtb_enumerator *e = &ee;
1197
1198 hashtb_start(nlsr->lsdb->name_lsdb, e);
1199 num_element=hashtb_n(nlsr->lsdb->name_lsdb);
1200 hashtb_end(e);
1201
1202 return num_element;
1203}
1204
1205long int
1206get_adj_lsdb_num_element(void)
1207{
1208 long int num_element;
1209
1210
1211 struct hashtb_enumerator ee;
1212 struct hashtb_enumerator *e = &ee;
1213
1214 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1215 num_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1216 hashtb_end(e);
1217
1218 return num_element;
akmhoqued79438d2012-08-27 13:31:42 -05001219}
akmhoque03004e62012-09-06 01:12:28 -05001220
akmhoque03004e62012-09-06 01:12:28 -05001221
1222int
1223check_is_new_name_lsa(char *orig_router,char *lst,char *lsid,char *orig_time)
1224{
1225 int ret=0;
1226 struct ccn_charbuf *key=ccn_charbuf_create();
1227 ccn_charbuf_append_string(key,orig_router);
1228 ccn_charbuf_append_string(key,"/");
1229 ccn_charbuf_append_string(key,lst);
1230 ccn_charbuf_append_string(key,"/");
1231 ccn_charbuf_append_string(key,lsid);
1232
1233 int res;
1234 struct nlsa *name_lsa;
1235
1236 struct hashtb_enumerator ee;
1237 struct hashtb_enumerator *e = &ee;
1238
1239 hashtb_start(nlsr->lsdb->name_lsdb, e);
1240 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1241
1242 if( res == HT_NEW_ENTRY )
1243 {
1244 hashtb_delete(e);
1245 ret=1;
1246
1247 }
1248 else if(res == HT_OLD_ENTRY)
1249 {
1250 name_lsa=e->data;
1251 if( strcmp ( orig_time , name_lsa->header->orig_time ) > 0 )
1252 {
1253 ret=1;
1254 }
1255 }
1256
1257 hashtb_end(e);
1258
1259 ccn_charbuf_destroy(&key);
1260
1261 return ret;
1262}
1263
1264int
1265check_is_new_adj_lsa(char *orig_router,char *lst,char *orig_time)
1266{
1267 int ret=0;
1268 struct ccn_charbuf *key=ccn_charbuf_create();
1269 ccn_charbuf_append_string(key,orig_router);
1270 ccn_charbuf_append_string(key,"/");
1271 ccn_charbuf_append_string(key,lst);
1272
1273 int res;
1274 struct alsa *adj_lsa;
1275
1276 struct hashtb_enumerator ee;
1277 struct hashtb_enumerator *e = &ee;
1278
1279 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1280 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1281
1282 if( res == HT_NEW_ENTRY )
1283 {
1284 hashtb_delete(e);
1285 ret=1;
1286
1287 }
1288 else if(res == HT_OLD_ENTRY)
1289 {
1290 adj_lsa=e->data;
1291 if( strcmp ( orig_time , adj_lsa->header->orig_time ) > 0 )
1292 {
1293 ret=1;
1294 }
1295 }
1296
1297 hashtb_end(e);
1298
1299 ccn_charbuf_destroy(&key);
1300
1301 return ret;
1302}
1303
akmhoqueb77b95f2013-02-08 12:28:47 -06001304int
1305check_is_new_cor_lsa(char *orig_router,char *lst,char *orig_time)
1306{
1307 int ret=0;
1308 struct ccn_charbuf *key=ccn_charbuf_create();
1309 ccn_charbuf_append_string(key,orig_router);
1310 ccn_charbuf_append_string(key,"/");
1311 ccn_charbuf_append_string(key,lst);
1312
1313 int res;
1314 struct clsa *cor_lsa;
1315
1316 struct hashtb_enumerator ee;
1317 struct hashtb_enumerator *e = &ee;
1318
1319 hashtb_start(nlsr->lsdb->cor_lsdb, e);
1320 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1321
1322 if( res == HT_NEW_ENTRY )
1323 {
1324 hashtb_delete(e);
1325 ret=1;
1326
1327 }
1328 else if(res == HT_OLD_ENTRY)
1329 {
1330 cor_lsa=e->data;
1331 if( strcmp ( orig_time , cor_lsa->header->orig_time ) > 0 )
1332 {
1333 ret=1;
1334 }
1335 }
1336
1337 hashtb_end(e);
1338
1339 ccn_charbuf_destroy(&key);
1340
1341 return ret;
1342}
1343
akmhoque03004e62012-09-06 01:12:28 -05001344void
1345get_name_lsa_data(struct ccn_charbuf *lsa_data, struct name_prefix *lsaId)
1346{
akmhoque7b791452012-10-30 11:24:56 -05001347 if ( nlsr->debugging )
1348 printf("get_name_lsa_data called \n");
1349 if ( nlsr->detailed_logging )
1350 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_name_lsa_data called \n");
akmhoque03004e62012-09-06 01:12:28 -05001351
1352 struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa ));
1353
1354 struct hashtb_enumerator ee;
1355 struct hashtb_enumerator *e = &ee;
1356 int res;
1357
1358 hashtb_start(nlsr->lsdb->name_lsdb, e);
1359 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1360
1361 if( res == HT_OLD_ENTRY )
1362 {
1363 name_lsa=e->data;
akmhoqueb77b95f2013-02-08 12:28:47 -06001364
akmhoque7b791452012-10-30 11:24:56 -05001365 if ( nlsr->debugging )
1366 printf("NAME LSA found \n");
1367 if ( nlsr->detailed_logging )
1368 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Name LSA found \n");
akmhoque03004e62012-09-06 01:12:28 -05001369
1370 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_router->name);
1371 ccn_charbuf_append_string(lsa_data,"|");
1372
1373 char *temp_length=(char *)malloc(20);
1374 memset(temp_length,0,20);
1375 sprintf(temp_length,"%d",name_lsa->header->orig_router->length);
1376 ccn_charbuf_append_string(lsa_data,temp_length);
1377 free(temp_length);
1378 ccn_charbuf_append_string(lsa_data,"|");
1379
1380 char *temp_ltype=(char *)malloc(20);
1381 memset(temp_ltype,0,20);
1382 sprintf(temp_ltype,"%d",name_lsa->header->ls_type);
1383 ccn_charbuf_append_string(lsa_data,temp_ltype);
1384 free(temp_ltype);
1385 ccn_charbuf_append_string(lsa_data,"|");
1386
1387 char *temp_lsid=(char *)malloc(20);
1388 memset(temp_lsid,0,20);
1389 sprintf(temp_lsid,"%ld",name_lsa->header->ls_id);
1390 ccn_charbuf_append_string(lsa_data,temp_lsid);
1391 free(temp_lsid);
1392 ccn_charbuf_append_string(lsa_data,"|");
1393
1394 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_time);
1395 ccn_charbuf_append_string(lsa_data,"|");
1396
1397 char *temp_valid=(char *)malloc(20);
1398 memset(temp_valid,0,20);
1399 sprintf(temp_valid,"%d",name_lsa->header->isValid);
1400 ccn_charbuf_append_string(lsa_data,temp_valid);
1401 free(temp_valid);
1402 ccn_charbuf_append_string(lsa_data,"|");
1403
1404 ccn_charbuf_append_string(lsa_data,name_lsa->name_prefix->name);
1405 ccn_charbuf_append_string(lsa_data,"|");
1406
1407 char *temp_npl=(char *)malloc(20);
1408 memset(temp_npl,0,20);
1409 sprintf(temp_npl,"%d",name_lsa->name_prefix->length);
1410 ccn_charbuf_append_string(lsa_data,temp_npl);
1411 free(temp_npl);
1412 ccn_charbuf_append_string(lsa_data,"|");
1413
1414 }
1415 else if(res == HT_NEW_ENTRY)
1416 {
1417 hashtb_delete(e);
1418 }
1419
1420 hashtb_end(e);
1421}
1422
1423void
1424get_adj_lsa_data(struct ccn_charbuf *lsa_data,struct name_prefix *lsaId)
1425{
akmhoque7b791452012-10-30 11:24:56 -05001426 if ( nlsr->debugging )
1427 printf("get_adj_lsa_data called \n");
1428 if ( nlsr->detailed_logging )
1429 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_adj_lsa_data called \n");
akmhoque03004e62012-09-06 01:12:28 -05001430
1431 struct alsa *adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
1432
1433 struct hashtb_enumerator ee;
1434 struct hashtb_enumerator *e = &ee;
1435 int res;
1436
1437 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1438 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1439
1440 if( res == HT_OLD_ENTRY )
1441 {
1442 adj_lsa=e->data;
akmhoque7b791452012-10-30 11:24:56 -05001443
1444 if ( nlsr->debugging )
1445 printf("Adj LSA found \n");
1446 if ( nlsr->detailed_logging )
1447 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adj LSA found \n");
akmhoque03004e62012-09-06 01:12:28 -05001448
1449 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_router->name);
1450 ccn_charbuf_append_string(lsa_data,"|");
1451
1452 char *temp_length=(char *)malloc(20);
1453 memset(temp_length,0,20);
1454 sprintf(temp_length,"%d",adj_lsa->header->orig_router->length);
1455 ccn_charbuf_append_string(lsa_data,temp_length);
akmhoque03004e62012-09-06 01:12:28 -05001456 ccn_charbuf_append_string(lsa_data,"|");
akmhoqueb77b95f2013-02-08 12:28:47 -06001457 free(temp_length);
akmhoque03004e62012-09-06 01:12:28 -05001458
1459 char *temp_ltype=(char *)malloc(20);
1460 memset(temp_ltype,0,20);
1461 sprintf(temp_ltype,"%d",adj_lsa->header->ls_type);
1462 ccn_charbuf_append_string(lsa_data,temp_ltype);
akmhoque03004e62012-09-06 01:12:28 -05001463 ccn_charbuf_append_string(lsa_data,"|");
akmhoqueb77b95f2013-02-08 12:28:47 -06001464 free(temp_ltype);
akmhoque03004e62012-09-06 01:12:28 -05001465
1466 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_time);
1467 ccn_charbuf_append_string(lsa_data,"|");
1468
1469 char *temp_nl=(char *)malloc(20);
1470 memset(temp_nl,0,20);
1471 sprintf(temp_nl,"%d",adj_lsa->no_link);
1472 ccn_charbuf_append_string(lsa_data,temp_nl);
akmhoque03004e62012-09-06 01:12:28 -05001473 ccn_charbuf_append_string(lsa_data,"|");
akmhoqueb77b95f2013-02-08 12:28:47 -06001474 free(temp_nl);
akmhoque03004e62012-09-06 01:12:28 -05001475
1476 ccn_charbuf_append_string(lsa_data,adj_lsa->body);
1477
1478
1479 }
1480 else if(res == HT_NEW_ENTRY)
1481 {
1482 hashtb_delete(e);
1483 }
1484
1485 hashtb_end(e);
1486}
akmhoqueffacaa82012-09-13 17:48:30 -05001487
akmhoque3171d652012-11-13 11:44:33 -06001488void
1489make_name_lsa_invalid(struct name_prefix *np,int ls_type, long int ls_id)
1490{
1491
1492 if ( nlsr->debugging )
1493 printf("make_name_lsa_invalid called \n");
1494 if ( nlsr->detailed_logging )
1495 writeLogg(__FILE__,__FUNCTION__,__LINE__,"make_name_lsa_invalid called \n");
1496
1497
1498 char lst[2];
1499 memset(lst,0,2);
1500 sprintf(lst,"%d",ls_type);
1501
1502 char lsid[10];
1503 memset(lsid,0,10);
1504 sprintf(lsid,"%ld",ls_id);
1505
1506
1507 char *key=(char *)malloc(strlen(np->name)+1+strlen(lst)+1+strlen(lsid)+1);
1508 memset(key,0,strlen(np->name)+1+strlen(lst)+1+strlen(lsid)+1);
1509
1510
1511 make_name_lsa_key(key, np->name,ls_type,ls_id);
akmhoqueb77b95f2013-02-08 12:28:47 -06001512
1513 if ( nlsr->debugging )
1514 printf("Key:%s Length:%d\n",key,(int)strlen(key));
akmhoque3171d652012-11-13 11:44:33 -06001515
1516 struct nlsa *nlsa;
1517
1518 struct hashtb_enumerator ee;
1519 struct hashtb_enumerator *e = &ee;
1520
1521 int res;
1522
1523 hashtb_start(nlsr->lsdb->name_lsdb, e);
1524 res = hashtb_seek(e, key,strlen(key) , 0);
1525
1526 if( res == HT_OLD_ENTRY )
1527 {
1528 nlsa=e->data;
1529
1530 nlsa->header->isValid=0;
1531
1532 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1533 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1534 write_log_for_name_lsa(nlsa);
1535 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1536
1537 hashtb_delete(e);
1538 }
1539 else if( res == HT_NEW_ENTRY )
1540 {
1541 hashtb_delete(e);
1542 }
1543 hashtb_end(e);
1544
1545 if ( nlsr->debugging )
1546 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1547 if ( nlsr->detailed_logging )
1548 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1549
1550 set_new_lsdb_version();
1551
1552 if ( nlsr->debugging )
1553 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1554 if ( nlsr->detailed_logging )
1555 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1556
1557}
1558
akmhoque866c2222013-02-12 10:49:33 -06001559//int
1560//delete_name_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1561int
1562delete_name_lsa(char *orig_router, char *name_prefix)
akmhoqueda5b6832012-09-13 22:33:55 -05001563{
akmhoqueb77b95f2013-02-08 12:28:47 -06001564
akmhoque7b791452012-10-30 11:24:56 -05001565 if ( nlsr->debugging )
1566 printf("delete_name_lsa called \n");
1567 if ( nlsr->detailed_logging )
1568 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_name_lsa called \n");
akmhoque866c2222013-02-12 10:49:33 -06001569 /*
akmhoqueda5b6832012-09-13 22:33:55 -05001570 if(flags == CCN_SCHEDULE_CANCEL)
1571 {
1572 return -1;
1573 }
1574
1575
1576
1577 nlsr_lock();
1578
akmhoque7b791452012-10-30 11:24:56 -05001579 if ( nlsr->debugging )
1580 printf("LSA Key: %s \n",(char *)ev->evdata);
1581 if ( nlsr->detailed_logging )
1582 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Key: %s \n",(char *)ev->evdata);
akmhoqueda5b6832012-09-13 22:33:55 -05001583
1584 struct nlsa *nlsa;
1585
1586 struct hashtb_enumerator ee;
1587 struct hashtb_enumerator *e = &ee;
1588
1589 int res;
1590
1591 hashtb_start(nlsr->lsdb->name_lsdb, e);
1592 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1593
1594 if( res == HT_OLD_ENTRY )
1595 {
1596 nlsa=e->data;
akmhoque866c2222013-02-12 10:49:33 -06001597 */
1598 delete_npt_entry_by_router_and_name_prefix(orig_router, name_prefix);
1599 /*
akmhoque9e9fc722012-09-26 14:03:25 -05001600 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1601 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1602 write_log_for_name_lsa(nlsa);
1603 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1604
akmhoqueda5b6832012-09-13 22:33:55 -05001605 hashtb_delete(e);
1606 }
akmhoque3cced642012-09-24 16:20:20 -05001607 else if( res == HT_NEW_ENTRY )
akmhoqueda5b6832012-09-13 22:33:55 -05001608 {
akmhoqueda5b6832012-09-13 22:33:55 -05001609 hashtb_delete(e);
1610 }
1611 hashtb_end(e);
akmhoque866c2222013-02-12 10:49:33 -06001612 */
akmhoqueda5b6832012-09-13 22:33:55 -05001613
akmhoque7b791452012-10-30 11:24:56 -05001614 if ( nlsr->debugging )
1615 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1616 if ( nlsr->detailed_logging )
1617 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1618
akmhoqueda5b6832012-09-13 22:33:55 -05001619 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -05001620
1621 if ( nlsr->debugging )
1622 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1623 if ( nlsr->detailed_logging )
1624 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueda5b6832012-09-13 22:33:55 -05001625
akmhoque866c2222013-02-12 10:49:33 -06001626 //nlsr_unlock();
akmhoqueda5b6832012-09-13 22:33:55 -05001627
1628 return 0;
1629}
1630
akmhoque866c2222013-02-12 10:49:33 -06001631//int
1632//delete_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1633int delete_adj_lsa()
akmhoqueda5b6832012-09-13 22:33:55 -05001634{
akmhoque866c2222013-02-12 10:49:33 -06001635
akmhoque7b791452012-10-30 11:24:56 -05001636 if ( nlsr->debugging )
1637 printf("delete_adj_lsa called \n");
1638 if ( nlsr->detailed_logging )
1639 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_adj_lsa called \n");
akmhoque866c2222013-02-12 10:49:33 -06001640
1641 /*
akmhoqueda5b6832012-09-13 22:33:55 -05001642 if(flags == CCN_SCHEDULE_CANCEL)
1643 {
1644 return -1;
1645 }
1646 nlsr_lock();
1647
akmhoque7b791452012-10-30 11:24:56 -05001648 if ( nlsr->debugging )
1649 printf("LSA Key: %s \n",(char *)ev->evdata);
1650 if ( nlsr->detailed_logging )
1651 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Key: %s \n",(char *)ev->evdata);
akmhoqueda5b6832012-09-13 22:33:55 -05001652
akmhoque9e9fc722012-09-26 14:03:25 -05001653 struct alsa *alsa;
akmhoqueda5b6832012-09-13 22:33:55 -05001654 struct hashtb_enumerator ee;
1655 struct hashtb_enumerator *e = &ee;
1656 int res;
1657
1658 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1659 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1660
1661 if( res == HT_OLD_ENTRY )
1662 {
akmhoque9e9fc722012-09-26 14:03:25 -05001663 alsa=e->data;
1664 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1665 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
1666 write_log_for_adj_lsa(alsa);
1667 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1668
akmhoqueda5b6832012-09-13 22:33:55 -05001669 hashtb_delete(e);
1670 }
1671 else if( res == HT_OLD_ENTRY )
1672 {
1673 hashtb_delete(e);
1674 }
1675 hashtb_end(e);
akmhoque866c2222013-02-12 10:49:33 -06001676 */
akmhoqueda5b6832012-09-13 22:33:55 -05001677
akmhoque7b791452012-10-30 11:24:56 -05001678 if ( nlsr->debugging )
1679 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1680 if ( nlsr->detailed_logging )
1681 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1682
akmhoqueda5b6832012-09-13 22:33:55 -05001683 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -05001684
1685 if ( nlsr->debugging )
1686 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1687 if ( nlsr->detailed_logging )
1688 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1689
akmhoqueda5b6832012-09-13 22:33:55 -05001690
1691 if ( !nlsr->is_route_calculation_scheduled)
1692 {
1693 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
1694 nlsr->is_route_calculation_scheduled=1;
1695 }
1696
akmhoque866c2222013-02-12 10:49:33 -06001697 //nlsr_unlock();
akmhoqueda5b6832012-09-13 22:33:55 -05001698
1699 return 0;
1700}
1701
akmhoqueffacaa82012-09-13 17:48:30 -05001702void
1703refresh_name_lsdb(void)
1704{
akmhoque7b791452012-10-30 11:24:56 -05001705 if ( nlsr->debugging )
1706 printf("refresh_name_lsdb called \n");
1707 if ( nlsr->detailed_logging )
1708 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_name_lsdb called \n");
akmhoqueffacaa82012-09-13 17:48:30 -05001709
1710 char *time_stamp=(char *)malloc(20);
1711 memset(time_stamp,0,20);
1712 get_current_timestamp_micro(time_stamp);
1713
1714 long int lsa_life_time;
1715
1716 int i, name_lsdb_element;
1717 struct nlsa *name_lsa;
1718
1719 struct hashtb_enumerator ee;
1720 struct hashtb_enumerator *e = &ee;
1721
1722 hashtb_start(nlsr->lsdb->name_lsdb, e);
1723 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
1724
1725 for(i=0;i<name_lsdb_element;i++)
1726 {
1727 name_lsa=e->data;
1728
1729 lsa_life_time=get_time_diff(time_stamp,name_lsa->header->orig_time);
akmhoque7b791452012-10-30 11:24:56 -05001730 if ( nlsr->debugging )
1731 printf("LSA Life Time: %ld \n",lsa_life_time);
1732 if ( nlsr->detailed_logging )
1733 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Life Time: %ld \n",lsa_life_time);
akmhoqueda5b6832012-09-13 22:33:55 -05001734
1735 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name) == 0)
1736 {
1737 if ( lsa_life_time > nlsr->lsa_refresh_time )
1738 {
akmhoque14b3f342012-09-14 10:39:02 -05001739 if ( name_lsa->header->isValid == NAME_LSA_VALID )
1740 {
akmhoque7b791452012-10-30 11:24:56 -05001741 if ( nlsr->debugging )
1742 printf("Own Name LSA need to be refrshed\n");
1743 if ( nlsr->detailed_logging )
1744 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Own Name LSA need to be refrshed\n");
akmhoquea98c2142012-10-25 15:22:24 -05001745
1746 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1747 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1748 write_log_for_name_lsa(name_lsa);
1749 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1750
1751
akmhoque14b3f342012-09-14 10:39:02 -05001752 char *current_time_stamp=(char *)malloc(20);
1753 memset(current_time_stamp,0,20);
1754 get_current_timestamp_micro(current_time_stamp);
akmhoqueda5b6832012-09-13 22:33:55 -05001755
akmhoque14b3f342012-09-14 10:39:02 -05001756 free(name_lsa->header->orig_time);
1757 name_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1758 memset(name_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1759 memcpy(name_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
akmhoquea98c2142012-10-25 15:22:24 -05001760
1761 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1762 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
1763 write_log_for_name_lsa(name_lsa);
1764 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
akmhoqueda5b6832012-09-13 22:33:55 -05001765
akmhoque14b3f342012-09-14 10:39:02 -05001766 free(current_time_stamp);
akmhoque866c2222013-02-12 10:49:33 -06001767
1768 hashtb_next(e);
akmhoque14b3f342012-09-14 10:39:02 -05001769 }
1770 else
akmhoque866c2222013-02-12 10:49:33 -06001771 { /*
akmhoque14b3f342012-09-14 10:39:02 -05001772 char lst[2];
1773 memset(lst,0,2);
1774 sprintf(lst,"%d",name_lsa->header->ls_type);
1775
1776 char lsid[10];
1777 memset(lsid,0,10);
1778 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1779
akmhoque866c2222013-02-12 10:49:33 -06001780
akmhoque14b3f342012-09-14 10:39:02 -05001781 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1782 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1783
1784
1785 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
akmhoque14b3f342012-09-14 10:39:02 -05001786
1787 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
akmhoque866c2222013-02-12 10:49:33 -06001788 */
1789 delete_name_lsa(name_lsa->header->orig_router->name, name_lsa->name_prefix->name);
1790 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1791 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1792 write_log_for_name_lsa(name_lsa);
1793 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1794 hashtb_delete(e);
1795 i++;
akmhoque14b3f342012-09-14 10:39:02 -05001796 }
akmhoqueda5b6832012-09-13 22:33:55 -05001797
akmhoque7b791452012-10-30 11:24:56 -05001798 if ( nlsr->debugging )
1799 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1800 if ( nlsr->detailed_logging )
1801 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1802
akmhoqueda5b6832012-09-13 22:33:55 -05001803 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -05001804
1805 if ( nlsr->debugging )
1806 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1807 if ( nlsr->detailed_logging )
1808 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1809
1810
akmhoqueda5b6832012-09-13 22:33:55 -05001811
1812 print_name_lsdb();
akmhoque866c2222013-02-12 10:49:33 -06001813 }
1814 else
1815 {
1816 hashtb_next(e);
akmhoqueda5b6832012-09-13 22:33:55 -05001817 }
1818 }
1819 else
1820 {
1821 if ( lsa_life_time > nlsr->router_dead_interval )
1822 {
akmhoque7b791452012-10-30 11:24:56 -05001823 if ( nlsr->debugging )
1824 printf("Others Name LSA need to be deleted\n");
1825 if ( nlsr->detailed_logging )
1826 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Others Name LSA need to be deleted\n");
akmhoqueda5b6832012-09-13 22:33:55 -05001827
akmhoque866c2222013-02-12 10:49:33 -06001828 /*char lst[2];
akmhoqueda5b6832012-09-13 22:33:55 -05001829 memset(lst,0,2);
1830 sprintf(lst,"%d",name_lsa->header->ls_type);
1831
1832 char lsid[10];
1833 memset(lsid,0,10);
1834 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1835
1836
1837 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1838 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1839
1840
1841 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
akmhoqueda5b6832012-09-13 22:33:55 -05001842
1843 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
akmhoque866c2222013-02-12 10:49:33 -06001844 */
1845 delete_name_lsa(name_lsa->header->orig_router->name, name_lsa->name_prefix->name);
1846 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1847 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1848 write_log_for_name_lsa(name_lsa);
1849 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1850 hashtb_delete(e);
1851 i++;
1852 }
1853 else
1854 {
1855 hashtb_next(e);
akmhoqueda5b6832012-09-13 22:33:55 -05001856 }
1857 }
akmhoque866c2222013-02-12 10:49:33 -06001858 /*else
1859 {
1860 hashtb_next(e);
1861 }*/
akmhoqueffacaa82012-09-13 17:48:30 -05001862 }
1863
1864 hashtb_end(e);
1865
1866 free(time_stamp);
akmhoqueda5b6832012-09-13 22:33:55 -05001867
akmhoqueffacaa82012-09-13 17:48:30 -05001868
1869}
1870
akmhoqueda5b6832012-09-13 22:33:55 -05001871void
akmhoqueffacaa82012-09-13 17:48:30 -05001872refresh_adj_lsdb(void)
1873{
akmhoqueffacaa82012-09-13 17:48:30 -05001874
akmhoque7b791452012-10-30 11:24:56 -05001875 if ( nlsr->debugging )
1876 printf("refresh_adj_lsdb called \n");
1877 if ( nlsr->detailed_logging )
1878 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_adj_lsdb called \n");
akmhoqueda5b6832012-09-13 22:33:55 -05001879
akmhoqueffacaa82012-09-13 17:48:30 -05001880 char *time_stamp=(char *)malloc(20);
1881 memset(time_stamp,0,20);
1882 get_current_timestamp_micro(time_stamp);
1883
1884 long int lsa_life_time;
1885
1886 int i, adj_lsdb_element;
1887 struct alsa *adj_lsa;
1888
1889 struct hashtb_enumerator ee;
1890 struct hashtb_enumerator *e = &ee;
1891
1892 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1893 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1894
1895 for(i=0;i<adj_lsdb_element;i++)
1896 {
1897 adj_lsa=e->data;
1898
akmhoqueb77b95f2013-02-08 12:28:47 -06001899 lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
akmhoque7b791452012-10-30 11:24:56 -05001900
1901 if ( nlsr->debugging )
1902 printf("LSA Life Time: %ld \n",lsa_life_time);
1903 if ( nlsr->detailed_logging )
1904 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Life Time: %ld \n",lsa_life_time);
akmhoqueffacaa82012-09-13 17:48:30 -05001905
akmhoqueda5b6832012-09-13 22:33:55 -05001906 if ( strcmp(adj_lsa->header->orig_router->name,nlsr->router_name) == 0)
1907 {
1908 if ( lsa_life_time > nlsr->lsa_refresh_time )
1909 {
akmhoque7b791452012-10-30 11:24:56 -05001910 if ( nlsr->debugging )
1911 printf("Own Adj LSA need to be refrshed\n");
1912 if ( nlsr->detailed_logging )
1913 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Own Adj LSA need to be refrshed\n");
akmhoqueda5b6832012-09-13 22:33:55 -05001914
akmhoquea98c2142012-10-25 15:22:24 -05001915 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1916 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
1917 write_log_for_adj_lsa(adj_lsa);
1918 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1919
akmhoqueda5b6832012-09-13 22:33:55 -05001920 char *current_time_stamp=(char *)malloc(20);
1921 memset(current_time_stamp,0,20);
1922 get_current_timestamp_micro(current_time_stamp);
1923
1924 free(adj_lsa->header->orig_time);
1925 adj_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1926 memset(adj_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1927 memcpy(adj_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1928
1929 free(current_time_stamp);
1930
akmhoquea98c2142012-10-25 15:22:24 -05001931 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1932 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
1933 write_log_for_adj_lsa(adj_lsa);
1934 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoqueda5b6832012-09-13 22:33:55 -05001935
akmhoque7b791452012-10-30 11:24:56 -05001936 if ( nlsr->debugging )
1937 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1938 if ( nlsr->detailed_logging )
1939 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1940
akmhoqueda5b6832012-09-13 22:33:55 -05001941 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -05001942
1943 if ( nlsr->debugging )
1944 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1945 if ( nlsr->detailed_logging )
1946 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueda5b6832012-09-13 22:33:55 -05001947
1948 print_adj_lsdb();
akmhoque866c2222013-02-12 10:49:33 -06001949
1950
akmhoqueda5b6832012-09-13 22:33:55 -05001951 }
akmhoque866c2222013-02-12 10:49:33 -06001952
1953 hashtb_next(e);
akmhoqueda5b6832012-09-13 22:33:55 -05001954 }
1955 else
1956 {
1957 if ( lsa_life_time > nlsr->router_dead_interval )
1958 {
akmhoque7b791452012-10-30 11:24:56 -05001959
1960 if ( nlsr->debugging )
1961 printf("Others Adj LSA need to be deleted\n");
1962 if ( nlsr->detailed_logging )
1963 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Others Adj LSA need to be deleted\n");
akmhoque866c2222013-02-12 10:49:33 -06001964 /*
akmhoqueda5b6832012-09-13 22:33:55 -05001965 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
1966 memset(key,0,adj_lsa->header->orig_router->length+2);
1967 make_adj_lsa_key(key,adj_lsa);
akmhoqueb77b95f2013-02-08 12:28:47 -06001968
akmhoqueda5b6832012-09-13 22:33:55 -05001969 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_adj_lsa, (void *)key, 0);
akmhoque866c2222013-02-12 10:49:33 -06001970 */
1971
1972 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1973 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
1974 write_log_for_adj_lsa(adj_lsa);
1975 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1976 delete_adj_lsa();
1977 hashtb_delete(e);
1978 i++;
1979
1980 }
1981 else
1982 {
1983 hashtb_next(e);
akmhoqueda5b6832012-09-13 22:33:55 -05001984 }
1985 }
1986
1987
1988
akmhoque866c2222013-02-12 10:49:33 -06001989 //hashtb_next(e);
akmhoqueffacaa82012-09-13 17:48:30 -05001990 }
1991
1992 hashtb_end(e);
1993
1994 free(time_stamp);
akmhoqueffacaa82012-09-13 17:48:30 -05001995}
1996
akmhoqueb77b95f2013-02-08 12:28:47 -06001997
1998void
1999refresh_cor_lsdb(void)
2000{
2001
2002 if ( nlsr->debugging )
2003 printf("refresh_cor_lsdb called \n");
2004 if ( nlsr->detailed_logging )
2005 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_cor_lsdb called \n");
2006
2007 char *time_stamp=(char *)malloc(20);
2008 memset(time_stamp,0,20);
2009 get_current_timestamp_micro(time_stamp);
2010
2011 long int lsa_life_time;
2012
2013 int i, cor_lsdb_element;
2014 struct clsa *cor_lsa;
2015
2016 struct hashtb_enumerator ee;
2017 struct hashtb_enumerator *e = &ee;
2018
2019 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2020 cor_lsdb_element=hashtb_n(nlsr->lsdb->cor_lsdb);
2021
2022 for(i=0;i<cor_lsdb_element;i++)
2023 {
2024 cor_lsa=e->data;
2025
2026 lsa_life_time=get_time_diff(time_stamp,cor_lsa->header->orig_time);
2027
2028 if ( nlsr->debugging )
2029 printf("LSA Life Time: %ld \n",lsa_life_time);
2030 if ( nlsr->detailed_logging )
2031 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Life Time: %ld \n",lsa_life_time);
2032
2033 if ( strcmp(cor_lsa->header->orig_router->name,nlsr->router_name) == 0)
2034 {
2035 if ( lsa_life_time > nlsr->lsa_refresh_time )
2036 {
2037 if ( nlsr->debugging )
2038 printf("Own Cor LSA need to be refrshed\n");
2039 if ( nlsr->detailed_logging )
2040 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Own Cor LSA need to be refrshed\n");
2041
2042 //writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
2043 //writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
2044 //write_log_for_adj_lsa(adj_lsa);
2045 //writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
2046
2047 char *current_time_stamp=(char *)malloc(20);
2048 memset(current_time_stamp,0,20);
2049 get_current_timestamp_micro(current_time_stamp);
2050
2051 free(cor_lsa->header->orig_time);
2052 cor_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
2053 memset(cor_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
2054 memcpy(cor_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
2055
2056 free(current_time_stamp);
2057
2058 //writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
2059 //writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
2060 //write_log_for_adj_lsa(adj_lsa);
2061 //writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
2062
2063 if ( nlsr->debugging )
2064 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
2065 if ( nlsr->detailed_logging )
2066 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
2067
2068 set_new_lsdb_version();
2069
2070 if ( nlsr->debugging )
2071 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
2072 if ( nlsr->detailed_logging )
2073 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
2074
2075 print_adj_lsdb();
akmhoque866c2222013-02-12 10:49:33 -06002076 }
2077
2078 hashtb_next(e);
akmhoqueb77b95f2013-02-08 12:28:47 -06002079 }
2080 else
2081 {
2082 if ( lsa_life_time > nlsr->router_dead_interval )
2083 {
2084
2085 if ( nlsr->debugging )
2086 printf("Others Adj LSA need to be deleted\n");
2087 if ( nlsr->detailed_logging )
2088 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Others Adj LSA need to be deleted\n");
2089
2090 hashtb_delete(e);
2091 i++;
2092 }
akmhoque866c2222013-02-12 10:49:33 -06002093 else
2094 {
2095 hashtb_next(e);
2096 }
akmhoqueb77b95f2013-02-08 12:28:47 -06002097 }
2098
2099
akmhoque866c2222013-02-12 10:49:33 -06002100
akmhoqueb77b95f2013-02-08 12:28:47 -06002101 }
2102
2103 hashtb_end(e);
2104
2105 free(time_stamp);
2106}
2107
akmhoqueffacaa82012-09-13 17:48:30 -05002108int
2109refresh_lsdb(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
2110{
2111 if(flags == CCN_SCHEDULE_CANCEL)
2112 {
2113 return -1;
2114 }
2115
2116 nlsr_lock();
akmhoqueda5b6832012-09-13 22:33:55 -05002117
akmhoque7b791452012-10-30 11:24:56 -05002118 if ( nlsr->debugging )
2119 printf("refresh_lsdb called\n");
2120 if ( nlsr->detailed_logging )
2121 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_lsdb called\n");
akmhoqueffacaa82012-09-13 17:48:30 -05002122
2123 refresh_name_lsdb();
2124 refresh_adj_lsdb();
akmhoqueb77b95f2013-02-08 12:28:47 -06002125 refresh_cor_lsdb();
akmhoqueffacaa82012-09-13 17:48:30 -05002126
akmhoqueffacaa82012-09-13 17:48:30 -05002127 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoqueda5b6832012-09-13 22:33:55 -05002128
akmhoqueffacaa82012-09-13 17:48:30 -05002129 nlsr_unlock();
2130 return 0;
2131}
akmhoqueb77b95f2013-02-08 12:28:47 -06002132
2133void
2134write_adj_lsa_to_repo(char *repo_content_prefix, struct name_prefix *lsa_id)
2135{
2136 if ( nlsr->debugging )
2137 printf("write_adj_lsa_to_repo called\n");
2138 if ( nlsr->debugging )
2139 printf("Content Prefix: %s\n",repo_content_prefix);
2140
2141 struct ccn_charbuf *lsa_data=ccn_charbuf_create();
2142 get_adj_lsa_data(lsa_data,lsa_id);
2143 if ( nlsr->debugging )
2144 printf("Adj LSA Data: %s \n",ccn_charbuf_as_string(lsa_data));
2145
2146 write_data_to_repo(ccn_charbuf_as_string(lsa_data), repo_content_prefix);
2147
2148 ccn_charbuf_destroy(&lsa_data);
2149}
2150
2151void
2152write_name_lsa_to_repo(char *repo_content_prefix, struct name_prefix *lsa_id)
2153{
2154 if ( nlsr->debugging )
2155 printf("write_name_lsa_to_repo called\n");
2156 if ( nlsr->debugging )
2157 printf("Content Prefix: %s\n",repo_content_prefix);
2158
2159 struct ccn_charbuf *lsa_data=ccn_charbuf_create();
2160 get_name_lsa_data(lsa_data,lsa_id);
2161
2162 if ( nlsr->debugging )
2163 printf("Name LSA Data: %s \n",ccn_charbuf_as_string(lsa_data));
2164
2165 write_data_to_repo(ccn_charbuf_as_string(lsa_data), repo_content_prefix);
2166
2167 ccn_charbuf_destroy(&lsa_data);
2168}
2169
2170
2171void
2172write_name_lsdb_to_repo(char *slice_prefix)
2173{
2174 int i, name_lsdb_element;
2175
2176 struct nlsa *name_lsa;
2177 struct hashtb_enumerator ee;
2178 struct hashtb_enumerator *e = &ee;
2179
2180 hashtb_start(nlsr->lsdb->name_lsdb, e);
2181 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
2182
2183 for(i=0;i<name_lsdb_element;i++)
2184 {
2185 name_lsa=e->data;
2186
2187 char lst[2];
2188 memset(lst,0,2);
2189 sprintf(lst,"%d",name_lsa->header->ls_type);
2190
2191 char lsid[10];
2192 memset(lsid,0,10);
2193 sprintf(lsid,"%ld",name_lsa->header->ls_id);
2194
2195
2196 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
2197 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
2198
2199
2200 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
2201
2202 if ( nlsr->debugging )
2203 printf("Name LSA Key: %s \n",key);
2204
2205
2206 char *repo_key=(char *)malloc(strlen(slice_prefix)+1+strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1+strlen(name_lsa->header->orig_time)+1+15);
2207 memset(repo_key,0,strlen(slice_prefix)+1+strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1+strlen(name_lsa->header->orig_time)+1+15);
2208 make_name_lsa_prefix_for_repo(repo_key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id,name_lsa->header->orig_time,slice_prefix);
2209
2210 if ( nlsr->debugging )
2211 printf("Name LSA Repo Key: %s \n",repo_key);
2212
2213 struct name_prefix *lsaid=(struct name_prefix *)malloc(sizeof(struct name_prefix));
2214 lsaid->name=(char *)malloc(strlen(key)+1);
2215 memset(lsaid->name, 0, strlen(key)+1);
2216 memcpy(lsaid->name,key,strlen(key));
2217 lsaid->length=strlen(key)+1;
2218
2219
2220 write_name_lsa_to_repo(repo_key, lsaid);
2221
2222 free(key);
2223 free(repo_key);
2224 free(lsaid->name);
2225 free(lsaid);
2226
2227 hashtb_next(e);
2228 }
2229
2230 hashtb_end(e);
2231
2232
2233}
2234
2235void
2236print_cor_lsa(struct clsa *cor_lsa)
2237{
2238 if ( nlsr->debugging )
2239 {
2240 printf("-----------Cor LSA Content---------------\n");
2241 printf(" Origination Router : %s\n",cor_lsa->header->orig_router->name);
2242 printf(" Origination Router Length: %d\n",cor_lsa->header->orig_router->length);
2243 printf(" LS Type : %d\n",cor_lsa->header->ls_type);
2244 printf(" Origination Time : %s\n",cor_lsa->header->orig_time);
2245 printf(" LSA Data \n");
2246 printf(" Cor R: : %f\n",cor_lsa->cor_r);
2247 printf(" Cor Theta : %f\n",cor_lsa->cor_theta);
2248
2249 printf("\n");
2250 }
2251}
2252
2253void
2254print_cor_lsdb()
2255{
2256
2257 if ( nlsr->debugging )
2258 printf("print_cor_lsdb called \n");
2259
2260 struct hashtb_enumerator ee;
2261 struct hashtb_enumerator *e = &ee;
2262
2263 int i=1;
2264
2265 for (hashtb_start(nlsr->lsdb->cor_lsdb, e); e->key != NULL; hashtb_next(e))
2266 {
2267 if ( nlsr->debugging )
2268 printf("-----------Cor LSA (%d)---------------\n",i);
2269 struct clsa *cor_lsa=e->data;
2270 print_cor_lsa(cor_lsa);
2271 i++;
2272 }
2273 hashtb_end(e);
2274
2275 if ( nlsr->debugging )
2276 printf("\n");
2277 if ( nlsr->detailed_logging )
2278 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
2279}
2280
2281void
2282install_cor_lsa(struct clsa *cor_lsa)
2283{
2284 if ( nlsr->debugging )
2285 printf("install_cor_lsa called \n");
2286 if ( nlsr->detailed_logging )
2287 writeLogg(__FILE__,__FUNCTION__,__LINE__,"install_cor_lsa called \n");
2288
2289
2290 char *time_stamp=(char *)malloc(20);
2291 memset(time_stamp,0,20);
2292 get_current_timestamp_micro(time_stamp);
2293
2294
akmhoque569a93d2013-02-21 10:19:54 -06002295 char *key=(char *)malloc(cor_lsa->header->orig_router->length+4);
2296 memset(key,0,cor_lsa->header->orig_router->length+4);
akmhoqueb77b95f2013-02-08 12:28:47 -06002297 make_cor_lsa_key(key,cor_lsa);
2298
2299 if ( nlsr->debugging )
2300 printf("Cor LSA key: %s \n",key);
2301
2302 struct hashtb_enumerator ee;
2303 struct hashtb_enumerator *e = &ee;
2304 int res;
2305
2306 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2307 res = hashtb_seek(e, key, strlen(key), 0);
2308
2309 if ( res == HT_NEW_ENTRY )
2310 {
2311 if ( nlsr->debugging )
2312 printf("New Cor LSA... \n");
2313
2314 struct clsa *new_cor_lsa=(struct clsa *)malloc(sizeof( struct clsa ));
2315 new_cor_lsa=e->data;
2316 new_cor_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
2317
2318 new_cor_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
2319 new_cor_lsa->header->orig_router->name=(char *)malloc(strlen(cor_lsa->header->orig_router->name)+1);
2320 memset(new_cor_lsa->header->orig_router->name,0,strlen(cor_lsa->header->orig_router->name)+1);
2321 memcpy(new_cor_lsa->header->orig_router->name,cor_lsa->header->orig_router->name,strlen(cor_lsa->header->orig_router->name)+1);
2322 new_cor_lsa->header->orig_router->length=cor_lsa->header->orig_router->length;
2323
2324 new_cor_lsa->header->orig_time=(char *)malloc(strlen(cor_lsa->header->orig_time)+1); //free
2325 memset(new_cor_lsa->header->orig_time,0,strlen(cor_lsa->header->orig_time)+1);
2326 memcpy(new_cor_lsa->header->orig_time,cor_lsa->header->orig_time,strlen(cor_lsa->header->orig_time)+1);
2327
2328 new_cor_lsa->header->ls_type=cor_lsa->header->ls_type;
2329
2330 new_cor_lsa->cor_r=cor_lsa->cor_r;
2331 new_cor_lsa->cor_theta=cor_lsa->cor_theta;
2332 }
2333 else if ( res == HT_OLD_ENTRY )
2334 {
2335 if ( nlsr->debugging )
2336 printf("Cor LSA exists (Old)... \n");
2337 }
2338 hashtb_end(e);
2339
akmhoque569a93d2013-02-21 10:19:54 -06002340 //free(key);
akmhoqueb77b95f2013-02-08 12:28:47 -06002341
2342}
2343
2344void
2345build_cor_lsa(struct clsa *cor_lsa, double cor_r, double cor_theta)
2346{
2347 cor_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
2348 cor_lsa->header->ls_type=LS_TYPE_COR;
2349
2350 char *time_stamp=(char *)malloc(20);
2351 memset(time_stamp,0,20);
2352 get_current_timestamp_micro(time_stamp);
2353
2354 cor_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1); //free
2355 memset(cor_lsa->header->orig_time,0,strlen(time_stamp)+1);
2356 memcpy(cor_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
2357 free(time_stamp);
2358
2359 cor_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
2360 cor_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1);
2361 memset(cor_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
2362 memcpy(cor_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
2363 cor_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
2364
2365 cor_lsa->cor_r=cor_r;
2366 cor_lsa->cor_theta=cor_theta;
2367
2368}
2369
2370void
2371build_others_cor_lsa(struct clsa *cor_lsa,char *orig_router, int ls_type,char *orig_time, double cor_r, double cor_theta)
2372{
2373 cor_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
2374 cor_lsa->header->ls_type=ls_type;
2375
2376 cor_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
2377 memset(cor_lsa->header->orig_time,0,strlen(orig_time)+1);
akmhoque569a93d2013-02-21 10:19:54 -06002378 memcpy(cor_lsa->header->orig_time,orig_time,strlen(orig_time));
akmhoqueb77b95f2013-02-08 12:28:47 -06002379
2380 cor_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
2381 cor_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
2382 memset(cor_lsa->header->orig_router->name,0,strlen(orig_router)+1);
akmhoque569a93d2013-02-21 10:19:54 -06002383 memcpy(cor_lsa->header->orig_router->name,orig_router,strlen(orig_router));
akmhoqueb77b95f2013-02-08 12:28:47 -06002384 cor_lsa->header->orig_router->length=strlen(orig_router)+1;
2385
2386 cor_lsa->cor_r=cor_r;
2387 cor_lsa->cor_theta=cor_theta;
2388
2389}
2390
2391void
2392build_and_install_others_cor_lsa(char *orig_router,int ls_type,char *orig_time, double cor_r, double cor_theta)
2393{
2394 struct clsa *cor_lsa=(struct clsa *)malloc(sizeof( struct clsa ));
2395 build_others_cor_lsa(cor_lsa,orig_router,ls_type, orig_time, cor_r, cor_theta);
2396 install_cor_lsa(cor_lsa);
2397
2398 print_cor_lsdb();
akmhoque569a93d2013-02-21 10:19:54 -06002399
2400 free(cor_lsa->header->orig_router->name);
akmhoqueb77b95f2013-02-08 12:28:47 -06002401 free(cor_lsa->header->orig_router);
2402 free(cor_lsa->header->orig_time);
2403 free(cor_lsa->header);
2404 free(cor_lsa);
2405
2406}
2407
2408
2409void
2410build_and_install_cor_lsa()
2411{
2412
2413
2414
2415 struct clsa *cor_lsa=(struct clsa *)malloc(sizeof( struct clsa ));
2416
2417 build_cor_lsa(cor_lsa,nlsr->cor_r,nlsr->cor_theta);
2418 install_cor_lsa(cor_lsa);
2419
2420 write_cor_lsa_to_repo(cor_lsa);
2421
2422 print_cor_lsdb();
2423
2424 free(cor_lsa->header->orig_router);
2425 free(cor_lsa->header->orig_time);
2426 free(cor_lsa->header);
2427 free(cor_lsa);
2428
2429}
2430
2431void
2432get_cor_lsa_data(struct ccn_charbuf *lsa_data,char *cor_lsa_key)
2433{
2434 if ( nlsr->debugging )
2435 printf("get_cor_lsa_data called \n");
2436 if ( nlsr->detailed_logging )
2437 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_adj_lsa_data called \n");
2438
2439 struct clsa *cor_lsa=(struct clsa*)malloc(sizeof(struct clsa ));
2440
2441 struct hashtb_enumerator ee;
2442 struct hashtb_enumerator *e = &ee;
2443 int res;
2444
2445 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2446 res = hashtb_seek(e, cor_lsa_key, strlen(cor_lsa_key), 0);
2447
2448 if( res == HT_OLD_ENTRY )
2449 {
2450 cor_lsa=e->data;
2451
2452 if ( nlsr->debugging )
2453 printf("Cor LSA found \n");
2454 if ( nlsr->detailed_logging )
2455 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Cor LSA found \n");
2456
2457 ccn_charbuf_append_string(lsa_data,cor_lsa->header->orig_router->name);
2458 ccn_charbuf_append_string(lsa_data,"|");
2459
2460 char *temp_length=(char *)malloc(20);
2461 memset(temp_length,0,20);
2462 sprintf(temp_length,"%d",cor_lsa->header->orig_router->length);
2463 ccn_charbuf_append_string(lsa_data,temp_length);
2464 ccn_charbuf_append_string(lsa_data,"|");
2465 free(temp_length);
2466
2467 char *temp_ltype=(char *)malloc(20);
2468 memset(temp_ltype,0,20);
2469 sprintf(temp_ltype,"%d",cor_lsa->header->ls_type);
2470 ccn_charbuf_append_string(lsa_data,temp_ltype);
2471 ccn_charbuf_append_string(lsa_data,"|");
2472 free(temp_ltype);
2473
2474 ccn_charbuf_append_string(lsa_data,cor_lsa->header->orig_time);
2475 ccn_charbuf_append_string(lsa_data,"|");
2476
2477 char *cor_r=(char *)malloc(20);
2478 memset(cor_r,0,20);
2479 sprintf(cor_r,"%f",cor_lsa->cor_r);
2480 ccn_charbuf_append_string(lsa_data,cor_r);
2481 ccn_charbuf_append_string(lsa_data,"|");
2482 free(cor_r);
2483
2484 char *cor_theta=(char *)malloc(20);
2485 memset(cor_theta,0,20);
2486 sprintf(cor_theta,"%f",cor_lsa->cor_theta);
2487 ccn_charbuf_append_string(lsa_data,cor_theta);
2488 ccn_charbuf_append_string(lsa_data,"|");
2489 free(cor_theta);
2490
2491 }
2492 else if(res == HT_NEW_ENTRY)
2493 {
2494 hashtb_delete(e);
2495 }
2496
2497 hashtb_end(e);
2498}
2499
2500void
2501write_cor_lsa_to_repo(struct clsa *cor_lsa)
2502{
2503
2504
2505 if ( nlsr->debugging )
2506 printf("write_cor_lsa_to_repo called\n");
2507
2508
2509 char *key=(char *)malloc(cor_lsa->header->orig_router->length+2+2);
2510 memset(key,0,cor_lsa->header->orig_router->length+2+2);
2511 make_cor_lsa_key(key,cor_lsa);
2512
2513 struct ccn_charbuf *lsa_data=ccn_charbuf_create();
2514 get_cor_lsa_data(lsa_data,key);
2515
2516 if ( nlsr->debugging )
2517 printf("Cor LSA Data: %s \n",ccn_charbuf_as_string(lsa_data));
2518 char *lst=(char *)malloc(20);
2519 memset(lst,0,20);
2520 sprintf(lst,"%d",cor_lsa->header->ls_type);
2521 char *repo_key=(char *)malloc(strlen(nlsr->slice_prefix)+strlen(cor_lsa->header->orig_time)+strlen(cor_lsa->header->orig_router->name) + strlen(lst) + 5+15);
2522 memset(repo_key, 0, strlen(nlsr->slice_prefix)+strlen(cor_lsa->header->orig_time)+strlen(cor_lsa->header->orig_router->name) + strlen(lst) + 5+15);
2523 make_cor_lsa_prefix_for_repo(repo_key, cor_lsa->header->orig_router->name,LS_TYPE_COR,cor_lsa->header->orig_time,nlsr->slice_prefix);
2524
2525 if ( nlsr->debugging )
2526 printf("Cor LSA Repo Key: %s \n",repo_key);
2527
2528 write_data_to_repo(ccn_charbuf_as_string(lsa_data), repo_key);
2529
2530
2531
2532 free(lst);
2533 free(key);
2534 free(repo_key);
2535 ccn_charbuf_destroy(&lsa_data);
2536}
2537
2538void
2539make_cor_lsa_key_by_router_name(char *key,char *router_name)
2540{
2541 memcpy(key+strlen(key),router_name,strlen(router_name));
2542 memcpy(key+strlen(key),"/",1);
2543 char ls_type[2];
2544 sprintf(ls_type,"%d",LS_TYPE_COR);
2545 memcpy(key+strlen(key),ls_type,strlen(ls_type));
2546 key[strlen(key)]='\0';
2547}
2548
2549
2550double
2551get_hyperbolic_r(char *router)
2552{
2553 double ret=-1.0;
2554 char *cor_lsa_key=(char *)calloc(strlen(router)+4,sizeof(char));
2555 make_cor_lsa_key_by_router_name(cor_lsa_key,router);
2556
2557
2558 struct clsa *cor_lsa;
2559 struct hashtb_enumerator ee;
2560 struct hashtb_enumerator *e = &ee;
2561 int res;
2562
2563 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2564 res = hashtb_seek(e, cor_lsa_key, strlen(cor_lsa_key), 0);
2565
2566 if ( res == HT_OLD_ENTRY)
2567 {
2568 cor_lsa=e->data;
2569 ret=cor_lsa->cor_r;
2570 }
2571 else if(res == HT_NEW_ENTRY)
2572 {
2573 hashtb_delete(e);
2574 }
2575
2576 hashtb_end(e);
2577
2578 free(cor_lsa_key);
2579 return ret;
2580}
2581
2582double
2583get_hyperbolic_theta(char *router)
2584{
2585 double ret=-1.0;
2586 char *cor_lsa_key=(char *)calloc(strlen(router)+4,sizeof(char));
2587 make_cor_lsa_key_by_router_name(cor_lsa_key,router);
2588
2589 struct clsa *cor_lsa;
2590 struct hashtb_enumerator ee;
2591 struct hashtb_enumerator *e = &ee;
2592 int res;
2593
2594 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2595 res = hashtb_seek(e, cor_lsa_key, strlen(cor_lsa_key), 0);
2596
2597 if ( res == HT_OLD_ENTRY)
2598 {
2599 cor_lsa=e->data;
2600 ret=cor_lsa->cor_theta;
2601 }
2602 else if(res == HT_NEW_ENTRY)
2603 {
2604 hashtb_delete(e);
2605 }
2606
2607 hashtb_end(e);
2608
2609 free(cor_lsa_key);
2610 return ret;
2611}