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