blob: feffb18b7ab7a1ab261d2c799fc0afe22c1d0ae7 [file] [log] [blame]
akmhoquef1161eb2012-08-21 09:37:21 -04001#include<stdio.h>
2#include<string.h>
3#include<stdlib.h>
4#include <unistd.h>
5#include <getopt.h>
6#include <sys/time.h>
7#include <assert.h>
8#ifdef HAVE_CONFIG_H
9#include <config.h>
10#endif
11
12
13#include <ccn/ccn.h>
14#include <ccn/uri.h>
15#include <ccn/keystore.h>
16#include <ccn/signing.h>
17#include <ccn/schedule.h>
18#include <ccn/hashtb.h>
19
20#include "nlsr.h"
akmhoque03004e62012-09-06 01:12:28 -050021#include "nlsr_ndn.h"
akmhoquef1161eb2012-08-21 09:37:21 -040022#include "nlsr_lsdb.h"
akmhoquef71d9082012-08-22 12:51:53 -040023#include "utility.h"
akmhoque03004e62012-09-06 01:12:28 -050024#include "nlsr_npl.h"
akmhoque53f64222012-09-05 13:57:51 -050025#include "nlsr_adl.h"
akmhoque29c1db52012-09-07 14:47:43 -050026#include "nlsr_route.h"
akmhoque3560cb62012-09-09 10:52:30 -050027#include "nlsr_npt.h"
akmhoqueb77b95f2013-02-08 12:28:47 -060028#include "nlsr_sync.h"
akmhoque438b07e2012-08-21 10:13:57 -040029
akmhoque0ab71642013-02-21 10:10:33 -060030/**
31* LSDB version is updated to last updated timestamp
32*/
33
akmhoquef71d9082012-08-22 12:51:53 -040034void
akmhoque53f64222012-09-05 13:57:51 -050035set_new_lsdb_version(void)
akmhoquef71d9082012-08-22 12:51:53 -040036{
akmhoquef71d9082012-08-22 12:51:53 -040037
akmhoque6682ca32013-02-22 00:29:35 -060038 char *time_stamp=get_current_timestamp_micro_v2();
akmhoque03004e62012-09-06 01:12:28 -050039 free(nlsr->lsdb->lsdb_version);
akmhoque6682ca32013-02-22 00:29:35 -060040 nlsr->lsdb->lsdb_version=(char *)calloc(strlen(time_stamp)+1,sizeof(char));
akmhoque03004e62012-09-06 01:12:28 -050041 memcpy(nlsr->lsdb->lsdb_version,time_stamp,strlen(time_stamp)+1);
akmhoque03004e62012-09-06 01:12:28 -050042 free(time_stamp);
akmhoquef71d9082012-08-22 12:51:53 -040043}
44
akmhoque0ab71642013-02-21 10:10:33 -060045/**
46* Make key for storing Name LSA in Name LSDB
47*/
48
akmhoque03004e62012-09-06 01:12:28 -050049void
50make_name_lsa_key(char *key, char *orig_router, int ls_type, long int ls_id)
51{
52
akmhoque03004e62012-09-06 01:12:28 -050053 char lst[2];
54 memset(lst,0,2);
55 sprintf(lst,"%d",ls_type);
56
57 char lsid[10];
58 memset(lsid,0,10);
59 sprintf(lsid,"%ld",ls_id);
60
akmhoqueeacf44f2013-02-22 01:02:13 -060061 memcpy(key,orig_router,strlen(orig_router));
akmhoque03004e62012-09-06 01:12:28 -050062 memcpy(key+strlen(key),"/",1);
63 memcpy(key+strlen(key),lst,strlen(lst));
64 memcpy(key+strlen(key),"/",1);
65 memcpy(key+strlen(key),lsid,strlen(lsid));
akmhoqueeacf44f2013-02-22 01:02:13 -060066 key[strlen(key)]='\0';
67
akmhoqueb77b95f2013-02-08 12:28:47 -060068 if ( nlsr->debugging )
69 printf("name LSA Key: %s\n", key);
70}
71
akmhoque0ed6d982013-02-22 14:28:01 -060072
akmhoque0ab71642013-02-21 10:10:33 -060073/**
74* Make content name prefix for Name LSA to store in repo
75*/
akmhoqueb77b95f2013-02-08 12:28:47 -060076
77void
akmhoque0ab71642013-02-21 10:10:33 -060078make_name_lsa_prefix_for_repo(char *key, char *orig_router, int ls_type,
79 long int ls_id,char *orig_time,char *slice_prefix)
akmhoqueb77b95f2013-02-08 12:28:47 -060080{
akmhoque0ab71642013-02-21 10:10:33 -060081 sprintf(key,"%s%s/lsType.%d/lsId.%ld/%s",slice_prefix, orig_router, ls_type,
82 ls_id, orig_time);
akmhoqueeacf44f2013-02-22 01:02:13 -060083 key[strlen(key)]='\0';
akmhoqueb77b95f2013-02-08 12:28:47 -060084 if ( nlsr->debugging )
85 printf("Name LSA prefix for repo content: %s\n",key);
86}
87
akmhoque0ab71642013-02-21 10:10:33 -060088/**
89* Make content name prefix for Adj LSA to store in repo
90*/
91
akmhoqueb77b95f2013-02-08 12:28:47 -060092void
akmhoque0ab71642013-02-21 10:10:33 -060093make_adj_lsa_prefix_for_repo(char *key, char *orig_router, int ls_type,
94 char *orig_time,char *slice_prefix)
akmhoqueb77b95f2013-02-08 12:28:47 -060095{
96
97 sprintf(key,"%s%s/lsType.%d/%s",slice_prefix,orig_router,ls_type, orig_time );
akmhoqueeacf44f2013-02-22 01:02:13 -060098 key[strlen(key)]='\0';
akmhoqueb77b95f2013-02-08 12:28:47 -060099 if ( nlsr->debugging )
100 printf("Name LSA prefix for repo content:%s\n",key);
101}
102
akmhoque0ab71642013-02-21 10:10:33 -0600103/**
104* Make content name prefix for Cor LSA to store in repo
105*/
106
akmhoqueb77b95f2013-02-08 12:28:47 -0600107void
akmhoque0ab71642013-02-21 10:10:33 -0600108make_cor_lsa_prefix_for_repo(char *key, char *orig_router, int ls_type,
109 char *orig_time,char *slice_prefix)
akmhoqueb77b95f2013-02-08 12:28:47 -0600110{
111
112 sprintf(key,"%s%s/lsType.%d/%s",slice_prefix,orig_router,ls_type, orig_time );
akmhoqueeacf44f2013-02-22 01:02:13 -0600113 key[strlen(key)]='\0';
akmhoqueb77b95f2013-02-08 12:28:47 -0600114 if ( nlsr->debugging )
115 printf("Cor LSA prefix for repo content:%s\n",key);
akmhoque03004e62012-09-06 01:12:28 -0500116}
akmhoque53f64222012-09-05 13:57:51 -0500117
akmhoque0ab71642013-02-21 10:10:33 -0600118/**
119* Build name lsa for all name prefixes in Name Prefix List (NPL). Intsall Name
120* LSA in Name LSDB for router itself.
121*/
122
akmhoqueeacf44f2013-02-22 01:02:13 -0600123void
akmhoquef5537b42013-02-22 08:33:32 -0600124destroy_name_lsa_component(struct nlsa * name_lsa)
akmhoqueeacf44f2013-02-22 01:02:13 -0600125{
126 if ( name_lsa->header->orig_router->name )
127 free(name_lsa->header->orig_router->name);
128 if ( name_lsa->header->orig_router )
129 free(name_lsa->header->orig_router);
akmhoquef5537b42013-02-22 08:33:32 -0600130 if ( name_lsa->header->orig_time )
131 free(name_lsa->header->orig_time);
akmhoqueeacf44f2013-02-22 01:02:13 -0600132 if ( name_lsa->header )
133 free(name_lsa->header);
akmhoquef5537b42013-02-22 08:33:32 -0600134
akmhoqueeacf44f2013-02-22 01:02:13 -0600135 if ( name_lsa->name_prefix->name )
136 free(name_lsa->name_prefix->name);
137 if ( name_lsa->name_prefix )
138 free(name_lsa->name_prefix);
akmhoquef5537b42013-02-22 08:33:32 -0600139}
140
141void
142destroy_name_lsa(struct nlsa * name_lsa)
143{
144 destroy_name_lsa_component(name_lsa);
akmhoqueeacf44f2013-02-22 01:02:13 -0600145 if ( name_lsa )
146 free(name_lsa);
147}
akmhoque0ab71642013-02-21 10:10:33 -0600148
akmhoquef5537b42013-02-22 08:33:32 -0600149void
150destroy_adj_lsa_component(struct alsa * adj_lsa)
151{
152 if ( adj_lsa->header->orig_router->name )
153 free(adj_lsa->header->orig_router->name);
154 if ( adj_lsa->header->orig_router )
155 free(adj_lsa->header->orig_router);
156 if ( adj_lsa->header->orig_time )
157 free(adj_lsa->header->orig_time);
158 if ( adj_lsa->header )
159 free(adj_lsa->header);
160
161 if ( adj_lsa->body )
162 free(adj_lsa->body);
163}
164
165void
166destroy_adj_lsa(struct alsa * adj_lsa)
167{
168
169 destroy_adj_lsa_component(adj_lsa);
170 if ( adj_lsa )
171 free(adj_lsa);
172}
173
174void
175destroy_cor_lsa_component(struct clsa * cor_lsa)
176{
177 if ( cor_lsa->header->orig_router->name )
178 free(cor_lsa->header->orig_router->name);
179 if ( cor_lsa->header->orig_router )
180 free(cor_lsa->header->orig_router);
181 if ( cor_lsa->header->orig_time )
182 free(cor_lsa->header->orig_time);
183 if ( cor_lsa->header )
184 free(cor_lsa->header);
185}
186
187void
188destroy_cor_lsa(struct clsa * cor_lsa)
189{
190
191 destroy_cor_lsa_component(cor_lsa);
192 if ( cor_lsa )
193 free(cor_lsa);
194}
195
196
197
akmhoquef71d9082012-08-22 12:51:53 -0400198void
akmhoque53f64222012-09-05 13:57:51 -0500199build_and_install_name_lsas(void)
akmhoquef71d9082012-08-22 12:51:53 -0400200{
akmhoque7b791452012-10-30 11:24:56 -0500201 if ( nlsr->debugging )
202 printf("build_and_install_name_lsas called \n");
203 if ( nlsr->detailed_logging )
204 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_name_lsas called\n");
akmhoquef71d9082012-08-22 12:51:53 -0400205
akmhoquef71d9082012-08-22 12:51:53 -0400206 int i, npl_element;
akmhoque3171d652012-11-13 11:44:33 -0600207 struct name_prefix_list_entry *npe;
akmhoquef71d9082012-08-22 12:51:53 -0400208
209 struct hashtb_enumerator ee;
210 struct hashtb_enumerator *e = &ee;
211
212 hashtb_start(nlsr->npl, e);
213 npl_element=hashtb_n(nlsr->npl);
214
215 for(i=0;i<npl_element;i++)
216 {
akmhoque3171d652012-11-13 11:44:33 -0600217 npe=e->data;
akmhoque53f64222012-09-05 13:57:51 -0500218 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
akmhoque3171d652012-11-13 11:44:33 -0600219 build_name_lsa(name_lsa,npe->np);
akmhoque03004e62012-09-06 01:12:28 -0500220
akmhoquef71d9082012-08-22 12:51:53 -0400221 install_name_lsa(name_lsa);
akmhoque3171d652012-11-13 11:44:33 -0600222 update_nlsa_id_for_name_in_npl(npe->np,name_lsa->header->ls_id);
akmhoqueeacf44f2013-02-22 01:02:13 -0600223 destroy_name_lsa(name_lsa);
akmhoquef71d9082012-08-22 12:51:53 -0400224 hashtb_next(e);
225 }
226
akmhoque53f64222012-09-05 13:57:51 -0500227 hashtb_end(e);
akmhoque3171d652012-11-13 11:44:33 -0600228
229 print_name_prefix_from_npl();
230
231}
232
akmhoque0ab71642013-02-21 10:10:33 -0600233/**
234* Build and Install one Name LSA Use ful for API
235*/
236
akmhoque3171d652012-11-13 11:44:33 -0600237void
238build_and_install_single_name_lsa(struct name_prefix *np)
239{
240 if ( nlsr->debugging )
241 printf("build_and_install_single_name_lsa called \n");
242 if ( nlsr->detailed_logging )
243 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_single_name_lsa called\n");
244
245 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
246 build_name_lsa(name_lsa,np);
247
248 install_name_lsa(name_lsa);
249 update_nlsa_id_for_name_in_npl(np,name_lsa->header->ls_id);
akmhoqueeacf44f2013-02-22 01:02:13 -0600250 destroy_name_lsa(name_lsa);
akmhoque3171d652012-11-13 11:44:33 -0600251 print_name_prefix_from_npl();
akmhoquef71d9082012-08-22 12:51:53 -0400252
akmhoquef71d9082012-08-22 12:51:53 -0400253}
254
akmhoque53f64222012-09-05 13:57:51 -0500255void
256build_name_lsa(struct nlsa *name_lsa, struct name_prefix *np)
257{
akmhoque53f64222012-09-05 13:57:51 -0500258 name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header ));
259 name_lsa->header->ls_type=LS_TYPE_NAME;
akmhoqueeacf44f2013-02-22 01:02:13 -0600260
261 char *time_stamp=get_current_timestamp_micro_v2();
akmhoque03004e62012-09-06 01:12:28 -0500262 name_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1); //free
akmhoque53f64222012-09-05 13:57:51 -0500263 memset(name_lsa->header->orig_time,0,strlen(time_stamp)+1);
264 memcpy(name_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
akmhoque03004e62012-09-06 01:12:28 -0500265
266 free(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -0500267
268 name_lsa->header->ls_id=++nlsr->nlsa_id;
akmhoqueeacf44f2013-02-22 01:02:13 -0600269 name_lsa->header->orig_router=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
270 name_lsa->header->orig_router->name=(char *)calloc(strlen(nlsr->router_name)+1,sizeof(char));
akmhoque03004e62012-09-06 01:12:28 -0500271 memcpy(name_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
272 name_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
akmhoque53f64222012-09-05 13:57:51 -0500273 name_lsa->header->isValid=1;
274
275
276 name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoqueeacf44f2013-02-22 01:02:13 -0600277 name_lsa->name_prefix->name=(char *)calloc(np->length,sizeof(char));
akmhoque53f64222012-09-05 13:57:51 -0500278 memcpy(name_lsa->name_prefix->name,np->name,np->length);
279 name_lsa->name_prefix->length=np->length;
280
281}
282
akmhoque53f64222012-09-05 13:57:51 -0500283void
284install_name_lsa(struct nlsa *name_lsa)
285{
akmhoque53f64222012-09-05 13:57:51 -0500286
akmhoque0ab71642013-02-21 10:10:33 -0600287 char lst[2];
288 memset(lst,0,2);
289 sprintf(lst,"%d",name_lsa->header->ls_type);
akmhoqueffacaa82012-09-13 17:48:30 -0500290
akmhoque0ab71642013-02-21 10:10:33 -0600291 char lsid[10];
292 memset(lsid,0,10);
293 sprintf(lsid,"%ld",name_lsa->header->ls_id);
akmhoque3171d652012-11-13 11:44:33 -0600294
akmhoque0ab71642013-02-21 10:10:33 -0600295 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
296 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
297 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 -0500298
akmhoque0ab71642013-02-21 10:10:33 -0600299 if ( nlsr->debugging )
300 printf("Key:%s Length:%d\n",key,(int)strlen(key));
301 if ( nlsr->detailed_logging )
302 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Key:%s Length:%d\n",key,(int)strlen(key));
303
akmhoquef5537b42013-02-22 08:33:32 -0600304 struct nlsa *new_name_lsa;
akmhoque53f64222012-09-05 13:57:51 -0500305
akmhoque0ab71642013-02-21 10:10:33 -0600306 struct hashtb_enumerator ee;
307 struct hashtb_enumerator *e = &ee;
308 int res;
akmhoque53f64222012-09-05 13:57:51 -0500309
akmhoque0ab71642013-02-21 10:10:33 -0600310 hashtb_start(nlsr->lsdb->name_lsdb, e);
311 res = hashtb_seek(e, key, strlen(key), 0);
akmhoque53f64222012-09-05 13:57:51 -0500312
akmhoque0ab71642013-02-21 10:10:33 -0600313 if(res == HT_NEW_ENTRY )
akmhoque3560cb62012-09-09 10:52:30 -0500314 {
akmhoqueffacaa82012-09-13 17:48:30 -0500315
akmhoquef5537b42013-02-22 08:33:32 -0600316 if ( name_lsa->header->isValid == 1 )
317 {
akmhoque7b791452012-10-30 11:24:56 -0500318 if ( nlsr->debugging )
319 printf("New Name LSA... Adding to LSDB\n");
320 if ( nlsr->detailed_logging )
321 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Name LSA... Adding to LSDB\n");
322
323
akmhoqueffacaa82012-09-13 17:48:30 -0500324 new_name_lsa = e->data;
akmhoque8876e982013-02-21 13:35:46 -0600325
akmhoque54d86112013-02-21 16:42:34 -0600326
akmhoque0ab71642013-02-21 10:10:33 -0600327 new_name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header ));
akmhoqueffacaa82012-09-13 17:48:30 -0500328 new_name_lsa->header->ls_type=name_lsa->header->ls_type;
329
330 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
331 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
akmhoque8876e982013-02-21 13:35:46 -0600332 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 -0500333
334 new_name_lsa->header->ls_id=name_lsa->header->ls_id;
akmhoque0ab71642013-02-21 10:10:33 -0600335 new_name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoqueffacaa82012-09-13 17:48:30 -0500336 new_name_lsa->header->orig_router->name=(char *)malloc(name_lsa->header->orig_router->length);
337 memcpy(new_name_lsa->header->orig_router->name,name_lsa->header->orig_router->name,name_lsa->header->orig_router->length);
338 new_name_lsa->header->orig_router->length=name_lsa->header->orig_router->length;
339 new_name_lsa->header->isValid=name_lsa->header->isValid;
340
341
akmhoque0ab71642013-02-21 10:10:33 -0600342 new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoqueffacaa82012-09-13 17:48:30 -0500343 new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
344 memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
345 new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
akmhoque8876e982013-02-21 13:35:46 -0600346
akmhoqueffacaa82012-09-13 17:48:30 -0500347
akmhoque7b791452012-10-30 11:24:56 -0500348 if ( nlsr->debugging )
349 {
350 printf("New Name LSA Added....\n");
351 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
352 }
353 if ( nlsr->detailed_logging )
354 {
355 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Name LSA Added....\n");
356 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
357 }
akmhoque54d86112013-02-21 16:42:34 -0600358
akmhoqueffacaa82012-09-13 17:48:30 -0500359 set_new_lsdb_version();
akmhoque54d86112013-02-21 16:42:34 -0600360
akmhoque7b791452012-10-30 11:24:56 -0500361 if ( nlsr->debugging )
362 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
363 if ( nlsr->detailed_logging )
364 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500365
akmhoque9e9fc722012-09-26 14:03:25 -0500366
akmhoquede61ba92012-09-20 22:19:12 -0500367 int num_next_hop=get_number_of_next_hop(new_name_lsa->header->orig_router->name);
akmhoquede61ba92012-09-20 22:19:12 -0500368 if ( num_next_hop < 0 )
akmhoque3560cb62012-09-09 10:52:30 -0500369 {
akmhoquede61ba92012-09-20 22:19:12 -0500370 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 -0500371 if ( check == HT_NEW_ENTRY )
372 {
akmhoque7b791452012-10-30 11:24:56 -0500373 if ( nlsr->debugging )
374 printf("Added in npt \n");
375 if ( nlsr->detailed_logging )
376 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
akmhoqueffacaa82012-09-13 17:48:30 -0500377 }
akmhoque3560cb62012-09-09 10:52:30 -0500378 }
akmhoqueffacaa82012-09-13 17:48:30 -0500379 else
akmhoque3560cb62012-09-09 10:52:30 -0500380 {
akmhoquede61ba92012-09-20 22:19:12 -0500381 int *faces=malloc(num_next_hop*sizeof(int));
382 int *route_costs=malloc(num_next_hop*sizeof(int));
383 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name,faces,route_costs);
akmhoque7b791452012-10-30 11:24:56 -0500384 if ( nlsr->debugging )
385 {
386 printf("Printing from install_name_lsa \n");
387 int j;
388 for(j=0;j<num_next_hop;j++)
389 printf("Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
390 }
391 if ( nlsr->detailed_logging )
392 {
393 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Printing from install_name_lsa \n");
394 int j;
395 for(j=0;j<num_next_hop;j++)
396 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
397 }
akmhoquede61ba92012-09-20 22:19:12 -0500398 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 -0500399 if ( check == HT_NEW_ENTRY )
400 {
akmhoque7b791452012-10-30 11:24:56 -0500401 if ( nlsr->debugging )
402 printf("Added in npt \n");
403 if ( nlsr->detailed_logging )
404 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
akmhoqueffacaa82012-09-13 17:48:30 -0500405 }
akmhoquede61ba92012-09-20 22:19:12 -0500406 free(faces);
407 free(route_costs);
akmhoqueffacaa82012-09-13 17:48:30 -0500408
akmhoque3560cb62012-09-09 10:52:30 -0500409 }
akmhoquede61ba92012-09-20 22:19:12 -0500410
akmhoque9e9fc722012-09-26 14:03:25 -0500411 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
412 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
akmhoquef5537b42013-02-22 08:33:32 -0600413 write_log_for_name_lsa(new_name_lsa);
akmhoque9e9fc722012-09-26 14:03:25 -0500414 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500415
akmhoque54d86112013-02-21 16:42:34 -0600416 //free(time_stamp);
akmhoquef5537b42013-02-22 08:33:32 -0600417 }
418 else
419 {
420 hashtb_delete(e);
421 }
akmhoqueffacaa82012-09-13 17:48:30 -0500422
423 }
424 else if(res == HT_OLD_ENTRY)
425 {
426 new_name_lsa=e->data;
akmhoque14b3f342012-09-14 10:39:02 -0500427 if(strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time)<0)
akmhoqueffacaa82012-09-13 17:48:30 -0500428 {
akmhoque7b791452012-10-30 11:24:56 -0500429 if ( nlsr->debugging )
430 printf("Older Adj LSA. Discarded... \n");
431 if ( nlsr->detailed_logging )
432 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Older Adj LSA. Discarded...\n");
akmhoque14b3f342012-09-14 10:39:02 -0500433 }
434 else if( strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time) == 0 )
435 {
akmhoque7b791452012-10-30 11:24:56 -0500436 if ( nlsr->debugging )
437 printf("Duplicate Adj LSA. Discarded... \n");
438 if ( nlsr->detailed_logging )
439 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Duplicate Adj LSA. Discarded...\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500440 }
441 else
442 {
443 if ( name_lsa->header->isValid == 0 )
444 {
445 // have to call to delete npt table entry
akmhoque3cced642012-09-24 16:20:20 -0500446 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 -0500447
448 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name)!= 0)
449 {
akmhoque9e9fc722012-09-26 14:03:25 -0500450 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
451 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
452 write_log_for_name_lsa(new_name_lsa);
453 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
454
akmhoqueffacaa82012-09-13 17:48:30 -0500455 hashtb_delete(e);
akmhoque7b791452012-10-30 11:24:56 -0500456 if ( nlsr->debugging )
457 printf("isValid bit not set for Router %s so LSA Deleted from LSDB\n",name_lsa->header->orig_router->name);
458 if ( nlsr->detailed_logging )
459 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 -0500460 }
461 else
462 {
463 new_name_lsa->header->isValid=name_lsa->header->isValid;
464 free(new_name_lsa->header->orig_time);
465 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
466 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
467 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
468 }
akmhoque7b791452012-10-30 11:24:56 -0500469 if ( nlsr->debugging )
470 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
471 if ( nlsr->detailed_logging )
472 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500473 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500474 if ( nlsr->debugging )
475 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
476 if ( nlsr->detailed_logging )
477 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500478 }
479 else
480 {
481 int is_npt_update=0;
482 if ( strcmp(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name) != 0 )
483 {
484 is_npt_update=1;
akmhoque3cced642012-09-24 16:20:20 -0500485 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 -0500486 }
487
488 // copying LSA content with header
akmhoque9e9fc722012-09-26 14:03:25 -0500489 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
490 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
491 write_log_for_name_lsa(new_name_lsa);
492 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
493
akmhoqueffacaa82012-09-13 17:48:30 -0500494
495 free(new_name_lsa->header->orig_time);
496 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
497 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
akmhoque0ab71642013-02-21 10:10:33 -0600498 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time));
akmhoqueffacaa82012-09-13 17:48:30 -0500499
500 new_name_lsa->header->isValid=name_lsa->header->isValid;
501
502 free(new_name_lsa->name_prefix->name);
503 free(new_name_lsa->name_prefix);
504 new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
505 new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
506 memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
507 new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
508
akmhoque9e9fc722012-09-26 14:03:25 -0500509
510 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
511 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
512 write_log_for_name_lsa(new_name_lsa);
513 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
514
akmhoque7b791452012-10-30 11:24:56 -0500515 if ( nlsr->debugging )
516 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
517 if ( nlsr->detailed_logging )
518 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
519
akmhoqueffacaa82012-09-13 17:48:30 -0500520 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500521
522 if ( nlsr->debugging )
523 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
524 if ( nlsr->detailed_logging )
525 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
526
akmhoqueffacaa82012-09-13 17:48:30 -0500527
528 if( is_npt_update == 1 )
529 {
akmhoquede61ba92012-09-20 22:19:12 -0500530 //struct hashtb *face_list;
531 int num_next_hop=get_number_of_next_hop(new_name_lsa->header->orig_router->name);
532 if ( num_next_hop < 0 )
akmhoqueffacaa82012-09-13 17:48:30 -0500533 {
akmhoquede61ba92012-09-20 22:19:12 -0500534
535 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 -0500536 if ( check == HT_NEW_ENTRY )
537 {
akmhoque7b791452012-10-30 11:24:56 -0500538 if ( nlsr->debugging )
539 printf("Added in npt \n");
540 if ( nlsr->detailed_logging )
541 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
akmhoqueffacaa82012-09-13 17:48:30 -0500542 }
543 }
544 else
545 {
akmhoquede61ba92012-09-20 22:19:12 -0500546 int *faces=malloc(num_next_hop*sizeof(int));
547 int *route_costs=malloc(num_next_hop*sizeof(int));
548 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name,faces,route_costs);
akmhoque7b791452012-10-30 11:24:56 -0500549
550 if ( nlsr->debugging )
551 {
552 printf("Printing from install_name_lsa \n");
553 int j;
554 for(j=0;j<num_next_hop;j++)
akmhoquede61ba92012-09-20 22:19:12 -0500555 printf("Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
akmhoque7b791452012-10-30 11:24:56 -0500556 }
557 if ( nlsr->detailed_logging )
558 {
559 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Printing from install_name_lsa \n");
560 int j;
561 for(j=0;j<num_next_hop;j++)
562 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
563 }
564
akmhoquede61ba92012-09-20 22:19:12 -0500565
566 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 -0500567 if ( check == HT_NEW_ENTRY )
568 {
akmhoque7b791452012-10-30 11:24:56 -0500569 if ( nlsr->debugging )
570 printf("Added in npt \n");
571 if ( nlsr->detailed_logging )
572 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
akmhoqueffacaa82012-09-13 17:48:30 -0500573 }
akmhoquede61ba92012-09-20 22:19:12 -0500574 free(faces);
575 free(route_costs);
576
akmhoqueffacaa82012-09-13 17:48:30 -0500577 }
akmhoquede61ba92012-09-20 22:19:12 -0500578
akmhoqueffacaa82012-09-13 17:48:30 -0500579 }
580 }
581 }
582
akmhoque3560cb62012-09-09 10:52:30 -0500583 }
akmhoque53f64222012-09-05 13:57:51 -0500584
akmhoqueffacaa82012-09-13 17:48:30 -0500585 hashtb_end(e);
akmhoque53f64222012-09-05 13:57:51 -0500586
akmhoquef5537b42013-02-22 08:33:32 -0600587 free(key);
akmhoque53f64222012-09-05 13:57:51 -0500588}
589
akmhoque9e9fc722012-09-26 14:03:25 -0500590void
591write_log_for_name_lsa(struct nlsa *name_lsa)
592{
593
594 writeLogg(__FILE__,__FUNCTION__,__LINE__,"-----------Name LSA Content---------------\n");
595 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s\n",name_lsa->header->orig_router->name);
596 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router Length: %d\n",name_lsa->header->orig_router->length);
akmhoque34b99f92012-09-27 12:24:27 -0500597 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Type: %d\n",name_lsa->header->ls_type);
598 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Id: %ld\n",name_lsa->header->ls_id);
599 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Time: %s\n",name_lsa->header->orig_time);
600 writeLogg(__FILE__,__FUNCTION__,__LINE__," Is Valid: %d\n",name_lsa->header->isValid);
akmhoque9e9fc722012-09-26 14:03:25 -0500601 writeLogg(__FILE__,__FUNCTION__,__LINE__," LSA Data \n");
602 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix: %s\n",name_lsa->name_prefix->name);
603 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix Length: %d\n",name_lsa->name_prefix->length);
604 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
605}
akmhoque53f64222012-09-05 13:57:51 -0500606
akmhoque03004e62012-09-06 01:12:28 -0500607void
608print_name_lsa(struct nlsa *name_lsa)
akmhoque53f64222012-09-05 13:57:51 -0500609{
akmhoque53f64222012-09-05 13:57:51 -0500610
akmhoque7b791452012-10-30 11:24:56 -0500611 if ( nlsr->debugging )
612 {
613 printf("-----------Name LSA Content---------------\n");
614 printf(" Origination Router : %s\n",name_lsa->header->orig_router->name);
615 printf(" Origination Router Length: %d\n",name_lsa->header->orig_router->length);
616 printf(" LS Type : %d\n",name_lsa->header->ls_type);
617 printf(" LS Id : %ld\n",name_lsa->header->ls_id);
618 printf(" Origination Time : %s\n",name_lsa->header->orig_time);
619 printf(" Is Valid : %d\n",name_lsa->header->isValid);
620 printf(" LSA Data \n");
621 printf(" Name Prefix: : %s\n",name_lsa->name_prefix->name);
622 printf(" Name Prefix Length : %d\n",name_lsa->name_prefix->length);
623
624 printf("\n");
625 }
akmhoquef71d9082012-08-22 12:51:53 -0400626}
627
628void
629print_name_lsdb(void)
630{
akmhoque7b791452012-10-30 11:24:56 -0500631 if ( nlsr->debugging )
632 printf("print_name_lsdb called \n");
akmhoque53f64222012-09-05 13:57:51 -0500633 int i, name_lsdb_element;
akmhoquef71d9082012-08-22 12:51:53 -0400634 struct nlsa *name_lsa;
635
636 struct hashtb_enumerator ee;
637 struct hashtb_enumerator *e = &ee;
638
639 hashtb_start(nlsr->lsdb->name_lsdb, e);
akmhoque53f64222012-09-05 13:57:51 -0500640 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
akmhoquef71d9082012-08-22 12:51:53 -0400641
akmhoque53f64222012-09-05 13:57:51 -0500642 for(i=0;i<name_lsdb_element;i++)
akmhoquef71d9082012-08-22 12:51:53 -0400643 {
akmhoque7b791452012-10-30 11:24:56 -0500644 if ( nlsr->debugging )
645 printf("-----------Name LSA (%d)---------------\n",i+1);
akmhoquef71d9082012-08-22 12:51:53 -0400646 name_lsa=e->data;
akmhoque53f64222012-09-05 13:57:51 -0500647 print_name_lsa(name_lsa);
akmhoquef71d9082012-08-22 12:51:53 -0400648 hashtb_next(e);
649 }
650
651 hashtb_end(e);
652
akmhoque3171d652012-11-13 11:44:33 -0600653 if ( nlsr->debugging )
654 printf("\n");
655 if ( nlsr->detailed_logging )
656 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoque53f64222012-09-05 13:57:51 -0500657}
658
akmhoque03004e62012-09-06 01:12:28 -0500659
660void
661build_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 -0500662{
akmhoque7b791452012-10-30 11:24:56 -0500663 if ( nlsr->debugging )
664 printf("build_and_install_others_name_lsa called \n");
665 if ( nlsr->detailed_logging )
666 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_others_name_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500667
akmhoque03004e62012-09-06 01:12:28 -0500668 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
669 build_others_name_lsa(name_lsa,orig_router,ls_type,ls_id,orig_time, isValid,np);
670 print_name_lsa(name_lsa);
671 install_name_lsa(name_lsa);
672 print_name_lsdb();
akmhoquede61ba92012-09-20 22:19:12 -0500673 print_npt();
akmhoque53f64222012-09-05 13:57:51 -0500674
akmhoquef5537b42013-02-22 08:33:32 -0600675 destroy_name_lsa(name_lsa);
akmhoque53f64222012-09-05 13:57:51 -0500676
677}
678
679void
akmhoque03004e62012-09-06 01:12:28 -0500680build_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 -0500681{
akmhoque7b791452012-10-30 11:24:56 -0500682 if ( nlsr->debugging )
683 printf("build_others_name_lsa called\n");
684 if ( nlsr->detailed_logging )
685 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_others_name_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500686
akmhoquef5537b42013-02-22 08:33:32 -0600687 name_lsa->header=(struct nlsa_header *)calloc(1,sizeof(struct nlsa_header ));
akmhoque03004e62012-09-06 01:12:28 -0500688 name_lsa->header->ls_type=LS_TYPE_NAME;
akmhoque53f64222012-09-05 13:57:51 -0500689
akmhoquef5537b42013-02-22 08:33:32 -0600690 name_lsa->header->orig_time=(char *)calloc(strlen(orig_time)+1,sizeof(char));
691 memcpy(name_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
akmhoque53f64222012-09-05 13:57:51 -0500692
akmhoque03004e62012-09-06 01:12:28 -0500693 name_lsa->header->ls_id=ls_id;
akmhoquef5537b42013-02-22 08:33:32 -0600694 name_lsa->header->orig_router=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
695 name_lsa->header->orig_router->name=(char *)calloc(strlen(orig_router)+1,sizeof(char));
696 memcpy(name_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
akmhoque03004e62012-09-06 01:12:28 -0500697 name_lsa->header->orig_router->length=strlen(orig_router)+1;
698 name_lsa->header->isValid=isValid;
akmhoque53f64222012-09-05 13:57:51 -0500699
akmhoquef5537b42013-02-22 08:33:32 -0600700 name_lsa->name_prefix=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
701 name_lsa->name_prefix->name=(char *)calloc(strlen(np)+1,sizeof(char));
702 memcpy(name_lsa->name_prefix->name,np,strlen(np)+1);
akmhoque03004e62012-09-06 01:12:28 -0500703 name_lsa->name_prefix->length=strlen(np)+1;
akmhoquef71d9082012-08-22 12:51:53 -0400704}
akmhoqued79438d2012-08-27 13:31:42 -0500705
706
akmhoqueb77b95f2013-02-08 12:28:47 -0600707void
708make_cor_lsa_key(char *key,struct clsa *cor_lsa)
709{
710 memcpy(key+strlen(key),cor_lsa->header->orig_router->name,cor_lsa->header->orig_router->length);
711 memcpy(key+strlen(key),"/",1);
712 char ls_type[2];
713 sprintf(ls_type,"%d",cor_lsa->header->ls_type);
714 memcpy(key+strlen(key),ls_type,strlen(ls_type));
715 key[strlen(key)]='\0';
716}
akmhoqueffacaa82012-09-13 17:48:30 -0500717
718
akmhoque53f64222012-09-05 13:57:51 -0500719void
720make_adj_lsa_key(char *key,struct alsa *adj_lsa)
akmhoqued79438d2012-08-27 13:31:42 -0500721{
akmhoque53f64222012-09-05 13:57:51 -0500722 memcpy(key+strlen(key),adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
723 memcpy(key+strlen(key),"/",1);
724 char ls_type[2];
725 sprintf(ls_type,"%d",adj_lsa->header->ls_type);
726 memcpy(key+strlen(key),ls_type,strlen(ls_type));
727 key[strlen(key)]='\0';
728}
729
akmhoque03004e62012-09-06 01:12:28 -0500730int
731build_and_install_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
732{
akmhoqueffacaa82012-09-13 17:48:30 -0500733 if(flags == CCN_SCHEDULE_CANCEL)
734 {
735 return -1;
736 }
737
738 nlsr_lock();
739
akmhoque7b791452012-10-30 11:24:56 -0500740 if ( nlsr->debugging )
741 printf("build_and_install_adj_lsa called \n");
742 if ( nlsr->detailed_logging )
743 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_adj_lsa called \n");
744
745 if ( nlsr->debugging )
746 printf("adj_build_flag = %d \n",nlsr->adj_build_flag);
747 if ( nlsr->detailed_logging )
748 writeLogg(__FILE__,__FUNCTION__,__LINE__,"adj_build_flag = %d \n",nlsr->adj_build_flag);
akmhoque03004e62012-09-06 01:12:28 -0500749
750 if(nlsr->adj_build_flag > 0)
751 {
akmhoque7b791452012-10-30 11:24:56 -0500752 if ( nlsr->debugging )
753 printf("is_adj_lsa_build = %d \n",is_adj_lsa_build());
754 if ( nlsr->detailed_logging )
755 writeLogg(__FILE__,__FUNCTION__,__LINE__,"is_adj_lsa_build = %d \n",is_adj_lsa_build());
756
akmhoque03004e62012-09-06 01:12:28 -0500757 if ( is_adj_lsa_build()> 0)
758 {
759 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
760 build_adj_lsa(adj_lsa);
761 install_adj_lsa(adj_lsa);
762
akmhoqueb77b95f2013-02-08 12:28:47 -0600763 char lst[2];
764 memset(lst,0,2);
765 sprintf(lst,"%d",LS_TYPE_ADJ);
766
akmhoquef5537b42013-02-22 08:33:32 -0600767 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));
768 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 -0600769
770 if ( nlsr->debugging )
771 printf("Adj LSA Repo Key: %s \n",repo_key);
772
akmhoquef5537b42013-02-22 08:33:32 -0600773 char *key=(char *)calloc(adj_lsa->header->orig_router->length+5,sizeof(char));
akmhoqueb77b95f2013-02-08 12:28:47 -0600774 make_adj_lsa_key(key,adj_lsa);
775 if ( nlsr->debugging )
776 printf("Adj LSA: %s \n",key);
777
akmhoquef5537b42013-02-22 08:33:32 -0600778 struct name_prefix *lsaid=(struct name_prefix *)calloc(1,sizeof(struct name_prefix));
779 lsaid->name=(char *)calloc(strlen(key)+1,sizeof(char));
780 memcpy(lsaid->name,key,strlen(key)+1);
akmhoqueb77b95f2013-02-08 12:28:47 -0600781 lsaid->length=strlen(key)+1;
782
783
784 write_adj_lsa_to_repo(repo_key, lsaid);
785
akmhoquef5537b42013-02-22 08:33:32 -0600786 free(key);
akmhoqueb77b95f2013-02-08 12:28:47 -0600787 free(repo_key);
788 free(lsaid->name);
789 free(lsaid);
790
akmhoquef5537b42013-02-22 08:33:32 -0600791 destroy_adj_lsa(adj_lsa);
akmhoqueb77b95f2013-02-08 12:28:47 -0600792
akmhoque03004e62012-09-06 01:12:28 -0500793 nlsr->adj_build_flag=0;
794 print_adj_lsdb();
795 }
796 else
797 {
akmhoque7b791452012-10-30 11:24:56 -0500798 if ( nlsr->debugging )
799 printf("Can not build adj LSA now\n");
800 if ( nlsr->detailed_logging )
801 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Can not build adj LSA now\n");
akmhoque03004e62012-09-06 01:12:28 -0500802 }
803 }
804 nlsr->is_build_adj_lsa_sheduled=0;
akmhoqueffacaa82012-09-13 17:48:30 -0500805
806 nlsr_unlock();
807
akmhoque03004e62012-09-06 01:12:28 -0500808 return 0;
809}
810
811
812void
813build_adj_lsa(struct alsa * adj_lsa)
814{
akmhoque7b791452012-10-30 11:24:56 -0500815 if ( nlsr->debugging )
816 printf("build_adj_lsa called \n");
817 if ( nlsr->detailed_logging )
818 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_adj_lsa called \n");
akmhoque03004e62012-09-06 01:12:28 -0500819
820 int no_link=no_active_nbr();
akmhoque7b791452012-10-30 11:24:56 -0500821
akmhoque03004e62012-09-06 01:12:28 -0500822 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
823 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
824 adj_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1);
825 memset(adj_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
826 memcpy(adj_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
827 adj_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
828
829 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
830
akmhoque03004e62012-09-06 01:12:28 -0500831
akmhoquef5537b42013-02-22 08:33:32 -0600832 char *time_stamp=get_current_timestamp_micro_v2();
akmhoque03004e62012-09-06 01:12:28 -0500833 adj_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1);
834 memset(adj_lsa->header->orig_time,0,strlen(time_stamp)+1);
835 memcpy(adj_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
836 free(time_stamp);
837
akmhoque03004e62012-09-06 01:12:28 -0500838 adj_lsa->no_link=no_link;
akmhoque03004e62012-09-06 01:12:28 -0500839
840
841 struct ccn_charbuf *c=ccn_charbuf_create();
842 get_active_nbr_adj_data(c);
843 char *data=ccn_charbuf_as_string(c);
844
akmhoquef5537b42013-02-22 08:33:32 -0600845 adj_lsa->body=(char *)calloc(strlen(data)+1,sizeof(char));
846 //memset(adj_lsa->body,0,strlen(data)+1);
akmhoque03004e62012-09-06 01:12:28 -0500847 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
akmhoquef5537b42013-02-22 08:33:32 -0600848 data[strlen(data)]='\0';
akmhoque03004e62012-09-06 01:12:28 -0500849 ccn_charbuf_destroy(&c);
850
akmhoque03004e62012-09-06 01:12:28 -0500851 nlsr->adj_build_count++;
852
853
854}
855
856
akmhoque53f64222012-09-05 13:57:51 -0500857void
858install_adj_lsa(struct alsa * adj_lsa)
859{
akmhoqued79438d2012-08-27 13:31:42 -0500860
akmhoque7b791452012-10-30 11:24:56 -0500861 if ( nlsr->debugging )
862 printf("install_adj_lsa called \n");
863 if ( nlsr->detailed_logging )
864 writeLogg(__FILE__,__FUNCTION__,__LINE__,"install_adj_lsa called \n");
865
akmhoque03004e62012-09-06 01:12:28 -0500866
akmhoque53f64222012-09-05 13:57:51 -0500867
akmhoqueffacaa82012-09-13 17:48:30 -0500868
akmhoquef5537b42013-02-22 08:33:32 -0600869 char *key=(char *)calloc(adj_lsa->header->orig_router->length+4,sizeof(char));
akmhoqueffacaa82012-09-13 17:48:30 -0500870 make_adj_lsa_key(key,adj_lsa);
akmhoqueffacaa82012-09-13 17:48:30 -0500871
akmhoque8876e982013-02-21 13:35:46 -0600872 struct alsa *new_adj_lsa;//=(struct alsa*)malloc(sizeof(struct alsa ));
akmhoqueffacaa82012-09-13 17:48:30 -0500873
874 struct hashtb_enumerator ee;
875 struct hashtb_enumerator *e = &ee;
876 int res;
877
878 hashtb_start(nlsr->lsdb->adj_lsdb, e);
879 res = hashtb_seek(e, key, strlen(key), 0);
akmhoque53f64222012-09-05 13:57:51 -0500880
akmhoque03004e62012-09-06 01:12:28 -0500881
akmhoque53f64222012-09-05 13:57:51 -0500882
akmhoque62c0c192012-09-24 07:49:25 -0500883 if(res == HT_NEW_ENTRY)
akmhoque53f64222012-09-05 13:57:51 -0500884 {
akmhoque62c0c192012-09-24 07:49:25 -0500885 if ( adj_lsa->no_link > 0)
886 {
akmhoque3171d652012-11-13 11:44:33 -0600887 if ( nlsr->debugging )
888 printf("New ADJ LSA... Adding to LSDB\n");
889 if ( nlsr->detailed_logging )
890 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New ADJ LSA... Adding to LSDB\n");
akmhoque62c0c192012-09-24 07:49:25 -0500891 new_adj_lsa = e->data;
akmhoque53f64222012-09-05 13:57:51 -0500892
akmhoquef5537b42013-02-22 08:33:32 -0600893 new_adj_lsa->header=(struct alsa_header *)calloc(1,sizeof(struct alsa_header ));
akmhoque62c0c192012-09-24 07:49:25 -0500894 new_adj_lsa->header->ls_type=adj_lsa->header->ls_type;
akmhoquef5537b42013-02-22 08:33:32 -0600895 new_adj_lsa->header->orig_time=(char *)calloc(strlen(adj_lsa->header->orig_time)+1,sizeof(char));
akmhoque62c0c192012-09-24 07:49:25 -0500896 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 -0500897
akmhoquef5537b42013-02-22 08:33:32 -0600898 new_adj_lsa->header->orig_router=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
899 new_adj_lsa->header->orig_router->name=(char *)calloc(adj_lsa->header->orig_router->length,sizeof(char));
akmhoque62c0c192012-09-24 07:49:25 -0500900 memcpy(new_adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
901 new_adj_lsa->header->orig_router->length=adj_lsa->header->orig_router->length;
akmhoque53f64222012-09-05 13:57:51 -0500902
akmhoque62c0c192012-09-24 07:49:25 -0500903 new_adj_lsa->no_link=adj_lsa->no_link;
akmhoqueffacaa82012-09-13 17:48:30 -0500904
akmhoquef5537b42013-02-22 08:33:32 -0600905 new_adj_lsa->body=(char *)calloc(strlen(adj_lsa->body)+1,sizeof(char));
906 //memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
akmhoque62c0c192012-09-24 07:49:25 -0500907 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
908
909 add_next_hop_router(new_adj_lsa->header->orig_router->name);
910 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
akmhoque9e9fc722012-09-26 14:03:25 -0500911
912 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
913 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
914 write_log_for_adj_lsa(new_adj_lsa);
915 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoque62c0c192012-09-24 07:49:25 -0500916 }
917 else
918 {
919 hashtb_delete(e);
920 }
akmhoque53f64222012-09-05 13:57:51 -0500921
akmhoque7b791452012-10-30 11:24:56 -0500922 if ( nlsr->debugging )
923 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
924 if ( nlsr->detailed_logging )
925 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
926
akmhoque53f64222012-09-05 13:57:51 -0500927 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500928
929 if ( nlsr->debugging )
930 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
931 if ( nlsr->detailed_logging )
932 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
933
akmhoqueffacaa82012-09-13 17:48:30 -0500934 }
935 else if(res == HT_OLD_ENTRY)
936 {
937 new_adj_lsa = e->data;
938 if(strcmp(adj_lsa->header->orig_time,new_adj_lsa->header->orig_time)<=0)
939 {
akmhoque7b791452012-10-30 11:24:56 -0500940 if ( nlsr->debugging )
941 printf("Older/Duplicate Adj LSA. Discarded...\n");
942 if ( nlsr->detailed_logging )
943 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Older/Duplicate Adj LSA. Discarded...\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500944 }
945 else
946 {
akmhoqueffacaa82012-09-13 17:48:30 -0500947
akmhoque62c0c192012-09-24 07:49:25 -0500948 if ( adj_lsa->no_link > 0)
949 {
akmhoque9e9fc722012-09-26 14:03:25 -0500950 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
951 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
952 write_log_for_adj_lsa(new_adj_lsa);
953 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500954
akmhoque62c0c192012-09-24 07:49:25 -0500955 free(new_adj_lsa->header->orig_time);
akmhoquef5537b42013-02-22 08:33:32 -0600956 new_adj_lsa->header->orig_time=(char *)calloc(strlen(adj_lsa->header->orig_time)+1,sizeof(char));
akmhoque62c0c192012-09-24 07:49:25 -0500957 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
958
959 new_adj_lsa->no_link=adj_lsa->no_link;
akmhoqueffacaa82012-09-13 17:48:30 -0500960
akmhoquef5537b42013-02-22 08:33:32 -0600961 new_adj_lsa->body=(char *)calloc(strlen(adj_lsa->body)+1,sizeof(char));
962 //memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
akmhoque62c0c192012-09-24 07:49:25 -0500963 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
akmhoqueffacaa82012-09-13 17:48:30 -0500964
akmhoque62c0c192012-09-24 07:49:25 -0500965 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
akmhoque9e9fc722012-09-26 14:03:25 -0500966
967 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
968 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
969 write_log_for_adj_lsa(new_adj_lsa);
970 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoque62c0c192012-09-24 07:49:25 -0500971 }
972 else
973 {
akmhoque9e9fc722012-09-26 14:03:25 -0500974 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
975 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
976 write_log_for_adj_lsa(new_adj_lsa);
977 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
978
akmhoque62c0c192012-09-24 07:49:25 -0500979 hashtb_delete(e);
980 }
akmhoque7b791452012-10-30 11:24:56 -0500981
982 if ( nlsr->debugging )
983 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
984 if ( nlsr->detailed_logging )
985 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
986
akmhoqueffacaa82012-09-13 17:48:30 -0500987 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500988
989 if ( nlsr->debugging )
990 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
991 if ( nlsr->detailed_logging )
992 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500993 }
994
995 }
996 hashtb_end(e);
997
998 if ( !nlsr->is_route_calculation_scheduled )
999 {
1000 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
1001 nlsr->is_route_calculation_scheduled=1;
akmhoque53f64222012-09-05 13:57:51 -05001002 }
1003
akmhoque03004e62012-09-06 01:12:28 -05001004
akmhoquef5537b42013-02-22 08:33:32 -06001005 free(key);
akmhoqueffacaa82012-09-13 17:48:30 -05001006
akmhoquef5537b42013-02-22 08:33:32 -06001007 //free(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -05001008}
1009
1010void
akmhoque9e9fc722012-09-26 14:03:25 -05001011write_log_for_adj_lsa_body(const char *body, int no_link)
1012{
1013 int i=0;
akmhoquef5537b42013-02-22 08:33:32 -06001014 char *lsa_data=(char *)calloc(strlen(body)+1,sizeof(char));
1015 //memset( lsa_data,0,strlen(body)+1);
akmhoque9e9fc722012-09-26 14:03:25 -05001016 memcpy(lsa_data,body,strlen(body)+1);
1017 char *sep="|";
1018 char *rem;
1019 char *rtr_id;
1020 char *length;
akmhoqueb28579d2013-02-12 11:15:52 -06001021 //char *face;
akmhoque9e9fc722012-09-26 14:03:25 -05001022 char *metric;
1023
1024 if(no_link >0 )
1025 {
1026 rtr_id=strtok_r(lsa_data,sep,&rem);
1027 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -06001028 //face=strtok_r(NULL,sep,&rem);
akmhoque9e9fc722012-09-26 14:03:25 -05001029 metric=strtok_r(NULL,sep,&rem);
1030
1031 writeLogg(__FILE__,__FUNCTION__,__LINE__," Link %d \n",i+1);
akmhoque34b99f92012-09-27 12:24:27 -05001032 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router: %s \n",rtr_id);
1033 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router Length: %s \n",length);
akmhoque866c2222013-02-12 10:49:33 -06001034 //writeLogg(__FILE__,__FUNCTION__,__LINE__," Connecting Face: %s \n",face);
akmhoque34b99f92012-09-27 12:24:27 -05001035 writeLogg(__FILE__,__FUNCTION__,__LINE__," Metric: %s \n",metric);
akmhoque9e9fc722012-09-26 14:03:25 -05001036
1037
1038 for(i=1;i<no_link;i++)
1039 {
1040 rtr_id=strtok_r(NULL,sep,&rem);
1041 length=strtok_r(NULL,sep,&rem);
akmhoqueb28579d2013-02-12 11:15:52 -06001042 //face=strtok_r(NULL,sep,&rem);
akmhoque9e9fc722012-09-26 14:03:25 -05001043 metric=strtok_r(NULL,sep,&rem);
1044 writeLogg(__FILE__,__FUNCTION__,__LINE__," Link %d \n",i+1);
akmhoque34b99f92012-09-27 12:24:27 -05001045 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router: %s \n",rtr_id);
1046 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router Length: %s \n",length);
akmhoque866c2222013-02-12 10:49:33 -06001047 //writeLogg(__FILE__,__FUNCTION__,__LINE__," Connecting Face: %s \n",face);
akmhoque34b99f92012-09-27 12:24:27 -05001048 writeLogg(__FILE__,__FUNCTION__,__LINE__," Metric: %s \n",metric);
akmhoque9e9fc722012-09-26 14:03:25 -05001049
1050 }
1051 }
1052
akmhoquef5537b42013-02-22 08:33:32 -06001053 if(lsa_data)
1054 free(lsa_data);
akmhoque9e9fc722012-09-26 14:03:25 -05001055}
1056
1057
1058void
1059write_log_for_adj_lsa(struct alsa * adj_lsa)
1060{
1061 writeLogg(__FILE__,__FUNCTION__,__LINE__,"-----------Adj LSA Content---------------\n");
akmhoque34b99f92012-09-27 12:24:27 -05001062 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s\n",adj_lsa->header->orig_router->name);
1063 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
1064 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Type: %d\n",adj_lsa->header->ls_type);
1065 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Time: %s\n",adj_lsa->header->orig_time);
akmhoque9e9fc722012-09-26 14:03:25 -05001066 writeLogg(__FILE__,__FUNCTION__,__LINE__," Lsa Data:\n");
akmhoque34b99f92012-09-27 12:24:27 -05001067 writeLogg(__FILE__,__FUNCTION__,__LINE__," No of Link: %d\n",adj_lsa->no_link);
akmhoque9e9fc722012-09-26 14:03:25 -05001068
1069 write_log_for_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
1070
1071 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
1072
1073}
1074
1075void
akmhoque53f64222012-09-05 13:57:51 -05001076print_adj_lsa_body(const char *body, int no_link)
1077{
1078 int i=0;
akmhoquef5537b42013-02-22 08:33:32 -06001079 char *lsa_data=(char *)calloc(strlen(body)+1,sizeof(char));
1080 //memset( lsa_data,0,strlen(body)+1);
akmhoque53f64222012-09-05 13:57:51 -05001081 memcpy(lsa_data,body,strlen(body)+1);
1082 char *sep="|";
1083 char *rem;
1084 char *rtr_id;
1085 char *length;
akmhoqueb28579d2013-02-12 11:15:52 -06001086 //char *face;
akmhoque53f64222012-09-05 13:57:51 -05001087 char *metric;
1088
akmhoque53f64222012-09-05 13:57:51 -05001089 if(no_link >0 )
1090 {
1091 rtr_id=strtok_r(lsa_data,sep,&rem);
1092 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -06001093 //face=strtok_r(NULL,sep,&rem);
akmhoque53f64222012-09-05 13:57:51 -05001094 metric=strtok_r(NULL,sep,&rem);
1095
akmhoqueb77b95f2013-02-08 12:28:47 -06001096 if ( nlsr->debugging ) {
1097 printf(" Link %d \n",i+1);
1098 printf(" Neighbor : %s \n",rtr_id);
1099 printf(" Neighbor Length : %s \n",length);
akmhoque866c2222013-02-12 10:49:33 -06001100 //printf(" Connecting Face : %s \n",face);
akmhoqueb77b95f2013-02-08 12:28:47 -06001101 printf(" Metric : %s \n",metric);
1102 }
akmhoque53f64222012-09-05 13:57:51 -05001103
1104 for(i=1;i<no_link;i++)
1105 {
1106 rtr_id=strtok_r(NULL,sep,&rem);
1107 length=strtok_r(NULL,sep,&rem);
akmhoqueb28579d2013-02-12 11:15:52 -06001108 //face=strtok_r(NULL,sep,&rem);
akmhoque53f64222012-09-05 13:57:51 -05001109 metric=strtok_r(NULL,sep,&rem);
1110 printf(" Link %d \n",i+1);
1111 printf(" Neighbor : %s \n",rtr_id);
1112 printf(" Neighbor Length : %s \n",length);
akmhoque866c2222013-02-12 10:49:33 -06001113 //printf(" Connecting Face : %s \n",face);
akmhoque53f64222012-09-05 13:57:51 -05001114 printf(" Metric : %s \n",metric);
1115
1116 }
1117 }
1118
akmhoquef5537b42013-02-22 08:33:32 -06001119 if( lsa_data )
1120 free(lsa_data);
akmhoque53f64222012-09-05 13:57:51 -05001121}
1122
1123void
1124print_adj_lsa(struct alsa * adj_lsa)
1125{
akmhoque7b791452012-10-30 11:24:56 -05001126 if ( nlsr->debugging )
1127 {
1128 printf("-----------ADJ LSA Content---------------\n");
1129 printf(" Origination Router : %s\n",adj_lsa->header->orig_router->name);
1130 printf(" Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
1131 printf(" LS Type : %d\n",adj_lsa->header->ls_type);
1132 printf(" Origination Time : %s\n",adj_lsa->header->orig_time);
1133 printf(" Lsa Data:\n");
1134 printf(" No of Link : %d\n",adj_lsa->no_link);
akmhoque53f64222012-09-05 13:57:51 -05001135
akmhoque7b791452012-10-30 11:24:56 -05001136 print_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
1137 printf("\n");
1138 }
akmhoque53f64222012-09-05 13:57:51 -05001139
1140}
1141
1142void
1143print_adj_lsdb(void)
1144{
akmhoque7b791452012-10-30 11:24:56 -05001145 if ( nlsr->debugging )
1146 printf("print_name_lsdb called \n");
akmhoque53f64222012-09-05 13:57:51 -05001147 int i, adj_lsdb_element;
1148 struct alsa *adj_lsa;
1149
1150 struct hashtb_enumerator ee;
1151 struct hashtb_enumerator *e = &ee;
1152
1153 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1154 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1155
1156 for(i=0;i<adj_lsdb_element;i++)
1157 {
akmhoque7b791452012-10-30 11:24:56 -05001158 if ( nlsr->debugging )
1159 printf("-----------Adj LSA (%d)---------------\n",i+1);
akmhoque53f64222012-09-05 13:57:51 -05001160 adj_lsa=e->data;
1161 print_adj_lsa(adj_lsa);
1162 hashtb_next(e);
1163 }
1164
1165 hashtb_end(e);
1166
akmhoque3171d652012-11-13 11:44:33 -06001167 if ( nlsr->debugging )
1168 printf("\n");
1169 if ( nlsr->detailed_logging )
1170 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoque53f64222012-09-05 13:57:51 -05001171}
1172
1173void
akmhoque03004e62012-09-06 01:12:28 -05001174build_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 -05001175{
akmhoque7b791452012-10-30 11:24:56 -05001176 if ( nlsr->debugging )
1177 printf("build_and_install_others_adj_lsa called \n");
1178 if ( nlsr->detailed_logging )
1179 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_others_adj_lsa called \n");
akmhoque03004e62012-09-06 01:12:28 -05001180 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
1181 build_others_adj_lsa(adj_lsa,orig_router,ls_type,orig_time,no_link,data);
akmhoque03004e62012-09-06 01:12:28 -05001182 install_adj_lsa(adj_lsa);
1183
akmhoquef5537b42013-02-22 08:33:32 -06001184 destroy_adj_lsa(adj_lsa);
akmhoque53f64222012-09-05 13:57:51 -05001185
akmhoque03004e62012-09-06 01:12:28 -05001186 print_adj_lsdb();
akmhoque53f64222012-09-05 13:57:51 -05001187
akmhoque53f64222012-09-05 13:57:51 -05001188}
1189
akmhoque03004e62012-09-06 01:12:28 -05001190
akmhoque53f64222012-09-05 13:57:51 -05001191void
akmhoque03004e62012-09-06 01:12:28 -05001192build_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 -05001193{
akmhoque7b791452012-10-30 11:24:56 -05001194 if ( nlsr->debugging )
1195 printf("build_others_adj_lsa called \n");
1196 if ( nlsr->detailed_logging )
1197 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_others_adj_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -05001198
akmhoque03004e62012-09-06 01:12:28 -05001199 /*Filling Up Header Data */
akmhoquef5537b42013-02-22 08:33:32 -06001200 adj_lsa->header=(struct alsa_header *)calloc(1,sizeof(struct alsa_header ));
1201 adj_lsa->header->orig_router=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
1202 adj_lsa->header->orig_router->name=(char *)calloc(strlen(orig_router)+1,sizeof(char));
akmhoque03004e62012-09-06 01:12:28 -05001203 memcpy(adj_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
akmhoque53f64222012-09-05 13:57:51 -05001204
akmhoque03004e62012-09-06 01:12:28 -05001205 adj_lsa->header->orig_router->length=strlen(orig_router)+1;
akmhoque53f64222012-09-05 13:57:51 -05001206
akmhoque53f64222012-09-05 13:57:51 -05001207
akmhoque03004e62012-09-06 01:12:28 -05001208 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
akmhoque53f64222012-09-05 13:57:51 -05001209
akmhoquef5537b42013-02-22 08:33:32 -06001210 adj_lsa->header->orig_time=(char *)calloc(strlen(orig_time)+1,sizeof(char));
akmhoque03004e62012-09-06 01:12:28 -05001211 memcpy(adj_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
akmhoque53f64222012-09-05 13:57:51 -05001212
akmhoque03004e62012-09-06 01:12:28 -05001213 adj_lsa->no_link=no_link;
akmhoque53f64222012-09-05 13:57:51 -05001214
akmhoquef5537b42013-02-22 08:33:32 -06001215 adj_lsa->body=(char *)calloc(strlen(data)+1,sizeof(char));
akmhoque03004e62012-09-06 01:12:28 -05001216 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
akmhoque53f64222012-09-05 13:57:51 -05001217
akmhoque53f64222012-09-05 13:57:51 -05001218}
1219
akmhoque03004e62012-09-06 01:12:28 -05001220
akmhoque53f64222012-09-05 13:57:51 -05001221long int
1222get_name_lsdb_num_element(void)
1223{
1224 long int num_element;
1225
1226
1227 struct hashtb_enumerator ee;
1228 struct hashtb_enumerator *e = &ee;
1229
1230 hashtb_start(nlsr->lsdb->name_lsdb, e);
1231 num_element=hashtb_n(nlsr->lsdb->name_lsdb);
1232 hashtb_end(e);
1233
1234 return num_element;
1235}
1236
1237long int
1238get_adj_lsdb_num_element(void)
1239{
1240 long int num_element;
1241
1242
1243 struct hashtb_enumerator ee;
1244 struct hashtb_enumerator *e = &ee;
1245
1246 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1247 num_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1248 hashtb_end(e);
1249
1250 return num_element;
akmhoqued79438d2012-08-27 13:31:42 -05001251}
akmhoque03004e62012-09-06 01:12:28 -05001252
akmhoque03004e62012-09-06 01:12:28 -05001253
1254int
1255check_is_new_name_lsa(char *orig_router,char *lst,char *lsid,char *orig_time)
1256{
1257 int ret=0;
1258 struct ccn_charbuf *key=ccn_charbuf_create();
1259 ccn_charbuf_append_string(key,orig_router);
1260 ccn_charbuf_append_string(key,"/");
1261 ccn_charbuf_append_string(key,lst);
1262 ccn_charbuf_append_string(key,"/");
1263 ccn_charbuf_append_string(key,lsid);
1264
1265 int res;
1266 struct nlsa *name_lsa;
1267
1268 struct hashtb_enumerator ee;
1269 struct hashtb_enumerator *e = &ee;
1270
1271 hashtb_start(nlsr->lsdb->name_lsdb, e);
1272 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1273
1274 if( res == HT_NEW_ENTRY )
1275 {
1276 hashtb_delete(e);
1277 ret=1;
1278
1279 }
1280 else if(res == HT_OLD_ENTRY)
1281 {
1282 name_lsa=e->data;
1283 if( strcmp ( orig_time , name_lsa->header->orig_time ) > 0 )
1284 {
1285 ret=1;
1286 }
1287 }
1288
1289 hashtb_end(e);
1290
1291 ccn_charbuf_destroy(&key);
1292
1293 return ret;
1294}
1295
1296int
1297check_is_new_adj_lsa(char *orig_router,char *lst,char *orig_time)
1298{
1299 int ret=0;
1300 struct ccn_charbuf *key=ccn_charbuf_create();
1301 ccn_charbuf_append_string(key,orig_router);
1302 ccn_charbuf_append_string(key,"/");
1303 ccn_charbuf_append_string(key,lst);
1304
1305 int res;
1306 struct alsa *adj_lsa;
1307
1308 struct hashtb_enumerator ee;
1309 struct hashtb_enumerator *e = &ee;
1310
1311 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1312 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1313
1314 if( res == HT_NEW_ENTRY )
1315 {
1316 hashtb_delete(e);
1317 ret=1;
1318
1319 }
1320 else if(res == HT_OLD_ENTRY)
1321 {
1322 adj_lsa=e->data;
1323 if( strcmp ( orig_time , adj_lsa->header->orig_time ) > 0 )
1324 {
1325 ret=1;
1326 }
1327 }
1328
1329 hashtb_end(e);
1330
1331 ccn_charbuf_destroy(&key);
1332
1333 return ret;
1334}
1335
akmhoqueb77b95f2013-02-08 12:28:47 -06001336int
1337check_is_new_cor_lsa(char *orig_router,char *lst,char *orig_time)
1338{
1339 int ret=0;
1340 struct ccn_charbuf *key=ccn_charbuf_create();
1341 ccn_charbuf_append_string(key,orig_router);
1342 ccn_charbuf_append_string(key,"/");
1343 ccn_charbuf_append_string(key,lst);
1344
1345 int res;
1346 struct clsa *cor_lsa;
1347
1348 struct hashtb_enumerator ee;
1349 struct hashtb_enumerator *e = &ee;
1350
1351 hashtb_start(nlsr->lsdb->cor_lsdb, e);
1352 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1353
1354 if( res == HT_NEW_ENTRY )
1355 {
1356 hashtb_delete(e);
1357 ret=1;
1358
1359 }
1360 else if(res == HT_OLD_ENTRY)
1361 {
1362 cor_lsa=e->data;
1363 if( strcmp ( orig_time , cor_lsa->header->orig_time ) > 0 )
1364 {
1365 ret=1;
1366 }
1367 }
1368
1369 hashtb_end(e);
1370
1371 ccn_charbuf_destroy(&key);
1372
1373 return ret;
1374}
1375
akmhoque03004e62012-09-06 01:12:28 -05001376void
1377get_name_lsa_data(struct ccn_charbuf *lsa_data, struct name_prefix *lsaId)
1378{
akmhoque7b791452012-10-30 11:24:56 -05001379 if ( nlsr->debugging )
1380 printf("get_name_lsa_data called \n");
1381 if ( nlsr->detailed_logging )
1382 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_name_lsa_data called \n");
akmhoque03004e62012-09-06 01:12:28 -05001383
akmhoque8876e982013-02-21 13:35:46 -06001384 struct nlsa *name_lsa;//=(struct nlsa*)malloc(sizeof(struct nlsa ));
akmhoque03004e62012-09-06 01:12:28 -05001385
1386 struct hashtb_enumerator ee;
1387 struct hashtb_enumerator *e = &ee;
1388 int res;
1389
1390 hashtb_start(nlsr->lsdb->name_lsdb, e);
1391 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1392
1393 if( res == HT_OLD_ENTRY )
1394 {
1395 name_lsa=e->data;
akmhoqueb77b95f2013-02-08 12:28:47 -06001396
akmhoque7b791452012-10-30 11:24:56 -05001397 if ( nlsr->debugging )
1398 printf("NAME LSA found \n");
1399 if ( nlsr->detailed_logging )
1400 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Name LSA found \n");
akmhoque03004e62012-09-06 01:12:28 -05001401
1402 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_router->name);
1403 ccn_charbuf_append_string(lsa_data,"|");
1404
1405 char *temp_length=(char *)malloc(20);
1406 memset(temp_length,0,20);
1407 sprintf(temp_length,"%d",name_lsa->header->orig_router->length);
1408 ccn_charbuf_append_string(lsa_data,temp_length);
1409 free(temp_length);
1410 ccn_charbuf_append_string(lsa_data,"|");
1411
1412 char *temp_ltype=(char *)malloc(20);
1413 memset(temp_ltype,0,20);
1414 sprintf(temp_ltype,"%d",name_lsa->header->ls_type);
1415 ccn_charbuf_append_string(lsa_data,temp_ltype);
1416 free(temp_ltype);
1417 ccn_charbuf_append_string(lsa_data,"|");
1418
1419 char *temp_lsid=(char *)malloc(20);
1420 memset(temp_lsid,0,20);
1421 sprintf(temp_lsid,"%ld",name_lsa->header->ls_id);
1422 ccn_charbuf_append_string(lsa_data,temp_lsid);
1423 free(temp_lsid);
1424 ccn_charbuf_append_string(lsa_data,"|");
1425
1426 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_time);
1427 ccn_charbuf_append_string(lsa_data,"|");
1428
1429 char *temp_valid=(char *)malloc(20);
1430 memset(temp_valid,0,20);
1431 sprintf(temp_valid,"%d",name_lsa->header->isValid);
1432 ccn_charbuf_append_string(lsa_data,temp_valid);
1433 free(temp_valid);
1434 ccn_charbuf_append_string(lsa_data,"|");
1435
1436 ccn_charbuf_append_string(lsa_data,name_lsa->name_prefix->name);
1437 ccn_charbuf_append_string(lsa_data,"|");
1438
1439 char *temp_npl=(char *)malloc(20);
1440 memset(temp_npl,0,20);
1441 sprintf(temp_npl,"%d",name_lsa->name_prefix->length);
1442 ccn_charbuf_append_string(lsa_data,temp_npl);
1443 free(temp_npl);
1444 ccn_charbuf_append_string(lsa_data,"|");
1445
1446 }
1447 else if(res == HT_NEW_ENTRY)
1448 {
1449 hashtb_delete(e);
1450 }
1451
1452 hashtb_end(e);
1453}
1454
1455void
1456get_adj_lsa_data(struct ccn_charbuf *lsa_data,struct name_prefix *lsaId)
1457{
akmhoque7b791452012-10-30 11:24:56 -05001458 if ( nlsr->debugging )
1459 printf("get_adj_lsa_data called \n");
1460 if ( nlsr->detailed_logging )
1461 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_adj_lsa_data called \n");
akmhoque03004e62012-09-06 01:12:28 -05001462
1463 struct alsa *adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
1464
1465 struct hashtb_enumerator ee;
1466 struct hashtb_enumerator *e = &ee;
1467 int res;
1468
1469 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1470 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1471
1472 if( res == HT_OLD_ENTRY )
1473 {
1474 adj_lsa=e->data;
akmhoque7b791452012-10-30 11:24:56 -05001475
1476 if ( nlsr->debugging )
1477 printf("Adj LSA found \n");
1478 if ( nlsr->detailed_logging )
1479 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adj LSA found \n");
akmhoque03004e62012-09-06 01:12:28 -05001480
1481 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_router->name);
1482 ccn_charbuf_append_string(lsa_data,"|");
1483
1484 char *temp_length=(char *)malloc(20);
1485 memset(temp_length,0,20);
1486 sprintf(temp_length,"%d",adj_lsa->header->orig_router->length);
1487 ccn_charbuf_append_string(lsa_data,temp_length);
akmhoque03004e62012-09-06 01:12:28 -05001488 ccn_charbuf_append_string(lsa_data,"|");
akmhoqueb77b95f2013-02-08 12:28:47 -06001489 free(temp_length);
akmhoque03004e62012-09-06 01:12:28 -05001490
1491 char *temp_ltype=(char *)malloc(20);
1492 memset(temp_ltype,0,20);
1493 sprintf(temp_ltype,"%d",adj_lsa->header->ls_type);
1494 ccn_charbuf_append_string(lsa_data,temp_ltype);
akmhoque03004e62012-09-06 01:12:28 -05001495 ccn_charbuf_append_string(lsa_data,"|");
akmhoqueb77b95f2013-02-08 12:28:47 -06001496 free(temp_ltype);
akmhoque03004e62012-09-06 01:12:28 -05001497
1498 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_time);
1499 ccn_charbuf_append_string(lsa_data,"|");
1500
1501 char *temp_nl=(char *)malloc(20);
1502 memset(temp_nl,0,20);
1503 sprintf(temp_nl,"%d",adj_lsa->no_link);
1504 ccn_charbuf_append_string(lsa_data,temp_nl);
akmhoque03004e62012-09-06 01:12:28 -05001505 ccn_charbuf_append_string(lsa_data,"|");
akmhoqueb77b95f2013-02-08 12:28:47 -06001506 free(temp_nl);
akmhoque03004e62012-09-06 01:12:28 -05001507
1508 ccn_charbuf_append_string(lsa_data,adj_lsa->body);
1509
1510
1511 }
1512 else if(res == HT_NEW_ENTRY)
1513 {
1514 hashtb_delete(e);
1515 }
1516
1517 hashtb_end(e);
1518}
akmhoqueffacaa82012-09-13 17:48:30 -05001519
akmhoque3171d652012-11-13 11:44:33 -06001520void
1521make_name_lsa_invalid(struct name_prefix *np,int ls_type, long int ls_id)
1522{
1523
1524 if ( nlsr->debugging )
1525 printf("make_name_lsa_invalid called \n");
1526 if ( nlsr->detailed_logging )
1527 writeLogg(__FILE__,__FUNCTION__,__LINE__,"make_name_lsa_invalid called \n");
1528
1529
1530 char lst[2];
1531 memset(lst,0,2);
1532 sprintf(lst,"%d",ls_type);
1533
1534 char lsid[10];
1535 memset(lsid,0,10);
1536 sprintf(lsid,"%ld",ls_id);
1537
1538
1539 char *key=(char *)malloc(strlen(np->name)+1+strlen(lst)+1+strlen(lsid)+1);
1540 memset(key,0,strlen(np->name)+1+strlen(lst)+1+strlen(lsid)+1);
1541
1542
1543 make_name_lsa_key(key, np->name,ls_type,ls_id);
akmhoqueb77b95f2013-02-08 12:28:47 -06001544
1545 if ( nlsr->debugging )
1546 printf("Key:%s Length:%d\n",key,(int)strlen(key));
akmhoque3171d652012-11-13 11:44:33 -06001547
1548 struct nlsa *nlsa;
1549
1550 struct hashtb_enumerator ee;
1551 struct hashtb_enumerator *e = &ee;
1552
1553 int res;
1554
1555 hashtb_start(nlsr->lsdb->name_lsdb, e);
1556 res = hashtb_seek(e, key,strlen(key) , 0);
1557
1558 if( res == HT_OLD_ENTRY )
1559 {
1560 nlsa=e->data;
1561
1562 nlsa->header->isValid=0;
1563
1564 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1565 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1566 write_log_for_name_lsa(nlsa);
1567 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1568
1569 hashtb_delete(e);
1570 }
1571 else if( res == HT_NEW_ENTRY )
1572 {
1573 hashtb_delete(e);
1574 }
1575 hashtb_end(e);
1576
1577 if ( nlsr->debugging )
1578 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1579 if ( nlsr->detailed_logging )
1580 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1581
1582 set_new_lsdb_version();
1583
1584 if ( nlsr->debugging )
1585 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1586 if ( nlsr->detailed_logging )
1587 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1588
1589}
1590
akmhoquef5537b42013-02-22 08:33:32 -06001591
akmhoque866c2222013-02-12 10:49:33 -06001592int
1593delete_name_lsa(char *orig_router, char *name_prefix)
akmhoqueda5b6832012-09-13 22:33:55 -05001594{
akmhoqueb77b95f2013-02-08 12:28:47 -06001595
akmhoque7b791452012-10-30 11:24:56 -05001596 if ( nlsr->debugging )
1597 printf("delete_name_lsa called \n");
1598 if ( nlsr->detailed_logging )
1599 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_name_lsa called \n");
akmhoqueda5b6832012-09-13 22:33:55 -05001600
akmhoque866c2222013-02-12 10:49:33 -06001601 delete_npt_entry_by_router_and_name_prefix(orig_router, name_prefix);
akmhoquef5537b42013-02-22 08:33:32 -06001602
akmhoqueda5b6832012-09-13 22:33:55 -05001603
akmhoque7b791452012-10-30 11:24:56 -05001604 if ( nlsr->debugging )
1605 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1606 if ( nlsr->detailed_logging )
1607 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1608
akmhoqueda5b6832012-09-13 22:33:55 -05001609 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -05001610
1611 if ( nlsr->debugging )
1612 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1613 if ( nlsr->detailed_logging )
1614 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueda5b6832012-09-13 22:33:55 -05001615
akmhoqueda5b6832012-09-13 22:33:55 -05001616
1617 return 0;
1618}
1619
akmhoquef5537b42013-02-22 08:33:32 -06001620
akmhoque866c2222013-02-12 10:49:33 -06001621int delete_adj_lsa()
akmhoqueda5b6832012-09-13 22:33:55 -05001622{
akmhoque866c2222013-02-12 10:49:33 -06001623
akmhoque7b791452012-10-30 11:24:56 -05001624 if ( nlsr->debugging )
1625 printf("delete_adj_lsa called \n");
1626 if ( nlsr->detailed_logging )
1627 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_adj_lsa called \n");
akmhoque866c2222013-02-12 10:49:33 -06001628
akmhoque7b791452012-10-30 11:24:56 -05001629 if ( nlsr->debugging )
1630 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1631 if ( nlsr->detailed_logging )
1632 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1633
akmhoqueda5b6832012-09-13 22:33:55 -05001634 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -05001635
1636 if ( nlsr->debugging )
1637 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1638 if ( nlsr->detailed_logging )
1639 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1640
akmhoqueda5b6832012-09-13 22:33:55 -05001641
1642 if ( !nlsr->is_route_calculation_scheduled)
1643 {
1644 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
1645 nlsr->is_route_calculation_scheduled=1;
1646 }
1647
akmhoqueda5b6832012-09-13 22:33:55 -05001648 return 0;
1649}
1650
akmhoqueffacaa82012-09-13 17:48:30 -05001651void
1652refresh_name_lsdb(void)
1653{
akmhoque7b791452012-10-30 11:24:56 -05001654 if ( nlsr->debugging )
1655 printf("refresh_name_lsdb called \n");
1656 if ( nlsr->detailed_logging )
1657 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_name_lsdb called \n");
akmhoqueffacaa82012-09-13 17:48:30 -05001658
akmhoquef5537b42013-02-22 08:33:32 -06001659 char *time_stamp=get_current_timestamp_micro_v2();
akmhoqueffacaa82012-09-13 17:48:30 -05001660
1661 long int lsa_life_time;
1662
1663 int i, name_lsdb_element;
1664 struct nlsa *name_lsa;
1665
1666 struct hashtb_enumerator ee;
1667 struct hashtb_enumerator *e = &ee;
1668
1669 hashtb_start(nlsr->lsdb->name_lsdb, e);
1670 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
1671
1672 for(i=0;i<name_lsdb_element;i++)
1673 {
1674 name_lsa=e->data;
1675
1676 lsa_life_time=get_time_diff(time_stamp,name_lsa->header->orig_time);
akmhoque7b791452012-10-30 11:24:56 -05001677 if ( nlsr->debugging )
1678 printf("LSA Life Time: %ld \n",lsa_life_time);
1679 if ( nlsr->detailed_logging )
1680 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Life Time: %ld \n",lsa_life_time);
akmhoqueda5b6832012-09-13 22:33:55 -05001681
1682 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name) == 0)
1683 {
1684 if ( lsa_life_time > nlsr->lsa_refresh_time )
1685 {
akmhoque14b3f342012-09-14 10:39:02 -05001686 if ( name_lsa->header->isValid == NAME_LSA_VALID )
1687 {
akmhoque7b791452012-10-30 11:24:56 -05001688 if ( nlsr->debugging )
1689 printf("Own Name LSA need to be refrshed\n");
1690 if ( nlsr->detailed_logging )
1691 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Own Name LSA need to be refrshed\n");
akmhoquea98c2142012-10-25 15:22:24 -05001692
1693 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1694 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1695 write_log_for_name_lsa(name_lsa);
1696 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1697
akmhoquef5537b42013-02-22 08:33:32 -06001698 char *current_time_stamp=get_current_timestamp_micro_v2();
akmhoqueda5b6832012-09-13 22:33:55 -05001699
akmhoque14b3f342012-09-14 10:39:02 -05001700 free(name_lsa->header->orig_time);
akmhoquef5537b42013-02-22 08:33:32 -06001701 name_lsa->header->orig_time=(char *)calloc(strlen(current_time_stamp)+1,sizeof(char));
akmhoque14b3f342012-09-14 10:39:02 -05001702 memcpy(name_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
akmhoquea98c2142012-10-25 15:22:24 -05001703
1704 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1705 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
1706 write_log_for_name_lsa(name_lsa);
1707 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
akmhoqueda5b6832012-09-13 22:33:55 -05001708
akmhoque14b3f342012-09-14 10:39:02 -05001709 free(current_time_stamp);
akmhoque866c2222013-02-12 10:49:33 -06001710
1711 hashtb_next(e);
akmhoque14b3f342012-09-14 10:39:02 -05001712 }
1713 else
akmhoquef5537b42013-02-22 08:33:32 -06001714 {
akmhoque866c2222013-02-12 10:49:33 -06001715 delete_name_lsa(name_lsa->header->orig_router->name, name_lsa->name_prefix->name);
1716 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1717 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1718 write_log_for_name_lsa(name_lsa);
1719 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
akmhoquef5537b42013-02-22 08:33:32 -06001720
1721 destroy_name_lsa_component(name_lsa);
1722
akmhoque866c2222013-02-12 10:49:33 -06001723 hashtb_delete(e);
1724 i++;
akmhoque14b3f342012-09-14 10:39:02 -05001725 }
akmhoqueda5b6832012-09-13 22:33:55 -05001726
akmhoque7b791452012-10-30 11:24:56 -05001727 if ( nlsr->debugging )
1728 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1729 if ( nlsr->detailed_logging )
1730 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1731
akmhoqueda5b6832012-09-13 22:33:55 -05001732 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -05001733
1734 if ( nlsr->debugging )
1735 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1736 if ( nlsr->detailed_logging )
1737 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1738
1739
akmhoqueda5b6832012-09-13 22:33:55 -05001740
1741 print_name_lsdb();
akmhoque866c2222013-02-12 10:49:33 -06001742 }
1743 else
1744 {
1745 hashtb_next(e);
akmhoqueda5b6832012-09-13 22:33:55 -05001746 }
1747 }
1748 else
1749 {
1750 if ( lsa_life_time > nlsr->router_dead_interval )
1751 {
akmhoque7b791452012-10-30 11:24:56 -05001752 if ( nlsr->debugging )
1753 printf("Others Name LSA need to be deleted\n");
1754 if ( nlsr->detailed_logging )
1755 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Others Name LSA need to be deleted\n");
akmhoqueda5b6832012-09-13 22:33:55 -05001756
akmhoque866c2222013-02-12 10:49:33 -06001757 delete_name_lsa(name_lsa->header->orig_router->name, name_lsa->name_prefix->name);
1758 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1759 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1760 write_log_for_name_lsa(name_lsa);
1761 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
akmhoquef5537b42013-02-22 08:33:32 -06001762
1763 destroy_name_lsa_component(name_lsa);
akmhoque866c2222013-02-12 10:49:33 -06001764 hashtb_delete(e);
1765 i++;
1766 }
1767 else
1768 {
1769 hashtb_next(e);
akmhoqueda5b6832012-09-13 22:33:55 -05001770 }
1771 }
akmhoqueffacaa82012-09-13 17:48:30 -05001772 }
1773
1774 hashtb_end(e);
1775
1776 free(time_stamp);
akmhoqueda5b6832012-09-13 22:33:55 -05001777
akmhoqueffacaa82012-09-13 17:48:30 -05001778
1779}
1780
akmhoqueda5b6832012-09-13 22:33:55 -05001781void
akmhoqueffacaa82012-09-13 17:48:30 -05001782refresh_adj_lsdb(void)
1783{
akmhoqueffacaa82012-09-13 17:48:30 -05001784
akmhoque7b791452012-10-30 11:24:56 -05001785 if ( nlsr->debugging )
1786 printf("refresh_adj_lsdb called \n");
1787 if ( nlsr->detailed_logging )
1788 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_adj_lsdb called \n");
akmhoqueffacaa82012-09-13 17:48:30 -05001789
akmhoquef5537b42013-02-22 08:33:32 -06001790 char *time_stamp=get_current_timestamp_micro_v2();
1791
akmhoqueffacaa82012-09-13 17:48:30 -05001792 long int lsa_life_time;
1793
1794 int i, adj_lsdb_element;
1795 struct alsa *adj_lsa;
1796
1797 struct hashtb_enumerator ee;
1798 struct hashtb_enumerator *e = &ee;
1799
1800 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1801 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1802
1803 for(i=0;i<adj_lsdb_element;i++)
1804 {
1805 adj_lsa=e->data;
1806
akmhoqueb77b95f2013-02-08 12:28:47 -06001807 lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
akmhoque7b791452012-10-30 11:24:56 -05001808
1809 if ( nlsr->debugging )
1810 printf("LSA Life Time: %ld \n",lsa_life_time);
1811 if ( nlsr->detailed_logging )
1812 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Life Time: %ld \n",lsa_life_time);
akmhoqueffacaa82012-09-13 17:48:30 -05001813
akmhoqueda5b6832012-09-13 22:33:55 -05001814 if ( strcmp(adj_lsa->header->orig_router->name,nlsr->router_name) == 0)
1815 {
1816 if ( lsa_life_time > nlsr->lsa_refresh_time )
1817 {
akmhoque7b791452012-10-30 11:24:56 -05001818 if ( nlsr->debugging )
1819 printf("Own Adj LSA need to be refrshed\n");
1820 if ( nlsr->detailed_logging )
1821 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Own Adj LSA need to be refrshed\n");
akmhoqueda5b6832012-09-13 22:33:55 -05001822
akmhoquea98c2142012-10-25 15:22:24 -05001823 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1824 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
1825 write_log_for_adj_lsa(adj_lsa);
1826 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1827
akmhoquef5537b42013-02-22 08:33:32 -06001828 char *current_time_stamp=get_current_timestamp_micro_v2();
akmhoqueda5b6832012-09-13 22:33:55 -05001829
1830 free(adj_lsa->header->orig_time);
akmhoquef5537b42013-02-22 08:33:32 -06001831 adj_lsa->header->orig_time=(char *)calloc(strlen(current_time_stamp)+1,sizeof(char));
akmhoqueda5b6832012-09-13 22:33:55 -05001832 memcpy(adj_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1833
1834 free(current_time_stamp);
1835
akmhoquea98c2142012-10-25 15:22:24 -05001836 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1837 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
1838 write_log_for_adj_lsa(adj_lsa);
1839 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoqueda5b6832012-09-13 22:33:55 -05001840
akmhoque7b791452012-10-30 11:24:56 -05001841 if ( nlsr->debugging )
1842 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1843 if ( nlsr->detailed_logging )
1844 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1845
akmhoqueda5b6832012-09-13 22:33:55 -05001846 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -05001847
1848 if ( nlsr->debugging )
1849 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1850 if ( nlsr->detailed_logging )
1851 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueda5b6832012-09-13 22:33:55 -05001852
1853 print_adj_lsdb();
akmhoque866c2222013-02-12 10:49:33 -06001854
1855
akmhoqueda5b6832012-09-13 22:33:55 -05001856 }
akmhoque866c2222013-02-12 10:49:33 -06001857
1858 hashtb_next(e);
akmhoqueda5b6832012-09-13 22:33:55 -05001859 }
1860 else
1861 {
1862 if ( lsa_life_time > nlsr->router_dead_interval )
1863 {
akmhoque7b791452012-10-30 11:24:56 -05001864
1865 if ( nlsr->debugging )
1866 printf("Others Adj LSA need to be deleted\n");
1867 if ( nlsr->detailed_logging )
1868 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Others Adj LSA need to be deleted\n");
akmhoque866c2222013-02-12 10:49:33 -06001869 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1870 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
1871 write_log_for_adj_lsa(adj_lsa);
1872 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1873 delete_adj_lsa();
akmhoquef5537b42013-02-22 08:33:32 -06001874
1875 destroy_adj_lsa_component(adj_lsa);
akmhoque866c2222013-02-12 10:49:33 -06001876 hashtb_delete(e);
1877 i++;
1878
1879 }
1880 else
1881 {
1882 hashtb_next(e);
akmhoqueda5b6832012-09-13 22:33:55 -05001883 }
1884 }
akmhoquef5537b42013-02-22 08:33:32 -06001885
akmhoqueffacaa82012-09-13 17:48:30 -05001886 }
1887
1888 hashtb_end(e);
1889
1890 free(time_stamp);
akmhoqueffacaa82012-09-13 17:48:30 -05001891}
1892
akmhoqueb77b95f2013-02-08 12:28:47 -06001893
1894void
1895refresh_cor_lsdb(void)
1896{
1897
1898 if ( nlsr->debugging )
1899 printf("refresh_cor_lsdb called \n");
1900 if ( nlsr->detailed_logging )
1901 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_cor_lsdb called \n");
1902
akmhoquef5537b42013-02-22 08:33:32 -06001903 char *time_stamp=get_current_timestamp_micro_v2();
1904
akmhoqueb77b95f2013-02-08 12:28:47 -06001905 long int lsa_life_time;
1906
1907 int i, cor_lsdb_element;
1908 struct clsa *cor_lsa;
1909
1910 struct hashtb_enumerator ee;
1911 struct hashtb_enumerator *e = &ee;
1912
1913 hashtb_start(nlsr->lsdb->cor_lsdb, e);
1914 cor_lsdb_element=hashtb_n(nlsr->lsdb->cor_lsdb);
1915
1916 for(i=0;i<cor_lsdb_element;i++)
1917 {
1918 cor_lsa=e->data;
1919
1920 lsa_life_time=get_time_diff(time_stamp,cor_lsa->header->orig_time);
1921
1922 if ( nlsr->debugging )
1923 printf("LSA Life Time: %ld \n",lsa_life_time);
1924 if ( nlsr->detailed_logging )
1925 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Life Time: %ld \n",lsa_life_time);
1926
1927 if ( strcmp(cor_lsa->header->orig_router->name,nlsr->router_name) == 0)
1928 {
1929 if ( lsa_life_time > nlsr->lsa_refresh_time )
1930 {
1931 if ( nlsr->debugging )
1932 printf("Own Cor LSA need to be refrshed\n");
1933 if ( nlsr->detailed_logging )
1934 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Own Cor LSA need to be refrshed\n");
1935
akmhoquef5537b42013-02-22 08:33:32 -06001936 char *current_time_stamp=get_current_timestamp_micro_v2();
akmhoqueb77b95f2013-02-08 12:28:47 -06001937
1938 free(cor_lsa->header->orig_time);
1939 cor_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1940 memset(cor_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1941 memcpy(cor_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1942
1943 free(current_time_stamp);
1944
akmhoqueb77b95f2013-02-08 12:28:47 -06001945 if ( nlsr->debugging )
1946 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1947 if ( nlsr->detailed_logging )
1948 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1949
1950 set_new_lsdb_version();
1951
1952 if ( nlsr->debugging )
1953 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1954 if ( nlsr->detailed_logging )
1955 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1956
1957 print_adj_lsdb();
akmhoque866c2222013-02-12 10:49:33 -06001958 }
1959
1960 hashtb_next(e);
akmhoqueb77b95f2013-02-08 12:28:47 -06001961 }
1962 else
1963 {
1964 if ( lsa_life_time > nlsr->router_dead_interval )
1965 {
1966
1967 if ( nlsr->debugging )
1968 printf("Others Adj LSA need to be deleted\n");
1969 if ( nlsr->detailed_logging )
1970 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Others Adj LSA need to be deleted\n");
akmhoquef5537b42013-02-22 08:33:32 -06001971
1972 destroy_cor_lsa_component(cor_lsa);
akmhoqueb77b95f2013-02-08 12:28:47 -06001973 hashtb_delete(e);
1974 i++;
1975 }
akmhoque866c2222013-02-12 10:49:33 -06001976 else
1977 {
1978 hashtb_next(e);
1979 }
akmhoqueb77b95f2013-02-08 12:28:47 -06001980 }
1981
1982
akmhoque866c2222013-02-12 10:49:33 -06001983
akmhoqueb77b95f2013-02-08 12:28:47 -06001984 }
1985
1986 hashtb_end(e);
1987
1988 free(time_stamp);
1989}
1990
akmhoqueffacaa82012-09-13 17:48:30 -05001991int
1992refresh_lsdb(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1993{
1994 if(flags == CCN_SCHEDULE_CANCEL)
1995 {
1996 return -1;
1997 }
1998
1999 nlsr_lock();
akmhoqueda5b6832012-09-13 22:33:55 -05002000
akmhoque7b791452012-10-30 11:24:56 -05002001 if ( nlsr->debugging )
2002 printf("refresh_lsdb called\n");
2003 if ( nlsr->detailed_logging )
2004 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_lsdb called\n");
akmhoqueffacaa82012-09-13 17:48:30 -05002005
2006 refresh_name_lsdb();
2007 refresh_adj_lsdb();
2008
akmhoqueffacaa82012-09-13 17:48:30 -05002009 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoqueda5b6832012-09-13 22:33:55 -05002010
akmhoqueffacaa82012-09-13 17:48:30 -05002011 nlsr_unlock();
2012 return 0;
2013}
akmhoqueb77b95f2013-02-08 12:28:47 -06002014
2015void
2016write_adj_lsa_to_repo(char *repo_content_prefix, struct name_prefix *lsa_id)
2017{
2018 if ( nlsr->debugging )
2019 printf("write_adj_lsa_to_repo called\n");
2020 if ( nlsr->debugging )
2021 printf("Content Prefix: %s\n",repo_content_prefix);
2022
2023 struct ccn_charbuf *lsa_data=ccn_charbuf_create();
2024 get_adj_lsa_data(lsa_data,lsa_id);
2025 if ( nlsr->debugging )
akmhoque323b5e92013-02-21 13:55:15 -06002026 printf("Adj LSA Data: %s \n",ccn_charbuf_as_string(lsa_data));
akmhoqueb77b95f2013-02-08 12:28:47 -06002027
akmhoquef5537b42013-02-22 08:33:32 -06002028 char *data=calloc(strlen(ccn_charbuf_as_string(lsa_data))+1,sizeof(char));
2029 data=ccn_charbuf_as_string(lsa_data);
2030 data[strlen(data)]='\0';
2031
2032 write_data_to_repo(data, repo_content_prefix);
akmhoqueb77b95f2013-02-08 12:28:47 -06002033
akmhoque323b5e92013-02-21 13:55:15 -06002034
2035 ccn_charbuf_destroy(&lsa_data);
akmhoqueb77b95f2013-02-08 12:28:47 -06002036}
2037
2038void
2039write_name_lsa_to_repo(char *repo_content_prefix, struct name_prefix *lsa_id)
2040{
2041 if ( nlsr->debugging )
2042 printf("write_name_lsa_to_repo called\n");
2043 if ( nlsr->debugging )
2044 printf("Content Prefix: %s\n",repo_content_prefix);
2045
2046 struct ccn_charbuf *lsa_data=ccn_charbuf_create();
2047 get_name_lsa_data(lsa_data,lsa_id);
2048
2049 if ( nlsr->debugging )
2050 printf("Name LSA Data: %s \n",ccn_charbuf_as_string(lsa_data));
2051
akmhoquef5537b42013-02-22 08:33:32 -06002052 char *data=calloc(strlen(ccn_charbuf_as_string(lsa_data))+1,sizeof(char));
2053 data=ccn_charbuf_as_string(lsa_data);
2054 data[strlen(data)]='\0';
akmhoque323b5e92013-02-21 13:55:15 -06002055
akmhoquef5537b42013-02-22 08:33:32 -06002056 write_data_to_repo(data, repo_content_prefix);
akmhoqueb77b95f2013-02-08 12:28:47 -06002057
akmhoque4f85aab2013-02-21 13:58:50 -06002058 ccn_charbuf_destroy(&lsa_data);
akmhoqueb77b95f2013-02-08 12:28:47 -06002059}
2060
2061
2062void
2063write_name_lsdb_to_repo(char *slice_prefix)
2064{
2065 int i, name_lsdb_element;
2066
2067 struct nlsa *name_lsa;
2068 struct hashtb_enumerator ee;
2069 struct hashtb_enumerator *e = &ee;
2070
2071 hashtb_start(nlsr->lsdb->name_lsdb, e);
2072 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
2073
2074 for(i=0;i<name_lsdb_element;i++)
2075 {
2076 name_lsa=e->data;
2077
2078 char lst[2];
2079 memset(lst,0,2);
2080 sprintf(lst,"%d",name_lsa->header->ls_type);
2081
2082 char lsid[10];
2083 memset(lsid,0,10);
2084 sprintf(lsid,"%ld",name_lsa->header->ls_id);
2085
2086
2087 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
2088 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
2089
2090
2091 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
2092
2093 if ( nlsr->debugging )
2094 printf("Name LSA Key: %s \n",key);
2095
2096
akmhoquef5537b42013-02-22 08:33:32 -06002097 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 -06002098 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);
2099
2100 if ( nlsr->debugging )
2101 printf("Name LSA Repo Key: %s \n",repo_key);
2102
akmhoquef5537b42013-02-22 08:33:32 -06002103 struct name_prefix *lsaid=(struct name_prefix *)calloc(1,sizeof(struct name_prefix));
2104 lsaid->name=(char *)calloc(strlen(key)+1,sizeof(char));
2105 memcpy(lsaid->name,key,strlen(key)+1);
akmhoqueb77b95f2013-02-08 12:28:47 -06002106 lsaid->length=strlen(key)+1;
2107
2108
2109 write_name_lsa_to_repo(repo_key, lsaid);
2110
2111 free(key);
2112 free(repo_key);
2113 free(lsaid->name);
2114 free(lsaid);
2115
2116 hashtb_next(e);
2117 }
2118
2119 hashtb_end(e);
2120
2121
2122}
2123
2124void
2125print_cor_lsa(struct clsa *cor_lsa)
2126{
2127 if ( nlsr->debugging )
2128 {
2129 printf("-----------Cor LSA Content---------------\n");
2130 printf(" Origination Router : %s\n",cor_lsa->header->orig_router->name);
2131 printf(" Origination Router Length: %d\n",cor_lsa->header->orig_router->length);
2132 printf(" LS Type : %d\n",cor_lsa->header->ls_type);
2133 printf(" Origination Time : %s\n",cor_lsa->header->orig_time);
2134 printf(" LSA Data \n");
2135 printf(" Cor R: : %f\n",cor_lsa->cor_r);
2136 printf(" Cor Theta : %f\n",cor_lsa->cor_theta);
2137
2138 printf("\n");
2139 }
2140}
2141
2142void
2143print_cor_lsdb()
2144{
2145
2146 if ( nlsr->debugging )
2147 printf("print_cor_lsdb called \n");
2148
2149 struct hashtb_enumerator ee;
2150 struct hashtb_enumerator *e = &ee;
2151
2152 int i=1;
2153
2154 for (hashtb_start(nlsr->lsdb->cor_lsdb, e); e->key != NULL; hashtb_next(e))
2155 {
2156 if ( nlsr->debugging )
2157 printf("-----------Cor LSA (%d)---------------\n",i);
2158 struct clsa *cor_lsa=e->data;
2159 print_cor_lsa(cor_lsa);
2160 i++;
2161 }
2162 hashtb_end(e);
2163
2164 if ( nlsr->debugging )
2165 printf("\n");
2166 if ( nlsr->detailed_logging )
2167 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
2168}
2169
2170void
2171install_cor_lsa(struct clsa *cor_lsa)
2172{
2173 if ( nlsr->debugging )
2174 printf("install_cor_lsa called \n");
2175 if ( nlsr->detailed_logging )
2176 writeLogg(__FILE__,__FUNCTION__,__LINE__,"install_cor_lsa called \n");
2177
2178
2179 char *time_stamp=(char *)malloc(20);
2180 memset(time_stamp,0,20);
2181 get_current_timestamp_micro(time_stamp);
2182
2183
akmhoque569a93d2013-02-21 10:19:54 -06002184 char *key=(char *)malloc(cor_lsa->header->orig_router->length+4);
2185 memset(key,0,cor_lsa->header->orig_router->length+4);
akmhoqueb77b95f2013-02-08 12:28:47 -06002186 make_cor_lsa_key(key,cor_lsa);
2187
2188 if ( nlsr->debugging )
2189 printf("Cor LSA key: %s \n",key);
2190
2191 struct hashtb_enumerator ee;
2192 struct hashtb_enumerator *e = &ee;
2193 int res;
2194
2195 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2196 res = hashtb_seek(e, key, strlen(key), 0);
2197
2198 if ( res == HT_NEW_ENTRY )
2199 {
2200 if ( nlsr->debugging )
2201 printf("New Cor LSA... \n");
2202
akmhoque8876e982013-02-21 13:35:46 -06002203 struct clsa *new_cor_lsa;//=(struct clsa *)malloc(sizeof( struct clsa ));
akmhoqueb77b95f2013-02-08 12:28:47 -06002204 new_cor_lsa=e->data;
2205 new_cor_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
2206
2207 new_cor_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
2208 new_cor_lsa->header->orig_router->name=(char *)malloc(strlen(cor_lsa->header->orig_router->name)+1);
2209 memset(new_cor_lsa->header->orig_router->name,0,strlen(cor_lsa->header->orig_router->name)+1);
2210 memcpy(new_cor_lsa->header->orig_router->name,cor_lsa->header->orig_router->name,strlen(cor_lsa->header->orig_router->name)+1);
2211 new_cor_lsa->header->orig_router->length=cor_lsa->header->orig_router->length;
2212
2213 new_cor_lsa->header->orig_time=(char *)malloc(strlen(cor_lsa->header->orig_time)+1); //free
2214 memset(new_cor_lsa->header->orig_time,0,strlen(cor_lsa->header->orig_time)+1);
2215 memcpy(new_cor_lsa->header->orig_time,cor_lsa->header->orig_time,strlen(cor_lsa->header->orig_time)+1);
2216
2217 new_cor_lsa->header->ls_type=cor_lsa->header->ls_type;
2218
2219 new_cor_lsa->cor_r=cor_lsa->cor_r;
2220 new_cor_lsa->cor_theta=cor_lsa->cor_theta;
2221 }
2222 else if ( res == HT_OLD_ENTRY )
2223 {
2224 if ( nlsr->debugging )
2225 printf("Cor LSA exists (Old)... \n");
2226 }
2227 hashtb_end(e);
2228
akmhoquef5537b42013-02-22 08:33:32 -06002229 free(key);
akmhoqueb77b95f2013-02-08 12:28:47 -06002230
2231}
2232
2233void
2234build_cor_lsa(struct clsa *cor_lsa, double cor_r, double cor_theta)
2235{
2236 cor_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
2237 cor_lsa->header->ls_type=LS_TYPE_COR;
2238
akmhoquef5537b42013-02-22 08:33:32 -06002239 char *time_stamp=get_current_timestamp_micro_v2();
akmhoqueb77b95f2013-02-08 12:28:47 -06002240 cor_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1); //free
2241 memset(cor_lsa->header->orig_time,0,strlen(time_stamp)+1);
2242 memcpy(cor_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
2243 free(time_stamp);
2244
akmhoquef5537b42013-02-22 08:33:32 -06002245 cor_lsa->header->orig_router=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
2246 cor_lsa->header->orig_router->name=(char *)calloc(strlen(nlsr->router_name)+1,sizeof(char));
akmhoqueb77b95f2013-02-08 12:28:47 -06002247 memset(cor_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
2248 memcpy(cor_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
2249 cor_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
2250
2251 cor_lsa->cor_r=cor_r;
2252 cor_lsa->cor_theta=cor_theta;
2253
2254}
2255
2256void
2257build_others_cor_lsa(struct clsa *cor_lsa,char *orig_router, int ls_type,char *orig_time, double cor_r, double cor_theta)
2258{
2259 cor_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
2260 cor_lsa->header->ls_type=ls_type;
2261
2262 cor_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
2263 memset(cor_lsa->header->orig_time,0,strlen(orig_time)+1);
akmhoque569a93d2013-02-21 10:19:54 -06002264 memcpy(cor_lsa->header->orig_time,orig_time,strlen(orig_time));
akmhoqueb77b95f2013-02-08 12:28:47 -06002265
2266 cor_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
2267 cor_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
2268 memset(cor_lsa->header->orig_router->name,0,strlen(orig_router)+1);
akmhoque569a93d2013-02-21 10:19:54 -06002269 memcpy(cor_lsa->header->orig_router->name,orig_router,strlen(orig_router));
akmhoqueb77b95f2013-02-08 12:28:47 -06002270 cor_lsa->header->orig_router->length=strlen(orig_router)+1;
2271
2272 cor_lsa->cor_r=cor_r;
2273 cor_lsa->cor_theta=cor_theta;
2274
2275}
2276
2277void
2278build_and_install_others_cor_lsa(char *orig_router,int ls_type,char *orig_time, double cor_r, double cor_theta)
2279{
2280 struct clsa *cor_lsa=(struct clsa *)malloc(sizeof( struct clsa ));
2281 build_others_cor_lsa(cor_lsa,orig_router,ls_type, orig_time, cor_r, cor_theta);
2282 install_cor_lsa(cor_lsa);
2283
2284 print_cor_lsdb();
akmhoque569a93d2013-02-21 10:19:54 -06002285
akmhoquef5537b42013-02-22 08:33:32 -06002286 destroy_cor_lsa(cor_lsa);
akmhoqueb77b95f2013-02-08 12:28:47 -06002287}
2288
2289
2290void
2291build_and_install_cor_lsa()
2292{
2293
2294
2295
2296 struct clsa *cor_lsa=(struct clsa *)malloc(sizeof( struct clsa ));
2297
2298 build_cor_lsa(cor_lsa,nlsr->cor_r,nlsr->cor_theta);
2299 install_cor_lsa(cor_lsa);
2300
2301 write_cor_lsa_to_repo(cor_lsa);
akmhoquef5537b42013-02-22 08:33:32 -06002302 destroy_cor_lsa(cor_lsa);
akmhoqueb77b95f2013-02-08 12:28:47 -06002303
2304 print_cor_lsdb();
akmhoqueb77b95f2013-02-08 12:28:47 -06002305
2306}
2307
2308void
2309get_cor_lsa_data(struct ccn_charbuf *lsa_data,char *cor_lsa_key)
2310{
2311 if ( nlsr->debugging )
2312 printf("get_cor_lsa_data called \n");
2313 if ( nlsr->detailed_logging )
2314 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_adj_lsa_data called \n");
2315
2316 struct clsa *cor_lsa=(struct clsa*)malloc(sizeof(struct clsa ));
2317
2318 struct hashtb_enumerator ee;
2319 struct hashtb_enumerator *e = &ee;
2320 int res;
2321
2322 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2323 res = hashtb_seek(e, cor_lsa_key, strlen(cor_lsa_key), 0);
2324
2325 if( res == HT_OLD_ENTRY )
2326 {
2327 cor_lsa=e->data;
2328
2329 if ( nlsr->debugging )
2330 printf("Cor LSA found \n");
2331 if ( nlsr->detailed_logging )
2332 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Cor LSA found \n");
2333
2334 ccn_charbuf_append_string(lsa_data,cor_lsa->header->orig_router->name);
2335 ccn_charbuf_append_string(lsa_data,"|");
2336
2337 char *temp_length=(char *)malloc(20);
2338 memset(temp_length,0,20);
2339 sprintf(temp_length,"%d",cor_lsa->header->orig_router->length);
2340 ccn_charbuf_append_string(lsa_data,temp_length);
2341 ccn_charbuf_append_string(lsa_data,"|");
2342 free(temp_length);
2343
2344 char *temp_ltype=(char *)malloc(20);
2345 memset(temp_ltype,0,20);
2346 sprintf(temp_ltype,"%d",cor_lsa->header->ls_type);
2347 ccn_charbuf_append_string(lsa_data,temp_ltype);
2348 ccn_charbuf_append_string(lsa_data,"|");
2349 free(temp_ltype);
2350
2351 ccn_charbuf_append_string(lsa_data,cor_lsa->header->orig_time);
2352 ccn_charbuf_append_string(lsa_data,"|");
2353
2354 char *cor_r=(char *)malloc(20);
2355 memset(cor_r,0,20);
2356 sprintf(cor_r,"%f",cor_lsa->cor_r);
2357 ccn_charbuf_append_string(lsa_data,cor_r);
2358 ccn_charbuf_append_string(lsa_data,"|");
2359 free(cor_r);
2360
2361 char *cor_theta=(char *)malloc(20);
2362 memset(cor_theta,0,20);
2363 sprintf(cor_theta,"%f",cor_lsa->cor_theta);
2364 ccn_charbuf_append_string(lsa_data,cor_theta);
2365 ccn_charbuf_append_string(lsa_data,"|");
2366 free(cor_theta);
2367
2368 }
2369 else if(res == HT_NEW_ENTRY)
2370 {
2371 hashtb_delete(e);
2372 }
2373
2374 hashtb_end(e);
2375}
2376
2377void
2378write_cor_lsa_to_repo(struct clsa *cor_lsa)
2379{
2380
2381
2382 if ( nlsr->debugging )
2383 printf("write_cor_lsa_to_repo called\n");
2384
2385
akmhoquef5537b42013-02-22 08:33:32 -06002386 char *key=(char *)calloc(cor_lsa->header->orig_router->length+4,sizeof(char));
akmhoqueb77b95f2013-02-08 12:28:47 -06002387 make_cor_lsa_key(key,cor_lsa);
2388
2389 struct ccn_charbuf *lsa_data=ccn_charbuf_create();
2390 get_cor_lsa_data(lsa_data,key);
akmhoque323b5e92013-02-21 13:55:15 -06002391
akmhoqueb77b95f2013-02-08 12:28:47 -06002392 if ( nlsr->debugging )
akmhoque0800eda2013-02-21 14:17:52 -06002393 printf("Cor LSA Data: %s \n",ccn_charbuf_as_string(lsa_data));
2394
akmhoqueb77b95f2013-02-08 12:28:47 -06002395 char *lst=(char *)malloc(20);
2396 memset(lst,0,20);
2397 sprintf(lst,"%d",cor_lsa->header->ls_type);
akmhoquef5537b42013-02-22 08:33:32 -06002398 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 -06002399 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);
2400
2401 if ( nlsr->debugging )
2402 printf("Cor LSA Repo Key: %s \n",repo_key);
2403
akmhoquef5537b42013-02-22 08:33:32 -06002404 char *data=calloc(strlen(ccn_charbuf_as_string(lsa_data))+1,sizeof(char));
2405 data=ccn_charbuf_as_string(lsa_data);
2406 data[strlen(data)]='\0';
2407
2408 write_data_to_repo(data, repo_key);
akmhoque0800eda2013-02-21 14:17:52 -06002409
akmhoqueb77b95f2013-02-08 12:28:47 -06002410
2411
2412
2413 free(lst);
2414 free(key);
2415 free(repo_key);
akmhoque4f85aab2013-02-21 13:58:50 -06002416 ccn_charbuf_destroy(&lsa_data);
akmhoqueb77b95f2013-02-08 12:28:47 -06002417}
2418
2419void
2420make_cor_lsa_key_by_router_name(char *key,char *router_name)
2421{
2422 memcpy(key+strlen(key),router_name,strlen(router_name));
2423 memcpy(key+strlen(key),"/",1);
2424 char ls_type[2];
2425 sprintf(ls_type,"%d",LS_TYPE_COR);
2426 memcpy(key+strlen(key),ls_type,strlen(ls_type));
2427 key[strlen(key)]='\0';
2428}
2429
2430
2431double
2432get_hyperbolic_r(char *router)
2433{
2434 double ret=-1.0;
2435 char *cor_lsa_key=(char *)calloc(strlen(router)+4,sizeof(char));
2436 make_cor_lsa_key_by_router_name(cor_lsa_key,router);
2437
2438
2439 struct clsa *cor_lsa;
2440 struct hashtb_enumerator ee;
2441 struct hashtb_enumerator *e = &ee;
2442 int res;
2443
2444 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2445 res = hashtb_seek(e, cor_lsa_key, strlen(cor_lsa_key), 0);
2446
2447 if ( res == HT_OLD_ENTRY)
2448 {
2449 cor_lsa=e->data;
2450 ret=cor_lsa->cor_r;
2451 }
2452 else if(res == HT_NEW_ENTRY)
2453 {
2454 hashtb_delete(e);
2455 }
2456
2457 hashtb_end(e);
2458
2459 free(cor_lsa_key);
2460 return ret;
2461}
2462
2463double
2464get_hyperbolic_theta(char *router)
2465{
akmhoquef5537b42013-02-22 08:33:32 -06002466 double ret=-1.0;
akmhoqueb77b95f2013-02-08 12:28:47 -06002467 char *cor_lsa_key=(char *)calloc(strlen(router)+4,sizeof(char));
2468 make_cor_lsa_key_by_router_name(cor_lsa_key,router);
2469
2470 struct clsa *cor_lsa;
2471 struct hashtb_enumerator ee;
2472 struct hashtb_enumerator *e = &ee;
2473 int res;
2474
2475 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2476 res = hashtb_seek(e, cor_lsa_key, strlen(cor_lsa_key), 0);
2477
2478 if ( res == HT_OLD_ENTRY)
2479 {
2480 cor_lsa=e->data;
2481 ret=cor_lsa->cor_theta;
2482 }
2483 else if(res == HT_NEW_ENTRY)
2484 {
2485 hashtb_delete(e);
2486 }
2487
2488 hashtb_end(e);
2489
2490 free(cor_lsa_key);
2491 return ret;
2492}
akmhoquead584782013-02-22 10:56:03 -06002493
2494
2495void
2496destroy_name_lsdb(void)
2497{
2498 int i, name_lsdb_element;
2499 struct nlsa *name_lsa;
2500
2501 struct hashtb_enumerator ee;
2502 struct hashtb_enumerator *e = &ee;
2503
2504 hashtb_start(nlsr->lsdb->name_lsdb, e);
2505 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
2506
2507 for(i=0;i<name_lsdb_element;i++)
2508 {
2509 name_lsa=e->data;
2510 destroy_name_lsa_component(name_lsa);
2511 hashtb_next(e);
2512 }
2513
2514 hashtb_end(e);
2515
2516 if ( nlsr->lsdb->name_lsdb )
2517 hashtb_destroy(&nlsr->lsdb->name_lsdb);
2518}
2519
2520void
2521destroy_adj_lsdb(void)
2522{
2523 int i, adj_lsdb_element;
2524 struct alsa *adj_lsa;
2525
2526 struct hashtb_enumerator ee;
2527 struct hashtb_enumerator *e = &ee;
2528
2529 hashtb_start(nlsr->lsdb->adj_lsdb, e);
2530 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
2531
2532 for(i=0;i<adj_lsdb_element;i++)
2533 {
2534 adj_lsa=e->data;
2535 destroy_adj_lsa_component(adj_lsa);
2536 hashtb_next(e);
2537 }
2538
2539 hashtb_end(e);
2540 if(nlsr->lsdb->adj_lsdb )
2541 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
2542}
2543
2544void
2545destroy_cor_lsdb(void)
2546{
2547 int i, cor_lsdb_element;
2548 struct clsa *cor_lsa;
2549
2550 struct hashtb_enumerator ee;
2551 struct hashtb_enumerator *e = &ee;
2552
2553 hashtb_start(nlsr->lsdb->cor_lsdb, e);
2554 cor_lsdb_element=hashtb_n(nlsr->lsdb->cor_lsdb);
2555
2556 for(i=0;i<cor_lsdb_element;i++)
2557 {
2558 cor_lsa=e->data;
2559 destroy_cor_lsa_component(cor_lsa);
2560 hashtb_next(e);
2561 }
2562
2563 hashtb_end(e);
2564 if(nlsr->lsdb->cor_lsdb )
2565 hashtb_destroy(&nlsr->lsdb->cor_lsdb);
2566}
2567
2568void
2569destroy_lsdb(void)
2570{
2571 destroy_name_lsdb();
2572 destroy_adj_lsdb();
2573 destroy_cor_lsdb();
2574
2575 if ( nlsr->lsdb->lsdb_version )
2576 free(nlsr->lsdb->lsdb_version);
2577 if ( nlsr->lsdb )
2578 free(nlsr->lsdb);
2579}