diff --git a/ezsnmp/include/helpers.h b/ezsnmp/include/helpers.h index f09998c3..6ccb5d2f 100644 --- a/ezsnmp/include/helpers.h +++ b/ezsnmp/include/helpers.h @@ -21,5 +21,13 @@ std::vector parse_results(std::vector const &inputs); void remove_v3_user_from_cache(std::string const &security_name_str, std::string const &context_engine_id_str); std::string print_objid_to_string(oid const *objid, size_t objidlen); - +#if NETSNMP_VERSION_MAJOR < 5 || \ + (NETSNMP_VERSION_MAJOR == 5 && \ + (NETSNMP_VERSION_MINOR < 6 || \ + (NETSNMP_VERSION_MINOR == 6 && \ + NETSNMP_VERSION_PATCH <= 2 ))) +#define NETSNMP_APPLICATION_CONFIG_TYPE "snmpapp" +void netsnmp_cleanup_session(netsnmp_session *s); +void netsnmp_get_monotonic_clock(struct timeval* tv); +#endif #endif // HELPERS_H \ No newline at end of file diff --git a/ezsnmp/src/helpers.cpp b/ezsnmp/src/helpers.cpp index 938ed77f..69d76250 100644 --- a/ezsnmp/src/helpers.cpp +++ b/ezsnmp/src/helpers.cpp @@ -245,3 +245,100 @@ std::string print_objid_to_string(oid const *objid, size_t objidlen) { SNMP_FREE(buf); return ss.str(); } + + +#if NETSNMP_VERSION_MAJOR < 5 || \ + (NETSNMP_VERSION_MAJOR == 5 && \ + (NETSNMP_VERSION_MINOR < 6 || \ + (NETSNMP_VERSION_MINOR == 6 && \ + NETSNMP_VERSION_PATCH <= 2 ))) + +/* Free the memory owned by a session but not the session object itself. */ +void netsnmp_cleanup_session(netsnmp_session *s) +{ + free(s->localname); + free(s->peername); + free(s->community); + free(s->contextEngineID); + free(s->contextName); + free(s->securityEngineID); + free(s->securityName); + free(s->securityAuthProto); + free(s->securityAuthLocalKey); + free(s->securityPrivProto); + free(s->securityPrivLocalKey); + free(s->paramName); +// #ifndef NETSNMP_NO_TRAP_STATS + // free(s->trap_stats); +// #endif /* NETSNMP_NO_TRAP_STATS */ + // usm_free_user(s->sessUser); + memset(s, 0, sizeof(*s)); +} + +/** + * Query the current value of the monotonic clock. + * + * Returns the current value of a monotonic clock if such a clock is provided by + * the operating system or the wall clock time if no such clock is provided by + * the operating system. A monotonic clock is a clock that is never adjusted + * backwards and that proceeds at the same rate as wall clock time. + * + * @param[out] tv Pointer to monotonic clock time. + */ +void netsnmp_get_monotonic_clock(struct timeval* tv) +{ +#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) + struct timespec ts; + int res; + + res = clock_gettime(CLOCK_MONOTONIC, &ts); + if (res >= 0) { + tv->tv_sec = ts.tv_sec; + tv->tv_usec = ts.tv_nsec / 1000; + } else { + gettimeofday(tv, NULL); + } +#elif defined(WIN32) + /* + * Windows: return tick count. Note: the rate at which the tick count + * increases is not adjusted by the time synchronization algorithm, so + * expect an error of <= 100 ppm for the rate at which this clock + * increases. + */ + typedef ULONGLONG (WINAPI * pfGetTickCount64)(void); + static int s_initialized; + static pfGetTickCount64 s_pfGetTickCount64; + uint64_t now64; + + if (!s_initialized) { + HMODULE hKernel32 = GetModuleHandle("kernel32"); + s_pfGetTickCount64 = + (pfGetTickCount64) GetProcAddress(hKernel32, "GetTickCount64"); + s_initialized = TRUE; + } + + if (s_pfGetTickCount64) { + /* Windows Vista, Windows 2008 or any later Windows version */ + now64 = (*s_pfGetTickCount64)(); + } else { + /* Windows XP, Windows 2003 or any earlier Windows version */ + static uint32_t s_wraps, s_last; + uint32_t now; + + now = GetTickCount(); + if (now < s_last) + s_wraps++; + s_last = now; + now64 = ((uint64_t)s_wraps << 32) | now; + } + tv->tv_sec = now64 / 1000; + tv->tv_usec = (now64 % 1000) * 1000; +#else + /* At least FreeBSD 4 doesn't provide monotonic clock support. */ +#warning Not sure how to query a monotonically increasing clock on your system. \ +Timers will not work correctly if the system clock is adjusted by e.g. ntpd. + gettimeofday(tv, NULL); +#endif +} + +#endif /* NETSNMP_VERSION <= 5.6.2 */ \ No newline at end of file diff --git a/setup.py b/setup.py index 5cb0b620..6dfb09f0 100644 --- a/setup.py +++ b/setup.py @@ -53,7 +53,7 @@ def is_net_snmp_installed_macports(): # Determine if a base directory has been provided with the --basedir option basedir = None in_tree = False -compile_args = ["-std=c++17", "-Werror"] +compile_args = ["-std=c++17", "-Werror", "-Wno-error=#warnings"] link_args = [] system_netsnmp_version = check_output("net-snmp-config --version", shell=True).decode() homebrew_version = None