blob: bcf897c737ef3ed93a827e3f6ddc3b8b1ccdcc5b [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
63void
64process_command_router_name(char *command)
65{
66 if(command==NULL)
67 {
68 printf(" Wrong Command Format ( router-name /router/name )\n");
69 return;
70 }
71 char *rem;
72 const char *sep=" \t\n";
73 char *rtr_name;
74
75 rtr_name=strtok_r(command,sep,&rem);
76 if(rtr_name==NULL)
77 {
78 printf(" Wrong Command Format ( router-name /router/name )\n");
79 return;
80 }
81 nlsr->router_name=(char *)malloc(strlen(rtr_name)+1);
82 memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1);
83 printf("Router Name: %s\n",nlsr->router_name);
84}
85
86void
87process_command_ccnname(char *command)
88{
89
90 if(command==NULL)
91 {
92 printf(" Wrong Command Format ( ccnname /name/prefix)\n");
93 return;
94 }
95 char *rem;
96 const char *sep=" \t\n";
97 char *name_prefix;
98 name_prefix=strtok_r(command,sep,&rem);
99 if(name_prefix==NULL)
100 {
101 printf(" Wrong Command Format ( ccnname /name/prefix/ )\n");
102 return;
103 }
104
105 printf("Name Prefix: %s \n",name_prefix);
akmhoque28c45022012-08-09 15:38:02 -0500106
107 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix *));
108 np->name=(char *)malloc(strlen(name_prefix)+1);
109 memcpy(np->name,name_prefix,strlen(name_prefix)+1);
110 np->length=strlen(name_prefix)+1;
111
112 add_name_prefix_to_npl(np);
113 /* Debugging Purpose */
114 print_name_prefix_from_npl();
115
116 free(np->name);
117 free(np);
118
119
akmhoque59980a52012-08-09 12:36:09 -0500120}
121
122void
123process_command_ccnneighbor(char *command)
124{
125 if(command==NULL)
126 {
akmhoque28c45022012-08-09 15:38:02 -0500127 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500128 return;
129 }
130 char *rem;
131 const char *sep=" \t\n";
akmhoque28c45022012-08-09 15:38:02 -0500132 char *rtr_name,*face;
akmhoque59980a52012-08-09 12:36:09 -0500133
akmhoque28c45022012-08-09 15:38:02 -0500134 rtr_name=strtok_r(command,sep,&rem);
135 if(rtr_name==NULL)
akmhoque59980a52012-08-09 12:36:09 -0500136 {
akmhoque28c45022012-08-09 15:38:02 -0500137 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500138 return;
139 }
140
141 face=strtok_r(NULL,sep,&rem);
142 if(face==NULL)
143 {
akmhoque28c45022012-08-09 15:38:02 -0500144 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500145 return;
146 }
147
akmhoque28c45022012-08-09 15:38:02 -0500148 printf("Router: %s face: %s\n",rtr_name,face);
149 int face_id;
150 int res;
151 res=sscanf(face,"face%d",&face_id);
152
153 if(res != 1 )
154 {
155 printf(" Wrong Command Format ( ccnneighbor router_name faceX) where X is integer\n");
156 return;
157 }
158
159 struct ndn_neighbor *nbr=(struct ndn_neighbor *)malloc(sizeof(struct ndn_neighbor*));
akmhoquea6817692012-08-21 13:50:01 -0400160 nbr->neighbor=ccn_charbuf_create();
akmhoquea6817692012-08-21 13:50:01 -0400161 ccn_charbuf_append_string(nbr->neighbor,rtr_name);
akmhoque28c45022012-08-09 15:38:02 -0500162 nbr->face=face_id;
akmhoque386081b2012-08-10 10:53:21 -0500163 nbr->status=0;
164
165
akmhoque28c45022012-08-09 15:38:02 -0500166
167 add_adjacent_to_adl(nbr);
168 print_adjacent_from_adl();
169
akmhoquea6817692012-08-21 13:50:01 -0400170 //free(nbr->neighbor->name);
171 ccn_charbuf_destroy(&nbr->neighbor);
akmhoque28c45022012-08-09 15:38:02 -0500172 free(nbr->neighbor);
173 free(nbr);
akmhoque59980a52012-08-09 12:36:09 -0500174
175}
176
177void
178process_conf_command(char *command)
179{
180 const char *separators=" \t\n";
181 char *remainder=NULL;
182 char *cmd_type=NULL;
183
184 if(command==NULL || strlen(command)==0 || command[0]=='!')
185 return;
186
187 cmd_type=strtok_r(command,separators,&remainder);
188
189 if(!strcmp(cmd_type,"router-name") )
190 {
191 process_command_router_name(remainder);
192 }
193 else if(!strcmp(cmd_type,"ccnneighbor") )
194 {
195 process_command_ccnneighbor(remainder);
196 }
197 else if(!strcmp(cmd_type,"ccnname") )
198 {
199 process_command_ccnname(remainder);
200 }
201 else
202 {
203 printf("Wrong configuration Command %s \n",cmd_type);
204 }
205}
206
207int
208readConfigFile(const char *filename)
209{
210 FILE *cfg;
211 char buf[1024];
212 int len;
213
214 cfg=fopen(filename, "r");
215
216 if(cfg == NULL)
217 {
218 printf("\nConfiguration File does not exists\n");
219 exit(1);
220 }
221
222 while(fgets((char *)buf, sizeof(buf), cfg))
223 {
224 len=strlen(buf);
225 if(buf[len-1] == '\n')
226 buf[len-1]='\0';
227 process_conf_command(buf);
228 }
229
230 fclose(cfg);
231
232 return 0;
233}
234
akmhoque28c45022012-08-09 15:38:02 -0500235void
236add_name_prefix_to_npl(struct name_prefix *np)
237{
238
239
240 printf("\nadd_name_prefix called\n");
241 printf("Name Prefix: %s and length: %d \n",np->name,np->length);
242
243 struct name_prefix *hnp=(struct name_prefix *)malloc(sizeof(struct name_prefix *));
244
245 struct hashtb_enumerator ee;
246 struct hashtb_enumerator *e = &ee;
247 int res;
248
249 hashtb_start(nlsr->npl, e);
250 res = hashtb_seek(e, np->name, strlen(np->name), 0);
akmhoque2852a222012-08-21 12:09:00 -0400251
akmhoquee7c4b6d2012-08-21 12:30:25 -0400252 if(res == HT_NEW_ENTRY)
253 {
akmhoque2852a222012-08-21 12:09:00 -0400254
akmhoquee7c4b6d2012-08-21 12:30:25 -0400255 hnp = e->data;
256 hnp->name=(char *)malloc(np->length);
257 memcpy(hnp->name,np->name,np->length);
258 hnp->length=np->length;
259 }
260
261 hashtb_end(e);
akmhoque28c45022012-08-09 15:38:02 -0500262
akmhoque28c45022012-08-09 15:38:02 -0500263
264 printf("\n");
265
266}
267
268void
269print_name_prefix_from_npl(void)
270{
271 printf("print_name_prefix_from_npl called \n");
272 int i, npl_element;
273 struct name_prefix *np;
274
275 struct hashtb_enumerator ee;
276 struct hashtb_enumerator *e = &ee;
277
278 hashtb_start(nlsr->npl, e);
279 npl_element=hashtb_n(nlsr->npl);
280
281 for(i=0;i<npl_element;i++)
282 {
283 np=e->data;
284 printf("Name Prefix: %s and Length: %d \n",np->name,np->length);
285 hashtb_next(e);
286 }
287
288 hashtb_end(e);
289
290 printf("\n");
291}
292
akmhoque28c45022012-08-09 15:38:02 -0500293
akmhoque386081b2012-08-10 10:53:21 -0500294void
295nlsr_destroy( void )
296{
297
298 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
299
300 int i, element;
301 struct ndn_neighbor *nbr;
302
303 struct hashtb_enumerator ee;
304 struct hashtb_enumerator *e = &ee;
305
306 hashtb_start(nlsr->adl, e);
307 element=hashtb_n(nlsr->adl);
308
309 for(i=0;i<element;i++)
310 {
311 nbr=e->data;
312 hashtb_destroy(&nbr->lsa_update_queue);
akmhoque49c0e7d2012-08-21 13:57:26 -0400313 ccn_charbuf_destroy(&nbr->neighbor);
akmhoque386081b2012-08-10 10:53:21 -0500314 hashtb_next(e);
315 }
316 hashtb_end(e);
akmhoque386081b2012-08-10 10:53:21 -0500317 hashtb_destroy(&nlsr->adl);
akmhoquef71d9082012-08-22 12:51:53 -0400318
319 /* Destroying every element in Name LSDB Hash Table */
320 hashtb_start(nlsr->adl, e);
321 element=hashtb_n(nlsr->lsdb->name_lsdb);
322
323 struct nlsa *name_lsa;
324
325 for(i=0;i<element;i++)
326 {
327 name_lsa=e->data;
328 ccn_charbuf_destroy(&name_lsa->name_prefix);
329 ccn_charbuf_destroy(&name_lsa->header->orig_router);
330 free(name_lsa->header);
331 free(name_lsa);
332 hashtb_next(e);
333 }
334 hashtb_end(e);
335
336 hashtb_destroy(&nlsr->lsdb->name_lsdb);
337
338 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
339
akmhoque386081b2012-08-10 10:53:21 -0500340 hashtb_destroy(&nlsr->npl);
341 ccn_schedule_destroy(&nlsr->sched);
342 ccn_destroy(&nlsr->ccn);
343 free(nlsr);
344
345}
346
akmhoque902d57e2012-08-17 09:24:38 -0500347void
348init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -0500349{
akmhoque59980a52012-08-09 12:36:09 -0500350 struct hashtb_param param_adl = {0};
351 struct hashtb_param param_npl = {0};
akmhoque902d57e2012-08-17 09:24:38 -0500352
353 struct hashtb_param param_adj_lsdb = {0};
354 struct hashtb_param param_name_lsdb = {0};
akmhoque59980a52012-08-09 12:36:09 -0500355
356 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
357
akmhoque28c45022012-08-09 15:38:02 -0500358 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque8c50d0d2012-08-09 13:38:03 -0500359 nlsr->npl = hashtb_create(sizeof(struct name_prefix), &param_npl);
akmhoque59980a52012-08-09 12:36:09 -0500360 nlsr->in_interest.p = &incoming_interest;
361 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -0500362
363 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase *));
akmhoquef6432c22012-08-21 13:18:05 -0400364 nlsr->lsdb->version=0;
akmhoque07dd8cc2012-08-16 10:23:01 -0500365
akmhoquef71d9082012-08-22 12:51:53 -0400366 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
367 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoque902d57e2012-08-17 09:24:38 -0500368
akmhoque59980a52012-08-09 12:36:09 -0500369 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -0500370 nlsr->nlsa_id=0;
akmhoque59980a52012-08-09 12:36:09 -0500371
akmhoque902d57e2012-08-17 09:24:38 -0500372}
373
374int
375main(int argc, char *argv[])
376{
377 int res;
378 char *config_file;
akmhoque2852a222012-08-21 12:09:00 -0400379 //int daemon_mode;
akmhoque59980a52012-08-09 12:36:09 -0500380 struct ccn_charbuf *router_prefix;
akmhoque902d57e2012-08-17 09:24:38 -0500381
382 init_nlsr();
akmhoque59980a52012-08-09 12:36:09 -0500383
384 while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1)
385 {
386 switch (res)
387 {
388 case 'd':
akmhoque2852a222012-08-21 12:09:00 -0400389 //daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -0500390 break;
391 case 'f':
392 config_file = optarg;
393 break;
394 case 'h':
395 default:
396 usage(argv[0]);
397 }
398 }
399
400 readConfigFile(config_file);
401
402 nlsr->ccn=ccn_create();
403 if(ccn_connect(nlsr->ccn, NULL) == -1)
404 {
405 fprintf(stderr,"Could not connect to ccnd\n");
406 exit(1);
407 }
408 router_prefix=ccn_charbuf_create();
409 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
410 if(res<0)
411 {
412 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
413 exit(1);
414 }
415
416 ccn_name_append_str(router_prefix,"nlsr");
417 nlsr->in_interest.data=nlsr->router_name;
418 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
419 if ( res < 0 )
420 {
421 fprintf(stderr,"Failed to register interest for router\n");
422 exit(1);
423 }
424
425 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
426
akmhoquec9286692012-08-16 09:57:58 -0500427 nlsr->event_send_lsdb_interest = ccn_schedule_event(nlsr->sched, 1000000, &send_lsdb_interest, NULL, 0);
akmhoquef71d9082012-08-22 12:51:53 -0400428 nlsr->event_send_lsdb_interest = ccn_schedule_event(nlsr->sched, 500000, &initial_build_name_lsa, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -0500429
akmhoque59980a52012-08-09 12:36:09 -0500430 while(1)
431 {
432 ccn_schedule_run(nlsr->sched);
433 res = ccn_run(nlsr->ccn, 500);
434
435 }
akmhoque59980a52012-08-09 12:36:09 -0500436
akmhoque386081b2012-08-10 10:53:21 -0500437 nlsr_destroy();
akmhoque59980a52012-08-09 12:36:09 -0500438
439 return 0;
440}
441