blob: 935b68670f446577d9ab021d3adb058b2ac30770 [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
akmhoque59980a52012-08-09 12:36:09 -050033
34struct option longopts[] =
35{
36 { "daemon", no_argument, NULL, 'd'},
37 { "config_file", required_argument, NULL, 'f'},
38 { "help", no_argument, NULL, 'h'},
39 { 0 }
40};
41
42static int
43usage(char *progname)
44{
45
46 printf("Usage: %s [OPTIONS...]\n\
47 NDN routing....\n\
48 -d, --daemon Run in daemon mode\n\
49 -f, --config_file Specify configuration file name\n\
50 -h, --help Display this help message\n", progname);
51
52 exit(1);
53}
54
55void ndn_rtr_gettime(const struct ccn_gettime *self, struct ccn_timeval *result)
56{
57 struct timeval now = {0};
58 gettimeofday(&now, 0);
59 result->s = now.tv_sec;
60 result->micros = now.tv_usec;
61}
62
63static struct ccn_gettime ndn_rtr_ticker = {
64 "timer",
65 &ndn_rtr_gettime,
66 1000000,
67 NULL
68};
69
akmhoque42098b12012-08-27 22:54:23 -050070
71void
akmhoque03004e62012-09-06 01:12:28 -050072nlsr_stop_signal_handler(int sig)
akmhoque42098b12012-08-27 22:54:23 -050073{
akmhoque03004e62012-09-06 01:12:28 -050074 signal(sig, SIG_IGN);
75 nlsr_destroy();
akmhoque29c1db52012-09-07 14:47:43 -050076 //exit(0);
akmhoque59980a52012-08-09 12:36:09 -050077}
78
79void
80process_command_ccnneighbor(char *command)
81{
82 if(command==NULL)
83 {
akmhoque28c45022012-08-09 15:38:02 -050084 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -050085 return;
86 }
87 char *rem;
88 const char *sep=" \t\n";
akmhoque28c45022012-08-09 15:38:02 -050089 char *rtr_name,*face;
akmhoque59980a52012-08-09 12:36:09 -050090
akmhoque28c45022012-08-09 15:38:02 -050091 rtr_name=strtok_r(command,sep,&rem);
92 if(rtr_name==NULL)
akmhoque59980a52012-08-09 12:36:09 -050093 {
akmhoque28c45022012-08-09 15:38:02 -050094 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -050095 return;
96 }
97
98 face=strtok_r(NULL,sep,&rem);
99 if(face==NULL)
100 {
akmhoque28c45022012-08-09 15:38:02 -0500101 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500102 return;
103 }
104
akmhoque28c45022012-08-09 15:38:02 -0500105 printf("Router: %s face: %s\n",rtr_name,face);
106 int face_id;
107 int res;
108 res=sscanf(face,"face%d",&face_id);
109
110 if(res != 1 )
111 {
112 printf(" Wrong Command Format ( ccnneighbor router_name faceX) where X is integer\n");
113 return;
114 }
115
akmhoque03004e62012-09-06 01:12:28 -0500116 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
117 nbr->name=(char *)malloc(strlen(rtr_name)+1);
118 memset(nbr->name,0,strlen(rtr_name)+1);
119 memcpy(nbr->name,rtr_name,strlen(rtr_name)+1);
120 nbr->length=strlen(rtr_name)+1;
121
122 add_nbr_to_adl(nbr,face_id);
123
124 free(nbr->name);
125 free(nbr);
126}
127
128void
129process_command_ccnname(char *command)
130{
131
132 if(command==NULL)
133 {
134 printf(" Wrong Command Format ( ccnname /name/prefix)\n");
135 return;
136 }
137 char *rem;
138 const char *sep=" \t\n";
139 char *name;
140 name=strtok_r(command,sep,&rem);
141 if(name==NULL)
142 {
143 printf(" Wrong Command Format ( ccnname /name/prefix/ )\n");
144 return;
145 }
146
147 printf("Name Prefix: %s \n",name);
148
akmhoque53f64222012-09-05 13:57:51 -0500149 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoque03004e62012-09-06 01:12:28 -0500150 np->name=(char *)malloc(strlen(name)+1);
151 memset(np->name,0,strlen(name)+1);
152 memcpy(np->name,name,strlen(name)+1);
153 np->length=strlen(name)+1;
akmhoque386081b2012-08-10 10:53:21 -0500154
akmhoque03004e62012-09-06 01:12:28 -0500155 add_name_to_npl(np);
akmhoque28c45022012-08-09 15:38:02 -0500156
akmhoque03004e62012-09-06 01:12:28 -0500157 free(np->name);
akmhoque53f64222012-09-05 13:57:51 -0500158 free(np);
akmhoque59980a52012-08-09 12:36:09 -0500159}
160
akmhoque03004e62012-09-06 01:12:28 -0500161
162void
163process_command_router_name(char *command)
164{
165 if(command==NULL)
166 {
167 printf(" Wrong Command Format ( router-name /router/name )\n");
168 return;
169 }
170 char *rem;
171 const char *sep=" \t\n";
172 char *rtr_name;
173
174 rtr_name=strtok_r(command,sep,&rem);
175 if(rtr_name==NULL)
176 {
177 printf(" Wrong Command Format ( router-name /router/name )\n");
178 return;
179 }
180
181
182 nlsr->router_name=(char *)malloc(strlen(rtr_name)+1);
183 memset(nlsr->router_name,0,strlen(rtr_name)+1);
184 memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1);
185
186
187}
188
akmhoque59980a52012-08-09 12:36:09 -0500189void
akmhoqued79438d2012-08-27 13:31:42 -0500190process_command_lsdb_synch_interval(char *command)
191{
192 if(command==NULL)
193 {
194 printf(" Wrong Command Format ( lsdb-synch-interval secs )\n");
195 return;
196 }
197 char *rem;
198 const char *sep=" \t\n";
199 char *secs;
200 long int seconds;
201
202 secs=strtok_r(command,sep,&rem);
203 if(secs==NULL)
204 {
205 printf(" Wrong Command Format ( lsdb-synch-interval secs)\n");
206 return;
207 }
208
209 seconds=atoi(secs);
210 nlsr->lsdb_synch_interval=seconds;
211
212}
213
214
215void
216process_command_interest_retry(char *command)
217{
218 if(command==NULL)
219 {
220 printf(" Wrong Command Format ( interest-retry number )\n");
221 return;
222 }
223 char *rem;
224 const char *sep=" \t\n";
225 char *secs;
226 long int seconds;
227
228 secs=strtok_r(command,sep,&rem);
229 if(secs==NULL)
230 {
231 printf(" Wrong Command Format ( interest-retry number)\n");
232 return;
233 }
234
235 seconds=atoi(secs);
236 nlsr->interest_retry=seconds;
237
238}
239
240void
241process_command_interest_resend_time(char *command)
242{
243 if(command==NULL)
244 {
245 printf(" Wrong Command Format ( interest-resend-time secs )\n");
246 return;
247 }
248 char *rem;
249 const char *sep=" \t\n";
250 char *secs;
251 long int seconds;
252
253 secs=strtok_r(command,sep,&rem);
254 if(secs==NULL)
255 {
256 printf(" Wrong Command Format ( interest-resend-time secs)\n");
257 return;
258 }
259
260 seconds=atoi(secs);
261 nlsr->interest_resend_time=seconds;
262
263}
264
akmhoque03004e62012-09-06 01:12:28 -0500265
266
akmhoqued79438d2012-08-27 13:31:42 -0500267void
akmhoque59980a52012-08-09 12:36:09 -0500268process_conf_command(char *command)
269{
270 const char *separators=" \t\n";
271 char *remainder=NULL;
272 char *cmd_type=NULL;
273
274 if(command==NULL || strlen(command)==0 || command[0]=='!')
275 return;
276
277 cmd_type=strtok_r(command,separators,&remainder);
278
279 if(!strcmp(cmd_type,"router-name") )
280 {
281 process_command_router_name(remainder);
282 }
283 else if(!strcmp(cmd_type,"ccnneighbor") )
284 {
285 process_command_ccnneighbor(remainder);
286 }
287 else if(!strcmp(cmd_type,"ccnname") )
288 {
289 process_command_ccnname(remainder);
290 }
akmhoqued79438d2012-08-27 13:31:42 -0500291 else if(!strcmp(cmd_type,"lsdb-synch-interval") )
292 {
293 process_command_lsdb_synch_interval(remainder);
294 }
295 else if(!strcmp(cmd_type,"interest-retry") )
296 {
297 process_command_interest_retry(remainder);
298 }
299 else if(!strcmp(cmd_type,"interest-resend-time") )
300 {
301 process_command_interest_resend_time(remainder);
302 }
akmhoque59980a52012-08-09 12:36:09 -0500303 else
304 {
305 printf("Wrong configuration Command %s \n",cmd_type);
306 }
307}
308
akmhoque03004e62012-09-06 01:12:28 -0500309
akmhoque59980a52012-08-09 12:36:09 -0500310int
311readConfigFile(const char *filename)
312{
313 FILE *cfg;
314 char buf[1024];
315 int len;
316
317 cfg=fopen(filename, "r");
318
319 if(cfg == NULL)
320 {
321 printf("\nConfiguration File does not exists\n");
322 exit(1);
323 }
324
325 while(fgets((char *)buf, sizeof(buf), cfg))
326 {
327 len=strlen(buf);
328 if(buf[len-1] == '\n')
329 buf[len-1]='\0';
330 process_conf_command(buf);
331 }
332
333 fclose(cfg);
334
335 return 0;
336}
337
akmhoque386081b2012-08-10 10:53:21 -0500338void
339nlsr_destroy( void )
340{
341
akmhoque53f64222012-09-05 13:57:51 -0500342 printf("Freeing Allocated Memory....\n");
Obaid Amin485277a2012-09-07 01:08:28 -0400343
akmhoque386081b2012-08-10 10:53:21 -0500344 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
akmhoque53f64222012-09-05 13:57:51 -0500345 hashtb_destroy(&nlsr->npl);
akmhoque03004e62012-09-06 01:12:28 -0500346 hashtb_destroy(&nlsr->adl);
347 hashtb_destroy(&nlsr->lsdb->name_lsdb);
348 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -0500349 hashtb_destroy(&nlsr->pit_alsa);
akmhoque3560cb62012-09-09 10:52:30 -0500350 hashtb_destroy(&nlsr->routing_table);
351
352
353 int i, npt_element;
354 struct npt_entry *ne;
355 struct hashtb_enumerator ee;
356 struct hashtb_enumerator *e = &ee;
357 hashtb_start(nlsr->npt, e);
358 npt_element=hashtb_n(nlsr->npt);
359 for(i=0;i<npt_element;i++)
360 {
361 ne=e->data;
akmhoque810a5b52012-09-09 16:53:14 -0500362 hashtb_destroy(&ne->name_list);
akmhoque3560cb62012-09-09 10:52:30 -0500363 hashtb_next(e);
364 }
365
366 hashtb_end(e);
367 hashtb_destroy(&nlsr->npt);
368
akmhoque03004e62012-09-06 01:12:28 -0500369
akmhoque386081b2012-08-10 10:53:21 -0500370 ccn_schedule_destroy(&nlsr->sched);
371 ccn_destroy(&nlsr->ccn);
akmhoque03004e62012-09-06 01:12:28 -0500372
373 free(nlsr->lsdb->lsdb_version);
374 free(nlsr->lsdb);
375 free(nlsr->router_name);
akmhoque386081b2012-08-10 10:53:21 -0500376 free(nlsr);
377
akmhoque53f64222012-09-05 13:57:51 -0500378 printf("Finished freeing allocated memory\n");
379
akmhoque386081b2012-08-10 10:53:21 -0500380}
381
akmhoque03004e62012-09-06 01:12:28 -0500382
akmhoque902d57e2012-08-17 09:24:38 -0500383void
384init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -0500385{
akmhoque03004e62012-09-06 01:12:28 -0500386 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
387 {
388 perror("SIGQUIT install error\n");
389 exit(1);
390 }
391 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
392 {
393 perror("SIGTERM install error\n");
394 exit(1);
395 }
396 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
397 {
398 perror("SIGTERM install error\n");
399 exit(1);
400 }
akmhoque902d57e2012-08-17 09:24:38 -0500401
akmhoque59980a52012-08-09 12:36:09 -0500402 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
akmhoque03004e62012-09-06 01:12:28 -0500403
404 struct hashtb_param param_adl = {0};
akmhoque28c45022012-08-09 15:38:02 -0500405 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque03004e62012-09-06 01:12:28 -0500406 struct hashtb_param param_npl = {0};
407 nlsr->npl = hashtb_create(sizeof(struct name_prefix), &param_npl);
akmhoque29c1db52012-09-07 14:47:43 -0500408 struct hashtb_param param_pit_alsa = {0};
409 nlsr->pit_alsa = hashtb_create(sizeof(struct pneding_interest), &param_pit_alsa);
akmhoque3560cb62012-09-09 10:52:30 -0500410 struct hashtb_param param_npt = {0};
411 nlsr->npt = hashtb_create(sizeof(struct npt_entry), &param_npt);
412 struct hashtb_param param_rte = {0};
413 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), &param_rte);
akmhoque29c1db52012-09-07 14:47:43 -0500414
akmhoque59980a52012-08-09 12:36:09 -0500415 nlsr->in_interest.p = &incoming_interest;
416 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -0500417
akmhoque03004e62012-09-06 01:12:28 -0500418 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
akmhoque07dd8cc2012-08-16 10:23:01 -0500419
akmhoque03004e62012-09-06 01:12:28 -0500420 char *time_stamp=(char *)malloc(20);
421 memset(time_stamp,0,20);
422 get_current_timestamp_micro(time_stamp);
423 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
424 memset(nlsr->lsdb->lsdb_version,'0',strlen(time_stamp));
425 free(time_stamp);
426
427 struct hashtb_param param_adj_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400428 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
akmhoque03004e62012-09-06 01:12:28 -0500429 struct hashtb_param param_name_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400430 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -0500431
432
akmhoque902d57e2012-08-17 09:24:38 -0500433
akmhoque53f64222012-09-05 13:57:51 -0500434
akmhoque59980a52012-08-09 12:36:09 -0500435 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -0500436 nlsr->nlsa_id=0;
akmhoqued79438d2012-08-27 13:31:42 -0500437 nlsr->adj_build_flag=0;
akmhoque53f64222012-09-05 13:57:51 -0500438 nlsr->adj_build_count=0;
439 nlsr->is_build_adj_lsa_sheduled=0;
akmhoque29c1db52012-09-07 14:47:43 -0500440 nlsr->is_send_lsdb_interest_scheduled=0;
441 nlsr->is_route_calculation_scheduled=0;
akmhoqued79438d2012-08-27 13:31:42 -0500442
443 nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
444 nlsr->interest_retry = INTEREST_RETRY;
445 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
akmhoque42098b12012-08-27 22:54:23 -0500446
447 nlsr->semaphor=0;
akmhoque902d57e2012-08-17 09:24:38 -0500448}
449
akmhoque03004e62012-09-06 01:12:28 -0500450
akmhoque902d57e2012-08-17 09:24:38 -0500451int
452main(int argc, char *argv[])
453{
454 int res;
455 char *config_file;
akmhoque3560cb62012-09-09 10:52:30 -0500456 int daemon_mode;
akmhoque902d57e2012-08-17 09:24:38 -0500457
akmhoque03004e62012-09-06 01:12:28 -0500458 init_nlsr();
akmhoque59980a52012-08-09 12:36:09 -0500459
460 while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1)
461 {
462 switch (res)
463 {
464 case 'd':
akmhoque3560cb62012-09-09 10:52:30 -0500465 daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -0500466 break;
467 case 'f':
468 config_file = optarg;
469 break;
470 case 'h':
471 default:
472 usage(argv[0]);
473 }
474 }
475
476 readConfigFile(config_file);
477
478 nlsr->ccn=ccn_create();
479 if(ccn_connect(nlsr->ccn, NULL) == -1)
akmhoque03004e62012-09-06 01:12:28 -0500480 {
481 fprintf(stderr,"Could not connect to ccnd\n");
482 exit(1);
483 }
akmhoque53f64222012-09-05 13:57:51 -0500484 struct ccn_charbuf *router_prefix;
akmhoque03004e62012-09-06 01:12:28 -0500485 router_prefix=ccn_charbuf_create();
486 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
akmhoque59980a52012-08-09 12:36:09 -0500487 if(res<0)
akmhoque03004e62012-09-06 01:12:28 -0500488 {
489 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
490 exit(1);
491 }
akmhoque59980a52012-08-09 12:36:09 -0500492
493 ccn_name_append_str(router_prefix,"nlsr");
akmhoque03004e62012-09-06 01:12:28 -0500494 nlsr->in_interest.data=nlsr->router_name;
akmhoque59980a52012-08-09 12:36:09 -0500495 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
496 if ( res < 0 )
akmhoque03004e62012-09-06 01:12:28 -0500497 {
498 fprintf(stderr,"Failed to register interest for router\n");
499 exit(1);
500 }
501 ccn_charbuf_destroy(&router_prefix);
502
503 printf("Router Name : %s\n",nlsr->router_name);
504 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque59980a52012-08-09 12:36:09 -0500505
akmhoque53f64222012-09-05 13:57:51 -0500506 print_name_prefix_from_npl();
507 print_adjacent_from_adl();
akmhoque53f64222012-09-05 13:57:51 -0500508 build_and_install_name_lsas();
509 print_name_lsdb();
510
511 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque03004e62012-09-06 01:12:28 -0500512 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -0500513
akmhoque59980a52012-08-09 12:36:09 -0500514 while(1)
akmhoque29c1db52012-09-07 14:47:43 -0500515 {
516 if( nlsr->sched != NULL )
517 {
Obaid Amin485277a2012-09-07 01:08:28 -0400518 ccn_schedule_run(nlsr->sched);
akmhoque29c1db52012-09-07 14:47:43 -0500519 }
520 if(nlsr->ccn != NULL)
521 {
522 res = ccn_run(nlsr->ccn, 500);
523 }
Obaid Amin485277a2012-09-07 01:08:28 -0400524 if (!(nlsr->sched && nlsr->ccn))
akmhoque29c1db52012-09-07 14:47:43 -0500525 {
526 break;
527 }
528
akmhoque59980a52012-08-09 12:36:09 -0500529 }
akmhoque59980a52012-08-09 12:36:09 -0500530
akmhoque59980a52012-08-09 12:36:09 -0500531 return 0;
532}
533