blob: b3816b5e7fe1c7246b28fea8d0d2c47160db1495 [file] [log] [blame]
akmhoque1771c412012-11-09 13:06:08 -06001#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
akmhoque59980a52012-08-09 12:36:09 -05004#include <unistd.h>
5#include <getopt.h>
6#include <sys/time.h>
akmhoquebfefef22012-09-26 10:09:34 -05007#include <sys/stat.h>
akmhoque59980a52012-08-09 12:36:09 -05008#include <assert.h>
akmhoque1771c412012-11-09 13:06:08 -06009#include <sys/types.h>
10#include <signal.h>
11#include <sys/socket.h>
12#include <sys/un.h>
13#include <fcntl.h>
14#include <sys/ioctl.h>
akmhoque95041802012-11-16 09:18:02 -060015#include <netinet/in.h>
16#include <netdb.h>
17#include <arpa/inet.h>
akmhoque1771c412012-11-09 13:06:08 -060018
akmhoque59980a52012-08-09 12:36:09 -050019#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
akmhoquebfefef22012-09-26 10:09:34 -050022
akmhoque59980a52012-08-09 12:36:09 -050023#include <ccn/ccn.h>
24#include <ccn/uri.h>
25#include <ccn/keystore.h>
26#include <ccn/signing.h>
27#include <ccn/schedule.h>
28#include <ccn/hashtb.h>
akmhoqueb77b95f2013-02-08 12:28:47 -060029#include <ccn/sync.h>
30#include <ccn/seqwriter.h>
akmhoque184dde02013-02-14 15:53:24 -060031#include <ccn/ccn_private.h>
akmhoque59980a52012-08-09 12:36:09 -050032
33#include "nlsr.h"
34#include "nlsr_ndn.h"
akmhoque902d57e2012-08-17 09:24:38 -050035#include "nlsr_lsdb.h"
akmhoque53f64222012-09-05 13:57:51 -050036#include "utility.h"
akmhoque03004e62012-09-06 01:12:28 -050037#include "nlsr_npl.h"
38#include "nlsr_adl.h"
akmhoque3560cb62012-09-09 10:52:30 -050039#include "nlsr_npt.h"
40#include "nlsr_route.h"
akmhoqueb77b95f2013-02-08 12:28:47 -060041#include "nlsr_sync.h"
42#include "nlsr_face.h"
43#include "nlsr_fib.h"
akmhoque3560cb62012-09-09 10:52:30 -050044
45
akmhoque81c25e02012-09-10 14:50:33 -050046#define ON_ERROR_DESTROY(resval) \
47{ \
Obaid Amin2a928a52013-02-20 11:06:51 -060048 if ((resval) < 0) { \
49 nlsr_destroy(); \
50 exit(1);\
51 } \
akmhoque81c25e02012-09-10 14:50:33 -050052}
53
54
55#define ON_ERROR_EXIT(resval) \
56{ \
Obaid Amin2a928a52013-02-20 11:06:51 -060057 if ((resval) < 0) { \
58 exit(1); \
59 } \
akmhoque81c25e02012-09-10 14:50:33 -050060}
akmhoque59980a52012-08-09 12:36:09 -050061
62struct option longopts[] =
63{
Obaid Amin2a928a52013-02-20 11:06:51 -060064 { "daemon", no_argument, NULL, 'd'},
65 { "config_file", required_argument, NULL, 'f'},
66 { "api_port", required_argument, NULL, 'p'},
67 { "help", no_argument, NULL, 'h'},
68 { 0 }
akmhoque59980a52012-08-09 12:36:09 -050069};
70
Obaid Amin2a928a52013-02-20 11:06:51 -060071 static int
akmhoque59980a52012-08-09 12:36:09 -050072usage(char *progname)
73{
74
Obaid Amin2a928a52013-02-20 11:06:51 -060075 printf("Usage: %s [OPTIONS...]\n\
76 NDN routing....\n\
77 -d, --daemon Run in daemon mode\n\
78 -f, --config_file Specify configuration file name\n\
79 -p, --api_port port where api client will connect\n\
80 -h, --help Display this help message\n", progname);
akmhoque59980a52012-08-09 12:36:09 -050081
Obaid Amin2a928a52013-02-20 11:06:51 -060082 exit(1);
akmhoque59980a52012-08-09 12:36:09 -050083}
84
85void ndn_rtr_gettime(const struct ccn_gettime *self, struct ccn_timeval *result)
86{
Obaid Amin2a928a52013-02-20 11:06:51 -060087 struct timeval now = {0};
88 gettimeofday(&now, 0);
89 result->s = now.tv_sec;
90 result->micros = now.tv_usec;
akmhoque59980a52012-08-09 12:36:09 -050091}
92
93static struct ccn_gettime ndn_rtr_ticker = {
Obaid Amin2a928a52013-02-20 11:06:51 -060094 "timer",
95 &ndn_rtr_gettime,
96 1000000,
97 NULL
akmhoque59980a52012-08-09 12:36:09 -050098};
99
Obaid Amin2a928a52013-02-20 11:06:51 -0600100 void
akmhoqueffacaa82012-09-13 17:48:30 -0500101nlsr_lock(void)
102{
103 nlsr->semaphor=NLSR_LOCKED;
104}
105
Obaid Amin2a928a52013-02-20 11:06:51 -0600106 void
akmhoqueffacaa82012-09-13 17:48:30 -0500107nlsr_unlock(void)
108{
109 nlsr->semaphor=NLSR_UNLOCKED;
110}
akmhoque42098b12012-08-27 22:54:23 -0500111
Obaid Amin2a928a52013-02-20 11:06:51 -0600112 void
akmhoque03004e62012-09-06 01:12:28 -0500113nlsr_stop_signal_handler(int sig)
akmhoque42098b12012-08-27 22:54:23 -0500114{
akmhoque03004e62012-09-06 01:12:28 -0500115 signal(sig, SIG_IGN);
Obaid Amin2a928a52013-02-20 11:06:51 -0600116 nlsr_destroy();
akmhoqueffacaa82012-09-13 17:48:30 -0500117 exit(0);
akmhoque59980a52012-08-09 12:36:09 -0500118}
119
Obaid Amin2a928a52013-02-20 11:06:51 -0600120 void
akmhoquebfefef22012-09-26 10:09:34 -0500121daemonize_nlsr(void)
122{
akmhoqueb28579d2013-02-12 11:15:52 -0600123 //int ret;
akmhoquebfefef22012-09-26 10:09:34 -0500124 pid_t process_id = 0;
125 pid_t sid = 0;
126 process_id = fork();
127 if (process_id < 0)
128 {
akmhoque7b791452012-10-30 11:24:56 -0500129 printf("Daemonization failed!\n");
akmhoquebfefef22012-09-26 10:09:34 -0500130 ON_ERROR_DESTROY(process_id);
131 }
132 if (process_id > 0)
133 {
134 printf("Process daemonized. Process id: %d \n", process_id);
akmhoqueb28579d2013-02-12 11:15:52 -0600135 //ret=process_id;
akmhoquebfefef22012-09-26 10:09:34 -0500136 exit(0);
137 }
Obaid Amin2a928a52013-02-20 11:06:51 -0600138
akmhoquebfefef22012-09-26 10:09:34 -0500139 umask(0);
140 sid = setsid();
141 if(sid < 0)
142 {
143 ON_ERROR_DESTROY(sid);
144 }
Obaid Amin2a928a52013-02-20 11:06:51 -0600145
akmhoquebfefef22012-09-26 10:09:34 -0500146 chdir("/");
147 close(STDIN_FILENO);
148 close(STDOUT_FILENO);
149 close(STDERR_FILENO);
150}
151
Obaid Amin2a928a52013-02-20 11:06:51 -0600152 void
akmhoque59980a52012-08-09 12:36:09 -0500153process_command_ccnneighbor(char *command)
154{
155 if(command==NULL)
156 {
akmhoque28c45022012-08-09 15:38:02 -0500157 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500158 return;
159 }
160 char *rem;
161 const char *sep=" \t\n";
akmhoqueb77b95f2013-02-08 12:28:47 -0600162 char *rtr_name;
163 char *nbr_ip_addr;
164 int is_ip_configured=0;
akmhoque7c234e02013-02-13 11:23:56 -0600165 char *ip_addr=(char *)calloc(20,sizeof(char));
akmhoque59980a52012-08-09 12:36:09 -0500166
akmhoque28c45022012-08-09 15:38:02 -0500167 rtr_name=strtok_r(command,sep,&rem);
168 if(rtr_name==NULL)
akmhoque59980a52012-08-09 12:36:09 -0500169 {
akmhoque28c45022012-08-09 15:38:02 -0500170 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500171 return;
172 }
akmhoqued5152122012-09-19 06:44:23 -0500173 if ( rtr_name[strlen(rtr_name)-1] == '/' )
174 {
175 rtr_name[strlen(rtr_name)-1]='\0';
176 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600177
178 if (rem != NULL )
179 {
180 nbr_ip_addr=strtok_r(NULL,sep,&rem);
akmhoque958ccf72013-02-11 10:42:03 -0600181 if ( nbr_ip_addr != NULL)
182 is_ip_configured=1;
akmhoqueb77b95f2013-02-08 12:28:47 -0600183 }
akmhoque7c234e02013-02-13 11:23:56 -0600184 struct name_prefix *nbr=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
185 nbr->name=(char *)calloc(strlen(rtr_name)+1,sizeof(char));
akmhoque03004e62012-09-06 01:12:28 -0500186 memcpy(nbr->name,rtr_name,strlen(rtr_name)+1);
187 nbr->length=strlen(rtr_name)+1;
188
Obaid Amin2a928a52013-02-20 11:06:51 -0600189
akmhoqueb77b95f2013-02-08 12:28:47 -0600190 if ( !is_ip_configured )
191 {
akmhoque7c234e02013-02-13 11:23:56 -0600192 struct name_prefix *nbr_name=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
akmhoqueb77b95f2013-02-08 12:28:47 -0600193 get_host_name_from_command_string(nbr_name,nbr->name,0);
akmhoque018692c2013-02-11 11:33:39 -0600194 if ( nlsr->debugging)
195 printf("Hostname of neighbor: %s ",nbr_name->name);
akmhoqueb77b95f2013-02-08 12:28:47 -0600196 get_ip_from_hostname_02(nbr_name->name,ip_addr);
akmhoque018692c2013-02-11 11:33:39 -0600197 if ( nlsr->debugging)
198 printf("IP Address: %s \n",ip_addr);
akmhoqueb77b95f2013-02-08 12:28:47 -0600199 free(nbr_name->name);
200 free(nbr_name);
201 }
202 else
203 {
akmhoque6682ca32013-02-22 00:29:35 -0600204 memcpy(ip_addr,nbr_ip_addr,strlen(nbr_ip_addr)+1);
akmhoque018692c2013-02-11 11:33:39 -0600205 if (nlsr->debugging)
206 {
207 printf("Name of neighbor: %s ",nbr->name);
208 printf("IP Address: %s \n",ip_addr);
209 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600210 }
211 add_nbr_to_adl(nbr,0,ip_addr);
Obaid Amin2a928a52013-02-20 11:06:51 -0600212
akmhoque03004e62012-09-06 01:12:28 -0500213
214 free(nbr->name);
215 free(nbr);
216}
217
Obaid Amin2a928a52013-02-20 11:06:51 -0600218 void
akmhoque03004e62012-09-06 01:12:28 -0500219process_command_ccnname(char *command)
220{
221
222 if(command==NULL)
223 {
224 printf(" Wrong Command Format ( ccnname /name/prefix)\n");
225 return;
226 }
227 char *rem;
228 const char *sep=" \t\n";
229 char *name;
230 name=strtok_r(command,sep,&rem);
231 if(name==NULL)
232 {
233 printf(" Wrong Command Format ( ccnname /name/prefix/ )\n");
234 return;
235 }
236
237 printf("Name Prefix: %s \n",name);
238
akmhoqued5152122012-09-19 06:44:23 -0500239 if ( name[strlen(name)-1] == '/' )
240 name[strlen(name)-1]='\0';
241
akmhoque7c234e02013-02-13 11:23:56 -0600242 struct name_prefix *np=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
243 np->name=(char *)calloc(strlen(name)+1,sizeof(char));
244 //memset(np->name,0,strlen(name)+1);
akmhoque03004e62012-09-06 01:12:28 -0500245 memcpy(np->name,name,strlen(name)+1);
246 np->length=strlen(name)+1;
akmhoque386081b2012-08-10 10:53:21 -0500247
akmhoque03004e62012-09-06 01:12:28 -0500248 add_name_to_npl(np);
akmhoque28c45022012-08-09 15:38:02 -0500249
akmhoque03004e62012-09-06 01:12:28 -0500250 free(np->name);
akmhoque53f64222012-09-05 13:57:51 -0500251 free(np);
akmhoque59980a52012-08-09 12:36:09 -0500252}
253
akmhoque03004e62012-09-06 01:12:28 -0500254
Obaid Amin2a928a52013-02-20 11:06:51 -0600255 void
akmhoque03004e62012-09-06 01:12:28 -0500256process_command_router_name(char *command)
257{
258 if(command==NULL)
259 {
260 printf(" Wrong Command Format ( router-name /router/name )\n");
261 return;
262 }
263 char *rem;
264 const char *sep=" \t\n";
265 char *rtr_name;
266
267 rtr_name=strtok_r(command,sep,&rem);
268 if(rtr_name==NULL)
269 {
270 printf(" Wrong Command Format ( router-name /router/name )\n");
271 return;
272 }
Obaid Amin2a928a52013-02-20 11:06:51 -0600273
akmhoque03004e62012-09-06 01:12:28 -0500274
akmhoqued5152122012-09-19 06:44:23 -0500275 if ( rtr_name[strlen(rtr_name)-1] == '/' )
276 rtr_name[strlen(rtr_name)-1]='\0';
277
akmhoque7c234e02013-02-13 11:23:56 -0600278 nlsr->router_name=(char *)calloc(strlen(rtr_name)+1,sizeof(char));
279 //memset(nlsr->router_name,0,strlen(rtr_name)+1);
akmhoque03004e62012-09-06 01:12:28 -0500280 memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1);
281
282
283}
284
akmhoqueb77b95f2013-02-08 12:28:47 -0600285/*
Obaid Amin2a928a52013-02-20 11:06:51 -0600286 void
287 process_command_lsdb_synch_interval(char *command)
288 {
289 if(command==NULL)
290 {
291 printf(" Wrong Command Format ( lsdb-synch-interval secs )\n");
292 return;
293 }
294 char *rem;
295 const char *sep=" \t\n";
296 char *secs;
297 long int seconds;
akmhoqued79438d2012-08-27 13:31:42 -0500298
Obaid Amin2a928a52013-02-20 11:06:51 -0600299 secs=strtok_r(command,sep,&rem);
300 if(secs==NULL)
301 {
302 printf(" Wrong Command Format ( lsdb-synch-interval secs)\n");
303 return;
304 }
akmhoqued79438d2012-08-27 13:31:42 -0500305
Obaid Amin2a928a52013-02-20 11:06:51 -0600306 seconds=atoi(secs);
307 if ( seconds >= 120 && seconds <= 3600 )
308 {
309 nlsr->lsdb_synch_interval=seconds;
310 }
akmhoqued79438d2012-08-27 13:31:42 -0500311
Obaid Amin2a928a52013-02-20 11:06:51 -0600312 }
313 */
314
315 void
akmhoqued79438d2012-08-27 13:31:42 -0500316process_command_interest_retry(char *command)
317{
318 if(command==NULL)
319 {
320 printf(" Wrong Command Format ( interest-retry number )\n");
321 return;
322 }
323 char *rem;
324 const char *sep=" \t\n";
akmhoqueffacaa82012-09-13 17:48:30 -0500325 char *retry;
326 long int retry_number;
Obaid Amin2a928a52013-02-20 11:06:51 -0600327
akmhoqueffacaa82012-09-13 17:48:30 -0500328 retry=strtok_r(command,sep,&rem);
329 if(retry==NULL)
akmhoqued79438d2012-08-27 13:31:42 -0500330 {
331 printf(" Wrong Command Format ( interest-retry number)\n");
332 return;
333 }
334
akmhoqueffacaa82012-09-13 17:48:30 -0500335 retry_number=atoi(retry);
336 if ( retry_number >= 1 && retry_number<=10 )
337 {
338 nlsr->interest_retry=retry_number;
339 }
akmhoqued79438d2012-08-27 13:31:42 -0500340
341}
342
Obaid Amin2a928a52013-02-20 11:06:51 -0600343 void
akmhoqued79438d2012-08-27 13:31:42 -0500344process_command_interest_resend_time(char *command)
345{
346 if(command==NULL)
347 {
348 printf(" Wrong Command Format ( interest-resend-time secs )\n");
349 return;
350 }
351 char *rem;
352 const char *sep=" \t\n";
353 char *secs;
354 long int seconds;
Obaid Amin2a928a52013-02-20 11:06:51 -0600355
akmhoqued79438d2012-08-27 13:31:42 -0500356 secs=strtok_r(command,sep,&rem);
357 if(secs==NULL)
358 {
359 printf(" Wrong Command Format ( interest-resend-time secs)\n");
360 return;
361 }
362
363 seconds=atoi(secs);
akmhoqueffacaa82012-09-13 17:48:30 -0500364 if ( seconds <= 60 && seconds >= 1 )
365 {
366 nlsr->interest_resend_time=seconds;
367 }
akmhoqued79438d2012-08-27 13:31:42 -0500368}
369
akmhoque03004e62012-09-06 01:12:28 -0500370
Obaid Amin2a928a52013-02-20 11:06:51 -0600371 void
akmhoqued5152122012-09-19 06:44:23 -0500372process_command_lsa_refresh_time(char *command)
373{
374 if(command==NULL)
375 {
376 printf(" Wrong Command Format ( lsa-refresh-time secs )\n");
377 return;
378 }
379 char *rem;
380 const char *sep=" \t\n";
381 char *secs;
382 long int seconds;
Obaid Amin2a928a52013-02-20 11:06:51 -0600383
akmhoqued5152122012-09-19 06:44:23 -0500384 secs=strtok_r(command,sep,&rem);
385 if(secs==NULL)
386 {
387 printf(" Wrong Command Format ( lsa-refresh-time secs)\n");
388 return;
389 }
390
391 seconds=atoi(secs);
akmhoqueb77b95f2013-02-08 12:28:47 -0600392 if ( seconds >= 240)
akmhoqued5152122012-09-19 06:44:23 -0500393 {
394 nlsr->lsa_refresh_time=seconds;
akmhoqueb77b95f2013-02-08 12:28:47 -0600395 if ( nlsr->router_dead_interval < nlsr->lsa_refresh_time * 2 )
396 {
397 nlsr->router_dead_interval=2*nlsr->lsa_refresh_time;
398 }
akmhoqued5152122012-09-19 06:44:23 -0500399 }
400
401}
402
Obaid Amin2a928a52013-02-20 11:06:51 -0600403 void
akmhoqued5152122012-09-19 06:44:23 -0500404process_command_router_dead_interval(char *command)
405{
406 if(command==NULL)
407 {
408 printf(" Wrong Command Format ( router-dead-interval secs )\n");
409 return;
410 }
411 char *rem;
412 const char *sep=" \t\n";
413 char *secs;
414 long int seconds;
Obaid Amin2a928a52013-02-20 11:06:51 -0600415
akmhoqued5152122012-09-19 06:44:23 -0500416 secs=strtok_r(command,sep,&rem);
417 if(secs==NULL)
418 {
419 printf(" Wrong Command Format ( router-dead-interval secs)\n");
420 return;
421 }
422
423 seconds=atoi(secs);
akmhoqueb77b95f2013-02-08 12:28:47 -0600424 if ( seconds >= 480 )
akmhoqued5152122012-09-19 06:44:23 -0500425 {
426 nlsr->router_dead_interval=seconds;
akmhoqueb77b95f2013-02-08 12:28:47 -0600427 if ( nlsr->router_dead_interval < nlsr->lsa_refresh_time * 2 )
428 {
429 nlsr->router_dead_interval=2*nlsr->lsa_refresh_time;
430 }
akmhoqued5152122012-09-19 06:44:23 -0500431 }
432
433}
akmhoque03004e62012-09-06 01:12:28 -0500434
Obaid Amin2a928a52013-02-20 11:06:51 -0600435 void
akmhoqueb77b95f2013-02-08 12:28:47 -0600436process_command_max_faces_per_prefix(char *command)
akmhoque3cced642012-09-24 16:20:20 -0500437{
438 if(command==NULL)
439 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600440 printf(" Wrong Command Format ( max-faces-per-prefix n )\n");
akmhoque3cced642012-09-24 16:20:20 -0500441 return;
442 }
443 char *rem;
444 const char *sep=" \t\n";
445 char *num;
446 long int number;
Obaid Amin2a928a52013-02-20 11:06:51 -0600447
akmhoque3cced642012-09-24 16:20:20 -0500448 num=strtok_r(command,sep,&rem);
449 if(num==NULL)
450 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600451 printf(" Wrong Command Format ( max-faces-per-prefix n)\n");
akmhoque3cced642012-09-24 16:20:20 -0500452 return;
453 }
454
455 number=atoi(num);
456 if ( number >= 0 && number <= 60 )
457 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600458 nlsr->max_faces_per_prefix=number;
akmhoque3cced642012-09-24 16:20:20 -0500459 }
460
461}
462
Obaid Amin2a928a52013-02-20 11:06:51 -0600463 void
akmhoquebfefef22012-09-26 10:09:34 -0500464process_command_logdir(char *command)
465{
466 if(command==NULL)
467 {
468 printf(" Wrong Command Format ( logdir /path/to/logdir )\n");
469 return;
470 }
471 char *rem;
472 const char *sep=" \t\n";
473 char *dir;
474
475 dir=strtok_r(command,sep,&rem);
476 if(dir==NULL)
477 {
478 printf(" Wrong Command Format ( logdir /path/to/logdir/ )\n");
479 return;
480 }
Obaid Amin2a928a52013-02-20 11:06:51 -0600481
akmhoque7c234e02013-02-13 11:23:56 -0600482 nlsr->logDir=(char *)calloc(strlen(dir)+1,sizeof(char));
akmhoque6682ca32013-02-22 00:29:35 -0600483 memcpy(nlsr->logDir,dir,strlen(dir)+1);
akmhoquebfefef22012-09-26 10:09:34 -0500484}
485
Obaid Amin2a928a52013-02-20 11:06:51 -0600486 void
akmhoque7b791452012-10-30 11:24:56 -0500487process_command_detailed_log(char *command)
488{
489 if(command==NULL)
490 {
491 printf(" Wrong Command Format ( detailed-log on/off )\n");
492 return;
493 }
494 char *rem;
495 const char *sep=" \t\n";
496 char *on_off;
497
498 on_off=strtok_r(command,sep,&rem);
499 if(on_off==NULL)
500 {
501 printf(" Wrong Command Format ( detailed-log on/off )\n");
502 return;
503 }
Obaid Amin2a928a52013-02-20 11:06:51 -0600504
akmhoque7b791452012-10-30 11:24:56 -0500505 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0)
506 {
507 nlsr->detailed_logging=1;
508 }
509}
510
Obaid Amin2a928a52013-02-20 11:06:51 -0600511 void
akmhoque7b791452012-10-30 11:24:56 -0500512process_command_debug(char *command)
513{
514 if(command==NULL)
515 {
516 printf(" Wrong Command Format ( debug on/off )\n");
517 return;
518 }
519 char *rem;
520 const char *sep=" \t\n";
521 char *on_off;
522
523 on_off=strtok_r(command,sep,&rem);
524 if(on_off==NULL)
525 {
526 printf(" Wrong Command Format ( debug on/off )\n");
527 return;
528 }
Obaid Amin2a928a52013-02-20 11:06:51 -0600529
akmhoque7b791452012-10-30 11:24:56 -0500530 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
531 {
532 nlsr->debugging=1;
533 }
534}
535
akmhoqueb77b95f2013-02-08 12:28:47 -0600536
Obaid Amin2a928a52013-02-20 11:06:51 -0600537 void
akmhoqueb77b95f2013-02-08 12:28:47 -0600538process_command_topo_prefix(char *command)
539{
540 if(command==NULL)
541 {
542 printf(" Wrong Command Format ( topo-prefix )\n");
543 return;
544 }
545 char *rem;
546 const char *sep=" \t\n";
547 char *topo_prefix;
548
549 topo_prefix=strtok_r(command,sep,&rem);
550 if(topo_prefix==NULL)
551 {
552 printf(" Wrong Command Format ( topo-prefix /name/prefix )\n");
553 return;
554 }
555 else
556 {
557 if( nlsr->topo_prefix != NULL)
558 free(nlsr->topo_prefix);
559 if ( topo_prefix[strlen(topo_prefix)-1] == '/' )
560 topo_prefix[strlen(topo_prefix)-1]='\0';
561
akmhoque7c234e02013-02-13 11:23:56 -0600562 nlsr->topo_prefix=(char *)calloc(strlen(topo_prefix)+1,sizeof(char));
akmhoque6682ca32013-02-22 00:29:35 -0600563 memcpy(nlsr->topo_prefix,topo_prefix,strlen(topo_prefix)+1);
Obaid Amin0e9a3002013-02-20 14:55:37 -0600564 printf ("Topo prefix is: %s", nlsr->topo_prefix);;
akmhoqueb77b95f2013-02-08 12:28:47 -0600565 }
566}
567
568
Obaid Amin2a928a52013-02-20 11:06:51 -0600569 void
akmhoqueb77b95f2013-02-08 12:28:47 -0600570process_command_slice_prefix(char *command)
571{
572 if(command==NULL)
573 {
574 printf(" Wrong Command Format ( slice-prefix /name/prefix )\n");
575 return;
576 }
577 char *rem;
578 const char *sep=" \t\n";
579 char *slice_prefix;
580
581 slice_prefix=strtok_r(command,sep,&rem);
582 if(slice_prefix==NULL)
583 {
584 printf(" Wrong Command Format ( slice-prefix /name/prefix )\n");
585 return;
586 }
587 else
588 {
589 if ( nlsr->slice_prefix != NULL)
590 free(nlsr->slice_prefix);
591 if ( slice_prefix[strlen(slice_prefix)-1] == '/' )
592 slice_prefix[strlen(slice_prefix)-1]='\0';
593
akmhoque7c234e02013-02-13 11:23:56 -0600594 nlsr->slice_prefix=(char *)calloc(strlen(slice_prefix)+1,sizeof(char));
akmhoque6682ca32013-02-22 00:29:35 -0600595 memcpy(nlsr->slice_prefix,slice_prefix,strlen(slice_prefix)+1);
akmhoqueb77b95f2013-02-08 12:28:47 -0600596 }
597}
598
Obaid Amin2a928a52013-02-20 11:06:51 -0600599 void
akmhoqueb77b95f2013-02-08 12:28:47 -0600600process_command_hyperbolic_routing(char *command)
601{
602 if(command==NULL)
603 {
604 printf(" Wrong Command Format ( hyperbolic-routing on)\n");
605 return;
606 }
607 char *rem;
608 const char *sep=" \t\n";
609 char *on_off;
610
611 on_off=strtok_r(command,sep,&rem);
612 if(on_off==NULL)
613 {
614 printf(" Wrong Command Format ( hyperbolic-routing on )\n");
615 return;
616 }
Obaid Amin2a928a52013-02-20 11:06:51 -0600617
akmhoqueb77b95f2013-02-08 12:28:47 -0600618 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
619 {
620 nlsr->is_hyperbolic_calc=1;
621 }
622}
623
Obaid Amin2a928a52013-02-20 11:06:51 -0600624 void
akmhoqueb77b95f2013-02-08 12:28:47 -0600625process_command_hyperbolic_cordinate(char *command)
626{
627 if(command==NULL)
628 {
629 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
630 return;
631 }
632
633 char *rem;
634 const char *sep=" \t\n\r";
635 char *radious;
636 char *theta;
637
638 radious=strtok_r(command,sep,&rem);
639 if (radious == NULL )
640 {
641 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
642 return;
643 }
644
645 theta=strtok_r(NULL,sep,&rem);
646 if (theta == NULL )
647 {
648 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
649 return;
650 }
651
652 nlsr->cor_r=strtof(radious,NULL);
653 nlsr->cor_theta=strtof(theta,NULL);
654
655}
656
Obaid Amin2a928a52013-02-20 11:06:51 -0600657 void
akmhoqueb77b95f2013-02-08 12:28:47 -0600658process_command_tunnel_type(char *command)
659{
660 if(command==NULL)
661 {
662 printf(" Wrong Command Format ( tunnel-type udp/tcp)\n");
663 return;
664 }
665 char *rem;
666 const char *sep=" \t\n";
667 char *on_off;
668
669 on_off=strtok_r(command,sep,&rem);
670 if(on_off==NULL)
671 {
672 printf(" Wrong Command Format ( tunnel-type udp/tcp )\n");
673 return;
674 }
Obaid Amin2a928a52013-02-20 11:06:51 -0600675
akmhoqueb77b95f2013-02-08 12:28:47 -0600676 if ( strcmp(on_off,"TCP") == 0 || strcmp(on_off,"tcp") == 0 )
677 {
678 nlsr->tunnel_type=IPPROTO_TCP;
679 }
680 else if ( strcmp(on_off,"UDP") == 0 || strcmp(on_off,"udp") == 0 )
681 {
682 nlsr->tunnel_type=IPPROTO_UDP;
683 }
684}
685
Obaid Amin2a928a52013-02-20 11:06:51 -0600686 void
akmhoque59980a52012-08-09 12:36:09 -0500687process_conf_command(char *command)
688{
689 const char *separators=" \t\n";
690 char *remainder=NULL;
691 char *cmd_type=NULL;
692
693 if(command==NULL || strlen(command)==0 || command[0]=='!')
694 return;
695
696 cmd_type=strtok_r(command,separators,&remainder);
697
698 if(!strcmp(cmd_type,"router-name") )
699 {
700 process_command_router_name(remainder);
701 }
702 else if(!strcmp(cmd_type,"ccnneighbor") )
703 {
704 process_command_ccnneighbor(remainder);
705 }
706 else if(!strcmp(cmd_type,"ccnname") )
707 {
708 process_command_ccnname(remainder);
709 }
akmhoqued79438d2012-08-27 13:31:42 -0500710 else if(!strcmp(cmd_type,"interest-retry") )
711 {
712 process_command_interest_retry(remainder);
713 }
714 else if(!strcmp(cmd_type,"interest-resend-time") )
715 {
716 process_command_interest_resend_time(remainder);
717 }
akmhoqued5152122012-09-19 06:44:23 -0500718 else if(!strcmp(cmd_type,"lsa-refresh-time") )
719 {
720 process_command_lsa_refresh_time(remainder);
721 }
722 else if(!strcmp(cmd_type,"router-dead-interval") )
723 {
724 process_command_router_dead_interval(remainder);
725 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600726 else if(!strcmp(cmd_type,"max-faces-per-prefix") )
akmhoque3cced642012-09-24 16:20:20 -0500727 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600728 process_command_max_faces_per_prefix(remainder);
akmhoque3cced642012-09-24 16:20:20 -0500729 }
akmhoquebfefef22012-09-26 10:09:34 -0500730 else if(!strcmp(cmd_type,"logdir") )
731 {
Obaid Amin2a928a52013-02-20 11:06:51 -0600732 process_command_logdir(remainder);
akmhoquebfefef22012-09-26 10:09:34 -0500733 }
akmhoque7b791452012-10-30 11:24:56 -0500734 else if(!strcmp(cmd_type,"detailed-log") )
735 {
Obaid Amin2a928a52013-02-20 11:06:51 -0600736 process_command_detailed_log(remainder);
akmhoque7b791452012-10-30 11:24:56 -0500737 }
738 else if(!strcmp(cmd_type,"debug") )
739 {
Obaid Amin2a928a52013-02-20 11:06:51 -0600740 process_command_debug(remainder);
akmhoque7b791452012-10-30 11:24:56 -0500741 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600742 else if(!strcmp(cmd_type,"topo-prefix") )
743 {
Obaid Amin2a928a52013-02-20 11:06:51 -0600744 process_command_topo_prefix(remainder);
akmhoqueb77b95f2013-02-08 12:28:47 -0600745 }
746 else if(!strcmp(cmd_type,"slice-prefix") )
747 {
Obaid Amin2a928a52013-02-20 11:06:51 -0600748 process_command_slice_prefix(remainder);
akmhoqueb77b95f2013-02-08 12:28:47 -0600749 }
750 else if(!strcmp(cmd_type,"hyperbolic-cordinate") )
751 {
752 process_command_hyperbolic_cordinate(remainder);
753 }
754 else if(!strcmp(cmd_type,"hyperbolic-routing") )
755 {
756 process_command_hyperbolic_routing(remainder);
757 }
758 else if(!strcmp(cmd_type,"tunnel-type") )
759 {
760 process_command_tunnel_type(remainder);
761 }
akmhoqued5152122012-09-19 06:44:23 -0500762 else
akmhoque59980a52012-08-09 12:36:09 -0500763 {
764 printf("Wrong configuration Command %s \n",cmd_type);
765 }
766}
767
akmhoque03004e62012-09-06 01:12:28 -0500768
Obaid Amin0e9a3002013-02-20 14:55:37 -0600769 int
akmhoque59980a52012-08-09 12:36:09 -0500770readConfigFile(const char *filename)
771{
772 FILE *cfg;
773 char buf[1024];
774 int len;
775
776 cfg=fopen(filename, "r");
777
778 if(cfg == NULL)
779 {
780 printf("\nConfiguration File does not exists\n");
Obaid Amin0e9a3002013-02-20 14:55:37 -0600781 return -1;
akmhoque59980a52012-08-09 12:36:09 -0500782 }
783
784 while(fgets((char *)buf, sizeof(buf), cfg))
785 {
786 len=strlen(buf);
787 if(buf[len-1] == '\n')
Obaid Amin2a928a52013-02-20 11:06:51 -0600788 buf[len-1]='\0';
akmhoqued5152122012-09-19 06:44:23 -0500789 if ( buf[0] != '#' && buf[0] != '!')
790 process_conf_command(buf);
akmhoque59980a52012-08-09 12:36:09 -0500791 }
792
793 fclose(cfg);
794
795 return 0;
796}
797
akmhoqueb77b95f2013-02-08 12:28:47 -0600798
Obaid Amin0e9a3002013-02-20 14:55:37 -0600799 void
akmhoqueb77b95f2013-02-08 12:28:47 -0600800add_faces_for_nbrs(void)
801{
802 int i, adl_element;
803 struct ndn_neighbor *nbr;
804
805 struct hashtb_enumerator ee;
Obaid Amin2a928a52013-02-20 11:06:51 -0600806 struct hashtb_enumerator *e = &ee;
807
808 hashtb_start(nlsr->adl, e);
akmhoqueb77b95f2013-02-08 12:28:47 -0600809 adl_element=hashtb_n(nlsr->adl);
810
811 for(i=0;i<adl_element;i++)
812 {
813 nbr=e->data;
Obaid Amin2a928a52013-02-20 11:06:51 -0600814 int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr->neighbor->name,
Obaid Amin0e9a3002013-02-20 14:55:37 -0600815 (const char *)nbr->ip_address, 9695,nlsr->tunnel_type);
akmhoqueb77b95f2013-02-08 12:28:47 -0600816 update_face_to_adl_for_nbr(nbr->neighbor->name, face_id);
Obaid Amin2a928a52013-02-20 11:06:51 -0600817 add_delete_ccn_face_by_face_id(nlsr->ccn,
818 (const char *)nlsr->topo_prefix, OP_REG, face_id);
819 add_delete_ccn_face_by_face_id(nlsr->ccn,
820 (const char *)nlsr->slice_prefix, OP_REG, face_id);
akmhoqueb77b95f2013-02-08 12:28:47 -0600821 hashtb_next(e);
822 }
823
824 hashtb_end(e);
825
826}
827
Obaid Amin0e9a3002013-02-20 14:55:37 -0600828 void
akmhoqueb77b95f2013-02-08 12:28:47 -0600829destroy_faces_for_nbrs(void)
830{
831 int i, adl_element;
832 struct ndn_neighbor *nbr;
833
834 struct hashtb_enumerator ee;
Obaid Amin2a928a52013-02-20 11:06:51 -0600835 struct hashtb_enumerator *e = &ee;
836
837 hashtb_start(nlsr->adl, e);
akmhoqueb77b95f2013-02-08 12:28:47 -0600838 adl_element=hashtb_n(nlsr->adl);
839
840 for(i=0;i<adl_element;i++)
841 {
842 nbr=e->data;
843 if ( nbr->face > 0 )
844 {
Obaid Amin2a928a52013-02-20 11:06:51 -0600845 add_delete_ccn_face_by_face_id(nlsr->ccn,
846 (const char *)nlsr->topo_prefix, OP_UNREG, nbr->face);
847 add_delete_ccn_face_by_face_id(nlsr->ccn,
848 (const char *)nbr->neighbor->name,OP_UNREG,nbr->face);
849 add_delete_ccn_face_by_face_id(nlsr->ccn,
850 (const char *)nlsr->slice_prefix, OP_UNREG, nbr->face);
akmhoqueb77b95f2013-02-08 12:28:47 -0600851 }
852 hashtb_next(e);
853 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600854 hashtb_end(e);
akmhoqueb77b95f2013-02-08 12:28:47 -0600855}
856
Obaid Amin0e9a3002013-02-20 14:55:37 -0600857 char *
akmhoque562caef2012-11-09 13:29:06 -0600858process_api_client_command(char *command)
859{
860 char *msg;
861 msg=(char *)malloc(100);
akmhoqueb77b95f2013-02-08 12:28:47 -0600862 memset(msg,0,100);
Obaid Amin2a928a52013-02-20 11:06:51 -0600863
akmhoque3171d652012-11-13 11:44:33 -0600864 const char *sep=" \t\n";
865 char *rem=NULL;
866 char *cmd_type=NULL;
867 char *op_type=NULL;
868 char *name=NULL;
869 char *face=NULL;
870 int face_id;
871 int res;
872
873 op_type=strtok_r(command,sep,&rem);
874 cmd_type=strtok_r(NULL,sep,&rem);
875 name=strtok_r(NULL,sep,&rem);
876 if ( name[strlen(name)-1] == '/' )
877 name[strlen(name)-1]='\0';
878
Obaid Amin2a928a52013-02-20 11:06:51 -0600879 struct name_prefix *np=(struct name_prefix *) calloc (1,
Obaid Amin0e9a3002013-02-20 14:55:37 -0600880 sizeof(struct name_prefix ));
Obaid Amin2a928a52013-02-20 11:06:51 -0600881 np->name = (char *) calloc (strlen(name)+1,sizeof(char));
akmhoque3171d652012-11-13 11:44:33 -0600882 memcpy(np->name,name,strlen(name)+1);
883 np->length=strlen(name)+1;
884
885 if ( strcmp(cmd_type,"name")!= 0 )
886 {
887 face=strtok_r(NULL,sep,&rem);
888 sscanf(face,"face%d",&face_id);
889 }
Obaid Amin2a928a52013-02-20 11:06:51 -0600890
akmhoque3171d652012-11-13 11:44:33 -0600891 if ( strcmp(cmd_type,"name")== 0 )
892 {
893 if ( strcmp(op_type,"del") == 0 )
894 {
895 res=does_name_exist_in_npl(np);
896 if ( res == 0)
897 {
898 sprintf(msg,"Name %s does not exist !!",name);
899 }
900 else
901 {
902 long int ls_id=get_lsa_id_from_npl(np);
903 if ( ls_id != 0 )
904 {
905 make_name_lsa_invalid(np,LS_TYPE_NAME,ls_id);
906 sprintf(msg,"Name %s has been deleted and Advertised.",name);
907 }
908 else
909 {
910 sprintf(msg,"Name %s does not have an Name LSA yet !!",name);
911 }
912 }
913 }
914 else if ( strcmp(op_type,"add") == 0 )
915 {
916 res=does_name_exist_in_npl(np);
917 if ( res == 0)
918 {
919 add_name_to_npl(np);
920 build_and_install_single_name_lsa(np);
921 sprintf(msg,"Name %s has been added to advertise.",name);
922 }
923 else
924 {
925 sprintf(msg,"Name %s has already been advertised from this router !!",name);
926 }
927 }
928 }
929 else if ( strcmp(cmd_type,"neighbor") == 0 )
930 {
931 if ( strcmp(op_type,"del") == 0 )
932 {
933 res=is_neighbor(np->name);
934 if ( res == 0)
935 {
936 sprintf(msg,"Neighbor %s does not exist !!",name);
937 }
938 else
939 {
940 update_adjacent_status_to_adl(np,NBR_DOWN);
akmhoqueb77b95f2013-02-08 12:28:47 -0600941 int face_id=get_next_hop_face_from_adl(np->name);
942 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)np->name, OP_UNREG, face_id);
943 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_UNREG, face_id);
944 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_UNREG, face_id);
akmhoque3171d652012-11-13 11:44:33 -0600945 delete_nbr_from_adl(np);
946 if(!nlsr->is_build_adj_lsa_sheduled)
947 {
948 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
949 nlsr->is_build_adj_lsa_sheduled=1;
950 }
951 sprintf(msg,"Neighbor %s has been deleted from adjacency list.",name);
952 }
953 }
954 else if ( strcmp(op_type,"add") == 0 )
955 {
956 res=is_neighbor(np->name);
957 if ( res == 0 )
958 {
akmhoque7c234e02013-02-13 11:23:56 -0600959 struct name_prefix *nbr_name=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
akmhoqueb77b95f2013-02-08 12:28:47 -0600960 get_host_name_from_command_string(nbr_name,np->name,0);
961 printf("Hostname of neighbor: %s ",nbr_name->name);
962
akmhoque7c234e02013-02-13 11:23:56 -0600963 char *ip_addr=(char *)calloc(20,sizeof(char));
964 //memset(ip_addr,0,20);
akmhoqueb77b95f2013-02-08 12:28:47 -0600965 get_ip_from_hostname_02(nbr_name->name,ip_addr);
966 printf("IP Address: %s \n",ip_addr);
967 int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr_name->name, (const char *)ip_addr, 9695,nlsr->tunnel_type);
968 update_face_to_adl_for_nbr(nbr_name->name, face_id);
969 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_REG, face_id);
970 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_REG, face_id);
971
972 add_nbr_to_adl(np,face_id,ip_addr);
973
akmhoque3171d652012-11-13 11:44:33 -0600974 sprintf(msg,"Neighbor %s has been added to adjacency list.",name);
akmhoque7c234e02013-02-13 11:23:56 -0600975 free(ip_addr);
akmhoqueb77b95f2013-02-08 12:28:47 -0600976
akmhoque3171d652012-11-13 11:44:33 -0600977 }
978 else
979 {
980 sprintf(msg,"Neighbor %s already exists in adjacency list.",name);
981 }
982 }
983 }
Obaid Amin2a928a52013-02-20 11:06:51 -0600984
akmhoque562caef2012-11-09 13:29:06 -0600985
986 return msg;
987}
akmhoque1771c412012-11-09 13:06:08 -0600988
Obaid Amin2a928a52013-02-20 11:06:51 -0600989 int
akmhoque1771c412012-11-09 13:06:08 -0600990nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd)
991{
992 struct timeval timeout;
akmhoqueb77b95f2013-02-08 12:28:47 -0600993 if (time_out_micro_sec< 500000 && time_out_micro_sec> 0 )
akmhoque1771c412012-11-09 13:06:08 -0600994 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600995 timeout.tv_sec=0;
996 timeout.tv_usec=time_out_micro_sec;
akmhoque1771c412012-11-09 13:06:08 -0600997 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600998 else
akmhoque1771c412012-11-09 13:06:08 -0600999 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001000 timeout.tv_sec = 0;
1001 timeout.tv_usec = 500000;
akmhoque1771c412012-11-09 13:06:08 -06001002 }
Obaid Amin2a928a52013-02-20 11:06:51 -06001003
akmhoque1771c412012-11-09 13:06:08 -06001004 int fd;
1005 int nread;
1006 int result;
1007 fd_set testfds;
1008 unsigned int client_len;
1009 int client_sockfd;
1010 char recv_buffer[1024];
1011 bzero(recv_buffer,1024);
akmhoque95041802012-11-16 09:18:02 -06001012 struct sockaddr_in client_address;
akmhoque1771c412012-11-09 13:06:08 -06001013
1014 testfds=nlsr->readfds;
1015 result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
Obaid Amin2a928a52013-02-20 11:06:51 -06001016
akmhoqueb77b95f2013-02-08 12:28:47 -06001017 for(fd = 0; fd < FD_SETSIZE && result > 0; fd++)
akmhoque1771c412012-11-09 13:06:08 -06001018 {
1019 if(FD_ISSET(fd,&testfds))
1020 {
1021 if ( fd == ccn_fd )
1022 {
1023 return 0;
1024 }
1025 else if(fd == nlsr->nlsr_api_server_sock_fd)
1026 {
1027 client_len = sizeof(client_address);
1028 client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len);
1029 FD_SET(client_sockfd, &nlsr->readfds);
1030 }
1031 else
1032 {
Obaid Amin2a928a52013-02-20 11:06:51 -06001033
akmhoque1771c412012-11-09 13:06:08 -06001034 ioctl(fd, FIONREAD, &nread);
1035 if(nread == 0)
1036 {
1037 close(fd);
1038 FD_CLR(fd, &nlsr->readfds);
1039 }
1040 else
1041 {
1042 recv(fd, recv_buffer, 1024, 0);
akmhoqueb77b95f2013-02-08 12:28:47 -06001043 printf("Received Data from NLSR API cleint: %s \n",recv_buffer);
akmhoque562caef2012-11-09 13:29:06 -06001044 char *msg=process_api_client_command(recv_buffer);
1045 send(fd, msg, strlen(msg),0);
1046 free(msg);
akmhoque1771c412012-11-09 13:06:08 -06001047 close(fd);
1048 FD_CLR(fd, &nlsr->readfds);
akmhoque1771c412012-11-09 13:06:08 -06001049 }
1050 }
1051 }
1052 }
1053
1054 return 0;
1055}
1056
Obaid Amin2a928a52013-02-20 11:06:51 -06001057 int
akmhoqueb77b95f2013-02-08 12:28:47 -06001058check_config_validity()
1059{
1060 if (nlsr->router_name == NULL )
1061 {
1062 fprintf(stderr,"Router name has not been configured :(\n");
1063 return -1;
1064 }
1065 if ( nlsr->is_hyperbolic_calc == 1 && (nlsr->cor_r == -1.0 && nlsr->cor_theta== -1.0) )
1066 {
1067 fprintf(stderr,"Hyperbolic codinate has not been defined :(\n");
1068 return -1;
1069 }
Obaid Amin2a928a52013-02-20 11:06:51 -06001070
akmhoqueb77b95f2013-02-08 12:28:47 -06001071 return 0;
1072}
1073
Obaid Amin2a928a52013-02-20 11:06:51 -06001074 void
akmhoque386081b2012-08-10 10:53:21 -05001075nlsr_destroy( void )
1076{
akmhoque7b791452012-10-30 11:24:56 -05001077 if ( nlsr->debugging )
1078 {
1079 printf("Freeing Allocated Memory....\n");
1080 }
akmhoque9e9fc722012-09-26 14:03:25 -05001081 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n");
akmhoquefbfd0982012-09-09 20:59:03 -05001082 /* Destroying all face created by nlsr in CCND */
1083 destroy_all_face_by_nlsr();
akmhoqueb77b95f2013-02-08 12:28:47 -06001084 destroy_faces_for_nbrs();
Obaid Amin0e9a3002013-02-20 14:55:37 -06001085
akmhoque386081b2012-08-10 10:53:21 -05001086 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
akmhoque7c234e02013-02-13 11:23:56 -06001087 hashtb_destroy(&nlsr->adl);
1088 hashtb_destroy(&nlsr->npl);
1089 hashtb_destroy(&nlsr->pit_alsa);
akmhoque03004e62012-09-06 01:12:28 -05001090 hashtb_destroy(&nlsr->lsdb->name_lsdb);
1091 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
akmhoque7c234e02013-02-13 11:23:56 -06001092 hashtb_destroy(&nlsr->lsdb->cor_lsdb);
akmhoque3cced642012-09-24 16:20:20 -05001093
akmhoque7c234e02013-02-13 11:23:56 -06001094 int i, npt_element,rt_element;
akmhoque3560cb62012-09-09 10:52:30 -05001095 struct npt_entry *ne;
1096 struct hashtb_enumerator ee;
Obaid Amin2a928a52013-02-20 11:06:51 -06001097 struct hashtb_enumerator *e = &ee;
1098 hashtb_start(nlsr->npt, e);
akmhoque3560cb62012-09-09 10:52:30 -05001099 npt_element=hashtb_n(nlsr->npt);
1100 for(i=0;i<npt_element;i++)
1101 {
1102 ne=e->data;
akmhoque3cced642012-09-24 16:20:20 -05001103 hashtb_destroy(&ne->name_list);
1104 hashtb_destroy(&ne->face_list);
akmhoque3560cb62012-09-09 10:52:30 -05001105 hashtb_next(e);
1106 }
1107
1108 hashtb_end(e);
1109 hashtb_destroy(&nlsr->npt);
Obaid Amin2a928a52013-02-20 11:06:51 -06001110
akmhoque7c234e02013-02-13 11:23:56 -06001111 struct routing_table_entry *rte;
1112 hashtb_start(nlsr->routing_table, e);
1113 rt_element=hashtb_n(nlsr->routing_table);
1114 for(i=0;i<rt_element;i++)
1115 {
1116 rte=e->data;
1117 hashtb_destroy(&rte->face_list);
1118 hashtb_next(e);
1119 }
1120 hashtb_end(e);
1121 hashtb_destroy(&nlsr->routing_table);
Obaid Amin2a928a52013-02-20 11:06:51 -06001122
akmhoque7c234e02013-02-13 11:23:56 -06001123 if ( nlsr->ccns != NULL )
1124 ccns_close(&nlsr->ccns, NULL, NULL);
1125 if ( nlsr->slice != NULL )
1126 ccns_slice_destroy(&nlsr->slice);
akmhoque3560cb62012-09-09 10:52:30 -05001127
akmhoque7c234e02013-02-13 11:23:56 -06001128 close(nlsr->nlsr_api_server_sock_fd);
akmhoque95041802012-11-16 09:18:02 -06001129
akmhoque866c2222013-02-12 10:49:33 -06001130 ccn_destroy(&nlsr->ccn);
akmhoque03004e62012-09-06 01:12:28 -05001131 free(nlsr->lsdb->lsdb_version);
1132 free(nlsr->lsdb);
1133 free(nlsr->router_name);
akmhoque7b791452012-10-30 11:24:56 -05001134 if ( nlsr->debugging )
1135 {
1136 printf("Finished freeing allocated memory\n");
1137 }
akmhoque9e9fc722012-09-26 14:03:25 -05001138 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n");
Obaid Amin2a928a52013-02-20 11:06:51 -06001139
akmhoque7c234e02013-02-13 11:23:56 -06001140 free(nlsr);
akmhoque53f64222012-09-05 13:57:51 -05001141
akmhoque386081b2012-08-10 10:53:21 -05001142}
1143
akmhoque03004e62012-09-06 01:12:28 -05001144
akmhoqueb77b95f2013-02-08 12:28:47 -06001145
Obaid Amin0e9a3002013-02-20 14:55:37 -06001146 void
akmhoque1771c412012-11-09 13:06:08 -06001147init_api_server(int ccn_fd)
1148{
1149 int server_sockfd;
1150 int server_len;
akmhoque95041802012-11-16 09:18:02 -06001151 struct sockaddr_in server_address;
akmhoquef31f13b2012-11-16 09:42:24 -06001152 unsigned int yes=1;
1153
akmhoque95041802012-11-16 09:18:02 -06001154 server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
akmhoque1771c412012-11-09 13:06:08 -06001155
1156 int flags = fcntl(server_sockfd, F_GETFL, 0);
1157 fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
1158
akmhoquef31f13b2012-11-16 09:42:24 -06001159 if (setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0)
1160 {
Obaid Amin2a928a52013-02-20 11:06:51 -06001161 ON_ERROR_DESTROY(-1);
1162 }
akmhoque95041802012-11-16 09:18:02 -06001163
1164 server_address.sin_family = AF_INET;
Adam Alyyanb5fff372013-01-09 14:32:52 -06001165 server_address.sin_addr.s_addr = INADDR_ANY;
1166 server_address.sin_port = htons(nlsr->api_port);
akmhoque95041802012-11-16 09:18:02 -06001167
akmhoque1771c412012-11-09 13:06:08 -06001168 server_len = sizeof(server_address);
1169 bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
1170 listen(server_sockfd, 100);
1171 FD_ZERO(&nlsr->readfds);
1172 FD_SET(server_sockfd, &nlsr->readfds);
1173 FD_SET(ccn_fd, &nlsr->readfds);
1174 nlsr->nlsr_api_server_sock_fd=server_sockfd;
akmhoque1771c412012-11-09 13:06:08 -06001175}
1176
Obaid Amin0e9a3002013-02-20 14:55:37 -06001177 int
akmhoque902d57e2012-08-17 09:24:38 -05001178init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -05001179{
akmhoque03004e62012-09-06 01:12:28 -05001180 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
1181 {
1182 perror("SIGQUIT install error\n");
akmhoque81c25e02012-09-10 14:50:33 -05001183 return -1;
akmhoque03004e62012-09-06 01:12:28 -05001184 }
1185 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
1186 {
1187 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -05001188 return -1;
Obaid Amin2a928a52013-02-20 11:06:51 -06001189 }
1190 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
akmhoque03004e62012-09-06 01:12:28 -05001191 {
1192 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -05001193 return -1;
akmhoque03004e62012-09-06 01:12:28 -05001194 }
akmhoque902d57e2012-08-17 09:24:38 -05001195
akmhoque7c234e02013-02-13 11:23:56 -06001196 nlsr=(struct nlsr *)calloc(1,sizeof(struct nlsr));
Obaid Amin2a928a52013-02-20 11:06:51 -06001197
1198 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), NULL);
1199 nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), NULL);
1200 nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), NULL);
1201 nlsr->npt = hashtb_create(sizeof(struct npt_entry), NULL);
1202 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), NULL);
akmhoque29c1db52012-09-07 14:47:43 -05001203
akmhoque59980a52012-08-09 12:36:09 -05001204 nlsr->in_interest.p = &incoming_interest;
1205 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -05001206
akmhoque03004e62012-09-06 01:12:28 -05001207 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
akmhoque07dd8cc2012-08-16 10:23:01 -05001208
Obaid Amin0e9a3002013-02-20 14:55:37 -06001209 char *time_stamp=(char *) calloc (20,sizeof(char));
akmhoque03004e62012-09-06 01:12:28 -05001210 get_current_timestamp_micro(time_stamp);
1211 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
akmhoqueb77b95f2013-02-08 12:28:47 -06001212 memset(nlsr->lsdb->lsdb_version,0,strlen(time_stamp));
akmhoque03004e62012-09-06 01:12:28 -05001213 free(time_stamp);
akmhoque902d57e2012-08-17 09:24:38 -05001214
Obaid Amin2a928a52013-02-20 11:06:51 -06001215 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), NULL);
1216 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), NULL);
1217 nlsr->lsdb->cor_lsdb = hashtb_create(sizeof(struct clsa), NULL);
akmhoque53f64222012-09-05 13:57:51 -05001218
akmhoque59980a52012-08-09 12:36:09 -05001219 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -05001220 nlsr->nlsa_id=0;
akmhoqued79438d2012-08-27 13:31:42 -05001221 nlsr->adj_build_flag=0;
akmhoque53f64222012-09-05 13:57:51 -05001222 nlsr->adj_build_count=0;
1223 nlsr->is_build_adj_lsa_sheduled=0;
akmhoque29c1db52012-09-07 14:47:43 -05001224 nlsr->is_send_lsdb_interest_scheduled=0;
1225 nlsr->is_route_calculation_scheduled=0;
akmhoqued79438d2012-08-27 13:31:42 -05001226
akmhoque7b791452012-10-30 11:24:56 -05001227 nlsr->detailed_logging=0;
1228 nlsr->debugging=0;
1229
akmhoqueb77b95f2013-02-08 12:28:47 -06001230 //nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
akmhoqued79438d2012-08-27 13:31:42 -05001231 nlsr->interest_retry = INTEREST_RETRY;
1232 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
akmhoqueda5b6832012-09-13 22:33:55 -05001233 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
1234 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
akmhoqueb77b95f2013-02-08 12:28:47 -06001235 nlsr->max_faces_per_prefix=MAX_FACES_PER_PREFIX;
akmhoqueffacaa82012-09-13 17:48:30 -05001236 nlsr->semaphor=NLSR_UNLOCKED;
akmhoque81c25e02012-09-10 14:50:33 -05001237
akmhoque95041802012-11-16 09:18:02 -06001238 nlsr->api_port=API_PORT;
1239
akmhoque7c234e02013-02-13 11:23:56 -06001240 nlsr->topo_prefix=(char *)calloc(strlen("/ndn/routing/nlsr")+1,sizeof(char));
akmhoqueb77b95f2013-02-08 12:28:47 -06001241 memcpy(nlsr->topo_prefix,"/ndn/routing/nlsr",strlen("/ndn/routing/nlsr"));
1242
akmhoque7c234e02013-02-13 11:23:56 -06001243 nlsr->slice_prefix=(char *)calloc(strlen("/ndn/routing/nlsr/LSA")+1,sizeof(char));
akmhoqueb77b95f2013-02-08 12:28:47 -06001244 memcpy(nlsr->slice_prefix,"/ndn/routing/nlsr/LSA",strlen("/ndn/routing/nlsr/LSA"));
1245
1246 nlsr->is_hyperbolic_calc=0;
1247 nlsr->cor_r=-1.0;
1248 nlsr->cor_theta=-1.0;
1249
1250 nlsr->tunnel_type=IPPROTO_UDP;
1251
akmhoque81c25e02012-09-10 14:50:33 -05001252 return 0;
akmhoque902d57e2012-08-17 09:24:38 -05001253}
1254
Obaid Amin0e9a3002013-02-20 14:55:37 -06001255 int
akmhoque902d57e2012-08-17 09:24:38 -05001256main(int argc, char *argv[])
1257{
Obaid Amin2a928a52013-02-20 11:06:51 -06001258 int res, ret;
1259 char *config_file;
akmhoquebfefef22012-09-26 10:09:34 -05001260 int daemon_mode=0;
akmhoque95041802012-11-16 09:18:02 -06001261 int port=0;
akmhoque902d57e2012-08-17 09:24:38 -05001262
akmhoque95041802012-11-16 09:18:02 -06001263 while ((res = getopt_long(argc, argv, "df:p:h", longopts, 0)) != -1)
akmhoque59980a52012-08-09 12:36:09 -05001264 {
Obaid Amin2a928a52013-02-20 11:06:51 -06001265 switch (res)
akmhoque59980a52012-08-09 12:36:09 -05001266 {
1267 case 'd':
akmhoquebfefef22012-09-26 10:09:34 -05001268 daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -05001269 break;
1270 case 'f':
1271 config_file = optarg;
1272 break;
akmhoque95041802012-11-16 09:18:02 -06001273 case 'p':
1274 port = atoi(optarg);
1275 break;
akmhoque59980a52012-08-09 12:36:09 -05001276 case 'h':
1277 default:
1278 usage(argv[0]);
1279 }
Obaid Amin2a928a52013-02-20 11:06:51 -06001280 }
akmhoque59980a52012-08-09 12:36:09 -05001281
Obaid Amin2a928a52013-02-20 11:06:51 -06001282 ret = init_nlsr();
1283 ON_ERROR_EXIT(ret);
akmhoque95041802012-11-16 09:18:02 -06001284
1285 if ( port !=0 )
1286 nlsr->api_port=port;
1287
Obaid Amin0e9a3002013-02-20 14:55:37 -06001288 ON_ERROR_DESTROY(readConfigFile(config_file));
akmhoqueb77b95f2013-02-08 12:28:47 -06001289
1290 ON_ERROR_DESTROY(check_config_validity());
1291
1292 print_adjacent_from_adl();
1293
akmhoquebfefef22012-09-26 10:09:34 -05001294 if ( daemon_mode == 1 )
1295 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001296 nlsr->debugging=0;
akmhoquebfefef22012-09-26 10:09:34 -05001297 daemonize_nlsr();
1298 }
Obaid Amin2a928a52013-02-20 11:06:51 -06001299
akmhoquebfefef22012-09-26 10:09:34 -05001300 startLogging(nlsr->logDir);
Obaid Amin2a928a52013-02-20 11:06:51 -06001301
akmhoque59980a52012-08-09 12:36:09 -05001302 nlsr->ccn=ccn_create();
akmhoque1771c412012-11-09 13:06:08 -06001303 int ccn_fd=ccn_connect(nlsr->ccn, NULL);
Obaid Amin0e9a3002013-02-20 14:55:37 -06001304
akmhoque1771c412012-11-09 13:06:08 -06001305 if(ccn_fd == -1)
akmhoque03004e62012-09-06 01:12:28 -05001306 {
1307 fprintf(stderr,"Could not connect to ccnd\n");
akmhoquebfefef22012-09-26 10:09:34 -05001308 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
akmhoque81c25e02012-09-10 14:50:33 -05001309 ON_ERROR_DESTROY(-1);
akmhoque03004e62012-09-06 01:12:28 -05001310 }
akmhoque1771c412012-11-09 13:06:08 -06001311
1312 init_api_server(ccn_fd);
akmhoqueb77b95f2013-02-08 12:28:47 -06001313
1314 res=create_sync_slice(nlsr->topo_prefix, nlsr->slice_prefix);
Obaid Amin2a928a52013-02-20 11:06:51 -06001315
akmhoqueb77b95f2013-02-08 12:28:47 -06001316 if(res<0)
1317 {
Obaid Amin2a928a52013-02-20 11:06:51 -06001318 fprintf(stderr, "Can not create slice for prefix %s\n",
Obaid Amin0e9a3002013-02-20 14:55:37 -06001319 nlsr->slice_prefix);
Obaid Amin2a928a52013-02-20 11:06:51 -06001320 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Can not create slice for"
Obaid Amin0e9a3002013-02-20 14:55:37 -06001321 "prefix %s\n",nlsr->slice_prefix);
akmhoqueb77b95f2013-02-08 12:28:47 -06001322 ON_ERROR_DESTROY(res);
1323 }
Obaid Amin0e9a3002013-02-20 14:55:37 -06001324
akmhoque53f64222012-09-05 13:57:51 -05001325 struct ccn_charbuf *router_prefix;
Obaid Amin0e9a3002013-02-20 14:55:37 -06001326
akmhoque03004e62012-09-06 01:12:28 -05001327 router_prefix=ccn_charbuf_create();
1328 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
akmhoque59980a52012-08-09 12:36:09 -05001329 if(res<0)
akmhoque03004e62012-09-06 01:12:28 -05001330 {
1331 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
Obaid Amin2a928a52013-02-20 11:06:51 -06001332 writeLogg(__FILE__,__FUNCTION__,__LINE__,
Obaid Amin0e9a3002013-02-20 14:55:37 -06001333 "Bad ccn URI: %s\n", nlsr->router_name);
akmhoque81c25e02012-09-10 14:50:33 -05001334 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -05001335 }
akmhoque59980a52012-08-09 12:36:09 -05001336
1337 ccn_name_append_str(router_prefix,"nlsr");
akmhoque03004e62012-09-06 01:12:28 -05001338 nlsr->in_interest.data=nlsr->router_name;
akmhoque59980a52012-08-09 12:36:09 -05001339 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
1340 if ( res < 0 )
akmhoque03004e62012-09-06 01:12:28 -05001341 {
1342 fprintf(stderr,"Failed to register interest for router\n");
Obaid Amin2a928a52013-02-20 11:06:51 -06001343 writeLogg(__FILE__,__FUNCTION__,__LINE__,
Obaid Amin0e9a3002013-02-20 14:55:37 -06001344 "Failed to register interest for router\n");
akmhoque81c25e02012-09-10 14:50:33 -05001345 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -05001346 }
1347 ccn_charbuf_destroy(&router_prefix);
Obaid Amin2a928a52013-02-20 11:06:51 -06001348
akmhoque7b791452012-10-30 11:24:56 -05001349 if ( nlsr->debugging )
1350 printf("Router Name : %s\n",nlsr->router_name);
akmhoque9e9fc722012-09-26 14:03:25 -05001351 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name);
akmhoque7b791452012-10-30 11:24:56 -05001352 if ( nlsr->debugging )
1353 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque9e9fc722012-09-26 14:03:25 -05001354 writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque59980a52012-08-09 12:36:09 -05001355
akmhoqueb77b95f2013-02-08 12:28:47 -06001356 add_faces_for_nbrs();
akmhoque53f64222012-09-05 13:57:51 -05001357 print_name_prefix_from_npl();
1358 print_adjacent_from_adl();
akmhoqueb77b95f2013-02-08 12:28:47 -06001359 build_and_install_name_lsas();
1360
akmhoqueb77b95f2013-02-08 12:28:47 -06001361 print_name_lsdb();
1362 if ( nlsr->cor_r != -1.0 && nlsr->cor_theta != -1.0)
1363 {
1364 build_and_install_cor_lsa();
1365 }
1366 write_name_lsdb_to_repo(nlsr->slice_prefix);
akmhoque53f64222012-09-05 13:57:51 -05001367
1368 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque184dde02013-02-14 15:53:24 -06001369 ccn_set_schedule(nlsr->ccn,nlsr->sched);
akmhoque03004e62012-09-06 01:12:28 -05001370 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
akmhoqueffacaa82012-09-13 17:48:30 -05001371 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
Obaid Amin2a928a52013-02-20 11:06:51 -06001372
akmhoque0678c5d2013-02-18 11:03:31 -06001373 res=sync_monitor(nlsr->topo_prefix,nlsr->slice_prefix);
1374 ON_ERROR_DESTROY(res);
akmhoque1c9b92f2012-08-13 10:57:50 -05001375
akmhoque59980a52012-08-09 12:36:09 -05001376 while(1)
akmhoque29c1db52012-09-07 14:47:43 -05001377 {
akmhoqueffacaa82012-09-13 17:48:30 -05001378 if ( nlsr->semaphor == NLSR_UNLOCKED )
akmhoque29c1db52012-09-07 14:47:43 -05001379 {
akmhoqueffacaa82012-09-13 17:48:30 -05001380 if( nlsr->sched != NULL )
1381 {
akmhoque1771c412012-11-09 13:06:08 -06001382 long int micro_sec=ccn_schedule_run(nlsr->sched);
1383 res=nlsr_api_server_poll(micro_sec,ccn_fd);
1384 ON_ERROR_DESTROY(res);
akmhoqueffacaa82012-09-13 17:48:30 -05001385 }
1386 if(nlsr->ccn != NULL)
1387 {
Obaid Amin2a928a52013-02-20 11:06:51 -06001388 res = ccn_run(nlsr->ccn, 1);
akmhoqueffacaa82012-09-13 17:48:30 -05001389 }
1390 if (!(nlsr->sched && nlsr->ccn))
1391 {
1392 break;
1393 }
akmhoque29c1db52012-09-07 14:47:43 -05001394 }
1395
akmhoque59980a52012-08-09 12:36:09 -05001396 }
akmhoque59980a52012-08-09 12:36:09 -05001397 return 0;
1398}
1399