From 25039127e41c21710f3e3e95069d0f5f7a6bd6bc Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Thu, 20 Dec 2018 10:44:03 +0100 Subject: [PATCH] Win changes --- include/LuaEngine.h | 2 +- include/ntop_win32.h | 2 +- src/HTTPserver.cpp | 14 ++++- src/LuaEngine.cpp | 38 +++++++++--- src/MacManufacturers.cpp | 6 +- src/Ntop.cpp | 29 ++++++---- src/PcapInterface.cpp | 9 ++- src/PeriodicActivities.cpp | 6 +- src/Prefs.cpp | 7 ++- src/ThreadedActivity.cpp | 18 ++++-- src/Utils.cpp | 58 +++++++++++++++---- third-party/rrdtool-1.4.8/src/rrd.h | 3 +- third-party/rrdtool-1.4.8/src/strftime.c | 5 ++ third-party/rrdtool-1.4.8/win32/config.h | 2 +- .../zeromq-4.1.3/src/socks_connecter.cpp | 6 +- .../zeromq-4.1.3/src/tcp_connecter.cpp | 2 +- 16 files changed, 160 insertions(+), 47 deletions(-) diff --git a/include/LuaEngine.h b/include/LuaEngine.h index 22de90c8027c..6d1030c09096 100644 --- a/include/LuaEngine.h +++ b/include/LuaEngine.h @@ -91,7 +91,7 @@ class LuaEngine { static void luaRegister(lua_State *L, const ntop_class_reg *reg); static void luaRegisterInternalRegs(lua_State *L); - void setInterface(const char * user, char * const ifname, ssize_t ifname_len, bool * const is_allowed) const; + void setInterface(const char * user, char * const ifname, u_int16_t ifname_len, bool * const is_allowed) const; }; /** diff --git a/include/ntop_win32.h b/include/ntop_win32.h index 43961d36af6f..489dd2bca5d6 100644 --- a/include/ntop_win32.h +++ b/include/ntop_win32.h @@ -187,7 +187,7 @@ struct win_in6_addr #if defined(_MSC_VER) #include -typedef SSIZE_T ssize_t; +#define SSIZE_T ssize_t #endif struct ip6_hdr diff --git a/src/HTTPserver.cpp b/src/HTTPserver.cpp index ead39b24da42..e226d1f2accf 100644 --- a/src/HTTPserver.cpp +++ b/src/HTTPserver.cpp @@ -909,7 +909,11 @@ static int handle_lua_request(struct mg_connection *conn) { || (strcmp(request_info->uri, "/") == 0)) { /* Lua Script */ char path[255] = { 0 }, uri[2048]; - struct stat buf; +#ifdef WIN32 + struct _stat64 buf; +#else + struct stat buf; +#endif bool found; if(strstr(request_info->uri, "/lua/pro") @@ -1004,14 +1008,18 @@ static int handle_http_message(const struct mg_connection *conn, const char *mes /* ****************************************** */ bool HTTPserver::check_ssl_cert(char *ssl_cert_path, size_t ssl_cert_path_len) { - struct stat statsBuf; +#ifdef WIN32 + struct _stat64 s; +#else + struct stat s; +#endif int stat_rc; ssl_cert_path[0] = '\0'; snprintf(ssl_cert_path, ssl_cert_path_len, "%s/ssl/%s", docs_dir, CONST_HTTPS_CERT_NAME); - stat_rc = stat(ssl_cert_path, &statsBuf); + stat_rc = stat(ssl_cert_path, &s); if(stat_rc == 0) { ntop->getTrace()->traceEvent(TRACE_INFO, "Found SSL certificate %s", ssl_cert_path); diff --git a/src/LuaEngine.cpp b/src/LuaEngine.cpp index 42ed22bb0550..ded18c0fb8c3 100644 --- a/src/LuaEngine.cpp +++ b/src/LuaEngine.cpp @@ -1508,7 +1508,11 @@ static int ntop_is_dir(lua_State* vm) { // ***API*** static int ntop_is_not_empty_file(lua_State* vm) { char *path; +#ifdef WIN32 + struct _stat64 buf; +#else struct stat buf; +#endif int rc; ntop->getTrace()->traceEvent(TRACE_DEBUG, "%s() called", __FUNCTION__); @@ -1528,7 +1532,11 @@ static int ntop_is_not_empty_file(lua_State* vm) { // ***API*** static int ntop_get_file_dir_exists(lua_State* vm) { char *path; - struct stat buf; +#ifdef WIN32 + struct _stat64 buf; +#else + struct buf; +#endif int rc; ntop->getTrace()->traceEvent(TRACE_DEBUG, "%s() called", __FUNCTION__); @@ -1548,7 +1556,11 @@ static int ntop_get_file_dir_exists(lua_State* vm) { // ***API*** static int ntop_get_file_last_change(lua_State* vm) { char *path; - struct stat buf; +#ifdef WIN32 + struct _stat64 buf; +#else + struct buf; +#endif ntop->getTrace()->traceEvent(TRACE_DEBUG, "%s() called", __FUNCTION__); @@ -4279,12 +4291,16 @@ static int ntop_rrd_create(lua_State* vm) { static int ntop_rrd_update(lua_State* vm) { const char *filename, *when = NULL, *v1 = NULL, *v2 = NULL, *v3 = NULL; int status; - struct stat stat_buf; +#ifdef WIN32 + struct _stat64 s; +#else + struct s; +#endif if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING) != CONST_LUA_OK) return(CONST_LUA_PARAM_ERROR); if((filename = (const char*)lua_tostring(vm, 1)) == NULL) return(CONST_LUA_PARAM_ERROR); - if(stat(filename, &stat_buf) != 0) { + if(stat(filename, &s) != 0) { char error_buf[256]; snprintf(error_buf, sizeof(error_buf), "File %s does not exist", filename); @@ -6172,7 +6188,11 @@ static int ntop_sqlite_exec_query(lua_State* vm) { sqlite3 *db; char *zErrMsg = 0; struct ntopng_sqlite_state state; - struct stat buf; +#ifdef WIN32 + struct _stat64 buf; +#else + struct buf; +#endif ntop->getTrace()->traceEvent(TRACE_DEBUG, "%s() called", __FUNCTION__); @@ -6804,7 +6824,11 @@ static int ntop_list_reports(lua_State* vm) { while ((ent = readdir(dir)) != NULL) { char filepath[MAX_PATH+3]; - struct stat buf; +#ifdef WIN32 + struct _stat64 buf; +#else + struct buf; +#endif snprintf(filepath, sizeof(filepath), "%s/%s", fullpath, ent->d_name); ntop->fixPath(filepath); @@ -8548,7 +8572,7 @@ void LuaEngine::purifyHTTPParameter(char *param) { #endif /* ****************************************** */ -void LuaEngine::setInterface(const char * user, char * const ifname, ssize_t ifname_len, bool * const is_allowed) const { +void LuaEngine::setInterface(const char * user, char * const ifname, u_int16_t ifname_len, bool * const is_allowed) const { NetworkInterface *iface = NULL; char key[CONST_MAX_LEN_REDIS_KEY]; ifname[0] = '\0'; diff --git a/src/MacManufacturers.cpp b/src/MacManufacturers.cpp index 7b2b2bf93aff..dfaf4871b603 100644 --- a/src/MacManufacturers.cpp +++ b/src/MacManufacturers.cpp @@ -41,7 +41,11 @@ MacManufacturers::MacManufacturers(const char * const home) { /* *************************************** */ void MacManufacturers::init() { - struct stat buf; +#ifdef WIN32 + struct _stat64 buf; +#else + struct buf; +#endif FILE *fd; char line[256], *cr; int _mac[3]; diff --git a/src/Ntop.cpp b/src/Ntop.cpp index 5c0ce8606965..32d24452baf2 100644 --- a/src/Ntop.cpp +++ b/src/Ntop.cpp @@ -163,11 +163,12 @@ Ntop::Ntop(char *appName) { void Ntop::initTimezone() { #ifdef WIN32 - time_offset = -timezone; + time_offset = -_timezone; #else time_t t = time(NULL); + struct tm *l = localtime(&t); - time_offset = localtime(&t)->tm_gmtoff; + time_offset = l->tm_gmtoff; #endif } @@ -236,22 +237,26 @@ Ntop::~Ntop() { /* ******************************************* */ void Ntop::registerPrefs(Prefs *_prefs, bool quick_registration) { - struct stat statbuf; +#ifdef WIN32 + struct _stat64 buf; +#else + struct buf; +#endif prefs = _prefs; if(!quick_registration) { - if(stat(prefs->get_data_dir(), &statbuf) - || (!(statbuf.st_mode & S_IFDIR)) /* It's not a directory */ - || (!(statbuf.st_mode & S_IWRITE)) /* It's not writable */) { + if(stat(prefs->get_data_dir(), &buf) + || (!(buf.st_mode & S_IFDIR)) /* It's not a directory */ + || (!(buf.st_mode & S_IWRITE)) /* It's not writable */) { ntop->getTrace()->traceEvent(TRACE_ERROR, "Invalid directory %s specified", prefs->get_data_dir()); exit(-1); } - if(stat(prefs->get_callbacks_dir(), &statbuf) - || (!(statbuf.st_mode & S_IFDIR)) /* It's not a directory */ - || (!(statbuf.st_mode & S_IREAD)) /* It's not readable */) { + if(stat(prefs->get_callbacks_dir(), &buf) + || (!(buf.st_mode & S_IFDIR)) /* It's not a directory */ + || (!(buf.st_mode & S_IREAD)) /* It's not readable */) { ntop->getTrace()->traceEvent(TRACE_ERROR, "Invalid directory %s specified", prefs->get_callbacks_dir()); exit(-1); @@ -1783,7 +1788,11 @@ void Ntop::fixPath(char *str, bool replaceDots) { char* Ntop::getValidPath(char *__path) { char _path[MAX_PATH+8]; - struct stat buf; +#ifdef WIN32 + struct _stat64 buf; +#else + struct buf; +#endif #ifdef WIN32 const char *install_dir = (const char *)get_install_dir(); #endif diff --git a/src/PcapInterface.cpp b/src/PcapInterface.cpp index 43f4f27e0476..6ff5825dfd01 100644 --- a/src/PcapInterface.cpp +++ b/src/PcapInterface.cpp @@ -31,7 +31,11 @@ PcapInterface::PcapInterface(const char *name) : NetworkInterface(name) { char pcap_error_buffer[PCAP_ERRBUF_SIZE]; - struct stat buf; +#ifdef WIN32 + struct _stat64 buf; +#else + struct buf; +#endif pcap_handle = NULL, pcap_list = NULL; memset(&last_pcap_stat, 0, sizeof(last_pcap_stat)); @@ -280,11 +284,14 @@ void PcapInterface::shutdown() { /* **************************************************** */ u_int32_t PcapInterface::getNumDroppedPackets() { +#ifndef WIN32 + /* For some reson it crashes under Windows: maybe a lock is needed ?*/ struct pcap_stat pcapStat; if(pcap_handle && (pcap_stats(pcap_handle, &pcapStat) >= 0)) { return(pcapStat.ps_drop); } else +#endif return 0; } diff --git a/src/PeriodicActivities.cpp b/src/PeriodicActivities.cpp index 9effa4a34632..5ec56b52892e 100644 --- a/src/PeriodicActivities.cpp +++ b/src/PeriodicActivities.cpp @@ -61,7 +61,11 @@ void PeriodicActivities::sendShutdownSignal() { /* ******************************************* */ void PeriodicActivities::startPeriodicActivitiesLoop() { - struct stat buf; +#ifdef WIN32 + struct _stat64 buf; +#else + struct buf; +#endif ThreadedActivity *startup_activity; static u_int8_t num_threads = DEFAULT_THREAD_POOL_SIZE; diff --git a/src/Prefs.cpp b/src/Prefs.cpp index 2a061164641e..221e7777e6f1 100755 --- a/src/Prefs.cpp +++ b/src/Prefs.cpp @@ -1412,7 +1412,12 @@ int Prefs::checkOptions() { int Prefs::loadFromCLI(int argc, char *argv[]) { u_char c; - while((c = getopt_long(argc, argv, + while((c = getopt_long( +#ifdef WIN32 + (int *(__cdecl *)(void))argc, (char *const **(__cdecl *)(void))argv, +#else + argc, argv, +#endif "k:eg:hi:w:r:sg:m:n:p:qd:t:x:1:2:3:4:5:l:uv:A:B:CD:E:F:N:G:I:O:Q:S:TU:X:W:VZ:", long_options, NULL)) != '?') { if(c == 255) break; diff --git a/src/ThreadedActivity.cpp b/src/ThreadedActivity.cpp index 30734aa42faa..cc22c9eb2f28 100644 --- a/src/ThreadedActivity.cpp +++ b/src/ThreadedActivity.cpp @@ -136,13 +136,17 @@ void ThreadedActivity::run() { /* Run a one-shot script / accurate (e.g. second) periodic script */ void ThreadedActivity::runScript() { - struct stat statbuf; +#ifdef WIN32 + struct _stat64 buf; +#else + struct buf; +#endif char script_path[MAX_PATH]; snprintf(script_path, sizeof(script_path), "%s/system/%s", ntop->get_callbacks_dir(), path); - if(stat(script_path, &statbuf) == 0) { + if(stat(script_path, &buf) == 0) { runScript(script_path, NULL); } else ntop->getTrace()->traceEvent(TRACE_WARNING, "Unable to find script %s", path); @@ -256,14 +260,18 @@ void ThreadedActivity::periodicActivityBody() { void ThreadedActivity::schedulePeriodicActivity(ThreadPool *pool) { /* Schedule per system / interface */ char script_path[MAX_PATH]; - struct stat statbuf; +#ifdef WIN32 + struct _stat64 buf; +#else + struct buf; +#endif if(!systemTaskRunning) { /* Schedule system script */ snprintf(script_path, sizeof(script_path), "%s/system/%s", ntop->get_callbacks_dir(), path); - if(stat(script_path, &statbuf) == 0) { + if(stat(script_path, &buf) == 0) { pool->queueJob(this, script_path, NULL); #ifdef THREAD_DEBUG ntop->getTrace()->traceEvent(TRACE_NORMAL, "Queued system job %s", script_path); @@ -275,7 +283,7 @@ void ThreadedActivity::schedulePeriodicActivity(ThreadPool *pool) { snprintf(script_path, sizeof(script_path), "%s/interface/%s", ntop->get_callbacks_dir(), path); - if(stat(script_path, &statbuf) == 0) { + if(stat(script_path, &buf) == 0) { for(int i = 0; i < ntop->get_num_interfaces(); i++) { NetworkInterface *iface = ntop->getInterface(i); diff --git a/src/Utils.cpp b/src/Utils.cpp index 5ce934dfa888..f62efd2e2d4e 100755 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -269,7 +269,12 @@ bool Utils::file_exists(const char *path) { /* ****************************************************** */ bool Utils::dir_exists(const char * const path) { - struct stat buf; +#ifdef WIN32 + struct _stat64 buf; +#else + struct buf; +#endif + return !((stat(path, &buf) != 0) || (!S_ISDIR(buf.st_mode))); } @@ -343,7 +348,11 @@ int Utils::remove_recursively(const char * const path) { buf = (char *) malloc(len); if(buf) { - struct stat statbuf; +#ifdef WIN32 + struct _stat64 statbuf; +#else + struct statbuf; +#endif snprintf(buf, len, "%s/%s", path, p->d_name); @@ -371,7 +380,11 @@ int Utils::remove_recursively(const char * const path) { bool Utils::mkdir_tree(char *path) { int rc; - struct stat s; +#ifdef WIN32 + struct _stat64 s; +#else + struct s; +#endif ntop->fixPath(path); @@ -1347,14 +1360,18 @@ bool Utils::postHTTPTextFile(lua_State* vm, char *username, char *password, char char *path, int timeout, HTTPTranferStats *stats) { CURL *curl; bool ret = true; - struct stat file_info; +#ifdef WIN32 + struct _stat64 buf; +#else + struct buf; +#endif size_t file_len; FILE *fd = fopen(path, "r"); - if((fd == NULL) || (stat(path, &file_info) != 0)) + if((fd == NULL) || (stat(path, &buf) != 0)) return(false); else - file_len = (size_t)file_info.st_size; + file_len = (size_t)buf.st_size; curl = curl_easy_init(); if(curl) { @@ -1809,7 +1826,11 @@ long Utils::httpGet(const char * const url, /* **************************************** */ char* Utils::getURL(char *url, char *buf, u_int buf_len) { - struct stat s; +#ifdef WIN32 + struct _stat64 s; +#else + struct s; +#endif if(!ntop->getPrefs()->is_pro_edition()) return(url); @@ -1910,7 +1931,11 @@ static bool scan_dir(const char * dir_name, int path_length; char path[MAX_PATH+2]; DIR *d; - struct stat file_stats; +#ifdef WIN32 + struct _stat64 buf; +#else + struct buf; +#endif d = opendir(dir_name); if(!d) return false; @@ -1925,12 +1950,12 @@ static bool scan_dir(const char * dir_name, if(entry->d_type & DT_REG) { snprintf(path, sizeof(path), "%s/%s", dir_name, entry->d_name); - if(!stat(path, &file_stats)) { + if(!stat(path, &buf)) { struct dirent *temp = (struct dirent *)malloc(sizeof(struct dirent)); memcpy(temp, entry, sizeof(struct dirent)); dirlist->push_back(make_pair(temp, strndup(path, MAX_PATH))); if(total) - *total += file_stats.st_size; + *total += buf.st_size; } } else if(entry->d_type & DT_DIR) { @@ -1955,7 +1980,12 @@ static bool scan_dir(const char * dir_name, /* **************************************** */ bool file_mtime_compare(const pair &d1, const pair &d2) { - struct stat sa, sb; +#ifdef WIN32 + struct _stat64 sa, sb; +#else + struct sa, sb; +#endif + if(!d1.second || !d2.second) return false; @@ -1971,7 +2001,11 @@ bool Utils::discardOldFilesExceeding(const char *path, const unsigned long max_s unsigned long total = 0; list > fileslist; list >::iterator it; - struct stat st; +#ifdef WIN32 + struct _stat64 st; +#else + struct st; +#endif if(path == NULL || !strncmp(path, "", MAX_PATH)) return false; diff --git a/third-party/rrdtool-1.4.8/src/rrd.h b/third-party/rrdtool-1.4.8/src/rrd.h index 98803cd3de0e..4938af6380b3 100644 --- a/third-party/rrdtool-1.4.8/src/rrd.h +++ b/third-party/rrdtool-1.4.8/src/rrd.h @@ -68,7 +68,8 @@ extern "C" { #else #if defined(_MSC_VER) #include - typedef SSIZE_T ssize_t; + // typedef SSIZE_T ssize_t; +#define ssize_t unsigned int #endif #endif typedef long off_t; diff --git a/third-party/rrdtool-1.4.8/src/strftime.c b/third-party/rrdtool-1.4.8/src/strftime.c index 3928a6f01f11..4896d435b86b 100644 --- a/third-party/rrdtool-1.4.8/src/strftime.c +++ b/third-party/rrdtool-1.4.8/src/strftime.c @@ -17,6 +17,11 @@ #include /* for struct tm */ #include "strftime.h" +#ifdef WIN32 +#define TZNAME_STD 1 +#define TZNAME_DST 1 +#endif + /* Define your own defaults in config.h if necessary */ #if defined(TZNAME_STD) && defined(TZNAME_DST) char *tzname_[2] = { TZNAME_STD, TZNAME_DST }; diff --git a/third-party/rrdtool-1.4.8/win32/config.h b/third-party/rrdtool-1.4.8/win32/config.h index be848961e3e5..68c4edd84ab1 100644 --- a/third-party/rrdtool-1.4.8/win32/config.h +++ b/third-party/rrdtool-1.4.8/win32/config.h @@ -41,7 +41,7 @@ #define isnan _isnan #endif #define finite _finite -#define snprintf _snprintf +//#define snprintf _snprintf //#define vsnprintf _vsnprintf //#define strftime strftime_ diff --git a/third-party/zeromq-4.1.3/src/socks_connecter.cpp b/third-party/zeromq-4.1.3/src/socks_connecter.cpp index a3b70436787b..8d983a0689da 100644 --- a/third-party/zeromq-4.1.3/src/socks_connecter.cpp +++ b/third-party/zeromq-4.1.3/src/socks_connecter.cpp @@ -27,6 +27,7 @@ along with this program. If not, see . */ + #include #include @@ -282,6 +283,8 @@ void zmq::socks_connecter_t::start_timer () socket->event_connect_retried (endpoint, interval); } +#define min(a, b) ((b < a) ? b : a) + int zmq::socks_connecter_t::get_new_reconnect_ivl () { // The new interval is the current interval + random value. @@ -294,7 +297,7 @@ int zmq::socks_connecter_t::get_new_reconnect_ivl () options.reconnect_ivl_max > options.reconnect_ivl) // Calculate the next interval current_reconnect_ivl = - std::min (current_reconnect_ivl * 2, options.reconnect_ivl_max); + min (current_reconnect_ivl * 2, options.reconnect_ivl_max); return interval; } @@ -476,3 +479,4 @@ int zmq::socks_connecter_t::parse_address ( } return 0; } + diff --git a/third-party/zeromq-4.1.3/src/tcp_connecter.cpp b/third-party/zeromq-4.1.3/src/tcp_connecter.cpp index fdbc45c2b131..b52af0bb5194 100644 --- a/third-party/zeromq-4.1.3/src/tcp_connecter.cpp +++ b/third-party/zeromq-4.1.3/src/tcp_connecter.cpp @@ -207,7 +207,7 @@ int zmq::tcp_connecter_t::get_new_reconnect_ivl () options.reconnect_ivl_max > options.reconnect_ivl) // Calculate the next interval current_reconnect_ivl = - std::min (current_reconnect_ivl * 2, options.reconnect_ivl_max); + min (current_reconnect_ivl * 2, options.reconnect_ivl_max); return interval; }