blob: 9d7438ed2a610b30212739a6ea34cbba351f9787 [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
11
12
13#include <ccn/ccn.h>
14#include <ccn/uri.h>
15#include <ccn/keystore.h>
16#include <ccn/signing.h>
17#include <ccn/schedule.h>
18#include <ccn/hashtb.h>
19
20#include "nlsr.h"
21#include "nlsr_ndn.h"
akmhoque902d57e2012-08-17 09:24:38 -050022#include "nlsr_lsdb.h"
akmhoque53f64222012-09-05 13:57:51 -050023#include "nlsr_adl.h"
24#include "utility.h"
akmhoque59980a52012-08-09 12:36:09 -050025
26
27struct option longopts[] =
28{
29 { "daemon", no_argument, NULL, 'd'},
30 { "config_file", required_argument, NULL, 'f'},
31 { "help", no_argument, NULL, 'h'},
32 { 0 }
33};
34
35static int
36usage(char *progname)
37{
38
39 printf("Usage: %s [OPTIONS...]\n\
40 NDN routing....\n\
41 -d, --daemon Run in daemon mode\n\
42 -f, --config_file Specify configuration file name\n\
43 -h, --help Display this help message\n", progname);
44
45 exit(1);
46}
47
48void ndn_rtr_gettime(const struct ccn_gettime *self, struct ccn_timeval *result)
49{
50 struct timeval now = {0};
51 gettimeofday(&now, 0);
52 result->s = now.tv_sec;
53 result->micros = now.tv_usec;
54}
55
56static struct ccn_gettime ndn_rtr_ticker = {
57 "timer",
58 &ndn_rtr_gettime,
59 1000000,
60 NULL
61};
62
akmhoque42098b12012-08-27 22:54:23 -050063void
64my_lock(void)
65{
66 nlsr->semaphor=1;
67}
68
69void
70my_unlock(void)
71{
72 nlsr->semaphor=0;
73}
74
akmhoque59980a52012-08-09 12:36:09 -050075void
76process_command_router_name(char *command)
77{
78 if(command==NULL)
79 {
80 printf(" Wrong Command Format ( router-name /router/name )\n");
81 return;
82 }
83 char *rem;
84 const char *sep=" \t\n";
85 char *rtr_name;
86
87 rtr_name=strtok_r(command,sep,&rem);
88 if(rtr_name==NULL)
89 {
90 printf(" Wrong Command Format ( router-name /router/name )\n");
91 return;
92 }
akmhoque53f64222012-09-05 13:57:51 -050093
94 nlsr->router_name->name=(char *)malloc(strlen(rtr_name)+1);
95 memcpy(nlsr->router_name->name,rtr_name,strlen(rtr_name)+1);
96 nlsr->router_name->length=strlen(rtr_name)+1;
97
akmhoque59980a52012-08-09 12:36:09 -050098}
99
100void
101process_command_ccnname(char *command)
102{
103
104 if(command==NULL)
105 {
106 printf(" Wrong Command Format ( ccnname /name/prefix)\n");
107 return;
108 }
109 char *rem;
110 const char *sep=" \t\n";
akmhoque53f64222012-09-05 13:57:51 -0500111 char *name;
112 name=strtok_r(command,sep,&rem);
113 if(name==NULL)
akmhoque59980a52012-08-09 12:36:09 -0500114 {
115 printf(" Wrong Command Format ( ccnname /name/prefix/ )\n");
116 return;
117 }
118
akmhoque53f64222012-09-05 13:57:51 -0500119 printf("Name Prefix: %s \n",name);
akmhoque28c45022012-08-09 15:38:02 -0500120
akmhoque53f64222012-09-05 13:57:51 -0500121 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
122 np->name=(char *)malloc(strlen(name)+1);
123 memcpy(np->name,name,strlen(name)+1);
124 np->length=strlen(name)+1;
125
akmhoque28c45022012-08-09 15:38:02 -0500126 add_name_prefix_to_npl(np);
akmhoque28c45022012-08-09 15:38:02 -0500127
akmhoque28c45022012-08-09 15:38:02 -0500128 free(np);
akmhoque53f64222012-09-05 13:57:51 -0500129
akmhoque28c45022012-08-09 15:38:02 -0500130
akmhoque59980a52012-08-09 12:36:09 -0500131}
132
133void
134process_command_ccnneighbor(char *command)
135{
136 if(command==NULL)
137 {
akmhoque28c45022012-08-09 15:38:02 -0500138 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500139 return;
140 }
141 char *rem;
142 const char *sep=" \t\n";
akmhoque28c45022012-08-09 15:38:02 -0500143 char *rtr_name,*face;
akmhoque59980a52012-08-09 12:36:09 -0500144
akmhoque28c45022012-08-09 15:38:02 -0500145 rtr_name=strtok_r(command,sep,&rem);
146 if(rtr_name==NULL)
akmhoque59980a52012-08-09 12:36:09 -0500147 {
akmhoque28c45022012-08-09 15:38:02 -0500148 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500149 return;
150 }
151
152 face=strtok_r(NULL,sep,&rem);
153 if(face==NULL)
154 {
akmhoque28c45022012-08-09 15:38:02 -0500155 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500156 return;
157 }
158
akmhoque28c45022012-08-09 15:38:02 -0500159 printf("Router: %s face: %s\n",rtr_name,face);
160 int face_id;
161 int res;
162 res=sscanf(face,"face%d",&face_id);
163
164 if(res != 1 )
165 {
166 printf(" Wrong Command Format ( ccnneighbor router_name faceX) where X is integer\n");
167 return;
168 }
169
akmhoque53f64222012-09-05 13:57:51 -0500170 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
171 np->name=(char *)malloc(strlen(rtr_name)+1);
172 memcpy(np->name,rtr_name,strlen(rtr_name)+1);
173 np->length=strlen(rtr_name)+1;
akmhoque386081b2012-08-10 10:53:21 -0500174
akmhoque53f64222012-09-05 13:57:51 -0500175 add_adjacent_to_adl(np,face_id);
akmhoque28c45022012-08-09 15:38:02 -0500176
akmhoque53f64222012-09-05 13:57:51 -0500177 free(np);
akmhoque59980a52012-08-09 12:36:09 -0500178}
179
180void
akmhoqued79438d2012-08-27 13:31:42 -0500181process_command_lsdb_synch_interval(char *command)
182{
183 if(command==NULL)
184 {
185 printf(" Wrong Command Format ( lsdb-synch-interval secs )\n");
186 return;
187 }
188 char *rem;
189 const char *sep=" \t\n";
190 char *secs;
191 long int seconds;
192
193 secs=strtok_r(command,sep,&rem);
194 if(secs==NULL)
195 {
196 printf(" Wrong Command Format ( lsdb-synch-interval secs)\n");
197 return;
198 }
199
200 seconds=atoi(secs);
201 nlsr->lsdb_synch_interval=seconds;
202
203}
204
205
206void
207process_command_interest_retry(char *command)
208{
209 if(command==NULL)
210 {
211 printf(" Wrong Command Format ( interest-retry number )\n");
212 return;
213 }
214 char *rem;
215 const char *sep=" \t\n";
216 char *secs;
217 long int seconds;
218
219 secs=strtok_r(command,sep,&rem);
220 if(secs==NULL)
221 {
222 printf(" Wrong Command Format ( interest-retry number)\n");
223 return;
224 }
225
226 seconds=atoi(secs);
227 nlsr->interest_retry=seconds;
228
229}
230
231void
232process_command_interest_resend_time(char *command)
233{
234 if(command==NULL)
235 {
236 printf(" Wrong Command Format ( interest-resend-time secs )\n");
237 return;
238 }
239 char *rem;
240 const char *sep=" \t\n";
241 char *secs;
242 long int seconds;
243
244 secs=strtok_r(command,sep,&rem);
245 if(secs==NULL)
246 {
247 printf(" Wrong Command Format ( interest-resend-time secs)\n");
248 return;
249 }
250
251 seconds=atoi(secs);
252 nlsr->interest_resend_time=seconds;
253
254}
255
256void
akmhoque59980a52012-08-09 12:36:09 -0500257process_conf_command(char *command)
258{
259 const char *separators=" \t\n";
260 char *remainder=NULL;
261 char *cmd_type=NULL;
262
263 if(command==NULL || strlen(command)==0 || command[0]=='!')
264 return;
265
266 cmd_type=strtok_r(command,separators,&remainder);
267
268 if(!strcmp(cmd_type,"router-name") )
269 {
270 process_command_router_name(remainder);
271 }
272 else if(!strcmp(cmd_type,"ccnneighbor") )
273 {
274 process_command_ccnneighbor(remainder);
275 }
276 else if(!strcmp(cmd_type,"ccnname") )
277 {
278 process_command_ccnname(remainder);
279 }
akmhoqued79438d2012-08-27 13:31:42 -0500280 else if(!strcmp(cmd_type,"lsdb-synch-interval") )
281 {
282 process_command_lsdb_synch_interval(remainder);
283 }
284 else if(!strcmp(cmd_type,"interest-retry") )
285 {
286 process_command_interest_retry(remainder);
287 }
288 else if(!strcmp(cmd_type,"interest-resend-time") )
289 {
290 process_command_interest_resend_time(remainder);
291 }
akmhoque59980a52012-08-09 12:36:09 -0500292 else
293 {
294 printf("Wrong configuration Command %s \n",cmd_type);
295 }
296}
297
298int
299readConfigFile(const char *filename)
300{
301 FILE *cfg;
302 char buf[1024];
303 int len;
304
305 cfg=fopen(filename, "r");
306
307 if(cfg == NULL)
308 {
309 printf("\nConfiguration File does not exists\n");
310 exit(1);
311 }
312
313 while(fgets((char *)buf, sizeof(buf), cfg))
314 {
315 len=strlen(buf);
316 if(buf[len-1] == '\n')
317 buf[len-1]='\0';
318 process_conf_command(buf);
319 }
320
321 fclose(cfg);
322
323 return 0;
324}
325
akmhoque28c45022012-08-09 15:38:02 -0500326void
327add_name_prefix_to_npl(struct name_prefix *np)
328{
329
330
331 printf("\nadd_name_prefix called\n");
332 printf("Name Prefix: %s and length: %d \n",np->name,np->length);
333
akmhoque53f64222012-09-05 13:57:51 -0500334 struct name_prefix *hnp=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoque28c45022012-08-09 15:38:02 -0500335
336 struct hashtb_enumerator ee;
337 struct hashtb_enumerator *e = &ee;
338 int res;
339
340 hashtb_start(nlsr->npl, e);
akmhoque53f64222012-09-05 13:57:51 -0500341 res = hashtb_seek(e, np->name, np->length, 0);
akmhoque2852a222012-08-21 12:09:00 -0400342
akmhoquee7c4b6d2012-08-21 12:30:25 -0400343 if(res == HT_NEW_ENTRY)
344 {
akmhoque2852a222012-08-21 12:09:00 -0400345
akmhoquee7c4b6d2012-08-21 12:30:25 -0400346 hnp = e->data;
akmhoquee7c4b6d2012-08-21 12:30:25 -0400347 hnp->length=np->length;
akmhoque53f64222012-09-05 13:57:51 -0500348 hnp->name=(char *)malloc(np->length);
349 memcpy(hnp->name,np->name,np->length);
akmhoquee7c4b6d2012-08-21 12:30:25 -0400350 }
351
352 hashtb_end(e);
akmhoque28c45022012-08-09 15:38:02 -0500353
akmhoque28c45022012-08-09 15:38:02 -0500354
355 printf("\n");
356
357}
358
359void
360print_name_prefix_from_npl(void)
361{
362 printf("print_name_prefix_from_npl called \n");
363 int i, npl_element;
364 struct name_prefix *np;
365
366 struct hashtb_enumerator ee;
367 struct hashtb_enumerator *e = &ee;
368
369 hashtb_start(nlsr->npl, e);
370 npl_element=hashtb_n(nlsr->npl);
371
372 for(i=0;i<npl_element;i++)
373 {
374 np=e->data;
375 printf("Name Prefix: %s and Length: %d \n",np->name,np->length);
376 hashtb_next(e);
377 }
378
379 hashtb_end(e);
380
381 printf("\n");
382}
383
akmhoque28c45022012-08-09 15:38:02 -0500384
akmhoque386081b2012-08-10 10:53:21 -0500385void
386nlsr_destroy( void )
387{
388
akmhoque53f64222012-09-05 13:57:51 -0500389 printf("Freeing Allocated Memory....\n");
akmhoque386081b2012-08-10 10:53:21 -0500390 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
391
392 int i, element;
akmhoque386081b2012-08-10 10:53:21 -0500393 struct hashtb_enumerator ee;
394 struct hashtb_enumerator *e = &ee;
395
akmhoque53f64222012-09-05 13:57:51 -0500396 /*destroying NAME LSDB */
397 struct nlsr *name_lsa;
398 hashtb_start(nlsr->lsdb->name_lsdb, e);
399 element=hashtb_n(nlsr->lsdb->name_lsdb);
400
401 for(i=0;i<element;i++)
402 {
403 name_lsa=e->data;
404 free(name_lsa);
405 hashtb_next(e);
406 }
407
408 hashtb_end(e);
409 hashtb_destroy(&nlsr->lsdb->name_lsdb);
410
411
412 /*destroying ADJ LSDB */
413 struct alsr *adj_lsa;
414 hashtb_start(nlsr->lsdb->adj_lsdb, e);
415 element=hashtb_n(nlsr->lsdb->adj_lsdb);
416
417 for(i=0;i<element;i++)
418 {
419 adj_lsa=e->data;
420 free(adj_lsa);
421 hashtb_next(e);
422 }
423
424 hashtb_end(e);
425
426
427 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
428
429 /* Destroying NPL */
430 struct ccn_charbuf *np;
431 hashtb_start(nlsr->npl, e);
432 element=hashtb_n(nlsr->npl);
433
434 for(i=0;i<element;i++)
435 {
436 np=e->data;
437 free(np);
438 hashtb_next(e);
439 }
440 hashtb_end(e);
441 hashtb_destroy(&nlsr->npl);
442
443 /* Destroying ADL */
444 struct ndn_neighbor *nbr;
445 hashtb_start(nlsr->adl, e);
akmhoque386081b2012-08-10 10:53:21 -0500446 element=hashtb_n(nlsr->adl);
447
448 for(i=0;i<element;i++)
449 {
450 nbr=e->data;
akmhoque53f64222012-09-05 13:57:51 -0500451 free(nbr);
akmhoque386081b2012-08-10 10:53:21 -0500452 hashtb_next(e);
453 }
454 hashtb_end(e);
akmhoque386081b2012-08-10 10:53:21 -0500455 hashtb_destroy(&nlsr->adl);
akmhoquef71d9082012-08-22 12:51:53 -0400456
akmhoquef71d9082012-08-22 12:51:53 -0400457
akmhoque53f64222012-09-05 13:57:51 -0500458
akmhoque386081b2012-08-10 10:53:21 -0500459 ccn_schedule_destroy(&nlsr->sched);
460 ccn_destroy(&nlsr->ccn);
461 free(nlsr);
462
akmhoque53f64222012-09-05 13:57:51 -0500463 printf("Finished freeing allocated memory\n");
464
akmhoque386081b2012-08-10 10:53:21 -0500465}
466
akmhoque902d57e2012-08-17 09:24:38 -0500467void
468init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -0500469{
akmhoque59980a52012-08-09 12:36:09 -0500470 struct hashtb_param param_adl = {0};
471 struct hashtb_param param_npl = {0};
akmhoque902d57e2012-08-17 09:24:38 -0500472
473 struct hashtb_param param_adj_lsdb = {0};
474 struct hashtb_param param_name_lsdb = {0};
akmhoque59980a52012-08-09 12:36:09 -0500475
476 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
477
akmhoque28c45022012-08-09 15:38:02 -0500478 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque53f64222012-09-05 13:57:51 -0500479 nlsr->npl = hashtb_create(sizeof(struct name_prefix ), &param_npl);
480
akmhoque59980a52012-08-09 12:36:09 -0500481 nlsr->in_interest.p = &incoming_interest;
482 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -0500483
akmhoque53f64222012-09-05 13:57:51 -0500484 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase ));
485 char *time_stamp=get_current_timestamp_micro();
486 nlsr->lsdb->version=(char *)malloc(strlen(time_stamp)+1);
487 memcpy(nlsr->lsdb->version,time_stamp,strlen(time_stamp)+1);
488 memset(nlsr->lsdb->version,'0',strlen(time_stamp));
489
akmhoque07dd8cc2012-08-16 10:23:01 -0500490
akmhoquef71d9082012-08-22 12:51:53 -0400491 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
492 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoque902d57e2012-08-17 09:24:38 -0500493
akmhoque53f64222012-09-05 13:57:51 -0500494 nlsr->router_name=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
495
akmhoque59980a52012-08-09 12:36:09 -0500496 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -0500497 nlsr->nlsa_id=0;
akmhoqued79438d2012-08-27 13:31:42 -0500498 nlsr->adj_build_flag=0;
akmhoque53f64222012-09-05 13:57:51 -0500499 nlsr->adj_build_count=0;
500 nlsr->is_build_adj_lsa_sheduled=0;
501 nlsr->is_send_lsdb_interest_scheduled=0;
akmhoqued79438d2012-08-27 13:31:42 -0500502
503 nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
504 nlsr->interest_retry = INTEREST_RETRY;
505 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
akmhoque42098b12012-08-27 22:54:23 -0500506
507 nlsr->semaphor=0;
akmhoqued79438d2012-08-27 13:31:42 -0500508
akmhoque59980a52012-08-09 12:36:09 -0500509
akmhoque902d57e2012-08-17 09:24:38 -0500510}
511
512int
513main(int argc, char *argv[])
514{
515 int res;
516 char *config_file;
akmhoque2852a222012-08-21 12:09:00 -0400517 //int daemon_mode;
akmhoque53f64222012-09-05 13:57:51 -0500518
akmhoque902d57e2012-08-17 09:24:38 -0500519
520 init_nlsr();
akmhoque59980a52012-08-09 12:36:09 -0500521
522 while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1)
523 {
524 switch (res)
525 {
526 case 'd':
akmhoque2852a222012-08-21 12:09:00 -0400527 //daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -0500528 break;
529 case 'f':
530 config_file = optarg;
531 break;
532 case 'h':
533 default:
534 usage(argv[0]);
535 }
536 }
537
538 readConfigFile(config_file);
539
540 nlsr->ccn=ccn_create();
541 if(ccn_connect(nlsr->ccn, NULL) == -1)
542 {
543 fprintf(stderr,"Could not connect to ccnd\n");
544 exit(1);
akmhoque53f64222012-09-05 13:57:51 -0500545 }
546 struct ccn_charbuf *router_prefix;
547 router_prefix=ccn_charbuf_create();
548 res=ccn_name_from_uri(router_prefix,nlsr->router_name->name);
akmhoque59980a52012-08-09 12:36:09 -0500549 if(res<0)
550 {
akmhoque53f64222012-09-05 13:57:51 -0500551 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name->name);
akmhoque59980a52012-08-09 12:36:09 -0500552 exit(1);
553 }
554
555 ccn_name_append_str(router_prefix,"nlsr");
akmhoque53f64222012-09-05 13:57:51 -0500556 nlsr->in_interest.data=nlsr->router_name->name;
akmhoque59980a52012-08-09 12:36:09 -0500557 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
558 if ( res < 0 )
559 {
560 fprintf(stderr,"Failed to register interest for router\n");
561 exit(1);
562 }
563
akmhoque53f64222012-09-05 13:57:51 -0500564 /* Debugging purpose */
565 print_name_prefix_from_npl();
566 print_adjacent_from_adl();
akmhoqued79438d2012-08-27 13:31:42 -0500567
akmhoque53f64222012-09-05 13:57:51 -0500568 printf("\n");
569 printf("Router Name: %s\n",nlsr->router_name->name);
570 printf("Time in MicroSec: %s \n",get_current_timestamp_micro());
571 printf("LSDB Version: %s\n",nlsr->lsdb->version);
572 printf("\n");
573
574 build_and_install_name_lsas();
575 print_name_lsdb();
576
577 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
578 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 500, &send_info_interest, NULL, 0);
579
580
akmhoque1c9b92f2012-08-13 10:57:50 -0500581
akmhoque59980a52012-08-09 12:36:09 -0500582 while(1)
583 {
akmhoque53f64222012-09-05 13:57:51 -0500584 if(nlsr->semaphor == 0)
585 {
akmhoque42098b12012-08-27 22:54:23 -0500586 ccn_schedule_run(nlsr->sched);
587 res = ccn_run(nlsr->ccn, 500);
588 }
akmhoque59980a52012-08-09 12:36:09 -0500589
590 }
akmhoque59980a52012-08-09 12:36:09 -0500591
akmhoque386081b2012-08-10 10:53:21 -0500592 nlsr_destroy();
akmhoque59980a52012-08-09 12:36:09 -0500593
594 return 0;
595}
596