blob: 00de7b807dc4511606af2d350ec45b6586512e71 [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{
akmhoquef71d9082012-08-22 12:51:53 -040037
akmhoque6682ca32013-02-22 00:29:35 -060038 char *time_stamp=get_current_timestamp_micro_v2();
akmhoque03004e62012-09-06 01:12:28 -050039 free(nlsr->lsdb->lsdb_version);
akmhoque6682ca32013-02-22 00:29:35 -060040 nlsr->lsdb->lsdb_version=(char *)calloc(strlen(time_stamp)+1,sizeof(char));
akmhoque03004e62012-09-06 01:12:28 -050041 memcpy(nlsr->lsdb->lsdb_version,time_stamp,strlen(time_stamp)+1);
akmhoque03004e62012-09-06 01:12:28 -050042 free(time_stamp);
akmhoquef71d9082012-08-22 12:51:53 -040043}
44
akmhoque0ab71642013-02-21 10:10:33 -060045/**
46* Make key for storing Name LSA in Name LSDB
47*/
48
akmhoque03004e62012-09-06 01:12:28 -050049void
50make_name_lsa_key(char *key, char *orig_router, int ls_type, long int ls_id)
51{
52
akmhoque03004e62012-09-06 01:12:28 -050053 char lst[2];
54 memset(lst,0,2);
55 sprintf(lst,"%d",ls_type);
56
57 char lsid[10];
58 memset(lsid,0,10);
59 sprintf(lsid,"%ld",ls_id);
60
akmhoqueeacf44f2013-02-22 01:02:13 -060061 memcpy(key,orig_router,strlen(orig_router));
akmhoque03004e62012-09-06 01:12:28 -050062 memcpy(key+strlen(key),"/",1);
63 memcpy(key+strlen(key),lst,strlen(lst));
64 memcpy(key+strlen(key),"/",1);
65 memcpy(key+strlen(key),lsid,strlen(lsid));
akmhoqueeacf44f2013-02-22 01:02:13 -060066 key[strlen(key)]='\0';
67
akmhoqueb77b95f2013-02-08 12:28:47 -060068 if ( nlsr->debugging )
69 printf("name LSA Key: %s\n", key);
70}
71
akmhoque0ab71642013-02-21 10:10:33 -060072/**
73* Make content name prefix for Name LSA to store in repo
74*/
akmhoqueb77b95f2013-02-08 12:28:47 -060075
76void
akmhoque0ab71642013-02-21 10:10:33 -060077make_name_lsa_prefix_for_repo(char *key, char *orig_router, int ls_type,
78 long int ls_id,char *orig_time,char *slice_prefix)
akmhoqueb77b95f2013-02-08 12:28:47 -060079{
akmhoque0ab71642013-02-21 10:10:33 -060080 sprintf(key,"%s%s/lsType.%d/lsId.%ld/%s",slice_prefix, orig_router, ls_type,
81 ls_id, orig_time);
akmhoqueeacf44f2013-02-22 01:02:13 -060082 key[strlen(key)]='\0';
akmhoqueb77b95f2013-02-08 12:28:47 -060083 if ( nlsr->debugging )
84 printf("Name LSA prefix for repo content: %s\n",key);
85}
86
akmhoque0ab71642013-02-21 10:10:33 -060087/**
88* Make content name prefix for Adj LSA to store in repo
89*/
90
akmhoqueb77b95f2013-02-08 12:28:47 -060091void
akmhoque0ab71642013-02-21 10:10:33 -060092make_adj_lsa_prefix_for_repo(char *key, char *orig_router, int ls_type,
93 char *orig_time,char *slice_prefix)
akmhoqueb77b95f2013-02-08 12:28:47 -060094{
95
96 sprintf(key,"%s%s/lsType.%d/%s",slice_prefix,orig_router,ls_type, orig_time );
akmhoqueeacf44f2013-02-22 01:02:13 -060097 key[strlen(key)]='\0';
akmhoqueb77b95f2013-02-08 12:28:47 -060098 if ( nlsr->debugging )
99 printf("Name LSA prefix for repo content:%s\n",key);
100}
101
akmhoque0ab71642013-02-21 10:10:33 -0600102/**
103* Make content name prefix for Cor LSA to store in repo
104*/
105
akmhoqueb77b95f2013-02-08 12:28:47 -0600106void
akmhoque0ab71642013-02-21 10:10:33 -0600107make_cor_lsa_prefix_for_repo(char *key, char *orig_router, int ls_type,
108 char *orig_time,char *slice_prefix)
akmhoqueb77b95f2013-02-08 12:28:47 -0600109{
110
111 sprintf(key,"%s%s/lsType.%d/%s",slice_prefix,orig_router,ls_type, orig_time );
akmhoqueeacf44f2013-02-22 01:02:13 -0600112 key[strlen(key)]='\0';
akmhoqueb77b95f2013-02-08 12:28:47 -0600113 if ( nlsr->debugging )
114 printf("Cor LSA prefix for repo content:%s\n",key);
akmhoque03004e62012-09-06 01:12:28 -0500115}
akmhoque53f64222012-09-05 13:57:51 -0500116
akmhoque0ab71642013-02-21 10:10:33 -0600117/**
118* Build name lsa for all name prefixes in Name Prefix List (NPL). Intsall Name
119* LSA in Name LSDB for router itself.
120*/
121
akmhoqueeacf44f2013-02-22 01:02:13 -0600122void
akmhoquef5537b42013-02-22 08:33:32 -0600123destroy_name_lsa_component(struct nlsa * name_lsa)
akmhoqueeacf44f2013-02-22 01:02:13 -0600124{
125 if ( name_lsa->header->orig_router->name )
126 free(name_lsa->header->orig_router->name);
127 if ( name_lsa->header->orig_router )
128 free(name_lsa->header->orig_router);
akmhoquef5537b42013-02-22 08:33:32 -0600129 if ( name_lsa->header->orig_time )
130 free(name_lsa->header->orig_time);
akmhoqueeacf44f2013-02-22 01:02:13 -0600131 if ( name_lsa->header )
132 free(name_lsa->header);
akmhoquef5537b42013-02-22 08:33:32 -0600133
akmhoqueeacf44f2013-02-22 01:02:13 -0600134 if ( name_lsa->name_prefix->name )
135 free(name_lsa->name_prefix->name);
136 if ( name_lsa->name_prefix )
137 free(name_lsa->name_prefix);
akmhoquef5537b42013-02-22 08:33:32 -0600138}
139
140void
141destroy_name_lsa(struct nlsa * name_lsa)
142{
143 destroy_name_lsa_component(name_lsa);
akmhoqueeacf44f2013-02-22 01:02:13 -0600144 if ( name_lsa )
145 free(name_lsa);
146}
akmhoque0ab71642013-02-21 10:10:33 -0600147
akmhoquef5537b42013-02-22 08:33:32 -0600148void
149destroy_adj_lsa_component(struct alsa * adj_lsa)
150{
151 if ( adj_lsa->header->orig_router->name )
152 free(adj_lsa->header->orig_router->name);
153 if ( adj_lsa->header->orig_router )
154 free(adj_lsa->header->orig_router);
155 if ( adj_lsa->header->orig_time )
156 free(adj_lsa->header->orig_time);
157 if ( adj_lsa->header )
158 free(adj_lsa->header);
159
160 if ( adj_lsa->body )
161 free(adj_lsa->body);
162}
163
164void
165destroy_adj_lsa(struct alsa * adj_lsa)
166{
167
168 destroy_adj_lsa_component(adj_lsa);
169 if ( adj_lsa )
170 free(adj_lsa);
171}
172
173void
174destroy_cor_lsa_component(struct clsa * cor_lsa)
175{
176 if ( cor_lsa->header->orig_router->name )
177 free(cor_lsa->header->orig_router->name);
178 if ( cor_lsa->header->orig_router )
179 free(cor_lsa->header->orig_router);
180 if ( cor_lsa->header->orig_time )
181 free(cor_lsa->header->orig_time);
182 if ( cor_lsa->header )
183 free(cor_lsa->header);
184}
185
186void
187destroy_cor_lsa(struct clsa * cor_lsa)
188{
189
190 destroy_cor_lsa_component(cor_lsa);
191 if ( cor_lsa )
192 free(cor_lsa);
193}
194
195
196
akmhoquef71d9082012-08-22 12:51:53 -0400197void
akmhoque53f64222012-09-05 13:57:51 -0500198build_and_install_name_lsas(void)
akmhoquef71d9082012-08-22 12:51:53 -0400199{
akmhoque7b791452012-10-30 11:24:56 -0500200 if ( nlsr->debugging )
201 printf("build_and_install_name_lsas called \n");
202 if ( nlsr->detailed_logging )
203 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_name_lsas called\n");
akmhoquef71d9082012-08-22 12:51:53 -0400204
akmhoquef71d9082012-08-22 12:51:53 -0400205 int i, npl_element;
akmhoque3171d652012-11-13 11:44:33 -0600206 struct name_prefix_list_entry *npe;
akmhoquef71d9082012-08-22 12:51:53 -0400207
208 struct hashtb_enumerator ee;
209 struct hashtb_enumerator *e = &ee;
210
211 hashtb_start(nlsr->npl, e);
212 npl_element=hashtb_n(nlsr->npl);
213
214 for(i=0;i<npl_element;i++)
215 {
akmhoque3171d652012-11-13 11:44:33 -0600216 npe=e->data;
akmhoque53f64222012-09-05 13:57:51 -0500217 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
akmhoque3171d652012-11-13 11:44:33 -0600218 build_name_lsa(name_lsa,npe->np);
akmhoque03004e62012-09-06 01:12:28 -0500219
akmhoquef71d9082012-08-22 12:51:53 -0400220 install_name_lsa(name_lsa);
akmhoque3171d652012-11-13 11:44:33 -0600221 update_nlsa_id_for_name_in_npl(npe->np,name_lsa->header->ls_id);
akmhoqueeacf44f2013-02-22 01:02:13 -0600222 destroy_name_lsa(name_lsa);
akmhoquef71d9082012-08-22 12:51:53 -0400223 hashtb_next(e);
224 }
225
akmhoque53f64222012-09-05 13:57:51 -0500226 hashtb_end(e);
akmhoque3171d652012-11-13 11:44:33 -0600227
228 print_name_prefix_from_npl();
229
230}
231
akmhoque0ab71642013-02-21 10:10:33 -0600232/**
233* Build and Install one Name LSA Use ful for API
234*/
235
akmhoque3171d652012-11-13 11:44:33 -0600236void
237build_and_install_single_name_lsa(struct name_prefix *np)
238{
239 if ( nlsr->debugging )
240 printf("build_and_install_single_name_lsa called \n");
241 if ( nlsr->detailed_logging )
242 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_single_name_lsa called\n");
243
244 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
245 build_name_lsa(name_lsa,np);
246
247 install_name_lsa(name_lsa);
248 update_nlsa_id_for_name_in_npl(np,name_lsa->header->ls_id);
akmhoqueeacf44f2013-02-22 01:02:13 -0600249 destroy_name_lsa(name_lsa);
akmhoque3171d652012-11-13 11:44:33 -0600250 print_name_prefix_from_npl();
akmhoquef71d9082012-08-22 12:51:53 -0400251
akmhoquef71d9082012-08-22 12:51:53 -0400252}
253
akmhoque53f64222012-09-05 13:57:51 -0500254void
255build_name_lsa(struct nlsa *name_lsa, struct name_prefix *np)
256{
akmhoque53f64222012-09-05 13:57:51 -0500257 name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header ));
258 name_lsa->header->ls_type=LS_TYPE_NAME;
akmhoqueeacf44f2013-02-22 01:02:13 -0600259
260 char *time_stamp=get_current_timestamp_micro_v2();
akmhoque03004e62012-09-06 01:12:28 -0500261 name_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1); //free
akmhoque53f64222012-09-05 13:57:51 -0500262 memset(name_lsa->header->orig_time,0,strlen(time_stamp)+1);
263 memcpy(name_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
akmhoque03004e62012-09-06 01:12:28 -0500264
265 free(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -0500266
267 name_lsa->header->ls_id=++nlsr->nlsa_id;
akmhoqueeacf44f2013-02-22 01:02:13 -0600268 name_lsa->header->orig_router=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
269 name_lsa->header->orig_router->name=(char *)calloc(strlen(nlsr->router_name)+1,sizeof(char));
akmhoque03004e62012-09-06 01:12:28 -0500270 memcpy(name_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
271 name_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
akmhoque53f64222012-09-05 13:57:51 -0500272 name_lsa->header->isValid=1;
273
274
275 name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoqueeacf44f2013-02-22 01:02:13 -0600276 name_lsa->name_prefix->name=(char *)calloc(np->length,sizeof(char));
akmhoque53f64222012-09-05 13:57:51 -0500277 memcpy(name_lsa->name_prefix->name,np->name,np->length);
278 name_lsa->name_prefix->length=np->length;
279
280}
281
akmhoque53f64222012-09-05 13:57:51 -0500282void
283install_name_lsa(struct nlsa *name_lsa)
284{
akmhoque53f64222012-09-05 13:57:51 -0500285
akmhoque0ab71642013-02-21 10:10:33 -0600286 char lst[2];
287 memset(lst,0,2);
288 sprintf(lst,"%d",name_lsa->header->ls_type);
akmhoqueffacaa82012-09-13 17:48:30 -0500289
akmhoque0ab71642013-02-21 10:10:33 -0600290 char lsid[10];
291 memset(lsid,0,10);
292 sprintf(lsid,"%ld",name_lsa->header->ls_id);
akmhoque3171d652012-11-13 11:44:33 -0600293
akmhoque0ab71642013-02-21 10:10:33 -0600294 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
295 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
296 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 -0500297
akmhoque0ab71642013-02-21 10:10:33 -0600298 if ( nlsr->debugging )
299 printf("Key:%s Length:%d\n",key,(int)strlen(key));
300 if ( nlsr->detailed_logging )
301 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Key:%s Length:%d\n",key,(int)strlen(key));
302
akmhoquef5537b42013-02-22 08:33:32 -0600303 struct nlsa *new_name_lsa;
akmhoque53f64222012-09-05 13:57:51 -0500304
akmhoque0ab71642013-02-21 10:10:33 -0600305 struct hashtb_enumerator ee;
306 struct hashtb_enumerator *e = &ee;
307 int res;
akmhoque53f64222012-09-05 13:57:51 -0500308
akmhoque0ab71642013-02-21 10:10:33 -0600309 hashtb_start(nlsr->lsdb->name_lsdb, e);
310 res = hashtb_seek(e, key, strlen(key), 0);
akmhoque53f64222012-09-05 13:57:51 -0500311
akmhoque0ab71642013-02-21 10:10:33 -0600312 if(res == HT_NEW_ENTRY )
akmhoque3560cb62012-09-09 10:52:30 -0500313 {
akmhoqueffacaa82012-09-13 17:48:30 -0500314
akmhoquef5537b42013-02-22 08:33:32 -0600315 if ( name_lsa->header->isValid == 1 )
316 {
akmhoque7b791452012-10-30 11:24:56 -0500317 if ( nlsr->debugging )
318 printf("New Name LSA... Adding to LSDB\n");
319 if ( nlsr->detailed_logging )
320 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Name LSA... Adding to LSDB\n");
321
322
akmhoqueffacaa82012-09-13 17:48:30 -0500323 new_name_lsa = e->data;
akmhoque8876e982013-02-21 13:35:46 -0600324
akmhoque54d86112013-02-21 16:42:34 -0600325
akmhoque0ab71642013-02-21 10:10:33 -0600326 new_name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header ));
akmhoqueffacaa82012-09-13 17:48:30 -0500327 new_name_lsa->header->ls_type=name_lsa->header->ls_type;
328
329 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
330 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
akmhoque8876e982013-02-21 13:35:46 -0600331 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
akmhoqueffacaa82012-09-13 17:48:30 -0500332
333 new_name_lsa->header->ls_id=name_lsa->header->ls_id;
akmhoque0ab71642013-02-21 10:10:33 -0600334 new_name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoqueffacaa82012-09-13 17:48:30 -0500335 new_name_lsa->header->orig_router->name=(char *)malloc(name_lsa->header->orig_router->length);
336 memcpy(new_name_lsa->header->orig_router->name,name_lsa->header->orig_router->name,name_lsa->header->orig_router->length);
337 new_name_lsa->header->orig_router->length=name_lsa->header->orig_router->length;
338 new_name_lsa->header->isValid=name_lsa->header->isValid;
339
340
akmhoque0ab71642013-02-21 10:10:33 -0600341 new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoqueffacaa82012-09-13 17:48:30 -0500342 new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
343 memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
344 new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
akmhoque8876e982013-02-21 13:35:46 -0600345
akmhoqueffacaa82012-09-13 17:48:30 -0500346
akmhoque7b791452012-10-30 11:24:56 -0500347 if ( nlsr->debugging )
348 {
349 printf("New Name LSA Added....\n");
350 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
351 }
352 if ( nlsr->detailed_logging )
353 {
354 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Name LSA Added....\n");
355 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
356 }
akmhoque54d86112013-02-21 16:42:34 -0600357
akmhoqueffacaa82012-09-13 17:48:30 -0500358 set_new_lsdb_version();
akmhoque54d86112013-02-21 16:42:34 -0600359
akmhoque7b791452012-10-30 11:24:56 -0500360 if ( nlsr->debugging )
361 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
362 if ( nlsr->detailed_logging )
363 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500364
akmhoque9e9fc722012-09-26 14:03:25 -0500365
akmhoquede61ba92012-09-20 22:19:12 -0500366 int num_next_hop=get_number_of_next_hop(new_name_lsa->header->orig_router->name);
akmhoquede61ba92012-09-20 22:19:12 -0500367 if ( num_next_hop < 0 )
akmhoque3560cb62012-09-09 10:52:30 -0500368 {
akmhoquede61ba92012-09-20 22:19:12 -0500369 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 -0500370 if ( check == HT_NEW_ENTRY )
371 {
akmhoque7b791452012-10-30 11:24:56 -0500372 if ( nlsr->debugging )
373 printf("Added in npt \n");
374 if ( nlsr->detailed_logging )
375 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
akmhoqueffacaa82012-09-13 17:48:30 -0500376 }
akmhoque3560cb62012-09-09 10:52:30 -0500377 }
akmhoqueffacaa82012-09-13 17:48:30 -0500378 else
akmhoque3560cb62012-09-09 10:52:30 -0500379 {
akmhoquede61ba92012-09-20 22:19:12 -0500380 int *faces=malloc(num_next_hop*sizeof(int));
381 int *route_costs=malloc(num_next_hop*sizeof(int));
382 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name,faces,route_costs);
akmhoque7b791452012-10-30 11:24:56 -0500383 if ( nlsr->debugging )
384 {
385 printf("Printing from install_name_lsa \n");
386 int j;
387 for(j=0;j<num_next_hop;j++)
388 printf("Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
389 }
390 if ( nlsr->detailed_logging )
391 {
392 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Printing from install_name_lsa \n");
393 int j;
394 for(j=0;j<num_next_hop;j++)
395 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
396 }
akmhoquede61ba92012-09-20 22:19:12 -0500397 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 -0500398 if ( check == HT_NEW_ENTRY )
399 {
akmhoque7b791452012-10-30 11:24:56 -0500400 if ( nlsr->debugging )
401 printf("Added in npt \n");
402 if ( nlsr->detailed_logging )
403 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
akmhoqueffacaa82012-09-13 17:48:30 -0500404 }
akmhoquede61ba92012-09-20 22:19:12 -0500405 free(faces);
406 free(route_costs);
akmhoqueffacaa82012-09-13 17:48:30 -0500407
akmhoque3560cb62012-09-09 10:52:30 -0500408 }
akmhoquede61ba92012-09-20 22:19:12 -0500409
akmhoque9e9fc722012-09-26 14:03:25 -0500410 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
411 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
akmhoquef5537b42013-02-22 08:33:32 -0600412 write_log_for_name_lsa(new_name_lsa);
akmhoque9e9fc722012-09-26 14:03:25 -0500413 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500414
akmhoque54d86112013-02-21 16:42:34 -0600415 //free(time_stamp);
akmhoquef5537b42013-02-22 08:33:32 -0600416 }
417 else
418 {
419 hashtb_delete(e);
420 }
akmhoqueffacaa82012-09-13 17:48:30 -0500421
422 }
423 else if(res == HT_OLD_ENTRY)
424 {
425 new_name_lsa=e->data;
akmhoque14b3f342012-09-14 10:39:02 -0500426 if(strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time)<0)
akmhoqueffacaa82012-09-13 17:48:30 -0500427 {
akmhoque7b791452012-10-30 11:24:56 -0500428 if ( nlsr->debugging )
429 printf("Older Adj LSA. Discarded... \n");
430 if ( nlsr->detailed_logging )
431 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Older Adj LSA. Discarded...\n");
akmhoque14b3f342012-09-14 10:39:02 -0500432 }
433 else if( strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time) == 0 )
434 {
akmhoque7b791452012-10-30 11:24:56 -0500435 if ( nlsr->debugging )
436 printf("Duplicate Adj LSA. Discarded... \n");
437 if ( nlsr->detailed_logging )
438 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Duplicate Adj LSA. Discarded...\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500439 }
440 else
441 {
442 if ( name_lsa->header->isValid == 0 )
443 {
444 // have to call to delete npt table entry
akmhoque3cced642012-09-24 16:20:20 -0500445 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 -0500446
447 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name)!= 0)
448 {
akmhoque9e9fc722012-09-26 14:03:25 -0500449 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
450 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
451 write_log_for_name_lsa(new_name_lsa);
452 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
453
akmhoqueffacaa82012-09-13 17:48:30 -0500454 hashtb_delete(e);
akmhoque7b791452012-10-30 11:24:56 -0500455 if ( nlsr->debugging )
456 printf("isValid bit not set for Router %s so LSA Deleted from LSDB\n",name_lsa->header->orig_router->name);
457 if ( nlsr->detailed_logging )
458 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 -0500459 }
460 else
461 {
462 new_name_lsa->header->isValid=name_lsa->header->isValid;
463 free(new_name_lsa->header->orig_time);
464 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
465 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
466 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
467 }
akmhoque7b791452012-10-30 11:24:56 -0500468 if ( nlsr->debugging )
469 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
470 if ( nlsr->detailed_logging )
471 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500472 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500473 if ( nlsr->debugging )
474 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
475 if ( nlsr->detailed_logging )
476 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500477 }
478 else
479 {
480 int is_npt_update=0;
481 if ( strcmp(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name) != 0 )
482 {
483 is_npt_update=1;
akmhoque3cced642012-09-24 16:20:20 -0500484 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 -0500485 }
486
487 // copying LSA content with header
akmhoque9e9fc722012-09-26 14:03:25 -0500488 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
489 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
490 write_log_for_name_lsa(new_name_lsa);
491 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
492
akmhoqueffacaa82012-09-13 17:48:30 -0500493
494 free(new_name_lsa->header->orig_time);
495 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
496 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
akmhoque0ab71642013-02-21 10:10:33 -0600497 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time));
akmhoqueffacaa82012-09-13 17:48:30 -0500498
499 new_name_lsa->header->isValid=name_lsa->header->isValid;
500
501 free(new_name_lsa->name_prefix->name);
502 free(new_name_lsa->name_prefix);
503 new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
504 new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
505 memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
506 new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
507
akmhoque9e9fc722012-09-26 14:03:25 -0500508
509 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
510 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
511 write_log_for_name_lsa(new_name_lsa);
512 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
513
akmhoque7b791452012-10-30 11:24:56 -0500514 if ( nlsr->debugging )
515 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
516 if ( nlsr->detailed_logging )
517 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
518
akmhoqueffacaa82012-09-13 17:48:30 -0500519 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500520
521 if ( nlsr->debugging )
522 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
523 if ( nlsr->detailed_logging )
524 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
525
akmhoqueffacaa82012-09-13 17:48:30 -0500526
527 if( is_npt_update == 1 )
528 {
akmhoquede61ba92012-09-20 22:19:12 -0500529 //struct hashtb *face_list;
530 int num_next_hop=get_number_of_next_hop(new_name_lsa->header->orig_router->name);
531 if ( num_next_hop < 0 )
akmhoqueffacaa82012-09-13 17:48:30 -0500532 {
akmhoquede61ba92012-09-20 22:19:12 -0500533
534 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 -0500535 if ( check == HT_NEW_ENTRY )
536 {
akmhoque7b791452012-10-30 11:24:56 -0500537 if ( nlsr->debugging )
538 printf("Added in npt \n");
539 if ( nlsr->detailed_logging )
540 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
akmhoqueffacaa82012-09-13 17:48:30 -0500541 }
542 }
543 else
544 {
akmhoquede61ba92012-09-20 22:19:12 -0500545 int *faces=malloc(num_next_hop*sizeof(int));
546 int *route_costs=malloc(num_next_hop*sizeof(int));
547 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name,faces,route_costs);
akmhoque7b791452012-10-30 11:24:56 -0500548
549 if ( nlsr->debugging )
550 {
551 printf("Printing from install_name_lsa \n");
552 int j;
553 for(j=0;j<num_next_hop;j++)
akmhoquede61ba92012-09-20 22:19:12 -0500554 printf("Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
akmhoque7b791452012-10-30 11:24:56 -0500555 }
556 if ( nlsr->detailed_logging )
557 {
558 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Printing from install_name_lsa \n");
559 int j;
560 for(j=0;j<num_next_hop;j++)
561 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
562 }
563
akmhoquede61ba92012-09-20 22:19:12 -0500564
565 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 -0500566 if ( check == HT_NEW_ENTRY )
567 {
akmhoque7b791452012-10-30 11:24:56 -0500568 if ( nlsr->debugging )
569 printf("Added in npt \n");
570 if ( nlsr->detailed_logging )
571 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
akmhoqueffacaa82012-09-13 17:48:30 -0500572 }
akmhoquede61ba92012-09-20 22:19:12 -0500573 free(faces);
574 free(route_costs);
575
akmhoqueffacaa82012-09-13 17:48:30 -0500576 }
akmhoquede61ba92012-09-20 22:19:12 -0500577
akmhoqueffacaa82012-09-13 17:48:30 -0500578 }
579 }
580 }
581
akmhoque3560cb62012-09-09 10:52:30 -0500582 }
akmhoque53f64222012-09-05 13:57:51 -0500583
akmhoqueffacaa82012-09-13 17:48:30 -0500584 hashtb_end(e);
akmhoque53f64222012-09-05 13:57:51 -0500585
akmhoquef5537b42013-02-22 08:33:32 -0600586 free(key);
akmhoque53f64222012-09-05 13:57:51 -0500587}
588
akmhoque9e9fc722012-09-26 14:03:25 -0500589void
590write_log_for_name_lsa(struct nlsa *name_lsa)
591{
592
593 writeLogg(__FILE__,__FUNCTION__,__LINE__,"-----------Name LSA Content---------------\n");
594 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s\n",name_lsa->header->orig_router->name);
595 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router Length: %d\n",name_lsa->header->orig_router->length);
akmhoque34b99f92012-09-27 12:24:27 -0500596 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Type: %d\n",name_lsa->header->ls_type);
597 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Id: %ld\n",name_lsa->header->ls_id);
598 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Time: %s\n",name_lsa->header->orig_time);
599 writeLogg(__FILE__,__FUNCTION__,__LINE__," Is Valid: %d\n",name_lsa->header->isValid);
akmhoque9e9fc722012-09-26 14:03:25 -0500600 writeLogg(__FILE__,__FUNCTION__,__LINE__," LSA Data \n");
601 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix: %s\n",name_lsa->name_prefix->name);
602 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix Length: %d\n",name_lsa->name_prefix->length);
603 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
604}
akmhoque53f64222012-09-05 13:57:51 -0500605
akmhoque03004e62012-09-06 01:12:28 -0500606void
607print_name_lsa(struct nlsa *name_lsa)
akmhoque53f64222012-09-05 13:57:51 -0500608{
akmhoque53f64222012-09-05 13:57:51 -0500609
akmhoque7b791452012-10-30 11:24:56 -0500610 if ( nlsr->debugging )
611 {
612 printf("-----------Name LSA Content---------------\n");
613 printf(" Origination Router : %s\n",name_lsa->header->orig_router->name);
614 printf(" Origination Router Length: %d\n",name_lsa->header->orig_router->length);
615 printf(" LS Type : %d\n",name_lsa->header->ls_type);
616 printf(" LS Id : %ld\n",name_lsa->header->ls_id);
617 printf(" Origination Time : %s\n",name_lsa->header->orig_time);
618 printf(" Is Valid : %d\n",name_lsa->header->isValid);
619 printf(" LSA Data \n");
620 printf(" Name Prefix: : %s\n",name_lsa->name_prefix->name);
621 printf(" Name Prefix Length : %d\n",name_lsa->name_prefix->length);
622
623 printf("\n");
624 }
akmhoquef71d9082012-08-22 12:51:53 -0400625}
626
627void
628print_name_lsdb(void)
629{
akmhoque7b791452012-10-30 11:24:56 -0500630 if ( nlsr->debugging )
631 printf("print_name_lsdb called \n");
akmhoque53f64222012-09-05 13:57:51 -0500632 int i, name_lsdb_element;
akmhoquef71d9082012-08-22 12:51:53 -0400633 struct nlsa *name_lsa;
634
635 struct hashtb_enumerator ee;
636 struct hashtb_enumerator *e = &ee;
637
638 hashtb_start(nlsr->lsdb->name_lsdb, e);
akmhoque53f64222012-09-05 13:57:51 -0500639 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
akmhoquef71d9082012-08-22 12:51:53 -0400640
akmhoque53f64222012-09-05 13:57:51 -0500641 for(i=0;i<name_lsdb_element;i++)
akmhoquef71d9082012-08-22 12:51:53 -0400642 {
akmhoque7b791452012-10-30 11:24:56 -0500643 if ( nlsr->debugging )
644 printf("-----------Name LSA (%d)---------------\n",i+1);
akmhoquef71d9082012-08-22 12:51:53 -0400645 name_lsa=e->data;
akmhoque53f64222012-09-05 13:57:51 -0500646 print_name_lsa(name_lsa);
akmhoquef71d9082012-08-22 12:51:53 -0400647 hashtb_next(e);
648 }
649
650 hashtb_end(e);
651
akmhoque3171d652012-11-13 11:44:33 -0600652 if ( nlsr->debugging )
653 printf("\n");
654 if ( nlsr->detailed_logging )
655 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoque53f64222012-09-05 13:57:51 -0500656}
657
akmhoque03004e62012-09-06 01:12:28 -0500658
659void
660build_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 -0500661{
akmhoque7b791452012-10-30 11:24:56 -0500662 if ( nlsr->debugging )
663 printf("build_and_install_others_name_lsa called \n");
664 if ( nlsr->detailed_logging )
665 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_others_name_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500666
akmhoque03004e62012-09-06 01:12:28 -0500667 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
668 build_others_name_lsa(name_lsa,orig_router,ls_type,ls_id,orig_time, isValid,np);
669 print_name_lsa(name_lsa);
670 install_name_lsa(name_lsa);
671 print_name_lsdb();
akmhoquede61ba92012-09-20 22:19:12 -0500672 print_npt();
akmhoque53f64222012-09-05 13:57:51 -0500673
akmhoquef5537b42013-02-22 08:33:32 -0600674 destroy_name_lsa(name_lsa);
akmhoque53f64222012-09-05 13:57:51 -0500675
676}
677
678void
akmhoque03004e62012-09-06 01:12:28 -0500679build_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 -0500680{
akmhoque7b791452012-10-30 11:24:56 -0500681 if ( nlsr->debugging )
682 printf("build_others_name_lsa called\n");
683 if ( nlsr->detailed_logging )
684 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_others_name_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500685
akmhoquef5537b42013-02-22 08:33:32 -0600686 name_lsa->header=(struct nlsa_header *)calloc(1,sizeof(struct nlsa_header ));
akmhoque03004e62012-09-06 01:12:28 -0500687 name_lsa->header->ls_type=LS_TYPE_NAME;
akmhoque53f64222012-09-05 13:57:51 -0500688
akmhoquef5537b42013-02-22 08:33:32 -0600689 name_lsa->header->orig_time=(char *)calloc(strlen(orig_time)+1,sizeof(char));
690 memcpy(name_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
akmhoque53f64222012-09-05 13:57:51 -0500691
akmhoque03004e62012-09-06 01:12:28 -0500692 name_lsa->header->ls_id=ls_id;
akmhoquef5537b42013-02-22 08:33:32 -0600693 name_lsa->header->orig_router=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
694 name_lsa->header->orig_router->name=(char *)calloc(strlen(orig_router)+1,sizeof(char));
695 memcpy(name_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
akmhoque03004e62012-09-06 01:12:28 -0500696 name_lsa->header->orig_router->length=strlen(orig_router)+1;
697 name_lsa->header->isValid=isValid;
akmhoque53f64222012-09-05 13:57:51 -0500698
akmhoquef5537b42013-02-22 08:33:32 -0600699 name_lsa->name_prefix=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
700 name_lsa->name_prefix->name=(char *)calloc(strlen(np)+1,sizeof(char));
701 memcpy(name_lsa->name_prefix->name,np,strlen(np)+1);
akmhoque03004e62012-09-06 01:12:28 -0500702 name_lsa->name_prefix->length=strlen(np)+1;
akmhoquef71d9082012-08-22 12:51:53 -0400703}
akmhoqued79438d2012-08-27 13:31:42 -0500704
705
akmhoqueb77b95f2013-02-08 12:28:47 -0600706void
707make_cor_lsa_key(char *key,struct clsa *cor_lsa)
708{
709 memcpy(key+strlen(key),cor_lsa->header->orig_router->name,cor_lsa->header->orig_router->length);
710 memcpy(key+strlen(key),"/",1);
711 char ls_type[2];
712 sprintf(ls_type,"%d",cor_lsa->header->ls_type);
713 memcpy(key+strlen(key),ls_type,strlen(ls_type));
714 key[strlen(key)]='\0';
715}
akmhoqueffacaa82012-09-13 17:48:30 -0500716
717
akmhoque53f64222012-09-05 13:57:51 -0500718void
719make_adj_lsa_key(char *key,struct alsa *adj_lsa)
akmhoqued79438d2012-08-27 13:31:42 -0500720{
akmhoque53f64222012-09-05 13:57:51 -0500721 memcpy(key+strlen(key),adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
722 memcpy(key+strlen(key),"/",1);
723 char ls_type[2];
724 sprintf(ls_type,"%d",adj_lsa->header->ls_type);
725 memcpy(key+strlen(key),ls_type,strlen(ls_type));
726 key[strlen(key)]='\0';
727}
728
akmhoque03004e62012-09-06 01:12:28 -0500729int
730build_and_install_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
731{
akmhoqueffacaa82012-09-13 17:48:30 -0500732 if(flags == CCN_SCHEDULE_CANCEL)
733 {
734 return -1;
735 }
736
737 nlsr_lock();
738
akmhoque7b791452012-10-30 11:24:56 -0500739 if ( nlsr->debugging )
740 printf("build_and_install_adj_lsa called \n");
741 if ( nlsr->detailed_logging )
742 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_adj_lsa called \n");
743
744 if ( nlsr->debugging )
745 printf("adj_build_flag = %d \n",nlsr->adj_build_flag);
746 if ( nlsr->detailed_logging )
747 writeLogg(__FILE__,__FUNCTION__,__LINE__,"adj_build_flag = %d \n",nlsr->adj_build_flag);
akmhoque03004e62012-09-06 01:12:28 -0500748
749 if(nlsr->adj_build_flag > 0)
750 {
akmhoque7b791452012-10-30 11:24:56 -0500751 if ( nlsr->debugging )
752 printf("is_adj_lsa_build = %d \n",is_adj_lsa_build());
753 if ( nlsr->detailed_logging )
754 writeLogg(__FILE__,__FUNCTION__,__LINE__,"is_adj_lsa_build = %d \n",is_adj_lsa_build());
755
akmhoque03004e62012-09-06 01:12:28 -0500756 if ( is_adj_lsa_build()> 0)
757 {
758 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
759 build_adj_lsa(adj_lsa);
760 install_adj_lsa(adj_lsa);
761
akmhoqueb77b95f2013-02-08 12:28:47 -0600762 char lst[2];
763 memset(lst,0,2);
764 sprintf(lst,"%d",LS_TYPE_ADJ);
765
akmhoquef5537b42013-02-22 08:33:32 -0600766 char *repo_key=(char *)calloc(strlen(nlsr->slice_prefix)+strlen(adj_lsa->header->orig_time)+strlen(adj_lsa->header->orig_router->name) + strlen(lst) + 5+15,sizeof(char));
767 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);
akmhoqueb77b95f2013-02-08 12:28:47 -0600768
769 if ( nlsr->debugging )
770 printf("Adj LSA Repo Key: %s \n",repo_key);
771
akmhoquef5537b42013-02-22 08:33:32 -0600772 char *key=(char *)calloc(adj_lsa->header->orig_router->length+5,sizeof(char));
akmhoqueb77b95f2013-02-08 12:28:47 -0600773 make_adj_lsa_key(key,adj_lsa);
774 if ( nlsr->debugging )
775 printf("Adj LSA: %s \n",key);
776
akmhoquef5537b42013-02-22 08:33:32 -0600777 struct name_prefix *lsaid=(struct name_prefix *)calloc(1,sizeof(struct name_prefix));
778 lsaid->name=(char *)calloc(strlen(key)+1,sizeof(char));
779 memcpy(lsaid->name,key,strlen(key)+1);
akmhoqueb77b95f2013-02-08 12:28:47 -0600780 lsaid->length=strlen(key)+1;
781
782
783 write_adj_lsa_to_repo(repo_key, lsaid);
784
akmhoquef5537b42013-02-22 08:33:32 -0600785 free(key);
akmhoqueb77b95f2013-02-08 12:28:47 -0600786 free(repo_key);
787 free(lsaid->name);
788 free(lsaid);
789
akmhoquef5537b42013-02-22 08:33:32 -0600790 destroy_adj_lsa(adj_lsa);
akmhoqueb77b95f2013-02-08 12:28:47 -0600791
akmhoque03004e62012-09-06 01:12:28 -0500792 nlsr->adj_build_flag=0;
793 print_adj_lsdb();
794 }
795 else
796 {
akmhoque7b791452012-10-30 11:24:56 -0500797 if ( nlsr->debugging )
798 printf("Can not build adj LSA now\n");
799 if ( nlsr->detailed_logging )
800 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Can not build adj LSA now\n");
akmhoque03004e62012-09-06 01:12:28 -0500801 }
802 }
803 nlsr->is_build_adj_lsa_sheduled=0;
akmhoqueffacaa82012-09-13 17:48:30 -0500804
805 nlsr_unlock();
806
akmhoque03004e62012-09-06 01:12:28 -0500807 return 0;
808}
809
810
811void
812build_adj_lsa(struct alsa * adj_lsa)
813{
akmhoque7b791452012-10-30 11:24:56 -0500814 if ( nlsr->debugging )
815 printf("build_adj_lsa called \n");
816 if ( nlsr->detailed_logging )
817 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_adj_lsa called \n");
akmhoque03004e62012-09-06 01:12:28 -0500818
819 int no_link=no_active_nbr();
akmhoque7b791452012-10-30 11:24:56 -0500820
akmhoque03004e62012-09-06 01:12:28 -0500821 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
822 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
823 adj_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1);
824 memset(adj_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
825 memcpy(adj_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
826 adj_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
827
828 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
829
akmhoque03004e62012-09-06 01:12:28 -0500830
akmhoquef5537b42013-02-22 08:33:32 -0600831 char *time_stamp=get_current_timestamp_micro_v2();
akmhoque03004e62012-09-06 01:12:28 -0500832 adj_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1);
833 memset(adj_lsa->header->orig_time,0,strlen(time_stamp)+1);
834 memcpy(adj_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
835 free(time_stamp);
836
akmhoque03004e62012-09-06 01:12:28 -0500837 adj_lsa->no_link=no_link;
akmhoque03004e62012-09-06 01:12:28 -0500838
839
840 struct ccn_charbuf *c=ccn_charbuf_create();
841 get_active_nbr_adj_data(c);
842 char *data=ccn_charbuf_as_string(c);
843
akmhoquef5537b42013-02-22 08:33:32 -0600844 adj_lsa->body=(char *)calloc(strlen(data)+1,sizeof(char));
845 //memset(adj_lsa->body,0,strlen(data)+1);
akmhoque03004e62012-09-06 01:12:28 -0500846 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
akmhoquef5537b42013-02-22 08:33:32 -0600847 data[strlen(data)]='\0';
akmhoque03004e62012-09-06 01:12:28 -0500848 ccn_charbuf_destroy(&c);
849
akmhoque03004e62012-09-06 01:12:28 -0500850 nlsr->adj_build_count++;
851
852
853}
854
855
akmhoque53f64222012-09-05 13:57:51 -0500856void
857install_adj_lsa(struct alsa * adj_lsa)
858{
akmhoqued79438d2012-08-27 13:31:42 -0500859
akmhoque7b791452012-10-30 11:24:56 -0500860 if ( nlsr->debugging )
861 printf("install_adj_lsa called \n");
862 if ( nlsr->detailed_logging )
863 writeLogg(__FILE__,__FUNCTION__,__LINE__,"install_adj_lsa called \n");
864
akmhoque03004e62012-09-06 01:12:28 -0500865
akmhoque53f64222012-09-05 13:57:51 -0500866
akmhoqueffacaa82012-09-13 17:48:30 -0500867
akmhoquef5537b42013-02-22 08:33:32 -0600868 char *key=(char *)calloc(adj_lsa->header->orig_router->length+4,sizeof(char));
akmhoqueffacaa82012-09-13 17:48:30 -0500869 make_adj_lsa_key(key,adj_lsa);
akmhoqueffacaa82012-09-13 17:48:30 -0500870
akmhoque8876e982013-02-21 13:35:46 -0600871 struct alsa *new_adj_lsa;//=(struct alsa*)malloc(sizeof(struct alsa ));
akmhoqueffacaa82012-09-13 17:48:30 -0500872
873 struct hashtb_enumerator ee;
874 struct hashtb_enumerator *e = &ee;
875 int res;
876
877 hashtb_start(nlsr->lsdb->adj_lsdb, e);
878 res = hashtb_seek(e, key, strlen(key), 0);
akmhoque53f64222012-09-05 13:57:51 -0500879
akmhoque03004e62012-09-06 01:12:28 -0500880
akmhoque53f64222012-09-05 13:57:51 -0500881
akmhoque62c0c192012-09-24 07:49:25 -0500882 if(res == HT_NEW_ENTRY)
akmhoque53f64222012-09-05 13:57:51 -0500883 {
akmhoque62c0c192012-09-24 07:49:25 -0500884 if ( adj_lsa->no_link > 0)
885 {
akmhoque3171d652012-11-13 11:44:33 -0600886 if ( nlsr->debugging )
887 printf("New ADJ LSA... Adding to LSDB\n");
888 if ( nlsr->detailed_logging )
889 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New ADJ LSA... Adding to LSDB\n");
akmhoque62c0c192012-09-24 07:49:25 -0500890 new_adj_lsa = e->data;
akmhoque53f64222012-09-05 13:57:51 -0500891
akmhoquef5537b42013-02-22 08:33:32 -0600892 new_adj_lsa->header=(struct alsa_header *)calloc(1,sizeof(struct alsa_header ));
akmhoque62c0c192012-09-24 07:49:25 -0500893 new_adj_lsa->header->ls_type=adj_lsa->header->ls_type;
akmhoquef5537b42013-02-22 08:33:32 -0600894 new_adj_lsa->header->orig_time=(char *)calloc(strlen(adj_lsa->header->orig_time)+1,sizeof(char));
akmhoque62c0c192012-09-24 07:49:25 -0500895 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 -0500896
akmhoquef5537b42013-02-22 08:33:32 -0600897 new_adj_lsa->header->orig_router=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
898 new_adj_lsa->header->orig_router->name=(char *)calloc(adj_lsa->header->orig_router->length,sizeof(char));
akmhoque62c0c192012-09-24 07:49:25 -0500899 memcpy(new_adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
900 new_adj_lsa->header->orig_router->length=adj_lsa->header->orig_router->length;
akmhoque53f64222012-09-05 13:57:51 -0500901
akmhoque62c0c192012-09-24 07:49:25 -0500902 new_adj_lsa->no_link=adj_lsa->no_link;
akmhoqueffacaa82012-09-13 17:48:30 -0500903
akmhoquef5537b42013-02-22 08:33:32 -0600904 new_adj_lsa->body=(char *)calloc(strlen(adj_lsa->body)+1,sizeof(char));
905 //memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
akmhoque62c0c192012-09-24 07:49:25 -0500906 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
907
908 add_next_hop_router(new_adj_lsa->header->orig_router->name);
909 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
akmhoque9e9fc722012-09-26 14:03:25 -0500910
911 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
912 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
913 write_log_for_adj_lsa(new_adj_lsa);
914 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoque62c0c192012-09-24 07:49:25 -0500915 }
916 else
917 {
918 hashtb_delete(e);
919 }
akmhoque53f64222012-09-05 13:57:51 -0500920
akmhoque7b791452012-10-30 11:24:56 -0500921 if ( nlsr->debugging )
922 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
923 if ( nlsr->detailed_logging )
924 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
925
akmhoque53f64222012-09-05 13:57:51 -0500926 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500927
928 if ( nlsr->debugging )
929 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
930 if ( nlsr->detailed_logging )
931 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
932
akmhoqueffacaa82012-09-13 17:48:30 -0500933 }
934 else if(res == HT_OLD_ENTRY)
935 {
936 new_adj_lsa = e->data;
937 if(strcmp(adj_lsa->header->orig_time,new_adj_lsa->header->orig_time)<=0)
938 {
akmhoque7b791452012-10-30 11:24:56 -0500939 if ( nlsr->debugging )
940 printf("Older/Duplicate Adj LSA. Discarded...\n");
941 if ( nlsr->detailed_logging )
942 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Older/Duplicate Adj LSA. Discarded...\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500943 }
944 else
945 {
akmhoqueffacaa82012-09-13 17:48:30 -0500946
akmhoque62c0c192012-09-24 07:49:25 -0500947 if ( adj_lsa->no_link > 0)
948 {
akmhoque9e9fc722012-09-26 14:03:25 -0500949 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
950 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
951 write_log_for_adj_lsa(new_adj_lsa);
952 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500953
akmhoque62c0c192012-09-24 07:49:25 -0500954 free(new_adj_lsa->header->orig_time);
akmhoquef5537b42013-02-22 08:33:32 -0600955 new_adj_lsa->header->orig_time=(char *)calloc(strlen(adj_lsa->header->orig_time)+1,sizeof(char));
akmhoque62c0c192012-09-24 07:49:25 -0500956 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
957
958 new_adj_lsa->no_link=adj_lsa->no_link;
akmhoqueffacaa82012-09-13 17:48:30 -0500959
akmhoquef5537b42013-02-22 08:33:32 -0600960 new_adj_lsa->body=(char *)calloc(strlen(adj_lsa->body)+1,sizeof(char));
961 //memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
akmhoque62c0c192012-09-24 07:49:25 -0500962 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
akmhoqueffacaa82012-09-13 17:48:30 -0500963
akmhoque62c0c192012-09-24 07:49:25 -0500964 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
akmhoque9e9fc722012-09-26 14:03:25 -0500965
966 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
967 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
968 write_log_for_adj_lsa(new_adj_lsa);
969 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoque62c0c192012-09-24 07:49:25 -0500970 }
971 else
972 {
akmhoque9e9fc722012-09-26 14:03:25 -0500973 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
974 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
975 write_log_for_adj_lsa(new_adj_lsa);
976 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
977
akmhoque62c0c192012-09-24 07:49:25 -0500978 hashtb_delete(e);
979 }
akmhoque7b791452012-10-30 11:24:56 -0500980
981 if ( nlsr->debugging )
982 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
983 if ( nlsr->detailed_logging )
984 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
985
akmhoqueffacaa82012-09-13 17:48:30 -0500986 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500987
988 if ( nlsr->debugging )
989 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
990 if ( nlsr->detailed_logging )
991 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500992 }
993
994 }
995 hashtb_end(e);
996
997 if ( !nlsr->is_route_calculation_scheduled )
998 {
999 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
1000 nlsr->is_route_calculation_scheduled=1;
akmhoque53f64222012-09-05 13:57:51 -05001001 }
1002
akmhoque03004e62012-09-06 01:12:28 -05001003
akmhoquef5537b42013-02-22 08:33:32 -06001004 free(key);
akmhoqueffacaa82012-09-13 17:48:30 -05001005
akmhoquef5537b42013-02-22 08:33:32 -06001006 //free(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -05001007}
1008
1009void
akmhoque9e9fc722012-09-26 14:03:25 -05001010write_log_for_adj_lsa_body(const char *body, int no_link)
1011{
1012 int i=0;
akmhoquef5537b42013-02-22 08:33:32 -06001013 char *lsa_data=(char *)calloc(strlen(body)+1,sizeof(char));
1014 //memset( lsa_data,0,strlen(body)+1);
akmhoque9e9fc722012-09-26 14:03:25 -05001015 memcpy(lsa_data,body,strlen(body)+1);
1016 char *sep="|";
1017 char *rem;
1018 char *rtr_id;
1019 char *length;
akmhoqueb28579d2013-02-12 11:15:52 -06001020 //char *face;
akmhoque9e9fc722012-09-26 14:03:25 -05001021 char *metric;
1022
1023 if(no_link >0 )
1024 {
1025 rtr_id=strtok_r(lsa_data,sep,&rem);
1026 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -06001027 //face=strtok_r(NULL,sep,&rem);
akmhoque9e9fc722012-09-26 14:03:25 -05001028 metric=strtok_r(NULL,sep,&rem);
1029
1030 writeLogg(__FILE__,__FUNCTION__,__LINE__," Link %d \n",i+1);
akmhoque34b99f92012-09-27 12:24:27 -05001031 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router: %s \n",rtr_id);
1032 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router Length: %s \n",length);
akmhoque866c2222013-02-12 10:49:33 -06001033 //writeLogg(__FILE__,__FUNCTION__,__LINE__," Connecting Face: %s \n",face);
akmhoque34b99f92012-09-27 12:24:27 -05001034 writeLogg(__FILE__,__FUNCTION__,__LINE__," Metric: %s \n",metric);
akmhoque9e9fc722012-09-26 14:03:25 -05001035
1036
1037 for(i=1;i<no_link;i++)
1038 {
1039 rtr_id=strtok_r(NULL,sep,&rem);
1040 length=strtok_r(NULL,sep,&rem);
akmhoqueb28579d2013-02-12 11:15:52 -06001041 //face=strtok_r(NULL,sep,&rem);
akmhoque9e9fc722012-09-26 14:03:25 -05001042 metric=strtok_r(NULL,sep,&rem);
1043 writeLogg(__FILE__,__FUNCTION__,__LINE__," Link %d \n",i+1);
akmhoque34b99f92012-09-27 12:24:27 -05001044 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router: %s \n",rtr_id);
1045 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router Length: %s \n",length);
akmhoque866c2222013-02-12 10:49:33 -06001046 //writeLogg(__FILE__,__FUNCTION__,__LINE__," Connecting Face: %s \n",face);
akmhoque34b99f92012-09-27 12:24:27 -05001047 writeLogg(__FILE__,__FUNCTION__,__LINE__," Metric: %s \n",metric);
akmhoque9e9fc722012-09-26 14:03:25 -05001048
1049 }
1050 }
1051
akmhoquef5537b42013-02-22 08:33:32 -06001052 if(lsa_data)
1053 free(lsa_data);
akmhoque9e9fc722012-09-26 14:03:25 -05001054}
1055
1056
1057void
1058write_log_for_adj_lsa(struct alsa * adj_lsa)
1059{
1060 writeLogg(__FILE__,__FUNCTION__,__LINE__,"-----------Adj LSA Content---------------\n");
akmhoque34b99f92012-09-27 12:24:27 -05001061 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s\n",adj_lsa->header->orig_router->name);
1062 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
1063 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Type: %d\n",adj_lsa->header->ls_type);
1064 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Time: %s\n",adj_lsa->header->orig_time);
akmhoque9e9fc722012-09-26 14:03:25 -05001065 writeLogg(__FILE__,__FUNCTION__,__LINE__," Lsa Data:\n");
akmhoque34b99f92012-09-27 12:24:27 -05001066 writeLogg(__FILE__,__FUNCTION__,__LINE__," No of Link: %d\n",adj_lsa->no_link);
akmhoque9e9fc722012-09-26 14:03:25 -05001067
1068 write_log_for_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
1069
1070 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
1071
1072}
1073
1074void
akmhoque53f64222012-09-05 13:57:51 -05001075print_adj_lsa_body(const char *body, int no_link)
1076{
1077 int i=0;
akmhoquef5537b42013-02-22 08:33:32 -06001078 char *lsa_data=(char *)calloc(strlen(body)+1,sizeof(char));
1079 //memset( lsa_data,0,strlen(body)+1);
akmhoque53f64222012-09-05 13:57:51 -05001080 memcpy(lsa_data,body,strlen(body)+1);
1081 char *sep="|";
1082 char *rem;
1083 char *rtr_id;
1084 char *length;
akmhoqueb28579d2013-02-12 11:15:52 -06001085 //char *face;
akmhoque53f64222012-09-05 13:57:51 -05001086 char *metric;
1087
akmhoque53f64222012-09-05 13:57:51 -05001088 if(no_link >0 )
1089 {
1090 rtr_id=strtok_r(lsa_data,sep,&rem);
1091 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -06001092 //face=strtok_r(NULL,sep,&rem);
akmhoque53f64222012-09-05 13:57:51 -05001093 metric=strtok_r(NULL,sep,&rem);
1094
akmhoqueb77b95f2013-02-08 12:28:47 -06001095 if ( nlsr->debugging ) {
1096 printf(" Link %d \n",i+1);
1097 printf(" Neighbor : %s \n",rtr_id);
1098 printf(" Neighbor Length : %s \n",length);
akmhoque866c2222013-02-12 10:49:33 -06001099 //printf(" Connecting Face : %s \n",face);
akmhoqueb77b95f2013-02-08 12:28:47 -06001100 printf(" Metric : %s \n",metric);
1101 }
akmhoque53f64222012-09-05 13:57:51 -05001102
1103 for(i=1;i<no_link;i++)
1104 {
1105 rtr_id=strtok_r(NULL,sep,&rem);
1106 length=strtok_r(NULL,sep,&rem);
akmhoqueb28579d2013-02-12 11:15:52 -06001107 //face=strtok_r(NULL,sep,&rem);
akmhoque53f64222012-09-05 13:57:51 -05001108 metric=strtok_r(NULL,sep,&rem);
1109 printf(" Link %d \n",i+1);
1110 printf(" Neighbor : %s \n",rtr_id);
1111 printf(" Neighbor Length : %s \n",length);
akmhoque866c2222013-02-12 10:49:33 -06001112 //printf(" Connecting Face : %s \n",face);
akmhoque53f64222012-09-05 13:57:51 -05001113 printf(" Metric : %s \n",metric);
1114
1115 }
1116 }
1117
akmhoquef5537b42013-02-22 08:33:32 -06001118 if( lsa_data )
1119 free(lsa_data);
akmhoque53f64222012-09-05 13:57:51 -05001120}
1121
1122void
1123print_adj_lsa(struct alsa * adj_lsa)
1124{
akmhoque7b791452012-10-30 11:24:56 -05001125 if ( nlsr->debugging )
1126 {
1127 printf("-----------ADJ LSA Content---------------\n");
1128 printf(" Origination Router : %s\n",adj_lsa->header->orig_router->name);
1129 printf(" Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
1130 printf(" LS Type : %d\n",adj_lsa->header->ls_type);
1131 printf(" Origination Time : %s\n",adj_lsa->header->orig_time);
1132 printf(" Lsa Data:\n");
1133 printf(" No of Link : %d\n",adj_lsa->no_link);
akmhoque53f64222012-09-05 13:57:51 -05001134
akmhoque7b791452012-10-30 11:24:56 -05001135 print_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
1136 printf("\n");
1137 }
akmhoque53f64222012-09-05 13:57:51 -05001138
1139}
1140
1141void
1142print_adj_lsdb(void)
1143{
akmhoque7b791452012-10-30 11:24:56 -05001144 if ( nlsr->debugging )
1145 printf("print_name_lsdb called \n");
akmhoque53f64222012-09-05 13:57:51 -05001146 int i, adj_lsdb_element;
1147 struct alsa *adj_lsa;
1148
1149 struct hashtb_enumerator ee;
1150 struct hashtb_enumerator *e = &ee;
1151
1152 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1153 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1154
1155 for(i=0;i<adj_lsdb_element;i++)
1156 {
akmhoque7b791452012-10-30 11:24:56 -05001157 if ( nlsr->debugging )
1158 printf("-----------Adj LSA (%d)---------------\n",i+1);
akmhoque53f64222012-09-05 13:57:51 -05001159 adj_lsa=e->data;
1160 print_adj_lsa(adj_lsa);
1161 hashtb_next(e);
1162 }
1163
1164 hashtb_end(e);
1165
akmhoque3171d652012-11-13 11:44:33 -06001166 if ( nlsr->debugging )
1167 printf("\n");
1168 if ( nlsr->detailed_logging )
1169 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoque53f64222012-09-05 13:57:51 -05001170}
1171
1172void
akmhoque03004e62012-09-06 01:12:28 -05001173build_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 -05001174{
akmhoque7b791452012-10-30 11:24:56 -05001175 if ( nlsr->debugging )
1176 printf("build_and_install_others_adj_lsa called \n");
1177 if ( nlsr->detailed_logging )
1178 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_others_adj_lsa called \n");
akmhoque03004e62012-09-06 01:12:28 -05001179 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
1180 build_others_adj_lsa(adj_lsa,orig_router,ls_type,orig_time,no_link,data);
akmhoque03004e62012-09-06 01:12:28 -05001181 install_adj_lsa(adj_lsa);
1182
akmhoquef5537b42013-02-22 08:33:32 -06001183 destroy_adj_lsa(adj_lsa);
akmhoque53f64222012-09-05 13:57:51 -05001184
akmhoque03004e62012-09-06 01:12:28 -05001185 print_adj_lsdb();
akmhoque53f64222012-09-05 13:57:51 -05001186
akmhoque53f64222012-09-05 13:57:51 -05001187}
1188
akmhoque03004e62012-09-06 01:12:28 -05001189
akmhoque53f64222012-09-05 13:57:51 -05001190void
akmhoque03004e62012-09-06 01:12:28 -05001191build_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 -05001192{
akmhoque7b791452012-10-30 11:24:56 -05001193 if ( nlsr->debugging )
1194 printf("build_others_adj_lsa called \n");
1195 if ( nlsr->detailed_logging )
1196 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_others_adj_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -05001197
akmhoque03004e62012-09-06 01:12:28 -05001198 /*Filling Up Header Data */
akmhoquef5537b42013-02-22 08:33:32 -06001199 adj_lsa->header=(struct alsa_header *)calloc(1,sizeof(struct alsa_header ));
1200 adj_lsa->header->orig_router=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
1201 adj_lsa->header->orig_router->name=(char *)calloc(strlen(orig_router)+1,sizeof(char));
akmhoque03004e62012-09-06 01:12:28 -05001202 memcpy(adj_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
akmhoque53f64222012-09-05 13:57:51 -05001203
akmhoque03004e62012-09-06 01:12:28 -05001204 adj_lsa->header->orig_router->length=strlen(orig_router)+1;
akmhoque53f64222012-09-05 13:57:51 -05001205
akmhoque53f64222012-09-05 13:57:51 -05001206
akmhoque03004e62012-09-06 01:12:28 -05001207 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
akmhoque53f64222012-09-05 13:57:51 -05001208
akmhoquef5537b42013-02-22 08:33:32 -06001209 adj_lsa->header->orig_time=(char *)calloc(strlen(orig_time)+1,sizeof(char));
akmhoque03004e62012-09-06 01:12:28 -05001210 memcpy(adj_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
akmhoque53f64222012-09-05 13:57:51 -05001211
akmhoque03004e62012-09-06 01:12:28 -05001212 adj_lsa->no_link=no_link;
akmhoque53f64222012-09-05 13:57:51 -05001213
akmhoquef5537b42013-02-22 08:33:32 -06001214 adj_lsa->body=(char *)calloc(strlen(data)+1,sizeof(char));
akmhoque03004e62012-09-06 01:12:28 -05001215 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
akmhoque53f64222012-09-05 13:57:51 -05001216
akmhoque53f64222012-09-05 13:57:51 -05001217}
1218
akmhoque03004e62012-09-06 01:12:28 -05001219
akmhoque53f64222012-09-05 13:57:51 -05001220long int
1221get_name_lsdb_num_element(void)
1222{
1223 long int num_element;
1224
1225
1226 struct hashtb_enumerator ee;
1227 struct hashtb_enumerator *e = &ee;
1228
1229 hashtb_start(nlsr->lsdb->name_lsdb, e);
1230 num_element=hashtb_n(nlsr->lsdb->name_lsdb);
1231 hashtb_end(e);
1232
1233 return num_element;
1234}
1235
1236long int
1237get_adj_lsdb_num_element(void)
1238{
1239 long int num_element;
1240
1241
1242 struct hashtb_enumerator ee;
1243 struct hashtb_enumerator *e = &ee;
1244
1245 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1246 num_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1247 hashtb_end(e);
1248
1249 return num_element;
akmhoqued79438d2012-08-27 13:31:42 -05001250}
akmhoque03004e62012-09-06 01:12:28 -05001251
akmhoque03004e62012-09-06 01:12:28 -05001252
1253int
1254check_is_new_name_lsa(char *orig_router,char *lst,char *lsid,char *orig_time)
1255{
1256 int ret=0;
1257 struct ccn_charbuf *key=ccn_charbuf_create();
1258 ccn_charbuf_append_string(key,orig_router);
1259 ccn_charbuf_append_string(key,"/");
1260 ccn_charbuf_append_string(key,lst);
1261 ccn_charbuf_append_string(key,"/");
1262 ccn_charbuf_append_string(key,lsid);
1263
1264 int res;
1265 struct nlsa *name_lsa;
1266
1267 struct hashtb_enumerator ee;
1268 struct hashtb_enumerator *e = &ee;
1269
1270 hashtb_start(nlsr->lsdb->name_lsdb, e);
1271 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1272
1273 if( res == HT_NEW_ENTRY )
1274 {
1275 hashtb_delete(e);
1276 ret=1;
1277
1278 }
1279 else if(res == HT_OLD_ENTRY)
1280 {
1281 name_lsa=e->data;
1282 if( strcmp ( orig_time , name_lsa->header->orig_time ) > 0 )
1283 {
1284 ret=1;
1285 }
1286 }
1287
1288 hashtb_end(e);
1289
1290 ccn_charbuf_destroy(&key);
1291
1292 return ret;
1293}
1294
1295int
1296check_is_new_adj_lsa(char *orig_router,char *lst,char *orig_time)
1297{
1298 int ret=0;
1299 struct ccn_charbuf *key=ccn_charbuf_create();
1300 ccn_charbuf_append_string(key,orig_router);
1301 ccn_charbuf_append_string(key,"/");
1302 ccn_charbuf_append_string(key,lst);
1303
1304 int res;
1305 struct alsa *adj_lsa;
1306
1307 struct hashtb_enumerator ee;
1308 struct hashtb_enumerator *e = &ee;
1309
1310 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1311 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1312
1313 if( res == HT_NEW_ENTRY )
1314 {
1315 hashtb_delete(e);
1316 ret=1;
1317
1318 }
1319 else if(res == HT_OLD_ENTRY)
1320 {
1321 adj_lsa=e->data;
1322 if( strcmp ( orig_time , adj_lsa->header->orig_time ) > 0 )
1323 {
1324 ret=1;
1325 }
1326 }
1327
1328 hashtb_end(e);
1329
1330 ccn_charbuf_destroy(&key);
1331
1332 return ret;
1333}
1334
akmhoqueb77b95f2013-02-08 12:28:47 -06001335int
1336check_is_new_cor_lsa(char *orig_router,char *lst,char *orig_time)
1337{
1338 int ret=0;
1339 struct ccn_charbuf *key=ccn_charbuf_create();
1340 ccn_charbuf_append_string(key,orig_router);
1341 ccn_charbuf_append_string(key,"/");
1342 ccn_charbuf_append_string(key,lst);
1343
1344 int res;
1345 struct clsa *cor_lsa;
1346
1347 struct hashtb_enumerator ee;
1348 struct hashtb_enumerator *e = &ee;
1349
1350 hashtb_start(nlsr->lsdb->cor_lsdb, e);
1351 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1352
1353 if( res == HT_NEW_ENTRY )
1354 {
1355 hashtb_delete(e);
1356 ret=1;
1357
1358 }
1359 else if(res == HT_OLD_ENTRY)
1360 {
1361 cor_lsa=e->data;
1362 if( strcmp ( orig_time , cor_lsa->header->orig_time ) > 0 )
1363 {
1364 ret=1;
1365 }
1366 }
1367
1368 hashtb_end(e);
1369
1370 ccn_charbuf_destroy(&key);
1371
1372 return ret;
1373}
1374
akmhoque03004e62012-09-06 01:12:28 -05001375void
1376get_name_lsa_data(struct ccn_charbuf *lsa_data, struct name_prefix *lsaId)
1377{
akmhoque7b791452012-10-30 11:24:56 -05001378 if ( nlsr->debugging )
1379 printf("get_name_lsa_data called \n");
1380 if ( nlsr->detailed_logging )
1381 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_name_lsa_data called \n");
akmhoque03004e62012-09-06 01:12:28 -05001382
akmhoque8876e982013-02-21 13:35:46 -06001383 struct nlsa *name_lsa;//=(struct nlsa*)malloc(sizeof(struct nlsa ));
akmhoque03004e62012-09-06 01:12:28 -05001384
1385 struct hashtb_enumerator ee;
1386 struct hashtb_enumerator *e = &ee;
1387 int res;
1388
1389 hashtb_start(nlsr->lsdb->name_lsdb, e);
1390 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1391
1392 if( res == HT_OLD_ENTRY )
1393 {
1394 name_lsa=e->data;
akmhoqueb77b95f2013-02-08 12:28:47 -06001395
akmhoque7b791452012-10-30 11:24:56 -05001396 if ( nlsr->debugging )
1397 printf("NAME LSA found \n");
1398 if ( nlsr->detailed_logging )
1399 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Name LSA found \n");
akmhoque03004e62012-09-06 01:12:28 -05001400
1401 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_router->name);
1402 ccn_charbuf_append_string(lsa_data,"|");
1403
1404 char *temp_length=(char *)malloc(20);
1405 memset(temp_length,0,20);
1406 sprintf(temp_length,"%d",name_lsa->header->orig_router->length);
1407 ccn_charbuf_append_string(lsa_data,temp_length);
1408 free(temp_length);
1409 ccn_charbuf_append_string(lsa_data,"|");
1410
1411 char *temp_ltype=(char *)malloc(20);
1412 memset(temp_ltype,0,20);
1413 sprintf(temp_ltype,"%d",name_lsa->header->ls_type);
1414 ccn_charbuf_append_string(lsa_data,temp_ltype);
1415 free(temp_ltype);
1416 ccn_charbuf_append_string(lsa_data,"|");
1417
1418 char *temp_lsid=(char *)malloc(20);
1419 memset(temp_lsid,0,20);
1420 sprintf(temp_lsid,"%ld",name_lsa->header->ls_id);
1421 ccn_charbuf_append_string(lsa_data,temp_lsid);
1422 free(temp_lsid);
1423 ccn_charbuf_append_string(lsa_data,"|");
1424
1425 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_time);
1426 ccn_charbuf_append_string(lsa_data,"|");
1427
1428 char *temp_valid=(char *)malloc(20);
1429 memset(temp_valid,0,20);
1430 sprintf(temp_valid,"%d",name_lsa->header->isValid);
1431 ccn_charbuf_append_string(lsa_data,temp_valid);
1432 free(temp_valid);
1433 ccn_charbuf_append_string(lsa_data,"|");
1434
1435 ccn_charbuf_append_string(lsa_data,name_lsa->name_prefix->name);
1436 ccn_charbuf_append_string(lsa_data,"|");
1437
1438 char *temp_npl=(char *)malloc(20);
1439 memset(temp_npl,0,20);
1440 sprintf(temp_npl,"%d",name_lsa->name_prefix->length);
1441 ccn_charbuf_append_string(lsa_data,temp_npl);
1442 free(temp_npl);
1443 ccn_charbuf_append_string(lsa_data,"|");
1444
1445 }
1446 else if(res == HT_NEW_ENTRY)
1447 {
1448 hashtb_delete(e);
1449 }
1450
1451 hashtb_end(e);
1452}
1453
1454void
1455get_adj_lsa_data(struct ccn_charbuf *lsa_data,struct name_prefix *lsaId)
1456{
akmhoque7b791452012-10-30 11:24:56 -05001457 if ( nlsr->debugging )
1458 printf("get_adj_lsa_data called \n");
1459 if ( nlsr->detailed_logging )
1460 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_adj_lsa_data called \n");
akmhoque03004e62012-09-06 01:12:28 -05001461
1462 struct alsa *adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
1463
1464 struct hashtb_enumerator ee;
1465 struct hashtb_enumerator *e = &ee;
1466 int res;
1467
1468 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1469 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1470
1471 if( res == HT_OLD_ENTRY )
1472 {
1473 adj_lsa=e->data;
akmhoque7b791452012-10-30 11:24:56 -05001474
1475 if ( nlsr->debugging )
1476 printf("Adj LSA found \n");
1477 if ( nlsr->detailed_logging )
1478 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adj LSA found \n");
akmhoque03004e62012-09-06 01:12:28 -05001479
1480 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_router->name);
1481 ccn_charbuf_append_string(lsa_data,"|");
1482
1483 char *temp_length=(char *)malloc(20);
1484 memset(temp_length,0,20);
1485 sprintf(temp_length,"%d",adj_lsa->header->orig_router->length);
1486 ccn_charbuf_append_string(lsa_data,temp_length);
akmhoque03004e62012-09-06 01:12:28 -05001487 ccn_charbuf_append_string(lsa_data,"|");
akmhoqueb77b95f2013-02-08 12:28:47 -06001488 free(temp_length);
akmhoque03004e62012-09-06 01:12:28 -05001489
1490 char *temp_ltype=(char *)malloc(20);
1491 memset(temp_ltype,0,20);
1492 sprintf(temp_ltype,"%d",adj_lsa->header->ls_type);
1493 ccn_charbuf_append_string(lsa_data,temp_ltype);
akmhoque03004e62012-09-06 01:12:28 -05001494 ccn_charbuf_append_string(lsa_data,"|");
akmhoqueb77b95f2013-02-08 12:28:47 -06001495 free(temp_ltype);
akmhoque03004e62012-09-06 01:12:28 -05001496
1497 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_time);
1498 ccn_charbuf_append_string(lsa_data,"|");
1499
1500 char *temp_nl=(char *)malloc(20);
1501 memset(temp_nl,0,20);
1502 sprintf(temp_nl,"%d",adj_lsa->no_link);
1503 ccn_charbuf_append_string(lsa_data,temp_nl);
akmhoque03004e62012-09-06 01:12:28 -05001504 ccn_charbuf_append_string(lsa_data,"|");
akmhoqueb77b95f2013-02-08 12:28:47 -06001505 free(temp_nl);
akmhoque03004e62012-09-06 01:12:28 -05001506
1507 ccn_charbuf_append_string(lsa_data,adj_lsa->body);
1508
1509
1510 }
1511 else if(res == HT_NEW_ENTRY)
1512 {
1513 hashtb_delete(e);
1514 }
1515
1516 hashtb_end(e);
1517}
akmhoqueffacaa82012-09-13 17:48:30 -05001518
akmhoque3171d652012-11-13 11:44:33 -06001519void
1520make_name_lsa_invalid(struct name_prefix *np,int ls_type, long int ls_id)
1521{
1522
1523 if ( nlsr->debugging )
1524 printf("make_name_lsa_invalid called \n");
1525 if ( nlsr->detailed_logging )
1526 writeLogg(__FILE__,__FUNCTION__,__LINE__,"make_name_lsa_invalid called \n");
1527
1528
1529 char lst[2];
1530 memset(lst,0,2);
1531 sprintf(lst,"%d",ls_type);
1532
1533 char lsid[10];
1534 memset(lsid,0,10);
1535 sprintf(lsid,"%ld",ls_id);
1536
1537
1538 char *key=(char *)malloc(strlen(np->name)+1+strlen(lst)+1+strlen(lsid)+1);
1539 memset(key,0,strlen(np->name)+1+strlen(lst)+1+strlen(lsid)+1);
1540
1541
1542 make_name_lsa_key(key, np->name,ls_type,ls_id);
akmhoqueb77b95f2013-02-08 12:28:47 -06001543
1544 if ( nlsr->debugging )
1545 printf("Key:%s Length:%d\n",key,(int)strlen(key));
akmhoque3171d652012-11-13 11:44:33 -06001546
1547 struct nlsa *nlsa;
1548
1549 struct hashtb_enumerator ee;
1550 struct hashtb_enumerator *e = &ee;
1551
1552 int res;
1553
1554 hashtb_start(nlsr->lsdb->name_lsdb, e);
1555 res = hashtb_seek(e, key,strlen(key) , 0);
1556
1557 if( res == HT_OLD_ENTRY )
1558 {
1559 nlsa=e->data;
1560
1561 nlsa->header->isValid=0;
1562
1563 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1564 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1565 write_log_for_name_lsa(nlsa);
1566 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1567
1568 hashtb_delete(e);
1569 }
1570 else if( res == HT_NEW_ENTRY )
1571 {
1572 hashtb_delete(e);
1573 }
1574 hashtb_end(e);
1575
1576 if ( nlsr->debugging )
1577 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1578 if ( nlsr->detailed_logging )
1579 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1580
1581 set_new_lsdb_version();
1582
1583 if ( nlsr->debugging )
1584 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1585 if ( nlsr->detailed_logging )
1586 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1587
1588}
1589
akmhoquef5537b42013-02-22 08:33:32 -06001590
akmhoque866c2222013-02-12 10:49:33 -06001591int
1592delete_name_lsa(char *orig_router, char *name_prefix)
akmhoqueda5b6832012-09-13 22:33:55 -05001593{
akmhoqueb77b95f2013-02-08 12:28:47 -06001594
akmhoque7b791452012-10-30 11:24:56 -05001595 if ( nlsr->debugging )
1596 printf("delete_name_lsa called \n");
1597 if ( nlsr->detailed_logging )
1598 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_name_lsa called \n");
akmhoqueda5b6832012-09-13 22:33:55 -05001599
akmhoque866c2222013-02-12 10:49:33 -06001600 delete_npt_entry_by_router_and_name_prefix(orig_router, name_prefix);
akmhoquef5537b42013-02-22 08:33:32 -06001601
akmhoqueda5b6832012-09-13 22:33:55 -05001602
akmhoque7b791452012-10-30 11:24:56 -05001603 if ( nlsr->debugging )
1604 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1605 if ( nlsr->detailed_logging )
1606 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1607
akmhoqueda5b6832012-09-13 22:33:55 -05001608 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -05001609
1610 if ( nlsr->debugging )
1611 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1612 if ( nlsr->detailed_logging )
1613 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueda5b6832012-09-13 22:33:55 -05001614
akmhoqueda5b6832012-09-13 22:33:55 -05001615
1616 return 0;
1617}
1618
akmhoquef5537b42013-02-22 08:33:32 -06001619
akmhoque866c2222013-02-12 10:49:33 -06001620int delete_adj_lsa()
akmhoqueda5b6832012-09-13 22:33:55 -05001621{
akmhoque866c2222013-02-12 10:49:33 -06001622
akmhoque7b791452012-10-30 11:24:56 -05001623 if ( nlsr->debugging )
1624 printf("delete_adj_lsa called \n");
1625 if ( nlsr->detailed_logging )
1626 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_adj_lsa called \n");
akmhoque866c2222013-02-12 10:49:33 -06001627
akmhoque7b791452012-10-30 11:24:56 -05001628 if ( nlsr->debugging )
1629 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1630 if ( nlsr->detailed_logging )
1631 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1632
akmhoqueda5b6832012-09-13 22:33:55 -05001633 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -05001634
1635 if ( nlsr->debugging )
1636 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1637 if ( nlsr->detailed_logging )
1638 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1639
akmhoqueda5b6832012-09-13 22:33:55 -05001640
1641 if ( !nlsr->is_route_calculation_scheduled)
1642 {
1643 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
1644 nlsr->is_route_calculation_scheduled=1;
1645 }
1646
akmhoqueda5b6832012-09-13 22:33:55 -05001647 return 0;
1648}
1649
akmhoqueffacaa82012-09-13 17:48:30 -05001650void
1651refresh_name_lsdb(void)
1652{
akmhoque7b791452012-10-30 11:24:56 -05001653 if ( nlsr->debugging )
1654 printf("refresh_name_lsdb called \n");
1655 if ( nlsr->detailed_logging )
1656 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_name_lsdb called \n");
akmhoqueffacaa82012-09-13 17:48:30 -05001657
akmhoquef5537b42013-02-22 08:33:32 -06001658 char *time_stamp=get_current_timestamp_micro_v2();
akmhoqueffacaa82012-09-13 17:48:30 -05001659
1660 long int lsa_life_time;
1661
1662 int i, name_lsdb_element;
1663 struct nlsa *name_lsa;
1664
1665 struct hashtb_enumerator ee;
1666 struct hashtb_enumerator *e = &ee;
1667
1668 hashtb_start(nlsr->lsdb->name_lsdb, e);
1669 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
1670
1671 for(i=0;i<name_lsdb_element;i++)
1672 {
1673 name_lsa=e->data;
1674
1675 lsa_life_time=get_time_diff(time_stamp,name_lsa->header->orig_time);
akmhoque7b791452012-10-30 11:24:56 -05001676 if ( nlsr->debugging )
1677 printf("LSA Life Time: %ld \n",lsa_life_time);
1678 if ( nlsr->detailed_logging )
1679 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Life Time: %ld \n",lsa_life_time);
akmhoqueda5b6832012-09-13 22:33:55 -05001680
1681 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name) == 0)
1682 {
1683 if ( lsa_life_time > nlsr->lsa_refresh_time )
1684 {
akmhoque14b3f342012-09-14 10:39:02 -05001685 if ( name_lsa->header->isValid == NAME_LSA_VALID )
1686 {
akmhoque7b791452012-10-30 11:24:56 -05001687 if ( nlsr->debugging )
1688 printf("Own Name LSA need to be refrshed\n");
1689 if ( nlsr->detailed_logging )
1690 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Own Name LSA need to be refrshed\n");
akmhoquea98c2142012-10-25 15:22:24 -05001691
1692 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1693 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1694 write_log_for_name_lsa(name_lsa);
1695 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1696
akmhoquef5537b42013-02-22 08:33:32 -06001697 char *current_time_stamp=get_current_timestamp_micro_v2();
akmhoqueda5b6832012-09-13 22:33:55 -05001698
akmhoque14b3f342012-09-14 10:39:02 -05001699 free(name_lsa->header->orig_time);
akmhoquef5537b42013-02-22 08:33:32 -06001700 name_lsa->header->orig_time=(char *)calloc(strlen(current_time_stamp)+1,sizeof(char));
akmhoque14b3f342012-09-14 10:39:02 -05001701 memcpy(name_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
akmhoquea98c2142012-10-25 15:22:24 -05001702
1703 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1704 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
1705 write_log_for_name_lsa(name_lsa);
1706 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
akmhoqueda5b6832012-09-13 22:33:55 -05001707
akmhoque14b3f342012-09-14 10:39:02 -05001708 free(current_time_stamp);
akmhoque866c2222013-02-12 10:49:33 -06001709
1710 hashtb_next(e);
akmhoque14b3f342012-09-14 10:39:02 -05001711 }
1712 else
akmhoquef5537b42013-02-22 08:33:32 -06001713 {
akmhoque866c2222013-02-12 10:49:33 -06001714 delete_name_lsa(name_lsa->header->orig_router->name, name_lsa->name_prefix->name);
1715 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1716 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1717 write_log_for_name_lsa(name_lsa);
1718 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
akmhoquef5537b42013-02-22 08:33:32 -06001719
1720 destroy_name_lsa_component(name_lsa);
1721
akmhoque866c2222013-02-12 10:49:33 -06001722 hashtb_delete(e);
1723 i++;
akmhoque14b3f342012-09-14 10:39:02 -05001724 }
akmhoqueda5b6832012-09-13 22:33:55 -05001725
akmhoque7b791452012-10-30 11:24:56 -05001726 if ( nlsr->debugging )
1727 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1728 if ( nlsr->detailed_logging )
1729 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1730
akmhoqueda5b6832012-09-13 22:33:55 -05001731 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -05001732
1733 if ( nlsr->debugging )
1734 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1735 if ( nlsr->detailed_logging )
1736 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1737
1738
akmhoqueda5b6832012-09-13 22:33:55 -05001739
1740 print_name_lsdb();
akmhoque866c2222013-02-12 10:49:33 -06001741 }
1742 else
1743 {
1744 hashtb_next(e);
akmhoqueda5b6832012-09-13 22:33:55 -05001745 }
1746 }
1747 else
1748 {
1749 if ( lsa_life_time > nlsr->router_dead_interval )
1750 {
akmhoque7b791452012-10-30 11:24:56 -05001751 if ( nlsr->debugging )
1752 printf("Others Name LSA need to be deleted\n");
1753 if ( nlsr->detailed_logging )
1754 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Others Name LSA need to be deleted\n");
akmhoqueda5b6832012-09-13 22:33:55 -05001755
akmhoque866c2222013-02-12 10:49:33 -06001756 delete_name_lsa(name_lsa->header->orig_router->name, name_lsa->name_prefix->name);
1757 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1758 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1759 write_log_for_name_lsa(name_lsa);
1760 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
akmhoquef5537b42013-02-22 08:33:32 -06001761
1762 destroy_name_lsa_component(name_lsa);
akmhoque866c2222013-02-12 10:49:33 -06001763 hashtb_delete(e);
1764 i++;
1765 }
1766 else
1767 {
1768 hashtb_next(e);
akmhoqueda5b6832012-09-13 22:33:55 -05001769 }
1770 }
akmhoqueffacaa82012-09-13 17:48:30 -05001771 }
1772
1773 hashtb_end(e);
1774
1775 free(time_stamp);
akmhoqueda5b6832012-09-13 22:33:55 -05001776
akmhoqueffacaa82012-09-13 17:48:30 -05001777
1778}
1779
akmhoqueda5b6832012-09-13 22:33:55 -05001780void
akmhoqueffacaa82012-09-13 17:48:30 -05001781refresh_adj_lsdb(void)
1782{
akmhoqueffacaa82012-09-13 17:48:30 -05001783
akmhoque7b791452012-10-30 11:24:56 -05001784 if ( nlsr->debugging )
1785 printf("refresh_adj_lsdb called \n");
1786 if ( nlsr->detailed_logging )
1787 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_adj_lsdb called \n");
akmhoqueffacaa82012-09-13 17:48:30 -05001788
akmhoquef5537b42013-02-22 08:33:32 -06001789 char *time_stamp=get_current_timestamp_micro_v2();
1790
akmhoqueffacaa82012-09-13 17:48:30 -05001791 long int lsa_life_time;
1792
1793 int i, adj_lsdb_element;
1794 struct alsa *adj_lsa;
1795
1796 struct hashtb_enumerator ee;
1797 struct hashtb_enumerator *e = &ee;
1798
1799 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1800 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1801
1802 for(i=0;i<adj_lsdb_element;i++)
1803 {
1804 adj_lsa=e->data;
1805
akmhoqueb77b95f2013-02-08 12:28:47 -06001806 lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
akmhoque7b791452012-10-30 11:24:56 -05001807
1808 if ( nlsr->debugging )
1809 printf("LSA Life Time: %ld \n",lsa_life_time);
1810 if ( nlsr->detailed_logging )
1811 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Life Time: %ld \n",lsa_life_time);
akmhoqueffacaa82012-09-13 17:48:30 -05001812
akmhoqueda5b6832012-09-13 22:33:55 -05001813 if ( strcmp(adj_lsa->header->orig_router->name,nlsr->router_name) == 0)
1814 {
1815 if ( lsa_life_time > nlsr->lsa_refresh_time )
1816 {
akmhoque7b791452012-10-30 11:24:56 -05001817 if ( nlsr->debugging )
1818 printf("Own Adj LSA need to be refrshed\n");
1819 if ( nlsr->detailed_logging )
1820 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Own Adj LSA need to be refrshed\n");
akmhoqueda5b6832012-09-13 22:33:55 -05001821
akmhoquea98c2142012-10-25 15:22:24 -05001822 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1823 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
1824 write_log_for_adj_lsa(adj_lsa);
1825 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1826
akmhoquef5537b42013-02-22 08:33:32 -06001827 char *current_time_stamp=get_current_timestamp_micro_v2();
akmhoqueda5b6832012-09-13 22:33:55 -05001828
1829 free(adj_lsa->header->orig_time);
akmhoquef5537b42013-02-22 08:33:32 -06001830 adj_lsa->header->orig_time=(char *)calloc(strlen(current_time_stamp)+1,sizeof(char));
akmhoqueda5b6832012-09-13 22:33:55 -05001831 memcpy(adj_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1832
1833 free(current_time_stamp);
1834
akmhoquea98c2142012-10-25 15:22:24 -05001835 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1836 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
1837 write_log_for_adj_lsa(adj_lsa);
1838 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoqueda5b6832012-09-13 22:33:55 -05001839
akmhoque7b791452012-10-30 11:24:56 -05001840 if ( nlsr->debugging )
1841 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1842 if ( nlsr->detailed_logging )
1843 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1844
akmhoqueda5b6832012-09-13 22:33:55 -05001845 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -05001846
1847 if ( nlsr->debugging )
1848 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1849 if ( nlsr->detailed_logging )
1850 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueda5b6832012-09-13 22:33:55 -05001851
1852 print_adj_lsdb();
akmhoque866c2222013-02-12 10:49:33 -06001853
1854
akmhoqueda5b6832012-09-13 22:33:55 -05001855 }
akmhoque866c2222013-02-12 10:49:33 -06001856
1857 hashtb_next(e);
akmhoqueda5b6832012-09-13 22:33:55 -05001858 }
1859 else
1860 {
1861 if ( lsa_life_time > nlsr->router_dead_interval )
1862 {
akmhoque7b791452012-10-30 11:24:56 -05001863
1864 if ( nlsr->debugging )
1865 printf("Others Adj LSA need to be deleted\n");
1866 if ( nlsr->detailed_logging )
1867 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Others Adj LSA need to be deleted\n");
akmhoque866c2222013-02-12 10:49:33 -06001868 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1869 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
1870 write_log_for_adj_lsa(adj_lsa);
1871 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1872 delete_adj_lsa();
akmhoquef5537b42013-02-22 08:33:32 -06001873
1874 destroy_adj_lsa_component(adj_lsa);
akmhoque866c2222013-02-12 10:49:33 -06001875 hashtb_delete(e);
1876 i++;
1877
1878 }
1879 else
1880 {
1881 hashtb_next(e);
akmhoqueda5b6832012-09-13 22:33:55 -05001882 }
1883 }
akmhoquef5537b42013-02-22 08:33:32 -06001884
akmhoqueffacaa82012-09-13 17:48:30 -05001885 }
1886
1887 hashtb_end(e);
1888
1889 free(time_stamp);
akmhoqueffacaa82012-09-13 17:48:30 -05001890}
1891
akmhoqueb77b95f2013-02-08 12:28:47 -06001892
1893void
1894refresh_cor_lsdb(void)
1895{
1896
1897 if ( nlsr->debugging )
1898 printf("refresh_cor_lsdb called \n");
1899 if ( nlsr->detailed_logging )
1900 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_cor_lsdb called \n");
1901
akmhoquef5537b42013-02-22 08:33:32 -06001902 char *time_stamp=get_current_timestamp_micro_v2();
1903
akmhoqueb77b95f2013-02-08 12:28:47 -06001904 long int lsa_life_time;
1905
1906 int i, cor_lsdb_element;
1907 struct clsa *cor_lsa;
1908
1909 struct hashtb_enumerator ee;
1910 struct hashtb_enumerator *e = &ee;
1911
1912 hashtb_start(nlsr->lsdb->cor_lsdb, e);
1913 cor_lsdb_element=hashtb_n(nlsr->lsdb->cor_lsdb);
1914
1915 for(i=0;i<cor_lsdb_element;i++)
1916 {
1917 cor_lsa=e->data;
1918
1919 lsa_life_time=get_time_diff(time_stamp,cor_lsa->header->orig_time);
1920
1921 if ( nlsr->debugging )
1922 printf("LSA Life Time: %ld \n",lsa_life_time);
1923 if ( nlsr->detailed_logging )
1924 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Life Time: %ld \n",lsa_life_time);
1925
1926 if ( strcmp(cor_lsa->header->orig_router->name,nlsr->router_name) == 0)
1927 {
1928 if ( lsa_life_time > nlsr->lsa_refresh_time )
1929 {
1930 if ( nlsr->debugging )
1931 printf("Own Cor LSA need to be refrshed\n");
1932 if ( nlsr->detailed_logging )
1933 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Own Cor LSA need to be refrshed\n");
1934
akmhoquef5537b42013-02-22 08:33:32 -06001935 char *current_time_stamp=get_current_timestamp_micro_v2();
akmhoqueb77b95f2013-02-08 12:28:47 -06001936
1937 free(cor_lsa->header->orig_time);
1938 cor_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1939 memset(cor_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1940 memcpy(cor_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1941
1942 free(current_time_stamp);
1943
akmhoqueb77b95f2013-02-08 12:28:47 -06001944 if ( nlsr->debugging )
1945 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1946 if ( nlsr->detailed_logging )
1947 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1948
1949 set_new_lsdb_version();
1950
1951 if ( nlsr->debugging )
1952 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1953 if ( nlsr->detailed_logging )
1954 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1955
1956 print_adj_lsdb();
akmhoque866c2222013-02-12 10:49:33 -06001957 }
1958
1959 hashtb_next(e);
akmhoqueb77b95f2013-02-08 12:28:47 -06001960 }
1961 else
1962 {
1963 if ( lsa_life_time > nlsr->router_dead_interval )
1964 {
1965
1966 if ( nlsr->debugging )
1967 printf("Others Adj LSA need to be deleted\n");
1968 if ( nlsr->detailed_logging )
1969 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Others Adj LSA need to be deleted\n");
akmhoquef5537b42013-02-22 08:33:32 -06001970
1971 destroy_cor_lsa_component(cor_lsa);
akmhoqueb77b95f2013-02-08 12:28:47 -06001972 hashtb_delete(e);
1973 i++;
1974 }
akmhoque866c2222013-02-12 10:49:33 -06001975 else
1976 {
1977 hashtb_next(e);
1978 }
akmhoqueb77b95f2013-02-08 12:28:47 -06001979 }
1980
1981
akmhoque866c2222013-02-12 10:49:33 -06001982
akmhoqueb77b95f2013-02-08 12:28:47 -06001983 }
1984
1985 hashtb_end(e);
1986
1987 free(time_stamp);
1988}
1989
akmhoqueffacaa82012-09-13 17:48:30 -05001990int
1991refresh_lsdb(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1992{
1993 if(flags == CCN_SCHEDULE_CANCEL)
1994 {
1995 return -1;
1996 }
1997
1998 nlsr_lock();
akmhoqueda5b6832012-09-13 22:33:55 -05001999
akmhoque7b791452012-10-30 11:24:56 -05002000 if ( nlsr->debugging )
2001 printf("refresh_lsdb called\n");
2002 if ( nlsr->detailed_logging )
2003 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_lsdb called\n");
akmhoqueffacaa82012-09-13 17:48:30 -05002004
2005 refresh_name_lsdb();
2006 refresh_adj_lsdb();
2007
akmhoqueffacaa82012-09-13 17:48:30 -05002008 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoqueda5b6832012-09-13 22:33:55 -05002009
akmhoqueffacaa82012-09-13 17:48:30 -05002010 nlsr_unlock();
2011 return 0;
2012}
akmhoqueb77b95f2013-02-08 12:28:47 -06002013
2014void
2015write_adj_lsa_to_repo(char *repo_content_prefix, struct name_prefix *lsa_id)
2016{
2017 if ( nlsr->debugging )
2018 printf("write_adj_lsa_to_repo called\n");
2019 if ( nlsr->debugging )
2020 printf("Content Prefix: %s\n",repo_content_prefix);
2021
2022 struct ccn_charbuf *lsa_data=ccn_charbuf_create();
2023 get_adj_lsa_data(lsa_data,lsa_id);
2024 if ( nlsr->debugging )
akmhoque323b5e92013-02-21 13:55:15 -06002025 printf("Adj LSA Data: %s \n",ccn_charbuf_as_string(lsa_data));
akmhoqueb77b95f2013-02-08 12:28:47 -06002026
akmhoquef5537b42013-02-22 08:33:32 -06002027 char *data=calloc(strlen(ccn_charbuf_as_string(lsa_data))+1,sizeof(char));
2028 data=ccn_charbuf_as_string(lsa_data);
2029 data[strlen(data)]='\0';
2030
2031 write_data_to_repo(data, repo_content_prefix);
akmhoqueb77b95f2013-02-08 12:28:47 -06002032
akmhoque323b5e92013-02-21 13:55:15 -06002033
2034 ccn_charbuf_destroy(&lsa_data);
akmhoqueb77b95f2013-02-08 12:28:47 -06002035}
2036
2037void
2038write_name_lsa_to_repo(char *repo_content_prefix, struct name_prefix *lsa_id)
2039{
2040 if ( nlsr->debugging )
2041 printf("write_name_lsa_to_repo called\n");
2042 if ( nlsr->debugging )
2043 printf("Content Prefix: %s\n",repo_content_prefix);
2044
2045 struct ccn_charbuf *lsa_data=ccn_charbuf_create();
2046 get_name_lsa_data(lsa_data,lsa_id);
2047
2048 if ( nlsr->debugging )
2049 printf("Name LSA Data: %s \n",ccn_charbuf_as_string(lsa_data));
2050
akmhoquef5537b42013-02-22 08:33:32 -06002051 char *data=calloc(strlen(ccn_charbuf_as_string(lsa_data))+1,sizeof(char));
2052 data=ccn_charbuf_as_string(lsa_data);
2053 data[strlen(data)]='\0';
akmhoque323b5e92013-02-21 13:55:15 -06002054
akmhoquef5537b42013-02-22 08:33:32 -06002055 write_data_to_repo(data, repo_content_prefix);
akmhoqueb77b95f2013-02-08 12:28:47 -06002056
akmhoque4f85aab2013-02-21 13:58:50 -06002057 ccn_charbuf_destroy(&lsa_data);
akmhoqueb77b95f2013-02-08 12:28:47 -06002058}
2059
2060
2061void
2062write_name_lsdb_to_repo(char *slice_prefix)
2063{
2064 int i, name_lsdb_element;
2065
2066 struct nlsa *name_lsa;
2067 struct hashtb_enumerator ee;
2068 struct hashtb_enumerator *e = &ee;
2069
2070 hashtb_start(nlsr->lsdb->name_lsdb, e);
2071 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
2072
2073 for(i=0;i<name_lsdb_element;i++)
2074 {
2075 name_lsa=e->data;
2076
2077 char lst[2];
2078 memset(lst,0,2);
2079 sprintf(lst,"%d",name_lsa->header->ls_type);
2080
2081 char lsid[10];
2082 memset(lsid,0,10);
2083 sprintf(lsid,"%ld",name_lsa->header->ls_id);
2084
2085
2086 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
2087 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
2088
2089
2090 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
2091
2092 if ( nlsr->debugging )
2093 printf("Name LSA Key: %s \n",key);
2094
2095
akmhoquef5537b42013-02-22 08:33:32 -06002096 char *repo_key=(char *)calloc(strlen(slice_prefix)+1+strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1+strlen(name_lsa->header->orig_time)+16,sizeof(char));
akmhoqueb77b95f2013-02-08 12:28:47 -06002097 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);
2098
2099 if ( nlsr->debugging )
2100 printf("Name LSA Repo Key: %s \n",repo_key);
2101
akmhoquef5537b42013-02-22 08:33:32 -06002102 struct name_prefix *lsaid=(struct name_prefix *)calloc(1,sizeof(struct name_prefix));
2103 lsaid->name=(char *)calloc(strlen(key)+1,sizeof(char));
2104 memcpy(lsaid->name,key,strlen(key)+1);
akmhoqueb77b95f2013-02-08 12:28:47 -06002105 lsaid->length=strlen(key)+1;
2106
2107
2108 write_name_lsa_to_repo(repo_key, lsaid);
2109
2110 free(key);
2111 free(repo_key);
2112 free(lsaid->name);
2113 free(lsaid);
2114
2115 hashtb_next(e);
2116 }
2117
2118 hashtb_end(e);
2119
2120
2121}
2122
2123void
2124print_cor_lsa(struct clsa *cor_lsa)
2125{
2126 if ( nlsr->debugging )
2127 {
2128 printf("-----------Cor LSA Content---------------\n");
2129 printf(" Origination Router : %s\n",cor_lsa->header->orig_router->name);
2130 printf(" Origination Router Length: %d\n",cor_lsa->header->orig_router->length);
2131 printf(" LS Type : %d\n",cor_lsa->header->ls_type);
2132 printf(" Origination Time : %s\n",cor_lsa->header->orig_time);
2133 printf(" LSA Data \n");
2134 printf(" Cor R: : %f\n",cor_lsa->cor_r);
2135 printf(" Cor Theta : %f\n",cor_lsa->cor_theta);
2136
2137 printf("\n");
2138 }
2139}
2140
2141void
2142print_cor_lsdb()
2143{
2144
2145 if ( nlsr->debugging )
2146 printf("print_cor_lsdb called \n");
2147
2148 struct hashtb_enumerator ee;
2149 struct hashtb_enumerator *e = &ee;
2150
2151 int i=1;
2152
2153 for (hashtb_start(nlsr->lsdb->cor_lsdb, e); e->key != NULL; hashtb_next(e))
2154 {
2155 if ( nlsr->debugging )
2156 printf("-----------Cor LSA (%d)---------------\n",i);
2157 struct clsa *cor_lsa=e->data;
2158 print_cor_lsa(cor_lsa);
2159 i++;
2160 }
2161 hashtb_end(e);
2162
2163 if ( nlsr->debugging )
2164 printf("\n");
2165 if ( nlsr->detailed_logging )
2166 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
2167}
2168
2169void
2170install_cor_lsa(struct clsa *cor_lsa)
2171{
2172 if ( nlsr->debugging )
2173 printf("install_cor_lsa called \n");
2174 if ( nlsr->detailed_logging )
2175 writeLogg(__FILE__,__FUNCTION__,__LINE__,"install_cor_lsa called \n");
2176
2177
2178 char *time_stamp=(char *)malloc(20);
2179 memset(time_stamp,0,20);
2180 get_current_timestamp_micro(time_stamp);
2181
2182
akmhoque569a93d2013-02-21 10:19:54 -06002183 char *key=(char *)malloc(cor_lsa->header->orig_router->length+4);
2184 memset(key,0,cor_lsa->header->orig_router->length+4);
akmhoqueb77b95f2013-02-08 12:28:47 -06002185 make_cor_lsa_key(key,cor_lsa);
2186
2187 if ( nlsr->debugging )
2188 printf("Cor LSA key: %s \n",key);
2189
2190 struct hashtb_enumerator ee;
2191 struct hashtb_enumerator *e = &ee;
2192 int res;
2193
2194 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2195 res = hashtb_seek(e, key, strlen(key), 0);
2196
2197 if ( res == HT_NEW_ENTRY )
2198 {
2199 if ( nlsr->debugging )
2200 printf("New Cor LSA... \n");
2201
akmhoque8876e982013-02-21 13:35:46 -06002202 struct clsa *new_cor_lsa;//=(struct clsa *)malloc(sizeof( struct clsa ));
akmhoqueb77b95f2013-02-08 12:28:47 -06002203 new_cor_lsa=e->data;
2204 new_cor_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
2205
2206 new_cor_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
2207 new_cor_lsa->header->orig_router->name=(char *)malloc(strlen(cor_lsa->header->orig_router->name)+1);
2208 memset(new_cor_lsa->header->orig_router->name,0,strlen(cor_lsa->header->orig_router->name)+1);
2209 memcpy(new_cor_lsa->header->orig_router->name,cor_lsa->header->orig_router->name,strlen(cor_lsa->header->orig_router->name)+1);
2210 new_cor_lsa->header->orig_router->length=cor_lsa->header->orig_router->length;
2211
2212 new_cor_lsa->header->orig_time=(char *)malloc(strlen(cor_lsa->header->orig_time)+1); //free
2213 memset(new_cor_lsa->header->orig_time,0,strlen(cor_lsa->header->orig_time)+1);
2214 memcpy(new_cor_lsa->header->orig_time,cor_lsa->header->orig_time,strlen(cor_lsa->header->orig_time)+1);
2215
2216 new_cor_lsa->header->ls_type=cor_lsa->header->ls_type;
2217
2218 new_cor_lsa->cor_r=cor_lsa->cor_r;
2219 new_cor_lsa->cor_theta=cor_lsa->cor_theta;
2220 }
2221 else if ( res == HT_OLD_ENTRY )
2222 {
2223 if ( nlsr->debugging )
2224 printf("Cor LSA exists (Old)... \n");
2225 }
2226 hashtb_end(e);
2227
akmhoquef5537b42013-02-22 08:33:32 -06002228 free(key);
akmhoqueb77b95f2013-02-08 12:28:47 -06002229
2230}
2231
2232void
2233build_cor_lsa(struct clsa *cor_lsa, double cor_r, double cor_theta)
2234{
2235 cor_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
2236 cor_lsa->header->ls_type=LS_TYPE_COR;
2237
akmhoquef5537b42013-02-22 08:33:32 -06002238 char *time_stamp=get_current_timestamp_micro_v2();
akmhoqueb77b95f2013-02-08 12:28:47 -06002239 cor_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1); //free
2240 memset(cor_lsa->header->orig_time,0,strlen(time_stamp)+1);
2241 memcpy(cor_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
2242 free(time_stamp);
2243
akmhoquef5537b42013-02-22 08:33:32 -06002244 cor_lsa->header->orig_router=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
2245 cor_lsa->header->orig_router->name=(char *)calloc(strlen(nlsr->router_name)+1,sizeof(char));
akmhoqueb77b95f2013-02-08 12:28:47 -06002246 memset(cor_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
2247 memcpy(cor_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
2248 cor_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
2249
2250 cor_lsa->cor_r=cor_r;
2251 cor_lsa->cor_theta=cor_theta;
2252
2253}
2254
2255void
2256build_others_cor_lsa(struct clsa *cor_lsa,char *orig_router, int ls_type,char *orig_time, double cor_r, double cor_theta)
2257{
2258 cor_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
2259 cor_lsa->header->ls_type=ls_type;
2260
2261 cor_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
2262 memset(cor_lsa->header->orig_time,0,strlen(orig_time)+1);
akmhoque569a93d2013-02-21 10:19:54 -06002263 memcpy(cor_lsa->header->orig_time,orig_time,strlen(orig_time));
akmhoqueb77b95f2013-02-08 12:28:47 -06002264
2265 cor_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
2266 cor_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
2267 memset(cor_lsa->header->orig_router->name,0,strlen(orig_router)+1);
akmhoque569a93d2013-02-21 10:19:54 -06002268 memcpy(cor_lsa->header->orig_router->name,orig_router,strlen(orig_router));
akmhoqueb77b95f2013-02-08 12:28:47 -06002269 cor_lsa->header->orig_router->length=strlen(orig_router)+1;
2270
2271 cor_lsa->cor_r=cor_r;
2272 cor_lsa->cor_theta=cor_theta;
2273
2274}
2275
2276void
2277build_and_install_others_cor_lsa(char *orig_router,int ls_type,char *orig_time, double cor_r, double cor_theta)
2278{
2279 struct clsa *cor_lsa=(struct clsa *)malloc(sizeof( struct clsa ));
2280 build_others_cor_lsa(cor_lsa,orig_router,ls_type, orig_time, cor_r, cor_theta);
2281 install_cor_lsa(cor_lsa);
2282
2283 print_cor_lsdb();
akmhoque569a93d2013-02-21 10:19:54 -06002284
akmhoquef5537b42013-02-22 08:33:32 -06002285 destroy_cor_lsa(cor_lsa);
akmhoqueb77b95f2013-02-08 12:28:47 -06002286}
2287
2288
2289void
2290build_and_install_cor_lsa()
2291{
2292
2293
2294
2295 struct clsa *cor_lsa=(struct clsa *)malloc(sizeof( struct clsa ));
2296
2297 build_cor_lsa(cor_lsa,nlsr->cor_r,nlsr->cor_theta);
2298 install_cor_lsa(cor_lsa);
2299
2300 write_cor_lsa_to_repo(cor_lsa);
akmhoquef5537b42013-02-22 08:33:32 -06002301 destroy_cor_lsa(cor_lsa);
akmhoqueb77b95f2013-02-08 12:28:47 -06002302
2303 print_cor_lsdb();
akmhoqueb77b95f2013-02-08 12:28:47 -06002304
2305}
2306
2307void
2308get_cor_lsa_data(struct ccn_charbuf *lsa_data,char *cor_lsa_key)
2309{
2310 if ( nlsr->debugging )
2311 printf("get_cor_lsa_data called \n");
2312 if ( nlsr->detailed_logging )
2313 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_adj_lsa_data called \n");
2314
2315 struct clsa *cor_lsa=(struct clsa*)malloc(sizeof(struct clsa ));
2316
2317 struct hashtb_enumerator ee;
2318 struct hashtb_enumerator *e = &ee;
2319 int res;
2320
2321 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2322 res = hashtb_seek(e, cor_lsa_key, strlen(cor_lsa_key), 0);
2323
2324 if( res == HT_OLD_ENTRY )
2325 {
2326 cor_lsa=e->data;
2327
2328 if ( nlsr->debugging )
2329 printf("Cor LSA found \n");
2330 if ( nlsr->detailed_logging )
2331 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Cor LSA found \n");
2332
2333 ccn_charbuf_append_string(lsa_data,cor_lsa->header->orig_router->name);
2334 ccn_charbuf_append_string(lsa_data,"|");
2335
2336 char *temp_length=(char *)malloc(20);
2337 memset(temp_length,0,20);
2338 sprintf(temp_length,"%d",cor_lsa->header->orig_router->length);
2339 ccn_charbuf_append_string(lsa_data,temp_length);
2340 ccn_charbuf_append_string(lsa_data,"|");
2341 free(temp_length);
2342
2343 char *temp_ltype=(char *)malloc(20);
2344 memset(temp_ltype,0,20);
2345 sprintf(temp_ltype,"%d",cor_lsa->header->ls_type);
2346 ccn_charbuf_append_string(lsa_data,temp_ltype);
2347 ccn_charbuf_append_string(lsa_data,"|");
2348 free(temp_ltype);
2349
2350 ccn_charbuf_append_string(lsa_data,cor_lsa->header->orig_time);
2351 ccn_charbuf_append_string(lsa_data,"|");
2352
2353 char *cor_r=(char *)malloc(20);
2354 memset(cor_r,0,20);
2355 sprintf(cor_r,"%f",cor_lsa->cor_r);
2356 ccn_charbuf_append_string(lsa_data,cor_r);
2357 ccn_charbuf_append_string(lsa_data,"|");
2358 free(cor_r);
2359
2360 char *cor_theta=(char *)malloc(20);
2361 memset(cor_theta,0,20);
2362 sprintf(cor_theta,"%f",cor_lsa->cor_theta);
2363 ccn_charbuf_append_string(lsa_data,cor_theta);
2364 ccn_charbuf_append_string(lsa_data,"|");
2365 free(cor_theta);
2366
2367 }
2368 else if(res == HT_NEW_ENTRY)
2369 {
2370 hashtb_delete(e);
2371 }
2372
2373 hashtb_end(e);
2374}
2375
2376void
2377write_cor_lsa_to_repo(struct clsa *cor_lsa)
2378{
2379
2380
2381 if ( nlsr->debugging )
2382 printf("write_cor_lsa_to_repo called\n");
2383
2384
akmhoquef5537b42013-02-22 08:33:32 -06002385 char *key=(char *)calloc(cor_lsa->header->orig_router->length+4,sizeof(char));
akmhoqueb77b95f2013-02-08 12:28:47 -06002386 make_cor_lsa_key(key,cor_lsa);
2387
2388 struct ccn_charbuf *lsa_data=ccn_charbuf_create();
2389 get_cor_lsa_data(lsa_data,key);
akmhoque323b5e92013-02-21 13:55:15 -06002390
akmhoqueb77b95f2013-02-08 12:28:47 -06002391 if ( nlsr->debugging )
akmhoque0800eda2013-02-21 14:17:52 -06002392 printf("Cor LSA Data: %s \n",ccn_charbuf_as_string(lsa_data));
2393
akmhoqueb77b95f2013-02-08 12:28:47 -06002394 char *lst=(char *)malloc(20);
2395 memset(lst,0,20);
2396 sprintf(lst,"%d",cor_lsa->header->ls_type);
akmhoquef5537b42013-02-22 08:33:32 -06002397 char *repo_key=(char *)calloc(strlen(nlsr->slice_prefix)+strlen(cor_lsa->header->orig_time)+strlen(cor_lsa->header->orig_router->name) + strlen(lst) + 20,sizeof(char));
akmhoqueb77b95f2013-02-08 12:28:47 -06002398 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);
2399
2400 if ( nlsr->debugging )
2401 printf("Cor LSA Repo Key: %s \n",repo_key);
2402
akmhoquef5537b42013-02-22 08:33:32 -06002403 char *data=calloc(strlen(ccn_charbuf_as_string(lsa_data))+1,sizeof(char));
2404 data=ccn_charbuf_as_string(lsa_data);
2405 data[strlen(data)]='\0';
2406
2407 write_data_to_repo(data, repo_key);
akmhoque0800eda2013-02-21 14:17:52 -06002408
akmhoqueb77b95f2013-02-08 12:28:47 -06002409
2410
2411
2412 free(lst);
2413 free(key);
2414 free(repo_key);
akmhoque4f85aab2013-02-21 13:58:50 -06002415 ccn_charbuf_destroy(&lsa_data);
akmhoqueb77b95f2013-02-08 12:28:47 -06002416}
2417
2418void
2419make_cor_lsa_key_by_router_name(char *key,char *router_name)
2420{
2421 memcpy(key+strlen(key),router_name,strlen(router_name));
2422 memcpy(key+strlen(key),"/",1);
2423 char ls_type[2];
2424 sprintf(ls_type,"%d",LS_TYPE_COR);
2425 memcpy(key+strlen(key),ls_type,strlen(ls_type));
2426 key[strlen(key)]='\0';
2427}
2428
2429
2430double
2431get_hyperbolic_r(char *router)
2432{
2433 double ret=-1.0;
2434 char *cor_lsa_key=(char *)calloc(strlen(router)+4,sizeof(char));
2435 make_cor_lsa_key_by_router_name(cor_lsa_key,router);
2436
2437
2438 struct clsa *cor_lsa;
2439 struct hashtb_enumerator ee;
2440 struct hashtb_enumerator *e = &ee;
2441 int res;
2442
2443 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2444 res = hashtb_seek(e, cor_lsa_key, strlen(cor_lsa_key), 0);
2445
2446 if ( res == HT_OLD_ENTRY)
2447 {
2448 cor_lsa=e->data;
2449 ret=cor_lsa->cor_r;
2450 }
2451 else if(res == HT_NEW_ENTRY)
2452 {
2453 hashtb_delete(e);
2454 }
2455
2456 hashtb_end(e);
2457
2458 free(cor_lsa_key);
2459 return ret;
2460}
2461
2462double
2463get_hyperbolic_theta(char *router)
2464{
akmhoquef5537b42013-02-22 08:33:32 -06002465 double ret=-1.0;
akmhoqueb77b95f2013-02-08 12:28:47 -06002466 char *cor_lsa_key=(char *)calloc(strlen(router)+4,sizeof(char));
2467 make_cor_lsa_key_by_router_name(cor_lsa_key,router);
2468
2469 struct clsa *cor_lsa;
2470 struct hashtb_enumerator ee;
2471 struct hashtb_enumerator *e = &ee;
2472 int res;
2473
2474 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2475 res = hashtb_seek(e, cor_lsa_key, strlen(cor_lsa_key), 0);
2476
2477 if ( res == HT_OLD_ENTRY)
2478 {
2479 cor_lsa=e->data;
2480 ret=cor_lsa->cor_theta;
2481 }
2482 else if(res == HT_NEW_ENTRY)
2483 {
2484 hashtb_delete(e);
2485 }
2486
2487 hashtb_end(e);
2488
2489 free(cor_lsa_key);
2490 return ret;
2491}
akmhoquead584782013-02-22 10:56:03 -06002492
2493
2494void
2495destroy_name_lsdb(void)
2496{
2497 int i, name_lsdb_element;
2498 struct nlsa *name_lsa;
2499
2500 struct hashtb_enumerator ee;
2501 struct hashtb_enumerator *e = &ee;
2502
2503 hashtb_start(nlsr->lsdb->name_lsdb, e);
2504 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
2505
2506 for(i=0;i<name_lsdb_element;i++)
2507 {
2508 name_lsa=e->data;
2509 destroy_name_lsa_component(name_lsa);
2510 hashtb_next(e);
2511 }
2512
2513 hashtb_end(e);
2514
2515 if ( nlsr->lsdb->name_lsdb )
2516 hashtb_destroy(&nlsr->lsdb->name_lsdb);
2517}
2518
2519void
2520destroy_adj_lsdb(void)
2521{
2522 int i, adj_lsdb_element;
2523 struct alsa *adj_lsa;
2524
2525 struct hashtb_enumerator ee;
2526 struct hashtb_enumerator *e = &ee;
2527
2528 hashtb_start(nlsr->lsdb->adj_lsdb, e);
2529 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
2530
2531 for(i=0;i<adj_lsdb_element;i++)
2532 {
2533 adj_lsa=e->data;
2534 destroy_adj_lsa_component(adj_lsa);
2535 hashtb_next(e);
2536 }
2537
2538 hashtb_end(e);
2539 if(nlsr->lsdb->adj_lsdb )
2540 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
2541}
2542
2543void
2544destroy_cor_lsdb(void)
2545{
2546 int i, cor_lsdb_element;
2547 struct clsa *cor_lsa;
2548
2549 struct hashtb_enumerator ee;
2550 struct hashtb_enumerator *e = &ee;
2551
2552 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2553 cor_lsdb_element=hashtb_n(nlsr->lsdb->cor_lsdb);
2554
2555 for(i=0;i<cor_lsdb_element;i++)
2556 {
2557 cor_lsa=e->data;
2558 destroy_cor_lsa_component(cor_lsa);
2559 hashtb_next(e);
2560 }
2561
2562 hashtb_end(e);
2563 if(nlsr->lsdb->cor_lsdb )
2564 hashtb_destroy(&nlsr->lsdb->cor_lsdb);
2565}
2566
2567void
2568destroy_lsdb(void)
2569{
2570 destroy_name_lsdb();
2571 destroy_adj_lsdb();
2572 destroy_cor_lsdb();
2573
2574 if ( nlsr->lsdb->lsdb_version )
2575 free(nlsr->lsdb->lsdb_version);
2576 if ( nlsr->lsdb )
2577 free(nlsr->lsdb);
2578}