From 55da9787a48dad4177d738c1b0067df54ff31690 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Thu, 3 Nov 2011 10:51:26 +0100 Subject: [PATCH] Win32 compile fixes. --- src/alert-prelude.c | 6 +++--- src/alert-syslog.c | 4 ++++ src/conf-yaml-loader.c | 2 +- src/conf.c | 22 +++++++++++++++++++- src/conf.h | 4 ++++ src/defrag.c | 2 +- src/suricata-common.h | 9 +++++++++ src/suricata.c | 28 ++++++++++++++++++++++++- src/util-affinity.c | 2 +- src/util-byte.h | 2 +- src/util-mem.h | 46 +++++++++++++++++++++++++++++++++++++++--- src/util-proto-name.c | 23 +++++++++++++++++---- src/util-proto-name.h | 6 +++++- src/win32-misc.c | 5 +++-- src/win32-misc.h | 6 ++---- 15 files changed, 144 insertions(+), 23 deletions(-) diff --git a/src/alert-prelude.c b/src/alert-prelude.c index 70733bd402..80dac6415d 100644 --- a/src/alert-prelude.c +++ b/src/alert-prelude.c @@ -224,13 +224,13 @@ static int EventToImpact(PacketAlert *pa, Packet *p, idmef_alert_t *alert) if ( ret < 0 ) SCReturnInt(ret); - if ( (uint)pa->s->prio < mid_priority ) + if ( (unsigned int)pa->s->prio < mid_priority ) severity = IDMEF_IMPACT_SEVERITY_HIGH; - else if ( (uint)pa->s->prio < low_priority ) + else if ( (unsigned int)pa->s->prio < low_priority ) severity = IDMEF_IMPACT_SEVERITY_MEDIUM; - else if ( (uint)pa->s->prio < info_priority ) + else if ( (unsigned int)pa->s->prio < info_priority ) severity = IDMEF_IMPACT_SEVERITY_LOW; else diff --git a/src/alert-syslog.c b/src/alert-syslog.c index aa59802bd9..78bdfd4010 100644 --- a/src/alert-syslog.c +++ b/src/alert-syslog.c @@ -55,7 +55,9 @@ #define MODULE_NAME "AlertSyslog" extern uint8_t engine_mode; +#ifndef OS_WIN32 static int alert_syslog_level = DEFAULT_ALERT_SYSLOG_LEVEL; +#endif /* OS_WIN32 */ typedef struct AlertSyslogThread_ { /** LogFileCtx has the pointer to the file and a mutex to allow multithreading */ @@ -70,7 +72,9 @@ TmEcode AlertSyslogThreadDeinit(ThreadVars *, void *); void AlertSyslogExitPrintStats(ThreadVars *, void *); void AlertSyslogRegisterTests(void); OutputCtx *AlertSyslogInitCtx(ConfNode *); +#ifndef OS_WIN32 static void AlertSyslogDeInitCtx(OutputCtx *); +#endif /* OS_WIN32 */ /** \brief Function to register the AlertSyslog module */ void TmModuleAlertSyslogRegister (void) { diff --git a/src/conf-yaml-loader.c b/src/conf-yaml-loader.c index e5d71d6aee..7875436d3d 100644 --- a/src/conf-yaml-loader.c +++ b/src/conf-yaml-loader.c @@ -63,7 +63,7 @@ ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq) while (!done) { if (!yaml_parser_parse(parser, &event)) { fprintf(stderr, - "Failed to parse configuration file at line %zu: %s\n", + "Failed to parse configuration file at line %" PRIu32 ": %s\n", parser->problem_mark.line, parser->problem); return -1; } diff --git a/src/conf.c b/src/conf.c index 075281fb11..beeaa489bb 100644 --- a/src/conf.c +++ b/src/conf.c @@ -121,18 +121,29 @@ ConfNode * ConfGetNode(char *key) { ConfNode *node = root; +#if !defined(__WIN32) && !defined(_WIN32) char *saveptr = NULL; +#endif /* __WIN32 */ char *token; /* Need to dup the key for tokenization... */ char *tokstr = SCStrdup(key); +#if defined(__WIN32) || defined(_WIN32) + token = strtok(tokstr, "."); +#else token = strtok_r(tokstr, ".", &saveptr); +#endif /* __WIN32 */ for (;;) { node = ConfNodeLookupChild(node, token); if (node == NULL) break; + +#if defined(__WIN32) || defined(_WIN32) + token = strtok(NULL, "."); +#else token = strtok_r(NULL, ".", &saveptr); +#endif /* __WIN32 */ if (token == NULL) break; } @@ -164,8 +175,9 @@ ConfSet(char *name, char *val, int allow_override) ConfNode *parent = root; ConfNode *node; char *token; +#if !defined(__WIN32) && !defined(_WIN32) char *saveptr = NULL; - +#endif /* __WIN32 */ /* First check if the node already exists. */ node = ConfGetNode(name); if (node != NULL) { @@ -182,7 +194,11 @@ ConfSet(char *name, char *val, int allow_override) } else { char *tokstr = SCStrdup(name); +#if defined(__WIN32) || defined(_WIN32) + token = strtok(tokstr, "."); +#else token = strtok_r(tokstr, ".", &saveptr); +#endif /* __WIN32 */ node = ConfNodeLookupChild(parent, token); for (;;) { if (node == NULL) { @@ -195,7 +211,11 @@ ConfSet(char *name, char *val, int allow_override) else { parent = node; } +#if defined(__WIN32) || defined(_WIN32) + token = strtok(NULL, "."); +#else token = strtok_r(NULL, ".", &saveptr); +#endif /* __WIN32 */ if (token == NULL) { if (!node->allow_override) break; diff --git a/src/conf.h b/src/conf.h index 92c1f99502..9dad409a09 100644 --- a/src/conf.h +++ b/src/conf.h @@ -45,7 +45,11 @@ typedef struct ConfNode_ { /** * The default log directory. */ +#ifdef OS_WIN32 +#define DEFAULT_LOG_DIR "C:\\WINDOWS\\Temp" +#else #define DEFAULT_LOG_DIR "/var/log/suricata" +#endif /* OS_WIN32 */ void ConfInit(void); void ConfDeInit(void); diff --git a/src/defrag.c b/src/defrag.c index 066dc60432..4c69da81c4 100644 --- a/src/defrag.c +++ b/src/defrag.c @@ -1024,7 +1024,7 @@ DefragTimeoutTracker(ThreadVars *tv, DecodeThreadVars *dtv, DefragContext *dc, while (next != NULL) { tracker = HashListTableGetListData(next); - if (tracker->timeout < (uint)p->ts.tv_sec) { + if (tracker->timeout < (unsigned int)p->ts.tv_sec) { /* Tracker has timeout out. */ HashListTableRemove(dc->frag_table, tracker, sizeof(tracker)); DefragTrackerReset(tracker); diff --git a/src/suricata-common.h b/src/suricata-common.h index 698d2c1c95..0fda536c09 100644 --- a/src/suricata-common.h +++ b/src/suricata-common.h @@ -154,6 +154,15 @@ #endif #endif +/** Windows does not define __WORDSIZE, but it uses __X86__ */ + #if defined(__X86__) || defined(_X86_) + #define __WORDSIZE 32 + #else + #if defined(__X86_64__) || defined(_X86_64_) + #define __WORDSIZE 64 + #endif + #endif + #ifndef __WORDSIZE #warning Defaulting to __WORDSIZE 32 #define __WORDSIZE 32 diff --git a/src/suricata.c b/src/suricata.c index 2a957d2a2c..2d5648bab0 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -327,11 +327,19 @@ static void SetBpfStringFromFile(char *filename) { char *bpf_comment_tmp = NULL; char *bpf_comment_start = NULL; uint32_t bpf_len = 0; +#ifdef OS_WIN32 + struct _stat st; +#else struct stat st; +#endif /* OS_WIN32 */ FILE *fp = NULL; size_t nm = 0; +#ifdef OS_WIN32 + if(_stat(filename, &st) != 0) { +#else if(stat(filename, &st) != 0) { +#endif /* OS_WIN32 */ SCLogError(SC_ERR_FOPEN, "Failed to stat file %s", filename); exit(EXIT_FAILURE); } @@ -579,16 +587,22 @@ int main(int argc, char **argv) int list_runmodes = 0; const char *runmode_custom_mode = NULL; int daemon = 0; +#ifndef OS_WIN32 char *user_name = NULL; char *group_name = NULL; uint8_t do_setuid = FALSE; uint8_t do_setgid = FALSE; uint32_t userid = 0; uint32_t groupid = 0; +#endif /* OS_WIN32 */ int build_info = 0; char *log_dir; +#ifdef OS_WIN32 + struct _stat buf; +#else struct stat buf; +#endif /* OS_WIN32 */ sc_set_caps = FALSE; @@ -1083,9 +1097,21 @@ int main(int argc, char **argv) /* Check for the existance of the default logging directory which we pick * from suricata.yaml. If not found, shut the engine down */ - if (ConfGet("default-log-dir", &log_dir) != 1) + if (ConfGet("default-log-dir", &log_dir) != 1) { +#ifdef OS_WIN32 + log_dir = _getcwd(NULL, 0); + if (log_dir == NULL) { + log_dir = DEFAULT_LOG_DIR; + } +#else log_dir = DEFAULT_LOG_DIR; +#endif /* OS_WIN32 */ + } +#ifdef OS_WIN32 + if (_stat(log_dir, &buf) != 0) { +#else if (stat(log_dir, &buf) != 0) { +#endif /* OS_WIN32 */ SCLogError(SC_ERR_LOGDIR_CONFIG, "The logging directory \"%s\" " "supplied by %s (default-log-dir) doesn't exist. " "Shutting down the engine", log_dir, conf_filename); diff --git a/src/util-affinity.c b/src/util-affinity.c index 97c72d5f33..5a102e30f8 100644 --- a/src/util-affinity.c +++ b/src/util-affinity.c @@ -292,9 +292,9 @@ void AffinitySetupLoadFromConfig() int AffinityGetNextCPU(ThreadsAffinityType *taf) { int ncpu = 0; - int iter = 0; #if !defined OS_WIN32 && !defined __OpenBSD__ + int iter = 0; SCMutexLock(&taf->taf_mutex); ncpu = taf->lcpu; while (!CPU_ISSET(ncpu, &taf->cpu_set) && iter < 2) { diff --git a/src/util-byte.h b/src/util-byte.h index 443bcd1ab4..8175e5ca63 100644 --- a/src/util-byte.h +++ b/src/util-byte.h @@ -45,7 +45,7 @@ #define SCByteSwap16(x) OSSwapInt16(x) #define SCByteSwap32(x) OSSwapInt32(x) #define SCByteSwap64(x) OSSwapInt64(x) -#elif OS_WIN32 +#elif defined(__WIN32) || defined(_WIN32) /* Quick & dirty solution, nothing seems to exist for this in Win32 API */ #define SCByteSwap16(x) \ ((((x) & 0xff00) >> 8) \ diff --git a/src/util-mem.h b/src/util-mem.h index 6f2414658c..33d664875b 100644 --- a/src/util-mem.h +++ b/src/util-mem.h @@ -31,6 +31,11 @@ #define __UTIL_MEM_H__ #include "util-atomic.h" + +#if defined(_WIN32) || defined(__WIN32) +#include "mm_malloc.h" +#endif + SC_ATOMIC_EXTERN(unsigned int, engine_stage); /* Use this only if you want to debug memory allocation and free() @@ -207,6 +212,12 @@ SC_ATOMIC_EXTERN(unsigned int, engine_stage); (void*)ptrmem; \ }) +#define SCFree(a) ({ \ + free(a); \ +}) + +#if defined(__WIN32) || defined(_WIN32) + /** \brief wrapper for allocing aligned mem * \param a size * \param b alignement @@ -214,7 +225,8 @@ SC_ATOMIC_EXTERN(unsigned int, engine_stage); #define SCMallocAligned(a, b) ({ \ void *ptrmem = NULL; \ \ - if (posix_memalign(&ptrmem, (b), (a)) != 0) { \ + ptrmem = _mm_malloc((a), (b)); \ + if (ptrmem == NULL) { \ if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\ SCLogError(SC_ERR_MEM_ALLOC, "SCMallocAligned(posix_memalign) failed: %s, while trying " \ "to allocate %"PRIuMAX" bytes, alignment %"PRIuMAX, strerror(errno), (uintmax_t)(a), (uintmax_t)(b)); \ @@ -225,8 +237,35 @@ SC_ATOMIC_EXTERN(unsigned int, engine_stage); (void*)ptrmem; \ }) -#define SCFree(a) ({ \ - free((a)); \ +/** \brief Free aligned memory + * + * Not needed for mem alloc'd by posix_memalign, + * but for possible future use of _mm_malloc needing + * _mm_free. + */ +#define SCFreeAligned(a) ({ \ + _mm_free(a); \ +}) + +#else /* !win */ + +/** \brief wrapper for allocing aligned mem + * \param a size + * \param b alignement + */ +#define SCMallocAligned(a, b) ({ \ + void *ptrmem = NULL; \ + \ + ptrmem = _mm_malloc((a), (b)); \ + if (ptrmem == NULL) { \ + if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\ + SCLogError(SC_ERR_MEM_ALLOC, "SCMallocAligned(posix_memalign) failed: %s, while trying " \ + "to allocate %"PRIuMAX" bytes, alignment %"PRIuMAX, strerror(errno), (uintmax_t)a, (uintmax_t)b); \ + SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \ + exit(EXIT_FAILURE); \ + } \ + } \ + (void*)ptrmem; \ }) /** \brief Free aligned memory @@ -239,6 +278,7 @@ SC_ATOMIC_EXTERN(unsigned int, engine_stage); free((a)); \ }) +#endif /* __WIN32 */ #endif /* DBG_MEM_ALLOC */ diff --git a/src/util-proto-name.c b/src/util-proto-name.c index d7fc97a9e9..2d1ec2dc22 100644 --- a/src/util-proto-name.c +++ b/src/util-proto-name.c @@ -37,17 +37,27 @@ void SCProtoNameInit() FILE *fp = fopen(PROTO_FILE,"r"); if (fp != NULL) { char line[200]; +#if !defined(__WIN32) && !defined(_WIN32) char *ptr = NULL; +#endif /* __WIN32 */ while(fgets(line, sizeof(line), fp) != NULL) { if (line[0] == '#') continue; - char *name = strtok_r(line," \t", &ptr); - if (name == NULL) +#if defined(__WIN32) || defined(_WIN32) + char *name = strtok(line," \t"); +#else + char *name = strtok_r(line," \t", &ptr); +#endif /* __WIN32 */ + if (name == NULL) continue; - char *proto_ch = strtok_r(NULL," \t", &ptr); +#if defined(__WIN32) || defined(_WIN32) + char *proto_ch = strtok(NULL," \t"); +#else + char *proto_ch = strtok_r(NULL," \t", &ptr); +#endif /* __WIN32 */ if (proto_ch == NULL) continue; @@ -55,7 +65,12 @@ void SCProtoNameInit() if (proto >= 255) continue; - char *cname = strtok_r(NULL, " \t", &ptr); +#if defined(__WIN32) || defined(_WIN32) + char *cname = strtok(NULL, " \t"); +#else + char *cname = strtok_r(NULL, " \t", &ptr); +#endif /* __WIN32 */ + if (cname != NULL) { known_proto[proto] = SCStrdup(cname); } else { diff --git a/src/util-proto-name.h b/src/util-proto-name.h index 667de93337..e349a6db2e 100644 --- a/src/util-proto-name.h +++ b/src/util-proto-name.h @@ -24,7 +24,11 @@ #ifndef __UTIL_PROTO_NAME_H__ #define __UTIL_PROTO_NAME_H__ -#define PROTO_FILE "/etc/protocols" +#ifndef OS_WIN32 +#define PROTO_FILE "/etc/protocols" +#else +#define PROTO_FILE "C:\\Windows\\system32\\drivers\\etc\\protocol" +#endif /* OS_WIN32 */ /** Lookup array to hold the information related to known protocol * in /etc/protocols */ diff --git a/src/win32-misc.c b/src/win32-misc.c index 72135f7799..09d3130d3b 100644 --- a/src/win32-misc.c +++ b/src/win32-misc.c @@ -27,8 +27,9 @@ #include "suricata-common.h" #include "win32-misc.h" +#include "direct.h" -int setenv(const char *name, const char *value, int overwrite) +void setenv(const char *name, const char *value, int overwrite) { if (overwrite || NULL == getenv(name)) { char *str = SCMalloc(strlen(name) + strlen(value) + 2); @@ -38,7 +39,7 @@ int setenv(const char *name, const char *value, int overwrite) } } -int unsetenv(const char *name) +void unsetenv(const char *name) { char *str = SCMalloc(strlen(name) + 2); snprintf(str, strlen(name) + 1, "%s=", name); diff --git a/src/win32-misc.h b/src/win32-misc.h index 615c6731cc..7242f9c448 100644 --- a/src/win32-misc.h +++ b/src/win32-misc.h @@ -27,16 +27,14 @@ #define index strchr #define rindex strrchr -#define strtok_r(s, d, p) strtok(s, d) - #define bzero(s, n) memset(s, 0, n) #ifndef O_NOFOLLOW #define O_NOFOLLOW 0 #endif /* O_NOFOLLOW */ -int setenv(const char *name, const char *value, int overwrite); -int unsetenv(const char *name); +void setenv(const char *name, const char *value, int overwrite); +void unsetenv(const char *name); const char* inet_ntop(int af, const void *src, char *dst, uint32_t cnt); int inet_pton(int af, const char *src, void *dst);