blob: bec9869b882287a539a161e94e7901bbbc4fb27e [file] [log] [blame]
akmhoquef1161eb2012-08-21 09:37:21 -04001#include<stdio.h>
2#include<string.h>
3#include<stdlib.h>
4#include <unistd.h>
5#include <getopt.h>
6#include <sys/time.h>
7#include <assert.h>
8#ifdef HAVE_CONFIG_H
9#include <config.h>
10#endif
11
12
13#include <ccn/ccn.h>
14#include <ccn/uri.h>
15#include <ccn/keystore.h>
16#include <ccn/signing.h>
17#include <ccn/schedule.h>
18#include <ccn/hashtb.h>
19
20#include "nlsr.h"
akmhoque03004e62012-09-06 01:12:28 -050021#include "nlsr_ndn.h"
akmhoquef1161eb2012-08-21 09:37:21 -040022#include "nlsr_lsdb.h"
akmhoquef71d9082012-08-22 12:51:53 -040023#include "utility.h"
akmhoque03004e62012-09-06 01:12:28 -050024#include "nlsr_npl.h"
akmhoque53f64222012-09-05 13:57:51 -050025#include "nlsr_adl.h"
akmhoque29c1db52012-09-07 14:47:43 -050026#include "nlsr_route.h"
akmhoque3560cb62012-09-09 10:52:30 -050027#include "nlsr_npt.h"
akmhoque438b07e2012-08-21 10:13:57 -040028
akmhoquef71d9082012-08-22 12:51:53 -040029void
akmhoque53f64222012-09-05 13:57:51 -050030set_new_lsdb_version(void)
akmhoquef71d9082012-08-22 12:51:53 -040031{
akmhoquef71d9082012-08-22 12:51:53 -040032
akmhoque03004e62012-09-06 01:12:28 -050033 char *time_stamp=(char *)malloc(20);
34 memset(time_stamp,0,20);
35 get_current_timestamp_micro(time_stamp);
akmhoquef71d9082012-08-22 12:51:53 -040036
akmhoque03004e62012-09-06 01:12:28 -050037 free(nlsr->lsdb->lsdb_version);
38 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
39 memset(nlsr->lsdb->lsdb_version,0,strlen(time_stamp)+1);
40 memcpy(nlsr->lsdb->lsdb_version,time_stamp,strlen(time_stamp)+1);
41
42 free(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -050043
akmhoquef71d9082012-08-22 12:51:53 -040044}
45
akmhoque03004e62012-09-06 01:12:28 -050046void
47make_name_lsa_key(char *key, char *orig_router, int ls_type, long int ls_id)
48{
49
50
akmhoque7b791452012-10-30 11:24:56 -050051 //printf("Orig Router: %s LS Type: %d LS Id: %ld\n",orig_router,ls_type,ls_id);
akmhoque03004e62012-09-06 01:12:28 -050052
53 char lst[2];
54 memset(lst,0,2);
55 sprintf(lst,"%d",ls_type);
56
57 char lsid[10];
58 memset(lsid,0,10);
59 sprintf(lsid,"%ld",ls_id);
60
61 memcpy(key+strlen(key),orig_router,strlen(orig_router));
62 memcpy(key+strlen(key),"/",1);
63 memcpy(key+strlen(key),lst,strlen(lst));
64 memcpy(key+strlen(key),"/",1);
65 memcpy(key+strlen(key),lsid,strlen(lsid));
66
akmhoque3171d652012-11-13 11:44:33 -060067 //printf("Key: %s\n",key);
akmhoque03004e62012-09-06 01:12:28 -050068
69}
akmhoque53f64222012-09-05 13:57:51 -050070
akmhoquef71d9082012-08-22 12:51:53 -040071void
akmhoque53f64222012-09-05 13:57:51 -050072build_and_install_name_lsas(void)
akmhoquef71d9082012-08-22 12:51:53 -040073{
akmhoque7b791452012-10-30 11:24:56 -050074 if ( nlsr->debugging )
75 printf("build_and_install_name_lsas called \n");
76 if ( nlsr->detailed_logging )
77 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_name_lsas called\n");
akmhoquef71d9082012-08-22 12:51:53 -040078
akmhoquef71d9082012-08-22 12:51:53 -040079 int i, npl_element;
akmhoque3171d652012-11-13 11:44:33 -060080 //struct name_prefix *np;
81 struct name_prefix_list_entry *npe;
akmhoquef71d9082012-08-22 12:51:53 -040082
83 struct hashtb_enumerator ee;
84 struct hashtb_enumerator *e = &ee;
85
86 hashtb_start(nlsr->npl, e);
87 npl_element=hashtb_n(nlsr->npl);
88
89 for(i=0;i<npl_element;i++)
90 {
akmhoque3171d652012-11-13 11:44:33 -060091 npe=e->data;
akmhoque53f64222012-09-05 13:57:51 -050092 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
akmhoque3171d652012-11-13 11:44:33 -060093 build_name_lsa(name_lsa,npe->np);
akmhoque03004e62012-09-06 01:12:28 -050094
akmhoquef71d9082012-08-22 12:51:53 -040095 install_name_lsa(name_lsa);
akmhoque3171d652012-11-13 11:44:33 -060096 update_nlsa_id_for_name_in_npl(npe->np,name_lsa->header->ls_id);
akmhoque03004e62012-09-06 01:12:28 -050097 free(name_lsa->header->orig_router->name);
98 free(name_lsa->header->orig_router);
99 free(name_lsa->header);
100 free(name_lsa->name_prefix->name);
101 free(name_lsa->name_prefix);
akmhoquef71d9082012-08-22 12:51:53 -0400102 free(name_lsa);
akmhoquef71d9082012-08-22 12:51:53 -0400103 hashtb_next(e);
104 }
105
akmhoque53f64222012-09-05 13:57:51 -0500106 hashtb_end(e);
akmhoque3171d652012-11-13 11:44:33 -0600107
108 print_name_prefix_from_npl();
109
110}
111
112void
113build_and_install_single_name_lsa(struct name_prefix *np)
114{
115 if ( nlsr->debugging )
116 printf("build_and_install_single_name_lsa called \n");
117 if ( nlsr->detailed_logging )
118 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_single_name_lsa called\n");
119
120 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
121 build_name_lsa(name_lsa,np);
122
123 install_name_lsa(name_lsa);
124 update_nlsa_id_for_name_in_npl(np,name_lsa->header->ls_id);
125
126 free(name_lsa->header->orig_router->name);
127 free(name_lsa->header->orig_router);
128 free(name_lsa->header);
129 free(name_lsa->name_prefix->name);
130 free(name_lsa->name_prefix);
131 free(name_lsa);
132
133 print_name_prefix_from_npl();
akmhoquef71d9082012-08-22 12:51:53 -0400134
akmhoquef71d9082012-08-22 12:51:53 -0400135}
136
akmhoque53f64222012-09-05 13:57:51 -0500137void
138build_name_lsa(struct nlsa *name_lsa, struct name_prefix *np)
139{
akmhoque53f64222012-09-05 13:57:51 -0500140 name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header ));
141 name_lsa->header->ls_type=LS_TYPE_NAME;
142
akmhoque03004e62012-09-06 01:12:28 -0500143 char *time_stamp=(char *)malloc(20);
144 memset(time_stamp,0,20);
145 get_current_timestamp_micro(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -0500146
akmhoque03004e62012-09-06 01:12:28 -0500147 name_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1); //free
akmhoque53f64222012-09-05 13:57:51 -0500148 memset(name_lsa->header->orig_time,0,strlen(time_stamp)+1);
149 memcpy(name_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
akmhoque03004e62012-09-06 01:12:28 -0500150
151 free(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -0500152
153 name_lsa->header->ls_id=++nlsr->nlsa_id;
154 name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoque03004e62012-09-06 01:12:28 -0500155 name_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1);
156 memset(name_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
157 memcpy(name_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
158 name_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
akmhoque53f64222012-09-05 13:57:51 -0500159 name_lsa->header->isValid=1;
160
161
162 name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
163 name_lsa->name_prefix->name=(char *)malloc(np->length);
164 memcpy(name_lsa->name_prefix->name,np->name,np->length);
165 name_lsa->name_prefix->length=np->length;
166
167}
168
akmhoque53f64222012-09-05 13:57:51 -0500169void
170install_name_lsa(struct nlsa *name_lsa)
171{
akmhoque53f64222012-09-05 13:57:51 -0500172
akmhoqueffacaa82012-09-13 17:48:30 -0500173 char *time_stamp=(char *)malloc(20);
174 memset(time_stamp,0,20);
175 get_current_timestamp_micro(time_stamp);
akmhoque7b791452012-10-30 11:24:56 -0500176 //long int lsa_life_time=get_time_diff(time_stamp,name_lsa->header->orig_time);
akmhoqueffacaa82012-09-13 17:48:30 -0500177
akmhoque7b791452012-10-30 11:24:56 -0500178 //printf("time difference: %ld \n",lsa_life_time);
akmhoqueffacaa82012-09-13 17:48:30 -0500179
180
181 char lst[2];
182 memset(lst,0,2);
183 sprintf(lst,"%d",name_lsa->header->ls_type);
184
185 char lsid[10];
186 memset(lsid,0,10);
187 sprintf(lsid,"%ld",name_lsa->header->ls_id);
akmhoque03004e62012-09-06 01:12:28 -0500188
189
akmhoqueffacaa82012-09-13 17:48:30 -0500190 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
191 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
akmhoque03004e62012-09-06 01:12:28 -0500192
193
akmhoque3171d652012-11-13 11:44:33 -0600194 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
195 if ( nlsr->debugging )
196 printf("Key:%s Length:%d\n",key,(int)strlen(key));
197 if ( nlsr->detailed_logging )
198 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Key:%s Length:%d\n",key,(int)strlen(key));
199
akmhoque53f64222012-09-05 13:57:51 -0500200
akmhoqueffacaa82012-09-13 17:48:30 -0500201 struct nlsa *new_name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa )); //free
akmhoque53f64222012-09-05 13:57:51 -0500202
akmhoqueffacaa82012-09-13 17:48:30 -0500203 struct hashtb_enumerator ee;
204 struct hashtb_enumerator *e = &ee;
205 int res;
akmhoque53f64222012-09-05 13:57:51 -0500206
akmhoqueffacaa82012-09-13 17:48:30 -0500207 hashtb_start(nlsr->lsdb->name_lsdb, e);
208 res = hashtb_seek(e, key, strlen(key), 0);
akmhoque53f64222012-09-05 13:57:51 -0500209
akmhoqueffacaa82012-09-13 17:48:30 -0500210 if(res == HT_NEW_ENTRY )
akmhoque3560cb62012-09-09 10:52:30 -0500211 {
akmhoqueffacaa82012-09-13 17:48:30 -0500212
akmhoque7b791452012-10-30 11:24:56 -0500213 if ( nlsr->debugging )
214 printf("New Name LSA... Adding to LSDB\n");
215 if ( nlsr->detailed_logging )
216 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Name LSA... Adding to LSDB\n");
217
218
akmhoqueffacaa82012-09-13 17:48:30 -0500219 new_name_lsa = e->data;
220
221 new_name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header )); //free
222 new_name_lsa->header->ls_type=name_lsa->header->ls_type;
223
224 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
225 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
226 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
227
228 new_name_lsa->header->ls_id=name_lsa->header->ls_id;
229 new_name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free
230 new_name_lsa->header->orig_router->name=(char *)malloc(name_lsa->header->orig_router->length);
231 memcpy(new_name_lsa->header->orig_router->name,name_lsa->header->orig_router->name,name_lsa->header->orig_router->length);
232 new_name_lsa->header->orig_router->length=name_lsa->header->orig_router->length;
233 new_name_lsa->header->isValid=name_lsa->header->isValid;
234
235
236 new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free
237 new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
238 memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
239 new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
240
akmhoque7b791452012-10-30 11:24:56 -0500241 if ( nlsr->debugging )
242 {
243 printf("New Name LSA Added....\n");
244 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
245 }
246 if ( nlsr->detailed_logging )
247 {
248 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Name LSA Added....\n");
249 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
250 }
akmhoqueffacaa82012-09-13 17:48:30 -0500251 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500252 if ( nlsr->debugging )
253 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
254 if ( nlsr->detailed_logging )
255 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500256
akmhoque9e9fc722012-09-26 14:03:25 -0500257
akmhoquede61ba92012-09-20 22:19:12 -0500258 int num_next_hop=get_number_of_next_hop(new_name_lsa->header->orig_router->name);
akmhoquede61ba92012-09-20 22:19:12 -0500259 if ( num_next_hop < 0 )
akmhoque3560cb62012-09-09 10:52:30 -0500260 {
akmhoquede61ba92012-09-20 22:19:12 -0500261 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 -0500262 if ( check == HT_NEW_ENTRY )
263 {
akmhoque7b791452012-10-30 11:24:56 -0500264 if ( nlsr->debugging )
265 printf("Added in npt \n");
266 if ( nlsr->detailed_logging )
267 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
akmhoqueffacaa82012-09-13 17:48:30 -0500268 }
akmhoque3560cb62012-09-09 10:52:30 -0500269 }
akmhoqueffacaa82012-09-13 17:48:30 -0500270 else
akmhoque3560cb62012-09-09 10:52:30 -0500271 {
akmhoquede61ba92012-09-20 22:19:12 -0500272 int *faces=malloc(num_next_hop*sizeof(int));
273 int *route_costs=malloc(num_next_hop*sizeof(int));
274 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name,faces,route_costs);
akmhoque7b791452012-10-30 11:24:56 -0500275 if ( nlsr->debugging )
276 {
277 printf("Printing from install_name_lsa \n");
278 int j;
279 for(j=0;j<num_next_hop;j++)
280 printf("Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
281 }
282 if ( nlsr->detailed_logging )
283 {
284 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Printing from install_name_lsa \n");
285 int j;
286 for(j=0;j<num_next_hop;j++)
287 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
288 }
akmhoquede61ba92012-09-20 22:19:12 -0500289 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 -0500290 if ( check == HT_NEW_ENTRY )
291 {
akmhoque7b791452012-10-30 11:24:56 -0500292 if ( nlsr->debugging )
293 printf("Added in npt \n");
294 if ( nlsr->detailed_logging )
295 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
akmhoqueffacaa82012-09-13 17:48:30 -0500296 }
akmhoquede61ba92012-09-20 22:19:12 -0500297 free(faces);
298 free(route_costs);
akmhoqueffacaa82012-09-13 17:48:30 -0500299
akmhoque3560cb62012-09-09 10:52:30 -0500300 }
akmhoquede61ba92012-09-20 22:19:12 -0500301
akmhoque9e9fc722012-09-26 14:03:25 -0500302 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
303 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
304 write_log_for_name_lsa(new_name_lsa);
305 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500306
akmhoquede61ba92012-09-20 22:19:12 -0500307 free(time_stamp);
akmhoqueffacaa82012-09-13 17:48:30 -0500308
309 }
310 else if(res == HT_OLD_ENTRY)
311 {
312 new_name_lsa=e->data;
akmhoque14b3f342012-09-14 10:39:02 -0500313 if(strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time)<0)
akmhoqueffacaa82012-09-13 17:48:30 -0500314 {
akmhoque7b791452012-10-30 11:24:56 -0500315 if ( nlsr->debugging )
316 printf("Older Adj LSA. Discarded... \n");
317 if ( nlsr->detailed_logging )
318 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Older Adj LSA. Discarded...\n");
akmhoque14b3f342012-09-14 10:39:02 -0500319 }
320 else if( strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time) == 0 )
321 {
akmhoque7b791452012-10-30 11:24:56 -0500322 if ( nlsr->debugging )
323 printf("Duplicate Adj LSA. Discarded... \n");
324 if ( nlsr->detailed_logging )
325 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Duplicate Adj LSA. Discarded...\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500326 }
327 else
328 {
329 if ( name_lsa->header->isValid == 0 )
330 {
331 // have to call to delete npt table entry
akmhoque3cced642012-09-24 16:20:20 -0500332 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 -0500333
334 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name)!= 0)
335 {
akmhoque9e9fc722012-09-26 14:03:25 -0500336 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
337 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
338 write_log_for_name_lsa(new_name_lsa);
339 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
340
akmhoqueffacaa82012-09-13 17:48:30 -0500341 hashtb_delete(e);
akmhoque7b791452012-10-30 11:24:56 -0500342 if ( nlsr->debugging )
343 printf("isValid bit not set for Router %s so LSA Deleted from LSDB\n",name_lsa->header->orig_router->name);
344 if ( nlsr->detailed_logging )
345 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 -0500346 }
347 else
348 {
349 new_name_lsa->header->isValid=name_lsa->header->isValid;
350 free(new_name_lsa->header->orig_time);
351 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
352 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
353 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
354 }
akmhoque7b791452012-10-30 11:24:56 -0500355 if ( nlsr->debugging )
356 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
357 if ( nlsr->detailed_logging )
358 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500359 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500360 if ( nlsr->debugging )
361 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
362 if ( nlsr->detailed_logging )
363 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500364 }
365 else
366 {
367 int is_npt_update=0;
368 if ( strcmp(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name) != 0 )
369 {
370 is_npt_update=1;
akmhoque3cced642012-09-24 16:20:20 -0500371 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 -0500372 }
373
374 // copying LSA content with header
akmhoque9e9fc722012-09-26 14:03:25 -0500375 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
376 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
377 write_log_for_name_lsa(new_name_lsa);
378 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
379
akmhoqueffacaa82012-09-13 17:48:30 -0500380
381 free(new_name_lsa->header->orig_time);
382 new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1);
383 memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1);
384 memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1);
385
386 new_name_lsa->header->isValid=name_lsa->header->isValid;
387
388 free(new_name_lsa->name_prefix->name);
389 free(new_name_lsa->name_prefix);
390 new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
391 new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length);
392 memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length);
393 new_name_lsa->name_prefix->length=name_lsa->name_prefix->length;
394
akmhoque9e9fc722012-09-26 14:03:25 -0500395
396 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
397 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
398 write_log_for_name_lsa(new_name_lsa);
399 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
400
akmhoque7b791452012-10-30 11:24:56 -0500401 if ( nlsr->debugging )
402 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
403 if ( nlsr->detailed_logging )
404 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
405
akmhoqueffacaa82012-09-13 17:48:30 -0500406 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500407
408 if ( nlsr->debugging )
409 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
410 if ( nlsr->detailed_logging )
411 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
412
akmhoqueffacaa82012-09-13 17:48:30 -0500413
414 if( is_npt_update == 1 )
415 {
akmhoquede61ba92012-09-20 22:19:12 -0500416 //struct hashtb *face_list;
417 int num_next_hop=get_number_of_next_hop(new_name_lsa->header->orig_router->name);
418 if ( num_next_hop < 0 )
akmhoqueffacaa82012-09-13 17:48:30 -0500419 {
akmhoquede61ba92012-09-20 22:19:12 -0500420
421 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 -0500422 if ( check == HT_NEW_ENTRY )
423 {
akmhoque7b791452012-10-30 11:24:56 -0500424 if ( nlsr->debugging )
425 printf("Added in npt \n");
426 if ( nlsr->detailed_logging )
427 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
akmhoqueffacaa82012-09-13 17:48:30 -0500428 }
429 }
430 else
431 {
akmhoquede61ba92012-09-20 22:19:12 -0500432 int *faces=malloc(num_next_hop*sizeof(int));
433 int *route_costs=malloc(num_next_hop*sizeof(int));
434 int next_hop=get_next_hop(new_name_lsa->header->orig_router->name,faces,route_costs);
akmhoque7b791452012-10-30 11:24:56 -0500435
436 if ( nlsr->debugging )
437 {
438 printf("Printing from install_name_lsa \n");
439 int j;
440 for(j=0;j<num_next_hop;j++)
akmhoquede61ba92012-09-20 22:19:12 -0500441 printf("Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
akmhoque7b791452012-10-30 11:24:56 -0500442 }
443 if ( nlsr->detailed_logging )
444 {
445 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Printing from install_name_lsa \n");
446 int j;
447 for(j=0;j<num_next_hop;j++)
448 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face: %d Route Cost: %d \n",faces[j],route_costs[j]);
449 }
450
akmhoquede61ba92012-09-20 22:19:12 -0500451
452 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 -0500453 if ( check == HT_NEW_ENTRY )
454 {
akmhoque7b791452012-10-30 11:24:56 -0500455 //printf("Added in npt \n");
456 if ( nlsr->debugging )
457 printf("Added in npt \n");
458 if ( nlsr->detailed_logging )
459 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Added in npt \n");
akmhoqueffacaa82012-09-13 17:48:30 -0500460 }
akmhoquede61ba92012-09-20 22:19:12 -0500461 free(faces);
462 free(route_costs);
463
akmhoqueffacaa82012-09-13 17:48:30 -0500464 }
akmhoquede61ba92012-09-20 22:19:12 -0500465
akmhoqueffacaa82012-09-13 17:48:30 -0500466 }
467 }
468 }
469
akmhoque3560cb62012-09-09 10:52:30 -0500470 }
akmhoque53f64222012-09-05 13:57:51 -0500471
akmhoqueffacaa82012-09-13 17:48:30 -0500472 hashtb_end(e);
akmhoque53f64222012-09-05 13:57:51 -0500473
akmhoqueffacaa82012-09-13 17:48:30 -0500474 free(key);
akmhoque53f64222012-09-05 13:57:51 -0500475}
476
akmhoque9e9fc722012-09-26 14:03:25 -0500477void
478write_log_for_name_lsa(struct nlsa *name_lsa)
479{
480
481 writeLogg(__FILE__,__FUNCTION__,__LINE__,"-----------Name LSA Content---------------\n");
482 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s\n",name_lsa->header->orig_router->name);
483 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router Length: %d\n",name_lsa->header->orig_router->length);
akmhoque34b99f92012-09-27 12:24:27 -0500484 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Type: %d\n",name_lsa->header->ls_type);
485 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Id: %ld\n",name_lsa->header->ls_id);
486 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Time: %s\n",name_lsa->header->orig_time);
487 writeLogg(__FILE__,__FUNCTION__,__LINE__," Is Valid: %d\n",name_lsa->header->isValid);
akmhoque9e9fc722012-09-26 14:03:25 -0500488 writeLogg(__FILE__,__FUNCTION__,__LINE__," LSA Data \n");
489 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix: %s\n",name_lsa->name_prefix->name);
490 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix Length: %d\n",name_lsa->name_prefix->length);
491 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
492}
akmhoque53f64222012-09-05 13:57:51 -0500493
akmhoque03004e62012-09-06 01:12:28 -0500494void
495print_name_lsa(struct nlsa *name_lsa)
akmhoque53f64222012-09-05 13:57:51 -0500496{
akmhoque53f64222012-09-05 13:57:51 -0500497
akmhoque7b791452012-10-30 11:24:56 -0500498 if ( nlsr->debugging )
499 {
500 printf("-----------Name LSA Content---------------\n");
501 printf(" Origination Router : %s\n",name_lsa->header->orig_router->name);
502 printf(" Origination Router Length: %d\n",name_lsa->header->orig_router->length);
503 printf(" LS Type : %d\n",name_lsa->header->ls_type);
504 printf(" LS Id : %ld\n",name_lsa->header->ls_id);
505 printf(" Origination Time : %s\n",name_lsa->header->orig_time);
506 printf(" Is Valid : %d\n",name_lsa->header->isValid);
507 printf(" LSA Data \n");
508 printf(" Name Prefix: : %s\n",name_lsa->name_prefix->name);
509 printf(" Name Prefix Length : %d\n",name_lsa->name_prefix->length);
510
511 printf("\n");
512 }
akmhoquef71d9082012-08-22 12:51:53 -0400513}
514
515void
516print_name_lsdb(void)
517{
akmhoque7b791452012-10-30 11:24:56 -0500518 if ( nlsr->debugging )
519 printf("print_name_lsdb called \n");
akmhoque53f64222012-09-05 13:57:51 -0500520 int i, name_lsdb_element;
akmhoquef71d9082012-08-22 12:51:53 -0400521 struct nlsa *name_lsa;
522
523 struct hashtb_enumerator ee;
524 struct hashtb_enumerator *e = &ee;
525
526 hashtb_start(nlsr->lsdb->name_lsdb, e);
akmhoque53f64222012-09-05 13:57:51 -0500527 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
akmhoquef71d9082012-08-22 12:51:53 -0400528
akmhoque53f64222012-09-05 13:57:51 -0500529 for(i=0;i<name_lsdb_element;i++)
akmhoquef71d9082012-08-22 12:51:53 -0400530 {
akmhoque7b791452012-10-30 11:24:56 -0500531 if ( nlsr->debugging )
532 printf("-----------Name LSA (%d)---------------\n",i+1);
akmhoquef71d9082012-08-22 12:51:53 -0400533 name_lsa=e->data;
akmhoque53f64222012-09-05 13:57:51 -0500534 print_name_lsa(name_lsa);
akmhoquef71d9082012-08-22 12:51:53 -0400535 hashtb_next(e);
536 }
537
538 hashtb_end(e);
539
akmhoque3171d652012-11-13 11:44:33 -0600540 if ( nlsr->debugging )
541 printf("\n");
542 if ( nlsr->detailed_logging )
543 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoque53f64222012-09-05 13:57:51 -0500544}
545
akmhoque03004e62012-09-06 01:12:28 -0500546
547void
548build_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 -0500549{
akmhoque7b791452012-10-30 11:24:56 -0500550 if ( nlsr->debugging )
551 printf("build_and_install_others_name_lsa called \n");
552 if ( nlsr->detailed_logging )
553 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_others_name_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500554
akmhoque03004e62012-09-06 01:12:28 -0500555 struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
556 build_others_name_lsa(name_lsa,orig_router,ls_type,ls_id,orig_time, isValid,np);
557 print_name_lsa(name_lsa);
558 install_name_lsa(name_lsa);
559 print_name_lsdb();
akmhoquede61ba92012-09-20 22:19:12 -0500560 print_npt();
akmhoque53f64222012-09-05 13:57:51 -0500561
akmhoque03004e62012-09-06 01:12:28 -0500562 free(name_lsa->header->orig_router->name);
563 free(name_lsa->header->orig_router);
564 free(name_lsa->header);
565 free(name_lsa->name_prefix->name);
566 free(name_lsa->name_prefix);
567 free(name_lsa);
akmhoque53f64222012-09-05 13:57:51 -0500568
569}
570
571void
akmhoque03004e62012-09-06 01:12:28 -0500572build_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 -0500573{
akmhoque7b791452012-10-30 11:24:56 -0500574 if ( nlsr->debugging )
575 printf("build_others_name_lsa called\n");
576 if ( nlsr->detailed_logging )
577 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_others_name_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -0500578
akmhoque03004e62012-09-06 01:12:28 -0500579 name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header ));
580 name_lsa->header->ls_type=LS_TYPE_NAME;
akmhoque53f64222012-09-05 13:57:51 -0500581
akmhoque03004e62012-09-06 01:12:28 -0500582 name_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
583 memset(name_lsa->header->orig_time,0,strlen(orig_time)+1);
584 memcpy(name_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
akmhoque53f64222012-09-05 13:57:51 -0500585
akmhoque03004e62012-09-06 01:12:28 -0500586 name_lsa->header->ls_id=ls_id;
587 name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
588 name_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
589 memset(name_lsa->header->orig_router->name,0,strlen(orig_router)+1);
590 memcpy(name_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
591 name_lsa->header->orig_router->length=strlen(orig_router)+1;
592 name_lsa->header->isValid=isValid;
akmhoque53f64222012-09-05 13:57:51 -0500593
akmhoque03004e62012-09-06 01:12:28 -0500594 name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
595 name_lsa->name_prefix->name=(char *)malloc(strlen(np)+1);
596 memset(name_lsa->name_prefix->name,0,strlen(np)+1);
597 memcpy(name_lsa->name_prefix->name,np,strlen(np)+1);
598 name_lsa->name_prefix->length=strlen(np)+1;
akmhoquef71d9082012-08-22 12:51:53 -0400599}
akmhoqued79438d2012-08-27 13:31:42 -0500600
601
akmhoqueffacaa82012-09-13 17:48:30 -0500602
603
akmhoque53f64222012-09-05 13:57:51 -0500604void
605make_adj_lsa_key(char *key,struct alsa *adj_lsa)
akmhoqued79438d2012-08-27 13:31:42 -0500606{
akmhoque53f64222012-09-05 13:57:51 -0500607 memcpy(key+strlen(key),adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
608 memcpy(key+strlen(key),"/",1);
609 char ls_type[2];
610 sprintf(ls_type,"%d",adj_lsa->header->ls_type);
611 memcpy(key+strlen(key),ls_type,strlen(ls_type));
612 key[strlen(key)]='\0';
613}
614
akmhoque03004e62012-09-06 01:12:28 -0500615int
616build_and_install_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
617{
akmhoqueffacaa82012-09-13 17:48:30 -0500618 if(flags == CCN_SCHEDULE_CANCEL)
619 {
620 return -1;
621 }
622
623 nlsr_lock();
624
akmhoque7b791452012-10-30 11:24:56 -0500625 if ( nlsr->debugging )
626 printf("build_and_install_adj_lsa called \n");
627 if ( nlsr->detailed_logging )
628 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_adj_lsa called \n");
629
630 if ( nlsr->debugging )
631 printf("adj_build_flag = %d \n",nlsr->adj_build_flag);
632 if ( nlsr->detailed_logging )
633 writeLogg(__FILE__,__FUNCTION__,__LINE__,"adj_build_flag = %d \n",nlsr->adj_build_flag);
akmhoque03004e62012-09-06 01:12:28 -0500634
635 if(nlsr->adj_build_flag > 0)
636 {
akmhoque7b791452012-10-30 11:24:56 -0500637 if ( nlsr->debugging )
638 printf("is_adj_lsa_build = %d \n",is_adj_lsa_build());
639 if ( nlsr->detailed_logging )
640 writeLogg(__FILE__,__FUNCTION__,__LINE__,"is_adj_lsa_build = %d \n",is_adj_lsa_build());
641
akmhoque03004e62012-09-06 01:12:28 -0500642 if ( is_adj_lsa_build()> 0)
643 {
644 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
645 build_adj_lsa(adj_lsa);
646 install_adj_lsa(adj_lsa);
647
648 free(adj_lsa->header->orig_router->name);
649 free(adj_lsa->header->orig_router);
650 free(adj_lsa->header->orig_time);
651 free(adj_lsa->header);
652 free(adj_lsa->body);
653 free(adj_lsa);
654 nlsr->adj_build_flag=0;
655 print_adj_lsdb();
656 }
657 else
658 {
akmhoque7b791452012-10-30 11:24:56 -0500659 if ( nlsr->debugging )
660 printf("Can not build adj LSA now\n");
661 if ( nlsr->detailed_logging )
662 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Can not build adj LSA now\n");
akmhoque03004e62012-09-06 01:12:28 -0500663 }
664 }
665 nlsr->is_build_adj_lsa_sheduled=0;
akmhoqueffacaa82012-09-13 17:48:30 -0500666
667 nlsr_unlock();
668
akmhoque03004e62012-09-06 01:12:28 -0500669 return 0;
670}
671
672
673void
674build_adj_lsa(struct alsa * adj_lsa)
675{
akmhoque7b791452012-10-30 11:24:56 -0500676 if ( nlsr->debugging )
677 printf("build_adj_lsa called \n");
678 if ( nlsr->detailed_logging )
679 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_adj_lsa called \n");
akmhoque03004e62012-09-06 01:12:28 -0500680
681 int no_link=no_active_nbr();
akmhoque7b791452012-10-30 11:24:56 -0500682
683 //printf("Number of link in Adjacent LSA: %d\n",no_link);
akmhoque03004e62012-09-06 01:12:28 -0500684
685 /*Filling Up Header Data */
686 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
687 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
688 adj_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1);
689 memset(adj_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1);
690 memcpy(adj_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1);
691 adj_lsa->header->orig_router->length=strlen(nlsr->router_name)+1;
692
693 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
694
695 char *time_stamp=(char *)malloc(20);
696 memset(time_stamp,0,20);
697 get_current_timestamp_micro(time_stamp);
698
699 adj_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1);
700 memset(adj_lsa->header->orig_time,0,strlen(time_stamp)+1);
701 memcpy(adj_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1);
702 free(time_stamp);
703
704
705 /* Filling Up Body Data */
706
707 adj_lsa->no_link=no_link;
akmhoque03004e62012-09-06 01:12:28 -0500708
709
710 struct ccn_charbuf *c=ccn_charbuf_create();
711 get_active_nbr_adj_data(c);
712 char *data=ccn_charbuf_as_string(c);
713
714 adj_lsa->body=(char *)malloc(strlen(data)+1);
715 memset(adj_lsa->body,0,strlen(data)+1);
716 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
717 ccn_charbuf_destroy(&c);
718
719
720
721 if( !nlsr->is_send_lsdb_interest_scheduled )
722 {
723 nlsr->event_send_lsdb_interest= ccn_schedule_event(nlsr->sched, 1000, &send_lsdb_interest, NULL, 0);
724 nlsr->is_send_lsdb_interest_scheduled=1;
725 }
726
727 nlsr->adj_build_count++;
728
729
730}
731
732
akmhoque53f64222012-09-05 13:57:51 -0500733void
734install_adj_lsa(struct alsa * adj_lsa)
735{
akmhoqued79438d2012-08-27 13:31:42 -0500736
akmhoque7b791452012-10-30 11:24:56 -0500737 if ( nlsr->debugging )
738 printf("install_adj_lsa called \n");
739 if ( nlsr->detailed_logging )
740 writeLogg(__FILE__,__FUNCTION__,__LINE__,"install_adj_lsa called \n");
741
akmhoque03004e62012-09-06 01:12:28 -0500742
akmhoqueffacaa82012-09-13 17:48:30 -0500743 char *time_stamp=(char *)malloc(20);
744 memset(time_stamp,0,20);
745 get_current_timestamp_micro(time_stamp);
akmhoque7b791452012-10-30 11:24:56 -0500746 //long int lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
akmhoque53f64222012-09-05 13:57:51 -0500747
akmhoque7b791452012-10-30 11:24:56 -0500748 //printf("time difference: %ld \n",lsa_life_time);
akmhoque53f64222012-09-05 13:57:51 -0500749
akmhoqueffacaa82012-09-13 17:48:30 -0500750
751 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
752 memset(key,0,adj_lsa->header->orig_router->length+2);
753 make_adj_lsa_key(key,adj_lsa);
akmhoque7b791452012-10-30 11:24:56 -0500754 //printf("Adjacent LSA key: %s \n",key);
akmhoqueffacaa82012-09-13 17:48:30 -0500755
756 struct alsa *new_adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
757
758 struct hashtb_enumerator ee;
759 struct hashtb_enumerator *e = &ee;
760 int res;
761
762 hashtb_start(nlsr->lsdb->adj_lsdb, e);
763 res = hashtb_seek(e, key, strlen(key), 0);
akmhoque53f64222012-09-05 13:57:51 -0500764
akmhoque03004e62012-09-06 01:12:28 -0500765
akmhoque53f64222012-09-05 13:57:51 -0500766
akmhoque62c0c192012-09-24 07:49:25 -0500767 if(res == HT_NEW_ENTRY)
akmhoque53f64222012-09-05 13:57:51 -0500768 {
akmhoque62c0c192012-09-24 07:49:25 -0500769 if ( adj_lsa->no_link > 0)
770 {
akmhoque3171d652012-11-13 11:44:33 -0600771 if ( nlsr->debugging )
772 printf("New ADJ LSA... Adding to LSDB\n");
773 if ( nlsr->detailed_logging )
774 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New ADJ LSA... Adding to LSDB\n");
akmhoque62c0c192012-09-24 07:49:25 -0500775 new_adj_lsa = e->data;
akmhoque53f64222012-09-05 13:57:51 -0500776
akmhoque62c0c192012-09-24 07:49:25 -0500777 new_adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
778 new_adj_lsa->header->ls_type=adj_lsa->header->ls_type;
779 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
780 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 -0500781
akmhoque62c0c192012-09-24 07:49:25 -0500782 new_adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
783 new_adj_lsa->header->orig_router->name=(char *)malloc(adj_lsa->header->orig_router->length);
784 memcpy(new_adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length);
785 new_adj_lsa->header->orig_router->length=adj_lsa->header->orig_router->length;
akmhoque53f64222012-09-05 13:57:51 -0500786
akmhoque62c0c192012-09-24 07:49:25 -0500787 new_adj_lsa->no_link=adj_lsa->no_link;
akmhoqueffacaa82012-09-13 17:48:30 -0500788
akmhoque62c0c192012-09-24 07:49:25 -0500789 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
790 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
791 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
792
793 add_next_hop_router(new_adj_lsa->header->orig_router->name);
794 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
akmhoque9e9fc722012-09-26 14:03:25 -0500795
796 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
797 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
798 write_log_for_adj_lsa(new_adj_lsa);
799 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoque62c0c192012-09-24 07:49:25 -0500800 }
801 else
802 {
803 hashtb_delete(e);
804 }
akmhoque53f64222012-09-05 13:57:51 -0500805
akmhoque7b791452012-10-30 11:24:56 -0500806 if ( nlsr->debugging )
807 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
808 if ( nlsr->detailed_logging )
809 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
810
akmhoque53f64222012-09-05 13:57:51 -0500811 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500812
813 if ( nlsr->debugging )
814 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
815 if ( nlsr->detailed_logging )
816 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
817
akmhoqueffacaa82012-09-13 17:48:30 -0500818 }
819 else if(res == HT_OLD_ENTRY)
820 {
821 new_adj_lsa = e->data;
822 if(strcmp(adj_lsa->header->orig_time,new_adj_lsa->header->orig_time)<=0)
823 {
akmhoque7b791452012-10-30 11:24:56 -0500824 if ( nlsr->debugging )
825 printf("Older/Duplicate Adj LSA. Discarded...\n");
826 if ( nlsr->detailed_logging )
827 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Older/Duplicate Adj LSA. Discarded...\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500828 }
829 else
830 {
akmhoqueffacaa82012-09-13 17:48:30 -0500831
akmhoque62c0c192012-09-24 07:49:25 -0500832 if ( adj_lsa->no_link > 0)
833 {
akmhoque9e9fc722012-09-26 14:03:25 -0500834 //new_adj_lsa = e->data;
835
836 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
837 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
838 write_log_for_adj_lsa(new_adj_lsa);
839 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoqueffacaa82012-09-13 17:48:30 -0500840
akmhoque62c0c192012-09-24 07:49:25 -0500841 free(new_adj_lsa->header->orig_time);
842 new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1);
843 memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1);
844
845 new_adj_lsa->no_link=adj_lsa->no_link;
akmhoqueffacaa82012-09-13 17:48:30 -0500846
akmhoque62c0c192012-09-24 07:49:25 -0500847 new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1);
848 memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1);
849 memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1);
akmhoqueffacaa82012-09-13 17:48:30 -0500850
akmhoque62c0c192012-09-24 07:49:25 -0500851 add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link);
akmhoque9e9fc722012-09-26 14:03:25 -0500852
853 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
854 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
855 write_log_for_adj_lsa(new_adj_lsa);
856 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoque62c0c192012-09-24 07:49:25 -0500857 }
858 else
859 {
akmhoque9e9fc722012-09-26 14:03:25 -0500860 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
861 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
862 write_log_for_adj_lsa(new_adj_lsa);
863 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
864
akmhoque62c0c192012-09-24 07:49:25 -0500865 hashtb_delete(e);
866 }
akmhoque7b791452012-10-30 11:24:56 -0500867
868 if ( nlsr->debugging )
869 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
870 if ( nlsr->detailed_logging )
871 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
872
akmhoqueffacaa82012-09-13 17:48:30 -0500873 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -0500874
875 if ( nlsr->debugging )
876 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
877 if ( nlsr->detailed_logging )
878 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueffacaa82012-09-13 17:48:30 -0500879 }
880
881 }
882 hashtb_end(e);
883
884 if ( !nlsr->is_route_calculation_scheduled )
885 {
886 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
887 nlsr->is_route_calculation_scheduled=1;
akmhoque53f64222012-09-05 13:57:51 -0500888 }
889
akmhoque03004e62012-09-06 01:12:28 -0500890
akmhoqueffacaa82012-09-13 17:48:30 -0500891 free(key);
892
893 free(time_stamp);
akmhoque53f64222012-09-05 13:57:51 -0500894}
895
896void
akmhoque9e9fc722012-09-26 14:03:25 -0500897write_log_for_adj_lsa_body(const char *body, int no_link)
898{
899 int i=0;
900 char *lsa_data=(char *)malloc(strlen(body)+1);
901 memset( lsa_data,0,strlen(body)+1);
902 memcpy(lsa_data,body,strlen(body)+1);
903 char *sep="|";
904 char *rem;
905 char *rtr_id;
906 char *length;
907 char *face;
908 char *metric;
909
910 if(no_link >0 )
911 {
912 rtr_id=strtok_r(lsa_data,sep,&rem);
913 length=strtok_r(NULL,sep,&rem);
914 face=strtok_r(NULL,sep,&rem);
915 metric=strtok_r(NULL,sep,&rem);
916
917 writeLogg(__FILE__,__FUNCTION__,__LINE__," Link %d \n",i+1);
akmhoque34b99f92012-09-27 12:24:27 -0500918 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router: %s \n",rtr_id);
919 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router Length: %s \n",length);
920 writeLogg(__FILE__,__FUNCTION__,__LINE__," Connecting Face: %s \n",face);
921 writeLogg(__FILE__,__FUNCTION__,__LINE__," Metric: %s \n",metric);
akmhoque9e9fc722012-09-26 14:03:25 -0500922
923
924 for(i=1;i<no_link;i++)
925 {
926 rtr_id=strtok_r(NULL,sep,&rem);
927 length=strtok_r(NULL,sep,&rem);
928 face=strtok_r(NULL,sep,&rem);
929 metric=strtok_r(NULL,sep,&rem);
930 writeLogg(__FILE__,__FUNCTION__,__LINE__," Link %d \n",i+1);
akmhoque34b99f92012-09-27 12:24:27 -0500931 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router: %s \n",rtr_id);
932 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adjacent Router Length: %s \n",length);
933 writeLogg(__FILE__,__FUNCTION__,__LINE__," Connecting Face: %s \n",face);
934 writeLogg(__FILE__,__FUNCTION__,__LINE__," Metric: %s \n",metric);
akmhoque9e9fc722012-09-26 14:03:25 -0500935
936 }
937 }
938
939 free(lsa_data);
940}
941
942
943void
944write_log_for_adj_lsa(struct alsa * adj_lsa)
945{
946 writeLogg(__FILE__,__FUNCTION__,__LINE__,"-----------Adj LSA Content---------------\n");
akmhoque34b99f92012-09-27 12:24:27 -0500947 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s\n",adj_lsa->header->orig_router->name);
948 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
949 writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Type: %d\n",adj_lsa->header->ls_type);
950 writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Time: %s\n",adj_lsa->header->orig_time);
akmhoque9e9fc722012-09-26 14:03:25 -0500951 writeLogg(__FILE__,__FUNCTION__,__LINE__," Lsa Data:\n");
akmhoque34b99f92012-09-27 12:24:27 -0500952 writeLogg(__FILE__,__FUNCTION__,__LINE__," No of Link: %d\n",adj_lsa->no_link);
akmhoque9e9fc722012-09-26 14:03:25 -0500953
954 write_log_for_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
955
956 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
957
958}
959
960void
akmhoque53f64222012-09-05 13:57:51 -0500961print_adj_lsa_body(const char *body, int no_link)
962{
963 int i=0;
964 char *lsa_data=(char *)malloc(strlen(body)+1);
965 memset( lsa_data,0,strlen(body)+1);
966 memcpy(lsa_data,body,strlen(body)+1);
967 char *sep="|";
968 char *rem;
969 char *rtr_id;
970 char *length;
971 char *face;
972 char *metric;
973
akmhoque53f64222012-09-05 13:57:51 -0500974 if(no_link >0 )
975 {
976 rtr_id=strtok_r(lsa_data,sep,&rem);
977 length=strtok_r(NULL,sep,&rem);
978 face=strtok_r(NULL,sep,&rem);
979 metric=strtok_r(NULL,sep,&rem);
980
981 printf(" Link %d \n",i+1);
982 printf(" Neighbor : %s \n",rtr_id);
983 printf(" Neighbor Length : %s \n",length);
984 printf(" Connecting Face : %s \n",face);
985 printf(" Metric : %s \n",metric);
akmhoque03004e62012-09-06 01:12:28 -0500986
akmhoque53f64222012-09-05 13:57:51 -0500987
988 for(i=1;i<no_link;i++)
989 {
990 rtr_id=strtok_r(NULL,sep,&rem);
991 length=strtok_r(NULL,sep,&rem);
992 face=strtok_r(NULL,sep,&rem);
993 metric=strtok_r(NULL,sep,&rem);
994 printf(" Link %d \n",i+1);
995 printf(" Neighbor : %s \n",rtr_id);
996 printf(" Neighbor Length : %s \n",length);
997 printf(" Connecting Face : %s \n",face);
998 printf(" Metric : %s \n",metric);
999
1000 }
1001 }
1002
1003 free(lsa_data);
1004}
1005
1006void
1007print_adj_lsa(struct alsa * adj_lsa)
1008{
akmhoque7b791452012-10-30 11:24:56 -05001009 if ( nlsr->debugging )
1010 {
1011 printf("-----------ADJ LSA Content---------------\n");
1012 printf(" Origination Router : %s\n",adj_lsa->header->orig_router->name);
1013 printf(" Origination Router Length: %d\n",adj_lsa->header->orig_router->length);
1014 printf(" LS Type : %d\n",adj_lsa->header->ls_type);
1015 printf(" Origination Time : %s\n",adj_lsa->header->orig_time);
1016 printf(" Lsa Data:\n");
1017 printf(" No of Link : %d\n",adj_lsa->no_link);
akmhoque53f64222012-09-05 13:57:51 -05001018
akmhoque7b791452012-10-30 11:24:56 -05001019 print_adj_lsa_body(adj_lsa->body,adj_lsa->no_link);
1020 printf("\n");
1021 }
akmhoque53f64222012-09-05 13:57:51 -05001022
1023}
1024
1025void
1026print_adj_lsdb(void)
1027{
akmhoque7b791452012-10-30 11:24:56 -05001028 if ( nlsr->debugging )
1029 printf("print_name_lsdb called \n");
akmhoque53f64222012-09-05 13:57:51 -05001030 int i, adj_lsdb_element;
1031 struct alsa *adj_lsa;
1032
1033 struct hashtb_enumerator ee;
1034 struct hashtb_enumerator *e = &ee;
1035
1036 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1037 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1038
1039 for(i=0;i<adj_lsdb_element;i++)
1040 {
akmhoque7b791452012-10-30 11:24:56 -05001041 if ( nlsr->debugging )
1042 printf("-----------Adj LSA (%d)---------------\n",i+1);
akmhoque53f64222012-09-05 13:57:51 -05001043 adj_lsa=e->data;
1044 print_adj_lsa(adj_lsa);
1045 hashtb_next(e);
1046 }
1047
1048 hashtb_end(e);
1049
akmhoque3171d652012-11-13 11:44:33 -06001050 if ( nlsr->debugging )
1051 printf("\n");
1052 if ( nlsr->detailed_logging )
1053 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoque53f64222012-09-05 13:57:51 -05001054}
1055
1056void
akmhoque03004e62012-09-06 01:12:28 -05001057build_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 -05001058{
akmhoque7b791452012-10-30 11:24:56 -05001059 if ( nlsr->debugging )
1060 printf("build_and_install_others_adj_lsa called \n");
1061 if ( nlsr->detailed_logging )
1062 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_others_adj_lsa called \n");
akmhoque03004e62012-09-06 01:12:28 -05001063 struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa ));
1064 build_others_adj_lsa(adj_lsa,orig_router,ls_type,orig_time,no_link,data);
1065 //print_adj_lsa(adj_lsa);
1066 install_adj_lsa(adj_lsa);
1067
akmhoque53f64222012-09-05 13:57:51 -05001068
akmhoque03004e62012-09-06 01:12:28 -05001069 free(adj_lsa->header->orig_router->name);
1070 free(adj_lsa->header->orig_router);
1071 free(adj_lsa->header->orig_time);
1072 free(adj_lsa->header);
1073 free(adj_lsa->body);
1074 free(adj_lsa);
akmhoque53f64222012-09-05 13:57:51 -05001075
akmhoque03004e62012-09-06 01:12:28 -05001076 print_adj_lsdb();
akmhoque53f64222012-09-05 13:57:51 -05001077
akmhoque53f64222012-09-05 13:57:51 -05001078}
1079
akmhoque03004e62012-09-06 01:12:28 -05001080
akmhoque53f64222012-09-05 13:57:51 -05001081void
akmhoque03004e62012-09-06 01:12:28 -05001082build_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 -05001083{
akmhoque7b791452012-10-30 11:24:56 -05001084 //printf("build_others_adj_lsa called \n");
1085 if ( nlsr->debugging )
1086 printf("build_others_adj_lsa called \n");
1087 if ( nlsr->detailed_logging )
1088 writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_others_adj_lsa called \n");
akmhoque53f64222012-09-05 13:57:51 -05001089
akmhoque03004e62012-09-06 01:12:28 -05001090 /*Filling Up Header Data */
1091 adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
1092 adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
1093 adj_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1);
1094 memset(adj_lsa->header->orig_router->name,0,strlen(orig_router)+1);
1095 memcpy(adj_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1);
akmhoque53f64222012-09-05 13:57:51 -05001096
akmhoque03004e62012-09-06 01:12:28 -05001097 adj_lsa->header->orig_router->length=strlen(orig_router)+1;
akmhoque53f64222012-09-05 13:57:51 -05001098
akmhoque53f64222012-09-05 13:57:51 -05001099
akmhoque03004e62012-09-06 01:12:28 -05001100 adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ;
akmhoque53f64222012-09-05 13:57:51 -05001101
akmhoque03004e62012-09-06 01:12:28 -05001102 adj_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1);
1103 memset(adj_lsa->header->orig_time,0,strlen(orig_time)+1);
1104 memcpy(adj_lsa->header->orig_time,orig_time,strlen(orig_time)+1);
akmhoque53f64222012-09-05 13:57:51 -05001105
akmhoque03004e62012-09-06 01:12:28 -05001106 adj_lsa->no_link=no_link;
akmhoque53f64222012-09-05 13:57:51 -05001107
akmhoque03004e62012-09-06 01:12:28 -05001108 adj_lsa->body=(char *)malloc(strlen(data)+1);
1109 memset(adj_lsa->body,0,strlen(data)+1);
1110 memcpy(adj_lsa->body,(char *)data,strlen(data)+1);
akmhoque53f64222012-09-05 13:57:51 -05001111
akmhoque53f64222012-09-05 13:57:51 -05001112}
1113
akmhoque03004e62012-09-06 01:12:28 -05001114
akmhoque53f64222012-09-05 13:57:51 -05001115long int
1116get_name_lsdb_num_element(void)
1117{
1118 long int num_element;
1119
1120
1121 struct hashtb_enumerator ee;
1122 struct hashtb_enumerator *e = &ee;
1123
1124 hashtb_start(nlsr->lsdb->name_lsdb, e);
1125 num_element=hashtb_n(nlsr->lsdb->name_lsdb);
1126 hashtb_end(e);
1127
1128 return num_element;
1129}
1130
1131long int
1132get_adj_lsdb_num_element(void)
1133{
1134 long int num_element;
1135
1136
1137 struct hashtb_enumerator ee;
1138 struct hashtb_enumerator *e = &ee;
1139
1140 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1141 num_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1142 hashtb_end(e);
1143
1144 return num_element;
akmhoqued79438d2012-08-27 13:31:42 -05001145}
akmhoque03004e62012-09-06 01:12:28 -05001146
1147void
1148get_name_lsdb_summary(struct ccn_charbuf *name_lsdb_data)
1149{
akmhoque7b791452012-10-30 11:24:56 -05001150 //printf("get_name_lsdb_summary called \n");
1151 if ( nlsr->debugging )
1152 printf("get_name_lsdb_summary called \n");
1153 if ( nlsr->detailed_logging )
1154 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_name_lsdb_summary called \n");
1155
akmhoque03004e62012-09-06 01:12:28 -05001156 int i, name_lsdb_element;
1157
1158 struct nlsa *name_lsa;
1159 struct hashtb_enumerator ee;
1160 struct hashtb_enumerator *e = &ee;
1161
1162 hashtb_start(nlsr->lsdb->name_lsdb, e);
1163 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
1164
1165 for(i=0;i<name_lsdb_element;i++)
1166 {
1167 name_lsa=e->data;
1168
1169 ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_router->name);
1170 ccn_charbuf_append_string(name_lsdb_data,"|");
1171
1172 char *lst=(char *)malloc(20);
1173 memset(lst,0,20);
1174 sprintf(lst,"%d",name_lsa->header->ls_type);
1175 ccn_charbuf_append_string(name_lsdb_data,lst);
1176 free(lst);
1177 ccn_charbuf_append_string(name_lsdb_data,"|");
1178
1179 char *lsid=(char *)malloc(20);
1180 memset(lsid,0,20);
1181 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1182 ccn_charbuf_append_string(name_lsdb_data,lsid);
1183 free(lsid);
1184 ccn_charbuf_append_string(name_lsdb_data,"|");
1185
1186 ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_time);
1187 ccn_charbuf_append_string(name_lsdb_data,"|");
1188
1189 hashtb_next(e);
1190 }
1191
1192 hashtb_end(e);
1193
1194}
1195
1196
1197void
1198get_adj_lsdb_summary(struct ccn_charbuf *adj_lsdb_data)
1199{
akmhoque7b791452012-10-30 11:24:56 -05001200 if ( nlsr->debugging )
1201 printf("get_adj_lsdb_summary called \n");
1202 if ( nlsr->detailed_logging )
1203 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_adj_lsdb_summary called \n");
akmhoque03004e62012-09-06 01:12:28 -05001204 int i, adj_lsdb_element;
1205 struct alsa *adj_lsa;
1206
1207 struct hashtb_enumerator ee;
1208 struct hashtb_enumerator *e = &ee;
1209
1210 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1211 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1212
1213 for(i=0;i<adj_lsdb_element;i++)
1214 {
1215 adj_lsa=e->data;
1216
1217 ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_router->name);
1218 ccn_charbuf_append_string(adj_lsdb_data,"|");
1219
1220 char *lst=(char *)malloc(20);
1221 memset(lst,0,20);
1222 sprintf(lst,"%d",adj_lsa->header->ls_type);
1223 ccn_charbuf_append_string(adj_lsdb_data,lst);
1224 free(lst);
1225 ccn_charbuf_append_string(adj_lsdb_data,"|");
1226
1227 ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_time);
1228 ccn_charbuf_append_string(adj_lsdb_data,"|");
1229
1230 hashtb_next(e);
1231 }
1232
1233 hashtb_end(e);
1234}
1235
1236
1237void
1238get_lsdb_summary(struct ccn_charbuf *lsdb_data)
1239{
1240 struct ccn_charbuf *name_lsdb_data=ccn_charbuf_create();
1241 struct ccn_charbuf *adj_lsdb_data=ccn_charbuf_create();
1242
1243 get_name_lsdb_summary(name_lsdb_data);
1244 get_adj_lsdb_summary(adj_lsdb_data);
1245
1246 long int num_lsa=get_name_lsdb_num_element() + get_adj_lsdb_num_element();
1247 char *num_element=(char *)malloc(15);
1248 memset(num_element,0,15);
1249 sprintf(num_element,"%ld",num_lsa);
1250
1251 if( num_lsa > 0)
1252 {
1253 ccn_charbuf_append_string(lsdb_data,num_element);
1254 ccn_charbuf_append_string(lsdb_data,"|");
1255 }
1256 if(name_lsdb_data->length>0)
1257 {
1258 char *data1=ccn_charbuf_as_string(name_lsdb_data);
1259 ccn_charbuf_append_string(lsdb_data,(char *)data1);
1260 }
1261 if(adj_lsdb_data->length>0)
1262 {
1263 char *data2=ccn_charbuf_as_string(adj_lsdb_data);
1264 ccn_charbuf_append_string(lsdb_data,(char *)data2);
1265 }
1266 ccn_charbuf_destroy(&name_lsdb_data);
1267 ccn_charbuf_destroy(&adj_lsdb_data);
1268 free(num_element);
1269
1270}
1271
1272int
1273check_is_new_name_lsa(char *orig_router,char *lst,char *lsid,char *orig_time)
1274{
1275 int ret=0;
1276 struct ccn_charbuf *key=ccn_charbuf_create();
1277 ccn_charbuf_append_string(key,orig_router);
1278 ccn_charbuf_append_string(key,"/");
1279 ccn_charbuf_append_string(key,lst);
1280 ccn_charbuf_append_string(key,"/");
1281 ccn_charbuf_append_string(key,lsid);
1282
1283 int res;
1284 struct nlsa *name_lsa;
1285
1286 struct hashtb_enumerator ee;
1287 struct hashtb_enumerator *e = &ee;
1288
1289 hashtb_start(nlsr->lsdb->name_lsdb, e);
1290 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1291
1292 if( res == HT_NEW_ENTRY )
1293 {
1294 hashtb_delete(e);
1295 ret=1;
1296
1297 }
1298 else if(res == HT_OLD_ENTRY)
1299 {
1300 name_lsa=e->data;
1301 if( strcmp ( orig_time , name_lsa->header->orig_time ) > 0 )
1302 {
1303 ret=1;
1304 }
1305 }
1306
1307 hashtb_end(e);
1308
1309 ccn_charbuf_destroy(&key);
1310
1311 return ret;
1312}
1313
1314int
1315check_is_new_adj_lsa(char *orig_router,char *lst,char *orig_time)
1316{
1317 int ret=0;
1318 struct ccn_charbuf *key=ccn_charbuf_create();
1319 ccn_charbuf_append_string(key,orig_router);
1320 ccn_charbuf_append_string(key,"/");
1321 ccn_charbuf_append_string(key,lst);
1322
1323 int res;
1324 struct alsa *adj_lsa;
1325
1326 struct hashtb_enumerator ee;
1327 struct hashtb_enumerator *e = &ee;
1328
1329 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1330 res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0);
1331
1332 if( res == HT_NEW_ENTRY )
1333 {
1334 hashtb_delete(e);
1335 ret=1;
1336
1337 }
1338 else if(res == HT_OLD_ENTRY)
1339 {
1340 adj_lsa=e->data;
1341 if( strcmp ( orig_time , adj_lsa->header->orig_time ) > 0 )
1342 {
1343 ret=1;
1344 }
1345 }
1346
1347 hashtb_end(e);
1348
1349 ccn_charbuf_destroy(&key);
1350
1351 return ret;
1352}
1353
1354void
1355get_name_lsa_data(struct ccn_charbuf *lsa_data, struct name_prefix *lsaId)
1356{
akmhoque7b791452012-10-30 11:24:56 -05001357 //printf("get_name_lsa_data called \n");
1358 if ( nlsr->debugging )
1359 printf("get_name_lsa_data called \n");
1360 if ( nlsr->detailed_logging )
1361 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_name_lsa_data called \n");
akmhoque03004e62012-09-06 01:12:28 -05001362
1363 struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa ));
1364
1365 struct hashtb_enumerator ee;
1366 struct hashtb_enumerator *e = &ee;
1367 int res;
1368
1369 hashtb_start(nlsr->lsdb->name_lsdb, e);
1370 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1371
1372 if( res == HT_OLD_ENTRY )
1373 {
1374 name_lsa=e->data;
akmhoque7b791452012-10-30 11:24:56 -05001375 //printf("NAME LSA found\n");
1376
1377 if ( nlsr->debugging )
1378 printf("NAME LSA found \n");
1379 if ( nlsr->detailed_logging )
1380 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Name LSA found \n");
akmhoque03004e62012-09-06 01:12:28 -05001381
1382 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_router->name);
1383 ccn_charbuf_append_string(lsa_data,"|");
1384
1385 char *temp_length=(char *)malloc(20);
1386 memset(temp_length,0,20);
1387 sprintf(temp_length,"%d",name_lsa->header->orig_router->length);
1388 ccn_charbuf_append_string(lsa_data,temp_length);
1389 free(temp_length);
1390 ccn_charbuf_append_string(lsa_data,"|");
1391
1392 char *temp_ltype=(char *)malloc(20);
1393 memset(temp_ltype,0,20);
1394 sprintf(temp_ltype,"%d",name_lsa->header->ls_type);
1395 ccn_charbuf_append_string(lsa_data,temp_ltype);
1396 free(temp_ltype);
1397 ccn_charbuf_append_string(lsa_data,"|");
1398
1399 char *temp_lsid=(char *)malloc(20);
1400 memset(temp_lsid,0,20);
1401 sprintf(temp_lsid,"%ld",name_lsa->header->ls_id);
1402 ccn_charbuf_append_string(lsa_data,temp_lsid);
1403 free(temp_lsid);
1404 ccn_charbuf_append_string(lsa_data,"|");
1405
1406 ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_time);
1407 ccn_charbuf_append_string(lsa_data,"|");
1408
1409 char *temp_valid=(char *)malloc(20);
1410 memset(temp_valid,0,20);
1411 sprintf(temp_valid,"%d",name_lsa->header->isValid);
1412 ccn_charbuf_append_string(lsa_data,temp_valid);
1413 free(temp_valid);
1414 ccn_charbuf_append_string(lsa_data,"|");
1415
1416 ccn_charbuf_append_string(lsa_data,name_lsa->name_prefix->name);
1417 ccn_charbuf_append_string(lsa_data,"|");
1418
1419 char *temp_npl=(char *)malloc(20);
1420 memset(temp_npl,0,20);
1421 sprintf(temp_npl,"%d",name_lsa->name_prefix->length);
1422 ccn_charbuf_append_string(lsa_data,temp_npl);
1423 free(temp_npl);
1424 ccn_charbuf_append_string(lsa_data,"|");
1425
1426 }
1427 else if(res == HT_NEW_ENTRY)
1428 {
1429 hashtb_delete(e);
1430 }
1431
1432 hashtb_end(e);
1433}
1434
1435void
1436get_adj_lsa_data(struct ccn_charbuf *lsa_data,struct name_prefix *lsaId)
1437{
akmhoque7b791452012-10-30 11:24:56 -05001438 if ( nlsr->debugging )
1439 printf("get_adj_lsa_data called \n");
1440 if ( nlsr->detailed_logging )
1441 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_adj_lsa_data called \n");
akmhoque03004e62012-09-06 01:12:28 -05001442
1443 struct alsa *adj_lsa=(struct alsa*)malloc(sizeof(struct alsa ));
1444
1445 struct hashtb_enumerator ee;
1446 struct hashtb_enumerator *e = &ee;
1447 int res;
1448
1449 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1450 res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0);
1451
1452 if( res == HT_OLD_ENTRY )
1453 {
1454 adj_lsa=e->data;
akmhoque7b791452012-10-30 11:24:56 -05001455 //printf("Adj LSA found\n");
1456
1457 if ( nlsr->debugging )
1458 printf("Adj LSA found \n");
1459 if ( nlsr->detailed_logging )
1460 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adj LSA found \n");
akmhoque03004e62012-09-06 01:12:28 -05001461
1462 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_router->name);
1463 ccn_charbuf_append_string(lsa_data,"|");
1464
1465 char *temp_length=(char *)malloc(20);
1466 memset(temp_length,0,20);
1467 sprintf(temp_length,"%d",adj_lsa->header->orig_router->length);
1468 ccn_charbuf_append_string(lsa_data,temp_length);
1469 free(temp_length);
1470 ccn_charbuf_append_string(lsa_data,"|");
1471
1472 char *temp_ltype=(char *)malloc(20);
1473 memset(temp_ltype,0,20);
1474 sprintf(temp_ltype,"%d",adj_lsa->header->ls_type);
1475 ccn_charbuf_append_string(lsa_data,temp_ltype);
1476 free(temp_ltype);
1477 ccn_charbuf_append_string(lsa_data,"|");
1478
1479 ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_time);
1480 ccn_charbuf_append_string(lsa_data,"|");
1481
1482 char *temp_nl=(char *)malloc(20);
1483 memset(temp_nl,0,20);
1484 sprintf(temp_nl,"%d",adj_lsa->no_link);
1485 ccn_charbuf_append_string(lsa_data,temp_nl);
1486 free(temp_nl);
1487 ccn_charbuf_append_string(lsa_data,"|");
1488
1489 ccn_charbuf_append_string(lsa_data,adj_lsa->body);
1490
1491
1492 }
1493 else if(res == HT_NEW_ENTRY)
1494 {
1495 hashtb_delete(e);
1496 }
1497
1498 hashtb_end(e);
1499}
akmhoqueffacaa82012-09-13 17:48:30 -05001500
akmhoque3171d652012-11-13 11:44:33 -06001501void
1502make_name_lsa_invalid(struct name_prefix *np,int ls_type, long int ls_id)
1503{
1504
1505 if ( nlsr->debugging )
1506 printf("make_name_lsa_invalid called \n");
1507 if ( nlsr->detailed_logging )
1508 writeLogg(__FILE__,__FUNCTION__,__LINE__,"make_name_lsa_invalid called \n");
1509
1510
1511 char lst[2];
1512 memset(lst,0,2);
1513 sprintf(lst,"%d",ls_type);
1514
1515 char lsid[10];
1516 memset(lsid,0,10);
1517 sprintf(lsid,"%ld",ls_id);
1518
1519
1520 char *key=(char *)malloc(strlen(np->name)+1+strlen(lst)+1+strlen(lsid)+1);
1521 memset(key,0,strlen(np->name)+1+strlen(lst)+1+strlen(lsid)+1);
1522
1523
1524 make_name_lsa_key(key, np->name,ls_type,ls_id);
1525 printf("Key:%s Length:%d\n",key,(int)strlen(key));
1526
1527 struct nlsa *nlsa;
1528
1529 struct hashtb_enumerator ee;
1530 struct hashtb_enumerator *e = &ee;
1531
1532 int res;
1533
1534 hashtb_start(nlsr->lsdb->name_lsdb, e);
1535 res = hashtb_seek(e, key,strlen(key) , 0);
1536
1537 if( res == HT_OLD_ENTRY )
1538 {
1539 nlsa=e->data;
1540
1541 nlsa->header->isValid=0;
1542
1543 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1544 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1545 write_log_for_name_lsa(nlsa);
1546 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1547
1548 hashtb_delete(e);
1549 }
1550 else if( res == HT_NEW_ENTRY )
1551 {
1552 hashtb_delete(e);
1553 }
1554 hashtb_end(e);
1555
1556 if ( nlsr->debugging )
1557 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1558 if ( nlsr->detailed_logging )
1559 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1560
1561 set_new_lsdb_version();
1562
1563 if ( nlsr->debugging )
1564 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1565 if ( nlsr->detailed_logging )
1566 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1567
1568}
1569
akmhoqueda5b6832012-09-13 22:33:55 -05001570int
1571delete_name_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1572{
akmhoque7b791452012-10-30 11:24:56 -05001573 //printf("delete_name_lsa called \n");
1574
1575 if ( nlsr->debugging )
1576 printf("delete_name_lsa called \n");
1577 if ( nlsr->detailed_logging )
1578 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_name_lsa called \n");
akmhoqueda5b6832012-09-13 22:33:55 -05001579
1580 if(flags == CCN_SCHEDULE_CANCEL)
1581 {
1582 return -1;
1583 }
1584
1585
1586
1587 nlsr_lock();
1588
akmhoque7b791452012-10-30 11:24:56 -05001589 if ( nlsr->debugging )
1590 printf("LSA Key: %s \n",(char *)ev->evdata);
1591 if ( nlsr->detailed_logging )
1592 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Key: %s \n",(char *)ev->evdata);
akmhoqueda5b6832012-09-13 22:33:55 -05001593
1594 struct nlsa *nlsa;
1595
1596 struct hashtb_enumerator ee;
1597 struct hashtb_enumerator *e = &ee;
1598
1599 int res;
1600
1601 hashtb_start(nlsr->lsdb->name_lsdb, e);
1602 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1603
1604 if( res == HT_OLD_ENTRY )
1605 {
1606 nlsa=e->data;
akmhoque3cced642012-09-24 16:20:20 -05001607 delete_npt_entry_by_router_and_name_prefix(nlsa->header->orig_router->name, nlsa->name_prefix->name);
akmhoque9e9fc722012-09-26 14:03:25 -05001608
1609 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1610 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1611 write_log_for_name_lsa(nlsa);
1612 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1613
akmhoqueda5b6832012-09-13 22:33:55 -05001614 hashtb_delete(e);
1615 }
akmhoque3cced642012-09-24 16:20:20 -05001616 else if( res == HT_NEW_ENTRY )
akmhoqueda5b6832012-09-13 22:33:55 -05001617 {
akmhoqueda5b6832012-09-13 22:33:55 -05001618 hashtb_delete(e);
1619 }
1620 hashtb_end(e);
1621
akmhoque7b791452012-10-30 11:24:56 -05001622 if ( nlsr->debugging )
1623 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1624 if ( nlsr->detailed_logging )
1625 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1626
akmhoqueda5b6832012-09-13 22:33:55 -05001627 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -05001628
1629 if ( nlsr->debugging )
1630 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1631 if ( nlsr->detailed_logging )
1632 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueda5b6832012-09-13 22:33:55 -05001633
1634 //print_name_lsdb();
1635
1636 nlsr_unlock();
1637
1638 return 0;
1639}
1640
1641int
1642delete_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1643{
akmhoque7b791452012-10-30 11:24:56 -05001644 //printf("delete_adj_lsa called \n");
1645
1646 if ( nlsr->debugging )
1647 printf("delete_adj_lsa called \n");
1648 if ( nlsr->detailed_logging )
1649 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_adj_lsa called \n");
akmhoqueda5b6832012-09-13 22:33:55 -05001650
1651 if(flags == CCN_SCHEDULE_CANCEL)
1652 {
1653 return -1;
1654 }
1655 nlsr_lock();
1656
akmhoque7b791452012-10-30 11:24:56 -05001657 //printf("LSA Key: %s \n",(char *)ev->evdata);
1658
1659 if ( nlsr->debugging )
1660 printf("LSA Key: %s \n",(char *)ev->evdata);
1661 if ( nlsr->detailed_logging )
1662 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Key: %s \n",(char *)ev->evdata);
akmhoqueda5b6832012-09-13 22:33:55 -05001663
akmhoque9e9fc722012-09-26 14:03:25 -05001664 struct alsa *alsa;
akmhoqueda5b6832012-09-13 22:33:55 -05001665 struct hashtb_enumerator ee;
1666 struct hashtb_enumerator *e = &ee;
1667 int res;
1668
1669 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1670 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1671
1672 if( res == HT_OLD_ENTRY )
1673 {
akmhoque9e9fc722012-09-26 14:03:25 -05001674 alsa=e->data;
1675 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1676 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
1677 write_log_for_adj_lsa(alsa);
1678 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1679
akmhoqueda5b6832012-09-13 22:33:55 -05001680 hashtb_delete(e);
1681 }
1682 else if( res == HT_OLD_ENTRY )
1683 {
1684 hashtb_delete(e);
1685 }
1686 hashtb_end(e);
1687
akmhoque7b791452012-10-30 11:24:56 -05001688 if ( nlsr->debugging )
1689 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1690 if ( nlsr->detailed_logging )
1691 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1692
akmhoqueda5b6832012-09-13 22:33:55 -05001693 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -05001694
1695 if ( nlsr->debugging )
1696 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1697 if ( nlsr->detailed_logging )
1698 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1699
akmhoqueda5b6832012-09-13 22:33:55 -05001700
1701 if ( !nlsr->is_route_calculation_scheduled)
1702 {
1703 nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0);
1704 nlsr->is_route_calculation_scheduled=1;
1705 }
1706
1707 //print_adj_lsdb();
1708
1709 nlsr_unlock();
1710
1711 return 0;
1712}
1713
akmhoqueffacaa82012-09-13 17:48:30 -05001714void
1715refresh_name_lsdb(void)
1716{
akmhoque7b791452012-10-30 11:24:56 -05001717 //printf("refresh_name_lsdb called \n");
1718
1719 if ( nlsr->debugging )
1720 printf("refresh_name_lsdb called \n");
1721 if ( nlsr->detailed_logging )
1722 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_name_lsdb called \n");
akmhoqueffacaa82012-09-13 17:48:30 -05001723
akmhoqueda5b6832012-09-13 22:33:55 -05001724 //int lsa_change_count=0;
1725
akmhoqueffacaa82012-09-13 17:48:30 -05001726 char *time_stamp=(char *)malloc(20);
1727 memset(time_stamp,0,20);
1728 get_current_timestamp_micro(time_stamp);
1729
1730 long int lsa_life_time;
1731
1732 int i, name_lsdb_element;
1733 struct nlsa *name_lsa;
1734
1735 struct hashtb_enumerator ee;
1736 struct hashtb_enumerator *e = &ee;
1737
1738 hashtb_start(nlsr->lsdb->name_lsdb, e);
1739 name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
1740
1741 for(i=0;i<name_lsdb_element;i++)
1742 {
1743 name_lsa=e->data;
1744
1745 lsa_life_time=get_time_diff(time_stamp,name_lsa->header->orig_time);
akmhoque7b791452012-10-30 11:24:56 -05001746 if ( nlsr->debugging )
1747 printf("LSA Life Time: %ld \n",lsa_life_time);
1748 if ( nlsr->detailed_logging )
1749 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Life Time: %ld \n",lsa_life_time);
akmhoqueda5b6832012-09-13 22:33:55 -05001750
1751 if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name) == 0)
1752 {
1753 if ( lsa_life_time > nlsr->lsa_refresh_time )
1754 {
akmhoque14b3f342012-09-14 10:39:02 -05001755 if ( name_lsa->header->isValid == NAME_LSA_VALID )
1756 {
akmhoque7b791452012-10-30 11:24:56 -05001757 if ( nlsr->debugging )
1758 printf("Own Name LSA need to be refrshed\n");
1759 if ( nlsr->detailed_logging )
1760 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Own Name LSA need to be refrshed\n");
akmhoquea98c2142012-10-25 15:22:24 -05001761
1762 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1763 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
1764 write_log_for_name_lsa(name_lsa);
1765 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
1766
1767
akmhoque14b3f342012-09-14 10:39:02 -05001768 char *current_time_stamp=(char *)malloc(20);
1769 memset(current_time_stamp,0,20);
1770 get_current_timestamp_micro(current_time_stamp);
akmhoqueda5b6832012-09-13 22:33:55 -05001771
akmhoque14b3f342012-09-14 10:39:02 -05001772 free(name_lsa->header->orig_time);
1773 name_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1774 memset(name_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1775 memcpy(name_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
akmhoquea98c2142012-10-25 15:22:24 -05001776
1777 writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
1778 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding name lsa\n");
1779 write_log_for_name_lsa(name_lsa);
1780 writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
akmhoqueda5b6832012-09-13 22:33:55 -05001781
akmhoque14b3f342012-09-14 10:39:02 -05001782 free(current_time_stamp);
1783 }
1784 else
1785 {
1786 char lst[2];
1787 memset(lst,0,2);
1788 sprintf(lst,"%d",name_lsa->header->ls_type);
1789
1790 char lsid[10];
1791 memset(lsid,0,10);
1792 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1793
1794
1795 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1796 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1797
1798
1799 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
akmhoque7b791452012-10-30 11:24:56 -05001800 //printf("Key:%s Length:%d\n",key,(int)strlen(key));
akmhoque14b3f342012-09-14 10:39:02 -05001801
1802 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
1803 }
akmhoqueda5b6832012-09-13 22:33:55 -05001804
akmhoque7b791452012-10-30 11:24:56 -05001805 if ( nlsr->debugging )
1806 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1807 if ( nlsr->detailed_logging )
1808 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1809
akmhoqueda5b6832012-09-13 22:33:55 -05001810 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -05001811
1812 if ( nlsr->debugging )
1813 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1814 if ( nlsr->detailed_logging )
1815 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1816
1817
akmhoqueda5b6832012-09-13 22:33:55 -05001818
1819 print_name_lsdb();
1820 //lsa_change_count++;
1821 }
1822 }
1823 else
1824 {
1825 if ( lsa_life_time > nlsr->router_dead_interval )
1826 {
akmhoque7b791452012-10-30 11:24:56 -05001827 if ( nlsr->debugging )
1828 printf("Others Name LSA need to be deleted\n");
1829 if ( nlsr->detailed_logging )
1830 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Others Name LSA need to be deleted\n");
akmhoqueda5b6832012-09-13 22:33:55 -05001831
1832 char lst[2];
1833 memset(lst,0,2);
1834 sprintf(lst,"%d",name_lsa->header->ls_type);
1835
1836 char lsid[10];
1837 memset(lsid,0,10);
1838 sprintf(lsid,"%ld",name_lsa->header->ls_id);
1839
1840
1841 char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1842 memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
1843
1844
1845 make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
akmhoque7b791452012-10-30 11:24:56 -05001846 //printf("Key:%s Length:%d\n",key,(int)strlen(key));
akmhoqueda5b6832012-09-13 22:33:55 -05001847
1848 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0);
1849 //lsa_change_count++;
1850 }
1851 }
akmhoqueffacaa82012-09-13 17:48:30 -05001852
1853 hashtb_next(e);
1854 }
1855
1856 hashtb_end(e);
1857
1858 free(time_stamp);
akmhoqueda5b6832012-09-13 22:33:55 -05001859
akmhoqueffacaa82012-09-13 17:48:30 -05001860
1861}
1862
akmhoqueda5b6832012-09-13 22:33:55 -05001863void
akmhoqueffacaa82012-09-13 17:48:30 -05001864refresh_adj_lsdb(void)
1865{
akmhoque7b791452012-10-30 11:24:56 -05001866 //printf("refresh_adj_lsdb called \n");
akmhoqueffacaa82012-09-13 17:48:30 -05001867
akmhoque7b791452012-10-30 11:24:56 -05001868 if ( nlsr->debugging )
1869 printf("refresh_adj_lsdb called \n");
1870 if ( nlsr->detailed_logging )
1871 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_adj_lsdb called \n");
akmhoqueda5b6832012-09-13 22:33:55 -05001872
akmhoqueffacaa82012-09-13 17:48:30 -05001873 char *time_stamp=(char *)malloc(20);
1874 memset(time_stamp,0,20);
1875 get_current_timestamp_micro(time_stamp);
1876
1877 long int lsa_life_time;
1878
1879 int i, adj_lsdb_element;
1880 struct alsa *adj_lsa;
1881
1882 struct hashtb_enumerator ee;
1883 struct hashtb_enumerator *e = &ee;
1884
1885 hashtb_start(nlsr->lsdb->adj_lsdb, e);
1886 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
1887
1888 for(i=0;i<adj_lsdb_element;i++)
1889 {
1890 adj_lsa=e->data;
1891
1892 lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time);
akmhoque7b791452012-10-30 11:24:56 -05001893 //printf("LSA Life Time: %ld \n",lsa_life_time);
1894
1895 if ( nlsr->debugging )
1896 printf("LSA Life Time: %ld \n",lsa_life_time);
1897 if ( nlsr->detailed_logging )
1898 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Life Time: %ld \n",lsa_life_time);
akmhoqueffacaa82012-09-13 17:48:30 -05001899
akmhoqueda5b6832012-09-13 22:33:55 -05001900 if ( strcmp(adj_lsa->header->orig_router->name,nlsr->router_name) == 0)
1901 {
1902 if ( lsa_life_time > nlsr->lsa_refresh_time )
1903 {
akmhoque7b791452012-10-30 11:24:56 -05001904 //printf("Own Adj LSA need to be refrshed\n");
1905 if ( nlsr->debugging )
1906 printf("Own Adj LSA need to be refrshed\n");
1907 if ( nlsr->detailed_logging )
1908 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Own Adj LSA need to be refrshed\n");
akmhoqueda5b6832012-09-13 22:33:55 -05001909
akmhoquea98c2142012-10-25 15:22:24 -05001910 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1911 writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting adj lsa\n");
1912 write_log_for_adj_lsa(adj_lsa);
1913 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
1914
akmhoqueda5b6832012-09-13 22:33:55 -05001915 char *current_time_stamp=(char *)malloc(20);
1916 memset(current_time_stamp,0,20);
1917 get_current_timestamp_micro(current_time_stamp);
1918
1919 free(adj_lsa->header->orig_time);
1920 adj_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free
1921 memset(adj_lsa->header->orig_time,0,strlen(current_time_stamp)+1);
1922 memcpy(adj_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1);
1923
1924 free(current_time_stamp);
1925
akmhoquea98c2142012-10-25 15:22:24 -05001926 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adj-LSA\n");
1927 writeLogg(__FILE__,__FUNCTION__,__LINE__," Adding adj lsa\n");
1928 write_log_for_adj_lsa(adj_lsa);
1929 writeLogg(__FILE__,__FUNCTION__,__LINE__," adj_lsa_end\n");
akmhoqueda5b6832012-09-13 22:33:55 -05001930
akmhoque7b791452012-10-30 11:24:56 -05001931 if ( nlsr->debugging )
1932 printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1933 if ( nlsr->detailed_logging )
1934 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1935
akmhoqueda5b6832012-09-13 22:33:55 -05001936 set_new_lsdb_version();
akmhoque7b791452012-10-30 11:24:56 -05001937
1938 if ( nlsr->debugging )
1939 printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
1940 if ( nlsr->detailed_logging )
1941 writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
akmhoqueda5b6832012-09-13 22:33:55 -05001942
1943 print_adj_lsdb();
1944 }
1945 }
1946 else
1947 {
1948 if ( lsa_life_time > nlsr->router_dead_interval )
1949 {
akmhoque7b791452012-10-30 11:24:56 -05001950 //printf("Others Adj LSA need to be deleted\n");
1951
1952 if ( nlsr->debugging )
1953 printf("Others Adj LSA need to be deleted\n");
1954 if ( nlsr->detailed_logging )
1955 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Others Adj LSA need to be deleted\n");
akmhoqueda5b6832012-09-13 22:33:55 -05001956
1957 char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2);
1958 memset(key,0,adj_lsa->header->orig_router->length+2);
1959 make_adj_lsa_key(key,adj_lsa);
akmhoque7b791452012-10-30 11:24:56 -05001960 //printf("Adjacent LSA key: %s \n",key);
akmhoqueda5b6832012-09-13 22:33:55 -05001961 nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_adj_lsa, (void *)key, 0);
1962 }
1963 }
1964
1965
1966
akmhoqueffacaa82012-09-13 17:48:30 -05001967 hashtb_next(e);
1968 }
1969
1970 hashtb_end(e);
1971
1972 free(time_stamp);
akmhoqueffacaa82012-09-13 17:48:30 -05001973}
1974
1975int
1976refresh_lsdb(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1977{
1978 if(flags == CCN_SCHEDULE_CANCEL)
1979 {
1980 return -1;
1981 }
1982
1983 nlsr_lock();
akmhoqueda5b6832012-09-13 22:33:55 -05001984
akmhoque7b791452012-10-30 11:24:56 -05001985 //printf("refresh_lsdb called \n");
1986
1987 if ( nlsr->debugging )
1988 printf("refresh_lsdb called\n");
1989 if ( nlsr->detailed_logging )
1990 writeLogg(__FILE__,__FUNCTION__,__LINE__,"refresh_lsdb called\n");
akmhoqueffacaa82012-09-13 17:48:30 -05001991
1992 refresh_name_lsdb();
1993 refresh_adj_lsdb();
1994
akmhoqueffacaa82012-09-13 17:48:30 -05001995 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoqueda5b6832012-09-13 22:33:55 -05001996
akmhoqueffacaa82012-09-13 17:48:30 -05001997 nlsr_unlock();
1998 return 0;
1999}