blob: fc5320dca56a9e6938af6ea75f9a2e0419455bb5 [file] [log] [blame]
akmhoque59980a52012-08-09 12:36:09 -05001#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
akmhoque03004e62012-09-06 01:12:28 -050011#include <sys/types.h>
12#include <signal.h>
13
akmhoque59980a52012-08-09 12:36:09 -050014
15
16#include <ccn/ccn.h>
17#include <ccn/uri.h>
18#include <ccn/keystore.h>
19#include <ccn/signing.h>
20#include <ccn/schedule.h>
21#include <ccn/hashtb.h>
22
23#include "nlsr.h"
24#include "nlsr_ndn.h"
akmhoque902d57e2012-08-17 09:24:38 -050025#include "nlsr_lsdb.h"
akmhoque53f64222012-09-05 13:57:51 -050026#include "utility.h"
akmhoque03004e62012-09-06 01:12:28 -050027#include "nlsr_npl.h"
28#include "nlsr_adl.h"
akmhoque3560cb62012-09-09 10:52:30 -050029#include "nlsr_npt.h"
30#include "nlsr_route.h"
31
32
akmhoque81c25e02012-09-10 14:50:33 -050033#define ON_ERROR_DESTROY(resval) \
34{ \
35 if ((resval) < 0) { \
36 nlsr_destroy(); \
akmhoqueffacaa82012-09-13 17:48:30 -050037 exit(1);\
akmhoque81c25e02012-09-10 14:50:33 -050038 } \
39}
40
41
42#define ON_ERROR_EXIT(resval) \
43{ \
44 if ((resval) < 0) { \
akmhoqueffacaa82012-09-13 17:48:30 -050045 exit(1); \
akmhoque81c25e02012-09-10 14:50:33 -050046 } \
47}
akmhoque59980a52012-08-09 12:36:09 -050048
49struct option longopts[] =
50{
51 { "daemon", no_argument, NULL, 'd'},
52 { "config_file", required_argument, NULL, 'f'},
53 { "help", no_argument, NULL, 'h'},
54 { 0 }
55};
56
57static int
58usage(char *progname)
59{
60
61 printf("Usage: %s [OPTIONS...]\n\
62 NDN routing....\n\
63 -d, --daemon Run in daemon mode\n\
64 -f, --config_file Specify configuration file name\n\
65 -h, --help Display this help message\n", progname);
66
67 exit(1);
68}
69
70void ndn_rtr_gettime(const struct ccn_gettime *self, struct ccn_timeval *result)
71{
72 struct timeval now = {0};
73 gettimeofday(&now, 0);
74 result->s = now.tv_sec;
75 result->micros = now.tv_usec;
76}
77
78static struct ccn_gettime ndn_rtr_ticker = {
79 "timer",
80 &ndn_rtr_gettime,
81 1000000,
82 NULL
83};
84
akmhoqueffacaa82012-09-13 17:48:30 -050085void
86nlsr_lock(void)
87{
88 nlsr->semaphor=NLSR_LOCKED;
89}
90
91void
92nlsr_unlock(void)
93{
94 nlsr->semaphor=NLSR_UNLOCKED;
95}
akmhoque42098b12012-08-27 22:54:23 -050096
97void
akmhoque03004e62012-09-06 01:12:28 -050098nlsr_stop_signal_handler(int sig)
akmhoque42098b12012-08-27 22:54:23 -050099{
akmhoque03004e62012-09-06 01:12:28 -0500100 signal(sig, SIG_IGN);
akmhoqueffacaa82012-09-13 17:48:30 -0500101 nlsr_destroy();
102 exit(0);
akmhoque59980a52012-08-09 12:36:09 -0500103}
104
105void
106process_command_ccnneighbor(char *command)
107{
108 if(command==NULL)
109 {
akmhoque28c45022012-08-09 15:38:02 -0500110 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500111 return;
112 }
113 char *rem;
114 const char *sep=" \t\n";
akmhoque28c45022012-08-09 15:38:02 -0500115 char *rtr_name,*face;
akmhoque59980a52012-08-09 12:36:09 -0500116
akmhoque28c45022012-08-09 15:38:02 -0500117 rtr_name=strtok_r(command,sep,&rem);
118 if(rtr_name==NULL)
akmhoque59980a52012-08-09 12:36:09 -0500119 {
akmhoque28c45022012-08-09 15:38:02 -0500120 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500121 return;
122 }
123
124 face=strtok_r(NULL,sep,&rem);
125 if(face==NULL)
126 {
akmhoque28c45022012-08-09 15:38:02 -0500127 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500128 return;
129 }
130
akmhoque28c45022012-08-09 15:38:02 -0500131 printf("Router: %s face: %s\n",rtr_name,face);
132 int face_id;
133 int res;
134 res=sscanf(face,"face%d",&face_id);
135
136 if(res != 1 )
137 {
138 printf(" Wrong Command Format ( ccnneighbor router_name faceX) where X is integer\n");
139 return;
140 }
141
akmhoqued5152122012-09-19 06:44:23 -0500142 if ( rtr_name[strlen(rtr_name)-1] == '/' )
143 {
144 rtr_name[strlen(rtr_name)-1]='\0';
145 }
akmhoque03004e62012-09-06 01:12:28 -0500146 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
147 nbr->name=(char *)malloc(strlen(rtr_name)+1);
148 memset(nbr->name,0,strlen(rtr_name)+1);
149 memcpy(nbr->name,rtr_name,strlen(rtr_name)+1);
150 nbr->length=strlen(rtr_name)+1;
151
152 add_nbr_to_adl(nbr,face_id);
153
154 free(nbr->name);
155 free(nbr);
156}
157
158void
159process_command_ccnname(char *command)
160{
161
162 if(command==NULL)
163 {
164 printf(" Wrong Command Format ( ccnname /name/prefix)\n");
165 return;
166 }
167 char *rem;
168 const char *sep=" \t\n";
169 char *name;
170 name=strtok_r(command,sep,&rem);
171 if(name==NULL)
172 {
173 printf(" Wrong Command Format ( ccnname /name/prefix/ )\n");
174 return;
175 }
176
177 printf("Name Prefix: %s \n",name);
178
akmhoqued5152122012-09-19 06:44:23 -0500179 if ( name[strlen(name)-1] == '/' )
180 name[strlen(name)-1]='\0';
181
akmhoque53f64222012-09-05 13:57:51 -0500182 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoque03004e62012-09-06 01:12:28 -0500183 np->name=(char *)malloc(strlen(name)+1);
184 memset(np->name,0,strlen(name)+1);
185 memcpy(np->name,name,strlen(name)+1);
186 np->length=strlen(name)+1;
akmhoque386081b2012-08-10 10:53:21 -0500187
akmhoque03004e62012-09-06 01:12:28 -0500188 add_name_to_npl(np);
akmhoque28c45022012-08-09 15:38:02 -0500189
akmhoque03004e62012-09-06 01:12:28 -0500190 free(np->name);
akmhoque53f64222012-09-05 13:57:51 -0500191 free(np);
akmhoque59980a52012-08-09 12:36:09 -0500192}
193
akmhoque03004e62012-09-06 01:12:28 -0500194
195void
196process_command_router_name(char *command)
197{
198 if(command==NULL)
199 {
200 printf(" Wrong Command Format ( router-name /router/name )\n");
201 return;
202 }
203 char *rem;
204 const char *sep=" \t\n";
205 char *rtr_name;
206
207 rtr_name=strtok_r(command,sep,&rem);
208 if(rtr_name==NULL)
209 {
210 printf(" Wrong Command Format ( router-name /router/name )\n");
211 return;
212 }
213
214
akmhoqued5152122012-09-19 06:44:23 -0500215 if ( rtr_name[strlen(rtr_name)-1] == '/' )
216 rtr_name[strlen(rtr_name)-1]='\0';
217
akmhoque03004e62012-09-06 01:12:28 -0500218 nlsr->router_name=(char *)malloc(strlen(rtr_name)+1);
219 memset(nlsr->router_name,0,strlen(rtr_name)+1);
220 memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1);
221
222
223}
224
akmhoque59980a52012-08-09 12:36:09 -0500225void
akmhoqued79438d2012-08-27 13:31:42 -0500226process_command_lsdb_synch_interval(char *command)
227{
228 if(command==NULL)
229 {
230 printf(" Wrong Command Format ( lsdb-synch-interval secs )\n");
231 return;
232 }
233 char *rem;
234 const char *sep=" \t\n";
235 char *secs;
236 long int seconds;
237
238 secs=strtok_r(command,sep,&rem);
239 if(secs==NULL)
240 {
241 printf(" Wrong Command Format ( lsdb-synch-interval secs)\n");
242 return;
243 }
244
245 seconds=atoi(secs);
akmhoqueffacaa82012-09-13 17:48:30 -0500246 if ( seconds >= 120 && seconds <= 3600 )
247 {
248 nlsr->lsdb_synch_interval=seconds;
249 }
akmhoqued79438d2012-08-27 13:31:42 -0500250
251}
252
253
254void
255process_command_interest_retry(char *command)
256{
257 if(command==NULL)
258 {
259 printf(" Wrong Command Format ( interest-retry number )\n");
260 return;
261 }
262 char *rem;
263 const char *sep=" \t\n";
akmhoqueffacaa82012-09-13 17:48:30 -0500264 char *retry;
265 long int retry_number;
akmhoqued79438d2012-08-27 13:31:42 -0500266
akmhoqueffacaa82012-09-13 17:48:30 -0500267 retry=strtok_r(command,sep,&rem);
268 if(retry==NULL)
akmhoqued79438d2012-08-27 13:31:42 -0500269 {
270 printf(" Wrong Command Format ( interest-retry number)\n");
271 return;
272 }
273
akmhoqueffacaa82012-09-13 17:48:30 -0500274 retry_number=atoi(retry);
275 if ( retry_number >= 1 && retry_number<=10 )
276 {
277 nlsr->interest_retry=retry_number;
278 }
akmhoqued79438d2012-08-27 13:31:42 -0500279
280}
281
282void
283process_command_interest_resend_time(char *command)
284{
285 if(command==NULL)
286 {
287 printf(" Wrong Command Format ( interest-resend-time secs )\n");
288 return;
289 }
290 char *rem;
291 const char *sep=" \t\n";
292 char *secs;
293 long int seconds;
294
295 secs=strtok_r(command,sep,&rem);
296 if(secs==NULL)
297 {
298 printf(" Wrong Command Format ( interest-resend-time secs)\n");
299 return;
300 }
301
302 seconds=atoi(secs);
akmhoqueffacaa82012-09-13 17:48:30 -0500303 if ( seconds <= 60 && seconds >= 1 )
304 {
305 nlsr->interest_resend_time=seconds;
306 }
akmhoqued79438d2012-08-27 13:31:42 -0500307}
308
akmhoque03004e62012-09-06 01:12:28 -0500309
akmhoqued5152122012-09-19 06:44:23 -0500310void
311process_command_lsa_refresh_time(char *command)
312{
313 if(command==NULL)
314 {
315 printf(" Wrong Command Format ( lsa-refresh-time secs )\n");
316 return;
317 }
318 char *rem;
319 const char *sep=" \t\n";
320 char *secs;
321 long int seconds;
322
323 secs=strtok_r(command,sep,&rem);
324 if(secs==NULL)
325 {
326 printf(" Wrong Command Format ( lsa-refresh-time secs)\n");
327 return;
328 }
329
330 seconds=atoi(secs);
331 if ( seconds >= 240 && seconds <= 3600 )
332 {
333 nlsr->lsa_refresh_time=seconds;
334 }
335
336}
337
338void
339process_command_router_dead_interval(char *command)
340{
341 if(command==NULL)
342 {
343 printf(" Wrong Command Format ( router-dead-interval secs )\n");
344 return;
345 }
346 char *rem;
347 const char *sep=" \t\n";
348 char *secs;
349 long int seconds;
350
351 secs=strtok_r(command,sep,&rem);
352 if(secs==NULL)
353 {
354 printf(" Wrong Command Format ( router-dead-interval secs)\n");
355 return;
356 }
357
358 seconds=atoi(secs);
359 if ( seconds >= 360 && seconds <= 5400 )
360 {
361 nlsr->router_dead_interval=seconds;
362 }
363
364}
akmhoque03004e62012-09-06 01:12:28 -0500365
akmhoqued79438d2012-08-27 13:31:42 -0500366void
akmhoque59980a52012-08-09 12:36:09 -0500367process_conf_command(char *command)
368{
369 const char *separators=" \t\n";
370 char *remainder=NULL;
371 char *cmd_type=NULL;
372
373 if(command==NULL || strlen(command)==0 || command[0]=='!')
374 return;
375
376 cmd_type=strtok_r(command,separators,&remainder);
377
378 if(!strcmp(cmd_type,"router-name") )
379 {
380 process_command_router_name(remainder);
381 }
382 else if(!strcmp(cmd_type,"ccnneighbor") )
383 {
384 process_command_ccnneighbor(remainder);
385 }
386 else if(!strcmp(cmd_type,"ccnname") )
387 {
388 process_command_ccnname(remainder);
389 }
akmhoqued79438d2012-08-27 13:31:42 -0500390 else if(!strcmp(cmd_type,"lsdb-synch-interval") )
391 {
392 process_command_lsdb_synch_interval(remainder);
393 }
394 else if(!strcmp(cmd_type,"interest-retry") )
395 {
396 process_command_interest_retry(remainder);
397 }
398 else if(!strcmp(cmd_type,"interest-resend-time") )
399 {
400 process_command_interest_resend_time(remainder);
401 }
akmhoqued5152122012-09-19 06:44:23 -0500402 else if(!strcmp(cmd_type,"lsa-refresh-time") )
403 {
404 process_command_lsa_refresh_time(remainder);
405 }
406 else if(!strcmp(cmd_type,"router-dead-interval") )
407 {
408 process_command_router_dead_interval(remainder);
409 }
410 else
akmhoque59980a52012-08-09 12:36:09 -0500411 {
412 printf("Wrong configuration Command %s \n",cmd_type);
413 }
414}
415
akmhoque03004e62012-09-06 01:12:28 -0500416
akmhoque59980a52012-08-09 12:36:09 -0500417int
418readConfigFile(const char *filename)
419{
420 FILE *cfg;
421 char buf[1024];
422 int len;
423
424 cfg=fopen(filename, "r");
425
426 if(cfg == NULL)
427 {
428 printf("\nConfiguration File does not exists\n");
429 exit(1);
430 }
431
432 while(fgets((char *)buf, sizeof(buf), cfg))
433 {
434 len=strlen(buf);
435 if(buf[len-1] == '\n')
436 buf[len-1]='\0';
akmhoqued5152122012-09-19 06:44:23 -0500437 if ( buf[0] != '#' && buf[0] != '!')
438 process_conf_command(buf);
akmhoque59980a52012-08-09 12:36:09 -0500439 }
440
441 fclose(cfg);
442
443 return 0;
444}
445
akmhoque386081b2012-08-10 10:53:21 -0500446void
447nlsr_destroy( void )
448{
449
akmhoque53f64222012-09-05 13:57:51 -0500450 printf("Freeing Allocated Memory....\n");
akmhoquefbfd0982012-09-09 20:59:03 -0500451 /* Destroying all face created by nlsr in CCND */
452 destroy_all_face_by_nlsr();
453
akmhoque386081b2012-08-10 10:53:21 -0500454 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
akmhoque53f64222012-09-05 13:57:51 -0500455 hashtb_destroy(&nlsr->npl);
akmhoque03004e62012-09-06 01:12:28 -0500456 hashtb_destroy(&nlsr->adl);
457 hashtb_destroy(&nlsr->lsdb->name_lsdb);
458 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -0500459 hashtb_destroy(&nlsr->pit_alsa);
akmhoque3560cb62012-09-09 10:52:30 -0500460 hashtb_destroy(&nlsr->routing_table);
461
462
463 int i, npt_element;
464 struct npt_entry *ne;
465 struct hashtb_enumerator ee;
466 struct hashtb_enumerator *e = &ee;
467 hashtb_start(nlsr->npt, e);
468 npt_element=hashtb_n(nlsr->npt);
469 for(i=0;i<npt_element;i++)
470 {
471 ne=e->data;
akmhoque810a5b52012-09-09 16:53:14 -0500472 hashtb_destroy(&ne->name_list);
akmhoque3560cb62012-09-09 10:52:30 -0500473 hashtb_next(e);
474 }
475
476 hashtb_end(e);
477 hashtb_destroy(&nlsr->npt);
478
akmhoque03004e62012-09-06 01:12:28 -0500479
akmhoque386081b2012-08-10 10:53:21 -0500480 ccn_schedule_destroy(&nlsr->sched);
481 ccn_destroy(&nlsr->ccn);
akmhoque03004e62012-09-06 01:12:28 -0500482
483 free(nlsr->lsdb->lsdb_version);
484 free(nlsr->lsdb);
485 free(nlsr->router_name);
akmhoque386081b2012-08-10 10:53:21 -0500486 free(nlsr);
487
akmhoque53f64222012-09-05 13:57:51 -0500488 printf("Finished freeing allocated memory\n");
489
akmhoque386081b2012-08-10 10:53:21 -0500490}
491
akmhoque03004e62012-09-06 01:12:28 -0500492
akmhoque81c25e02012-09-10 14:50:33 -0500493int
akmhoque902d57e2012-08-17 09:24:38 -0500494init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -0500495{
akmhoque03004e62012-09-06 01:12:28 -0500496 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
497 {
498 perror("SIGQUIT install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500499 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500500 }
501 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
502 {
503 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500504 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500505 }
506 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
507 {
508 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500509 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500510 }
akmhoque902d57e2012-08-17 09:24:38 -0500511
akmhoque59980a52012-08-09 12:36:09 -0500512 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
akmhoque03004e62012-09-06 01:12:28 -0500513
514 struct hashtb_param param_adl = {0};
akmhoque28c45022012-08-09 15:38:02 -0500515 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque03004e62012-09-06 01:12:28 -0500516 struct hashtb_param param_npl = {0};
517 nlsr->npl = hashtb_create(sizeof(struct name_prefix), &param_npl);
akmhoque29c1db52012-09-07 14:47:43 -0500518 struct hashtb_param param_pit_alsa = {0};
akmhoque1ce71052012-09-13 22:51:32 -0500519 nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
akmhoque3560cb62012-09-09 10:52:30 -0500520 struct hashtb_param param_npt = {0};
521 nlsr->npt = hashtb_create(sizeof(struct npt_entry), &param_npt);
522 struct hashtb_param param_rte = {0};
523 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), &param_rte);
akmhoque29c1db52012-09-07 14:47:43 -0500524
akmhoque59980a52012-08-09 12:36:09 -0500525 nlsr->in_interest.p = &incoming_interest;
526 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -0500527
akmhoque03004e62012-09-06 01:12:28 -0500528 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
akmhoque07dd8cc2012-08-16 10:23:01 -0500529
akmhoque03004e62012-09-06 01:12:28 -0500530 char *time_stamp=(char *)malloc(20);
531 memset(time_stamp,0,20);
532 get_current_timestamp_micro(time_stamp);
533 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
534 memset(nlsr->lsdb->lsdb_version,'0',strlen(time_stamp));
535 free(time_stamp);
536
537 struct hashtb_param param_adj_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400538 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
akmhoque03004e62012-09-06 01:12:28 -0500539 struct hashtb_param param_name_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400540 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -0500541
542
akmhoque902d57e2012-08-17 09:24:38 -0500543
akmhoque53f64222012-09-05 13:57:51 -0500544
akmhoque59980a52012-08-09 12:36:09 -0500545 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -0500546 nlsr->nlsa_id=0;
akmhoqued79438d2012-08-27 13:31:42 -0500547 nlsr->adj_build_flag=0;
akmhoque53f64222012-09-05 13:57:51 -0500548 nlsr->adj_build_count=0;
549 nlsr->is_build_adj_lsa_sheduled=0;
akmhoque29c1db52012-09-07 14:47:43 -0500550 nlsr->is_send_lsdb_interest_scheduled=0;
551 nlsr->is_route_calculation_scheduled=0;
akmhoqued79438d2012-08-27 13:31:42 -0500552
553 nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
554 nlsr->interest_retry = INTEREST_RETRY;
555 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
akmhoqueda5b6832012-09-13 22:33:55 -0500556 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
557 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
558
akmhoque42098b12012-08-27 22:54:23 -0500559
akmhoqueffacaa82012-09-13 17:48:30 -0500560 nlsr->semaphor=NLSR_UNLOCKED;
akmhoque81c25e02012-09-10 14:50:33 -0500561
562 return 0;
akmhoque902d57e2012-08-17 09:24:38 -0500563}
564
akmhoque03004e62012-09-06 01:12:28 -0500565
akmhoque902d57e2012-08-17 09:24:38 -0500566int
567main(int argc, char *argv[])
568{
akmhoque81c25e02012-09-10 14:50:33 -0500569 int res, ret;
akmhoque902d57e2012-08-17 09:24:38 -0500570 char *config_file;
akmhoqueffacaa82012-09-13 17:48:30 -0500571 //int daemon_mode;
akmhoque902d57e2012-08-17 09:24:38 -0500572
akmhoque81c25e02012-09-10 14:50:33 -0500573 ret=init_nlsr();
574 ON_ERROR_EXIT(ret);
575
akmhoque59980a52012-08-09 12:36:09 -0500576 while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1)
577 {
578 switch (res)
579 {
580 case 'd':
akmhoqueffacaa82012-09-13 17:48:30 -0500581 //daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -0500582 break;
583 case 'f':
584 config_file = optarg;
585 break;
586 case 'h':
587 default:
588 usage(argv[0]);
589 }
590 }
591
592 readConfigFile(config_file);
593
594 nlsr->ccn=ccn_create();
595 if(ccn_connect(nlsr->ccn, NULL) == -1)
akmhoque03004e62012-09-06 01:12:28 -0500596 {
597 fprintf(stderr,"Could not connect to ccnd\n");
akmhoque81c25e02012-09-10 14:50:33 -0500598 ON_ERROR_DESTROY(-1);
akmhoque03004e62012-09-06 01:12:28 -0500599 }
akmhoque53f64222012-09-05 13:57:51 -0500600 struct ccn_charbuf *router_prefix;
akmhoque03004e62012-09-06 01:12:28 -0500601 router_prefix=ccn_charbuf_create();
602 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
akmhoque59980a52012-08-09 12:36:09 -0500603 if(res<0)
akmhoque03004e62012-09-06 01:12:28 -0500604 {
605 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
akmhoque81c25e02012-09-10 14:50:33 -0500606 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -0500607 }
akmhoque59980a52012-08-09 12:36:09 -0500608
609 ccn_name_append_str(router_prefix,"nlsr");
akmhoque03004e62012-09-06 01:12:28 -0500610 nlsr->in_interest.data=nlsr->router_name;
akmhoque59980a52012-08-09 12:36:09 -0500611 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
612 if ( res < 0 )
akmhoque03004e62012-09-06 01:12:28 -0500613 {
614 fprintf(stderr,"Failed to register interest for router\n");
akmhoque81c25e02012-09-10 14:50:33 -0500615 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -0500616 }
617 ccn_charbuf_destroy(&router_prefix);
618
619 printf("Router Name : %s\n",nlsr->router_name);
620 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque59980a52012-08-09 12:36:09 -0500621
akmhoque53f64222012-09-05 13:57:51 -0500622 print_name_prefix_from_npl();
623 print_adjacent_from_adl();
akmhoque53f64222012-09-05 13:57:51 -0500624 build_and_install_name_lsas();
625 print_name_lsdb();
626
627 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque03004e62012-09-06 01:12:28 -0500628 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
akmhoqueffacaa82012-09-13 17:48:30 -0500629 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -0500630
akmhoque59980a52012-08-09 12:36:09 -0500631 while(1)
akmhoque29c1db52012-09-07 14:47:43 -0500632 {
akmhoqueffacaa82012-09-13 17:48:30 -0500633 if ( nlsr->semaphor == NLSR_UNLOCKED )
akmhoque29c1db52012-09-07 14:47:43 -0500634 {
akmhoqueffacaa82012-09-13 17:48:30 -0500635 if( nlsr->sched != NULL )
636 {
637 ccn_schedule_run(nlsr->sched);
638 }
639 if(nlsr->ccn != NULL)
640 {
641 res = ccn_run(nlsr->ccn, 500);
642 }
643 if (!(nlsr->sched && nlsr->ccn))
644 {
645 break;
646 }
akmhoque29c1db52012-09-07 14:47:43 -0500647 }
648
akmhoque59980a52012-08-09 12:36:09 -0500649 }
akmhoque59980a52012-08-09 12:36:09 -0500650
akmhoque59980a52012-08-09 12:36:09 -0500651 return 0;
652}
653