summaryrefslogtreecommitdiff
path: root/ext/snmp
diff options
context:
space:
mode:
authorSVN Migration <svn@php.net>2008-12-03 20:30:45 +0000
committerSVN Migration <svn@php.net>2008-12-03 20:30:45 +0000
commit2876046398950e59c3b3c460e67e6fec7ff2ba3c (patch)
tree33b2b8b4b859960a6446ad19d0ada1c55f9cfcda /ext/snmp
parent3fb86b0b9e79e6a3312b694f30ee627e2e1b325c (diff)
downloadphp-git-php-5.3.0alpha2.tar.gz
This commit was manufactured by cvs2svn to create tag 'php_5_3_0alpha2'.php-5.3.0alpha2
Diffstat (limited to 'ext/snmp')
-rw-r--r--ext/snmp/snmp.c33
-rw-r--r--ext/snmp/winsnmp.c249
2 files changed, 273 insertions, 9 deletions
diff --git a/ext/snmp/snmp.c b/ext/snmp/snmp.c
index ea73a33709..b52d9c072e 100644
--- a/ext/snmp/snmp.c
+++ b/ext/snmp/snmp.c
@@ -113,6 +113,7 @@ static PHP_GINIT_FUNCTION(snmp);
static oid objid_mib[] = {1, 3, 6, 1, 2, 1};
/* {{{ arginfo */
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpget, 0, 0, 3)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, community)
@@ -121,6 +122,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpget, 0, 0, 3)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpgetnext, 0, 0, 3)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, community)
@@ -129,6 +131,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpgetnext, 0, 0, 3)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpwalk, 0, 0, 3)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, community)
@@ -137,6 +140,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpwalk, 0, 0, 3)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmprealwalk, 0, 0, 3)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, community)
@@ -145,24 +149,29 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_snmprealwalk, 0, 0, 3)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_get_quick_print, 0, 0, 1)
ZEND_ARG_INFO(0, d)
ZEND_END_ARG_INFO()
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set_quick_print, 0, 0, 1)
ZEND_ARG_INFO(0, quick_print)
ZEND_END_ARG_INFO()
#ifdef HAVE_NET_SNMP
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set_enum_print, 0, 0, 1)
ZEND_ARG_INFO(0, enum_print)
ZEND_END_ARG_INFO()
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set_oid_output_format, 0, 0, 1)
ZEND_ARG_INFO(0, oid_format)
ZEND_END_ARG_INFO()
#endif
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpset, 0, 0, 5)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, community)
@@ -173,6 +182,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpset, 0, 0, 5)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_get, 0, 0, 3)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, community)
@@ -181,6 +191,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_get, 0, 0, 3)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_getnext, 0, 0, 3)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, community)
@@ -189,6 +200,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_getnext, 0, 0, 3)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_walk, 0, 0, 3)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, community)
@@ -197,6 +209,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_walk, 0, 0, 3)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_real_walk, 0, 0, 3)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, community)
@@ -205,6 +218,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_real_walk, 0, 0, 3)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_set, 0, 0, 5)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, community)
@@ -215,11 +229,13 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_set, 0, 0, 5)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_php_snmpv3, 0, 0, 2)
ZEND_ARG_INFO(0, s)
ZEND_ARG_INFO(0, st)
ZEND_END_ARG_INFO()
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_get, 0, 0, 8)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, sec_name)
@@ -233,6 +249,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_get, 0, 0, 8)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_getnext, 0, 0, 8)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, sec_name)
@@ -246,6 +263,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_getnext, 0, 0, 8)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_walk, 0, 0, 8)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, sec_name)
@@ -259,6 +277,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_walk, 0, 0, 8)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_real_walk, 0, 0, 8)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, sec_name)
@@ -272,6 +291,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_real_walk, 0, 0, 8)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_set, 0, 0, 10)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, sec_name)
@@ -287,13 +307,16 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_set, 0, 0, 10)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set_valueretrieval, 0, 0, 1)
ZEND_ARG_INFO(0, method)
ZEND_END_ARG_INFO()
+static
ZEND_BEGIN_ARG_INFO(arginfo_snmp_get_valueretrieval, 0)
ZEND_END_ARG_INFO()
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_read_mib, 0, 0, 1)
ZEND_ARG_INFO(0, filename)
ZEND_END_ARG_INFO()
@@ -604,13 +627,13 @@ static void php_snmp_internal(INTERNAL_FUNCTION_PARAMETERS, int st,
while (keepwalking) {
keepwalking = 0;
if ((st == SNMP_CMD_GET) || (st == SNMP_CMD_GETNEXT)) {
+ pdu = snmp_pdu_create((st == SNMP_CMD_GET) ? SNMP_MSG_GET : SNMP_MSG_GETNEXT);
name_length = MAX_OID_LEN;
if (!snmp_parse_oid(objid, name, &name_length)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid object identifier: %s", objid);
snmp_close(ss);
RETURN_FALSE;
}
- pdu = snmp_pdu_create((st == SNMP_CMD_GET) ? SNMP_MSG_GET : SNMP_MSG_GETNEXT);
snmp_add_null_var(pdu, name, name_length);
} else if (st == SNMP_CMD_SET) {
pdu = snmp_pdu_create(SNMP_MSG_SET);
@@ -621,7 +644,6 @@ static void php_snmp_internal(INTERNAL_FUNCTION_PARAMETERS, int st,
sprint_objid(buf, name, name_length);
#endif
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not add variable: %s %c %s", buf, type, value);
- snmp_free_pdu(pdu);
snmp_close(ss);
RETURN_FALSE;
}
@@ -655,13 +677,11 @@ retry:
*return_value = *snmpval;
zval_copy_ctor(return_value);
zval_ptr_dtor(&snmpval);
- snmp_free_pdu(pdu);
snmp_close(ss);
return;
} else if (st == SNMP_CMD_GETNEXT) {
*return_value = *snmpval;
zval_copy_ctor(return_value);
- snmp_free_pdu(pdu);
snmp_close(ss);
return;
} else if (st == SNMP_CMD_WALK) {
@@ -700,28 +720,23 @@ retry:
}
if (st == SNMP_CMD_GET) {
if ((pdu = snmp_fix_pdu(response, SNMP_MSG_GET)) != NULL) {
- snmp_free_pdu(pdu);
goto retry;
}
} else if (st == SNMP_CMD_SET) {
if ((pdu = snmp_fix_pdu(response, SNMP_MSG_SET)) != NULL) {
- snmp_free_pdu(pdu);
goto retry;
}
} else if (st == SNMP_CMD_GETNEXT) {
if ((pdu = snmp_fix_pdu(response, SNMP_MSG_GETNEXT)) != NULL) {
- snmp_free_pdu(pdu);
goto retry;
}
} else if (st >= SNMP_CMD_WALK) { /* Here we do walks. */
if ((pdu = snmp_fix_pdu(response, ((session->version == SNMP_VERSION_1)
? SNMP_MSG_GETNEXT
: SNMP_MSG_GETBULK))) != NULL) {
- snmp_free_pdu(pdu);
goto retry;
}
}
- snmp_free_pdu(pdu);
snmp_close(ss);
if (st == SNMP_CMD_WALK || st == SNMP_CMD_REALWALK) {
zval_dtor(return_value);
diff --git a/ext/snmp/winsnmp.c b/ext/snmp/winsnmp.c
new file mode 100644
index 0000000000..331a116e74
--- /dev/null
+++ b/ext/snmp/winsnmp.c
@@ -0,0 +1,249 @@
+/*
+Created from the snmputil sample in the Microsoft SDK for NT
+*/
+
+#include "php.h"
+#if COMPILE_DL
+#include "../phpdl.h"
+#include "functions/dl.h"
+#endif
+#include "php_snmp.h"
+#include <sys/types.h>
+
+#include <stdio.h>
+#include <string.h>
+#include <malloc.h>
+
+#if HAVE_SNMP
+
+#include <snmp.h>
+#include <mgmtapi.h>
+
+/* {{{ snmp_functions[]
+ */
+const zend_function_entry snmp_functions[] = {
+ {"snmpget", php3_snmpget, NULL},
+ {"snmpwalk", php3_snmpwalk, NULL},
+ {NULL,NULL,NULL}
+};
+/* }}} */
+
+/* {{{ snmp_module_entry
+ */
+zend_module_entry snmp_module_entry = {
+ STANDARD_MODULE_HEADER,
+ "SNMP",
+ snmp_functions,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NO_VERSION_YET,
+ STANDARD_MODULE_PROPERTIES
+};
+/* }}} */
+
+#if COMPILE_DL
+DLEXPORT zend_module_entry *get_module() { return &snmp_module_entry; }
+#endif
+
+#define GET 1
+#define WALK 2
+#define GETNEXT 3
+
+#define TIMEOUT 6000 /* milliseconds */
+#define RETRIES 3
+
+/* {{{ _php_snmp
+ */
+void _php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st) {
+ zval *a1, *a2, *a3;
+ INT operation;
+ LPSTR agent;
+ LPSTR community;
+ RFC1157VarBindList variableBindings;
+ LPSNMP_MGR_SESSION session;
+
+ INT timeout = TIMEOUT;
+ INT retries = RETRIES;
+
+ BYTE requestType;
+ AsnInteger errorStatus;
+ AsnInteger errorIndex;
+ AsnObjectIdentifier oid;
+ char *chkPtr = NULL;
+
+ if (zend_get_parameters_ex(3, &a1, &a2, &a3) == FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+ convert_to_string_ex(a1);
+ convert_to_string_ex(a2);
+ convert_to_string_ex(a3);
+
+ agent=Z_STRVAL_PP(a1);
+ community=Z_STRVAL_PP(a2);
+ operation=st;
+ SnmpMgrStrToOid(Z_STRVAL_PP(a3), &oid);
+
+/*
+ I've limited this to only one oid, but we can create a
+ list of oid's here, and expand the function to take multiple
+ oid's
+*/
+ variableBindings.list->name = oid;
+ variableBindings.list->value.asnType = ASN_NULL;
+ variableBindings.len = 1;
+
+/* Establish a SNMP session to communicate with the remote agent. The
+ community, communications timeout, and communications retry count
+ for the session are also required.
+*/
+ if ((session = SnmpMgrOpen(agent, community, timeout, retries)) == NULL){
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "error on SnmpMgrOpen %d", GetLastError());
+ }
+
+ /* Determine and perform the requested operation.*/
+ if (operation == GET || operation == GETNEXT){
+ /* Get and GetNext are relatively simple operations to perform.
+ Simply initiate the request and process the result and/or
+ possible error conditions. */
+
+ if (operation == GET){
+ requestType = ASN_RFC1157_GETREQUEST;
+ } else {
+ requestType = ASN_RFC1157_GETNEXTREQUEST;
+ }
+
+ /* Request that the API carry out the desired operation.*/
+ if (!SnmpMgrRequest(session, requestType, &variableBindings,
+ &errorStatus, &errorIndex)){
+ /* The API is indicating an error. */
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "error on SnmpMgrRequest %d", GetLastError());
+ } else {
+ /* The API succeeded, errors may be indicated from the remote
+ agent. */
+ if (errorStatus > 0){
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error: errorStatus=%d, errorIndex=%d",
+ errorStatus, errorIndex);
+ } else {
+ /* Display the resulting variable bindings.*/
+ UINT i;
+ char *str = NULL;
+
+ for(i=0; i < variableBindings.len; i++)
+ {
+ SnmpMgrOidToStr(&variableBindings.list[i].name, &str);
+ php_printf("Variable = %s\n", str);
+ if (str) SNMP_free(str);
+
+ php_printf("Value = ");
+ SnmpUtilPrintAsnAny(&variableBindings.list[i].value);
+
+ php_printf("\n");
+ } /* end for() */
+ }
+ }
+
+ /* Free the variable bindings that have been allocated.*/
+ SnmpUtilVarBindListFree(&variableBindings);
+ }
+ else if (operation == WALK)
+ {
+ /* Walk is a common term used to indicate that all MIB variables
+ under a given OID are to be traversed and displayed. This is
+ a more complex operation requiring tests and looping in addition
+ to the steps for get/getnext above. */
+ AsnObjectIdentifier root;
+ AsnObjectIdentifier tempOid;
+
+ SnmpUtilOidCpy(&root, &variableBindings.list[0].name);
+ requestType = ASN_RFC1157_GETNEXTREQUEST;
+
+ while(1)
+ {
+ if (!SnmpMgrRequest(session, requestType, &variableBindings,
+ &errorStatus, &errorIndex)){
+ /* The API is indicating an error.*/
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "error on SnmpMgrRequest %d", GetLastError());
+ break;
+ }
+ else
+ {
+ /* The API succeeded, errors may be indicated from the remote
+ agent.
+ Test for end of subtree or end of MIB. */
+ if (errorStatus == SNMP_ERRORSTATUS_NOSUCHNAME ||
+ SnmpUtilOidNCmp(&variableBindings.list[0].name,
+ &root, root.idLength))
+ {
+ PUTS("End of MIB subtree.\n\n");
+ break;
+ }
+
+ /* Test for general error conditions or sucesss. */
+ if (errorStatus > 0){
+ php_error_docref(NULL TSRMLS_CC, E_ERROR,"Error: errorStatus=%d, errorIndex=%d", errorStatus, errorIndex);
+ break;
+ }
+ else
+ {
+ /* Display resulting variable binding for this iteration. */
+ char *str = NULL;
+
+ SnmpMgrOidToStr(&variableBindings.list[0].name, &str);
+ php_printf("Variable = %s\n", str);
+ if (str) SNMP_free(str);
+
+ php_printf("Value = ");
+ SnmpUtilPrintAsnAny(&variableBindings.list[0].value);
+
+ php_printf("\n");
+ }
+ } /* end if () */
+ /* Prepare for the next iteration. Make sure returned oid is
+ preserved and the returned value is freed.
+ */
+ SnmpUtilOidCpy(&tempOid, &variableBindings.list[0].name);
+ SnmpUtilVarBindFree(&variableBindings.list[0]);
+ SnmpUtilOidCpy(&variableBindings.list[0].name, &tempOid);
+ variableBindings.list[0].value.asnType = ASN_NULL;
+ SnmpUtilOidFree(&tempOid);
+ } /* end while() */
+ /* Free the variable bindings that have been allocated.*/
+ SnmpUtilVarBindListFree(&variableBindings);
+ SnmpUtilOidFree(&root);
+ } /* end if (operation) */
+
+
+ /* Close SNMP session with the remote agent.*/
+ if (!SnmpMgrClose(session)){
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "error on SnmpMgrClose %d", GetLastError());
+ }
+}
+/* }}} */
+
+/* {{{ php3_snmpget
+ */
+DLEXPORT void php3_snmpget(INTERNAL_FUNCTION_PARAMETERS) {
+ _php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,1);
+}
+/* }}} */
+
+/* {{{ php3_snmpwalk
+ */
+DLEXPORT void php3_snmpwalk(INTERNAL_FUNCTION_PARAMETERS) {
+ _php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,2);
+}
+/* }}} */
+
+#endif
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim600: sw=4 ts=4 fdm=marker
+ * vim<600: sw=4 ts=4
+ */