blob: f5a826f30f8d02505599ed61dda0af7db9fd98a3 [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"
23
24
25struct option longopts[] =
26{
27 { "daemon", no_argument, NULL, 'd'},
28 { "config_file", required_argument, NULL, 'f'},
29 { "help", no_argument, NULL, 'h'},
30 { 0 }
31};
32
33static int
34usage(char *progname)
35{
36
37 printf("Usage: %s [OPTIONS...]\n\
38 NDN routing....\n\
39 -d, --daemon Run in daemon mode\n\
40 -f, --config_file Specify configuration file name\n\
41 -h, --help Display this help message\n", progname);
42
43 exit(1);
44}
45
46void ndn_rtr_gettime(const struct ccn_gettime *self, struct ccn_timeval *result)
47{
48 struct timeval now = {0};
49 gettimeofday(&now, 0);
50 result->s = now.tv_sec;
51 result->micros = now.tv_usec;
52}
53
54static struct ccn_gettime ndn_rtr_ticker = {
55 "timer",
56 &ndn_rtr_gettime,
57 1000000,
58 NULL
59};
60
61void
62process_command_router_name(char *command)
63{
64 if(command==NULL)
65 {
66 printf(" Wrong Command Format ( router-name /router/name )\n");
67 return;
68 }
69 char *rem;
70 const char *sep=" \t\n";
71 char *rtr_name;
72
73 rtr_name=strtok_r(command,sep,&rem);
74 if(rtr_name==NULL)
75 {
76 printf(" Wrong Command Format ( router-name /router/name )\n");
77 return;
78 }
79 nlsr->router_name=(char *)malloc(strlen(rtr_name)+1);
80 memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1);
81 printf("Router Name: %s\n",nlsr->router_name);
82}
83
84void
85process_command_ccnname(char *command)
86{
87
88 if(command==NULL)
89 {
90 printf(" Wrong Command Format ( ccnname /name/prefix)\n");
91 return;
92 }
93 char *rem;
94 const char *sep=" \t\n";
95 char *name_prefix;
96 name_prefix=strtok_r(command,sep,&rem);
97 if(name_prefix==NULL)
98 {
99 printf(" Wrong Command Format ( ccnname /name/prefix/ )\n");
100 return;
101 }
102
103 printf("Name Prefix: %s \n",name_prefix);
akmhoque28c45022012-08-09 15:38:02 -0500104
105 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix *));
106 np->name=(char *)malloc(strlen(name_prefix)+1);
107 memcpy(np->name,name_prefix,strlen(name_prefix)+1);
108 np->length=strlen(name_prefix)+1;
109
110 add_name_prefix_to_npl(np);
111 /* Debugging Purpose */
112 print_name_prefix_from_npl();
113
114 free(np->name);
115 free(np);
116
117
akmhoque59980a52012-08-09 12:36:09 -0500118}
119
120void
121process_command_ccnneighbor(char *command)
122{
123 if(command==NULL)
124 {
akmhoque28c45022012-08-09 15:38:02 -0500125 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500126 return;
127 }
128 char *rem;
129 const char *sep=" \t\n";
akmhoque28c45022012-08-09 15:38:02 -0500130 char *rtr_name,*face;
akmhoque59980a52012-08-09 12:36:09 -0500131
akmhoque28c45022012-08-09 15:38:02 -0500132 rtr_name=strtok_r(command,sep,&rem);
133 if(rtr_name==NULL)
akmhoque59980a52012-08-09 12:36:09 -0500134 {
akmhoque28c45022012-08-09 15:38:02 -0500135 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500136 return;
137 }
138
139 face=strtok_r(NULL,sep,&rem);
140 if(face==NULL)
141 {
akmhoque28c45022012-08-09 15:38:02 -0500142 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500143 return;
144 }
145
akmhoque28c45022012-08-09 15:38:02 -0500146 printf("Router: %s face: %s\n",rtr_name,face);
147 int face_id;
148 int res;
149 res=sscanf(face,"face%d",&face_id);
150
151 if(res != 1 )
152 {
153 printf(" Wrong Command Format ( ccnneighbor router_name faceX) where X is integer\n");
154 return;
155 }
156
157 struct ndn_neighbor *nbr=(struct ndn_neighbor *)malloc(sizeof(struct ndn_neighbor*));
158 nbr->neighbor=(struct name_prefix *)malloc(sizeof(struct name_prefix *));
159 nbr->neighbor->name=(char *)malloc(strlen(rtr_name)+1);
160 memcpy(nbr->neighbor->name,rtr_name,strlen(rtr_name)+1);
161 nbr->neighbor->length=strlen(rtr_name)+1;
162 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
170 free(nbr->neighbor->name);
171 free(nbr->neighbor);
172 free(nbr);
akmhoque59980a52012-08-09 12:36:09 -0500173
174}
175
176void
177process_conf_command(char *command)
178{
179 const char *separators=" \t\n";
180 char *remainder=NULL;
181 char *cmd_type=NULL;
182
183 if(command==NULL || strlen(command)==0 || command[0]=='!')
184 return;
185
186 cmd_type=strtok_r(command,separators,&remainder);
187
188 if(!strcmp(cmd_type,"router-name") )
189 {
190 process_command_router_name(remainder);
191 }
192 else if(!strcmp(cmd_type,"ccnneighbor") )
193 {
194 process_command_ccnneighbor(remainder);
195 }
196 else if(!strcmp(cmd_type,"ccnname") )
197 {
198 process_command_ccnname(remainder);
199 }
200 else
201 {
202 printf("Wrong configuration Command %s \n",cmd_type);
203 }
204}
205
206int
207readConfigFile(const char *filename)
208{
209 FILE *cfg;
210 char buf[1024];
211 int len;
212
213 cfg=fopen(filename, "r");
214
215 if(cfg == NULL)
216 {
217 printf("\nConfiguration File does not exists\n");
218 exit(1);
219 }
220
221 while(fgets((char *)buf, sizeof(buf), cfg))
222 {
223 len=strlen(buf);
224 if(buf[len-1] == '\n')
225 buf[len-1]='\0';
226 process_conf_command(buf);
227 }
228
229 fclose(cfg);
230
231 return 0;
232}
233
akmhoque28c45022012-08-09 15:38:02 -0500234void
235add_name_prefix_to_npl(struct name_prefix *np)
236{
237
238
239 printf("\nadd_name_prefix called\n");
240 printf("Name Prefix: %s and length: %d \n",np->name,np->length);
241
242 struct name_prefix *hnp=(struct name_prefix *)malloc(sizeof(struct name_prefix *));
243
244 struct hashtb_enumerator ee;
245 struct hashtb_enumerator *e = &ee;
246 int res;
247
248 hashtb_start(nlsr->npl, e);
249 res = hashtb_seek(e, np->name, strlen(np->name), 0);
250
251 hnp = e->data;
252 hnp->name=(char *)malloc(np->length);
253 memcpy(hnp->name,np->name,np->length);
254 hnp->length=np->length;
255
256 hashtb_end(e);
257
258 printf("\n");
259
260}
261
262void
263print_name_prefix_from_npl(void)
264{
265 printf("print_name_prefix_from_npl called \n");
266 int i, npl_element;
267 struct name_prefix *np;
268
269 struct hashtb_enumerator ee;
270 struct hashtb_enumerator *e = &ee;
271
272 hashtb_start(nlsr->npl, e);
273 npl_element=hashtb_n(nlsr->npl);
274
275 for(i=0;i<npl_element;i++)
276 {
277 np=e->data;
278 printf("Name Prefix: %s and Length: %d \n",np->name,np->length);
279 hashtb_next(e);
280 }
281
282 hashtb_end(e);
283
284 printf("\n");
285}
286
287void
288add_adjacent_to_adl(struct ndn_neighbor *nbr)
289{
290 printf("\nadd_adjacent_to_adl called\n");
291 printf("Neighbor: %s Length: %d Face: %d Status: %d\n",nbr->neighbor->name,nbr->neighbor->length,nbr->face, nbr->status);
292
293 struct ndn_neighbor *hnbr=(struct ndn_neighbor *)malloc(sizeof(struct ndn_neighbor*));
294
295 struct hashtb_enumerator ee;
296 struct hashtb_enumerator *e = &ee;
297 int res;
298
299 hashtb_start(nlsr->adl, e);
300 res = hashtb_seek(e, nbr->neighbor->name , nbr->neighbor->length, 0);
301
302 hnbr = e->data;
303
304 hnbr->neighbor=(struct name_prefix *)malloc(sizeof(struct name_prefix *));
305 hnbr->neighbor->name=(char *)malloc(nbr->neighbor->length);
306 memcpy(hnbr->neighbor->name,nbr->neighbor->name,nbr->neighbor->length);
akmhoquec9286692012-08-16 09:57:58 -0500307 hnbr->last_lsdb_version=(char *)malloc(16);
308
akmhoque28c45022012-08-09 15:38:02 -0500309 hnbr->neighbor->length=nbr->neighbor->length;
310 hnbr->face=nbr->face;
311 hnbr->status=nbr->status;
akmhoquec9286692012-08-16 09:57:58 -0500312 hnbr->last_lsdb_version="0000000000000000";
akmhoque386081b2012-08-10 10:53:21 -0500313
314 struct hashtb_param param_luq = {0};
315 hnbr->lsa_update_queue=hashtb_create(200, &param_luq);
akmhoque28c45022012-08-09 15:38:02 -0500316
317 hashtb_end(e);
318
319 printf("\n");
320
321
322}
323
324void
325print_adjacent_from_adl(void)
326{
327 printf("print_adjacent_from_adl called \n");
328 int i, adl_element;
329 struct ndn_neighbor *nbr;
330
331 struct hashtb_enumerator ee;
332 struct hashtb_enumerator *e = &ee;
333
334 hashtb_start(nlsr->adl, e);
335 adl_element=hashtb_n(nlsr->adl);
336
337 for(i=0;i<adl_element;i++)
338 {
339 nbr=e->data;
akmhoquec9286692012-08-16 09:57:58 -0500340 printf("Neighbor: %s Length: %d Face: %d Status: %d LSDB Version: %s \n",nbr->neighbor->name,nbr->neighbor->length,nbr->face, nbr->status, nbr->last_lsdb_version);
akmhoque28c45022012-08-09 15:38:02 -0500341 hashtb_next(e);
342 }
343
344 hashtb_end(e);
345
346 printf("\n");
347}
348
349
akmhoque386081b2012-08-10 10:53:21 -0500350void
351nlsr_destroy( void )
352{
353
354 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
355
356 int i, element;
357 struct ndn_neighbor *nbr;
358
359 struct hashtb_enumerator ee;
360 struct hashtb_enumerator *e = &ee;
361
362 hashtb_start(nlsr->adl, e);
363 element=hashtb_n(nlsr->adl);
364
365 for(i=0;i<element;i++)
366 {
367 nbr=e->data;
368 hashtb_destroy(&nbr->lsa_update_queue);
369 hashtb_next(e);
370 }
371 hashtb_end(e);
372
373
374
375 hashtb_destroy(&nlsr->adl);
376 hashtb_destroy(&nlsr->npl);
377 ccn_schedule_destroy(&nlsr->sched);
378 ccn_destroy(&nlsr->ccn);
379 free(nlsr);
380
381}
382
383
akmhoque59980a52012-08-09 12:36:09 -0500384int
385main(int argc, char *argv[])
386{
387 int res;
388 char *config_file;
389 int daemon_mode;
390 struct hashtb_param param_adl = {0};
391 struct hashtb_param param_npl = {0};
392
393 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
394
akmhoque28c45022012-08-09 15:38:02 -0500395 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque8c50d0d2012-08-09 13:38:03 -0500396 nlsr->npl = hashtb_create(sizeof(struct name_prefix), &param_npl);
akmhoque59980a52012-08-09 12:36:09 -0500397 nlsr->in_interest.p = &incoming_interest;
398 nlsr->in_content.p = &incoming_content;
399 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -0500400 nlsr->nlsa_id=0;
akmhoque59980a52012-08-09 12:36:09 -0500401
402 struct ccn_charbuf *router_prefix;
403
404 while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1)
405 {
406 switch (res)
407 {
408 case 'd':
409 daemon_mode = 1;
410 break;
411 case 'f':
412 config_file = optarg;
413 break;
414 case 'h':
415 default:
416 usage(argv[0]);
417 }
418 }
419
420 readConfigFile(config_file);
421
422 nlsr->ccn=ccn_create();
423 if(ccn_connect(nlsr->ccn, NULL) == -1)
424 {
425 fprintf(stderr,"Could not connect to ccnd\n");
426 exit(1);
427 }
428 router_prefix=ccn_charbuf_create();
429 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
430 if(res<0)
431 {
432 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
433 exit(1);
434 }
435
436 ccn_name_append_str(router_prefix,"nlsr");
437 nlsr->in_interest.data=nlsr->router_name;
438 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
439 if ( res < 0 )
440 {
441 fprintf(stderr,"Failed to register interest for router\n");
442 exit(1);
443 }
444
445 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
446
akmhoquec9286692012-08-16 09:57:58 -0500447 nlsr->event_send_lsdb_interest = ccn_schedule_event(nlsr->sched, 1000000, &send_lsdb_interest, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -0500448
akmhoque59980a52012-08-09 12:36:09 -0500449 while(1)
450 {
451 ccn_schedule_run(nlsr->sched);
452 res = ccn_run(nlsr->ccn, 500);
453
454 }
akmhoque59980a52012-08-09 12:36:09 -0500455
akmhoque386081b2012-08-10 10:53:21 -0500456 nlsr_destroy();
akmhoque59980a52012-08-09 12:36:09 -0500457
458 return 0;
459}
460