blob: 1576bc4384ab432231175a8495da80d4054b9238 [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"
22#include "utility.h"
akmhoque8a5babe2012-08-16 17:39:33 -050023#include "nlsr_adl.h"
akmhoque902d57e2012-08-17 09:24:38 -050024#include "nlsr_lsdb.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 }
93 nlsr->router_name=(char *)malloc(strlen(rtr_name)+1);
94 memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1);
95 printf("Router Name: %s\n",nlsr->router_name);
96}
97
98void
99process_command_ccnname(char *command)
100{
101
102 if(command==NULL)
103 {
104 printf(" Wrong Command Format ( ccnname /name/prefix)\n");
105 return;
106 }
107 char *rem;
108 const char *sep=" \t\n";
109 char *name_prefix;
110 name_prefix=strtok_r(command,sep,&rem);
111 if(name_prefix==NULL)
112 {
113 printf(" Wrong Command Format ( ccnname /name/prefix/ )\n");
114 return;
115 }
116
117 printf("Name Prefix: %s \n",name_prefix);
akmhoque28c45022012-08-09 15:38:02 -0500118
119 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix *));
120 np->name=(char *)malloc(strlen(name_prefix)+1);
121 memcpy(np->name,name_prefix,strlen(name_prefix)+1);
122 np->length=strlen(name_prefix)+1;
123
124 add_name_prefix_to_npl(np);
125 /* Debugging Purpose */
126 print_name_prefix_from_npl();
127
128 free(np->name);
129 free(np);
130
131
akmhoque59980a52012-08-09 12:36:09 -0500132}
133
134void
135process_command_ccnneighbor(char *command)
136{
137 if(command==NULL)
138 {
akmhoque28c45022012-08-09 15:38:02 -0500139 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500140 return;
141 }
142 char *rem;
143 const char *sep=" \t\n";
akmhoque28c45022012-08-09 15:38:02 -0500144 char *rtr_name,*face;
akmhoque59980a52012-08-09 12:36:09 -0500145
akmhoque28c45022012-08-09 15:38:02 -0500146 rtr_name=strtok_r(command,sep,&rem);
147 if(rtr_name==NULL)
akmhoque59980a52012-08-09 12:36:09 -0500148 {
akmhoque28c45022012-08-09 15:38:02 -0500149 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500150 return;
151 }
152
153 face=strtok_r(NULL,sep,&rem);
154 if(face==NULL)
155 {
akmhoque28c45022012-08-09 15:38:02 -0500156 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500157 return;
158 }
159
akmhoque28c45022012-08-09 15:38:02 -0500160 printf("Router: %s face: %s\n",rtr_name,face);
161 int face_id;
162 int res;
163 res=sscanf(face,"face%d",&face_id);
164
165 if(res != 1 )
166 {
167 printf(" Wrong Command Format ( ccnneighbor router_name faceX) where X is integer\n");
168 return;
169 }
170
171 struct ndn_neighbor *nbr=(struct ndn_neighbor *)malloc(sizeof(struct ndn_neighbor*));
akmhoquea6817692012-08-21 13:50:01 -0400172 nbr->neighbor=ccn_charbuf_create();
akmhoquea6817692012-08-21 13:50:01 -0400173 ccn_charbuf_append_string(nbr->neighbor,rtr_name);
akmhoque28c45022012-08-09 15:38:02 -0500174 nbr->face=face_id;
akmhoque42098b12012-08-27 22:54:23 -0500175 nbr->status=NBR_DOWN;
akmhoque386081b2012-08-10 10:53:21 -0500176
177
akmhoque28c45022012-08-09 15:38:02 -0500178
179 add_adjacent_to_adl(nbr);
180 print_adjacent_from_adl();
181
akmhoquea6817692012-08-21 13:50:01 -0400182 //free(nbr->neighbor->name);
183 ccn_charbuf_destroy(&nbr->neighbor);
akmhoque28c45022012-08-09 15:38:02 -0500184 free(nbr->neighbor);
185 free(nbr);
akmhoque59980a52012-08-09 12:36:09 -0500186
187}
188
189void
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
265void
akmhoque59980a52012-08-09 12:36:09 -0500266process_conf_command(char *command)
267{
268 const char *separators=" \t\n";
269 char *remainder=NULL;
270 char *cmd_type=NULL;
271
272 if(command==NULL || strlen(command)==0 || command[0]=='!')
273 return;
274
275 cmd_type=strtok_r(command,separators,&remainder);
276
277 if(!strcmp(cmd_type,"router-name") )
278 {
279 process_command_router_name(remainder);
280 }
281 else if(!strcmp(cmd_type,"ccnneighbor") )
282 {
283 process_command_ccnneighbor(remainder);
284 }
285 else if(!strcmp(cmd_type,"ccnname") )
286 {
287 process_command_ccnname(remainder);
288 }
akmhoqued79438d2012-08-27 13:31:42 -0500289 else if(!strcmp(cmd_type,"lsdb-synch-interval") )
290 {
291 process_command_lsdb_synch_interval(remainder);
292 }
293 else if(!strcmp(cmd_type,"interest-retry") )
294 {
295 process_command_interest_retry(remainder);
296 }
297 else if(!strcmp(cmd_type,"interest-resend-time") )
298 {
299 process_command_interest_resend_time(remainder);
300 }
akmhoque59980a52012-08-09 12:36:09 -0500301 else
302 {
303 printf("Wrong configuration Command %s \n",cmd_type);
304 }
305}
306
307int
308readConfigFile(const char *filename)
309{
310 FILE *cfg;
311 char buf[1024];
312 int len;
313
314 cfg=fopen(filename, "r");
315
316 if(cfg == NULL)
317 {
318 printf("\nConfiguration File does not exists\n");
319 exit(1);
320 }
321
322 while(fgets((char *)buf, sizeof(buf), cfg))
323 {
324 len=strlen(buf);
325 if(buf[len-1] == '\n')
326 buf[len-1]='\0';
327 process_conf_command(buf);
328 }
329
330 fclose(cfg);
331
332 return 0;
333}
334
akmhoque28c45022012-08-09 15:38:02 -0500335void
336add_name_prefix_to_npl(struct name_prefix *np)
337{
338
339
340 printf("\nadd_name_prefix called\n");
341 printf("Name Prefix: %s and length: %d \n",np->name,np->length);
342
343 struct name_prefix *hnp=(struct name_prefix *)malloc(sizeof(struct name_prefix *));
344
345 struct hashtb_enumerator ee;
346 struct hashtb_enumerator *e = &ee;
347 int res;
348
349 hashtb_start(nlsr->npl, e);
350 res = hashtb_seek(e, np->name, strlen(np->name), 0);
akmhoque2852a222012-08-21 12:09:00 -0400351
akmhoquee7c4b6d2012-08-21 12:30:25 -0400352 if(res == HT_NEW_ENTRY)
353 {
akmhoque2852a222012-08-21 12:09:00 -0400354
akmhoquee7c4b6d2012-08-21 12:30:25 -0400355 hnp = e->data;
356 hnp->name=(char *)malloc(np->length);
357 memcpy(hnp->name,np->name,np->length);
358 hnp->length=np->length;
359 }
360
361 hashtb_end(e);
akmhoque28c45022012-08-09 15:38:02 -0500362
akmhoque28c45022012-08-09 15:38:02 -0500363
364 printf("\n");
365
366}
367
368void
369print_name_prefix_from_npl(void)
370{
371 printf("print_name_prefix_from_npl called \n");
372 int i, npl_element;
373 struct name_prefix *np;
374
375 struct hashtb_enumerator ee;
376 struct hashtb_enumerator *e = &ee;
377
378 hashtb_start(nlsr->npl, e);
379 npl_element=hashtb_n(nlsr->npl);
380
381 for(i=0;i<npl_element;i++)
382 {
383 np=e->data;
384 printf("Name Prefix: %s and Length: %d \n",np->name,np->length);
385 hashtb_next(e);
386 }
387
388 hashtb_end(e);
389
390 printf("\n");
391}
392
akmhoque28c45022012-08-09 15:38:02 -0500393
akmhoque386081b2012-08-10 10:53:21 -0500394void
395nlsr_destroy( void )
396{
397
398 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
399
400 int i, element;
401 struct ndn_neighbor *nbr;
402
403 struct hashtb_enumerator ee;
404 struct hashtb_enumerator *e = &ee;
405
406 hashtb_start(nlsr->adl, e);
407 element=hashtb_n(nlsr->adl);
408
409 for(i=0;i<element;i++)
410 {
411 nbr=e->data;
412 hashtb_destroy(&nbr->lsa_update_queue);
akmhoque49c0e7d2012-08-21 13:57:26 -0400413 ccn_charbuf_destroy(&nbr->neighbor);
akmhoque386081b2012-08-10 10:53:21 -0500414 hashtb_next(e);
415 }
416 hashtb_end(e);
akmhoque386081b2012-08-10 10:53:21 -0500417 hashtb_destroy(&nlsr->adl);
akmhoquef71d9082012-08-22 12:51:53 -0400418
419 /* Destroying every element in Name LSDB Hash Table */
420 hashtb_start(nlsr->adl, e);
421 element=hashtb_n(nlsr->lsdb->name_lsdb);
422
423 struct nlsa *name_lsa;
424
425 for(i=0;i<element;i++)
426 {
427 name_lsa=e->data;
428 ccn_charbuf_destroy(&name_lsa->name_prefix);
429 ccn_charbuf_destroy(&name_lsa->header->orig_router);
430 free(name_lsa->header);
431 free(name_lsa);
432 hashtb_next(e);
433 }
434 hashtb_end(e);
435
436 hashtb_destroy(&nlsr->lsdb->name_lsdb);
437
438 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
439
akmhoque386081b2012-08-10 10:53:21 -0500440 hashtb_destroy(&nlsr->npl);
441 ccn_schedule_destroy(&nlsr->sched);
442 ccn_destroy(&nlsr->ccn);
443 free(nlsr);
444
445}
446
akmhoque902d57e2012-08-17 09:24:38 -0500447void
448init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -0500449{
akmhoque59980a52012-08-09 12:36:09 -0500450 struct hashtb_param param_adl = {0};
451 struct hashtb_param param_npl = {0};
akmhoque902d57e2012-08-17 09:24:38 -0500452
453 struct hashtb_param param_adj_lsdb = {0};
454 struct hashtb_param param_name_lsdb = {0};
akmhoque59980a52012-08-09 12:36:09 -0500455
456 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
457
akmhoque28c45022012-08-09 15:38:02 -0500458 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque8c50d0d2012-08-09 13:38:03 -0500459 nlsr->npl = hashtb_create(sizeof(struct name_prefix), &param_npl);
akmhoque59980a52012-08-09 12:36:09 -0500460 nlsr->in_interest.p = &incoming_interest;
461 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -0500462
463 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase *));
akmhoquef6432c22012-08-21 13:18:05 -0400464 nlsr->lsdb->version=0;
akmhoque07dd8cc2012-08-16 10:23:01 -0500465
akmhoquef71d9082012-08-22 12:51:53 -0400466 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
467 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoque902d57e2012-08-17 09:24:38 -0500468
akmhoque59980a52012-08-09 12:36:09 -0500469 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -0500470 nlsr->nlsa_id=0;
akmhoqued79438d2012-08-27 13:31:42 -0500471 nlsr->adj_build_flag=0;
472 nlsr->adj_build_count=0;
473
474 nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
475 nlsr->interest_retry = INTEREST_RETRY;
476 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
akmhoque42098b12012-08-27 22:54:23 -0500477
478 nlsr->semaphor=0;
akmhoqued79438d2012-08-27 13:31:42 -0500479
akmhoque59980a52012-08-09 12:36:09 -0500480
akmhoque902d57e2012-08-17 09:24:38 -0500481}
482
483int
484main(int argc, char *argv[])
485{
486 int res;
487 char *config_file;
akmhoque2852a222012-08-21 12:09:00 -0400488 //int daemon_mode;
akmhoque59980a52012-08-09 12:36:09 -0500489 struct ccn_charbuf *router_prefix;
akmhoque902d57e2012-08-17 09:24:38 -0500490
491 init_nlsr();
akmhoque59980a52012-08-09 12:36:09 -0500492
493 while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1)
494 {
495 switch (res)
496 {
497 case 'd':
akmhoque2852a222012-08-21 12:09:00 -0400498 //daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -0500499 break;
500 case 'f':
501 config_file = optarg;
502 break;
503 case 'h':
504 default:
505 usage(argv[0]);
506 }
507 }
508
509 readConfigFile(config_file);
510
511 nlsr->ccn=ccn_create();
512 if(ccn_connect(nlsr->ccn, NULL) == -1)
513 {
514 fprintf(stderr,"Could not connect to ccnd\n");
515 exit(1);
516 }
517 router_prefix=ccn_charbuf_create();
518 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
519 if(res<0)
520 {
521 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
522 exit(1);
523 }
524
525 ccn_name_append_str(router_prefix,"nlsr");
526 nlsr->in_interest.data=nlsr->router_name;
527 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
528 if ( res < 0 )
529 {
530 fprintf(stderr,"Failed to register interest for router\n");
531 exit(1);
532 }
533
534 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
535
akmhoque42098b12012-08-27 22:54:23 -0500536 nlsr->event_build_name_lsa = ccn_schedule_event(nlsr->sched, 100, &initial_build_name_lsa, NULL, 0);
akmhoqued79438d2012-08-27 13:31:42 -0500537 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1000000, &send_info_interest, NULL, 0);
538
akmhoque1c9b92f2012-08-13 10:57:50 -0500539
akmhoque59980a52012-08-09 12:36:09 -0500540 while(1)
541 {
akmhoque42098b12012-08-27 22:54:23 -0500542 if(nlsr->semaphor !=1)
543 {
544 ccn_schedule_run(nlsr->sched);
545 res = ccn_run(nlsr->ccn, 500);
546 }
akmhoque59980a52012-08-09 12:36:09 -0500547
548 }
akmhoque59980a52012-08-09 12:36:09 -0500549
akmhoque386081b2012-08-10 10:53:21 -0500550 nlsr_destroy();
akmhoque59980a52012-08-09 12:36:09 -0500551
552 return 0;
553}
554