blob: e46f60d285c09d16c2c817e411645f69d3f71a9b [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
akmhoque54a14f42013-02-24 06:16:20 -060038 //char *time_stamp=get_current_timestamp_micro_v2();
akmhoque03004e62012-09-06 01:12:28 -050039 free(nlsr->lsdb->lsdb_version);
akmhoque54a14f42013-02-24 06:16:20 -060040 nlsr->lsdb->lsdb_version=get_current_timestamp_micro_v2();
41 //nlsr->lsdb->lsdb_version=(char *)calloc(strlen(time_stamp)+1,sizeof(char));
42 //memcpy(nlsr->lsdb->lsdb_version,time_stamp,strlen(time_stamp)+1);
43 //free(time_stamp);
akmhoquef71d9082012-08-22 12:51:53 -040044}
45
akmhoque0ab71642013-02-21 10:10:33 -060046/**
47* Make key for storing Name LSA in Name LSDB
48*/
49
akmhoque03004e62012-09-06 01:12:28 -050050void
51make_name_lsa_key(char *key, char *orig_router, int ls_type, long int ls_id)
52{
53
akmhoque03004e62012-09-06 01:12:28 -050054 char lst[2];
55 memset(lst,0,2);
56 sprintf(lst,"%d",ls_type);
57
58 char lsid[10];
59 memset(lsid,0,10);
60 sprintf(lsid,"%ld",ls_id);
61
akmhoqueeacf44f2013-02-22 01:02:13 -060062 memcpy(key,orig_router,strlen(orig_router));
akmhoque03004e62012-09-06 01:12:28 -050063 memcpy(key+strlen(key),"/",1);
64 memcpy(key+strlen(key),lst,strlen(lst));
65 memcpy(key+strlen(key),"/",1);
66 memcpy(key+strlen(key),lsid,strlen(lsid));
akmhoqueeacf44f2013-02-22 01:02:13 -060067 key[strlen(key)]='\0';
68
akmhoqueb77b95f2013-02-08 12:28:47 -060069 if ( nlsr->debugging )
70 printf("name LSA Key: %s\n", key);
71}
72
akmhoque0ed6d982013-02-22 14:28:01 -060073
akmhoque0ab71642013-02-21 10:10:33 -060074/**
75* Make content name prefix for Name LSA to store in repo
76*/
akmhoqueb77b95f2013-02-08 12:28:47 -060077
78void
akmhoque0ab71642013-02-21 10:10:33 -060079make_name_lsa_prefix_for_repo(char *key, char *orig_router, int ls_type,
80 long int ls_id,char *orig_time,char *slice_prefix)
akmhoqueb77b95f2013-02-08 12:28:47 -060081{
akmhoque0ab71642013-02-21 10:10:33 -060082 sprintf(key,"%s%s/lsType.%d/lsId.%ld/%s",slice_prefix, orig_router, ls_type,
83 ls_id, orig_time);
akmhoqueeacf44f2013-02-22 01:02:13 -060084 key[strlen(key)]='\0';
akmhoqueb77b95f2013-02-08 12:28:47 -060085 if ( nlsr->debugging )
86 printf("Name LSA prefix for repo content: %s\n",key);
87}
88
akmhoque0ab71642013-02-21 10:10:33 -060089/**
90* Make content name prefix for Adj LSA to store in repo
91*/
92
akmhoqueb77b95f2013-02-08 12:28:47 -060093void
akmhoque0ab71642013-02-21 10:10:33 -060094make_adj_lsa_prefix_for_repo(char *key, char *orig_router, int ls_type,
95 char *orig_time,char *slice_prefix)
akmhoqueb77b95f2013-02-08 12:28:47 -060096{
97
98 sprintf(key,"%s%s/lsType.%d/%s",slice_prefix,orig_router,ls_type, orig_time );
akmhoqueeacf44f2013-02-22 01:02:13 -060099 key[strlen(key)]='\0';
akmhoqueb77b95f2013-02-08 12:28:47 -0600100 if ( nlsr->debugging )
101 printf("Name LSA prefix for repo content:%s\n",key);
102}
103
akmhoque0ab71642013-02-21 10:10:33 -0600104/**
105* Make content name prefix for Cor LSA to store in repo
106*/
107
akmhoqueb77b95f2013-02-08 12:28:47 -0600108void
akmhoque0ab71642013-02-21 10:10:33 -0600109make_cor_lsa_prefix_for_repo(char *key, char *orig_router, int ls_type,
110 char *orig_time,char *slice_prefix)
akmhoqueb77b95f2013-02-08 12:28:47 -0600111{
112
113 sprintf(key,"%s%s/lsType.%d/%s",slice_prefix,orig_router,ls_type, orig_time );
akmhoqueeacf44f2013-02-22 01:02:13 -0600114 key[strlen(key)]='\0';
akmhoqueb77b95f2013-02-08 12:28:47 -0600115 if ( nlsr->debugging )
116 printf("Cor LSA prefix for repo content:%s\n",key);
akmhoque03004e62012-09-06 01:12:28 -0500117}
akmhoque53f64222012-09-05 13:57:51 -0500118
akmhoque0ab71642013-02-21 10:10:33 -0600119/**
120* Build name lsa for all name prefixes in Name Prefix List (NPL). Intsall Name
121* LSA in Name LSDB for router itself.
122*/
123
akmhoqueeacf44f2013-02-22 01:02:13 -0600124void
akmhoquef5537b42013-02-22 08:33:32 -0600125destroy_name_lsa_component(struct nlsa * name_lsa)
akmhoqueeacf44f2013-02-22 01:02:13 -0600126{
127 if ( name_lsa->header->orig_router->name )
128 free(name_lsa->header->orig_router->name);
129 if ( name_lsa->header->orig_router )
130 free(name_lsa->header->orig_router);
akmhoquef5537b42013-02-22 08:33:32 -0600131 if ( name_lsa->header->orig_time )
132 free(name_lsa->header->orig_time);
akmhoqueeacf44f2013-02-22 01:02:13 -0600133 if ( name_lsa->header )
134 free(name_lsa->header);
akmhoquef5537b42013-02-22 08:33:32 -0600135
akmhoqueeacf44f2013-02-22 01:02:13 -0600136 if ( name_lsa->name_prefix->name )
137 free(name_lsa->name_prefix->name);
138 if ( name_lsa->name_prefix )
139 free(name_lsa->name_prefix);
akmhoquef5537b42013-02-22 08:33:32 -0600140}
141
142void
143destroy_name_lsa(struct nlsa * name_lsa)
144{
145 destroy_name_lsa_component(name_lsa);
akmhoqueeacf44f2013-02-22 01:02:13 -0600146 if ( name_lsa )
147 free(name_lsa);
148}
akmhoque0ab71642013-02-21 10:10:33 -0600149
akmhoquef5537b42013-02-22 08:33:32 -0600150void
151destroy_adj_lsa_component(struct alsa * adj_lsa)
152{
153 if ( adj_lsa->header->orig_router->name )
154 free(adj_lsa->header->orig_router->name);
155 if ( adj_lsa->header->orig_router )
156 free(adj_lsa->header->orig_router);
157 if ( adj_lsa->header->orig_time )
158 free(adj_lsa->header->orig_time);
159 if ( adj_lsa->header )
160 free(adj_lsa->header);
161
162 if ( adj_lsa->body )
163 free(adj_lsa->body);
164}
165
166void
167destroy_adj_lsa(struct alsa * adj_lsa)
168{
169
170 destroy_adj_lsa_component(adj_lsa);
171 if ( adj_lsa )
172 free(adj_lsa);
173}
174
175void
176destroy_cor_lsa_component(struct clsa * cor_lsa)
177{
178 if ( cor_lsa->header->orig_router->name )
179 free(cor_lsa->header->orig_router->name);
180 if ( cor_lsa->header->orig_router )
181 free(cor_lsa->header->orig_router);
182 if ( cor_lsa->header->orig_time )
183 free(cor_lsa->header->orig_time);
184 if ( cor_lsa->header )
185 free(cor_lsa->header);
186}
187
188void
189destroy_cor_lsa(struct clsa * cor_lsa)
190{
191
192 destroy_cor_lsa_component(cor_lsa);
193 if ( cor_lsa )
194 free(cor_lsa);
195}
196
197
198
akmhoquef71d9082012-08-22 12:51:53 -0400199void
akmhoque53f64222012-09-05 13:57:51 -0500200build_and_install_name_lsas(void)
akmhoquef71d9082012-08-22 12:51:53 -0400201{
akmhoque7b791452012-10-30 11:24:56 -0500202 if ( nlsr->debugging )
203 printf("build_and_install_name_lsas called \n");
204 if ( nlsr->detailed_logging )
205 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_name_lsas called\n");
akmhoquef71d9082012-08-22 12:51:53 -0400206
akmhoquef71d9082012-08-22 12:51:53 -0400207 int i, npl_element;
akmhoque3171d652012-11-13 11:44:33 -0600208 struct name_prefix_list_entry *npe;
akmhoquef71d9082012-08-22 12:51:53 -0400209
210 struct hashtb_enumerator ee;
211 struct hashtb_enumerator *e = &ee;
212
213 hashtb_start(nlsr->npl, e);
214 npl_element=hashtb_n(nlsr->npl);
215
216 for(i=0;i<npl_element;i++)
217 {
akmhoque3171d652012-11-13 11:44:33 -0600218 npe=e->data;
akmhoque53f64222012-09-05 13:57:51 -0500219 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
akmhoque3171d652012-11-13 11:44:33 -0600220 build_name_lsa(name_lsa,npe->np);
akmhoque03004e62012-09-06 01:12:28 -0500221
akmhoquef71d9082012-08-22 12:51:53 -0400222 install_name_lsa(name_lsa);
akmhoque3171d652012-11-13 11:44:33 -0600223 update_nlsa_id_for_name_in_npl(npe->np,name_lsa->header->ls_id);
akmhoqueeacf44f2013-02-22 01:02:13 -0600224 destroy_name_lsa(name_lsa);
akmhoquef71d9082012-08-22 12:51:53 -0400225 hashtb_next(e);
226 }
227
akmhoque53f64222012-09-05 13:57:51 -0500228 hashtb_end(e);
akmhoque3171d652012-11-13 11:44:33 -0600229
230 print_name_prefix_from_npl();
231
232}
233
akmhoque0ab71642013-02-21 10:10:33 -0600234/**
235* Build and Install one Name LSA Use ful for API
236*/
237
akmhoque3171d652012-11-13 11:44:33 -0600238void
239build_and_install_single_name_lsa(struct name_prefix *np)
240{
241 if ( nlsr->debugging )
242 printf("build_and_install_single_name_lsa called \n");
243 if ( nlsr->detailed_logging )
244 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_single_name_lsa called\n");
245
246 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
247 build_name_lsa(name_lsa,np);
248
249 install_name_lsa(name_lsa);
250 update_nlsa_id_for_name_in_npl(np,name_lsa->header->ls_id);
akmhoqueeacf44f2013-02-22 01:02:13 -0600251 destroy_name_lsa(name_lsa);
akmhoque3171d652012-11-13 11:44:33 -0600252 print_name_prefix_from_npl();
akmhoquef71d9082012-08-22 12:51:53 -0400253
akmhoquef71d9082012-08-22 12:51:53 -0400254}
255
akmhoque53f64222012-09-05 13:57:51 -0500256void
257build_name_lsa(struct nlsa *name_lsa, struct name_prefix *np)
258{
akmhoque53f64222012-09-05 13:57:51 -0500259 name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header ));
260 name_lsa->header->ls_type=LS_TYPE_NAME;
akmhoqueeacf44f2013-02-22 01:02:13 -0600261
262 char *time_stamp=get_current_timestamp_micro_v2();
akmhoque03004e62012-09-06 01:12:28 -0500263 name_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1); //free
akmhoque53f64222012-09-05 13:57:51 -0500264 memset(name_lsa->header->orig_time,0,strlen(time_stamp)+1);
265 memcpy(name_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
akmhoque03004e62012-09-06 01:12:28 -0500266
267 free(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -0500268
269 name_lsa->header->ls_id=++nlsr->nlsa_id;
akmhoqueeacf44f2013-02-22 01:02:13 -0600270 name_lsa->header->orig_router=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
271 name_lsa->header->orig_router->name=(char *)calloc(strlen(nlsr->router_name)+1,sizeof(char));
akmhoque03004e62012-09-06 01:12:28 -0500272 memcpy(name_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
273 name_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
akmhoque53f64222012-09-05 13:57:51 -0500274 name_lsa->header->isValid=1;
275
276
277 name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoqueeacf44f2013-02-22 01:02:13 -0600278 name_lsa->name_prefix->name=(char *)calloc(np->length,sizeof(char));
akmhoque53f64222012-09-05 13:57:51 -0500279 memcpy(name_lsa->name_prefix->name,np->name,np->length);
280 name_lsa->name_prefix->length=np->length;
281
282}
283
akmhoque53f64222012-09-05 13:57:51 -0500284void
285install_name_lsa(struct nlsa *name_lsa)
286{
akmhoque53f64222012-09-05 13:57:51 -0500287
akmhoque0ab71642013-02-21 10:10:33 -0600288 char lst[2];
289 memset(lst,0,2);
290 sprintf(lst,"%d",name_lsa->header->ls_type);
akmhoqueffacaa82012-09-13 17:48:30 -0500291
akmhoque0ab71642013-02-21 10:10:33 -0600292 char lsid[10];
293 memset(lsid,0,10);
294 sprintf(lsid,"%ld",name_lsa->header->ls_id);
akmhoque3171d652012-11-13 11:44:33 -0600295
akmhoque0ab71642013-02-21 10:10:33 -0600296 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
297 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
298 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 -0500299
akmhoque0ab71642013-02-21 10:10:33 -0600300 if ( nlsr->debugging )
301 printf("Key:%s Length:%d\n",key,(int)strlen(key));
302 if ( nlsr->detailed_logging )
303 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Key:%s Length:%d\n",key,(int)strlen(key));
304
akmhoquef5537b42013-02-22 08:33:32 -0600305 struct nlsa *new_name_lsa;
akmhoque53f64222012-09-05 13:57:51 -0500306
akmhoque0ab71642013-02-21 10:10:33 -0600307 struct hashtb_enumerator ee;
308 struct hashtb_enumerator *e = &ee;
309 int res;
akmhoque53f64222012-09-05 13:57:51 -0500310
akmhoque0ab71642013-02-21 10:10:33 -0600311 hashtb_start(nlsr->lsdb->name_lsdb, e);
312 res = hashtb_seek(e, key, strlen(key), 0);
akmhoque53f64222012-09-05 13:57:51 -0500313
akmhoque0ab71642013-02-21 10:10:33 -0600314 if(res == HT_NEW_ENTRY )
akmhoque3560cb62012-09-09 10:52:30 -0500315 {
akmhoqueffacaa82012-09-13 17:48:30 -0500316
akmhoquef5537b42013-02-22 08:33:32 -0600317 if ( name_lsa->header->isValid == 1 )
318 {
akmhoque7b791452012-10-30 11:24:56 -0500319 if ( nlsr->debugging )
320 printf("New Name LSA... Adding to LSDB\n");
321 if ( nlsr->detailed_logging )
322 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Name LSA... Adding to LSDB\n");
323
324
akmhoqueffacaa82012-09-13 17:48:30 -0500325 new_name_lsa = e->data;
akmhoque8876e982013-02-21 13:35:46 -0600326
akmhoque54d86112013-02-21 16:42:34 -0600327
akmhoque0ab71642013-02-21 10:10:33 -0600328 new_name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header ));
akmhoqueffacaa82012-09-13 17:48:30 -0500329 new_name_lsa->header->ls_type=name_lsa->header->ls_type;
330
331 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
332 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
akmhoque8876e982013-02-21 13:35:46 -0600333 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 -0500334
335 new_name_lsa->header->ls_id=name_lsa->header->ls_id;
akmhoque0ab71642013-02-21 10:10:33 -0600336 new_name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoqueffacaa82012-09-13 17:48:30 -0500337 new_name_lsa->header->orig_router->name=(char *)malloc(name_lsa->header->orig_router->length);
338 memcpy(new_name_lsa->header->orig_router->name,name_lsa->header->orig_router->name,name_lsa->header->orig_router->length);
339 new_name_lsa->header->orig_router->length=name_lsa->header->orig_router->length;
340 new_name_lsa->header->isValid=name_lsa->header->isValid;
341
342
akmhoque0ab71642013-02-21 10:10:33 -0600343 new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoqueffacaa82012-09-13 17:48:30 -0500344 new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
345 memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
346 new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
akmhoque8876e982013-02-21 13:35:46 -0600347
akmhoqueffacaa82012-09-13 17:48:30 -0500348
akmhoque7b791452012-10-30 11:24:56 -0500349 if ( nlsr->debugging )
350 {
351 printf("New Name LSA Added....\n");
352 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
353 }
354 if ( nlsr->detailed_logging )
355 {
356 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Name LSA Added....\n");
357 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
358 }
akmhoque54d86112013-02-21 16:42:34 -0600359
akmhoqueffacaa82012-09-13 17:48:30 -0500360 set_new_lsdb_version();
akmhoque54d86112013-02-21 16:42:34 -0600361
akmhoque7b791452012-10-30 11:24:56 -0500362 if ( nlsr->debugging )
363 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
364 if ( nlsr->detailed_logging )
365 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500366
akmhoque9e9fc722012-09-26 14:03:25 -0500367
akmhoquede61ba92012-09-20 22:19:12 -0500368 int num_next_hop=get_number_of_next_hop(new_name_lsa->header->orig_router->name);
akmhoquede61ba92012-09-20 22:19:12 -0500369 if ( num_next_hop < 0 )
akmhoque3560cb62012-09-09 10:52:30 -0500370 {
akmhoquede61ba92012-09-20 22:19:12 -0500371 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 -0500372 if ( check == HT_NEW_ENTRY )
373 {
akmhoque7b791452012-10-30 11:24:56 -0500374 if ( nlsr->debugging )
375 printf("Added in npt \n");
376 if ( nlsr->detailed_logging )
377 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
akmhoqueffacaa82012-09-13 17:48:30 -0500378 }
akmhoque3560cb62012-09-09 10:52:30 -0500379 }
akmhoqueffacaa82012-09-13 17:48:30 -0500380 else
akmhoque3560cb62012-09-09 10:52:30 -0500381 {
akmhoquede61ba92012-09-20 22:19:12 -0500382 int *faces=malloc(num_next_hop*sizeof(int));
383 int *route_costs=malloc(num_next_hop*sizeof(int));
384 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name,faces,route_costs);
akmhoque7b791452012-10-30 11:24:56 -0500385 if ( nlsr->debugging )
386 {
387 printf("Printing from install_name_lsa \n");
388 int j;
389 for(j=0;j<num_next_hop;j++)
390 printf("Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
391 }
392 if ( nlsr->detailed_logging )
393 {
394 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Printing from install_name_lsa \n");
395 int j;
396 for(j=0;j<num_next_hop;j++)
397 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
398 }
akmhoquede61ba92012-09-20 22:19:12 -0500399 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 -0500400 if ( check == HT_NEW_ENTRY )
401 {
akmhoque7b791452012-10-30 11:24:56 -0500402 if ( nlsr->debugging )
403 printf("Added in npt \n");
404 if ( nlsr->detailed_logging )
405 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
akmhoqueffacaa82012-09-13 17:48:30 -0500406 }
akmhoquede61ba92012-09-20 22:19:12 -0500407 free(faces);
408 free(route_costs);
akmhoqueffacaa82012-09-13 17:48:30 -0500409
akmhoque3560cb62012-09-09 10:52:30 -0500410 }
akmhoquede61ba92012-09-20 22:19:12 -0500411
akmhoque9e9fc722012-09-26 14:03:25 -0500412 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
413 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
akmhoquef5537b42013-02-22 08:33:32 -0600414 write_log_for_name_lsa(new_name_lsa);
akmhoque9e9fc722012-09-26 14:03:25 -0500415 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500416
akmhoque54d86112013-02-21 16:42:34 -0600417 //free(time_stamp);
akmhoquef5537b42013-02-22 08:33:32 -0600418 }
419 else
420 {
421 hashtb_delete(e);
422 }
akmhoqueffacaa82012-09-13 17:48:30 -0500423
424 }
425 else if(res == HT_OLD_ENTRY)
426 {
427 new_name_lsa=e->data;
akmhoque14b3f342012-09-14 10:39:02 -0500428 if(strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time)<0)
akmhoqueffacaa82012-09-13 17:48:30 -0500429 {
akmhoque7b791452012-10-30 11:24:56 -0500430 if ( nlsr->debugging )
431 printf("Older Adj LSA. Discarded... \n");
432 if ( nlsr->detailed_logging )
433 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Older Adj LSA. Discarded...\n");
akmhoque14b3f342012-09-14 10:39:02 -0500434 }
435 else if( strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time) == 0 )
436 {
akmhoque7b791452012-10-30 11:24:56 -0500437 if ( nlsr->debugging )
438 printf("Duplicate Adj LSA. Discarded... \n");
439 if ( nlsr->detailed_logging )
440 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Duplicate Adj LSA. Discarded...\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500441 }
442 else
443 {
444 if ( name_lsa->header->isValid == 0 )
445 {
446 // have to call to delete npt table entry
akmhoque3cced642012-09-24 16:20:20 -0500447 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 -0500448
449 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name)!= 0)
450 {
akmhoque9e9fc722012-09-26 14:03:25 -0500451 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
452 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
453 write_log_for_name_lsa(new_name_lsa);
454 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
455
akmhoqueffacaa82012-09-13 17:48:30 -0500456 hashtb_delete(e);
akmhoque7b791452012-10-30 11:24:56 -0500457 if ( nlsr->debugging )
458 printf("isValid bit not set for Router %s so LSA Deleted from LSDB\n",name_lsa->header->orig_router->name);
459 if ( nlsr->detailed_logging )
460 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 -0500461 }
462 else
463 {
464 new_name_lsa->header->isValid=name_lsa->header->isValid;
465 free(new_name_lsa->header->orig_time);
466 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
467 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
468 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
469 }
akmhoque7b791452012-10-30 11:24:56 -0500470 if ( nlsr->debugging )
471 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
472 if ( nlsr->detailed_logging )
473 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500474 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500475 if ( nlsr->debugging )
476 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
477 if ( nlsr->detailed_logging )
478 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500479 }
480 else
481 {
482 int is_npt_update=0;
483 if ( strcmp(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name) != 0 )
484 {
485 is_npt_update=1;
akmhoque3cced642012-09-24 16:20:20 -0500486 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 -0500487 }
488
489 // copying LSA content with header
akmhoque9e9fc722012-09-26 14:03:25 -0500490 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
491 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
492 write_log_for_name_lsa(new_name_lsa);
493 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
494
akmhoqueffacaa82012-09-13 17:48:30 -0500495
496 free(new_name_lsa->header->orig_time);
497 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
498 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
akmhoque0ab71642013-02-21 10:10:33 -0600499 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time));
akmhoqueffacaa82012-09-13 17:48:30 -0500500
501 new_name_lsa->header->isValid=name_lsa->header->isValid;
502
503 free(new_name_lsa->name_prefix->name);
504 free(new_name_lsa->name_prefix);
505 new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
506 new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
507 memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
508 new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
509
akmhoque9e9fc722012-09-26 14:03:25 -0500510
511 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
512 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
513 write_log_for_name_lsa(new_name_lsa);
514 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
515
akmhoque7b791452012-10-30 11:24:56 -0500516 if ( nlsr->debugging )
517 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
518 if ( nlsr->detailed_logging )
519 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
520
akmhoqueffacaa82012-09-13 17:48:30 -0500521 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500522
523 if ( nlsr->debugging )
524 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
525 if ( nlsr->detailed_logging )
526 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
527
akmhoqueffacaa82012-09-13 17:48:30 -0500528
529 if( is_npt_update == 1 )
530 {
akmhoquede61ba92012-09-20 22:19:12 -0500531 //struct hashtb *face_list;
532 int num_next_hop=get_number_of_next_hop(new_name_lsa->header->orig_router->name);
533 if ( num_next_hop < 0 )
akmhoqueffacaa82012-09-13 17:48:30 -0500534 {
akmhoquede61ba92012-09-20 22:19:12 -0500535
536 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 -0500537 if ( check == HT_NEW_ENTRY )
538 {
akmhoque7b791452012-10-30 11:24:56 -0500539 if ( nlsr->debugging )
540 printf("Added in npt \n");
541 if ( nlsr->detailed_logging )
542 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
akmhoqueffacaa82012-09-13 17:48:30 -0500543 }
544 }
545 else
546 {
akmhoquede61ba92012-09-20 22:19:12 -0500547 int *faces=malloc(num_next_hop*sizeof(int));
548 int *route_costs=malloc(num_next_hop*sizeof(int));
549 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name,faces,route_costs);
akmhoque7b791452012-10-30 11:24:56 -0500550
551 if ( nlsr->debugging )
552 {
553 printf("Printing from install_name_lsa \n");
554 int j;
555 for(j=0;j<num_next_hop;j++)
akmhoquede61ba92012-09-20 22:19:12 -0500556 printf("Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
akmhoque7b791452012-10-30 11:24:56 -0500557 }
558 if ( nlsr->detailed_logging )
559 {
560 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Printing from install_name_lsa \n");
561 int j;
562 for(j=0;j<num_next_hop;j++)
563 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
564 }
565
akmhoquede61ba92012-09-20 22:19:12 -0500566
567 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 -0500568 if ( check == HT_NEW_ENTRY )
569 {
akmhoque7b791452012-10-30 11:24:56 -0500570 if ( nlsr->debugging )
571 printf("Added in npt \n");
572 if ( nlsr->detailed_logging )
573 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
akmhoqueffacaa82012-09-13 17:48:30 -0500574 }
akmhoquede61ba92012-09-20 22:19:12 -0500575 free(faces);
576 free(route_costs);
577
akmhoqueffacaa82012-09-13 17:48:30 -0500578 }
akmhoquede61ba92012-09-20 22:19:12 -0500579
akmhoqueffacaa82012-09-13 17:48:30 -0500580 }
581 }
582 }
583
akmhoque3560cb62012-09-09 10:52:30 -0500584 }
akmhoque53f64222012-09-05 13:57:51 -0500585
akmhoqueffacaa82012-09-13 17:48:30 -0500586 hashtb_end(e);
akmhoque53f64222012-09-05 13:57:51 -0500587
akmhoquef5537b42013-02-22 08:33:32 -0600588 free(key);
akmhoque53f64222012-09-05 13:57:51 -0500589}
590
akmhoque9e9fc722012-09-26 14:03:25 -0500591void
592write_log_for_name_lsa(struct nlsa *name_lsa)
593{
594
595 writeLogg(__FILE__,__FUNCTION__,__LINE__,"-----------Name LSA Content---------------\n");
596 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s\n",name_lsa->header->orig_router->name);
597 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router Length: %d\n",name_lsa->header->orig_router->length);
akmhoque34b99f92012-09-27 12:24:27 -0500598 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Type: %d\n",name_lsa->header->ls_type);
599 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Id: %ld\n",name_lsa->header->ls_id);
600 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Time: %s\n",name_lsa->header->orig_time);
601 writeLogg(__FILE__,__FUNCTION__,__LINE__," Is Valid: %d\n",name_lsa->header->isValid);
akmhoque9e9fc722012-09-26 14:03:25 -0500602 writeLogg(__FILE__,__FUNCTION__,__LINE__," LSA Data \n");
603 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix: %s\n",name_lsa->name_prefix->name);
604 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix Length: %d\n",name_lsa->name_prefix->length);
605 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
606}
akmhoque53f64222012-09-05 13:57:51 -0500607
akmhoque03004e62012-09-06 01:12:28 -0500608void
609print_name_lsa(struct nlsa *name_lsa)
akmhoque53f64222012-09-05 13:57:51 -0500610{
akmhoque53f64222012-09-05 13:57:51 -0500611
akmhoque7b791452012-10-30 11:24:56 -0500612 if ( nlsr->debugging )
613 {
614 printf("-----------Name LSA Content---------------\n");
615 printf(" Origination Router : %s\n",name_lsa->header->orig_router->name);
616 printf(" Origination Router Length: %d\n",name_lsa->header->orig_router->length);
617 printf(" LS Type : %d\n",name_lsa->header->ls_type);
618 printf(" LS Id : %ld\n",name_lsa->header->ls_id);
619 printf(" Origination Time : %s\n",name_lsa->header->orig_time);
620 printf(" Is Valid : %d\n",name_lsa->header->isValid);
621 printf(" LSA Data \n");
622 printf(" Name Prefix: : %s\n",name_lsa->name_prefix->name);
623 printf(" Name Prefix Length : %d\n",name_lsa->name_prefix->length);
624
625 printf("\n");
626 }
akmhoquef71d9082012-08-22 12:51:53 -0400627}
628
629void
630print_name_lsdb(void)
631{
akmhoque7b791452012-10-30 11:24:56 -0500632 if ( nlsr->debugging )
633 printf("print_name_lsdb called \n");
akmhoque53f64222012-09-05 13:57:51 -0500634 int i, name_lsdb_element;
akmhoquef71d9082012-08-22 12:51:53 -0400635 struct nlsa *name_lsa;
636
637 struct hashtb_enumerator ee;
638 struct hashtb_enumerator *e = &ee;
639
640 hashtb_start(nlsr->lsdb->name_lsdb, e);
akmhoque53f64222012-09-05 13:57:51 -0500641 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
akmhoquef71d9082012-08-22 12:51:53 -0400642
akmhoque53f64222012-09-05 13:57:51 -0500643 for(i=0;i<name_lsdb_element;i++)
akmhoquef71d9082012-08-22 12:51:53 -0400644 {
akmhoque7b791452012-10-30 11:24:56 -0500645 if ( nlsr->debugging )
646 printf("-----------Name LSA (%d)---------------\n",i+1);
akmhoquef71d9082012-08-22 12:51:53 -0400647 name_lsa=e->data;
akmhoque53f64222012-09-05 13:57:51 -0500648 print_name_lsa(name_lsa);
akmhoquef71d9082012-08-22 12:51:53 -0400649 hashtb_next(e);
650 }
651
652 hashtb_end(e);
653
akmhoque3171d652012-11-13 11:44:33 -0600654 if ( nlsr->debugging )
655 printf("\n");
656 if ( nlsr->detailed_logging )
657 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoque53f64222012-09-05 13:57:51 -0500658}
659
akmhoque03004e62012-09-06 01:12:28 -0500660
661void
662build_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 -0500663{
akmhoque7b791452012-10-30 11:24:56 -0500664 if ( nlsr->debugging )
665 printf("build_and_install_others_name_lsa called \n");
666 if ( nlsr->detailed_logging )
667 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_others_name_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500668
akmhoque03004e62012-09-06 01:12:28 -0500669 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
670 build_others_name_lsa(name_lsa,orig_router,ls_type,ls_id,orig_time, isValid,np);
671 print_name_lsa(name_lsa);
672 install_name_lsa(name_lsa);
673 print_name_lsdb();
akmhoquede61ba92012-09-20 22:19:12 -0500674 print_npt();
akmhoque53f64222012-09-05 13:57:51 -0500675
akmhoquef5537b42013-02-22 08:33:32 -0600676 destroy_name_lsa(name_lsa);
akmhoque53f64222012-09-05 13:57:51 -0500677
678}
679
680void
akmhoque03004e62012-09-06 01:12:28 -0500681build_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 -0500682{
akmhoque7b791452012-10-30 11:24:56 -0500683 if ( nlsr->debugging )
684 printf("build_others_name_lsa called\n");
685 if ( nlsr->detailed_logging )
686 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_others_name_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500687
akmhoquef5537b42013-02-22 08:33:32 -0600688 name_lsa->header=(struct nlsa_header *)calloc(1,sizeof(struct nlsa_header ));
akmhoque03004e62012-09-06 01:12:28 -0500689 name_lsa->header->ls_type=LS_TYPE_NAME;
akmhoque53f64222012-09-05 13:57:51 -0500690
akmhoquef5537b42013-02-22 08:33:32 -0600691 name_lsa->header->orig_time=(char *)calloc(strlen(orig_time)+1,sizeof(char));
692 memcpy(name_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
akmhoque53f64222012-09-05 13:57:51 -0500693
akmhoque03004e62012-09-06 01:12:28 -0500694 name_lsa->header->ls_id=ls_id;
akmhoquef5537b42013-02-22 08:33:32 -0600695 name_lsa->header->orig_router=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
696 name_lsa->header->orig_router->name=(char *)calloc(strlen(orig_router)+1,sizeof(char));
697 memcpy(name_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
akmhoque03004e62012-09-06 01:12:28 -0500698 name_lsa->header->orig_router->length=strlen(orig_router)+1;
699 name_lsa->header->isValid=isValid;
akmhoque53f64222012-09-05 13:57:51 -0500700
akmhoquef5537b42013-02-22 08:33:32 -0600701 name_lsa->name_prefix=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
702 name_lsa->name_prefix->name=(char *)calloc(strlen(np)+1,sizeof(char));
703 memcpy(name_lsa->name_prefix->name,np,strlen(np)+1);
akmhoque03004e62012-09-06 01:12:28 -0500704 name_lsa->name_prefix->length=strlen(np)+1;
akmhoquef71d9082012-08-22 12:51:53 -0400705}
akmhoqued79438d2012-08-27 13:31:42 -0500706
707
akmhoqueb77b95f2013-02-08 12:28:47 -0600708void
709make_cor_lsa_key(char *key,struct clsa *cor_lsa)
710{
711 memcpy(key+strlen(key),cor_lsa->header->orig_router->name,cor_lsa->header->orig_router->length);
712 memcpy(key+strlen(key),"/",1);
713 char ls_type[2];
714 sprintf(ls_type,"%d",cor_lsa->header->ls_type);
715 memcpy(key+strlen(key),ls_type,strlen(ls_type));
716 key[strlen(key)]='\0';
717}
akmhoqueffacaa82012-09-13 17:48:30 -0500718
719
akmhoque53f64222012-09-05 13:57:51 -0500720void
721make_adj_lsa_key(char *key,struct alsa *adj_lsa)
akmhoqued79438d2012-08-27 13:31:42 -0500722{
akmhoque53f64222012-09-05 13:57:51 -0500723 memcpy(key+strlen(key),adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
724 memcpy(key+strlen(key),"/",1);
725 char ls_type[2];
726 sprintf(ls_type,"%d",adj_lsa->header->ls_type);
727 memcpy(key+strlen(key),ls_type,strlen(ls_type));
728 key[strlen(key)]='\0';
729}
730
akmhoque03004e62012-09-06 01:12:28 -0500731int
732build_and_install_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
733{
akmhoqueffacaa82012-09-13 17:48:30 -0500734 if(flags == CCN_SCHEDULE_CANCEL)
735 {
736 return -1;
737 }
738
739 nlsr_lock();
740
akmhoque7b791452012-10-30 11:24:56 -0500741 if ( nlsr->debugging )
742 printf("build_and_install_adj_lsa called \n");
743 if ( nlsr->detailed_logging )
744 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_adj_lsa called \n");
745
746 if ( nlsr->debugging )
747 printf("adj_build_flag = %d \n",nlsr->adj_build_flag);
748 if ( nlsr->detailed_logging )
749 writeLogg(__FILE__,__FUNCTION__,__LINE__,"adj_build_flag = %d \n",nlsr->adj_build_flag);
akmhoque03004e62012-09-06 01:12:28 -0500750
751 if(nlsr->adj_build_flag > 0)
752 {
akmhoque7b791452012-10-30 11:24:56 -0500753 if ( nlsr->debugging )
754 printf("is_adj_lsa_build = %d \n",is_adj_lsa_build());
755 if ( nlsr->detailed_logging )
756 writeLogg(__FILE__,__FUNCTION__,__LINE__,"is_adj_lsa_build = %d \n",is_adj_lsa_build());
757
akmhoque03004e62012-09-06 01:12:28 -0500758 if ( is_adj_lsa_build()> 0)
759 {
760 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
761 build_adj_lsa(adj_lsa);
762 install_adj_lsa(adj_lsa);
763
akmhoqueb77b95f2013-02-08 12:28:47 -0600764 char lst[2];
765 memset(lst,0,2);
766 sprintf(lst,"%d",LS_TYPE_ADJ);
767
akmhoquef5537b42013-02-22 08:33:32 -0600768 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));
769 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 -0600770
771 if ( nlsr->debugging )
772 printf("Adj LSA Repo Key: %s \n",repo_key);
773
akmhoquef5537b42013-02-22 08:33:32 -0600774 char *key=(char *)calloc(adj_lsa->header->orig_router->length+5,sizeof(char));
akmhoqueb77b95f2013-02-08 12:28:47 -0600775 make_adj_lsa_key(key,adj_lsa);
776 if ( nlsr->debugging )
777 printf("Adj LSA: %s \n",key);
778
akmhoquef5537b42013-02-22 08:33:32 -0600779 struct name_prefix *lsaid=(struct name_prefix *)calloc(1,sizeof(struct name_prefix));
780 lsaid->name=(char *)calloc(strlen(key)+1,sizeof(char));
781 memcpy(lsaid->name,key,strlen(key)+1);
akmhoqueb77b95f2013-02-08 12:28:47 -0600782 lsaid->length=strlen(key)+1;
783
784
785 write_adj_lsa_to_repo(repo_key, lsaid);
786
akmhoquef5537b42013-02-22 08:33:32 -0600787 free(key);
akmhoqueb77b95f2013-02-08 12:28:47 -0600788 free(repo_key);
789 free(lsaid->name);
790 free(lsaid);
791
akmhoquef5537b42013-02-22 08:33:32 -0600792 destroy_adj_lsa(adj_lsa);
akmhoqueb77b95f2013-02-08 12:28:47 -0600793
akmhoque03004e62012-09-06 01:12:28 -0500794 nlsr->adj_build_flag=0;
795 print_adj_lsdb();
796 }
797 else
798 {
akmhoque7b791452012-10-30 11:24:56 -0500799 if ( nlsr->debugging )
800 printf("Can not build adj LSA now\n");
801 if ( nlsr->detailed_logging )
802 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Can not build adj LSA now\n");
akmhoque03004e62012-09-06 01:12:28 -0500803 }
804 }
805 nlsr->is_build_adj_lsa_sheduled=0;
akmhoqueffacaa82012-09-13 17:48:30 -0500806
807 nlsr_unlock();
808
akmhoque03004e62012-09-06 01:12:28 -0500809 return 0;
810}
811
812
813void
814build_adj_lsa(struct alsa * adj_lsa)
815{
akmhoque7b791452012-10-30 11:24:56 -0500816 if ( nlsr->debugging )
817 printf("build_adj_lsa called \n");
818 if ( nlsr->detailed_logging )
819 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_adj_lsa called \n");
akmhoque03004e62012-09-06 01:12:28 -0500820
821 int no_link=no_active_nbr();
akmhoque7b791452012-10-30 11:24:56 -0500822
akmhoque03004e62012-09-06 01:12:28 -0500823 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
824 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
825 adj_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1);
826 memset(adj_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
827 memcpy(adj_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
828 adj_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
829
830 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
831
akmhoque03004e62012-09-06 01:12:28 -0500832
akmhoquef5537b42013-02-22 08:33:32 -0600833 char *time_stamp=get_current_timestamp_micro_v2();
akmhoque03004e62012-09-06 01:12:28 -0500834 adj_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1);
835 memset(adj_lsa->header->orig_time,0,strlen(time_stamp)+1);
836 memcpy(adj_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
837 free(time_stamp);
838
akmhoque03004e62012-09-06 01:12:28 -0500839 adj_lsa->no_link=no_link;
akmhoque03004e62012-09-06 01:12:28 -0500840
841
842 struct ccn_charbuf *c=ccn_charbuf_create();
843 get_active_nbr_adj_data(c);
844 char *data=ccn_charbuf_as_string(c);
845
akmhoquef5537b42013-02-22 08:33:32 -0600846 adj_lsa->body=(char *)calloc(strlen(data)+1,sizeof(char));
847 //memset(adj_lsa->body,0,strlen(data)+1);
akmhoque03004e62012-09-06 01:12:28 -0500848 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
akmhoquef5537b42013-02-22 08:33:32 -0600849 data[strlen(data)]='\0';
akmhoque03004e62012-09-06 01:12:28 -0500850 ccn_charbuf_destroy(&c);
851
akmhoque03004e62012-09-06 01:12:28 -0500852 nlsr->adj_build_count++;
853
854
855}
856
857
akmhoque53f64222012-09-05 13:57:51 -0500858void
859install_adj_lsa(struct alsa * adj_lsa)
860{
akmhoqued79438d2012-08-27 13:31:42 -0500861
akmhoque7b791452012-10-30 11:24:56 -0500862 if ( nlsr->debugging )
863 printf("install_adj_lsa called \n");
864 if ( nlsr->detailed_logging )
865 writeLogg(__FILE__,__FUNCTION__,__LINE__,"install_adj_lsa called \n");
866
akmhoque03004e62012-09-06 01:12:28 -0500867
akmhoque53f64222012-09-05 13:57:51 -0500868
akmhoqueffacaa82012-09-13 17:48:30 -0500869
akmhoquef5537b42013-02-22 08:33:32 -0600870 char *key=(char *)calloc(adj_lsa->header->orig_router->length+4,sizeof(char));
akmhoqueffacaa82012-09-13 17:48:30 -0500871 make_adj_lsa_key(key,adj_lsa);
akmhoqueffacaa82012-09-13 17:48:30 -0500872
akmhoque8876e982013-02-21 13:35:46 -0600873 struct alsa *new_adj_lsa;//=(struct alsa*)malloc(sizeof(struct alsa ));
akmhoqueffacaa82012-09-13 17:48:30 -0500874
875 struct hashtb_enumerator ee;
876 struct hashtb_enumerator *e = &ee;
877 int res;
878
879 hashtb_start(nlsr->lsdb->adj_lsdb, e);
880 res = hashtb_seek(e, key, strlen(key), 0);
akmhoque53f64222012-09-05 13:57:51 -0500881
akmhoque03004e62012-09-06 01:12:28 -0500882
akmhoque53f64222012-09-05 13:57:51 -0500883
akmhoque62c0c192012-09-24 07:49:25 -0500884 if(res == HT_NEW_ENTRY)
akmhoque53f64222012-09-05 13:57:51 -0500885 {
akmhoque62c0c192012-09-24 07:49:25 -0500886 if ( adj_lsa->no_link > 0)
887 {
akmhoque3171d652012-11-13 11:44:33 -0600888 if ( nlsr->debugging )
889 printf("New ADJ LSA... Adding to LSDB\n");
890 if ( nlsr->detailed_logging )
891 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New ADJ LSA... Adding to LSDB\n");
akmhoque62c0c192012-09-24 07:49:25 -0500892 new_adj_lsa = e->data;
akmhoque53f64222012-09-05 13:57:51 -0500893
akmhoquef5537b42013-02-22 08:33:32 -0600894 new_adj_lsa->header=(struct alsa_header *)calloc(1,sizeof(struct alsa_header ));
akmhoque62c0c192012-09-24 07:49:25 -0500895 new_adj_lsa->header->ls_type=adj_lsa->header->ls_type;
akmhoquef5537b42013-02-22 08:33:32 -0600896 new_adj_lsa->header->orig_time=(char *)calloc(strlen(adj_lsa->header->orig_time)+1,sizeof(char));
akmhoque62c0c192012-09-24 07:49:25 -0500897 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 -0500898
akmhoquef5537b42013-02-22 08:33:32 -0600899 new_adj_lsa->header->orig_router=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
900 new_adj_lsa->header->orig_router->name=(char *)calloc(adj_lsa->header->orig_router->length,sizeof(char));
akmhoque62c0c192012-09-24 07:49:25 -0500901 memcpy(new_adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
902 new_adj_lsa->header->orig_router->length=adj_lsa->header->orig_router->length;
akmhoque53f64222012-09-05 13:57:51 -0500903
akmhoque62c0c192012-09-24 07:49:25 -0500904 new_adj_lsa->no_link=adj_lsa->no_link;
akmhoqueffacaa82012-09-13 17:48:30 -0500905
akmhoquef5537b42013-02-22 08:33:32 -0600906 new_adj_lsa->body=(char *)calloc(strlen(adj_lsa->body)+1,sizeof(char));
907 //memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
akmhoque62c0c192012-09-24 07:49:25 -0500908 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
909
910 add_next_hop_router(new_adj_lsa->header->orig_router->name);
911 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
akmhoque9e9fc722012-09-26 14:03:25 -0500912
913 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
914 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
915 write_log_for_adj_lsa(new_adj_lsa);
916 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoque62c0c192012-09-24 07:49:25 -0500917 }
918 else
919 {
920 hashtb_delete(e);
921 }
akmhoque53f64222012-09-05 13:57:51 -0500922
akmhoque7b791452012-10-30 11:24:56 -0500923 if ( nlsr->debugging )
924 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
925 if ( nlsr->detailed_logging )
926 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
927
akmhoque53f64222012-09-05 13:57:51 -0500928 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500929
930 if ( nlsr->debugging )
931 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
932 if ( nlsr->detailed_logging )
933 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
934
akmhoqueffacaa82012-09-13 17:48:30 -0500935 }
936 else if(res == HT_OLD_ENTRY)
937 {
938 new_adj_lsa = e->data;
939 if(strcmp(adj_lsa->header->orig_time,new_adj_lsa->header->orig_time)<=0)
940 {
akmhoque7b791452012-10-30 11:24:56 -0500941 if ( nlsr->debugging )
942 printf("Older/Duplicate Adj LSA. Discarded...\n");
943 if ( nlsr->detailed_logging )
944 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Older/Duplicate Adj LSA. Discarded...\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500945 }
946 else
947 {
akmhoqueffacaa82012-09-13 17:48:30 -0500948
akmhoque62c0c192012-09-24 07:49:25 -0500949 if ( adj_lsa->no_link > 0)
950 {
akmhoque9e9fc722012-09-26 14:03:25 -0500951 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
952 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
953 write_log_for_adj_lsa(new_adj_lsa);
954 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500955
akmhoque62c0c192012-09-24 07:49:25 -0500956 free(new_adj_lsa->header->orig_time);
akmhoquef5537b42013-02-22 08:33:32 -0600957 new_adj_lsa->header->orig_time=(char *)calloc(strlen(adj_lsa->header->orig_time)+1,sizeof(char));
akmhoque62c0c192012-09-24 07:49:25 -0500958 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
959
960 new_adj_lsa->no_link=adj_lsa->no_link;
akmhoqueffacaa82012-09-13 17:48:30 -0500961
akmhoquef5537b42013-02-22 08:33:32 -0600962 new_adj_lsa->body=(char *)calloc(strlen(adj_lsa->body)+1,sizeof(char));
963 //memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
akmhoque62c0c192012-09-24 07:49:25 -0500964 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
akmhoqueffacaa82012-09-13 17:48:30 -0500965
akmhoque62c0c192012-09-24 07:49:25 -0500966 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
akmhoque9e9fc722012-09-26 14:03:25 -0500967
968 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
969 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
970 write_log_for_adj_lsa(new_adj_lsa);
971 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoque62c0c192012-09-24 07:49:25 -0500972 }
973 else
974 {
akmhoque9e9fc722012-09-26 14:03:25 -0500975 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
976 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
977 write_log_for_adj_lsa(new_adj_lsa);
978 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
979
akmhoque62c0c192012-09-24 07:49:25 -0500980 hashtb_delete(e);
981 }
akmhoque7b791452012-10-30 11:24:56 -0500982
983 if ( nlsr->debugging )
984 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
985 if ( nlsr->detailed_logging )
986 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
987
akmhoqueffacaa82012-09-13 17:48:30 -0500988 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500989
990 if ( nlsr->debugging )
991 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
992 if ( nlsr->detailed_logging )
993 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500994 }
995
996 }
997 hashtb_end(e);
998
999 if ( !nlsr->is_route_calculation_scheduled )
1000 {
1001 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
1002 nlsr->is_route_calculation_scheduled=1;
akmhoque53f64222012-09-05 13:57:51 -05001003 }
1004
akmhoque03004e62012-09-06 01:12:28 -05001005
akmhoquef5537b42013-02-22 08:33:32 -06001006 free(key);
akmhoqueffacaa82012-09-13 17:48:30 -05001007
akmhoquef5537b42013-02-22 08:33:32 -06001008 //free(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -05001009}
1010
1011void
akmhoque9e9fc722012-09-26 14:03:25 -05001012write_log_for_adj_lsa_body(const char *body, int no_link)
1013{
1014 int i=0;
akmhoquef5537b42013-02-22 08:33:32 -06001015 char *lsa_data=(char *)calloc(strlen(body)+1,sizeof(char));
1016 //memset( lsa_data,0,strlen(body)+1);
akmhoque9e9fc722012-09-26 14:03:25 -05001017 memcpy(lsa_data,body,strlen(body)+1);
1018 char *sep="|";
1019 char *rem;
1020 char *rtr_id;
1021 char *length;
akmhoqueb28579d2013-02-12 11:15:52 -06001022 //char *face;
akmhoque9e9fc722012-09-26 14:03:25 -05001023 char *metric;
1024
1025 if(no_link >0 )
1026 {
1027 rtr_id=strtok_r(lsa_data,sep,&rem);
1028 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -06001029 //face=strtok_r(NULL,sep,&rem);
akmhoque9e9fc722012-09-26 14:03:25 -05001030 metric=strtok_r(NULL,sep,&rem);
1031
1032 writeLogg(__FILE__,__FUNCTION__,__LINE__," Link %d \n",i+1);
akmhoque34b99f92012-09-27 12:24:27 -05001033 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router: %s \n",rtr_id);
1034 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router Length: %s \n",length);
akmhoque866c2222013-02-12 10:49:33 -06001035 //writeLogg(__FILE__,__FUNCTION__,__LINE__," Connecting Face: %s \n",face);
akmhoque34b99f92012-09-27 12:24:27 -05001036 writeLogg(__FILE__,__FUNCTION__,__LINE__," Metric: %s \n",metric);
akmhoque9e9fc722012-09-26 14:03:25 -05001037
1038
1039 for(i=1;i<no_link;i++)
1040 {
1041 rtr_id=strtok_r(NULL,sep,&rem);
1042 length=strtok_r(NULL,sep,&rem);
akmhoqueb28579d2013-02-12 11:15:52 -06001043 //face=strtok_r(NULL,sep,&rem);
akmhoque9e9fc722012-09-26 14:03:25 -05001044 metric=strtok_r(NULL,sep,&rem);
1045 writeLogg(__FILE__,__FUNCTION__,__LINE__," Link %d \n",i+1);
akmhoque34b99f92012-09-27 12:24:27 -05001046 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router: %s \n",rtr_id);
1047 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router Length: %s \n",length);
akmhoque866c2222013-02-12 10:49:33 -06001048 //writeLogg(__FILE__,__FUNCTION__,__LINE__," Connecting Face: %s \n",face);
akmhoque34b99f92012-09-27 12:24:27 -05001049 writeLogg(__FILE__,__FUNCTION__,__LINE__," Metric: %s \n",metric);
akmhoque9e9fc722012-09-26 14:03:25 -05001050
1051 }
1052 }
1053
akmhoquef5537b42013-02-22 08:33:32 -06001054 if(lsa_data)
1055 free(lsa_data);
akmhoque9e9fc722012-09-26 14:03:25 -05001056}
1057
1058
1059void
1060write_log_for_adj_lsa(struct alsa * adj_lsa)
1061{
1062 writeLogg(__FILE__,__FUNCTION__,__LINE__,"-----------Adj LSA Content---------------\n");
akmhoque34b99f92012-09-27 12:24:27 -05001063 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s\n",adj_lsa->header->orig_router->name);
1064 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
1065 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Type: %d\n",adj_lsa->header->ls_type);
1066 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Time: %s\n",adj_lsa->header->orig_time);
akmhoque9e9fc722012-09-26 14:03:25 -05001067 writeLogg(__FILE__,__FUNCTION__,__LINE__," Lsa Data:\n");
akmhoque34b99f92012-09-27 12:24:27 -05001068 writeLogg(__FILE__,__FUNCTION__,__LINE__," No of Link: %d\n",adj_lsa->no_link);
akmhoque9e9fc722012-09-26 14:03:25 -05001069
1070 write_log_for_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
1071
1072 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
1073
1074}
1075
1076void
akmhoque53f64222012-09-05 13:57:51 -05001077print_adj_lsa_body(const char *body, int no_link)
1078{
1079 int i=0;
akmhoquef5537b42013-02-22 08:33:32 -06001080 char *lsa_data=(char *)calloc(strlen(body)+1,sizeof(char));
1081 //memset( lsa_data,0,strlen(body)+1);
akmhoque53f64222012-09-05 13:57:51 -05001082 memcpy(lsa_data,body,strlen(body)+1);
1083 char *sep="|";
1084 char *rem;
1085 char *rtr_id;
1086 char *length;
akmhoqueb28579d2013-02-12 11:15:52 -06001087 //char *face;
akmhoque53f64222012-09-05 13:57:51 -05001088 char *metric;
1089
akmhoque53f64222012-09-05 13:57:51 -05001090 if(no_link >0 )
1091 {
1092 rtr_id=strtok_r(lsa_data,sep,&rem);
1093 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -06001094 //face=strtok_r(NULL,sep,&rem);
akmhoque53f64222012-09-05 13:57:51 -05001095 metric=strtok_r(NULL,sep,&rem);
1096
akmhoqueb77b95f2013-02-08 12:28:47 -06001097 if ( nlsr->debugging ) {
1098 printf(" Link %d \n",i+1);
1099 printf(" Neighbor : %s \n",rtr_id);
1100 printf(" Neighbor Length : %s \n",length);
akmhoque866c2222013-02-12 10:49:33 -06001101 //printf(" Connecting Face : %s \n",face);
akmhoqueb77b95f2013-02-08 12:28:47 -06001102 printf(" Metric : %s \n",metric);
1103 }
akmhoque53f64222012-09-05 13:57:51 -05001104
1105 for(i=1;i<no_link;i++)
1106 {
1107 rtr_id=strtok_r(NULL,sep,&rem);
1108 length=strtok_r(NULL,sep,&rem);
akmhoqueb28579d2013-02-12 11:15:52 -06001109 //face=strtok_r(NULL,sep,&rem);
akmhoque53f64222012-09-05 13:57:51 -05001110 metric=strtok_r(NULL,sep,&rem);
1111 printf(" Link %d \n",i+1);
1112 printf(" Neighbor : %s \n",rtr_id);
1113 printf(" Neighbor Length : %s \n",length);
akmhoque866c2222013-02-12 10:49:33 -06001114 //printf(" Connecting Face : %s \n",face);
akmhoque53f64222012-09-05 13:57:51 -05001115 printf(" Metric : %s \n",metric);
1116
1117 }
1118 }
1119
akmhoquef5537b42013-02-22 08:33:32 -06001120 if( lsa_data )
1121 free(lsa_data);
akmhoque53f64222012-09-05 13:57:51 -05001122}
1123
1124void
1125print_adj_lsa(struct alsa * adj_lsa)
1126{
akmhoque7b791452012-10-30 11:24:56 -05001127 if ( nlsr->debugging )
1128 {
1129 printf("-----------ADJ LSA Content---------------\n");
1130 printf(" Origination Router : %s\n",adj_lsa->header->orig_router->name);
1131 printf(" Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
1132 printf(" LS Type : %d\n",adj_lsa->header->ls_type);
1133 printf(" Origination Time : %s\n",adj_lsa->header->orig_time);
1134 printf(" Lsa Data:\n");
1135 printf(" No of Link : %d\n",adj_lsa->no_link);
akmhoque53f64222012-09-05 13:57:51 -05001136
akmhoque7b791452012-10-30 11:24:56 -05001137 print_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
1138 printf("\n");
1139 }
akmhoque53f64222012-09-05 13:57:51 -05001140
1141}
1142
1143void
1144print_adj_lsdb(void)
1145{
akmhoque7b791452012-10-30 11:24:56 -05001146 if ( nlsr->debugging )
1147 printf("print_name_lsdb called \n");
akmhoque53f64222012-09-05 13:57:51 -05001148 int i, adj_lsdb_element;
1149 struct alsa *adj_lsa;
1150
1151 struct hashtb_enumerator ee;
1152 struct hashtb_enumerator *e = &ee;
1153
1154 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1155 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1156
1157 for(i=0;i<adj_lsdb_element;i++)
1158 {
akmhoque7b791452012-10-30 11:24:56 -05001159 if ( nlsr->debugging )
1160 printf("-----------Adj LSA (%d)---------------\n",i+1);
akmhoque53f64222012-09-05 13:57:51 -05001161 adj_lsa=e->data;
1162 print_adj_lsa(adj_lsa);
1163 hashtb_next(e);
1164 }
1165
1166 hashtb_end(e);
1167
akmhoque3171d652012-11-13 11:44:33 -06001168 if ( nlsr->debugging )
1169 printf("\n");
1170 if ( nlsr->detailed_logging )
1171 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoque53f64222012-09-05 13:57:51 -05001172}
1173
1174void
akmhoque03004e62012-09-06 01:12:28 -05001175build_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 -05001176{
akmhoque7b791452012-10-30 11:24:56 -05001177 if ( nlsr->debugging )
1178 printf("build_and_install_others_adj_lsa called \n");
1179 if ( nlsr->detailed_logging )
1180 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_others_adj_lsa called \n");
akmhoque03004e62012-09-06 01:12:28 -05001181 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
1182 build_others_adj_lsa(adj_lsa,orig_router,ls_type,orig_time,no_link,data);
akmhoque03004e62012-09-06 01:12:28 -05001183 install_adj_lsa(adj_lsa);
1184
akmhoquef5537b42013-02-22 08:33:32 -06001185 destroy_adj_lsa(adj_lsa);
akmhoque53f64222012-09-05 13:57:51 -05001186
akmhoque03004e62012-09-06 01:12:28 -05001187 print_adj_lsdb();
akmhoque53f64222012-09-05 13:57:51 -05001188
akmhoque53f64222012-09-05 13:57:51 -05001189}
1190
akmhoque03004e62012-09-06 01:12:28 -05001191
akmhoque53f64222012-09-05 13:57:51 -05001192void
akmhoque03004e62012-09-06 01:12:28 -05001193build_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 -05001194{
akmhoque7b791452012-10-30 11:24:56 -05001195 if ( nlsr->debugging )
1196 printf("build_others_adj_lsa called \n");
1197 if ( nlsr->detailed_logging )
1198 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_others_adj_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -05001199
akmhoque03004e62012-09-06 01:12:28 -05001200 /*Filling Up Header Data */
akmhoquef5537b42013-02-22 08:33:32 -06001201 adj_lsa->header=(struct alsa_header *)calloc(1,sizeof(struct alsa_header ));
1202 adj_lsa->header->orig_router=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
1203 adj_lsa->header->orig_router->name=(char *)calloc(strlen(orig_router)+1,sizeof(char));
akmhoque03004e62012-09-06 01:12:28 -05001204 memcpy(adj_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
akmhoque53f64222012-09-05 13:57:51 -05001205
akmhoque03004e62012-09-06 01:12:28 -05001206 adj_lsa->header->orig_router->length=strlen(orig_router)+1;
akmhoque53f64222012-09-05 13:57:51 -05001207
akmhoque53f64222012-09-05 13:57:51 -05001208
akmhoque03004e62012-09-06 01:12:28 -05001209 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
akmhoque53f64222012-09-05 13:57:51 -05001210
akmhoquef5537b42013-02-22 08:33:32 -06001211 adj_lsa->header->orig_time=(char *)calloc(strlen(orig_time)+1,sizeof(char));
akmhoque03004e62012-09-06 01:12:28 -05001212 memcpy(adj_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
akmhoque53f64222012-09-05 13:57:51 -05001213
akmhoque03004e62012-09-06 01:12:28 -05001214 adj_lsa->no_link=no_link;
akmhoque53f64222012-09-05 13:57:51 -05001215
akmhoquef5537b42013-02-22 08:33:32 -06001216 adj_lsa->body=(char *)calloc(strlen(data)+1,sizeof(char));
akmhoque03004e62012-09-06 01:12:28 -05001217 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
akmhoque53f64222012-09-05 13:57:51 -05001218
akmhoque53f64222012-09-05 13:57:51 -05001219}
1220
akmhoque03004e62012-09-06 01:12:28 -05001221
akmhoque53f64222012-09-05 13:57:51 -05001222long int
1223get_name_lsdb_num_element(void)
1224{
1225 long int num_element;
1226
1227
1228 struct hashtb_enumerator ee;
1229 struct hashtb_enumerator *e = &ee;
1230
1231 hashtb_start(nlsr->lsdb->name_lsdb, e);
1232 num_element=hashtb_n(nlsr->lsdb->name_lsdb);
1233 hashtb_end(e);
1234
1235 return num_element;
1236}
1237
1238long int
1239get_adj_lsdb_num_element(void)
1240{
1241 long int num_element;
1242
1243
1244 struct hashtb_enumerator ee;
1245 struct hashtb_enumerator *e = &ee;
1246
1247 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1248 num_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1249 hashtb_end(e);
1250
1251 return num_element;
akmhoqued79438d2012-08-27 13:31:42 -05001252}
akmhoque03004e62012-09-06 01:12:28 -05001253
akmhoque03004e62012-09-06 01:12:28 -05001254
1255int
1256check_is_new_name_lsa(char *orig_router,char *lst,char *lsid,char *orig_time)
1257{
1258 int ret=0;
1259 struct ccn_charbuf *key=ccn_charbuf_create();
1260 ccn_charbuf_append_string(key,orig_router);
1261 ccn_charbuf_append_string(key,"/");
1262 ccn_charbuf_append_string(key,lst);
1263 ccn_charbuf_append_string(key,"/");
1264 ccn_charbuf_append_string(key,lsid);
1265
1266 int res;
1267 struct nlsa *name_lsa;
1268
1269 struct hashtb_enumerator ee;
1270 struct hashtb_enumerator *e = &ee;
1271
1272 hashtb_start(nlsr->lsdb->name_lsdb, e);
1273 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1274
1275 if( res == HT_NEW_ENTRY )
1276 {
1277 hashtb_delete(e);
1278 ret=1;
1279
1280 }
1281 else if(res == HT_OLD_ENTRY)
1282 {
1283 name_lsa=e->data;
1284 if( strcmp ( orig_time , name_lsa->header->orig_time ) > 0 )
1285 {
1286 ret=1;
1287 }
1288 }
1289
1290 hashtb_end(e);
1291
1292 ccn_charbuf_destroy(&key);
1293
1294 return ret;
1295}
1296
1297int
1298check_is_new_adj_lsa(char *orig_router,char *lst,char *orig_time)
1299{
1300 int ret=0;
1301 struct ccn_charbuf *key=ccn_charbuf_create();
1302 ccn_charbuf_append_string(key,orig_router);
1303 ccn_charbuf_append_string(key,"/");
1304 ccn_charbuf_append_string(key,lst);
1305
1306 int res;
1307 struct alsa *adj_lsa;
1308
1309 struct hashtb_enumerator ee;
1310 struct hashtb_enumerator *e = &ee;
1311
1312 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1313 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1314
1315 if( res == HT_NEW_ENTRY )
1316 {
1317 hashtb_delete(e);
1318 ret=1;
1319
1320 }
1321 else if(res == HT_OLD_ENTRY)
1322 {
1323 adj_lsa=e->data;
1324 if( strcmp ( orig_time , adj_lsa->header->orig_time ) > 0 )
1325 {
1326 ret=1;
1327 }
1328 }
1329
1330 hashtb_end(e);
1331
1332 ccn_charbuf_destroy(&key);
1333
1334 return ret;
1335}
1336
akmhoqueb77b95f2013-02-08 12:28:47 -06001337int
1338check_is_new_cor_lsa(char *orig_router,char *lst,char *orig_time)
1339{
1340 int ret=0;
1341 struct ccn_charbuf *key=ccn_charbuf_create();
1342 ccn_charbuf_append_string(key,orig_router);
1343 ccn_charbuf_append_string(key,"/");
1344 ccn_charbuf_append_string(key,lst);
1345
1346 int res;
1347 struct clsa *cor_lsa;
1348
1349 struct hashtb_enumerator ee;
1350 struct hashtb_enumerator *e = &ee;
1351
1352 hashtb_start(nlsr->lsdb->cor_lsdb, e);
1353 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1354
1355 if( res == HT_NEW_ENTRY )
1356 {
1357 hashtb_delete(e);
1358 ret=1;
1359
1360 }
1361 else if(res == HT_OLD_ENTRY)
1362 {
1363 cor_lsa=e->data;
1364 if( strcmp ( orig_time , cor_lsa->header->orig_time ) > 0 )
1365 {
1366 ret=1;
1367 }
1368 }
1369
1370 hashtb_end(e);
1371
1372 ccn_charbuf_destroy(&key);
1373
1374 return ret;
1375}
1376
akmhoque03004e62012-09-06 01:12:28 -05001377void
1378get_name_lsa_data(struct ccn_charbuf *lsa_data, struct name_prefix *lsaId)
1379{
akmhoque7b791452012-10-30 11:24:56 -05001380 if ( nlsr->debugging )
1381 printf("get_name_lsa_data called \n");
1382 if ( nlsr->detailed_logging )
1383 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_name_lsa_data called \n");
akmhoque03004e62012-09-06 01:12:28 -05001384
akmhoque8876e982013-02-21 13:35:46 -06001385 struct nlsa *name_lsa;//=(struct nlsa*)malloc(sizeof(struct nlsa ));
akmhoque03004e62012-09-06 01:12:28 -05001386
1387 struct hashtb_enumerator ee;
1388 struct hashtb_enumerator *e = &ee;
1389 int res;
1390
1391 hashtb_start(nlsr->lsdb->name_lsdb, e);
1392 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1393
1394 if( res == HT_OLD_ENTRY )
1395 {
1396 name_lsa=e->data;
akmhoqueb77b95f2013-02-08 12:28:47 -06001397
akmhoque7b791452012-10-30 11:24:56 -05001398 if ( nlsr->debugging )
1399 printf("NAME LSA found \n");
1400 if ( nlsr->detailed_logging )
1401 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Name LSA found \n");
akmhoque03004e62012-09-06 01:12:28 -05001402
1403 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_router->name);
1404 ccn_charbuf_append_string(lsa_data,"|");
1405
1406 char *temp_length=(char *)malloc(20);
1407 memset(temp_length,0,20);
1408 sprintf(temp_length,"%d",name_lsa->header->orig_router->length);
1409 ccn_charbuf_append_string(lsa_data,temp_length);
1410 free(temp_length);
1411 ccn_charbuf_append_string(lsa_data,"|");
1412
1413 char *temp_ltype=(char *)malloc(20);
1414 memset(temp_ltype,0,20);
1415 sprintf(temp_ltype,"%d",name_lsa->header->ls_type);
1416 ccn_charbuf_append_string(lsa_data,temp_ltype);
1417 free(temp_ltype);
1418 ccn_charbuf_append_string(lsa_data,"|");
1419
1420 char *temp_lsid=(char *)malloc(20);
1421 memset(temp_lsid,0,20);
1422 sprintf(temp_lsid,"%ld",name_lsa->header->ls_id);
1423 ccn_charbuf_append_string(lsa_data,temp_lsid);
1424 free(temp_lsid);
1425 ccn_charbuf_append_string(lsa_data,"|");
1426
1427 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_time);
1428 ccn_charbuf_append_string(lsa_data,"|");
1429
1430 char *temp_valid=(char *)malloc(20);
1431 memset(temp_valid,0,20);
1432 sprintf(temp_valid,"%d",name_lsa->header->isValid);
1433 ccn_charbuf_append_string(lsa_data,temp_valid);
1434 free(temp_valid);
1435 ccn_charbuf_append_string(lsa_data,"|");
1436
1437 ccn_charbuf_append_string(lsa_data,name_lsa->name_prefix->name);
1438 ccn_charbuf_append_string(lsa_data,"|");
1439
1440 char *temp_npl=(char *)malloc(20);
1441 memset(temp_npl,0,20);
1442 sprintf(temp_npl,"%d",name_lsa->name_prefix->length);
1443 ccn_charbuf_append_string(lsa_data,temp_npl);
1444 free(temp_npl);
1445 ccn_charbuf_append_string(lsa_data,"|");
1446
1447 }
1448 else if(res == HT_NEW_ENTRY)
1449 {
1450 hashtb_delete(e);
1451 }
1452
1453 hashtb_end(e);
1454}
1455
1456void
1457get_adj_lsa_data(struct ccn_charbuf *lsa_data,struct name_prefix *lsaId)
1458{
akmhoque7b791452012-10-30 11:24:56 -05001459 if ( nlsr->debugging )
1460 printf("get_adj_lsa_data called \n");
1461 if ( nlsr->detailed_logging )
1462 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_adj_lsa_data called \n");
akmhoque03004e62012-09-06 01:12:28 -05001463
1464 struct alsa *adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
1465
1466 struct hashtb_enumerator ee;
1467 struct hashtb_enumerator *e = &ee;
1468 int res;
1469
1470 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1471 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1472
1473 if( res == HT_OLD_ENTRY )
1474 {
1475 adj_lsa=e->data;
akmhoque7b791452012-10-30 11:24:56 -05001476
1477 if ( nlsr->debugging )
1478 printf("Adj LSA found \n");
1479 if ( nlsr->detailed_logging )
1480 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adj LSA found \n");
akmhoque03004e62012-09-06 01:12:28 -05001481
1482 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_router->name);
1483 ccn_charbuf_append_string(lsa_data,"|");
1484
1485 char *temp_length=(char *)malloc(20);
1486 memset(temp_length,0,20);
1487 sprintf(temp_length,"%d",adj_lsa->header->orig_router->length);
1488 ccn_charbuf_append_string(lsa_data,temp_length);
akmhoque03004e62012-09-06 01:12:28 -05001489 ccn_charbuf_append_string(lsa_data,"|");
akmhoqueb77b95f2013-02-08 12:28:47 -06001490 free(temp_length);
akmhoque03004e62012-09-06 01:12:28 -05001491
1492 char *temp_ltype=(char *)malloc(20);
1493 memset(temp_ltype,0,20);
1494 sprintf(temp_ltype,"%d",adj_lsa->header->ls_type);
1495 ccn_charbuf_append_string(lsa_data,temp_ltype);
akmhoque03004e62012-09-06 01:12:28 -05001496 ccn_charbuf_append_string(lsa_data,"|");
akmhoqueb77b95f2013-02-08 12:28:47 -06001497 free(temp_ltype);
akmhoque03004e62012-09-06 01:12:28 -05001498
1499 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_time);
1500 ccn_charbuf_append_string(lsa_data,"|");
1501
1502 char *temp_nl=(char *)malloc(20);
1503 memset(temp_nl,0,20);
1504 sprintf(temp_nl,"%d",adj_lsa->no_link);
1505 ccn_charbuf_append_string(lsa_data,temp_nl);
akmhoque03004e62012-09-06 01:12:28 -05001506 ccn_charbuf_append_string(lsa_data,"|");
akmhoqueb77b95f2013-02-08 12:28:47 -06001507 free(temp_nl);
akmhoque03004e62012-09-06 01:12:28 -05001508
1509 ccn_charbuf_append_string(lsa_data,adj_lsa->body);
1510
1511
1512 }
1513 else if(res == HT_NEW_ENTRY)
1514 {
1515 hashtb_delete(e);
1516 }
1517
1518 hashtb_end(e);
1519}
akmhoqueffacaa82012-09-13 17:48:30 -05001520
akmhoque3171d652012-11-13 11:44:33 -06001521void
1522make_name_lsa_invalid(struct name_prefix *np,int ls_type, long int ls_id)
1523{
1524
1525 if ( nlsr->debugging )
1526 printf("make_name_lsa_invalid called \n");
1527 if ( nlsr->detailed_logging )
1528 writeLogg(__FILE__,__FUNCTION__,__LINE__,"make_name_lsa_invalid called \n");
1529
1530
1531 char lst[2];
1532 memset(lst,0,2);
1533 sprintf(lst,"%d",ls_type);
1534
1535 char lsid[10];
1536 memset(lsid,0,10);
1537 sprintf(lsid,"%ld",ls_id);
1538
1539
1540 char *key=(char *)malloc(strlen(np->name)+1+strlen(lst)+1+strlen(lsid)+1);
1541 memset(key,0,strlen(np->name)+1+strlen(lst)+1+strlen(lsid)+1);
1542
1543
1544 make_name_lsa_key(key, np->name,ls_type,ls_id);
akmhoqueb77b95f2013-02-08 12:28:47 -06001545
1546 if ( nlsr->debugging )
1547 printf("Key:%s Length:%d\n",key,(int)strlen(key));
akmhoque3171d652012-11-13 11:44:33 -06001548
1549 struct nlsa *nlsa;
1550
1551 struct hashtb_enumerator ee;
1552 struct hashtb_enumerator *e = &ee;
1553
1554 int res;
1555
1556 hashtb_start(nlsr->lsdb->name_lsdb, e);
1557 res = hashtb_seek(e, key,strlen(key) , 0);
1558
1559 if( res == HT_OLD_ENTRY )
1560 {
1561 nlsa=e->data;
1562
1563 nlsa->header->isValid=0;
1564
1565 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1566 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1567 write_log_for_name_lsa(nlsa);
1568 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1569
1570 hashtb_delete(e);
1571 }
1572 else if( res == HT_NEW_ENTRY )
1573 {
1574 hashtb_delete(e);
1575 }
1576 hashtb_end(e);
1577
1578 if ( nlsr->debugging )
1579 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1580 if ( nlsr->detailed_logging )
1581 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1582
1583 set_new_lsdb_version();
1584
1585 if ( nlsr->debugging )
1586 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1587 if ( nlsr->detailed_logging )
1588 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1589
1590}
1591
akmhoquef5537b42013-02-22 08:33:32 -06001592
akmhoque866c2222013-02-12 10:49:33 -06001593int
1594delete_name_lsa(char *orig_router, char *name_prefix)
akmhoqueda5b6832012-09-13 22:33:55 -05001595{
akmhoqueb77b95f2013-02-08 12:28:47 -06001596
akmhoque7b791452012-10-30 11:24:56 -05001597 if ( nlsr->debugging )
1598 printf("delete_name_lsa called \n");
1599 if ( nlsr->detailed_logging )
1600 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_name_lsa called \n");
akmhoqueda5b6832012-09-13 22:33:55 -05001601
akmhoque866c2222013-02-12 10:49:33 -06001602 delete_npt_entry_by_router_and_name_prefix(orig_router, name_prefix);
akmhoquef5537b42013-02-22 08:33:32 -06001603
akmhoqueda5b6832012-09-13 22:33:55 -05001604
akmhoque7b791452012-10-30 11:24:56 -05001605 if ( nlsr->debugging )
1606 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1607 if ( nlsr->detailed_logging )
1608 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1609
akmhoqueda5b6832012-09-13 22:33:55 -05001610 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -05001611
1612 if ( nlsr->debugging )
1613 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1614 if ( nlsr->detailed_logging )
1615 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueda5b6832012-09-13 22:33:55 -05001616
akmhoqueda5b6832012-09-13 22:33:55 -05001617
1618 return 0;
1619}
1620
akmhoquef5537b42013-02-22 08:33:32 -06001621
akmhoque866c2222013-02-12 10:49:33 -06001622int delete_adj_lsa()
akmhoqueda5b6832012-09-13 22:33:55 -05001623{
akmhoque866c2222013-02-12 10:49:33 -06001624
akmhoque7b791452012-10-30 11:24:56 -05001625 if ( nlsr->debugging )
1626 printf("delete_adj_lsa called \n");
1627 if ( nlsr->detailed_logging )
1628 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_adj_lsa called \n");
akmhoque866c2222013-02-12 10:49:33 -06001629
akmhoque7b791452012-10-30 11:24:56 -05001630 if ( nlsr->debugging )
1631 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1632 if ( nlsr->detailed_logging )
1633 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1634
akmhoqueda5b6832012-09-13 22:33:55 -05001635 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -05001636
1637 if ( nlsr->debugging )
1638 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1639 if ( nlsr->detailed_logging )
1640 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1641
akmhoqueda5b6832012-09-13 22:33:55 -05001642
1643 if ( !nlsr->is_route_calculation_scheduled)
1644 {
1645 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
1646 nlsr->is_route_calculation_scheduled=1;
1647 }
1648
akmhoqueda5b6832012-09-13 22:33:55 -05001649 return 0;
1650}
1651
akmhoqueffacaa82012-09-13 17:48:30 -05001652void
1653refresh_name_lsdb(void)
1654{
akmhoque7b791452012-10-30 11:24:56 -05001655 if ( nlsr->debugging )
1656 printf("refresh_name_lsdb called \n");
1657 if ( nlsr->detailed_logging )
1658 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_name_lsdb called \n");
akmhoqueffacaa82012-09-13 17:48:30 -05001659
akmhoquef5537b42013-02-22 08:33:32 -06001660 char *time_stamp=get_current_timestamp_micro_v2();
akmhoqueffacaa82012-09-13 17:48:30 -05001661
1662 long int lsa_life_time;
1663
1664 int i, name_lsdb_element;
1665 struct nlsa *name_lsa;
1666
1667 struct hashtb_enumerator ee;
1668 struct hashtb_enumerator *e = &ee;
1669
1670 hashtb_start(nlsr->lsdb->name_lsdb, e);
1671 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
1672
1673 for(i=0;i<name_lsdb_element;i++)
1674 {
1675 name_lsa=e->data;
1676
1677 lsa_life_time=get_time_diff(time_stamp,name_lsa->header->orig_time);
akmhoque7b791452012-10-30 11:24:56 -05001678 if ( nlsr->debugging )
1679 printf("LSA Life Time: %ld \n",lsa_life_time);
1680 if ( nlsr->detailed_logging )
1681 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Life Time: %ld \n",lsa_life_time);
akmhoqueda5b6832012-09-13 22:33:55 -05001682
1683 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name) == 0)
1684 {
1685 if ( lsa_life_time > nlsr->lsa_refresh_time )
1686 {
akmhoque14b3f342012-09-14 10:39:02 -05001687 if ( name_lsa->header->isValid == NAME_LSA_VALID )
1688 {
akmhoque7b791452012-10-30 11:24:56 -05001689 if ( nlsr->debugging )
1690 printf("Own Name LSA need to be refrshed\n");
1691 if ( nlsr->detailed_logging )
1692 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Own Name LSA need to be refrshed\n");
akmhoquea98c2142012-10-25 15:22:24 -05001693
1694 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1695 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1696 write_log_for_name_lsa(name_lsa);
1697 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1698
akmhoquef5537b42013-02-22 08:33:32 -06001699 char *current_time_stamp=get_current_timestamp_micro_v2();
akmhoqueda5b6832012-09-13 22:33:55 -05001700
akmhoque14b3f342012-09-14 10:39:02 -05001701 free(name_lsa->header->orig_time);
akmhoquef5537b42013-02-22 08:33:32 -06001702 name_lsa->header->orig_time=(char *)calloc(strlen(current_time_stamp)+1,sizeof(char));
akmhoque14b3f342012-09-14 10:39:02 -05001703 memcpy(name_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
akmhoquea98c2142012-10-25 15:22:24 -05001704
1705 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1706 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
1707 write_log_for_name_lsa(name_lsa);
1708 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
akmhoqueda5b6832012-09-13 22:33:55 -05001709
akmhoque14b3f342012-09-14 10:39:02 -05001710 free(current_time_stamp);
akmhoque866c2222013-02-12 10:49:33 -06001711
1712 hashtb_next(e);
akmhoque14b3f342012-09-14 10:39:02 -05001713 }
1714 else
akmhoquef5537b42013-02-22 08:33:32 -06001715 {
akmhoque866c2222013-02-12 10:49:33 -06001716 delete_name_lsa(name_lsa->header->orig_router->name, name_lsa->name_prefix->name);
1717 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1718 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1719 write_log_for_name_lsa(name_lsa);
1720 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
akmhoquef5537b42013-02-22 08:33:32 -06001721
1722 destroy_name_lsa_component(name_lsa);
1723
akmhoque866c2222013-02-12 10:49:33 -06001724 hashtb_delete(e);
1725 i++;
akmhoque14b3f342012-09-14 10:39:02 -05001726 }
akmhoqueda5b6832012-09-13 22:33:55 -05001727
akmhoque7b791452012-10-30 11:24:56 -05001728 if ( nlsr->debugging )
1729 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1730 if ( nlsr->detailed_logging )
1731 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1732
akmhoqueda5b6832012-09-13 22:33:55 -05001733 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -05001734
1735 if ( nlsr->debugging )
1736 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1737 if ( nlsr->detailed_logging )
1738 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1739
1740
akmhoqueda5b6832012-09-13 22:33:55 -05001741
1742 print_name_lsdb();
akmhoque866c2222013-02-12 10:49:33 -06001743 }
1744 else
1745 {
1746 hashtb_next(e);
akmhoqueda5b6832012-09-13 22:33:55 -05001747 }
1748 }
1749 else
1750 {
1751 if ( lsa_life_time > nlsr->router_dead_interval )
1752 {
akmhoque7b791452012-10-30 11:24:56 -05001753 if ( nlsr->debugging )
1754 printf("Others Name LSA need to be deleted\n");
1755 if ( nlsr->detailed_logging )
1756 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Others Name LSA need to be deleted\n");
akmhoqueda5b6832012-09-13 22:33:55 -05001757
akmhoque866c2222013-02-12 10:49:33 -06001758 delete_name_lsa(name_lsa->header->orig_router->name, name_lsa->name_prefix->name);
1759 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1760 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1761 write_log_for_name_lsa(name_lsa);
1762 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
akmhoquef5537b42013-02-22 08:33:32 -06001763
1764 destroy_name_lsa_component(name_lsa);
akmhoque866c2222013-02-12 10:49:33 -06001765 hashtb_delete(e);
1766 i++;
1767 }
1768 else
1769 {
1770 hashtb_next(e);
akmhoqueda5b6832012-09-13 22:33:55 -05001771 }
1772 }
akmhoqueffacaa82012-09-13 17:48:30 -05001773 }
1774
1775 hashtb_end(e);
1776
1777 free(time_stamp);
akmhoqueda5b6832012-09-13 22:33:55 -05001778
akmhoqueffacaa82012-09-13 17:48:30 -05001779
1780}
1781
akmhoqueda5b6832012-09-13 22:33:55 -05001782void
akmhoqueffacaa82012-09-13 17:48:30 -05001783refresh_adj_lsdb(void)
1784{
akmhoqueffacaa82012-09-13 17:48:30 -05001785
akmhoque7b791452012-10-30 11:24:56 -05001786 if ( nlsr->debugging )
1787 printf("refresh_adj_lsdb called \n");
1788 if ( nlsr->detailed_logging )
1789 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_adj_lsdb called \n");
akmhoqueffacaa82012-09-13 17:48:30 -05001790
akmhoquef5537b42013-02-22 08:33:32 -06001791 char *time_stamp=get_current_timestamp_micro_v2();
1792
akmhoqueffacaa82012-09-13 17:48:30 -05001793 long int lsa_life_time;
1794
1795 int i, adj_lsdb_element;
1796 struct alsa *adj_lsa;
1797
1798 struct hashtb_enumerator ee;
1799 struct hashtb_enumerator *e = &ee;
1800
1801 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1802 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1803
1804 for(i=0;i<adj_lsdb_element;i++)
1805 {
1806 adj_lsa=e->data;
1807
akmhoqueb77b95f2013-02-08 12:28:47 -06001808 lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
akmhoque7b791452012-10-30 11:24:56 -05001809
1810 if ( nlsr->debugging )
1811 printf("LSA Life Time: %ld \n",lsa_life_time);
1812 if ( nlsr->detailed_logging )
1813 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Life Time: %ld \n",lsa_life_time);
akmhoqueffacaa82012-09-13 17:48:30 -05001814
akmhoqueda5b6832012-09-13 22:33:55 -05001815 if ( strcmp(adj_lsa->header->orig_router->name,nlsr->router_name) == 0)
1816 {
1817 if ( lsa_life_time > nlsr->lsa_refresh_time )
1818 {
akmhoque7b791452012-10-30 11:24:56 -05001819 if ( nlsr->debugging )
1820 printf("Own Adj LSA need to be refrshed\n");
1821 if ( nlsr->detailed_logging )
1822 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Own Adj LSA need to be refrshed\n");
akmhoqueda5b6832012-09-13 22:33:55 -05001823
akmhoquea98c2142012-10-25 15:22:24 -05001824 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1825 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
1826 write_log_for_adj_lsa(adj_lsa);
1827 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1828
akmhoquef5537b42013-02-22 08:33:32 -06001829 char *current_time_stamp=get_current_timestamp_micro_v2();
akmhoqueda5b6832012-09-13 22:33:55 -05001830
1831 free(adj_lsa->header->orig_time);
akmhoquef5537b42013-02-22 08:33:32 -06001832 adj_lsa->header->orig_time=(char *)calloc(strlen(current_time_stamp)+1,sizeof(char));
akmhoqueda5b6832012-09-13 22:33:55 -05001833 memcpy(adj_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1834
1835 free(current_time_stamp);
1836
akmhoquea98c2142012-10-25 15:22:24 -05001837 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1838 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
1839 write_log_for_adj_lsa(adj_lsa);
1840 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoqueda5b6832012-09-13 22:33:55 -05001841
akmhoque7b791452012-10-30 11:24:56 -05001842 if ( nlsr->debugging )
1843 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1844 if ( nlsr->detailed_logging )
1845 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1846
akmhoqueda5b6832012-09-13 22:33:55 -05001847 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -05001848
1849 if ( nlsr->debugging )
1850 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1851 if ( nlsr->detailed_logging )
1852 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueda5b6832012-09-13 22:33:55 -05001853
1854 print_adj_lsdb();
akmhoque866c2222013-02-12 10:49:33 -06001855
1856
akmhoqueda5b6832012-09-13 22:33:55 -05001857 }
akmhoque866c2222013-02-12 10:49:33 -06001858
1859 hashtb_next(e);
akmhoqueda5b6832012-09-13 22:33:55 -05001860 }
1861 else
1862 {
1863 if ( lsa_life_time > nlsr->router_dead_interval )
1864 {
akmhoque7b791452012-10-30 11:24:56 -05001865
1866 if ( nlsr->debugging )
1867 printf("Others Adj LSA need to be deleted\n");
1868 if ( nlsr->detailed_logging )
1869 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Others Adj LSA need to be deleted\n");
akmhoque866c2222013-02-12 10:49:33 -06001870 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1871 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
1872 write_log_for_adj_lsa(adj_lsa);
1873 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1874 delete_adj_lsa();
akmhoquef5537b42013-02-22 08:33:32 -06001875
1876 destroy_adj_lsa_component(adj_lsa);
akmhoque866c2222013-02-12 10:49:33 -06001877 hashtb_delete(e);
1878 i++;
1879
1880 }
1881 else
1882 {
1883 hashtb_next(e);
akmhoqueda5b6832012-09-13 22:33:55 -05001884 }
1885 }
akmhoquef5537b42013-02-22 08:33:32 -06001886
akmhoqueffacaa82012-09-13 17:48:30 -05001887 }
1888
1889 hashtb_end(e);
1890
1891 free(time_stamp);
akmhoqueffacaa82012-09-13 17:48:30 -05001892}
1893
akmhoqueb77b95f2013-02-08 12:28:47 -06001894
1895void
1896refresh_cor_lsdb(void)
1897{
1898
1899 if ( nlsr->debugging )
1900 printf("refresh_cor_lsdb called \n");
1901 if ( nlsr->detailed_logging )
1902 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_cor_lsdb called \n");
1903
akmhoquef5537b42013-02-22 08:33:32 -06001904 char *time_stamp=get_current_timestamp_micro_v2();
1905
akmhoqueb77b95f2013-02-08 12:28:47 -06001906 long int lsa_life_time;
1907
1908 int i, cor_lsdb_element;
1909 struct clsa *cor_lsa;
1910
1911 struct hashtb_enumerator ee;
1912 struct hashtb_enumerator *e = &ee;
1913
1914 hashtb_start(nlsr->lsdb->cor_lsdb, e);
1915 cor_lsdb_element=hashtb_n(nlsr->lsdb->cor_lsdb);
1916
1917 for(i=0;i<cor_lsdb_element;i++)
1918 {
1919 cor_lsa=e->data;
1920
1921 lsa_life_time=get_time_diff(time_stamp,cor_lsa->header->orig_time);
1922
1923 if ( nlsr->debugging )
1924 printf("LSA Life Time: %ld \n",lsa_life_time);
1925 if ( nlsr->detailed_logging )
1926 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Life Time: %ld \n",lsa_life_time);
1927
1928 if ( strcmp(cor_lsa->header->orig_router->name,nlsr->router_name) == 0)
1929 {
1930 if ( lsa_life_time > nlsr->lsa_refresh_time )
1931 {
1932 if ( nlsr->debugging )
1933 printf("Own Cor LSA need to be refrshed\n");
1934 if ( nlsr->detailed_logging )
1935 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Own Cor LSA need to be refrshed\n");
1936
akmhoquef5537b42013-02-22 08:33:32 -06001937 char *current_time_stamp=get_current_timestamp_micro_v2();
akmhoqueb77b95f2013-02-08 12:28:47 -06001938
1939 free(cor_lsa->header->orig_time);
1940 cor_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1941 memset(cor_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1942 memcpy(cor_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1943
1944 free(current_time_stamp);
1945
akmhoqueb77b95f2013-02-08 12:28:47 -06001946 if ( nlsr->debugging )
1947 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1948 if ( nlsr->detailed_logging )
1949 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1950
1951 set_new_lsdb_version();
1952
1953 if ( nlsr->debugging )
1954 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1955 if ( nlsr->detailed_logging )
1956 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1957
1958 print_adj_lsdb();
akmhoque866c2222013-02-12 10:49:33 -06001959 }
1960
1961 hashtb_next(e);
akmhoqueb77b95f2013-02-08 12:28:47 -06001962 }
1963 else
1964 {
1965 if ( lsa_life_time > nlsr->router_dead_interval )
1966 {
1967
1968 if ( nlsr->debugging )
1969 printf("Others Adj LSA need to be deleted\n");
1970 if ( nlsr->detailed_logging )
1971 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Others Adj LSA need to be deleted\n");
akmhoquef5537b42013-02-22 08:33:32 -06001972
1973 destroy_cor_lsa_component(cor_lsa);
akmhoqueb77b95f2013-02-08 12:28:47 -06001974 hashtb_delete(e);
1975 i++;
1976 }
akmhoque866c2222013-02-12 10:49:33 -06001977 else
1978 {
1979 hashtb_next(e);
1980 }
akmhoqueb77b95f2013-02-08 12:28:47 -06001981 }
1982
1983
akmhoque866c2222013-02-12 10:49:33 -06001984
akmhoqueb77b95f2013-02-08 12:28:47 -06001985 }
1986
1987 hashtb_end(e);
1988
1989 free(time_stamp);
1990}
1991
akmhoqueffacaa82012-09-13 17:48:30 -05001992int
1993refresh_lsdb(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1994{
1995 if(flags == CCN_SCHEDULE_CANCEL)
1996 {
1997 return -1;
1998 }
1999
2000 nlsr_lock();
akmhoqueda5b6832012-09-13 22:33:55 -05002001
akmhoque7b791452012-10-30 11:24:56 -05002002 if ( nlsr->debugging )
2003 printf("refresh_lsdb called\n");
2004 if ( nlsr->detailed_logging )
2005 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_lsdb called\n");
akmhoqueffacaa82012-09-13 17:48:30 -05002006
2007 refresh_name_lsdb();
2008 refresh_adj_lsdb();
2009
akmhoqueffacaa82012-09-13 17:48:30 -05002010 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoqueda5b6832012-09-13 22:33:55 -05002011
akmhoqueffacaa82012-09-13 17:48:30 -05002012 nlsr_unlock();
2013 return 0;
2014}
akmhoqueb77b95f2013-02-08 12:28:47 -06002015
2016void
2017write_adj_lsa_to_repo(char *repo_content_prefix, struct name_prefix *lsa_id)
2018{
2019 if ( nlsr->debugging )
2020 printf("write_adj_lsa_to_repo called\n");
2021 if ( nlsr->debugging )
2022 printf("Content Prefix: %s\n",repo_content_prefix);
2023
2024 struct ccn_charbuf *lsa_data=ccn_charbuf_create();
2025 get_adj_lsa_data(lsa_data,lsa_id);
2026 if ( nlsr->debugging )
akmhoque323b5e92013-02-21 13:55:15 -06002027 printf("Adj LSA Data: %s \n",ccn_charbuf_as_string(lsa_data));
akmhoqueb77b95f2013-02-08 12:28:47 -06002028
akmhoquef5537b42013-02-22 08:33:32 -06002029 char *data=calloc(strlen(ccn_charbuf_as_string(lsa_data))+1,sizeof(char));
2030 data=ccn_charbuf_as_string(lsa_data);
2031 data[strlen(data)]='\0';
2032
2033 write_data_to_repo(data, repo_content_prefix);
akmhoqueb77b95f2013-02-08 12:28:47 -06002034
akmhoque323b5e92013-02-21 13:55:15 -06002035
2036 ccn_charbuf_destroy(&lsa_data);
akmhoqueb77b95f2013-02-08 12:28:47 -06002037}
2038
2039void
2040write_name_lsa_to_repo(char *repo_content_prefix, struct name_prefix *lsa_id)
2041{
2042 if ( nlsr->debugging )
2043 printf("write_name_lsa_to_repo called\n");
2044 if ( nlsr->debugging )
2045 printf("Content Prefix: %s\n",repo_content_prefix);
2046
2047 struct ccn_charbuf *lsa_data=ccn_charbuf_create();
2048 get_name_lsa_data(lsa_data,lsa_id);
2049
2050 if ( nlsr->debugging )
2051 printf("Name LSA Data: %s \n",ccn_charbuf_as_string(lsa_data));
2052
akmhoquef5537b42013-02-22 08:33:32 -06002053 char *data=calloc(strlen(ccn_charbuf_as_string(lsa_data))+1,sizeof(char));
2054 data=ccn_charbuf_as_string(lsa_data);
2055 data[strlen(data)]='\0';
akmhoque323b5e92013-02-21 13:55:15 -06002056
akmhoquef5537b42013-02-22 08:33:32 -06002057 write_data_to_repo(data, repo_content_prefix);
akmhoqueb77b95f2013-02-08 12:28:47 -06002058
akmhoque4f85aab2013-02-21 13:58:50 -06002059 ccn_charbuf_destroy(&lsa_data);
akmhoqueb77b95f2013-02-08 12:28:47 -06002060}
2061
2062
2063void
2064write_name_lsdb_to_repo(char *slice_prefix)
2065{
2066 int i, name_lsdb_element;
2067
2068 struct nlsa *name_lsa;
2069 struct hashtb_enumerator ee;
2070 struct hashtb_enumerator *e = &ee;
2071
2072 hashtb_start(nlsr->lsdb->name_lsdb, e);
2073 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
2074
2075 for(i=0;i<name_lsdb_element;i++)
2076 {
2077 name_lsa=e->data;
2078
2079 char lst[2];
2080 memset(lst,0,2);
2081 sprintf(lst,"%d",name_lsa->header->ls_type);
2082
2083 char lsid[10];
2084 memset(lsid,0,10);
2085 sprintf(lsid,"%ld",name_lsa->header->ls_id);
2086
2087
2088 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
2089 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
2090
2091
2092 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
2093
2094 if ( nlsr->debugging )
2095 printf("Name LSA Key: %s \n",key);
2096
2097
akmhoquef5537b42013-02-22 08:33:32 -06002098 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 -06002099 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);
2100
2101 if ( nlsr->debugging )
2102 printf("Name LSA Repo Key: %s \n",repo_key);
2103
akmhoquef5537b42013-02-22 08:33:32 -06002104 struct name_prefix *lsaid=(struct name_prefix *)calloc(1,sizeof(struct name_prefix));
2105 lsaid->name=(char *)calloc(strlen(key)+1,sizeof(char));
2106 memcpy(lsaid->name,key,strlen(key)+1);
akmhoqueb77b95f2013-02-08 12:28:47 -06002107 lsaid->length=strlen(key)+1;
2108
2109
2110 write_name_lsa_to_repo(repo_key, lsaid);
2111
2112 free(key);
2113 free(repo_key);
2114 free(lsaid->name);
2115 free(lsaid);
2116
2117 hashtb_next(e);
2118 }
2119
2120 hashtb_end(e);
2121
2122
2123}
2124
2125void
2126print_cor_lsa(struct clsa *cor_lsa)
2127{
2128 if ( nlsr->debugging )
2129 {
2130 printf("-----------Cor LSA Content---------------\n");
2131 printf(" Origination Router : %s\n",cor_lsa->header->orig_router->name);
2132 printf(" Origination Router Length: %d\n",cor_lsa->header->orig_router->length);
2133 printf(" LS Type : %d\n",cor_lsa->header->ls_type);
2134 printf(" Origination Time : %s\n",cor_lsa->header->orig_time);
2135 printf(" LSA Data \n");
2136 printf(" Cor R: : %f\n",cor_lsa->cor_r);
2137 printf(" Cor Theta : %f\n",cor_lsa->cor_theta);
2138
2139 printf("\n");
2140 }
2141}
2142
2143void
2144print_cor_lsdb()
2145{
2146
2147 if ( nlsr->debugging )
2148 printf("print_cor_lsdb called \n");
2149
2150 struct hashtb_enumerator ee;
2151 struct hashtb_enumerator *e = &ee;
2152
2153 int i=1;
2154
2155 for (hashtb_start(nlsr->lsdb->cor_lsdb, e); e->key != NULL; hashtb_next(e))
2156 {
2157 if ( nlsr->debugging )
2158 printf("-----------Cor LSA (%d)---------------\n",i);
2159 struct clsa *cor_lsa=e->data;
2160 print_cor_lsa(cor_lsa);
2161 i++;
2162 }
2163 hashtb_end(e);
2164
2165 if ( nlsr->debugging )
2166 printf("\n");
2167 if ( nlsr->detailed_logging )
2168 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
2169}
2170
2171void
2172install_cor_lsa(struct clsa *cor_lsa)
2173{
2174 if ( nlsr->debugging )
2175 printf("install_cor_lsa called \n");
2176 if ( nlsr->detailed_logging )
2177 writeLogg(__FILE__,__FUNCTION__,__LINE__,"install_cor_lsa called \n");
2178
2179
2180 char *time_stamp=(char *)malloc(20);
2181 memset(time_stamp,0,20);
2182 get_current_timestamp_micro(time_stamp);
2183
2184
akmhoque569a93d2013-02-21 10:19:54 -06002185 char *key=(char *)malloc(cor_lsa->header->orig_router->length+4);
2186 memset(key,0,cor_lsa->header->orig_router->length+4);
akmhoqueb77b95f2013-02-08 12:28:47 -06002187 make_cor_lsa_key(key,cor_lsa);
2188
2189 if ( nlsr->debugging )
2190 printf("Cor LSA key: %s \n",key);
2191
2192 struct hashtb_enumerator ee;
2193 struct hashtb_enumerator *e = &ee;
2194 int res;
2195
2196 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2197 res = hashtb_seek(e, key, strlen(key), 0);
2198
2199 if ( res == HT_NEW_ENTRY )
2200 {
2201 if ( nlsr->debugging )
2202 printf("New Cor LSA... \n");
2203
akmhoque8876e982013-02-21 13:35:46 -06002204 struct clsa *new_cor_lsa;//=(struct clsa *)malloc(sizeof( struct clsa ));
akmhoqueb77b95f2013-02-08 12:28:47 -06002205 new_cor_lsa=e->data;
2206 new_cor_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
2207
2208 new_cor_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
2209 new_cor_lsa->header->orig_router->name=(char *)malloc(strlen(cor_lsa->header->orig_router->name)+1);
2210 memset(new_cor_lsa->header->orig_router->name,0,strlen(cor_lsa->header->orig_router->name)+1);
2211 memcpy(new_cor_lsa->header->orig_router->name,cor_lsa->header->orig_router->name,strlen(cor_lsa->header->orig_router->name)+1);
2212 new_cor_lsa->header->orig_router->length=cor_lsa->header->orig_router->length;
2213
2214 new_cor_lsa->header->orig_time=(char *)malloc(strlen(cor_lsa->header->orig_time)+1); //free
2215 memset(new_cor_lsa->header->orig_time,0,strlen(cor_lsa->header->orig_time)+1);
2216 memcpy(new_cor_lsa->header->orig_time,cor_lsa->header->orig_time,strlen(cor_lsa->header->orig_time)+1);
2217
2218 new_cor_lsa->header->ls_type=cor_lsa->header->ls_type;
2219
2220 new_cor_lsa->cor_r=cor_lsa->cor_r;
2221 new_cor_lsa->cor_theta=cor_lsa->cor_theta;
2222 }
2223 else if ( res == HT_OLD_ENTRY )
2224 {
2225 if ( nlsr->debugging )
2226 printf("Cor LSA exists (Old)... \n");
2227 }
2228 hashtb_end(e);
2229
akmhoquef5537b42013-02-22 08:33:32 -06002230 free(key);
akmhoqueb77b95f2013-02-08 12:28:47 -06002231
2232}
2233
2234void
2235build_cor_lsa(struct clsa *cor_lsa, double cor_r, double cor_theta)
2236{
2237 cor_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
2238 cor_lsa->header->ls_type=LS_TYPE_COR;
2239
akmhoquef5537b42013-02-22 08:33:32 -06002240 char *time_stamp=get_current_timestamp_micro_v2();
akmhoqueb77b95f2013-02-08 12:28:47 -06002241 cor_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1); //free
2242 memset(cor_lsa->header->orig_time,0,strlen(time_stamp)+1);
2243 memcpy(cor_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
2244 free(time_stamp);
2245
akmhoquef5537b42013-02-22 08:33:32 -06002246 cor_lsa->header->orig_router=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
2247 cor_lsa->header->orig_router->name=(char *)calloc(strlen(nlsr->router_name)+1,sizeof(char));
akmhoqueb77b95f2013-02-08 12:28:47 -06002248 memset(cor_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
2249 memcpy(cor_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
2250 cor_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
2251
2252 cor_lsa->cor_r=cor_r;
2253 cor_lsa->cor_theta=cor_theta;
2254
2255}
2256
2257void
2258build_others_cor_lsa(struct clsa *cor_lsa,char *orig_router, int ls_type,char *orig_time, double cor_r, double cor_theta)
2259{
2260 cor_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
2261 cor_lsa->header->ls_type=ls_type;
2262
2263 cor_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
2264 memset(cor_lsa->header->orig_time,0,strlen(orig_time)+1);
akmhoque569a93d2013-02-21 10:19:54 -06002265 memcpy(cor_lsa->header->orig_time,orig_time,strlen(orig_time));
akmhoqueb77b95f2013-02-08 12:28:47 -06002266
2267 cor_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
2268 cor_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
2269 memset(cor_lsa->header->orig_router->name,0,strlen(orig_router)+1);
akmhoque569a93d2013-02-21 10:19:54 -06002270 memcpy(cor_lsa->header->orig_router->name,orig_router,strlen(orig_router));
akmhoqueb77b95f2013-02-08 12:28:47 -06002271 cor_lsa->header->orig_router->length=strlen(orig_router)+1;
2272
2273 cor_lsa->cor_r=cor_r;
2274 cor_lsa->cor_theta=cor_theta;
2275
2276}
2277
2278void
2279build_and_install_others_cor_lsa(char *orig_router,int ls_type,char *orig_time, double cor_r, double cor_theta)
2280{
2281 struct clsa *cor_lsa=(struct clsa *)malloc(sizeof( struct clsa ));
2282 build_others_cor_lsa(cor_lsa,orig_router,ls_type, orig_time, cor_r, cor_theta);
2283 install_cor_lsa(cor_lsa);
2284
2285 print_cor_lsdb();
akmhoque569a93d2013-02-21 10:19:54 -06002286
akmhoquef5537b42013-02-22 08:33:32 -06002287 destroy_cor_lsa(cor_lsa);
akmhoqueb77b95f2013-02-08 12:28:47 -06002288}
2289
2290
2291void
2292build_and_install_cor_lsa()
2293{
2294
2295
2296
2297 struct clsa *cor_lsa=(struct clsa *)malloc(sizeof( struct clsa ));
2298
2299 build_cor_lsa(cor_lsa,nlsr->cor_r,nlsr->cor_theta);
2300 install_cor_lsa(cor_lsa);
2301
2302 write_cor_lsa_to_repo(cor_lsa);
akmhoquef5537b42013-02-22 08:33:32 -06002303 destroy_cor_lsa(cor_lsa);
akmhoqueb77b95f2013-02-08 12:28:47 -06002304
2305 print_cor_lsdb();
akmhoqueb77b95f2013-02-08 12:28:47 -06002306
2307}
2308
2309void
2310get_cor_lsa_data(struct ccn_charbuf *lsa_data,char *cor_lsa_key)
2311{
2312 if ( nlsr->debugging )
2313 printf("get_cor_lsa_data called \n");
2314 if ( nlsr->detailed_logging )
2315 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_adj_lsa_data called \n");
2316
2317 struct clsa *cor_lsa=(struct clsa*)malloc(sizeof(struct clsa ));
2318
2319 struct hashtb_enumerator ee;
2320 struct hashtb_enumerator *e = &ee;
2321 int res;
2322
2323 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2324 res = hashtb_seek(e, cor_lsa_key, strlen(cor_lsa_key), 0);
2325
2326 if( res == HT_OLD_ENTRY )
2327 {
2328 cor_lsa=e->data;
2329
2330 if ( nlsr->debugging )
2331 printf("Cor LSA found \n");
2332 if ( nlsr->detailed_logging )
2333 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Cor LSA found \n");
2334
2335 ccn_charbuf_append_string(lsa_data,cor_lsa->header->orig_router->name);
2336 ccn_charbuf_append_string(lsa_data,"|");
2337
2338 char *temp_length=(char *)malloc(20);
2339 memset(temp_length,0,20);
2340 sprintf(temp_length,"%d",cor_lsa->header->orig_router->length);
2341 ccn_charbuf_append_string(lsa_data,temp_length);
2342 ccn_charbuf_append_string(lsa_data,"|");
2343 free(temp_length);
2344
2345 char *temp_ltype=(char *)malloc(20);
2346 memset(temp_ltype,0,20);
2347 sprintf(temp_ltype,"%d",cor_lsa->header->ls_type);
2348 ccn_charbuf_append_string(lsa_data,temp_ltype);
2349 ccn_charbuf_append_string(lsa_data,"|");
2350 free(temp_ltype);
2351
2352 ccn_charbuf_append_string(lsa_data,cor_lsa->header->orig_time);
2353 ccn_charbuf_append_string(lsa_data,"|");
2354
2355 char *cor_r=(char *)malloc(20);
2356 memset(cor_r,0,20);
2357 sprintf(cor_r,"%f",cor_lsa->cor_r);
2358 ccn_charbuf_append_string(lsa_data,cor_r);
2359 ccn_charbuf_append_string(lsa_data,"|");
2360 free(cor_r);
2361
2362 char *cor_theta=(char *)malloc(20);
2363 memset(cor_theta,0,20);
2364 sprintf(cor_theta,"%f",cor_lsa->cor_theta);
2365 ccn_charbuf_append_string(lsa_data,cor_theta);
2366 ccn_charbuf_append_string(lsa_data,"|");
2367 free(cor_theta);
2368
2369 }
2370 else if(res == HT_NEW_ENTRY)
2371 {
2372 hashtb_delete(e);
2373 }
2374
2375 hashtb_end(e);
2376}
2377
2378void
2379write_cor_lsa_to_repo(struct clsa *cor_lsa)
2380{
2381
2382
2383 if ( nlsr->debugging )
2384 printf("write_cor_lsa_to_repo called\n");
2385
2386
akmhoquef5537b42013-02-22 08:33:32 -06002387 char *key=(char *)calloc(cor_lsa->header->orig_router->length+4,sizeof(char));
akmhoqueb77b95f2013-02-08 12:28:47 -06002388 make_cor_lsa_key(key,cor_lsa);
2389
2390 struct ccn_charbuf *lsa_data=ccn_charbuf_create();
2391 get_cor_lsa_data(lsa_data,key);
akmhoque323b5e92013-02-21 13:55:15 -06002392
akmhoqueb77b95f2013-02-08 12:28:47 -06002393 if ( nlsr->debugging )
akmhoque0800eda2013-02-21 14:17:52 -06002394 printf("Cor LSA Data: %s \n",ccn_charbuf_as_string(lsa_data));
2395
akmhoqueb77b95f2013-02-08 12:28:47 -06002396 char *lst=(char *)malloc(20);
2397 memset(lst,0,20);
2398 sprintf(lst,"%d",cor_lsa->header->ls_type);
akmhoquef5537b42013-02-22 08:33:32 -06002399 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 -06002400 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);
2401
2402 if ( nlsr->debugging )
2403 printf("Cor LSA Repo Key: %s \n",repo_key);
2404
akmhoquef5537b42013-02-22 08:33:32 -06002405 char *data=calloc(strlen(ccn_charbuf_as_string(lsa_data))+1,sizeof(char));
2406 data=ccn_charbuf_as_string(lsa_data);
2407 data[strlen(data)]='\0';
2408
2409 write_data_to_repo(data, repo_key);
akmhoque0800eda2013-02-21 14:17:52 -06002410
akmhoqueb77b95f2013-02-08 12:28:47 -06002411
2412
2413
2414 free(lst);
2415 free(key);
2416 free(repo_key);
akmhoque4f85aab2013-02-21 13:58:50 -06002417 ccn_charbuf_destroy(&lsa_data);
akmhoqueb77b95f2013-02-08 12:28:47 -06002418}
2419
2420void
2421make_cor_lsa_key_by_router_name(char *key,char *router_name)
2422{
2423 memcpy(key+strlen(key),router_name,strlen(router_name));
2424 memcpy(key+strlen(key),"/",1);
2425 char ls_type[2];
2426 sprintf(ls_type,"%d",LS_TYPE_COR);
2427 memcpy(key+strlen(key),ls_type,strlen(ls_type));
2428 key[strlen(key)]='\0';
2429}
2430
2431
2432double
2433get_hyperbolic_r(char *router)
2434{
2435 double ret=-1.0;
2436 char *cor_lsa_key=(char *)calloc(strlen(router)+4,sizeof(char));
2437 make_cor_lsa_key_by_router_name(cor_lsa_key,router);
2438
2439
2440 struct clsa *cor_lsa;
2441 struct hashtb_enumerator ee;
2442 struct hashtb_enumerator *e = &ee;
2443 int res;
2444
2445 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2446 res = hashtb_seek(e, cor_lsa_key, strlen(cor_lsa_key), 0);
2447
2448 if ( res == HT_OLD_ENTRY)
2449 {
2450 cor_lsa=e->data;
2451 ret=cor_lsa->cor_r;
2452 }
2453 else if(res == HT_NEW_ENTRY)
2454 {
2455 hashtb_delete(e);
2456 }
2457
2458 hashtb_end(e);
2459
2460 free(cor_lsa_key);
2461 return ret;
2462}
2463
2464double
2465get_hyperbolic_theta(char *router)
2466{
akmhoquef5537b42013-02-22 08:33:32 -06002467 double ret=-1.0;
akmhoqueb77b95f2013-02-08 12:28:47 -06002468 char *cor_lsa_key=(char *)calloc(strlen(router)+4,sizeof(char));
2469 make_cor_lsa_key_by_router_name(cor_lsa_key,router);
2470
2471 struct clsa *cor_lsa;
2472 struct hashtb_enumerator ee;
2473 struct hashtb_enumerator *e = &ee;
2474 int res;
2475
2476 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2477 res = hashtb_seek(e, cor_lsa_key, strlen(cor_lsa_key), 0);
2478
2479 if ( res == HT_OLD_ENTRY)
2480 {
2481 cor_lsa=e->data;
2482 ret=cor_lsa->cor_theta;
2483 }
2484 else if(res == HT_NEW_ENTRY)
2485 {
2486 hashtb_delete(e);
2487 }
2488
2489 hashtb_end(e);
2490
2491 free(cor_lsa_key);
2492 return ret;
2493}
akmhoquead584782013-02-22 10:56:03 -06002494
2495
2496void
2497destroy_name_lsdb(void)
2498{
2499 int i, name_lsdb_element;
2500 struct nlsa *name_lsa;
2501
2502 struct hashtb_enumerator ee;
2503 struct hashtb_enumerator *e = &ee;
2504
2505 hashtb_start(nlsr->lsdb->name_lsdb, e);
2506 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
2507
2508 for(i=0;i<name_lsdb_element;i++)
2509 {
2510 name_lsa=e->data;
2511 destroy_name_lsa_component(name_lsa);
2512 hashtb_next(e);
2513 }
2514
2515 hashtb_end(e);
2516
2517 if ( nlsr->lsdb->name_lsdb )
2518 hashtb_destroy(&nlsr->lsdb->name_lsdb);
2519}
2520
2521void
2522destroy_adj_lsdb(void)
2523{
2524 int i, adj_lsdb_element;
2525 struct alsa *adj_lsa;
2526
2527 struct hashtb_enumerator ee;
2528 struct hashtb_enumerator *e = &ee;
2529
2530 hashtb_start(nlsr->lsdb->adj_lsdb, e);
2531 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
2532
2533 for(i=0;i<adj_lsdb_element;i++)
2534 {
2535 adj_lsa=e->data;
2536 destroy_adj_lsa_component(adj_lsa);
2537 hashtb_next(e);
2538 }
2539
2540 hashtb_end(e);
2541 if(nlsr->lsdb->adj_lsdb )
2542 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
2543}
2544
2545void
2546destroy_cor_lsdb(void)
2547{
2548 int i, cor_lsdb_element;
2549 struct clsa *cor_lsa;
2550
2551 struct hashtb_enumerator ee;
2552 struct hashtb_enumerator *e = &ee;
2553
2554 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2555 cor_lsdb_element=hashtb_n(nlsr->lsdb->cor_lsdb);
2556
2557 for(i=0;i<cor_lsdb_element;i++)
2558 {
2559 cor_lsa=e->data;
2560 destroy_cor_lsa_component(cor_lsa);
2561 hashtb_next(e);
2562 }
2563
2564 hashtb_end(e);
2565 if(nlsr->lsdb->cor_lsdb )
2566 hashtb_destroy(&nlsr->lsdb->cor_lsdb);
2567}
2568
2569void
2570destroy_lsdb(void)
2571{
2572 destroy_name_lsdb();
2573 destroy_adj_lsdb();
2574 destroy_cor_lsdb();
2575
2576 if ( nlsr->lsdb->lsdb_version )
2577 free(nlsr->lsdb->lsdb_version);
2578 if ( nlsr->lsdb )
2579 free(nlsr->lsdb);
2580}