From f5a0b8a5e24cede05e15ab696f0e15257a503525 Mon Sep 17 00:00:00 2001 From: Angelo Mantellini Date: Thu, 31 Jan 2019 18:20:48 +0100 Subject: [HICN24] Windows compatibility for hicn-light Change-Id: I8e19e52c9b4ec0fcbd7344c28765f5da1937569c Signed-off-by: Angelo Mantellini --- .../src/command_line/daemon/hicnLightDaemon_main.c | 56 +++++++++++++++++----- 1 file changed, 44 insertions(+), 12 deletions(-) (limited to 'hicn-light/src/command_line/daemon/hicnLightDaemon_main.c') 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 +#endif #include #include #include @@ -21,9 +24,7 @@ #include #include #include -#include #include -#include #include #include @@ -35,21 +36,27 @@ #include #include -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; -- cgit 1.2.3-korg