blob: b3c2f64998229de38bc14b458bc2841d0fd74341 [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("/");
akmhoqueb63a41e2013-03-01 12:00:20 -0600147 //close(STDIN_FILENO);
148 //close(STDOUT_FILENO);
149 //close(STDERR_FILENO);
akmhoquebfefef22012-09-26 10:09:34 -0500150}
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
akmhoquececba942013-02-25 17:33:34 -0600213 free(ip_addr);
akmhoque03004e62012-09-06 01:12:28 -0500214 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 }
akmhoque0b60ba92013-02-25 17:55:35 -0600481 //if ( nlsr->logDir)
482 //free(nlsr->logDir);
akmhoque7c234e02013-02-13 11:23:56 -0600483 nlsr->logDir=(char *)calloc(strlen(dir)+1,sizeof(char));
akmhoque6682ca32013-02-22 00:29:35 -0600484 memcpy(nlsr->logDir,dir,strlen(dir)+1);
akmhoquebfefef22012-09-26 10:09:34 -0500485}
486
Obaid Amin2a928a52013-02-20 11:06:51 -0600487 void
akmhoque7b791452012-10-30 11:24:56 -0500488process_command_detailed_log(char *command)
489{
490 if(command==NULL)
491 {
492 printf(" Wrong Command Format ( detailed-log on/off )\n");
493 return;
494 }
495 char *rem;
496 const char *sep=" \t\n";
497 char *on_off;
498
499 on_off=strtok_r(command,sep,&rem);
500 if(on_off==NULL)
501 {
502 printf(" Wrong Command Format ( detailed-log on/off )\n");
503 return;
504 }
Obaid Amin2a928a52013-02-20 11:06:51 -0600505
akmhoque7b791452012-10-30 11:24:56 -0500506 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0)
507 {
508 nlsr->detailed_logging=1;
509 }
510}
511
Obaid Amin2a928a52013-02-20 11:06:51 -0600512 void
akmhoque7b791452012-10-30 11:24:56 -0500513process_command_debug(char *command)
514{
515 if(command==NULL)
516 {
517 printf(" Wrong Command Format ( debug on/off )\n");
518 return;
519 }
520 char *rem;
521 const char *sep=" \t\n";
522 char *on_off;
523
524 on_off=strtok_r(command,sep,&rem);
525 if(on_off==NULL)
526 {
527 printf(" Wrong Command Format ( debug on/off )\n");
528 return;
529 }
Obaid Amin2a928a52013-02-20 11:06:51 -0600530
akmhoque7b791452012-10-30 11:24:56 -0500531 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
532 {
533 nlsr->debugging=1;
534 }
535}
536
akmhoqueb77b95f2013-02-08 12:28:47 -0600537
Obaid Amin2a928a52013-02-20 11:06:51 -0600538 void
akmhoqueb77b95f2013-02-08 12:28:47 -0600539process_command_topo_prefix(char *command)
540{
541 if(command==NULL)
542 {
543 printf(" Wrong Command Format ( topo-prefix )\n");
544 return;
545 }
546 char *rem;
547 const char *sep=" \t\n";
548 char *topo_prefix;
549
550 topo_prefix=strtok_r(command,sep,&rem);
551 if(topo_prefix==NULL)
552 {
553 printf(" Wrong Command Format ( topo-prefix /name/prefix )\n");
554 return;
555 }
556 else
557 {
558 if( nlsr->topo_prefix != NULL)
559 free(nlsr->topo_prefix);
560 if ( topo_prefix[strlen(topo_prefix)-1] == '/' )
561 topo_prefix[strlen(topo_prefix)-1]='\0';
akmhoque829228c2013-02-25 17:39:40 -0600562 //if(nlsr->topo_prefix)
563 //free(nlsr->topo_prefix);
akmhoque7c234e02013-02-13 11:23:56 -0600564 nlsr->topo_prefix=(char *)calloc(strlen(topo_prefix)+1,sizeof(char));
akmhoque6682ca32013-02-22 00:29:35 -0600565 memcpy(nlsr->topo_prefix,topo_prefix,strlen(topo_prefix)+1);
Obaid Amin0e9a3002013-02-20 14:55:37 -0600566 printf ("Topo prefix is: %s", nlsr->topo_prefix);;
akmhoqueb77b95f2013-02-08 12:28:47 -0600567 }
568}
569
570
Obaid Amin2a928a52013-02-20 11:06:51 -0600571 void
akmhoqueb77b95f2013-02-08 12:28:47 -0600572process_command_slice_prefix(char *command)
573{
574 if(command==NULL)
575 {
576 printf(" Wrong Command Format ( slice-prefix /name/prefix )\n");
577 return;
578 }
579 char *rem;
580 const char *sep=" \t\n";
581 char *slice_prefix;
582
583 slice_prefix=strtok_r(command,sep,&rem);
584 if(slice_prefix==NULL)
585 {
586 printf(" Wrong Command Format ( slice-prefix /name/prefix )\n");
587 return;
588 }
589 else
590 {
591 if ( nlsr->slice_prefix != NULL)
592 free(nlsr->slice_prefix);
593 if ( slice_prefix[strlen(slice_prefix)-1] == '/' )
594 slice_prefix[strlen(slice_prefix)-1]='\0';
akmhoque829228c2013-02-25 17:39:40 -0600595 //if( nlsr->slice_prefix)
596 //free(nlsr->slice_prefix);
akmhoque7c234e02013-02-13 11:23:56 -0600597 nlsr->slice_prefix=(char *)calloc(strlen(slice_prefix)+1,sizeof(char));
akmhoque6682ca32013-02-22 00:29:35 -0600598 memcpy(nlsr->slice_prefix,slice_prefix,strlen(slice_prefix)+1);
akmhoqueb77b95f2013-02-08 12:28:47 -0600599 }
600}
601
Obaid Amin2a928a52013-02-20 11:06:51 -0600602 void
akmhoqueb77b95f2013-02-08 12:28:47 -0600603process_command_hyperbolic_routing(char *command)
604{
605 if(command==NULL)
606 {
607 printf(" Wrong Command Format ( hyperbolic-routing on)\n");
608 return;
609 }
610 char *rem;
611 const char *sep=" \t\n";
612 char *on_off;
613
614 on_off=strtok_r(command,sep,&rem);
615 if(on_off==NULL)
616 {
617 printf(" Wrong Command Format ( hyperbolic-routing on )\n");
618 return;
619 }
Obaid Amin2a928a52013-02-20 11:06:51 -0600620
akmhoqueb77b95f2013-02-08 12:28:47 -0600621 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
622 {
623 nlsr->is_hyperbolic_calc=1;
624 }
625}
626
Obaid Amin2a928a52013-02-20 11:06:51 -0600627 void
akmhoqueb77b95f2013-02-08 12:28:47 -0600628process_command_hyperbolic_cordinate(char *command)
629{
630 if(command==NULL)
631 {
632 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
633 return;
634 }
635
636 char *rem;
637 const char *sep=" \t\n\r";
638 char *radious;
639 char *theta;
640
641 radious=strtok_r(command,sep,&rem);
642 if (radious == NULL )
643 {
644 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
645 return;
646 }
647
648 theta=strtok_r(NULL,sep,&rem);
649 if (theta == NULL )
650 {
651 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
652 return;
653 }
654
655 nlsr->cor_r=strtof(radious,NULL);
656 nlsr->cor_theta=strtof(theta,NULL);
657
658}
659
Obaid Amin2a928a52013-02-20 11:06:51 -0600660 void
akmhoqueb77b95f2013-02-08 12:28:47 -0600661process_command_tunnel_type(char *command)
662{
663 if(command==NULL)
664 {
665 printf(" Wrong Command Format ( tunnel-type udp/tcp)\n");
666 return;
667 }
668 char *rem;
669 const char *sep=" \t\n";
670 char *on_off;
671
672 on_off=strtok_r(command,sep,&rem);
673 if(on_off==NULL)
674 {
675 printf(" Wrong Command Format ( tunnel-type udp/tcp )\n");
676 return;
677 }
Obaid Amin2a928a52013-02-20 11:06:51 -0600678
akmhoqueb77b95f2013-02-08 12:28:47 -0600679 if ( strcmp(on_off,"TCP") == 0 || strcmp(on_off,"tcp") == 0 )
680 {
681 nlsr->tunnel_type=IPPROTO_TCP;
682 }
683 else if ( strcmp(on_off,"UDP") == 0 || strcmp(on_off,"udp") == 0 )
684 {
685 nlsr->tunnel_type=IPPROTO_UDP;
686 }
687}
688
Obaid Amin2a928a52013-02-20 11:06:51 -0600689 void
akmhoque59980a52012-08-09 12:36:09 -0500690process_conf_command(char *command)
691{
692 const char *separators=" \t\n";
693 char *remainder=NULL;
694 char *cmd_type=NULL;
695
696 if(command==NULL || strlen(command)==0 || command[0]=='!')
697 return;
698
699 cmd_type=strtok_r(command,separators,&remainder);
700
701 if(!strcmp(cmd_type,"router-name") )
702 {
703 process_command_router_name(remainder);
704 }
705 else if(!strcmp(cmd_type,"ccnneighbor") )
706 {
707 process_command_ccnneighbor(remainder);
708 }
709 else if(!strcmp(cmd_type,"ccnname") )
710 {
711 process_command_ccnname(remainder);
712 }
akmhoqued79438d2012-08-27 13:31:42 -0500713 else if(!strcmp(cmd_type,"interest-retry") )
714 {
715 process_command_interest_retry(remainder);
716 }
717 else if(!strcmp(cmd_type,"interest-resend-time") )
718 {
719 process_command_interest_resend_time(remainder);
720 }
akmhoqued5152122012-09-19 06:44:23 -0500721 else if(!strcmp(cmd_type,"lsa-refresh-time") )
722 {
723 process_command_lsa_refresh_time(remainder);
724 }
725 else if(!strcmp(cmd_type,"router-dead-interval") )
726 {
727 process_command_router_dead_interval(remainder);
728 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600729 else if(!strcmp(cmd_type,"max-faces-per-prefix") )
akmhoque3cced642012-09-24 16:20:20 -0500730 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600731 process_command_max_faces_per_prefix(remainder);
akmhoque3cced642012-09-24 16:20:20 -0500732 }
akmhoquebfefef22012-09-26 10:09:34 -0500733 else if(!strcmp(cmd_type,"logdir") )
734 {
Obaid Amin2a928a52013-02-20 11:06:51 -0600735 process_command_logdir(remainder);
akmhoquebfefef22012-09-26 10:09:34 -0500736 }
akmhoque7b791452012-10-30 11:24:56 -0500737 else if(!strcmp(cmd_type,"detailed-log") )
738 {
Obaid Amin2a928a52013-02-20 11:06:51 -0600739 process_command_detailed_log(remainder);
akmhoque7b791452012-10-30 11:24:56 -0500740 }
741 else if(!strcmp(cmd_type,"debug") )
742 {
Obaid Amin2a928a52013-02-20 11:06:51 -0600743 process_command_debug(remainder);
akmhoque7b791452012-10-30 11:24:56 -0500744 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600745 else if(!strcmp(cmd_type,"topo-prefix") )
746 {
Obaid Amin2a928a52013-02-20 11:06:51 -0600747 process_command_topo_prefix(remainder);
akmhoqueb77b95f2013-02-08 12:28:47 -0600748 }
749 else if(!strcmp(cmd_type,"slice-prefix") )
750 {
Obaid Amin2a928a52013-02-20 11:06:51 -0600751 process_command_slice_prefix(remainder);
akmhoqueb77b95f2013-02-08 12:28:47 -0600752 }
753 else if(!strcmp(cmd_type,"hyperbolic-cordinate") )
754 {
755 process_command_hyperbolic_cordinate(remainder);
756 }
757 else if(!strcmp(cmd_type,"hyperbolic-routing") )
758 {
759 process_command_hyperbolic_routing(remainder);
760 }
761 else if(!strcmp(cmd_type,"tunnel-type") )
762 {
763 process_command_tunnel_type(remainder);
764 }
akmhoqued5152122012-09-19 06:44:23 -0500765 else
akmhoque59980a52012-08-09 12:36:09 -0500766 {
767 printf("Wrong configuration Command %s \n",cmd_type);
768 }
769}
770
akmhoque03004e62012-09-06 01:12:28 -0500771
Obaid Amin0e9a3002013-02-20 14:55:37 -0600772 int
akmhoque59980a52012-08-09 12:36:09 -0500773readConfigFile(const char *filename)
774{
775 FILE *cfg;
776 char buf[1024];
777 int len;
778
779 cfg=fopen(filename, "r");
780
781 if(cfg == NULL)
782 {
783 printf("\nConfiguration File does not exists\n");
Obaid Amin0e9a3002013-02-20 14:55:37 -0600784 return -1;
akmhoque59980a52012-08-09 12:36:09 -0500785 }
786
787 while(fgets((char *)buf, sizeof(buf), cfg))
788 {
789 len=strlen(buf);
790 if(buf[len-1] == '\n')
Obaid Amin2a928a52013-02-20 11:06:51 -0600791 buf[len-1]='\0';
akmhoqued5152122012-09-19 06:44:23 -0500792 if ( buf[0] != '#' && buf[0] != '!')
793 process_conf_command(buf);
akmhoque59980a52012-08-09 12:36:09 -0500794 }
795
796 fclose(cfg);
797
798 return 0;
799}
800
akmhoqueb77b95f2013-02-08 12:28:47 -0600801
Obaid Amin0e9a3002013-02-20 14:55:37 -0600802 void
akmhoqueb77b95f2013-02-08 12:28:47 -0600803add_faces_for_nbrs(void)
804{
805 int i, adl_element;
806 struct ndn_neighbor *nbr;
807
808 struct hashtb_enumerator ee;
Obaid Amin2a928a52013-02-20 11:06:51 -0600809 struct hashtb_enumerator *e = &ee;
810
811 hashtb_start(nlsr->adl, e);
akmhoqueb77b95f2013-02-08 12:28:47 -0600812 adl_element=hashtb_n(nlsr->adl);
813
814 for(i=0;i<adl_element;i++)
815 {
816 nbr=e->data;
Obaid Amin2a928a52013-02-20 11:06:51 -0600817 int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr->neighbor->name,
Obaid Amin0e9a3002013-02-20 14:55:37 -0600818 (const char *)nbr->ip_address, 9695,nlsr->tunnel_type);
akmhoqueb77b95f2013-02-08 12:28:47 -0600819 update_face_to_adl_for_nbr(nbr->neighbor->name, face_id);
Obaid Amin2a928a52013-02-20 11:06:51 -0600820 add_delete_ccn_face_by_face_id(nlsr->ccn,
821 (const char *)nlsr->topo_prefix, OP_REG, face_id);
822 add_delete_ccn_face_by_face_id(nlsr->ccn,
823 (const char *)nlsr->slice_prefix, OP_REG, face_id);
akmhoqueb77b95f2013-02-08 12:28:47 -0600824 hashtb_next(e);
825 }
826
827 hashtb_end(e);
828
829}
830
Obaid Amin0e9a3002013-02-20 14:55:37 -0600831 void
akmhoqueb77b95f2013-02-08 12:28:47 -0600832destroy_faces_for_nbrs(void)
833{
834 int i, adl_element;
835 struct ndn_neighbor *nbr;
836
837 struct hashtb_enumerator ee;
Obaid Amin2a928a52013-02-20 11:06:51 -0600838 struct hashtb_enumerator *e = &ee;
839
840 hashtb_start(nlsr->adl, e);
akmhoqueb77b95f2013-02-08 12:28:47 -0600841 adl_element=hashtb_n(nlsr->adl);
842
843 for(i=0;i<adl_element;i++)
844 {
845 nbr=e->data;
846 if ( nbr->face > 0 )
847 {
Obaid Amin2a928a52013-02-20 11:06:51 -0600848 add_delete_ccn_face_by_face_id(nlsr->ccn,
849 (const char *)nlsr->topo_prefix, OP_UNREG, nbr->face);
850 add_delete_ccn_face_by_face_id(nlsr->ccn,
851 (const char *)nbr->neighbor->name,OP_UNREG,nbr->face);
852 add_delete_ccn_face_by_face_id(nlsr->ccn,
853 (const char *)nlsr->slice_prefix, OP_UNREG, nbr->face);
akmhoqueb77b95f2013-02-08 12:28:47 -0600854 }
855 hashtb_next(e);
856 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600857 hashtb_end(e);
akmhoqueb77b95f2013-02-08 12:28:47 -0600858}
859
Obaid Amin0e9a3002013-02-20 14:55:37 -0600860 char *
akmhoque562caef2012-11-09 13:29:06 -0600861process_api_client_command(char *command)
862{
863 char *msg;
864 msg=(char *)malloc(100);
akmhoqueb77b95f2013-02-08 12:28:47 -0600865 memset(msg,0,100);
Obaid Amin2a928a52013-02-20 11:06:51 -0600866
akmhoque3171d652012-11-13 11:44:33 -0600867 const char *sep=" \t\n";
868 char *rem=NULL;
869 char *cmd_type=NULL;
870 char *op_type=NULL;
871 char *name=NULL;
872 char *face=NULL;
873 int face_id;
874 int res;
875
876 op_type=strtok_r(command,sep,&rem);
877 cmd_type=strtok_r(NULL,sep,&rem);
878 name=strtok_r(NULL,sep,&rem);
879 if ( name[strlen(name)-1] == '/' )
880 name[strlen(name)-1]='\0';
881
Obaid Amin2a928a52013-02-20 11:06:51 -0600882 struct name_prefix *np=(struct name_prefix *) calloc (1,
Obaid Amin0e9a3002013-02-20 14:55:37 -0600883 sizeof(struct name_prefix ));
Obaid Amin2a928a52013-02-20 11:06:51 -0600884 np->name = (char *) calloc (strlen(name)+1,sizeof(char));
akmhoque3171d652012-11-13 11:44:33 -0600885 memcpy(np->name,name,strlen(name)+1);
886 np->length=strlen(name)+1;
887
888 if ( strcmp(cmd_type,"name")!= 0 )
889 {
890 face=strtok_r(NULL,sep,&rem);
891 sscanf(face,"face%d",&face_id);
892 }
Obaid Amin2a928a52013-02-20 11:06:51 -0600893
akmhoque3171d652012-11-13 11:44:33 -0600894 if ( strcmp(cmd_type,"name")== 0 )
895 {
896 if ( strcmp(op_type,"del") == 0 )
897 {
898 res=does_name_exist_in_npl(np);
899 if ( res == 0)
900 {
901 sprintf(msg,"Name %s does not exist !!",name);
902 }
903 else
904 {
905 long int ls_id=get_lsa_id_from_npl(np);
906 if ( ls_id != 0 )
907 {
908 make_name_lsa_invalid(np,LS_TYPE_NAME,ls_id);
909 sprintf(msg,"Name %s has been deleted and Advertised.",name);
910 }
911 else
912 {
913 sprintf(msg,"Name %s does not have an Name LSA yet !!",name);
914 }
915 }
916 }
917 else if ( strcmp(op_type,"add") == 0 )
918 {
919 res=does_name_exist_in_npl(np);
920 if ( res == 0)
921 {
922 add_name_to_npl(np);
923 build_and_install_single_name_lsa(np);
924 sprintf(msg,"Name %s has been added to advertise.",name);
925 }
926 else
927 {
928 sprintf(msg,"Name %s has already been advertised from this router !!",name);
929 }
930 }
931 }
932 else if ( strcmp(cmd_type,"neighbor") == 0 )
933 {
934 if ( strcmp(op_type,"del") == 0 )
935 {
936 res=is_neighbor(np->name);
937 if ( res == 0)
938 {
939 sprintf(msg,"Neighbor %s does not exist !!",name);
940 }
941 else
942 {
943 update_adjacent_status_to_adl(np,NBR_DOWN);
akmhoqueb77b95f2013-02-08 12:28:47 -0600944 int face_id=get_next_hop_face_from_adl(np->name);
945 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)np->name, OP_UNREG, face_id);
946 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_UNREG, face_id);
947 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_UNREG, face_id);
akmhoque3171d652012-11-13 11:44:33 -0600948 delete_nbr_from_adl(np);
949 if(!nlsr->is_build_adj_lsa_sheduled)
950 {
951 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
952 nlsr->is_build_adj_lsa_sheduled=1;
953 }
954 sprintf(msg,"Neighbor %s has been deleted from adjacency list.",name);
955 }
956 }
957 else if ( strcmp(op_type,"add") == 0 )
958 {
959 res=is_neighbor(np->name);
960 if ( res == 0 )
961 {
akmhoque7c234e02013-02-13 11:23:56 -0600962 struct name_prefix *nbr_name=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
akmhoqueb77b95f2013-02-08 12:28:47 -0600963 get_host_name_from_command_string(nbr_name,np->name,0);
964 printf("Hostname of neighbor: %s ",nbr_name->name);
965
akmhoque7c234e02013-02-13 11:23:56 -0600966 char *ip_addr=(char *)calloc(20,sizeof(char));
967 //memset(ip_addr,0,20);
akmhoqueb77b95f2013-02-08 12:28:47 -0600968 get_ip_from_hostname_02(nbr_name->name,ip_addr);
969 printf("IP Address: %s \n",ip_addr);
970 int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr_name->name, (const char *)ip_addr, 9695,nlsr->tunnel_type);
971 update_face_to_adl_for_nbr(nbr_name->name, face_id);
972 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_REG, face_id);
973 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_REG, face_id);
974
975 add_nbr_to_adl(np,face_id,ip_addr);
976
akmhoque3171d652012-11-13 11:44:33 -0600977 sprintf(msg,"Neighbor %s has been added to adjacency list.",name);
akmhoque7c234e02013-02-13 11:23:56 -0600978 free(ip_addr);
akmhoqueb77b95f2013-02-08 12:28:47 -0600979
akmhoque3171d652012-11-13 11:44:33 -0600980 }
981 else
982 {
983 sprintf(msg,"Neighbor %s already exists in adjacency list.",name);
984 }
985 }
986 }
Obaid Amin2a928a52013-02-20 11:06:51 -0600987
akmhoque562caef2012-11-09 13:29:06 -0600988
989 return msg;
990}
akmhoque1771c412012-11-09 13:06:08 -0600991
Obaid Amin2a928a52013-02-20 11:06:51 -0600992 int
akmhoque1771c412012-11-09 13:06:08 -0600993nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd)
994{
995 struct timeval timeout;
akmhoqueb77b95f2013-02-08 12:28:47 -0600996 if (time_out_micro_sec< 500000 && time_out_micro_sec> 0 )
akmhoque1771c412012-11-09 13:06:08 -0600997 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600998 timeout.tv_sec=0;
999 timeout.tv_usec=time_out_micro_sec;
akmhoque1771c412012-11-09 13:06:08 -06001000 }
akmhoqueb77b95f2013-02-08 12:28:47 -06001001 else
akmhoque1771c412012-11-09 13:06:08 -06001002 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001003 timeout.tv_sec = 0;
1004 timeout.tv_usec = 500000;
akmhoque1771c412012-11-09 13:06:08 -06001005 }
Obaid Amin2a928a52013-02-20 11:06:51 -06001006
akmhoque1771c412012-11-09 13:06:08 -06001007 int fd;
1008 int nread;
1009 int result;
1010 fd_set testfds;
1011 unsigned int client_len;
1012 int client_sockfd;
1013 char recv_buffer[1024];
1014 bzero(recv_buffer,1024);
akmhoque95041802012-11-16 09:18:02 -06001015 struct sockaddr_in client_address;
akmhoque1771c412012-11-09 13:06:08 -06001016
1017 testfds=nlsr->readfds;
1018 result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
Obaid Amin2a928a52013-02-20 11:06:51 -06001019
akmhoqueb77b95f2013-02-08 12:28:47 -06001020 for(fd = 0; fd < FD_SETSIZE && result > 0; fd++)
akmhoque1771c412012-11-09 13:06:08 -06001021 {
1022 if(FD_ISSET(fd,&testfds))
1023 {
1024 if ( fd == ccn_fd )
1025 {
1026 return 0;
1027 }
1028 else if(fd == nlsr->nlsr_api_server_sock_fd)
1029 {
1030 client_len = sizeof(client_address);
1031 client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len);
1032 FD_SET(client_sockfd, &nlsr->readfds);
1033 }
1034 else
1035 {
Obaid Amin2a928a52013-02-20 11:06:51 -06001036
akmhoque1771c412012-11-09 13:06:08 -06001037 ioctl(fd, FIONREAD, &nread);
1038 if(nread == 0)
1039 {
1040 close(fd);
1041 FD_CLR(fd, &nlsr->readfds);
1042 }
1043 else
1044 {
1045 recv(fd, recv_buffer, 1024, 0);
akmhoqueb77b95f2013-02-08 12:28:47 -06001046 printf("Received Data from NLSR API cleint: %s \n",recv_buffer);
akmhoque562caef2012-11-09 13:29:06 -06001047 char *msg=process_api_client_command(recv_buffer);
1048 send(fd, msg, strlen(msg),0);
1049 free(msg);
akmhoque1771c412012-11-09 13:06:08 -06001050 close(fd);
1051 FD_CLR(fd, &nlsr->readfds);
akmhoque1771c412012-11-09 13:06:08 -06001052 }
1053 }
1054 }
1055 }
1056
1057 return 0;
1058}
1059
Obaid Amin2a928a52013-02-20 11:06:51 -06001060 int
akmhoqueb77b95f2013-02-08 12:28:47 -06001061check_config_validity()
1062{
1063 if (nlsr->router_name == NULL )
1064 {
1065 fprintf(stderr,"Router name has not been configured :(\n");
1066 return -1;
1067 }
1068 if ( nlsr->is_hyperbolic_calc == 1 && (nlsr->cor_r == -1.0 && nlsr->cor_theta== -1.0) )
1069 {
1070 fprintf(stderr,"Hyperbolic codinate has not been defined :(\n");
1071 return -1;
1072 }
Obaid Amin2a928a52013-02-20 11:06:51 -06001073
akmhoqueb77b95f2013-02-08 12:28:47 -06001074 return 0;
1075}
1076
Obaid Amin2a928a52013-02-20 11:06:51 -06001077 void
akmhoque386081b2012-08-10 10:53:21 -05001078nlsr_destroy( void )
1079{
akmhoque7b791452012-10-30 11:24:56 -05001080 if ( nlsr->debugging )
1081 {
1082 printf("Freeing Allocated Memory....\n");
1083 }
akmhoque9e9fc722012-09-26 14:03:25 -05001084 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n");
akmhoquead584782013-02-22 10:56:03 -06001085
akmhoquefbfd0982012-09-09 20:59:03 -05001086 /* Destroying all face created by nlsr in CCND */
1087 destroy_all_face_by_nlsr();
akmhoqueb77b95f2013-02-08 12:28:47 -06001088 destroy_faces_for_nbrs();
Obaid Amin0e9a3002013-02-20 14:55:37 -06001089
akmhoquee6f98a12013-02-22 10:33:26 -06001090 destroy_adl();
akmhoquee6f98a12013-02-22 10:33:26 -06001091 destroy_npl();
akmhoquead584782013-02-22 10:56:03 -06001092 destroy_lsdb();
akmhoque54a14f42013-02-24 06:16:20 -06001093 destroy_npt();
1094 destroy_routing_table();
Obaid Amin2a928a52013-02-20 11:06:51 -06001095
akmhoque7c234e02013-02-13 11:23:56 -06001096 if ( nlsr->ccns != NULL )
1097 ccns_close(&nlsr->ccns, NULL, NULL);
1098 if ( nlsr->slice != NULL )
1099 ccns_slice_destroy(&nlsr->slice);
akmhoque3560cb62012-09-09 10:52:30 -05001100
akmhoque7c234e02013-02-13 11:23:56 -06001101 close(nlsr->nlsr_api_server_sock_fd);
akmhoque95041802012-11-16 09:18:02 -06001102
akmhoque866c2222013-02-12 10:49:33 -06001103 ccn_destroy(&nlsr->ccn);
akmhoque03004e62012-09-06 01:12:28 -05001104 free(nlsr->router_name);
akmhoque7b791452012-10-30 11:24:56 -05001105 if ( nlsr->debugging )
1106 {
1107 printf("Finished freeing allocated memory\n");
1108 }
akmhoque9e9fc722012-09-26 14:03:25 -05001109 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n");
Obaid Amin2a928a52013-02-20 11:06:51 -06001110
akmhoque7c234e02013-02-13 11:23:56 -06001111 free(nlsr);
akmhoque53f64222012-09-05 13:57:51 -05001112
akmhoque386081b2012-08-10 10:53:21 -05001113}
1114
akmhoque03004e62012-09-06 01:12:28 -05001115
akmhoqueb77b95f2013-02-08 12:28:47 -06001116
Obaid Amin0e9a3002013-02-20 14:55:37 -06001117 void
akmhoque1771c412012-11-09 13:06:08 -06001118init_api_server(int ccn_fd)
1119{
1120 int server_sockfd;
1121 int server_len;
akmhoque95041802012-11-16 09:18:02 -06001122 struct sockaddr_in server_address;
akmhoquef31f13b2012-11-16 09:42:24 -06001123 unsigned int yes=1;
1124
akmhoque95041802012-11-16 09:18:02 -06001125 server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
akmhoque1771c412012-11-09 13:06:08 -06001126
1127 int flags = fcntl(server_sockfd, F_GETFL, 0);
1128 fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
1129
akmhoquef31f13b2012-11-16 09:42:24 -06001130 if (setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0)
1131 {
Obaid Amin2a928a52013-02-20 11:06:51 -06001132 ON_ERROR_DESTROY(-1);
1133 }
akmhoque95041802012-11-16 09:18:02 -06001134
1135 server_address.sin_family = AF_INET;
Adam Alyyanb5fff372013-01-09 14:32:52 -06001136 server_address.sin_addr.s_addr = INADDR_ANY;
1137 server_address.sin_port = htons(nlsr->api_port);
akmhoque95041802012-11-16 09:18:02 -06001138
akmhoque1771c412012-11-09 13:06:08 -06001139 server_len = sizeof(server_address);
1140 bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
1141 listen(server_sockfd, 100);
1142 FD_ZERO(&nlsr->readfds);
1143 FD_SET(server_sockfd, &nlsr->readfds);
1144 FD_SET(ccn_fd, &nlsr->readfds);
1145 nlsr->nlsr_api_server_sock_fd=server_sockfd;
akmhoque1771c412012-11-09 13:06:08 -06001146}
1147
Obaid Amin0e9a3002013-02-20 14:55:37 -06001148 int
akmhoque902d57e2012-08-17 09:24:38 -05001149init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -05001150{
akmhoque03004e62012-09-06 01:12:28 -05001151 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
1152 {
1153 perror("SIGQUIT install error\n");
akmhoque81c25e02012-09-10 14:50:33 -05001154 return -1;
akmhoque03004e62012-09-06 01:12:28 -05001155 }
1156 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
1157 {
1158 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -05001159 return -1;
Obaid Amin2a928a52013-02-20 11:06:51 -06001160 }
1161 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
akmhoque03004e62012-09-06 01:12:28 -05001162 {
1163 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -05001164 return -1;
akmhoque03004e62012-09-06 01:12:28 -05001165 }
akmhoque902d57e2012-08-17 09:24:38 -05001166
akmhoque7c234e02013-02-13 11:23:56 -06001167 nlsr=(struct nlsr *)calloc(1,sizeof(struct nlsr));
Obaid Amin2a928a52013-02-20 11:06:51 -06001168
1169 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), NULL);
1170 nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), NULL);
Obaid Amin2a928a52013-02-20 11:06:51 -06001171 nlsr->npt = hashtb_create(sizeof(struct npt_entry), NULL);
1172 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), NULL);
akmhoque29c1db52012-09-07 14:47:43 -05001173
akmhoque03004e62012-09-06 01:12:28 -05001174 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
akmhoque54a14f42013-02-24 06:16:20 -06001175 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), NULL);
1176 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), NULL);
1177 nlsr->lsdb->cor_lsdb = hashtb_create(sizeof(struct clsa), NULL);
akmhoque07dd8cc2012-08-16 10:23:01 -05001178
akmhoque54a14f42013-02-24 06:16:20 -06001179 /*
Obaid Amin0e9a3002013-02-20 14:55:37 -06001180 char *time_stamp=(char *) calloc (20,sizeof(char));
akmhoque03004e62012-09-06 01:12:28 -05001181 get_current_timestamp_micro(time_stamp);
1182 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
akmhoqueb77b95f2013-02-08 12:28:47 -06001183 memset(nlsr->lsdb->lsdb_version,0,strlen(time_stamp));
akmhoque03004e62012-09-06 01:12:28 -05001184 free(time_stamp);
akmhoque54a14f42013-02-24 06:16:20 -06001185 */
akmhoque902d57e2012-08-17 09:24:38 -05001186
akmhoque54a14f42013-02-24 06:16:20 -06001187 nlsr->lsdb->lsdb_version=get_current_timestamp_micro_v2();
1188
1189 nlsr->in_interest.p = &incoming_interest;
1190 nlsr->in_content.p = &incoming_content;
akmhoque53f64222012-09-05 13:57:51 -05001191
akmhoque59980a52012-08-09 12:36:09 -05001192 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -05001193 nlsr->nlsa_id=0;
akmhoqued79438d2012-08-27 13:31:42 -05001194 nlsr->adj_build_flag=0;
akmhoque53f64222012-09-05 13:57:51 -05001195 nlsr->adj_build_count=0;
1196 nlsr->is_build_adj_lsa_sheduled=0;
akmhoque29c1db52012-09-07 14:47:43 -05001197 nlsr->is_send_lsdb_interest_scheduled=0;
1198 nlsr->is_route_calculation_scheduled=0;
akmhoqued79438d2012-08-27 13:31:42 -05001199
akmhoque7b791452012-10-30 11:24:56 -05001200 nlsr->detailed_logging=0;
1201 nlsr->debugging=0;
1202
akmhoqued79438d2012-08-27 13:31:42 -05001203 nlsr->interest_retry = INTEREST_RETRY;
1204 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
akmhoqueda5b6832012-09-13 22:33:55 -05001205 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
1206 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
akmhoqueb77b95f2013-02-08 12:28:47 -06001207 nlsr->max_faces_per_prefix=MAX_FACES_PER_PREFIX;
akmhoqueffacaa82012-09-13 17:48:30 -05001208 nlsr->semaphor=NLSR_UNLOCKED;
akmhoque81c25e02012-09-10 14:50:33 -05001209
akmhoque95041802012-11-16 09:18:02 -06001210 nlsr->api_port=API_PORT;
1211
akmhoque7c234e02013-02-13 11:23:56 -06001212 nlsr->topo_prefix=(char *)calloc(strlen("/ndn/routing/nlsr")+1,sizeof(char));
akmhoqueb77b95f2013-02-08 12:28:47 -06001213 memcpy(nlsr->topo_prefix,"/ndn/routing/nlsr",strlen("/ndn/routing/nlsr"));
1214
akmhoque7c234e02013-02-13 11:23:56 -06001215 nlsr->slice_prefix=(char *)calloc(strlen("/ndn/routing/nlsr/LSA")+1,sizeof(char));
akmhoqueb77b95f2013-02-08 12:28:47 -06001216 memcpy(nlsr->slice_prefix,"/ndn/routing/nlsr/LSA",strlen("/ndn/routing/nlsr/LSA"));
1217
1218 nlsr->is_hyperbolic_calc=0;
1219 nlsr->cor_r=-1.0;
1220 nlsr->cor_theta=-1.0;
1221
1222 nlsr->tunnel_type=IPPROTO_UDP;
1223
akmhoque81c25e02012-09-10 14:50:33 -05001224 return 0;
akmhoque902d57e2012-08-17 09:24:38 -05001225}
1226
Obaid Amin0e9a3002013-02-20 14:55:37 -06001227 int
akmhoque902d57e2012-08-17 09:24:38 -05001228main(int argc, char *argv[])
1229{
Obaid Amin2a928a52013-02-20 11:06:51 -06001230 int res, ret;
1231 char *config_file;
akmhoquebfefef22012-09-26 10:09:34 -05001232 int daemon_mode=0;
akmhoque95041802012-11-16 09:18:02 -06001233 int port=0;
akmhoque902d57e2012-08-17 09:24:38 -05001234
akmhoque95041802012-11-16 09:18:02 -06001235 while ((res = getopt_long(argc, argv, "df:p:h", longopts, 0)) != -1)
akmhoque59980a52012-08-09 12:36:09 -05001236 {
Obaid Amin2a928a52013-02-20 11:06:51 -06001237 switch (res)
akmhoque59980a52012-08-09 12:36:09 -05001238 {
1239 case 'd':
akmhoquebfefef22012-09-26 10:09:34 -05001240 daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -05001241 break;
1242 case 'f':
1243 config_file = optarg;
1244 break;
akmhoque95041802012-11-16 09:18:02 -06001245 case 'p':
1246 port = atoi(optarg);
1247 break;
akmhoque59980a52012-08-09 12:36:09 -05001248 case 'h':
1249 default:
1250 usage(argv[0]);
1251 }
Obaid Amin2a928a52013-02-20 11:06:51 -06001252 }
akmhoque59980a52012-08-09 12:36:09 -05001253
Obaid Amin2a928a52013-02-20 11:06:51 -06001254 ret = init_nlsr();
1255 ON_ERROR_EXIT(ret);
akmhoque95041802012-11-16 09:18:02 -06001256
1257 if ( port !=0 )
1258 nlsr->api_port=port;
1259
Obaid Amin0e9a3002013-02-20 14:55:37 -06001260 ON_ERROR_DESTROY(readConfigFile(config_file));
akmhoqueb77b95f2013-02-08 12:28:47 -06001261
1262 ON_ERROR_DESTROY(check_config_validity());
1263
1264 print_adjacent_from_adl();
1265
akmhoquebfefef22012-09-26 10:09:34 -05001266 if ( daemon_mode == 1 )
1267 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001268 nlsr->debugging=0;
akmhoquebfefef22012-09-26 10:09:34 -05001269 daemonize_nlsr();
1270 }
Obaid Amin2a928a52013-02-20 11:06:51 -06001271
akmhoquebfefef22012-09-26 10:09:34 -05001272 startLogging(nlsr->logDir);
Obaid Amin2a928a52013-02-20 11:06:51 -06001273
akmhoque59980a52012-08-09 12:36:09 -05001274 nlsr->ccn=ccn_create();
akmhoque1771c412012-11-09 13:06:08 -06001275 int ccn_fd=ccn_connect(nlsr->ccn, NULL);
Obaid Amin0e9a3002013-02-20 14:55:37 -06001276
akmhoque1771c412012-11-09 13:06:08 -06001277 if(ccn_fd == -1)
akmhoque03004e62012-09-06 01:12:28 -05001278 {
1279 fprintf(stderr,"Could not connect to ccnd\n");
akmhoquebfefef22012-09-26 10:09:34 -05001280 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
akmhoque81c25e02012-09-10 14:50:33 -05001281 ON_ERROR_DESTROY(-1);
akmhoque03004e62012-09-06 01:12:28 -05001282 }
akmhoque1771c412012-11-09 13:06:08 -06001283
1284 init_api_server(ccn_fd);
akmhoqueb77b95f2013-02-08 12:28:47 -06001285
1286 res=create_sync_slice(nlsr->topo_prefix, nlsr->slice_prefix);
Obaid Amin2a928a52013-02-20 11:06:51 -06001287
akmhoqueb77b95f2013-02-08 12:28:47 -06001288 if(res<0)
1289 {
Obaid Amin2a928a52013-02-20 11:06:51 -06001290 fprintf(stderr, "Can not create slice for prefix %s\n",
Obaid Amin0e9a3002013-02-20 14:55:37 -06001291 nlsr->slice_prefix);
Obaid Amin2a928a52013-02-20 11:06:51 -06001292 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Can not create slice for"
Obaid Amin0e9a3002013-02-20 14:55:37 -06001293 "prefix %s\n",nlsr->slice_prefix);
akmhoqueb77b95f2013-02-08 12:28:47 -06001294 ON_ERROR_DESTROY(res);
1295 }
Obaid Amin0e9a3002013-02-20 14:55:37 -06001296
akmhoque53f64222012-09-05 13:57:51 -05001297 struct ccn_charbuf *router_prefix;
Obaid Amin0e9a3002013-02-20 14:55:37 -06001298
akmhoque03004e62012-09-06 01:12:28 -05001299 router_prefix=ccn_charbuf_create();
1300 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
akmhoque59980a52012-08-09 12:36:09 -05001301 if(res<0)
akmhoque03004e62012-09-06 01:12:28 -05001302 {
1303 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
Obaid Amin2a928a52013-02-20 11:06:51 -06001304 writeLogg(__FILE__,__FUNCTION__,__LINE__,
Obaid Amin0e9a3002013-02-20 14:55:37 -06001305 "Bad ccn URI: %s\n", nlsr->router_name);
akmhoque81c25e02012-09-10 14:50:33 -05001306 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -05001307 }
akmhoque59980a52012-08-09 12:36:09 -05001308
1309 ccn_name_append_str(router_prefix,"nlsr");
akmhoque03004e62012-09-06 01:12:28 -05001310 nlsr->in_interest.data=nlsr->router_name;
akmhoque59980a52012-08-09 12:36:09 -05001311 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
1312 if ( res < 0 )
akmhoque03004e62012-09-06 01:12:28 -05001313 {
1314 fprintf(stderr,"Failed to register interest for router\n");
Obaid Amin2a928a52013-02-20 11:06:51 -06001315 writeLogg(__FILE__,__FUNCTION__,__LINE__,
Obaid Amin0e9a3002013-02-20 14:55:37 -06001316 "Failed to register interest for router\n");
akmhoque81c25e02012-09-10 14:50:33 -05001317 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -05001318 }
1319 ccn_charbuf_destroy(&router_prefix);
Obaid Amin2a928a52013-02-20 11:06:51 -06001320
akmhoque7b791452012-10-30 11:24:56 -05001321 if ( nlsr->debugging )
1322 printf("Router Name : %s\n",nlsr->router_name);
akmhoque9e9fc722012-09-26 14:03:25 -05001323 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name);
akmhoque7b791452012-10-30 11:24:56 -05001324 if ( nlsr->debugging )
1325 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque9e9fc722012-09-26 14:03:25 -05001326 writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque59980a52012-08-09 12:36:09 -05001327
akmhoqueb77b95f2013-02-08 12:28:47 -06001328 add_faces_for_nbrs();
akmhoque53f64222012-09-05 13:57:51 -05001329 print_name_prefix_from_npl();
1330 print_adjacent_from_adl();
akmhoqueb77b95f2013-02-08 12:28:47 -06001331 build_and_install_name_lsas();
1332
akmhoqueb77b95f2013-02-08 12:28:47 -06001333 print_name_lsdb();
1334 if ( nlsr->cor_r != -1.0 && nlsr->cor_theta != -1.0)
1335 {
1336 build_and_install_cor_lsa();
1337 }
1338 write_name_lsdb_to_repo(nlsr->slice_prefix);
akmhoque53f64222012-09-05 13:57:51 -05001339
1340 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque184dde02013-02-14 15:53:24 -06001341 ccn_set_schedule(nlsr->ccn,nlsr->sched);
akmhoque03004e62012-09-06 01:12:28 -05001342 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
akmhoqueffacaa82012-09-13 17:48:30 -05001343 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
Obaid Amin2a928a52013-02-20 11:06:51 -06001344
akmhoque0678c5d2013-02-18 11:03:31 -06001345 res=sync_monitor(nlsr->topo_prefix,nlsr->slice_prefix);
1346 ON_ERROR_DESTROY(res);
akmhoque1c9b92f2012-08-13 10:57:50 -05001347
akmhoque59980a52012-08-09 12:36:09 -05001348 while(1)
akmhoque29c1db52012-09-07 14:47:43 -05001349 {
akmhoqueffacaa82012-09-13 17:48:30 -05001350 if ( nlsr->semaphor == NLSR_UNLOCKED )
akmhoque29c1db52012-09-07 14:47:43 -05001351 {
akmhoqueffacaa82012-09-13 17:48:30 -05001352 if( nlsr->sched != NULL )
1353 {
akmhoque1771c412012-11-09 13:06:08 -06001354 long int micro_sec=ccn_schedule_run(nlsr->sched);
1355 res=nlsr_api_server_poll(micro_sec,ccn_fd);
1356 ON_ERROR_DESTROY(res);
akmhoqueffacaa82012-09-13 17:48:30 -05001357 }
1358 if(nlsr->ccn != NULL)
1359 {
Obaid Amin2a928a52013-02-20 11:06:51 -06001360 res = ccn_run(nlsr->ccn, 1);
akmhoqueffacaa82012-09-13 17:48:30 -05001361 }
1362 if (!(nlsr->sched && nlsr->ccn))
1363 {
1364 break;
1365 }
akmhoque29c1db52012-09-07 14:47:43 -05001366 }
1367
akmhoque59980a52012-08-09 12:36:09 -05001368 }
akmhoque59980a52012-08-09 12:36:09 -05001369 return 0;
1370}
1371