summaryrefslogtreecommitdiffstats
path: root/hicn-light/src/command_line
diff options
context:
space:
mode:
Diffstat (limited to 'hicn-light/src/command_line')
-rw-r--r--hicn-light/src/command_line/controller/hicnLightControl_main.c57
-rw-r--r--hicn-light/src/command_line/daemon/hicnLightDaemon_main.c56
2 files changed, 83 insertions, 30 deletions
diff --git a/hicn-light/src/command_line/controller/hicnLightControl_main.c b/hicn-light/src/command_line/controller/hicnLightControl_main.c
index 4641bddf5..03f74692f 100644
--- a/hicn-light/src/command_line/controller/hicnLightControl_main.c
+++ b/hicn-light/src/command_line/controller/hicnLightControl_main.c
@@ -15,13 +15,18 @@
#include <src/config.h>
+#ifndef _WIN32
+#include <arpa/inet.h>
#include <getopt.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <sys/uio.h>
+#include <unistd.h>
+#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
-#include <strings.h>
-#include <unistd.h>
#include <parc/assert/parc_Assert.h>
#include <string.h>
@@ -71,22 +76,17 @@ static int payloadLengthController[LAST_COMMAND_VALUE] = {
sizeof(mapme_timing_command),
sizeof(mapme_timing_command)};
-#include <arpa/inet.h>
-#include <netinet/in.h>
-#include <sys/socket.h>
-#include <sys/uio.h>
-
typedef struct controller_main_state {
ControlState *controlState;
} ControlMainState;
-static void _displayForwarderLogo(void){
- const char cli_banner [] =
- "\033[0;31m ____ ___ _ \033[0m __ _ __ _ __ __\n"
- "\033[0;31m / __// _ \\ (_)___ \033[0m / / (_)____ ___ ____/ /(_)___ _ / / / /_\n"
- "\033[0;31m / _/ / // /_ / // _ \\ \033[0m / _ \\ / // __// _ \\___/ // // _ `// _ \\/ __/\n"
- "\033[0;31m/_/ /____/(_)/_/ \\___/ \033[0m/_//_//_/ \\__//_//_/ /_//_/ \\_, //_//_/\\__/\n"
- " /___/ \n";
+static void _displayForwarderLogo(void) {
+ const char cli_banner[] =
+ "\033[0;31m ____ ___ _ \033[0m __ _ __ _ __ __\n"
+ "\033[0;31m / __// _ \\ (_)___ \033[0m / / (_)____ ___ ____/ /(_)___ _ / / / /_\n"
+ "\033[0;31m / _/ / // /_ / // _ \\ \033[0m / _ \\ / // __// _ \\___/ // // _ `// _ \\/ __/\n"
+ "\033[0;31m/_/ /____/(_)/_/ \\___/ \033[0m/_//_//_/ \\__//_//_/ /_//_/ \\_, //_//_/\\__/\n"
+ " /___/ \n";
printf("%s", cli_banner);
printf("\n");
}
@@ -165,15 +165,25 @@ struct iovec *_writeAndReadMessage(ControlState *state, struct iovec *msg) {
0) { // command with payload
// write header + payload (compatibility issue: two write needed instead of
// the writev)
- if (write(sockfd, msg[0].iov_base, msg[0].iov_len) < 0 ||
- write(sockfd, msg[1].iov_base, msg[1].iov_len) < 0) {
+#ifndef _WIN32
+ if (write(sockfd, msg[0].iov_base, (unsigned int)msg[0].iov_len) < 0 ||
+ write(sockfd, msg[1].iov_base, (unsigned int)msg[1].iov_len) < 0) {
+#else
+ if (send(sockfd, msg[0].iov_base, msg[0].iov_len, 0) == SOCKET_ERROR ||
+ send(sockfd, msg[1].iov_base, msg[1].iov_len, 0) == SOCKET_ERROR) {
+#endif
printf("\nError while sending the Message: cannot write on socket \n");
exit(EXIT_FAILURE);
}
parcMemory_Deallocate(&msg[1].iov_base);
} else { // command without payload, e.g. 'list'
- // write header only
+ // write header only
+#ifndef _WIN32
if (write(sockfd, msg[0].iov_base, msg[0].iov_len) < 0) {
+#else
+ int result = send(sockfd, msg[0].iov_base, msg[0].iov_len, 0);
+ if (result == SOCKET_ERROR) {
+#endif
printf("\nError while sending the Message: cannot write on socket \n");
exit(EXIT_FAILURE);
}
@@ -193,8 +203,12 @@ struct iovec *_writeAndReadMessage(ControlState *state, struct iovec *msg) {
if (headerResponse->messageType < RESPONSE_LIGHT ||
headerResponse->messageType >= LAST_MSG_TYPE_VALUE) {
char *checkFinMsg = parcMemory_Reallocate(headerResponse, 32);
+#ifndef _WIN32
if (recv(sockfd, checkFinMsg, sizeof(checkFinMsg),
MSG_PEEK | MSG_DONTWAIT) == 0) {
+#else
+ if (recv(sockfd, checkFinMsg, sizeof(checkFinMsg), MSG_PEEK) == 0) {
+#endif
// if recv returns zero, that means the connection has been closed:
close(sockfd);
printf("\nConnection terminated by the Daemon. Exiting... \n");
@@ -236,6 +250,11 @@ struct iovec *_writeAndReadMessage(ControlState *state, struct iovec *msg) {
int main(int argc, char *argv[]) {
_displayForwarderLogo();
+#ifdef _WIN32
+ WSADATA wsaData;
+ WSAStartup(MAKEWORD(2, 2), &wsaData);
+#endif
+
if (argc == 2 && strcmp("-h", argv[1]) == 0) {
_displayUsage(argv[0]);
exit(EXIT_SUCCESS);
@@ -279,6 +298,8 @@ int main(int argc, char *argv[]) {
parcList_Release(&commands);
controlState_Destroy(&mainState.controlState);
-
+#ifdef _WIN32
+ WSACleanup();
+#endif
return EXIT_SUCCESS;
}
diff --git a/hicn-light/src/command_line/daemon/hicnLightDaemon_main.c b/hicn-light/src/command_line/daemon/hicnLightDaemon_main.c
index 415e98d65..1b7e92e75 100644
--- a/hicn-light/src/command_line/daemon/hicnLightDaemon_main.c
+++ b/hicn-light/src/command_line/daemon/hicnLightDaemon_main.c
@@ -13,6 +13,9 @@
* limitations under the License.
*/
+#ifndef _WIN32
+#include <unistd.h>
+#endif
#include <errno.h>
#include <fcntl.h>
#include <src/config.h>
@@ -21,9 +24,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <strings.h>
#include <sys/stat.h>
-#include <unistd.h>
#include <parc/algol/parc_FileOutputStream.h>
#include <parc/logging/parc_LogLevel.h>
@@ -35,21 +36,27 @@
#include <src/core/dispatcher.h>
#include <src/core/forwarder.h>
-static void _displayForwarderLogo(void){
- const char cli_banner [] =
- "\033[0;31m ____ ___ _ \033[0m __ _ __ _ __ __\n"
- "\033[0;31m / __// _ \\ (_)___ \033[0m / / (_)____ ___ ____/ /(_)___ _ / / / /_\n"
- "\033[0;31m / _/ / // /_ / // _ \\ \033[0m / _ \\ / // __// _ \\___/ // // _ `// _ \\/ __/\n"
- "\033[0;31m/_/ /____/(_)/_/ \\___/ \033[0m/_//_//_/ \\__//_//_/ /_//_/ \\_, //_//_/\\__/\n"
- " /___/ \n";
+static void _displayForwarderLogo(void) {
+ const char cli_banner[] =
+ "\033[0;31m ____ ___ _ \033[0m __ _ __ _ __ __\n"
+ "\033[0;31m / __// _ \\ (_)___ \033[0m / / (_)____ ___ ____/ /(_)___ _ / / / /_\n"
+ "\033[0;31m / _/ / // /_ / // _ \\ \033[0m / _ \\ / // __// _ \\___/ // // _ `// _ \\/ __/\n"
+ "\033[0;31m/_/ /____/(_)/_/ \\___/ \033[0m/_//_//_/ \\__//_//_/ /_//_/ \\_, //_//_/\\__/\n"
+ " /___/ \n";
printf("%s", cli_banner);
printf("\n");
}
static void _usage(int exitCode) {
+#ifndef _WIN32
+ printf(
+ "Usage: daemon [--port port] [--capacity objectStoreSize] "
+ "[--log facility=level] [--log-file filename] [--config file]\n");
+#else
printf(
"Usage: daemon [--port port] [--daemon] [--capacity objectStoreSize] "
"[--log facility=level] [--log-file filename] [--config file]\n");
+#endif
printf("\n");
printf(
"hicn-light run as a daemon is the program to launch the forwarder, "
@@ -79,7 +86,9 @@ static void _usage(int exitCode) {
printf("\n");
printf("Options:\n");
printf("--port = tcp port for in-bound connections\n");
+#ifndef _WIN32
printf("--daemon = start as daemon process\n");
+#endif
printf("--objectStoreSize = maximum number of content objects to cache\n");
printf(
"--log = sets a facility to a given log level. You can have "
@@ -122,9 +131,9 @@ static void _setLogLevel(int logLevelArray[LoggerFacility_END],
char *tofree = parcMemory_StringDuplicate(string, strlen(string));
char *p = tofree;
- char *facilityString = strsep(&p, "=");
+ char *facilityString = strtok(p, "=");
if (facilityString) {
- char *levelString = p;
+ char *levelString = strtok(NULL, "=");
if (strcasecmp(facilityString, "all") == 0) {
for (LoggerFacility facility = 0; facility < LoggerFacility_END;
@@ -151,6 +160,7 @@ static void _setLogLevel(int logLevelArray[LoggerFacility_END],
parcMemory_Deallocate((void **)&tofree);
}
+#ifndef _WIN32
static void _daemonize(void) {
if (getppid() == 1) {
// already a daemon
@@ -195,16 +205,24 @@ static void _daemonize(void) {
// forwarder will capture signals
}
+#endif
static Logger *_createLogfile(const char *logfile) {
+#ifndef _WIN32
int logfd = open(logfile, O_WRONLY | O_APPEND | O_CREAT, S_IWUSR | S_IRUSR);
+#else
+ int logfd =
+ _open(logfile, _O_WRONLY | _O_APPEND | _O_CREAT, _S_IWRITE | _S_IREAD);
+#endif
if (logfd < 0) {
fprintf(stderr, "Error opening %s for writing: (%d) %s\n", logfile, errno,
strerror(errno));
exit(EXIT_FAILURE);
}
+#ifndef _WIN32
chmod(logfile, S_IRWXU);
+#endif
PARCFileOutputStream *fos = parcFileOutputStream_Create(logfd);
PARCOutputStream *pos = parcFileOutputStream_AsOutputStream(fos);
@@ -220,10 +238,15 @@ static Logger *_createLogfile(const char *logfile) {
int main(int argc, const char *argv[]) {
_displayForwarderLogo();
+#ifndef _WIN32
+ bool daemon = false;
+#else
+ WSADATA wsaData = {0};
+ WSAStartup(MAKEWORD(2, 2), &wsaData);
+#endif
uint16_t port = PORT_NUMBER;
uint16_t configurationPort = 2001;
- bool daemon = false;
int capacity = -1;
const char *configFileName = NULL;
@@ -246,8 +269,10 @@ int main(int argc, const char *argv[]) {
} else if (strcmp(argv[i], "--port") == 0) {
port = atoi(argv[i + 1]);
i++;
+#ifndef _WIN32
} else if (strcmp(argv[i], "--daemon") == 0) {
daemon = true;
+#endif
} else if (strcmp(argv[i], "--capacity") == 0 ||
strcmp(argv[i], "-c") == 0) {
capacity = atoi(argv[i + 1]);
@@ -273,6 +298,7 @@ int main(int argc, const char *argv[]) {
// set restrictive umask, in case we create any files
umask(027);
+#ifndef _WIN32
if (daemon && (logfile == NULL)) {
fprintf(stderr, "Must specify a logfile when running in daemon mode\n");
_usage(EXIT_FAILURE);
@@ -282,6 +308,7 @@ int main(int argc, const char *argv[]) {
// inside this call, parent will EXIT_SUCCESS and child will continue
_daemonize();
}
+#endif
Logger *logger = NULL;
if (logfile) {
@@ -329,7 +356,12 @@ int main(int argc, const char *argv[]) {
forwarder_Destroy(&forwarder);
+#ifndef _WIN32
sleep(2);
+#else
+ Sleep(2000);
+ WSACleanup();
+#endif
logger_Release(&logger);
return 0;