From e4eca848c8d95f48652096284fd8e2752ed1a316 Mon Sep 17 00:00:00 2001 From: michele papalini Date: Thu, 9 Jan 2020 14:55:32 +0100 Subject: [HICN-469] execute hicn-light-control from remote Signed-off-by: michele papalini Change-Id: I2a23a5f7648614213b83e2409e2ff3784b083758 Signed-off-by: michele papalini --- .../controller/hicnLightControl_main.c | 53 +++++++++++++++------- hicn-light/src/hicn/config/configurationFile.c | 3 +- hicn-light/src/hicn/config/controlRoot.c | 4 +- hicn-light/src/hicn/config/controlState.c | 16 +++---- hicn-light/src/hicn/config/controlState.h | 6 ++- 5 files changed, 52 insertions(+), 30 deletions(-) diff --git a/hicn-light/src/hicn/command_line/controller/hicnLightControl_main.c b/hicn-light/src/hicn/command_line/controller/hicnLightControl_main.c index 739326669..f27eb3ba5 100644 --- a/hicn-light/src/hicn/command_line/controller/hicnLightControl_main.c +++ b/hicn-light/src/hicn/command_line/controller/hicnLightControl_main.c @@ -14,6 +14,7 @@ */ #include +#include #ifndef _WIN32 #include @@ -48,6 +49,8 @@ #include +#include + size_t commandOutputLen = 0; // preserve the number of structs composing // payload in case on not interactive call. @@ -155,12 +158,12 @@ static void _displayUsage(char *programName) { printf("\n"); } -static int _parseArgs(int argc, char *argv[], char **keystorePath, - char **keystorePassword, PARCList *commandList) { +static int _parseArgs(int argc, char *argv[], char **server_ip, + uint16_t *server_port, PARCList *commandList){ static struct option longFormOptions[] = { {"help", no_argument, 0, 'h'}, - {"keystore", required_argument, 0, 'k'}, - {"password", required_argument, 0, 'p'}, + {"server", required_argument, 0, 'S'}, + {"port", required_argument, 0, 'P'}, {0, 0, 0, 0}}; int c; @@ -169,7 +172,7 @@ static int _parseArgs(int argc, char *argv[], char **keystorePath, // getopt_long stores the option index here. int optionIndex = 0; - c = getopt_long(argc, argv, "hk:p:", longFormOptions, &optionIndex); + c = getopt_long(argc, argv, "hS:P:", longFormOptions, &optionIndex); // Detect the end of the options. if (c == -1) { @@ -177,22 +180,33 @@ static int _parseArgs(int argc, char *argv[], char **keystorePath, } switch (c) { - case 'k': - *keystorePath = optarg; - break; - - case 'p': - *keystorePassword = optarg; - break; - + case 'S': + { + *server_ip = optarg; + struct sockaddr_in sa; + int result = inet_pton(AF_INET, *server_ip, &(sa.sin_addr)); + //inet_pton() returns 1 on success + if(result != 1){ + return 0; + } + break; + } + case 'P': + { + char * port_str = optarg; + if(utils_IsNumber(port_str)){ + *server_port = (uint16_t) strtol(port_str, NULL, 10); + } else { + return 0; + } + break; + } case 'h': default: - _displayUsage(argv[0]); return 0; } } - // Any remaining parameters get put in the command list. if (optind < argc) { while (optind < argc) { parcList_Add(commandList, argv[optind]); @@ -311,14 +325,19 @@ int main(int argc, char *argv[]) { PARCList *commands = parcList(parcArrayList_Create(NULL), PARCArrayListAsPARCList); - if (!_parseArgs(argc, argv, NULL, NULL, commands)) { + char *server_ip = SRV_CTRL_IP; + uint16_t server_port = SRV_CTRL_PORT; + if (!_parseArgs(argc, argv, &server_ip, + &server_port, commands)) { + _displayUsage(argv[0]); parcList_Release(&commands); exit(EXIT_FAILURE); } ControlMainState mainState; mainState.controlState = - controlState_Create(&mainState, _writeAndReadMessage, true); + controlState_Create(&mainState, _writeAndReadMessage, true, + server_ip, server_port); controlState_RegisterCommand(mainState.controlState, controlRoot_HelpCreate(mainState.controlState)); diff --git a/hicn-light/src/hicn/config/configurationFile.c b/hicn-light/src/hicn/config/configurationFile.c index adc089396..3cce740ac 100644 --- a/hicn-light/src/hicn/config/configurationFile.c +++ b/hicn-light/src/hicn/config/configurationFile.c @@ -210,7 +210,8 @@ ConfigurationFile *configurationFile_Create(Forwarder *forwarder, // because // writeRead still not implemented from configuration file. configFile->controlState = - controlState_Create(configFile, _writeRead, false); + controlState_Create(configFile, _writeRead, false, + SRV_CTRL_IP, SRV_CTRL_PORT); // we do not register Help commands controlState_RegisterCommand(configFile->controlState, diff --git a/hicn-light/src/hicn/config/controlRoot.c b/hicn-light/src/hicn/config/controlRoot.c index e135dfc50..8c8cc1804 100644 --- a/hicn-light/src/hicn/config/controlRoot.c +++ b/hicn-light/src/hicn/config/controlRoot.c @@ -62,11 +62,11 @@ static CommandReturn _controlRoot_HelpExecute(CommandParser *parser, CommandOps *ops, PARCList *args) { printf("Command-line execution:\n"); printf( - " controller [--keystore ] [--password ] " + " controller [--server ] [--port ] " "command\n"); printf("\n"); printf("Interactive execution:\n"); - printf(" controller [--keystore ] [--password ]\n"); + printf(" controller [--server ] [--port ]\n"); printf("\n"); printf( "If the keystore is not specified, the default path is used. Keystore " diff --git a/hicn-light/src/hicn/config/controlState.c b/hicn-light/src/hicn/config/controlState.c index 6cd1b905d..2df8805c6 100644 --- a/hicn-light/src/hicn/config/controlState.c +++ b/hicn-light/src/hicn/config/controlState.c @@ -37,9 +37,6 @@ #include -#define SRV_IP "127.0.0.1" -#define PORT 9695 - struct controller_state { CommandParser *parser; bool debugFlag; @@ -51,7 +48,7 @@ struct controller_state { bool isInteractive; }; -int controlState_connectToFwdDeamon() { +int controlState_connectToFwdDeamon(char *server_ip, uint16_t port) { int sockfd; struct sockaddr_in servaddr; @@ -64,8 +61,8 @@ int controlState_connectToFwdDeamon() { // Filling server information servaddr.sin_family = AF_INET; - servaddr.sin_port = htons(PORT); - inet_pton(AF_INET, SRV_IP, &(servaddr.sin_addr.s_addr)); + servaddr.sin_port = htons(port); + inet_pton(AF_INET, server_ip, &(servaddr.sin_addr.s_addr)); // Establish connection if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { @@ -79,7 +76,8 @@ int controlState_connectToFwdDeamon() { ControlState *controlState_Create( void *userdata, struct iovec *(*writeRead)(ControlState *state, struct iovec *msg), - bool openControllerConnetion) { + bool openControllerConnetion, + char *server_ip, uint16_t port) { ControlState *state = parcMemory_AllocateAndClear(sizeof(ControlState)); parcAssertNotNull(state, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(ControlState)); @@ -92,7 +90,7 @@ ControlState *controlState_Create( state->isInteractive = true; if (openControllerConnetion) { - state->sockfd = controlState_connectToFwdDeamon(); + state->sockfd = controlState_connectToFwdDeamon(server_ip, port); } else { state->sockfd = 2; // stderr } @@ -235,4 +233,4 @@ bool controlState_isConfigFile(ControlState *state) { } else { return true; } -} \ No newline at end of file +} diff --git a/hicn-light/src/hicn/config/controlState.h b/hicn-light/src/hicn/config/controlState.h index 25542657f..cc38cbe37 100644 --- a/hicn-light/src/hicn/config/controlState.h +++ b/hicn-light/src/hicn/config/controlState.h @@ -32,6 +32,9 @@ #include +#define SRV_CTRL_IP "127.0.0.1" +#define SRV_CTRL_PORT 9695 + struct controller_state; typedef struct controller_state ControlState; @@ -59,7 +62,8 @@ typedef struct controller_state ControlState; ControlState *controlState_Create( void *userdata, struct iovec *(*writeRead)(ControlState *state, struct iovec *msg), - bool openControllerConnetion); + bool openControllerConnetion, + char * server_ip, uint16_t port); /** * Destroys the control state, closing all network connections -- cgit 1.2.3-korg