blob: c896fb3d55e5d64b6c292c4d36edec0b905cabc5 [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");
akmhoquefbfd0982012-09-09 20:59:03 -0500343 /* Destroying all face created by nlsr in CCND */
344 destroy_all_face_by_nlsr();
345
akmhoque386081b2012-08-10 10:53:21 -0500346 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
akmhoque53f64222012-09-05 13:57:51 -0500347 hashtb_destroy(&nlsr->npl);
akmhoque03004e62012-09-06 01:12:28 -0500348 hashtb_destroy(&nlsr->adl);
349 hashtb_destroy(&nlsr->lsdb->name_lsdb);
350 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -0500351 hashtb_destroy(&nlsr->pit_alsa);
akmhoque3560cb62012-09-09 10:52:30 -0500352 hashtb_destroy(&nlsr->routing_table);
353
354
355 int i, npt_element;
356 struct npt_entry *ne;
357 struct hashtb_enumerator ee;
358 struct hashtb_enumerator *e = &ee;
359 hashtb_start(nlsr->npt, e);
360 npt_element=hashtb_n(nlsr->npt);
361 for(i=0;i<npt_element;i++)
362 {
363 ne=e->data;
akmhoque810a5b52012-09-09 16:53:14 -0500364 hashtb_destroy(&ne->name_list);
akmhoque3560cb62012-09-09 10:52:30 -0500365 hashtb_next(e);
366 }
367
368 hashtb_end(e);
369 hashtb_destroy(&nlsr->npt);
370
akmhoque03004e62012-09-06 01:12:28 -0500371
akmhoque386081b2012-08-10 10:53:21 -0500372 ccn_schedule_destroy(&nlsr->sched);
373 ccn_destroy(&nlsr->ccn);
akmhoque03004e62012-09-06 01:12:28 -0500374
375 free(nlsr->lsdb->lsdb_version);
376 free(nlsr->lsdb);
377 free(nlsr->router_name);
akmhoque386081b2012-08-10 10:53:21 -0500378 free(nlsr);
379
akmhoque53f64222012-09-05 13:57:51 -0500380 printf("Finished freeing allocated memory\n");
381
akmhoque386081b2012-08-10 10:53:21 -0500382}
383
akmhoque03004e62012-09-06 01:12:28 -0500384
akmhoque902d57e2012-08-17 09:24:38 -0500385void
386init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -0500387{
akmhoque03004e62012-09-06 01:12:28 -0500388 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
389 {
390 perror("SIGQUIT install error\n");
391 exit(1);
392 }
393 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
394 {
395 perror("SIGTERM install error\n");
396 exit(1);
397 }
398 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
399 {
400 perror("SIGTERM install error\n");
401 exit(1);
402 }
akmhoque902d57e2012-08-17 09:24:38 -0500403
akmhoque59980a52012-08-09 12:36:09 -0500404 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
akmhoque03004e62012-09-06 01:12:28 -0500405
406 struct hashtb_param param_adl = {0};
akmhoque28c45022012-08-09 15:38:02 -0500407 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque03004e62012-09-06 01:12:28 -0500408 struct hashtb_param param_npl = {0};
409 nlsr->npl = hashtb_create(sizeof(struct name_prefix), &param_npl);
akmhoque29c1db52012-09-07 14:47:43 -0500410 struct hashtb_param param_pit_alsa = {0};
411 nlsr->pit_alsa = hashtb_create(sizeof(struct pneding_interest), &param_pit_alsa);
akmhoque3560cb62012-09-09 10:52:30 -0500412 struct hashtb_param param_npt = {0};
413 nlsr->npt = hashtb_create(sizeof(struct npt_entry), &param_npt);
414 struct hashtb_param param_rte = {0};
415 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), &param_rte);
akmhoque29c1db52012-09-07 14:47:43 -0500416
akmhoque59980a52012-08-09 12:36:09 -0500417 nlsr->in_interest.p = &incoming_interest;
418 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -0500419
akmhoque03004e62012-09-06 01:12:28 -0500420 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
akmhoque07dd8cc2012-08-16 10:23:01 -0500421
akmhoque03004e62012-09-06 01:12:28 -0500422 char *time_stamp=(char *)malloc(20);
423 memset(time_stamp,0,20);
424 get_current_timestamp_micro(time_stamp);
425 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
426 memset(nlsr->lsdb->lsdb_version,'0',strlen(time_stamp));
427 free(time_stamp);
428
429 struct hashtb_param param_adj_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400430 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
akmhoque03004e62012-09-06 01:12:28 -0500431 struct hashtb_param param_name_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400432 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -0500433
434
akmhoque902d57e2012-08-17 09:24:38 -0500435
akmhoque53f64222012-09-05 13:57:51 -0500436
akmhoque59980a52012-08-09 12:36:09 -0500437 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -0500438 nlsr->nlsa_id=0;
akmhoqued79438d2012-08-27 13:31:42 -0500439 nlsr->adj_build_flag=0;
akmhoque53f64222012-09-05 13:57:51 -0500440 nlsr->adj_build_count=0;
441 nlsr->is_build_adj_lsa_sheduled=0;
akmhoque29c1db52012-09-07 14:47:43 -0500442 nlsr->is_send_lsdb_interest_scheduled=0;
443 nlsr->is_route_calculation_scheduled=0;
akmhoqued79438d2012-08-27 13:31:42 -0500444
445 nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
446 nlsr->interest_retry = INTEREST_RETRY;
447 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
akmhoque42098b12012-08-27 22:54:23 -0500448
449 nlsr->semaphor=0;
akmhoque902d57e2012-08-17 09:24:38 -0500450}
451
akmhoque03004e62012-09-06 01:12:28 -0500452
akmhoque902d57e2012-08-17 09:24:38 -0500453int
454main(int argc, char *argv[])
455{
456 int res;
457 char *config_file;
akmhoque3560cb62012-09-09 10:52:30 -0500458 int daemon_mode;
akmhoque902d57e2012-08-17 09:24:38 -0500459
akmhoque03004e62012-09-06 01:12:28 -0500460 init_nlsr();
akmhoque59980a52012-08-09 12:36:09 -0500461
462 while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1)
463 {
464 switch (res)
465 {
466 case 'd':
akmhoque3560cb62012-09-09 10:52:30 -0500467 daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -0500468 break;
469 case 'f':
470 config_file = optarg;
471 break;
472 case 'h':
473 default:
474 usage(argv[0]);
475 }
476 }
477
478 readConfigFile(config_file);
479
480 nlsr->ccn=ccn_create();
481 if(ccn_connect(nlsr->ccn, NULL) == -1)
akmhoque03004e62012-09-06 01:12:28 -0500482 {
483 fprintf(stderr,"Could not connect to ccnd\n");
484 exit(1);
485 }
akmhoque53f64222012-09-05 13:57:51 -0500486 struct ccn_charbuf *router_prefix;
akmhoque03004e62012-09-06 01:12:28 -0500487 router_prefix=ccn_charbuf_create();
488 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
akmhoque59980a52012-08-09 12:36:09 -0500489 if(res<0)
akmhoque03004e62012-09-06 01:12:28 -0500490 {
491 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
492 exit(1);
493 }
akmhoque59980a52012-08-09 12:36:09 -0500494
495 ccn_name_append_str(router_prefix,"nlsr");
akmhoque03004e62012-09-06 01:12:28 -0500496 nlsr->in_interest.data=nlsr->router_name;
akmhoque59980a52012-08-09 12:36:09 -0500497 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
498 if ( res < 0 )
akmhoque03004e62012-09-06 01:12:28 -0500499 {
500 fprintf(stderr,"Failed to register interest for router\n");
501 exit(1);
502 }
503 ccn_charbuf_destroy(&router_prefix);
504
505 printf("Router Name : %s\n",nlsr->router_name);
506 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque59980a52012-08-09 12:36:09 -0500507
akmhoque53f64222012-09-05 13:57:51 -0500508 print_name_prefix_from_npl();
509 print_adjacent_from_adl();
akmhoque53f64222012-09-05 13:57:51 -0500510 build_and_install_name_lsas();
511 print_name_lsdb();
512
513 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque03004e62012-09-06 01:12:28 -0500514 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -0500515
akmhoque59980a52012-08-09 12:36:09 -0500516 while(1)
akmhoque29c1db52012-09-07 14:47:43 -0500517 {
518 if( nlsr->sched != NULL )
519 {
Obaid Amin485277a2012-09-07 01:08:28 -0400520 ccn_schedule_run(nlsr->sched);
akmhoque29c1db52012-09-07 14:47:43 -0500521 }
522 if(nlsr->ccn != NULL)
523 {
524 res = ccn_run(nlsr->ccn, 500);
525 }
Obaid Amin485277a2012-09-07 01:08:28 -0400526 if (!(nlsr->sched && nlsr->ccn))
akmhoque29c1db52012-09-07 14:47:43 -0500527 {
528 break;
529 }
530
akmhoque59980a52012-08-09 12:36:09 -0500531 }
akmhoque59980a52012-08-09 12:36:09 -0500532
akmhoque59980a52012-08-09 12:36:09 -0500533 return 0;
534}
535