From 99abcffc62a9c124aa4eb3aae828d342ce26b6fb Mon Sep 17 00:00:00 2001 From: Steve Wilton Date: Sun, 15 Mar 2026 14:39:46 +0800 Subject: [PATCH 01/11] Add snmp_init_mib function to allow MIB tree to be reset using environment variables --- ext/snmp/snmp.c | 8 ++++++++ ext/snmp/snmp.stub.php | 2 ++ ext/snmp/snmp_arginfo.h | 7 ++++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/ext/snmp/snmp.c b/ext/snmp/snmp.c index d116339dd1f40..0a00163306bcd 100644 --- a/ext/snmp/snmp.c +++ b/ext/snmp/snmp.c @@ -1637,6 +1637,14 @@ PHP_FUNCTION(snmp_read_mib) } /* }}} */ +/* {{{ Resets the MIB tree using the MIBDIRS, MIBS and MIBFILES environment variables. */ +PHP_FUNCTION(snmp_init_mib) +{ + shutdown_mib(); + init_mib(); +} +/* }}} */ + /* {{{ Creates a new SNMP session to specified host. */ PHP_METHOD(SNMP, __construct) { diff --git a/ext/snmp/snmp.stub.php b/ext/snmp/snmp.stub.php index 0a303aea77ff0..51f42a4456701 100644 --- a/ext/snmp/snmp.stub.php +++ b/ext/snmp/snmp.stub.php @@ -181,6 +181,8 @@ function snmp_get_valueretrieval(): int {} function snmp_read_mib(string $filename): bool {} +function snmp_init_mib(): void {} + class SNMP { /** @cvalue SNMP_VERSION_1 */ diff --git a/ext/snmp/snmp_arginfo.h b/ext/snmp/snmp_arginfo.h index 1ee821f0538da..5600545328466 100644 --- a/ext/snmp/snmp_arginfo.h +++ b/ext/snmp/snmp_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit snmp.stub.php instead. - * Stub hash: e2451ac3ea0fa5eb1158e8b7252e61c6794d514f */ + * Stub hash: 08a8c1d1f26d62aa9e7333a9f72d4d50ede21932 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_snmpget, 0, 3, IS_MIXED, 0) ZEND_ARG_TYPE_INFO(0, hostname, IS_STRING, 0) @@ -114,6 +114,9 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_snmp_read_mib, 0, 1, _IS_BOOL, 0 ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_snmp_init_mib, 0, 0, IS_VOID, 0) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SNMP___construct, 0, 0, 3) ZEND_ARG_TYPE_INFO(0, version, IS_LONG, 0) ZEND_ARG_TYPE_INFO(0, hostname, IS_STRING, 0) @@ -185,6 +188,7 @@ ZEND_FUNCTION(snmp3_set); ZEND_FUNCTION(snmp_set_valueretrieval); ZEND_FUNCTION(snmp_get_valueretrieval); ZEND_FUNCTION(snmp_read_mib); +ZEND_FUNCTION(snmp_init_mib); ZEND_METHOD(SNMP, __construct); ZEND_METHOD(SNMP, close); ZEND_METHOD(SNMP, setSecurity); @@ -220,6 +224,7 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(snmp_set_valueretrieval, arginfo_snmp_set_valueretrieval) ZEND_FE(snmp_get_valueretrieval, arginfo_snmp_get_valueretrieval) ZEND_FE(snmp_read_mib, arginfo_snmp_read_mib) + ZEND_FE(snmp_init_mib, arginfo_snmp_init_mib) ZEND_FE_END }; From 4912462f5540cff44b2c050e101431ee203b7ea7 Mon Sep 17 00:00:00 2001 From: Steve Wilton Date: Sun, 15 Mar 2026 23:18:27 +0800 Subject: [PATCH 02/11] Fix to make the MIB tree re-initialisation work --- ext/snmp/snmp.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ext/snmp/snmp.c b/ext/snmp/snmp.c index 0a00163306bcd..7a459b0889baa 100644 --- a/ext/snmp/snmp.c +++ b/ext/snmp/snmp.c @@ -1640,7 +1640,10 @@ PHP_FUNCTION(snmp_read_mib) /* {{{ Resets the MIB tree using the MIBDIRS, MIBS and MIBFILES environment variables. */ PHP_FUNCTION(snmp_init_mib) { + // Destroy the old MIB tree and set the internal MIB directory list to NULL shutdown_mib(); + netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIBDIRS, NULL); + // Initialise the new MIB tree init_mib(); } /* }}} */ From 528090f35dd7e45aad19cf8c8b23aba44784a432 Mon Sep 17 00:00:00 2001 From: Steve Wilton Date: Thu, 19 Mar 2026 12:12:46 +0800 Subject: [PATCH 03/11] Add mibdirs argument to init_mib --- ext/snmp/snmp.c | 9 ++++++++- ext/snmp/snmp.stub.php | 2 +- ext/snmp/snmp_arginfo.h | 5 +++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/ext/snmp/snmp.c b/ext/snmp/snmp.c index 7a459b0889baa..f218df806ccca 100644 --- a/ext/snmp/snmp.c +++ b/ext/snmp/snmp.c @@ -1640,9 +1640,16 @@ PHP_FUNCTION(snmp_read_mib) /* {{{ Resets the MIB tree using the MIBDIRS, MIBS and MIBFILES environment variables. */ 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(); + // Destroy the old MIB tree and set the internal MIB directory list to NULL shutdown_mib(); - netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIBDIRS, NULL); + netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIBDIRS, mibdirs); // Initialise the new MIB tree init_mib(); } diff --git a/ext/snmp/snmp.stub.php b/ext/snmp/snmp.stub.php index 51f42a4456701..9bfb47e640dd3 100644 --- a/ext/snmp/snmp.stub.php +++ b/ext/snmp/snmp.stub.php @@ -181,7 +181,7 @@ function snmp_get_valueretrieval(): int {} function snmp_read_mib(string $filename): bool {} -function snmp_init_mib(): void {} +function snmp_init_mib(?string $mibdirs): void {} class SNMP { diff --git a/ext/snmp/snmp_arginfo.h b/ext/snmp/snmp_arginfo.h index 5600545328466..7e3d647ed7e21 100644 --- a/ext/snmp/snmp_arginfo.h +++ b/ext/snmp/snmp_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit snmp.stub.php instead. - * Stub hash: 08a8c1d1f26d62aa9e7333a9f72d4d50ede21932 */ + * Stub hash: 68dd2791d2ed4c931d99e012f6dc2d5d6f5e23b4 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_snmpget, 0, 3, IS_MIXED, 0) ZEND_ARG_TYPE_INFO(0, hostname, IS_STRING, 0) @@ -114,7 +114,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_snmp_read_mib, 0, 1, _IS_BOOL, 0 ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_snmp_init_mib, 0, 0, IS_VOID, 0) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_snmp_init_mib, 0, 1, IS_VOID, 0) + ZEND_ARG_TYPE_INFO(0, mibdirs, IS_STRING, 1) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SNMP___construct, 0, 0, 3) From 491193e481a733376b83390d516a5685dbec34db Mon Sep 17 00:00:00 2001 From: Steve Wilton Date: Thu, 19 Mar 2026 12:24:06 +0800 Subject: [PATCH 04/11] Update comments --- ext/snmp/snmp.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ext/snmp/snmp.c b/ext/snmp/snmp.c index f218df806ccca..dd0ec8d7212a3 100644 --- a/ext/snmp/snmp.c +++ b/ext/snmp/snmp.c @@ -1637,7 +1637,7 @@ PHP_FUNCTION(snmp_read_mib) } /* }}} */ -/* {{{ Resets the MIB tree using the MIBDIRS, MIBS and MIBFILES environment variables. */ +/* {{{ Resets the MIB tree and set the mib directories to the provided mibdirs. */ PHP_FUNCTION(snmp_init_mib) { zend_string *mibdirs = NULL; @@ -1647,10 +1647,8 @@ PHP_FUNCTION(snmp_init_mib) Z_PARAM_STR_OR_NULL(mibdirs) ZEND_PARSE_PARAMETERS_END(); - // Destroy the old MIB tree and set the internal MIB directory list to NULL shutdown_mib(); netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIBDIRS, mibdirs); - // Initialise the new MIB tree init_mib(); } /* }}} */ From d4e28b94e1fe7bf2b1211962ce454369f1984a9f Mon Sep 17 00:00:00 2001 From: Steve Wilton Date: Mon, 23 Mar 2026 21:57:54 +0800 Subject: [PATCH 05/11] Fix use of zend_string being passed to netsnmp_ds_set_string --- ext/snmp/snmp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/snmp/snmp.c b/ext/snmp/snmp.c index dd0ec8d7212a3..a8659c4fcbd52 100644 --- a/ext/snmp/snmp.c +++ b/ext/snmp/snmp.c @@ -1648,7 +1648,7 @@ PHP_FUNCTION(snmp_init_mib) ZEND_PARSE_PARAMETERS_END(); shutdown_mib(); - netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIBDIRS, mibdirs); + netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIBDIRS, ZSTR_VAL(mibdirs)); init_mib(); } /* }}} */ From d7dc8de816af7373fd66446f6eacf28daa4bd134 Mon Sep 17 00:00:00 2001 From: Steve Wilton Date: Fri, 27 Mar 2026 15:01:23 +0800 Subject: [PATCH 06/11] Add a new PHP_RSHUTDOWN_FUNCTION to reset the mib state if it was changed --- ext/snmp/snmp.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/ext/snmp/snmp.c b/ext/snmp/snmp.c index a8659c4fcbd52..caa5b6ad5c1ce 100644 --- a/ext/snmp/snmp.c +++ b/ext/snmp/snmp.c @@ -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); @@ -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); @@ -1647,6 +1650,11 @@ PHP_FUNCTION(snmp_init_mib) 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 (ZSTR_VAL(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(); @@ -2189,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) { @@ -2216,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), From 916618084347a76a32a1dede26aaea8d363bde56 Mon Sep 17 00:00:00 2001 From: Steve Wilton Date: Fri, 27 Mar 2026 15:16:21 +0800 Subject: [PATCH 07/11] Fix typo --- ext/snmp/snmp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/snmp/snmp.c b/ext/snmp/snmp.c index caa5b6ad5c1ce..c0e6612cf493c 100644 --- a/ext/snmp/snmp.c +++ b/ext/snmp/snmp.c @@ -2237,7 +2237,7 @@ zend_module_entry snmp_module_entry = { PHP_MINIT(snmp), PHP_MSHUTDOWN(snmp), NULL, - PHP_RSHUTDOWN(snmp),, + PHP_RSHUTDOWN(snmp), PHP_MINFO(snmp), PHP_SNMP_VERSION, PHP_MODULE_GLOBALS(snmp), From b53ad6a5ce54a2db9fbb784555515642bcd3257b Mon Sep 17 00:00:00 2001 From: Steve Wilton Date: Fri, 27 Mar 2026 15:43:54 +0800 Subject: [PATCH 08/11] Change NULL to uppercase --- ext/snmp/snmp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/snmp/snmp.c b/ext/snmp/snmp.c index c0e6612cf493c..ff285fe08151f 100644 --- a/ext/snmp/snmp.c +++ b/ext/snmp/snmp.c @@ -2202,7 +2202,7 @@ static PHP_RSHUTDOWN_FUNCTION(snmp) { if (mib_needs_reset) { shutdown_mib(); - netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIBDIRS, null); + netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIBDIRS, NULL); init_mib(); } From c45cd416e680299bd8afc8630628623856e813d0 Mon Sep 17 00:00:00 2001 From: Steve Wilton Date: Fri, 27 Mar 2026 16:50:18 +0800 Subject: [PATCH 09/11] Another NULL fix --- ext/snmp/snmp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/snmp/snmp.c b/ext/snmp/snmp.c index ff285fe08151f..2d5d0eafa24e0 100644 --- a/ext/snmp/snmp.c +++ b/ext/snmp/snmp.c @@ -1651,7 +1651,7 @@ PHP_FUNCTION(snmp_init_mib) ZEND_PARSE_PARAMETERS_END(); // If the mibdirs has been changed, we need to reset the MIB tree at the end of the request - if (ZSTR_VAL(mibdirs) != null) { + if (ZSTR_VAL(mibdirs) != NULL) { mib_needs_reset = 1; } From ac6d157b167a422d8c743c3c2ada51a7fc1db283 Mon Sep 17 00:00:00 2001 From: Steve Wilton Date: Fri, 27 Mar 2026 17:13:04 +0800 Subject: [PATCH 10/11] Fix NULL test for mibdirs --- ext/snmp/snmp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/snmp/snmp.c b/ext/snmp/snmp.c index 2d5d0eafa24e0..61fd5f9d772cc 100644 --- a/ext/snmp/snmp.c +++ b/ext/snmp/snmp.c @@ -1651,7 +1651,7 @@ PHP_FUNCTION(snmp_init_mib) ZEND_PARSE_PARAMETERS_END(); // If the mibdirs has been changed, we need to reset the MIB tree at the end of the request - if (ZSTR_VAL(mibdirs) != NULL) { + if (mibdirs != NULL) { mib_needs_reset = 1; } From b83b37b63a7903c2463fba37d91ac605b25f752a Mon Sep 17 00:00:00 2001 From: Steve Wilton Date: Sun, 29 Mar 2026 21:29:59 +0800 Subject: [PATCH 11/11] Whitespace cleanup --- ext/snmp/snmp.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ext/snmp/snmp.c b/ext/snmp/snmp.c index 61fd5f9d772cc..c52e7edec07c6 100644 --- a/ext/snmp/snmp.c +++ b/ext/snmp/snmp.c @@ -1643,15 +1643,15 @@ 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_string *mibdirs = NULL; - ZEND_PARSE_PARAMETERS_START(0, 1) - Z_PARAM_OPTIONAL - Z_PARAM_STR_OR_NULL(mibdirs) - ZEND_PARSE_PARAMETERS_END(); + 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) { + if (mibdirs != NULL) { mib_needs_reset = 1; }