API modified to Connect Remote Host
diff --git a/nlsr.c b/nlsr.c
index 06cc75f..d1f98e0 100644
--- a/nlsr.c
+++ b/nlsr.c
@@ -12,6 +12,9 @@
#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>
@@ -54,6 +57,7 @@
{
{ "daemon", no_argument, NULL, 'd'},
{ "config_file", required_argument, NULL, 'f'},
+ { "api_port", required_argument, NULL, 'p'},
{ "help", no_argument, NULL, 'h'},
{ 0 }
};
@@ -66,6 +70,7 @@
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);
@@ -735,7 +740,7 @@
int client_sockfd;
char recv_buffer[1024];
bzero(recv_buffer,1024);
- struct sockaddr_un client_address;
+ struct sockaddr_in client_address;
testfds=nlsr->readfds;
result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
@@ -820,7 +825,9 @@
hashtb_end(e);
hashtb_destroy(&nlsr->npt);
-
+
+ close(nlsr->nlsr_api_server_sock_fd);
+
ccn_schedule_destroy(&nlsr->sched);
ccn_destroy(&nlsr->ccn);
@@ -842,16 +849,21 @@
{
int server_sockfd;
int server_len;
- struct sockaddr_un server_address;
+ struct sockaddr_in server_address;
- unlink("/tmp/nlsr_api_server_socket");
- server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
+ //unlink("/tmp/nlsr_api_server_socket");
+ server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
int flags = fcntl(server_sockfd, F_GETFL, 0);
fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
- server_address.sun_family = AF_UNIX;
- strcpy(server_address.sun_path, "/tmp/nlsr_api_server_socket");
+ //server_address.sun_family = AF_UNIX;
+ //strcpy(server_address.sun_path, "/tmp/nlsr_api_server_socket");
+
+ 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);
@@ -933,6 +945,8 @@
nlsr->multi_path_face_num=MULTI_PATH_FACE_NUM;
nlsr->semaphor=NLSR_UNLOCKED;
+ nlsr->api_port=API_PORT;
+
return 0;
}
@@ -943,10 +957,11 @@
int res, ret;
char *config_file;
int daemon_mode=0;
+ int port=0;
- while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1)
+ while ((res = getopt_long(argc, argv, "df:p:h", longopts, 0)) != -1)
{
switch (res)
{
@@ -956,6 +971,9 @@
case 'f':
config_file = optarg;
break;
+ case 'p':
+ port = atoi(optarg);
+ break;
case 'h':
default:
usage(argv[0]);
@@ -964,8 +982,11 @@
ret=init_nlsr();
ON_ERROR_EXIT(ret);
+
+ if ( port !=0 )
+ nlsr->api_port=port;
+
readConfigFile(config_file);
-
if ( daemon_mode == 1 )
{
daemonize_nlsr();
diff --git a/nlsr.h b/nlsr.h
index 69e82dd..a53cc2b 100644
--- a/nlsr.h
+++ b/nlsr.h
@@ -16,6 +16,8 @@
#define NAME_LSA_VALID 1
#define NAME_LSA_INVALID 0
+#define API_PORT 9696
+
struct name_prefix
{
@@ -86,6 +88,7 @@
int nlsr_api_server_sock_fd;
fd_set readfds;
+ int api_port;
};
diff --git a/nlsrc.c b/nlsrc.c
index 20e199f..44ce2ad 100755
--- a/nlsrc.c
+++ b/nlsrc.c
@@ -6,13 +6,24 @@
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+#include <getopt.h>
+
+struct option longopts[] =
+{
+ { "server_ip", required_argument, NULL, 's'},
+ { "server_port", required_argument, NULL, 'p'},
+ { 0 }
+};
static int
usage(char *progname)
{
printf("Usage: %s [OPTIONS...]\n\
NLSR Api client....\n\
- nlsrc add|del name|neighbor name_prefix [faceX] \n\
+ nlsrc -s server_ip -p server_port add|del name|neighbor name_prefix [faceX] \n\
add/del, -- adding/deleting operation\n\
name/neighbor, -- Operation for name/neighbor\n\
name_name, -- Name prefix for name/neighbor\n\
@@ -25,35 +36,54 @@
{
int sockfd;
int len;
- struct sockaddr_un address;
+ struct sockaddr_in address;
int result;
int byteSend;
-
+ char *server_address, *server_port;
+
int command_len=0;
int i;
- if (argc < 4 )
+ if (argc < 8 )
usage(argv[0]);
- if ( strcmp(argv[2],"neighbor") == 0 && argc <5 )
+ if ( strcmp(argv[6],"neighbor") == 0 && argc <9 )
usage(argv[0]);
+ while ((result = getopt_long(argc, argv, "s:p:", longopts, 0)) != -1)
+ {
+ switch (result)
+ {
+ case 's':
+ server_address = optarg;
+ break;
+ case 'p':
+ server_port = optarg;
+ break;
+ }
+ }
+
+
char recv_buffer[1024];
bzero(recv_buffer,1024);
- for(i=1;i<argc;i++)
+ for(i=5;i<argc;i++)
command_len+=(strlen(argv[i])+1);
char *command=malloc(command_len);
memset(command,command_len+1,0);
- for(i=1;i<argc;i++)
+ for(i=5;i<argc;i++)
{
memcpy(command+strlen(command),argv[i],strlen(argv[i]));
if ( i < argc-1 )
memcpy(command+strlen(command)," ",1);
}
- sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
- address.sun_family = AF_UNIX;
- strcpy(address.sun_path, "/tmp/nlsr_api_server_socket");
+ sockfd = socket(AF_INET, SOCK_STREAM, 0);
+ //address.sun_family = AF_UNIX;
+ //strcpy(address.sun_path, "/tmp/nlsr_api_server_socket");
+ address.sin_family = AF_INET;
+ address.sin_addr.s_addr = inet_addr(server_address);
+ address.sin_port = atoi(server_port);
+
len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
if(result == -1)