First Commit with CCN Sync
diff --git a/nlsr-sync-0.0/nlsr.c b/nlsr-sync-0.0/nlsr.c
new file mode 100755
index 0000000..e3b43ae
--- /dev/null
+++ b/nlsr-sync-0.0/nlsr.c
@@ -0,0 +1,1099 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <sys/time.h>
+#include <sys/stat.h>
+#include <assert.h>
+#include <sys/types.h>
+#include <signal.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <ccn/ccn.h>
+#include <ccn/uri.h>
+#include <ccn/keystore.h>
+#include <ccn/signing.h>
+#include <ccn/schedule.h>
+#include <ccn/hashtb.h>
+#include <ccn/sync.h>
+#include <ccn/seqwriter.h>
+
+#include "nlsr.h"
+#include "nlsr_ndn.h"
+#include "nlsr_lsdb.h"
+#include "utility.h"
+#include "nlsr_npl.h"
+#include "nlsr_adl.h"
+#include "nlsr_npt.h"
+#include "nlsr_route.h"
+#include "nlsr_sync.h"
+
+
+#define ON_ERROR_DESTROY(resval) \
+{ \
+ if ((resval) < 0) { \
+ nlsr_destroy(); \
+ exit(1);\
+ } \
+}
+
+
+#define ON_ERROR_EXIT(resval) \
+{ \
+ if ((resval) < 0) { \
+ exit(1); \
+ } \
+}
+
+struct option longopts[] =
+{
+ { "daemon", no_argument, NULL, 'd'},
+ { "config_file", required_argument, NULL, 'f'},
+ { "api_port", required_argument, NULL, 'p'},
+ { "help", no_argument, NULL, 'h'},
+ { 0 }
+};
+
+static int
+usage(char *progname)
+{
+
+ printf("Usage: %s [OPTIONS...]\n\
+ NDN routing....\n\
+ -d, --daemon Run in daemon mode\n\
+ -f, --config_file Specify configuration file name\n\
+ -p, --api_port port where api client will connect\n\
+ -h, --help Display this help message\n", progname);
+
+ exit(1);
+}
+
+void ndn_rtr_gettime(const struct ccn_gettime *self, struct ccn_timeval *result)
+{
+ struct timeval now = {0};
+ gettimeofday(&now, 0);
+ result->s = now.tv_sec;
+ result->micros = now.tv_usec;
+}
+
+static struct ccn_gettime ndn_rtr_ticker = {
+ "timer",
+ &ndn_rtr_gettime,
+ 1000000,
+ NULL
+};
+
+void
+nlsr_lock(void)
+{
+ nlsr->semaphor=NLSR_LOCKED;
+}
+
+void
+nlsr_unlock(void)
+{
+ nlsr->semaphor=NLSR_UNLOCKED;
+}
+
+void
+nlsr_stop_signal_handler(int sig)
+{
+ signal(sig, SIG_IGN);
+ nlsr_destroy();
+ exit(0);
+}
+
+void
+daemonize_nlsr(void)
+{
+ int ret;
+ pid_t process_id = 0;
+ pid_t sid = 0;
+ process_id = fork();
+ if (process_id < 0)
+ {
+ printf("Daemonization failed!\n");
+ ON_ERROR_DESTROY(process_id);
+ }
+ if (process_id > 0)
+ {
+ printf("Process daemonized. Process id: %d \n", process_id);
+ ret=process_id;
+ exit(0);
+ }
+
+ umask(0);
+ sid = setsid();
+ if(sid < 0)
+ {
+ ON_ERROR_DESTROY(sid);
+ }
+
+ chdir("/");
+ close(STDIN_FILENO);
+ close(STDOUT_FILENO);
+ close(STDERR_FILENO);
+}
+
+void
+process_command_ccnneighbor(char *command)
+{
+ if(command==NULL)
+ {
+ printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
+ return;
+ }
+ char *rem;
+ const char *sep=" \t\n";
+ char *rtr_name,*face;
+
+ rtr_name=strtok_r(command,sep,&rem);
+ if(rtr_name==NULL)
+ {
+ printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
+ return;
+ }
+
+ face=strtok_r(NULL,sep,&rem);
+ if(face==NULL)
+ {
+ printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
+ return;
+ }
+
+ printf("Router: %s face: %s\n",rtr_name,face);
+ int face_id;
+ int res;
+ res=sscanf(face,"face%d",&face_id);
+
+ if(res != 1 )
+ {
+ printf(" Wrong Command Format ( ccnneighbor router_name faceX) where X is integer\n");
+ return;
+ }
+
+ if ( rtr_name[strlen(rtr_name)-1] == '/' )
+ {
+ rtr_name[strlen(rtr_name)-1]='\0';
+ }
+ struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
+ nbr->name=(char *)malloc(strlen(rtr_name)+1);
+ memset(nbr->name,0,strlen(rtr_name)+1);
+ memcpy(nbr->name,rtr_name,strlen(rtr_name)+1);
+ nbr->length=strlen(rtr_name)+1;
+
+ add_nbr_to_adl(nbr,face_id);
+
+ free(nbr->name);
+ free(nbr);
+}
+
+void
+process_command_ccnname(char *command)
+{
+
+ if(command==NULL)
+ {
+ printf(" Wrong Command Format ( ccnname /name/prefix)\n");
+ return;
+ }
+ char *rem;
+ const char *sep=" \t\n";
+ char *name;
+ name=strtok_r(command,sep,&rem);
+ if(name==NULL)
+ {
+ printf(" Wrong Command Format ( ccnname /name/prefix/ )\n");
+ return;
+ }
+
+ printf("Name Prefix: %s \n",name);
+
+ if ( name[strlen(name)-1] == '/' )
+ name[strlen(name)-1]='\0';
+
+ struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
+ np->name=(char *)malloc(strlen(name)+1);
+ memset(np->name,0,strlen(name)+1);
+ memcpy(np->name,name,strlen(name)+1);
+ np->length=strlen(name)+1;
+
+ add_name_to_npl(np);
+
+ free(np->name);
+ free(np);
+}
+
+
+void
+process_command_router_name(char *command)
+{
+ if(command==NULL)
+ {
+ printf(" Wrong Command Format ( router-name /router/name )\n");
+ return;
+ }
+ char *rem;
+ const char *sep=" \t\n";
+ char *rtr_name;
+
+ rtr_name=strtok_r(command,sep,&rem);
+ if(rtr_name==NULL)
+ {
+ printf(" Wrong Command Format ( router-name /router/name )\n");
+ return;
+ }
+
+
+ if ( rtr_name[strlen(rtr_name)-1] == '/' )
+ rtr_name[strlen(rtr_name)-1]='\0';
+
+ nlsr->router_name=(char *)malloc(strlen(rtr_name)+1);
+ memset(nlsr->router_name,0,strlen(rtr_name)+1);
+ memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1);
+
+
+}
+
+void
+process_command_lsdb_synch_interval(char *command)
+{
+ if(command==NULL)
+ {
+ printf(" Wrong Command Format ( lsdb-synch-interval secs )\n");
+ return;
+ }
+ char *rem;
+ const char *sep=" \t\n";
+ char *secs;
+ long int seconds;
+
+ secs=strtok_r(command,sep,&rem);
+ if(secs==NULL)
+ {
+ printf(" Wrong Command Format ( lsdb-synch-interval secs)\n");
+ return;
+ }
+
+ seconds=atoi(secs);
+ if ( seconds >= 120 && seconds <= 3600 )
+ {
+ nlsr->lsdb_synch_interval=seconds;
+ }
+
+}
+
+
+void
+process_command_interest_retry(char *command)
+{
+ if(command==NULL)
+ {
+ printf(" Wrong Command Format ( interest-retry number )\n");
+ return;
+ }
+ char *rem;
+ const char *sep=" \t\n";
+ char *retry;
+ long int retry_number;
+
+ retry=strtok_r(command,sep,&rem);
+ if(retry==NULL)
+ {
+ printf(" Wrong Command Format ( interest-retry number)\n");
+ return;
+ }
+
+ retry_number=atoi(retry);
+ if ( retry_number >= 1 && retry_number<=10 )
+ {
+ nlsr->interest_retry=retry_number;
+ }
+
+}
+
+void
+process_command_interest_resend_time(char *command)
+{
+ if(command==NULL)
+ {
+ printf(" Wrong Command Format ( interest-resend-time secs )\n");
+ return;
+ }
+ char *rem;
+ const char *sep=" \t\n";
+ char *secs;
+ long int seconds;
+
+ secs=strtok_r(command,sep,&rem);
+ if(secs==NULL)
+ {
+ printf(" Wrong Command Format ( interest-resend-time secs)\n");
+ return;
+ }
+
+ seconds=atoi(secs);
+ if ( seconds <= 60 && seconds >= 1 )
+ {
+ nlsr->interest_resend_time=seconds;
+ }
+}
+
+
+void
+process_command_lsa_refresh_time(char *command)
+{
+ if(command==NULL)
+ {
+ printf(" Wrong Command Format ( lsa-refresh-time secs )\n");
+ return;
+ }
+ char *rem;
+ const char *sep=" \t\n";
+ char *secs;
+ long int seconds;
+
+ secs=strtok_r(command,sep,&rem);
+ if(secs==NULL)
+ {
+ printf(" Wrong Command Format ( lsa-refresh-time secs)\n");
+ return;
+ }
+
+ seconds=atoi(secs);
+ if ( seconds >= 240 && seconds <= 3600 )
+ {
+ nlsr->lsa_refresh_time=seconds;
+ }
+
+}
+
+void
+process_command_router_dead_interval(char *command)
+{
+ if(command==NULL)
+ {
+ printf(" Wrong Command Format ( router-dead-interval secs )\n");
+ return;
+ }
+ char *rem;
+ const char *sep=" \t\n";
+ char *secs;
+ long int seconds;
+
+ secs=strtok_r(command,sep,&rem);
+ if(secs==NULL)
+ {
+ printf(" Wrong Command Format ( router-dead-interval secs)\n");
+ return;
+ }
+
+ seconds=atoi(secs);
+ if ( seconds >= 360 && seconds <= 5400 )
+ {
+ nlsr->router_dead_interval=seconds;
+ }
+
+}
+
+void
+process_command_multi_path_face_num(char *command)
+{
+ if(command==NULL)
+ {
+ printf(" Wrong Command Format ( multi-path-face-num n )\n");
+ return;
+ }
+ char *rem;
+ const char *sep=" \t\n";
+ char *num;
+ long int number;
+
+ num=strtok_r(command,sep,&rem);
+ if(num==NULL)
+ {
+ printf(" Wrong Command Format ( multi-path-face-num n)\n");
+ return;
+ }
+
+ number=atoi(num);
+ if ( number >= 0 && number <= 60 )
+ {
+ nlsr->multi_path_face_num=number;
+ }
+
+}
+
+void
+process_command_logdir(char *command)
+{
+ if(command==NULL)
+ {
+ printf(" Wrong Command Format ( logdir /path/to/logdir )\n");
+ return;
+ }
+ char *rem;
+ const char *sep=" \t\n";
+ char *dir;
+
+ dir=strtok_r(command,sep,&rem);
+ if(dir==NULL)
+ {
+ printf(" Wrong Command Format ( logdir /path/to/logdir/ )\n");
+ return;
+ }
+
+ nlsr->logDir=(char *)malloc(strlen(dir)+1);
+ memset(nlsr->logDir,0,strlen(dir)+1);
+ memcpy(nlsr->logDir,dir,strlen(dir));
+}
+
+void
+process_command_detailed_log(char *command)
+{
+ if(command==NULL)
+ {
+ printf(" Wrong Command Format ( detailed-log on/off )\n");
+ return;
+ }
+ char *rem;
+ const char *sep=" \t\n";
+ char *on_off;
+
+ on_off=strtok_r(command,sep,&rem);
+ if(on_off==NULL)
+ {
+ printf(" Wrong Command Format ( detailed-log on/off )\n");
+ return;
+ }
+
+ if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0)
+ {
+ nlsr->detailed_logging=1;
+ }
+}
+
+void
+process_command_debug(char *command)
+{
+ if(command==NULL)
+ {
+ printf(" Wrong Command Format ( debug on/off )\n");
+ return;
+ }
+ char *rem;
+ const char *sep=" \t\n";
+ char *on_off;
+
+ on_off=strtok_r(command,sep,&rem);
+ if(on_off==NULL)
+ {
+ printf(" Wrong Command Format ( debug on/off )\n");
+ return;
+ }
+
+ if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
+ {
+ nlsr->debugging=1;
+ }
+}
+
+void
+process_conf_command(char *command)
+{
+ const char *separators=" \t\n";
+ char *remainder=NULL;
+ char *cmd_type=NULL;
+
+ if(command==NULL || strlen(command)==0 || command[0]=='!')
+ return;
+
+ cmd_type=strtok_r(command,separators,&remainder);
+
+ if(!strcmp(cmd_type,"router-name") )
+ {
+ process_command_router_name(remainder);
+ }
+ else if(!strcmp(cmd_type,"ccnneighbor") )
+ {
+ process_command_ccnneighbor(remainder);
+ }
+ else if(!strcmp(cmd_type,"ccnname") )
+ {
+ process_command_ccnname(remainder);
+ }
+ else if(!strcmp(cmd_type,"lsdb-synch-interval") )
+ {
+ process_command_lsdb_synch_interval(remainder);
+ }
+ else if(!strcmp(cmd_type,"interest-retry") )
+ {
+ process_command_interest_retry(remainder);
+ }
+ else if(!strcmp(cmd_type,"interest-resend-time") )
+ {
+ process_command_interest_resend_time(remainder);
+ }
+ else if(!strcmp(cmd_type,"lsa-refresh-time") )
+ {
+ process_command_lsa_refresh_time(remainder);
+ }
+ else if(!strcmp(cmd_type,"router-dead-interval") )
+ {
+ process_command_router_dead_interval(remainder);
+ }
+ else if(!strcmp(cmd_type,"multi-path-face-num") )
+ {
+ process_command_multi_path_face_num(remainder);
+ }
+ else if(!strcmp(cmd_type,"logdir") )
+ {
+ process_command_logdir(remainder);
+ }
+ else if(!strcmp(cmd_type,"detailed-log") )
+ {
+ process_command_detailed_log(remainder);
+ }
+ else if(!strcmp(cmd_type,"debug") )
+ {
+ process_command_debug(remainder);
+ }
+ else
+ {
+ printf("Wrong configuration Command %s \n",cmd_type);
+ }
+}
+
+
+int
+readConfigFile(const char *filename)
+{
+ FILE *cfg;
+ char buf[1024];
+ int len;
+
+ cfg=fopen(filename, "r");
+
+ if(cfg == NULL)
+ {
+ printf("\nConfiguration File does not exists\n");
+ exit(1);
+ }
+
+ while(fgets((char *)buf, sizeof(buf), cfg))
+ {
+ len=strlen(buf);
+ if(buf[len-1] == '\n')
+ buf[len-1]='\0';
+ if ( buf[0] != '#' && buf[0] != '!')
+ process_conf_command(buf);
+ }
+
+ fclose(cfg);
+
+ return 0;
+}
+
+char *
+process_api_client_command(char *command)
+{
+ char *msg;
+ msg=(char *)malloc(100);
+ memset(msg,100,0);
+
+ const char *sep=" \t\n";
+ char *rem=NULL;
+ char *cmd_type=NULL;
+ char *op_type=NULL;
+ char *name=NULL;
+ char *face=NULL;
+ int face_id;
+ int res;
+
+ op_type=strtok_r(command,sep,&rem);
+ cmd_type=strtok_r(NULL,sep,&rem);
+ name=strtok_r(NULL,sep,&rem);
+ if ( name[strlen(name)-1] == '/' )
+ name[strlen(name)-1]='\0';
+
+ struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
+ np->name=(char *)malloc(strlen(name)+1);
+ memset(np->name,0,strlen(name)+1);
+ memcpy(np->name,name,strlen(name)+1);
+ np->length=strlen(name)+1;
+
+ if ( strcmp(cmd_type,"name")!= 0 )
+ {
+ face=strtok_r(NULL,sep,&rem);
+ sscanf(face,"face%d",&face_id);
+ }
+
+ if ( strcmp(cmd_type,"name")== 0 )
+ {
+ if ( strcmp(op_type,"del") == 0 )
+ {
+ res=does_name_exist_in_npl(np);
+ if ( res == 0)
+ {
+ sprintf(msg,"Name %s does not exist !!",name);
+ }
+ else
+ {
+ long int ls_id=get_lsa_id_from_npl(np);
+ if ( ls_id != 0 )
+ {
+ make_name_lsa_invalid(np,LS_TYPE_NAME,ls_id);
+ sprintf(msg,"Name %s has been deleted and Advertised.",name);
+ }
+ else
+ {
+ sprintf(msg,"Name %s does not have an Name LSA yet !!",name);
+ }
+ }
+ }
+ else if ( strcmp(op_type,"add") == 0 )
+ {
+ res=does_name_exist_in_npl(np);
+ if ( res == 0)
+ {
+ add_name_to_npl(np);
+ build_and_install_single_name_lsa(np);
+ sprintf(msg,"Name %s has been added to advertise.",name);
+ }
+ else
+ {
+ sprintf(msg,"Name %s has already been advertised from this router !!",name);
+ }
+ }
+ }
+ else if ( strcmp(cmd_type,"neighbor") == 0 )
+ {
+ if ( strcmp(op_type,"del") == 0 )
+ {
+ res=is_neighbor(np->name);
+ if ( res == 0)
+ {
+ sprintf(msg,"Neighbor %s does not exist !!",name);
+ }
+ else
+ {
+ update_adjacent_status_to_adl(np,NBR_DOWN);
+ delete_nbr_from_adl(np);
+ if(!nlsr->is_build_adj_lsa_sheduled)
+ {
+ nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
+ nlsr->is_build_adj_lsa_sheduled=1;
+ }
+ sprintf(msg,"Neighbor %s has been deleted from adjacency list.",name);
+ }
+ }
+ else if ( strcmp(op_type,"add") == 0 )
+ {
+ res=is_neighbor(np->name);
+ if ( res == 0 )
+ {
+ add_nbr_to_adl(np,face_id);
+ sprintf(msg,"Neighbor %s has been added to adjacency list.",name);
+ }
+ else
+ {
+ sprintf(msg,"Neighbor %s already exists in adjacency list.",name);
+ }
+ }
+ }
+
+
+ return msg;
+}
+
+int
+nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd)
+{
+ struct timeval timeout;
+ if ( time_out_micro_sec < 0 )
+ {
+ timeout.tv_sec=1;
+ timeout.tv_usec=0;
+ }
+ else
+ {
+ time_out_micro_sec=(long int)time_out_micro_sec*0.4;
+ timeout.tv_sec=time_out_micro_sec / 1000000;
+ timeout.tv_usec=time_out_micro_sec % 1000000;
+ }
+
+
+ int fd;
+ int nread;
+ int result;
+ fd_set testfds;
+ unsigned int client_len;
+ int client_sockfd;
+ char recv_buffer[1024];
+ bzero(recv_buffer,1024);
+ struct sockaddr_in client_address;
+
+ testfds=nlsr->readfds;
+ result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
+
+ for(fd = 0; fd < FD_SETSIZE; fd++)
+ {
+ if(FD_ISSET(fd,&testfds))
+ {
+ if ( fd == ccn_fd )
+ {
+ return 0;
+ }
+ else if(fd == nlsr->nlsr_api_server_sock_fd)
+ {
+ client_len = sizeof(client_address);
+ client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len);
+ FD_SET(client_sockfd, &nlsr->readfds);
+ }
+ else
+ {
+
+ ioctl(fd, FIONREAD, &nread);
+ if(nread == 0)
+ {
+ close(fd);
+ FD_CLR(fd, &nlsr->readfds);
+ }
+ else
+ {
+ recv(fd, recv_buffer, 1024, 0);
+ printf("Received Data from NLSR API cleint: %s \n",recv_buffer);
+ char *msg=process_api_client_command(recv_buffer);
+ send(fd, msg, strlen(msg),0);
+ free(msg);
+ close(fd);
+ FD_CLR(fd, &nlsr->readfds);
+ }
+ }
+ }
+ }
+
+ return 0;
+}
+
+void
+nlsr_destroy( void )
+{
+ if ( nlsr->debugging )
+ {
+ printf("Freeing Allocated Memory....\n");
+ }
+ writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n");
+ /* Destroying all face created by nlsr in CCND */
+ destroy_all_face_by_nlsr();
+
+ /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
+ hashtb_destroy(&nlsr->npl);
+ hashtb_destroy(&nlsr->adl);
+ hashtb_destroy(&nlsr->lsdb->name_lsdb);
+ hashtb_destroy(&nlsr->lsdb->adj_lsdb);
+ hashtb_destroy(&nlsr->pit_alsa);
+
+ //To Do: has to destroy the face_list one by one
+
+ hashtb_destroy(&nlsr->routing_table);
+
+
+ int i, npt_element;
+ struct npt_entry *ne;
+ struct hashtb_enumerator ee;
+ struct hashtb_enumerator *e = ⅇ
+ hashtb_start(nlsr->npt, e);
+ npt_element=hashtb_n(nlsr->npt);
+ for(i=0;i<npt_element;i++)
+ {
+ ne=e->data;
+ hashtb_destroy(&ne->name_list);
+ hashtb_destroy(&ne->face_list);
+ hashtb_next(e);
+ }
+
+ hashtb_end(e);
+ hashtb_destroy(&nlsr->npt);
+
+
+ ccns_close(&nlsr->ccns, NULL, NULL);
+ ccns_slice_destroy(&nlsr->slice);
+
+
+
+ close(nlsr->nlsr_api_server_sock_fd);
+
+ ccn_schedule_destroy(&nlsr->sched);
+ ccn_destroy(&nlsr->ccn);
+
+ free(nlsr->lsdb->lsdb_version);
+ free(nlsr->lsdb);
+ free(nlsr->router_name);
+ free(nlsr);
+ if ( nlsr->debugging )
+ {
+ printf("Finished freeing allocated memory\n");
+ }
+ writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n");
+
+}
+
+
+
+void
+init_api_server(int ccn_fd)
+{
+ int server_sockfd;
+ int server_len;
+ struct sockaddr_in server_address;
+ unsigned int yes=1;
+
+ server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
+
+ int flags = fcntl(server_sockfd, F_GETFL, 0);
+ fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
+
+ if (setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0)
+ {
+ ON_ERROR_DESTROY(-1);
+ }
+
+ server_address.sin_family = AF_INET;
+ server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
+ server_address.sin_port = nlsr->api_port;
+
+ server_len = sizeof(server_address);
+ bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
+ listen(server_sockfd, 100);
+ FD_ZERO(&nlsr->readfds);
+ FD_SET(server_sockfd, &nlsr->readfds);
+ FD_SET(ccn_fd, &nlsr->readfds);
+ nlsr->nlsr_api_server_sock_fd=server_sockfd;
+
+}
+
+int
+init_nlsr(void)
+{
+ if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
+ {
+ perror("SIGQUIT install error\n");
+ return -1;
+ }
+ if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
+ {
+ perror("SIGTERM install error\n");
+ return -1;
+ }
+ if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
+ {
+ perror("SIGTERM install error\n");
+ return -1;
+ }
+
+ nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
+
+ struct hashtb_param param_adl = {0};
+ nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), ¶m_adl);
+ struct hashtb_param param_npl = {0};
+ nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), ¶m_npl);
+ struct hashtb_param param_pit_alsa = {0};
+ nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), ¶m_pit_alsa);
+ struct hashtb_param param_npt = {0};
+ nlsr->npt = hashtb_create(sizeof(struct npt_entry), ¶m_npt);
+ struct hashtb_param param_rte = {0};
+ nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), ¶m_rte);
+
+ nlsr->in_interest.p = &incoming_interest;
+ nlsr->in_content.p = &incoming_content;
+
+ nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
+
+ char *time_stamp=(char *)malloc(20);
+ memset(time_stamp,0,20);
+ get_current_timestamp_micro(time_stamp);
+ nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
+ memset(nlsr->lsdb->lsdb_version,'0',strlen(time_stamp));
+ free(time_stamp);
+
+ struct hashtb_param param_adj_lsdb = {0};
+ nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), ¶m_adj_lsdb);
+ struct hashtb_param param_name_lsdb = {0};
+ nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), ¶m_name_lsdb);
+
+
+
+
+ nlsr->is_synch_init=1;
+ nlsr->nlsa_id=0;
+ nlsr->adj_build_flag=0;
+ nlsr->adj_build_count=0;
+ nlsr->is_build_adj_lsa_sheduled=0;
+ nlsr->is_send_lsdb_interest_scheduled=0;
+ nlsr->is_route_calculation_scheduled=0;
+
+ nlsr->detailed_logging=0;
+ nlsr->debugging=0;
+
+ nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
+ nlsr->interest_retry = INTEREST_RETRY;
+ nlsr->interest_resend_time = INTEREST_RESEND_TIME;
+ nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
+ nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
+ nlsr->multi_path_face_num=MULTI_PATH_FACE_NUM;
+ nlsr->semaphor=NLSR_UNLOCKED;
+
+ nlsr->api_port=API_PORT;
+
+ //create_sync_slice();
+
+ nlsr->topo_prefix=(char *)malloc(strlen("/ndn/routing/nlsr")+1);
+ memset(nlsr->topo_prefix,strlen("/ndn/routing/nlsr")+1,0);
+ memcpy(nlsr->topo_prefix,"/ndn/routing/nlsr",strlen("/ndn/routing/nlsr"));
+
+ nlsr->slice_prefix=(char *)malloc(strlen("/ndn/routing/nlsr/LSA")+1);
+ memset(nlsr->slice_prefix,strlen("/ndn/routing/nlsr/LSA")+1,0);
+ memcpy(nlsr->slice_prefix,"/ndn/routing/nlsr/LSA",strlen("/ndn/routing/nlsr/LSA"));
+
+ return 0;
+}
+
+
+int
+main(int argc, char *argv[])
+{
+ int res, ret;
+ char *config_file;
+ int daemon_mode=0;
+ int port=0;
+
+
+
+ while ((res = getopt_long(argc, argv, "df:p:h", longopts, 0)) != -1)
+ {
+ switch (res)
+ {
+ case 'd':
+ daemon_mode = 1;
+ break;
+ case 'f':
+ config_file = optarg;
+ break;
+ case 'p':
+ port = atoi(optarg);
+ break;
+ case 'h':
+ default:
+ usage(argv[0]);
+ }
+ }
+
+ ret=init_nlsr();
+ ON_ERROR_EXIT(ret);
+
+ if ( port !=0 )
+ nlsr->api_port=port;
+
+ readConfigFile(config_file);
+ if ( daemon_mode == 1 )
+ {
+ daemonize_nlsr();
+ }
+
+ startLogging(nlsr->logDir);
+
+ nlsr->ccn=ccn_create();
+ int ccn_fd=ccn_connect(nlsr->ccn, NULL);
+ if(ccn_fd == -1)
+ {
+ fprintf(stderr,"Could not connect to ccnd\n");
+ writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
+ ON_ERROR_DESTROY(-1);
+ }
+
+ init_api_server(ccn_fd);
+
+ create_sync_slice(nlsr->topo_prefix, nlsr->slice_prefix);
+
+ struct ccn_charbuf *router_prefix;
+ router_prefix=ccn_charbuf_create();
+ res=ccn_name_from_uri(router_prefix,nlsr->router_name);
+ if(res<0)
+ {
+ fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
+ writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name);
+ ON_ERROR_DESTROY(res);
+ }
+
+ ccn_name_append_str(router_prefix,"nlsr");
+ nlsr->in_interest.data=nlsr->router_name;
+ res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
+ if ( res < 0 )
+ {
+ fprintf(stderr,"Failed to register interest for router\n");
+ writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n");
+ ON_ERROR_DESTROY(res);
+ }
+ ccn_charbuf_destroy(&router_prefix);
+
+ if ( nlsr->debugging )
+ printf("Router Name : %s\n",nlsr->router_name);
+ writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name);
+ if ( nlsr->debugging )
+ printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
+ writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
+
+ print_name_prefix_from_npl();
+ print_adjacent_from_adl();
+ build_and_install_name_lsas();
+ print_name_lsdb();
+
+ sync_monitor(nlsr->topo_prefix,nlsr->slice_prefix);
+
+ write_name_lsdb_to_repo(nlsr->slice_prefix);
+
+ //write_data_to_repo();
+
+ nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
+ //nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
+ //nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
+
+
+ while(1)
+ {
+ if ( nlsr->semaphor == NLSR_UNLOCKED )
+ {
+ if( nlsr->sched != NULL )
+ {
+ long int micro_sec=ccn_schedule_run(nlsr->sched);
+ res=nlsr_api_server_poll(micro_sec,ccn_fd);
+ ON_ERROR_DESTROY(res);
+ }
+ if(nlsr->ccn != NULL)
+ {
+ res = ccn_run(nlsr->ccn, 0);
+ }
+ if (!(nlsr->sched && nlsr->ccn))
+ {
+ break;
+ }
+ }
+
+ }
+
+
+ return 0;
+}
+