blob: 266a9013872a189f2e6a609bda2c122ddb3d5e80 [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"
akmhoque59980a52012-08-09 12:36:09 -050029
30struct option longopts[] =
31{
32 { "daemon", no_argument, NULL, 'd'},
33 { "config_file", required_argument, NULL, 'f'},
34 { "help", no_argument, NULL, 'h'},
35 { 0 }
36};
37
38static int
39usage(char *progname)
40{
41
42 printf("Usage: %s [OPTIONS...]\n\
43 NDN routing....\n\
44 -d, --daemon Run in daemon mode\n\
45 -f, --config_file Specify configuration file name\n\
46 -h, --help Display this help message\n", progname);
47
48 exit(1);
49}
50
51void ndn_rtr_gettime(const struct ccn_gettime *self, struct ccn_timeval *result)
52{
53 struct timeval now = {0};
54 gettimeofday(&now, 0);
55 result->s = now.tv_sec;
56 result->micros = now.tv_usec;
57}
58
59static struct ccn_gettime ndn_rtr_ticker = {
60 "timer",
61 &ndn_rtr_gettime,
62 1000000,
63 NULL
64};
65
akmhoque42098b12012-08-27 22:54:23 -050066
67void
akmhoque03004e62012-09-06 01:12:28 -050068nlsr_stop_signal_handler(int sig)
akmhoque42098b12012-08-27 22:54:23 -050069{
akmhoque03004e62012-09-06 01:12:28 -050070 signal(sig, SIG_IGN);
71 nlsr_destroy();
72 exit(0);
akmhoque59980a52012-08-09 12:36:09 -050073}
74
75void
76process_command_ccnneighbor(char *command)
77{
78 if(command==NULL)
79 {
akmhoque28c45022012-08-09 15:38:02 -050080 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -050081 return;
82 }
83 char *rem;
84 const char *sep=" \t\n";
akmhoque28c45022012-08-09 15:38:02 -050085 char *rtr_name,*face;
akmhoque59980a52012-08-09 12:36:09 -050086
akmhoque28c45022012-08-09 15:38:02 -050087 rtr_name=strtok_r(command,sep,&rem);
88 if(rtr_name==NULL)
akmhoque59980a52012-08-09 12:36:09 -050089 {
akmhoque28c45022012-08-09 15:38:02 -050090 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -050091 return;
92 }
93
94 face=strtok_r(NULL,sep,&rem);
95 if(face==NULL)
96 {
akmhoque28c45022012-08-09 15:38:02 -050097 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -050098 return;
99 }
100
akmhoque28c45022012-08-09 15:38:02 -0500101 printf("Router: %s face: %s\n",rtr_name,face);
102 int face_id;
103 int res;
104 res=sscanf(face,"face%d",&face_id);
105
106 if(res != 1 )
107 {
108 printf(" Wrong Command Format ( ccnneighbor router_name faceX) where X is integer\n");
109 return;
110 }
111
akmhoque03004e62012-09-06 01:12:28 -0500112 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
113 nbr->name=(char *)malloc(strlen(rtr_name)+1);
114 memset(nbr->name,0,strlen(rtr_name)+1);
115 memcpy(nbr->name,rtr_name,strlen(rtr_name)+1);
116 nbr->length=strlen(rtr_name)+1;
117
118 add_nbr_to_adl(nbr,face_id);
119
120 free(nbr->name);
121 free(nbr);
122}
123
124void
125process_command_ccnname(char *command)
126{
127
128 if(command==NULL)
129 {
130 printf(" Wrong Command Format ( ccnname /name/prefix)\n");
131 return;
132 }
133 char *rem;
134 const char *sep=" \t\n";
135 char *name;
136 name=strtok_r(command,sep,&rem);
137 if(name==NULL)
138 {
139 printf(" Wrong Command Format ( ccnname /name/prefix/ )\n");
140 return;
141 }
142
143 printf("Name Prefix: %s \n",name);
144
akmhoque53f64222012-09-05 13:57:51 -0500145 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoque03004e62012-09-06 01:12:28 -0500146 np->name=(char *)malloc(strlen(name)+1);
147 memset(np->name,0,strlen(name)+1);
148 memcpy(np->name,name,strlen(name)+1);
149 np->length=strlen(name)+1;
akmhoque386081b2012-08-10 10:53:21 -0500150
akmhoque03004e62012-09-06 01:12:28 -0500151 add_name_to_npl(np);
akmhoque28c45022012-08-09 15:38:02 -0500152
akmhoque03004e62012-09-06 01:12:28 -0500153 free(np->name);
akmhoque53f64222012-09-05 13:57:51 -0500154 free(np);
akmhoque59980a52012-08-09 12:36:09 -0500155}
156
akmhoque03004e62012-09-06 01:12:28 -0500157
158void
159process_command_router_name(char *command)
160{
161 if(command==NULL)
162 {
163 printf(" Wrong Command Format ( router-name /router/name )\n");
164 return;
165 }
166 char *rem;
167 const char *sep=" \t\n";
168 char *rtr_name;
169
170 rtr_name=strtok_r(command,sep,&rem);
171 if(rtr_name==NULL)
172 {
173 printf(" Wrong Command Format ( router-name /router/name )\n");
174 return;
175 }
176
177
178 nlsr->router_name=(char *)malloc(strlen(rtr_name)+1);
179 memset(nlsr->router_name,0,strlen(rtr_name)+1);
180 memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1);
181
182
183}
184
akmhoque59980a52012-08-09 12:36:09 -0500185void
akmhoqued79438d2012-08-27 13:31:42 -0500186process_command_lsdb_synch_interval(char *command)
187{
188 if(command==NULL)
189 {
190 printf(" Wrong Command Format ( lsdb-synch-interval secs )\n");
191 return;
192 }
193 char *rem;
194 const char *sep=" \t\n";
195 char *secs;
196 long int seconds;
197
198 secs=strtok_r(command,sep,&rem);
199 if(secs==NULL)
200 {
201 printf(" Wrong Command Format ( lsdb-synch-interval secs)\n");
202 return;
203 }
204
205 seconds=atoi(secs);
206 nlsr->lsdb_synch_interval=seconds;
207
208}
209
210
211void
212process_command_interest_retry(char *command)
213{
214 if(command==NULL)
215 {
216 printf(" Wrong Command Format ( interest-retry number )\n");
217 return;
218 }
219 char *rem;
220 const char *sep=" \t\n";
221 char *secs;
222 long int seconds;
223
224 secs=strtok_r(command,sep,&rem);
225 if(secs==NULL)
226 {
227 printf(" Wrong Command Format ( interest-retry number)\n");
228 return;
229 }
230
231 seconds=atoi(secs);
232 nlsr->interest_retry=seconds;
233
234}
235
236void
237process_command_interest_resend_time(char *command)
238{
239 if(command==NULL)
240 {
241 printf(" Wrong Command Format ( interest-resend-time secs )\n");
242 return;
243 }
244 char *rem;
245 const char *sep=" \t\n";
246 char *secs;
247 long int seconds;
248
249 secs=strtok_r(command,sep,&rem);
250 if(secs==NULL)
251 {
252 printf(" Wrong Command Format ( interest-resend-time secs)\n");
253 return;
254 }
255
256 seconds=atoi(secs);
257 nlsr->interest_resend_time=seconds;
258
259}
260
akmhoque03004e62012-09-06 01:12:28 -0500261
262
akmhoqued79438d2012-08-27 13:31:42 -0500263void
akmhoque59980a52012-08-09 12:36:09 -0500264process_conf_command(char *command)
265{
266 const char *separators=" \t\n";
267 char *remainder=NULL;
268 char *cmd_type=NULL;
269
270 if(command==NULL || strlen(command)==0 || command[0]=='!')
271 return;
272
273 cmd_type=strtok_r(command,separators,&remainder);
274
275 if(!strcmp(cmd_type,"router-name") )
276 {
277 process_command_router_name(remainder);
278 }
279 else if(!strcmp(cmd_type,"ccnneighbor") )
280 {
281 process_command_ccnneighbor(remainder);
282 }
283 else if(!strcmp(cmd_type,"ccnname") )
284 {
285 process_command_ccnname(remainder);
286 }
akmhoqued79438d2012-08-27 13:31:42 -0500287 else if(!strcmp(cmd_type,"lsdb-synch-interval") )
288 {
289 process_command_lsdb_synch_interval(remainder);
290 }
291 else if(!strcmp(cmd_type,"interest-retry") )
292 {
293 process_command_interest_retry(remainder);
294 }
295 else if(!strcmp(cmd_type,"interest-resend-time") )
296 {
297 process_command_interest_resend_time(remainder);
298 }
akmhoque59980a52012-08-09 12:36:09 -0500299 else
300 {
301 printf("Wrong configuration Command %s \n",cmd_type);
302 }
303}
304
akmhoque03004e62012-09-06 01:12:28 -0500305
akmhoque59980a52012-08-09 12:36:09 -0500306int
307readConfigFile(const char *filename)
308{
309 FILE *cfg;
310 char buf[1024];
311 int len;
312
313 cfg=fopen(filename, "r");
314
315 if(cfg == NULL)
316 {
317 printf("\nConfiguration File does not exists\n");
318 exit(1);
319 }
320
321 while(fgets((char *)buf, sizeof(buf), cfg))
322 {
323 len=strlen(buf);
324 if(buf[len-1] == '\n')
325 buf[len-1]='\0';
326 process_conf_command(buf);
327 }
328
329 fclose(cfg);
330
331 return 0;
332}
333
akmhoque386081b2012-08-10 10:53:21 -0500334void
335nlsr_destroy( void )
336{
337
akmhoque53f64222012-09-05 13:57:51 -0500338 printf("Freeing Allocated Memory....\n");
akmhoque386081b2012-08-10 10:53:21 -0500339 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
akmhoque53f64222012-09-05 13:57:51 -0500340 hashtb_destroy(&nlsr->npl);
akmhoque03004e62012-09-06 01:12:28 -0500341 hashtb_destroy(&nlsr->adl);
342 hashtb_destroy(&nlsr->lsdb->name_lsdb);
343 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
344
akmhoque53f64222012-09-05 13:57:51 -0500345
akmhoque386081b2012-08-10 10:53:21 -0500346 ccn_schedule_destroy(&nlsr->sched);
347 ccn_destroy(&nlsr->ccn);
akmhoque03004e62012-09-06 01:12:28 -0500348
349 free(nlsr->lsdb->lsdb_version);
350 free(nlsr->lsdb);
351 free(nlsr->router_name);
akmhoque386081b2012-08-10 10:53:21 -0500352 free(nlsr);
353
akmhoque53f64222012-09-05 13:57:51 -0500354 printf("Finished freeing allocated memory\n");
355
akmhoque386081b2012-08-10 10:53:21 -0500356}
357
akmhoque03004e62012-09-06 01:12:28 -0500358
akmhoque902d57e2012-08-17 09:24:38 -0500359void
360init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -0500361{
akmhoque03004e62012-09-06 01:12:28 -0500362 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
363 {
364 perror("SIGQUIT install error\n");
365 exit(1);
366 }
367 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
368 {
369 perror("SIGTERM install error\n");
370 exit(1);
371 }
372 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
373 {
374 perror("SIGTERM install error\n");
375 exit(1);
376 }
akmhoque902d57e2012-08-17 09:24:38 -0500377
akmhoque59980a52012-08-09 12:36:09 -0500378 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
akmhoque03004e62012-09-06 01:12:28 -0500379
380 struct hashtb_param param_adl = {0};
akmhoque28c45022012-08-09 15:38:02 -0500381 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque03004e62012-09-06 01:12:28 -0500382 struct hashtb_param param_npl = {0};
383 nlsr->npl = hashtb_create(sizeof(struct name_prefix), &param_npl);
akmhoque53f64222012-09-05 13:57:51 -0500384
akmhoque59980a52012-08-09 12:36:09 -0500385 nlsr->in_interest.p = &incoming_interest;
386 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -0500387
akmhoque03004e62012-09-06 01:12:28 -0500388 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
akmhoque07dd8cc2012-08-16 10:23:01 -0500389
akmhoque03004e62012-09-06 01:12:28 -0500390 char *time_stamp=(char *)malloc(20);
391 memset(time_stamp,0,20);
392 get_current_timestamp_micro(time_stamp);
393 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
394 memset(nlsr->lsdb->lsdb_version,'0',strlen(time_stamp));
395 free(time_stamp);
396
397 struct hashtb_param param_adj_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400398 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
akmhoque03004e62012-09-06 01:12:28 -0500399 struct hashtb_param param_name_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400400 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoque902d57e2012-08-17 09:24:38 -0500401
akmhoque53f64222012-09-05 13:57:51 -0500402
akmhoque59980a52012-08-09 12:36:09 -0500403 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -0500404 nlsr->nlsa_id=0;
akmhoqued79438d2012-08-27 13:31:42 -0500405 nlsr->adj_build_flag=0;
akmhoque53f64222012-09-05 13:57:51 -0500406 nlsr->adj_build_count=0;
407 nlsr->is_build_adj_lsa_sheduled=0;
408 nlsr->is_send_lsdb_interest_scheduled=0;
akmhoqued79438d2012-08-27 13:31:42 -0500409
410 nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
411 nlsr->interest_retry = INTEREST_RETRY;
412 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
akmhoque42098b12012-08-27 22:54:23 -0500413
414 nlsr->semaphor=0;
akmhoque902d57e2012-08-17 09:24:38 -0500415}
416
akmhoque03004e62012-09-06 01:12:28 -0500417
akmhoque902d57e2012-08-17 09:24:38 -0500418int
419main(int argc, char *argv[])
420{
421 int res;
422 char *config_file;
akmhoque2852a222012-08-21 12:09:00 -0400423 //int daemon_mode;
akmhoque902d57e2012-08-17 09:24:38 -0500424
akmhoque03004e62012-09-06 01:12:28 -0500425 init_nlsr();
akmhoque59980a52012-08-09 12:36:09 -0500426
427 while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1)
428 {
429 switch (res)
430 {
431 case 'd':
akmhoque2852a222012-08-21 12:09:00 -0400432 //daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -0500433 break;
434 case 'f':
435 config_file = optarg;
436 break;
437 case 'h':
438 default:
439 usage(argv[0]);
440 }
441 }
442
443 readConfigFile(config_file);
444
445 nlsr->ccn=ccn_create();
446 if(ccn_connect(nlsr->ccn, NULL) == -1)
akmhoque03004e62012-09-06 01:12:28 -0500447 {
448 fprintf(stderr,"Could not connect to ccnd\n");
449 exit(1);
450 }
akmhoque53f64222012-09-05 13:57:51 -0500451 struct ccn_charbuf *router_prefix;
akmhoque03004e62012-09-06 01:12:28 -0500452 router_prefix=ccn_charbuf_create();
453 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
akmhoque59980a52012-08-09 12:36:09 -0500454 if(res<0)
akmhoque03004e62012-09-06 01:12:28 -0500455 {
456 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
457 exit(1);
458 }
akmhoque59980a52012-08-09 12:36:09 -0500459
460 ccn_name_append_str(router_prefix,"nlsr");
akmhoque03004e62012-09-06 01:12:28 -0500461 nlsr->in_interest.data=nlsr->router_name;
akmhoque59980a52012-08-09 12:36:09 -0500462 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
463 if ( res < 0 )
akmhoque03004e62012-09-06 01:12:28 -0500464 {
465 fprintf(stderr,"Failed to register interest for router\n");
466 exit(1);
467 }
468 ccn_charbuf_destroy(&router_prefix);
469
470 printf("Router Name : %s\n",nlsr->router_name);
471 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque59980a52012-08-09 12:36:09 -0500472
akmhoque53f64222012-09-05 13:57:51 -0500473 print_name_prefix_from_npl();
474 print_adjacent_from_adl();
akmhoque53f64222012-09-05 13:57:51 -0500475 build_and_install_name_lsas();
476 print_name_lsdb();
477
478 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque03004e62012-09-06 01:12:28 -0500479 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -0500480
akmhoque59980a52012-08-09 12:36:09 -0500481 while(1)
akmhoque03004e62012-09-06 01:12:28 -0500482 {
483 ccn_schedule_run(nlsr->sched);
484 res = ccn_run(nlsr->ccn, 500);
akmhoque59980a52012-08-09 12:36:09 -0500485
486 }
akmhoque59980a52012-08-09 12:36:09 -0500487
akmhoque03004e62012-09-06 01:12:28 -0500488
489 nlsr_destroy();
akmhoque59980a52012-08-09 12:36:09 -0500490 return 0;
491}
492