Skip to content

Commit

Permalink
Win changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaderi committed Dec 20, 2018
1 parent 46b950b commit 2503912
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 47 deletions.
2 changes: 1 addition & 1 deletion include/LuaEngine.h
Expand Up @@ -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;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion include/ntop_win32.h
Expand Up @@ -187,7 +187,7 @@ struct win_in6_addr

#if defined(_MSC_VER)
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#define SSIZE_T ssize_t

This comment has been minimized.

Copy link
@gvanem

gvanem Jan 30, 2020

Contributor

Have you tried building with MSVC and this?
SSIZE_T is a typedef in <basetsd.h>. So you got it backwards.

Besides, since there are other potential problems with e.g. RRD-tool also type-defining ssize_t
for MSVC, it's IMHO best to say:

#if defined(_MSC_VER) && !defined(_SSIZE_T_DEFINED)
  #include <BaseTsd.h>
  #define ssize_t SSIZE_T
  #define _SSIZE_T_DEFINED
#endif
#endif

struct ip6_hdr
Expand Down
14 changes: 11 additions & 3 deletions src/HTTPserver.cpp
Expand Up @@ -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")
Expand Down Expand Up @@ -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);
Expand Down
38 changes: 31 additions & 7 deletions src/LuaEngine.cpp
Expand Up @@ -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__);
Expand All @@ -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__);
Expand All @@ -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__);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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__);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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';
Expand Down
6 changes: 5 additions & 1 deletion src/MacManufacturers.cpp
Expand Up @@ -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];
Expand Down
29 changes: 19 additions & 10 deletions src/Ntop.cpp
Expand Up @@ -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
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion src/PcapInterface.cpp
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}

Expand Down
6 changes: 5 additions & 1 deletion src/PeriodicActivities.cpp
Expand Up @@ -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;

Expand Down
7 changes: 6 additions & 1 deletion src/Prefs.cpp
Expand Up @@ -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,

This comment has been minimized.

Copy link
@gvanem

gvanem Jan 29, 2020

Contributor

Where is the getopt_long() that matches this horrid prototype?
With clang-cl, I get this error:

Prefs.cpp(1481,14): error: no matching function for call to 'getopt_long'
  while((c = getopt_long(
             ^~~~~~~~~~~
.\getopt_long.h(58,5): note: candidate function not viable: no known conversion from
 'int *(*)() __attribute__((cdecl))' to 'int' for 1st argument

AFAICS, WIN32 should not be handled specially since this patch works fine:

--- a/Prefs.cpp 2020-01-28 17:41:35
+++ b/Prefs.cpp 2020-01-29 16:46:16
@@ -1479,11 +1479,7 @@
   u_char c;

   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:y: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;

Yikes!

#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;
Expand Down
18 changes: 13 additions & 5 deletions src/ThreadedActivity.cpp
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down

0 comments on commit 2503912

Please sign in to comment.