blob: f963b74cf9deccc4312ef814f87ba1683bf63384 [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();
Obaid Amin485277a2012-09-07 01:08:28 -040072// 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");
Obaid Amin485277a2012-09-07 01:08:28 -0400339
340
akmhoque386081b2012-08-10 10:53:21 -0500341 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
akmhoque53f64222012-09-05 13:57:51 -0500342 hashtb_destroy(&nlsr->npl);
akmhoque03004e62012-09-06 01:12:28 -0500343 hashtb_destroy(&nlsr->adl);
344 hashtb_destroy(&nlsr->lsdb->name_lsdb);
345 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
346
akmhoque386081b2012-08-10 10:53:21 -0500347 ccn_schedule_destroy(&nlsr->sched);
348 ccn_destroy(&nlsr->ccn);
akmhoque03004e62012-09-06 01:12:28 -0500349
350 free(nlsr->lsdb->lsdb_version);
351 free(nlsr->lsdb);
352 free(nlsr->router_name);
akmhoque386081b2012-08-10 10:53:21 -0500353 free(nlsr);
354
akmhoque53f64222012-09-05 13:57:51 -0500355 printf("Finished freeing allocated memory\n");
356
akmhoque386081b2012-08-10 10:53:21 -0500357}
358
akmhoque03004e62012-09-06 01:12:28 -0500359
akmhoque902d57e2012-08-17 09:24:38 -0500360void
361init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -0500362{
akmhoque03004e62012-09-06 01:12:28 -0500363 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
364 {
365 perror("SIGQUIT install error\n");
366 exit(1);
367 }
368 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
369 {
370 perror("SIGTERM install error\n");
371 exit(1);
372 }
373 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
374 {
375 perror("SIGTERM install error\n");
376 exit(1);
377 }
akmhoque902d57e2012-08-17 09:24:38 -0500378
Obaid Amin485277a2012-09-07 01:08:28 -0400379
akmhoque59980a52012-08-09 12:36:09 -0500380 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
akmhoque03004e62012-09-06 01:12:28 -0500381
382 struct hashtb_param param_adl = {0};
akmhoque28c45022012-08-09 15:38:02 -0500383 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque03004e62012-09-06 01:12:28 -0500384 struct hashtb_param param_npl = {0};
385 nlsr->npl = hashtb_create(sizeof(struct name_prefix), &param_npl);
akmhoque53f64222012-09-05 13:57:51 -0500386
akmhoque59980a52012-08-09 12:36:09 -0500387 nlsr->in_interest.p = &incoming_interest;
388 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -0500389
akmhoque03004e62012-09-06 01:12:28 -0500390 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
akmhoque07dd8cc2012-08-16 10:23:01 -0500391
akmhoque03004e62012-09-06 01:12:28 -0500392 char *time_stamp=(char *)malloc(20);
393 memset(time_stamp,0,20);
394 get_current_timestamp_micro(time_stamp);
395 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
396 memset(nlsr->lsdb->lsdb_version,'0',strlen(time_stamp));
397 free(time_stamp);
398
399 struct hashtb_param param_adj_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400400 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
akmhoque03004e62012-09-06 01:12:28 -0500401 struct hashtb_param param_name_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400402 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoque902d57e2012-08-17 09:24:38 -0500403
akmhoque53f64222012-09-05 13:57:51 -0500404
akmhoque59980a52012-08-09 12:36:09 -0500405 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -0500406 nlsr->nlsa_id=0;
akmhoqued79438d2012-08-27 13:31:42 -0500407 nlsr->adj_build_flag=0;
akmhoque53f64222012-09-05 13:57:51 -0500408 nlsr->adj_build_count=0;
409 nlsr->is_build_adj_lsa_sheduled=0;
410 nlsr->is_send_lsdb_interest_scheduled=0;
akmhoqued79438d2012-08-27 13:31:42 -0500411
412 nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
413 nlsr->interest_retry = INTEREST_RETRY;
414 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
akmhoque42098b12012-08-27 22:54:23 -0500415
416 nlsr->semaphor=0;
akmhoque902d57e2012-08-17 09:24:38 -0500417}
418
akmhoque03004e62012-09-06 01:12:28 -0500419
akmhoque902d57e2012-08-17 09:24:38 -0500420int
421main(int argc, char *argv[])
422{
423 int res;
424 char *config_file;
akmhoque2852a222012-08-21 12:09:00 -0400425 //int daemon_mode;
akmhoque902d57e2012-08-17 09:24:38 -0500426
akmhoque03004e62012-09-06 01:12:28 -0500427 init_nlsr();
akmhoque59980a52012-08-09 12:36:09 -0500428
429 while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1)
430 {
431 switch (res)
432 {
433 case 'd':
akmhoque2852a222012-08-21 12:09:00 -0400434 //daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -0500435 break;
436 case 'f':
437 config_file = optarg;
438 break;
439 case 'h':
440 default:
441 usage(argv[0]);
442 }
443 }
444
445 readConfigFile(config_file);
446
447 nlsr->ccn=ccn_create();
448 if(ccn_connect(nlsr->ccn, NULL) == -1)
akmhoque03004e62012-09-06 01:12:28 -0500449 {
450 fprintf(stderr,"Could not connect to ccnd\n");
451 exit(1);
452 }
akmhoque53f64222012-09-05 13:57:51 -0500453 struct ccn_charbuf *router_prefix;
akmhoque03004e62012-09-06 01:12:28 -0500454 router_prefix=ccn_charbuf_create();
455 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
akmhoque59980a52012-08-09 12:36:09 -0500456 if(res<0)
akmhoque03004e62012-09-06 01:12:28 -0500457 {
458 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
459 exit(1);
460 }
akmhoque59980a52012-08-09 12:36:09 -0500461
462 ccn_name_append_str(router_prefix,"nlsr");
akmhoque03004e62012-09-06 01:12:28 -0500463 nlsr->in_interest.data=nlsr->router_name;
akmhoque59980a52012-08-09 12:36:09 -0500464 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
465 if ( res < 0 )
akmhoque03004e62012-09-06 01:12:28 -0500466 {
467 fprintf(stderr,"Failed to register interest for router\n");
468 exit(1);
469 }
470 ccn_charbuf_destroy(&router_prefix);
471
472 printf("Router Name : %s\n",nlsr->router_name);
473 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque59980a52012-08-09 12:36:09 -0500474
akmhoque53f64222012-09-05 13:57:51 -0500475 print_name_prefix_from_npl();
476 print_adjacent_from_adl();
akmhoque53f64222012-09-05 13:57:51 -0500477 build_and_install_name_lsas();
478 print_name_lsdb();
479
480 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque03004e62012-09-06 01:12:28 -0500481 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -0500482
akmhoque59980a52012-08-09 12:36:09 -0500483 while(1)
Obaid Amin485277a2012-09-07 01:08:28 -0400484 {
485 if (nlsr->sched)
486 ccn_schedule_run(nlsr->sched);
487 if (nlsr->ccn)
488 res = ccn_run(nlsr->ccn, 500);
akmhoque59980a52012-08-09 12:36:09 -0500489
Obaid Amin485277a2012-09-07 01:08:28 -0400490 if (!(nlsr->sched && nlsr->ccn))
491 break;
akmhoque59980a52012-08-09 12:36:09 -0500492 }
akmhoque59980a52012-08-09 12:36:09 -0500493
akmhoque03004e62012-09-06 01:12:28 -0500494
495 nlsr_destroy();
akmhoque59980a52012-08-09 12:36:09 -0500496 return 0;
497}
498