Skip to content
39 changes: 38 additions & 1 deletion ext/snmp/snmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ typedef struct snmp_session php_snmp_session;
} \
}

static int mib_needs_reset;

ZEND_DECLARE_MODULE_GLOBALS(snmp)
static PHP_GINIT_FUNCTION(snmp);

Expand Down Expand Up @@ -1628,6 +1630,7 @@ PHP_FUNCTION(snmp_read_mib)
RETURN_THROWS();
}

mib_needs_reset = 1;
if (!read_mib(filename)) {
char *error = strerror(errno);
php_error_docref(NULL, E_WARNING, "Error while reading MIB file '%s': %s", filename, error);
Expand All @@ -1637,6 +1640,27 @@ PHP_FUNCTION(snmp_read_mib)
}
/* }}} */

/* {{{ Resets the MIB tree and set the mib directories to the provided mibdirs. */
PHP_FUNCTION(snmp_init_mib)
{
zend_string *mibdirs = NULL;

ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_STR_OR_NULL(mibdirs)
ZEND_PARSE_PARAMETERS_END();

// If the mibdirs has been changed, we need to reset the MIB tree at the end of the request
if (mibdirs != NULL) {
mib_needs_reset = 1;
}

shutdown_mib();
netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIBDIRS, ZSTR_VAL(mibdirs));
init_mib();
}
/* }}} */

/* {{{ Creates a new SNMP session to specified host. */
PHP_METHOD(SNMP, __construct)
{
Expand Down Expand Up @@ -2173,6 +2197,19 @@ PHP_MSHUTDOWN_FUNCTION(snmp)
}
/* }}} */

/* {{{ PHP_RSHUTDOWN_FUNCTION */
static PHP_RSHUTDOWN_FUNCTION(snmp)
{
if (mib_needs_reset) {
shutdown_mib();
netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIBDIRS, NULL);
init_mib();
}

return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(snmp)
{
Expand Down Expand Up @@ -2200,7 +2237,7 @@ zend_module_entry snmp_module_entry = {
PHP_MINIT(snmp),
PHP_MSHUTDOWN(snmp),
NULL,
NULL,
PHP_RSHUTDOWN(snmp),
PHP_MINFO(snmp),
PHP_SNMP_VERSION,
PHP_MODULE_GLOBALS(snmp),
Expand Down
2 changes: 2 additions & 0 deletions ext/snmp/snmp.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ function snmp_get_valueretrieval(): int {}

function snmp_read_mib(string $filename): bool {}

function snmp_init_mib(?string $mibdirs): void {}

class SNMP
{
/** @cvalue SNMP_VERSION_1 */
Expand Down
8 changes: 7 additions & 1 deletion ext/snmp/snmp_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading