#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>
zend_function_entry snmp_functions[] = {
{"snmpget", php3_snmpget, NULL},
{"snmpwalk", php3_snmpwalk, NULL},
{NULL,NULL,NULL}
};
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
#define RETRIES 3
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);
variableBindings.list->name = oid;
variableBindings.list->value.asnType = ASN_NULL;
variableBindings.len = 1;
if ((session = SnmpMgrOpen(agent, community, timeout, retries)) == NULL){
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error on SnmpMgrOpen %d", GetLastError());
}
if (operation == GET || operation == GETNEXT){
if (operation == GET){
requestType = ASN_RFC1157_GETREQUEST;
} else {
requestType = ASN_RFC1157_GETNEXTREQUEST;
}
if (!SnmpMgrRequest(session, requestType, &variableBindings,
&errorStatus, &errorIndex)){
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error on SnmpMgrRequest %d", GetLastError());
} else {
if (errorStatus > 0){
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error: errorStatus=%d, errorIndex=%d",
errorStatus, errorIndex);
} else {
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");
}
}
}
SnmpUtilVarBindListFree(&variableBindings);
}
else if (operation == WALK)
{
AsnObjectIdentifier root;
AsnObjectIdentifier tempOid;
SnmpUtilOidCpy(&root, &variableBindings.list[0].name);
requestType = ASN_RFC1157_GETNEXTREQUEST;
while(1)
{
if (!SnmpMgrRequest(session, requestType, &variableBindings,
&errorStatus, &errorIndex)){
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error on SnmpMgrRequest %d", GetLastError());
break;
}
else
{
if (errorStatus == SNMP_ERRORSTATUS_NOSUCHNAME ||
SnmpUtilOidNCmp(&variableBindings.list[0].name,
&root, root.idLength))
{
PUTS("End of MIB subtree.\n\n");
break;
}
if (errorStatus > 0){
php_error_docref(NULL TSRMLS_CC, E_ERROR,"Error: errorStatus=%d, errorIndex=%d", errorStatus, errorIndex);
break;
}
else
{
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");
}
}
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);
}
SnmpUtilVarBindListFree(&variableBindings);
SnmpUtilOidFree(&root);
}
if (!SnmpMgrClose(session)){
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error on SnmpMgrClose %d", GetLastError());
}
}
DLEXPORT void php3_snmpget(INTERNAL_FUNCTION_PARAMETERS) {
_php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,1);
}
DLEXPORT void php3_snmpwalk(INTERNAL_FUNCTION_PARAMETERS) {
_php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,2);
}
#endif