blob: b9a15573fa3d6103b1e641ba9731233bb8dda147 [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;
163 nbr->status=0;
164
165 add_adjacent_to_adl(nbr);
166 print_adjacent_from_adl();
167
168 free(nbr->neighbor->name);
169 free(nbr->neighbor);
170 free(nbr);
akmhoque59980a52012-08-09 12:36:09 -0500171
172}
173
174void
175process_conf_command(char *command)
176{
177 const char *separators=" \t\n";
178 char *remainder=NULL;
179 char *cmd_type=NULL;
180
181 if(command==NULL || strlen(command)==0 || command[0]=='!')
182 return;
183
184 cmd_type=strtok_r(command,separators,&remainder);
185
186 if(!strcmp(cmd_type,"router-name") )
187 {
188 process_command_router_name(remainder);
189 }
190 else if(!strcmp(cmd_type,"ccnneighbor") )
191 {
192 process_command_ccnneighbor(remainder);
193 }
194 else if(!strcmp(cmd_type,"ccnname") )
195 {
196 process_command_ccnname(remainder);
197 }
198 else
199 {
200 printf("Wrong configuration Command %s \n",cmd_type);
201 }
202}
203
204int
205readConfigFile(const char *filename)
206{
207 FILE *cfg;
208 char buf[1024];
209 int len;
210
211 cfg=fopen(filename, "r");
212
213 if(cfg == NULL)
214 {
215 printf("\nConfiguration File does not exists\n");
216 exit(1);
217 }
218
219 while(fgets((char *)buf, sizeof(buf), cfg))
220 {
221 len=strlen(buf);
222 if(buf[len-1] == '\n')
223 buf[len-1]='\0';
224 process_conf_command(buf);
225 }
226
227 fclose(cfg);
228
229 return 0;
230}
231
akmhoque28c45022012-08-09 15:38:02 -0500232void
233add_name_prefix_to_npl(struct name_prefix *np)
234{
235
236
237 printf("\nadd_name_prefix called\n");
238 printf("Name Prefix: %s and length: %d \n",np->name,np->length);
239
240 struct name_prefix *hnp=(struct name_prefix *)malloc(sizeof(struct name_prefix *));
241
242 struct hashtb_enumerator ee;
243 struct hashtb_enumerator *e = &ee;
244 int res;
245
246 hashtb_start(nlsr->npl, e);
247 res = hashtb_seek(e, np->name, strlen(np->name), 0);
248
249 hnp = e->data;
250 hnp->name=(char *)malloc(np->length);
251 memcpy(hnp->name,np->name,np->length);
252 hnp->length=np->length;
253
254 hashtb_end(e);
255
256 printf("\n");
257
258}
259
260void
261print_name_prefix_from_npl(void)
262{
263 printf("print_name_prefix_from_npl called \n");
264 int i, npl_element;
265 struct name_prefix *np;
266
267 struct hashtb_enumerator ee;
268 struct hashtb_enumerator *e = &ee;
269
270 hashtb_start(nlsr->npl, e);
271 npl_element=hashtb_n(nlsr->npl);
272
273 for(i=0;i<npl_element;i++)
274 {
275 np=e->data;
276 printf("Name Prefix: %s and Length: %d \n",np->name,np->length);
277 hashtb_next(e);
278 }
279
280 hashtb_end(e);
281
282 printf("\n");
283}
284
285void
286add_adjacent_to_adl(struct ndn_neighbor *nbr)
287{
288 printf("\nadd_adjacent_to_adl called\n");
289 printf("Neighbor: %s Length: %d Face: %d Status: %d\n",nbr->neighbor->name,nbr->neighbor->length,nbr->face, nbr->status);
290
291 struct ndn_neighbor *hnbr=(struct ndn_neighbor *)malloc(sizeof(struct ndn_neighbor*));
292
293 struct hashtb_enumerator ee;
294 struct hashtb_enumerator *e = &ee;
295 int res;
296
297 hashtb_start(nlsr->adl, e);
298 res = hashtb_seek(e, nbr->neighbor->name , nbr->neighbor->length, 0);
299
300 hnbr = e->data;
301
302 hnbr->neighbor=(struct name_prefix *)malloc(sizeof(struct name_prefix *));
303 hnbr->neighbor->name=(char *)malloc(nbr->neighbor->length);
304 memcpy(hnbr->neighbor->name,nbr->neighbor->name,nbr->neighbor->length);
305 hnbr->neighbor->length=nbr->neighbor->length;
306 hnbr->face=nbr->face;
307 hnbr->status=nbr->status;
308
309 hashtb_end(e);
310
311 printf("\n");
312
313
314}
315
316void
317print_adjacent_from_adl(void)
318{
319 printf("print_adjacent_from_adl called \n");
320 int i, adl_element;
321 struct ndn_neighbor *nbr;
322
323 struct hashtb_enumerator ee;
324 struct hashtb_enumerator *e = &ee;
325
326 hashtb_start(nlsr->adl, e);
327 adl_element=hashtb_n(nlsr->adl);
328
329 for(i=0;i<adl_element;i++)
330 {
331 nbr=e->data;
332 printf("Neighbor: %s Length: %d Face: %d Status: %d\n",nbr->neighbor->name,nbr->neighbor->length,nbr->face, nbr->status);
333 hashtb_next(e);
334 }
335
336 hashtb_end(e);
337
338 printf("\n");
339}
340
341
akmhoque59980a52012-08-09 12:36:09 -0500342int
343main(int argc, char *argv[])
344{
345 int res;
346 char *config_file;
347 int daemon_mode;
348 struct hashtb_param param_adl = {0};
349 struct hashtb_param param_npl = {0};
350
351 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
352
akmhoque28c45022012-08-09 15:38:02 -0500353 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque8c50d0d2012-08-09 13:38:03 -0500354 nlsr->npl = hashtb_create(sizeof(struct name_prefix), &param_npl);
akmhoque59980a52012-08-09 12:36:09 -0500355 nlsr->in_interest.p = &incoming_interest;
356 nlsr->in_content.p = &incoming_content;
357 nlsr->is_synch_init=1;
358
359 struct ccn_charbuf *router_prefix;
360
361 while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1)
362 {
363 switch (res)
364 {
365 case 'd':
366 daemon_mode = 1;
367 break;
368 case 'f':
369 config_file = optarg;
370 break;
371 case 'h':
372 default:
373 usage(argv[0]);
374 }
375 }
376
377 readConfigFile(config_file);
378
379 nlsr->ccn=ccn_create();
380 if(ccn_connect(nlsr->ccn, NULL) == -1)
381 {
382 fprintf(stderr,"Could not connect to ccnd\n");
383 exit(1);
384 }
385 router_prefix=ccn_charbuf_create();
386 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
387 if(res<0)
388 {
389 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
390 exit(1);
391 }
392
393 ccn_name_append_str(router_prefix,"nlsr");
394 nlsr->in_interest.data=nlsr->router_name;
395 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
396 if ( res < 0 )
397 {
398 fprintf(stderr,"Failed to register interest for router\n");
399 exit(1);
400 }
401
402 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
403
404 while(1)
405 {
406 ccn_schedule_run(nlsr->sched);
407 res = ccn_run(nlsr->ccn, 500);
408
409 }
akmhoque59980a52012-08-09 12:36:09 -0500410
411 hashtb_destroy(&nlsr->adl);
412 hashtb_destroy(&nlsr->npl);
413 ccn_schedule_destroy(&nlsr->sched);
414 ccn_destroy(&nlsr->ccn);
415 free(nlsr);
416
417 return 0;
418}
419