blob: 6dc8fb7e98abde4328210c334522ae5d5ea79e20 [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"
akmhoque59980a52012-08-09 12:36:09 -050024
25
26struct option longopts[] =
27{
28 { "daemon", no_argument, NULL, 'd'},
29 { "config_file", required_argument, NULL, 'f'},
30 { "help", no_argument, NULL, 'h'},
31 { 0 }
32};
33
34static int
35usage(char *progname)
36{
37
38 printf("Usage: %s [OPTIONS...]\n\
39 NDN routing....\n\
40 -d, --daemon Run in daemon mode\n\
41 -f, --config_file Specify configuration file name\n\
42 -h, --help Display this help message\n", progname);
43
44 exit(1);
45}
46
47void ndn_rtr_gettime(const struct ccn_gettime *self, struct ccn_timeval *result)
48{
49 struct timeval now = {0};
50 gettimeofday(&now, 0);
51 result->s = now.tv_sec;
52 result->micros = now.tv_usec;
53}
54
55static struct ccn_gettime ndn_rtr_ticker = {
56 "timer",
57 &ndn_rtr_gettime,
58 1000000,
59 NULL
60};
61
62void
63process_command_router_name(char *command)
64{
65 if(command==NULL)
66 {
67 printf(" Wrong Command Format ( router-name /router/name )\n");
68 return;
69 }
70 char *rem;
71 const char *sep=" \t\n";
72 char *rtr_name;
73
74 rtr_name=strtok_r(command,sep,&rem);
75 if(rtr_name==NULL)
76 {
77 printf(" Wrong Command Format ( router-name /router/name )\n");
78 return;
79 }
80 nlsr->router_name=(char *)malloc(strlen(rtr_name)+1);
81 memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1);
82 printf("Router Name: %s\n",nlsr->router_name);
83}
84
85void
86process_command_ccnname(char *command)
87{
88
89 if(command==NULL)
90 {
91 printf(" Wrong Command Format ( ccnname /name/prefix)\n");
92 return;
93 }
94 char *rem;
95 const char *sep=" \t\n";
96 char *name_prefix;
97 name_prefix=strtok_r(command,sep,&rem);
98 if(name_prefix==NULL)
99 {
100 printf(" Wrong Command Format ( ccnname /name/prefix/ )\n");
101 return;
102 }
103
104 printf("Name Prefix: %s \n",name_prefix);
akmhoque28c45022012-08-09 15:38:02 -0500105
106 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix *));
107 np->name=(char *)malloc(strlen(name_prefix)+1);
108 memcpy(np->name,name_prefix,strlen(name_prefix)+1);
109 np->length=strlen(name_prefix)+1;
110
111 add_name_prefix_to_npl(np);
112 /* Debugging Purpose */
113 print_name_prefix_from_npl();
114
115 free(np->name);
116 free(np);
117
118
akmhoque59980a52012-08-09 12:36:09 -0500119}
120
121void
122process_command_ccnneighbor(char *command)
123{
124 if(command==NULL)
125 {
akmhoque28c45022012-08-09 15:38:02 -0500126 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500127 return;
128 }
129 char *rem;
130 const char *sep=" \t\n";
akmhoque28c45022012-08-09 15:38:02 -0500131 char *rtr_name,*face;
akmhoque59980a52012-08-09 12:36:09 -0500132
akmhoque28c45022012-08-09 15:38:02 -0500133 rtr_name=strtok_r(command,sep,&rem);
134 if(rtr_name==NULL)
akmhoque59980a52012-08-09 12:36:09 -0500135 {
akmhoque28c45022012-08-09 15:38:02 -0500136 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500137 return;
138 }
139
140 face=strtok_r(NULL,sep,&rem);
141 if(face==NULL)
142 {
akmhoque28c45022012-08-09 15:38:02 -0500143 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500144 return;
145 }
146
akmhoque28c45022012-08-09 15:38:02 -0500147 printf("Router: %s face: %s\n",rtr_name,face);
148 int face_id;
149 int res;
150 res=sscanf(face,"face%d",&face_id);
151
152 if(res != 1 )
153 {
154 printf(" Wrong Command Format ( ccnneighbor router_name faceX) where X is integer\n");
155 return;
156 }
157
158 struct ndn_neighbor *nbr=(struct ndn_neighbor *)malloc(sizeof(struct ndn_neighbor*));
159 nbr->neighbor=(struct name_prefix *)malloc(sizeof(struct name_prefix *));
160 nbr->neighbor->name=(char *)malloc(strlen(rtr_name)+1);
161 memcpy(nbr->neighbor->name,rtr_name,strlen(rtr_name)+1);
akmhoque8a5babe2012-08-16 17:39:33 -0500162 nbr->neighbor->length=strlen(rtr_name);
akmhoque28c45022012-08-09 15:38:02 -0500163 nbr->face=face_id;
akmhoque386081b2012-08-10 10:53:21 -0500164 nbr->status=0;
165
166
akmhoque28c45022012-08-09 15:38:02 -0500167
168 add_adjacent_to_adl(nbr);
169 print_adjacent_from_adl();
170
171 free(nbr->neighbor->name);
172 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);
251
252 hnp = e->data;
253 hnp->name=(char *)malloc(np->length);
254 memcpy(hnp->name,np->name,np->length);
255 hnp->length=np->length;
256
257 hashtb_end(e);
258
259 printf("\n");
260
261}
262
263void
264print_name_prefix_from_npl(void)
265{
266 printf("print_name_prefix_from_npl called \n");
267 int i, npl_element;
268 struct name_prefix *np;
269
270 struct hashtb_enumerator ee;
271 struct hashtb_enumerator *e = &ee;
272
273 hashtb_start(nlsr->npl, e);
274 npl_element=hashtb_n(nlsr->npl);
275
276 for(i=0;i<npl_element;i++)
277 {
278 np=e->data;
279 printf("Name Prefix: %s and Length: %d \n",np->name,np->length);
280 hashtb_next(e);
281 }
282
283 hashtb_end(e);
284
285 printf("\n");
286}
287
akmhoque28c45022012-08-09 15:38:02 -0500288
akmhoque386081b2012-08-10 10:53:21 -0500289void
290nlsr_destroy( void )
291{
292
293 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
294
295 int i, element;
296 struct ndn_neighbor *nbr;
297
298 struct hashtb_enumerator ee;
299 struct hashtb_enumerator *e = &ee;
300
301 hashtb_start(nlsr->adl, e);
302 element=hashtb_n(nlsr->adl);
303
304 for(i=0;i<element;i++)
305 {
306 nbr=e->data;
307 hashtb_destroy(&nbr->lsa_update_queue);
308 hashtb_next(e);
309 }
310 hashtb_end(e);
311
312
313
314 hashtb_destroy(&nlsr->adl);
315 hashtb_destroy(&nlsr->npl);
316 ccn_schedule_destroy(&nlsr->sched);
317 ccn_destroy(&nlsr->ccn);
318 free(nlsr);
319
320}
321
322
akmhoque59980a52012-08-09 12:36:09 -0500323int
324main(int argc, char *argv[])
325{
326 int res;
327 char *config_file;
328 int daemon_mode;
329 struct hashtb_param param_adl = {0};
330 struct hashtb_param param_npl = {0};
331
332 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
333
akmhoque28c45022012-08-09 15:38:02 -0500334 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque8c50d0d2012-08-09 13:38:03 -0500335 nlsr->npl = hashtb_create(sizeof(struct name_prefix), &param_npl);
akmhoque59980a52012-08-09 12:36:09 -0500336 nlsr->in_interest.p = &incoming_interest;
337 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -0500338
339 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase *));
340 nlsr->lsdb->version=(char *)malloc(16);
341 nlsr->lsdb->version="0000000000000000";
342
akmhoque59980a52012-08-09 12:36:09 -0500343 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -0500344 nlsr->nlsa_id=0;
akmhoque59980a52012-08-09 12:36:09 -0500345
346 struct ccn_charbuf *router_prefix;
347
348 while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1)
349 {
350 switch (res)
351 {
352 case 'd':
353 daemon_mode = 1;
354 break;
355 case 'f':
356 config_file = optarg;
357 break;
358 case 'h':
359 default:
360 usage(argv[0]);
361 }
362 }
363
364 readConfigFile(config_file);
365
366 nlsr->ccn=ccn_create();
367 if(ccn_connect(nlsr->ccn, NULL) == -1)
368 {
369 fprintf(stderr,"Could not connect to ccnd\n");
370 exit(1);
371 }
372 router_prefix=ccn_charbuf_create();
373 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
374 if(res<0)
375 {
376 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
377 exit(1);
378 }
379
380 ccn_name_append_str(router_prefix,"nlsr");
381 nlsr->in_interest.data=nlsr->router_name;
382 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
383 if ( res < 0 )
384 {
385 fprintf(stderr,"Failed to register interest for router\n");
386 exit(1);
387 }
388
389 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
390
akmhoquec9286692012-08-16 09:57:58 -0500391 nlsr->event_send_lsdb_interest = ccn_schedule_event(nlsr->sched, 1000000, &send_lsdb_interest, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -0500392
akmhoque59980a52012-08-09 12:36:09 -0500393 while(1)
394 {
395 ccn_schedule_run(nlsr->sched);
396 res = ccn_run(nlsr->ccn, 500);
397
398 }
akmhoque59980a52012-08-09 12:36:09 -0500399
akmhoque386081b2012-08-10 10:53:21 -0500400 nlsr_destroy();
akmhoque59980a52012-08-09 12:36:09 -0500401
402 return 0;
403}
404