Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MacOS Compilation #265

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests_native.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ jobs:
)
echo 'PYTHONMALLOC=malloc' >> $GITHUB_OUTPUT
fi
LD_LIBRARY_PATH=$(net-snmp-config --libdir | sed 's/-L//'):$LD_LIBRARY_PATH ${VALGRIND[@]} python -m pytest ${PYTEST_ARGS[@]} --junitxml=test-results_${{ matrix.os }}_${{ matrix.python-version }}.xml | tee ./test-outputs_${{ matrix.os }}_${{ matrix.python-version }}.txt
LD_LIBRARY_PATH=$(net-snmp-config --libdir | sed 's/-L//'):$LD_LIBRARY_PATH ${VALGRIND[@]} pytest -s tests/ ${PYTEST_ARGS[@]} --junitxml=test-results_${{ matrix.os }}_${{ matrix.python-version }}.xml | tee ./test-outputs_${{ matrix.os }}_${{ matrix.python-version }}.txt

- name: Upload Test Results
uses: actions/upload-artifact@v4
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ them with the following on Linux:
echo 'mibs +ALL' > ~/.snmp/snmp.conf;
sudo systemctl start snmpd;
rm -drf build/ dist/ ezsnmp.egg-info;
python3 -m pip install -r requirements.txt;
python3 -m pip install -r tests/requirements.txt;
python3 -m pip install . && pytest tests/;
# Bottom one for debug. Replace the top one with it if needed.
# python3 -m pip install . && gdb -ex run -ex bt -ex quit --args python3 -m pytest .;
Expand All @@ -179,7 +179,7 @@ On MacOS
sudo launchctl unload /System/Library/LaunchDaemons/org.net-snmp.snmpd.plist;
sudo launchctl load -w /System/Library/LaunchDaemons/org.net-snmp.snmpd.plist;
rm -drf build/ dist/ ezsnmp.egg-info;
python3 -m pip install -r requirements.txt;
python3 -m pip install -r tests/requirements.txt;
python3 -m pip install . && pytest tests/;


Expand Down
10 changes: 9 additions & 1 deletion ezsnmp/include/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,13 @@ std::vector<Result> parse_results(std::vector<std::string> 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
97 changes: 97 additions & 0 deletions ezsnmp/src/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", "--no-warnings"]
link_args = []
system_netsnmp_version = check_output("net-snmp-config --version", shell=True).decode()
homebrew_version = None
Expand Down
Loading