1999-04-22 10:48:28 +08:00
/*
2003-01-24 17:40:53 +08:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2009-05-03 05:13:39 +08:00
| PHP Version 5 |
2003-01-24 17:40:53 +08:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2011-01-01 10:17:06 +08:00
| Copyright ( c ) 1997 - 2011 The PHP Group |
2003-01-24 17:40:53 +08:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2006-01-01 21:10:10 +08:00
| This source file is subject to version 3.01 of the PHP license , |
2003-01-24 17:40:53 +08:00
| that is bundled with this package in the file LICENSE , and is |
2003-06-11 04:04:29 +08:00
| available through the world - wide - web at the following url : |
2006-01-01 21:10:10 +08:00
| http : //www.php.net/license/3_01.txt |
2003-01-24 17:40:53 +08:00
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world - wide - web , please send a note to |
| license @ php . net so we can mail you a copy immediately . |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| Authors : Rasmus Lerdorf < rasmus @ php . net > |
| Mike Jackson < mhjack @ tscnet . com > |
| Steven Lawrance < slawrance @ technologist . com > |
| Harrie Hazewinkel < harrie @ lisanza . net > |
2003-07-15 01:11:04 +08:00
| Johann Hanne < jonny @ nurfuerspam . de > |
2011-01-31 19:41:33 +08:00
| Boris Lytockin < lytboris @ gmail . com > |
2003-01-24 17:40:53 +08:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/
1999-04-22 10:48:28 +08:00
/* $Id$ */
2001-05-24 18:07:29 +08:00
# ifdef HAVE_CONFIG_H
# include "config.h"
# endif
1999-04-22 10:48:28 +08:00
# include "php.h"
2000-04-07 05:07:44 +08:00
# include "ext/standard/info.h"
1999-12-05 03:19:57 +08:00
# include "php_snmp.h"
2002-10-15 08:43:34 +08:00
2011-01-31 19:41:33 +08:00
# include "zend_exceptions.h"
2002-10-15 08:43:34 +08:00
# if HAVE_SNMP
1999-04-22 10:48:28 +08:00
# include <sys/types.h>
2000-02-11 23:59:30 +08:00
# ifdef PHP_WIN32
2003-02-16 11:48:49 +08:00
# include <winsock2.h>
1999-04-22 10:48:28 +08:00
# include <errno.h>
# include <process.h>
# include "win32/time.h"
2002-09-05 19:50:43 +08:00
# elif defined(NETWARE)
# ifdef USE_WINSOCK
# include <novsock2.h>
# else
# include <sys/socket.h>
# endif
# include <errno.h>
# include <sys/timeval.h>
# else
1999-04-22 10:48:28 +08:00
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
1999-08-05 04:56:33 +08:00
# ifndef _OSD_POSIX
1999-04-22 10:48:28 +08:00
# include <sys/errno.h>
1999-08-05 04:56:33 +08:00
# else
# include <errno.h> /* BS2000/OSD uses <errno.h>, not <sys/errno.h> */
# endif
1999-04-22 10:48:28 +08:00
# include <netdb.h>
# endif
# ifdef HAVE_UNISTD_H
# include <unistd.h>
# endif
# ifndef __P
# ifdef __GNUC__
# define __P(args) args
# else
# define __P(args) ()
# endif
# endif
2002-10-15 08:43:34 +08:00
# include <net-snmp/net-snmp-config.h>
# include <net-snmp/net-snmp-includes.h>
2011-04-05 20:20:35 +08:00
2011-02-17 20:02:16 +08:00
/* For net-snmp prior to 5.4 */
2011-02-17 20:12:22 +08:00
# ifndef HAVE_SHUTDOWN_SNMP_LOGGING
2011-02-17 20:02:16 +08:00
extern netsnmp_log_handler * logh_head ;
# define shutdown_snmp_logging() \
{ \
snmp_disable_log ( ) ; \
while ( NULL ! = logh_head ) \
netsnmp_remove_loghandler ( logh_head ) ; \
}
2011-02-17 20:12:22 +08:00
# endif
2011-02-17 20:02:16 +08:00
2011-07-18 01:18:31 +08:00
# define SNMP_VALUE_LIBRARY (0 << 0)
# define SNMP_VALUE_PLAIN (1 << 0)
# define SNMP_VALUE_OBJECT (1 << 1)
2003-07-15 01:11:04 +08:00
2011-01-31 19:41:33 +08:00
typedef struct snmp_session php_snmp_session ;
# define PHP_SNMP_SESSION_RES_NAME "SNMP session"
# define PHP_SNMP_ADD_PROPERTIES(a, b) \
{ \
int i = 0 ; \
while ( b [ i ] . name ! = NULL ) { \
php_snmp_add_property ( ( a ) , ( b ) [ i ] . name , ( b ) [ i ] . name_length , \
( php_snmp_read_t ) ( b ) [ i ] . read_func , ( php_snmp_write_t ) ( b ) [ i ] . write_func TSRMLS_CC ) ; \
i + + ; \
} \
}
2011-02-26 16:27:26 +08:00
# define PHP_SNMP_ERRNO_NOERROR 0
# define PHP_SNMP_ERRNO_GENERIC 1
# define PHP_SNMP_ERRNO_TIMEOUT 2
# define PHP_SNMP_ERRNO_ERROR_IN_REPLY 3
# define PHP_SNMP_ERRNO_OID_NOT_INCREASING 4
2011-01-31 19:41:33 +08:00
2003-07-15 01:11:04 +08:00
ZEND_DECLARE_MODULE_GLOBALS ( snmp )
2006-06-13 21:12:20 +08:00
static PHP_GINIT_FUNCTION ( snmp ) ;
2003-07-15 01:11:04 +08:00
1999-04-22 10:48:28 +08:00
/* constant - can be shared among threads */
static oid objid_mib [ ] = { 1 , 3 , 6 , 1 , 2 , 1 } ;
2011-01-31 19:41:33 +08:00
static int le_snmp_session ;
/* Handlers */
static zend_object_handlers php_snmp_object_handlers ;
/* Class entries */
zend_class_entry * php_snmp_class_entry ;
2011-02-26 16:27:26 +08:00
zend_class_entry * php_snmp_get_ce ( )
{
return php_snmp_class_entry ;
}
2011-01-31 19:41:33 +08:00
/* Class object properties */
static HashTable php_snmp_properties ;
2008-07-02 03:14:28 +08:00
/* {{{ arginfo */
2011-01-31 19:41:33 +08:00
2008-07-02 03:14:28 +08:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmpget , 0 , 0 , 3 )
ZEND_ARG_INFO ( 0 , host )
ZEND_ARG_INFO ( 0 , community )
ZEND_ARG_INFO ( 0 , object_id )
ZEND_ARG_INFO ( 0 , timeout )
ZEND_ARG_INFO ( 0 , retries )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmpgetnext , 0 , 0 , 3 )
ZEND_ARG_INFO ( 0 , host )
ZEND_ARG_INFO ( 0 , community )
ZEND_ARG_INFO ( 0 , object_id )
ZEND_ARG_INFO ( 0 , timeout )
ZEND_ARG_INFO ( 0 , retries )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmpwalk , 0 , 0 , 3 )
ZEND_ARG_INFO ( 0 , host )
ZEND_ARG_INFO ( 0 , community )
ZEND_ARG_INFO ( 0 , object_id )
ZEND_ARG_INFO ( 0 , timeout )
ZEND_ARG_INFO ( 0 , retries )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmprealwalk , 0 , 0 , 3 )
ZEND_ARG_INFO ( 0 , host )
ZEND_ARG_INFO ( 0 , community )
ZEND_ARG_INFO ( 0 , object_id )
ZEND_ARG_INFO ( 0 , timeout )
ZEND_ARG_INFO ( 0 , retries )
ZEND_END_ARG_INFO ( )
2011-01-31 19:41:33 +08:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmpset , 0 , 0 , 5 )
ZEND_ARG_INFO ( 0 , host )
ZEND_ARG_INFO ( 0 , community )
ZEND_ARG_INFO ( 0 , object_id )
ZEND_ARG_INFO ( 0 , type )
ZEND_ARG_INFO ( 0 , value )
ZEND_ARG_INFO ( 0 , timeout )
ZEND_ARG_INFO ( 0 , retries )
ZEND_END_ARG_INFO ( )
2008-07-02 03:14:28 +08:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp_get_quick_print , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , d )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp_set_quick_print , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , quick_print )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp_set_enum_print , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , enum_print )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp_set_oid_output_format , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , oid_format )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp2_get , 0 , 0 , 3 )
ZEND_ARG_INFO ( 0 , host )
ZEND_ARG_INFO ( 0 , community )
ZEND_ARG_INFO ( 0 , object_id )
ZEND_ARG_INFO ( 0 , timeout )
ZEND_ARG_INFO ( 0 , retries )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp2_getnext , 0 , 0 , 3 )
ZEND_ARG_INFO ( 0 , host )
ZEND_ARG_INFO ( 0 , community )
ZEND_ARG_INFO ( 0 , object_id )
ZEND_ARG_INFO ( 0 , timeout )
ZEND_ARG_INFO ( 0 , retries )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp2_walk , 0 , 0 , 3 )
ZEND_ARG_INFO ( 0 , host )
ZEND_ARG_INFO ( 0 , community )
ZEND_ARG_INFO ( 0 , object_id )
ZEND_ARG_INFO ( 0 , timeout )
ZEND_ARG_INFO ( 0 , retries )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp2_real_walk , 0 , 0 , 3 )
ZEND_ARG_INFO ( 0 , host )
ZEND_ARG_INFO ( 0 , community )
ZEND_ARG_INFO ( 0 , object_id )
ZEND_ARG_INFO ( 0 , timeout )
ZEND_ARG_INFO ( 0 , retries )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp2_set , 0 , 0 , 5 )
ZEND_ARG_INFO ( 0 , host )
ZEND_ARG_INFO ( 0 , community )
ZEND_ARG_INFO ( 0 , object_id )
ZEND_ARG_INFO ( 0 , type )
ZEND_ARG_INFO ( 0 , value )
ZEND_ARG_INFO ( 0 , timeout )
ZEND_ARG_INFO ( 0 , retries )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp3_get , 0 , 0 , 8 )
ZEND_ARG_INFO ( 0 , host )
ZEND_ARG_INFO ( 0 , sec_name )
ZEND_ARG_INFO ( 0 , sec_level )
ZEND_ARG_INFO ( 0 , auth_protocol )
ZEND_ARG_INFO ( 0 , auth_passphrase )
ZEND_ARG_INFO ( 0 , priv_protocol )
ZEND_ARG_INFO ( 0 , priv_passphrase )
ZEND_ARG_INFO ( 0 , object_id )
ZEND_ARG_INFO ( 0 , timeout )
ZEND_ARG_INFO ( 0 , retries )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp3_getnext , 0 , 0 , 8 )
ZEND_ARG_INFO ( 0 , host )
ZEND_ARG_INFO ( 0 , sec_name )
ZEND_ARG_INFO ( 0 , sec_level )
ZEND_ARG_INFO ( 0 , auth_protocol )
ZEND_ARG_INFO ( 0 , auth_passphrase )
ZEND_ARG_INFO ( 0 , priv_protocol )
ZEND_ARG_INFO ( 0 , priv_passphrase )
ZEND_ARG_INFO ( 0 , object_id )
ZEND_ARG_INFO ( 0 , timeout )
ZEND_ARG_INFO ( 0 , retries )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp3_walk , 0 , 0 , 8 )
ZEND_ARG_INFO ( 0 , host )
ZEND_ARG_INFO ( 0 , sec_name )
ZEND_ARG_INFO ( 0 , sec_level )
ZEND_ARG_INFO ( 0 , auth_protocol )
ZEND_ARG_INFO ( 0 , auth_passphrase )
ZEND_ARG_INFO ( 0 , priv_protocol )
ZEND_ARG_INFO ( 0 , priv_passphrase )
ZEND_ARG_INFO ( 0 , object_id )
ZEND_ARG_INFO ( 0 , timeout )
ZEND_ARG_INFO ( 0 , retries )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp3_real_walk , 0 , 0 , 8 )
ZEND_ARG_INFO ( 0 , host )
ZEND_ARG_INFO ( 0 , sec_name )
ZEND_ARG_INFO ( 0 , sec_level )
ZEND_ARG_INFO ( 0 , auth_protocol )
ZEND_ARG_INFO ( 0 , auth_passphrase )
ZEND_ARG_INFO ( 0 , priv_protocol )
ZEND_ARG_INFO ( 0 , priv_passphrase )
ZEND_ARG_INFO ( 0 , object_id )
ZEND_ARG_INFO ( 0 , timeout )
ZEND_ARG_INFO ( 0 , retries )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp3_set , 0 , 0 , 10 )
ZEND_ARG_INFO ( 0 , host )
ZEND_ARG_INFO ( 0 , sec_name )
ZEND_ARG_INFO ( 0 , sec_level )
ZEND_ARG_INFO ( 0 , auth_protocol )
ZEND_ARG_INFO ( 0 , auth_passphrase )
ZEND_ARG_INFO ( 0 , priv_protocol )
ZEND_ARG_INFO ( 0 , priv_passphrase )
ZEND_ARG_INFO ( 0 , object_id )
ZEND_ARG_INFO ( 0 , type )
ZEND_ARG_INFO ( 0 , value )
ZEND_ARG_INFO ( 0 , timeout )
ZEND_ARG_INFO ( 0 , retries )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp_set_valueretrieval , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , method )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO ( arginfo_snmp_get_valueretrieval , 0 )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp_read_mib , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , filename )
ZEND_END_ARG_INFO ( )
2011-01-31 19:41:33 +08:00
/* OO arginfo */
2011-05-10 03:34:07 +08:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp_create , 0 , 0 , 3 )
2011-01-31 19:41:33 +08:00
ZEND_ARG_INFO ( 0 , version )
ZEND_ARG_INFO ( 0 , host )
ZEND_ARG_INFO ( 0 , community )
ZEND_ARG_INFO ( 0 , timeout )
ZEND_ARG_INFO ( 0 , retries )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO ( arginfo_snmp_void , 0 )
ZEND_END_ARG_INFO ( )
2011-03-21 04:07:33 +08:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp_setSecurity , 0 , 0 , 8 )
2011-01-31 19:41:33 +08:00
ZEND_ARG_INFO ( 0 , session )
ZEND_ARG_INFO ( 0 , sec_level )
ZEND_ARG_INFO ( 0 , auth_protocol )
ZEND_ARG_INFO ( 0 , auth_passphrase )
ZEND_ARG_INFO ( 0 , priv_protocol )
ZEND_ARG_INFO ( 0 , priv_passphrase )
ZEND_ARG_INFO ( 0 , contextName )
ZEND_ARG_INFO ( 0 , contextEngineID )
ZEND_ARG_INFO ( 0 , )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp_get , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , object_id )
2011-07-17 22:06:05 +08:00
ZEND_ARG_INFO ( 0 , use_orignames )
2011-01-31 19:41:33 +08:00
ZEND_END_ARG_INFO ( )
2011-07-17 22:06:05 +08:00
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp_walk , 0 , 0 , 4 )
2011-01-31 19:41:33 +08:00
ZEND_ARG_INFO ( 0 , object_id )
2011-07-17 22:06:05 +08:00
ZEND_ARG_INFO ( 0 , suffix_keys )
2011-01-31 19:41:33 +08:00
ZEND_ARG_INFO ( 0 , max_repetitions )
2011-07-18 02:14:38 +08:00
ZEND_ARG_INFO ( 0 , non_repeaters )
2011-01-31 19:41:33 +08:00
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp_set , 0 , 0 , 3 )
ZEND_ARG_INFO ( 0 , object_id )
ZEND_ARG_INFO ( 0 , type )
ZEND_ARG_INFO ( 0 , value )
ZEND_END_ARG_INFO ( )
ZEND_BEGIN_ARG_INFO_EX ( arginfo_snmp_class_set_quick_print , 0 , 0 , 1 )
ZEND_ARG_INFO ( 0 , quick_print )
ZEND_END_ARG_INFO ( )
2008-07-02 03:14:28 +08:00
/* }}} */
2011-02-19 19:13:10 +08:00
struct objid_query {
2011-01-31 19:41:33 +08:00
int count ;
int offset ;
int step ;
2011-02-26 03:22:15 +08:00
long non_repeaters ;
long max_repetitions ;
2011-02-19 19:13:10 +08:00
int valueretrieval ;
2011-01-31 19:41:33 +08:00
int array_output ;
2011-07-21 20:48:47 +08:00
int oid_increasing_check ;
2011-02-01 15:45:30 +08:00
snmpobjarg * vars ;
2011-01-31 19:41:33 +08:00
} ;
2001-06-05 21:12:10 +08:00
/* {{{ snmp_functions[]
*/
2007-09-28 02:28:44 +08:00
const zend_function_entry snmp_functions [ ] = {
2011-01-31 19:41:33 +08:00
PHP_FE ( snmpget , arginfo_snmpget )
PHP_FE ( snmpgetnext , arginfo_snmpgetnext )
PHP_FE ( snmpwalk , arginfo_snmpwalk )
PHP_FE ( snmprealwalk , arginfo_snmprealwalk )
PHP_FALIAS ( snmpwalkoid , snmprealwalk , arginfo_snmprealwalk )
PHP_FE ( snmpset , arginfo_snmpset )
2008-07-02 03:14:28 +08:00
PHP_FE ( snmp_get_quick_print , arginfo_snmp_get_quick_print )
PHP_FE ( snmp_set_quick_print , arginfo_snmp_set_quick_print )
PHP_FE ( snmp_set_enum_print , arginfo_snmp_set_enum_print )
PHP_FE ( snmp_set_oid_output_format , arginfo_snmp_set_oid_output_format )
PHP_FALIAS ( snmp_set_oid_numeric_print , snmp_set_oid_output_format , arginfo_snmp_set_oid_output_format )
PHP_FE ( snmp2_get , arginfo_snmp2_get )
2011-01-31 19:41:33 +08:00
PHP_FE ( snmp2_getnext , arginfo_snmp2_getnext )
2008-07-02 03:14:28 +08:00
PHP_FE ( snmp2_walk , arginfo_snmp2_walk )
2011-01-31 19:41:33 +08:00
PHP_FE ( snmp2_real_walk , arginfo_snmp2_real_walk )
2008-07-02 03:14:28 +08:00
PHP_FE ( snmp2_set , arginfo_snmp2_set )
PHP_FE ( snmp3_get , arginfo_snmp3_get )
2011-01-31 19:41:33 +08:00
PHP_FE ( snmp3_getnext , arginfo_snmp3_getnext )
2008-07-02 03:14:28 +08:00
PHP_FE ( snmp3_walk , arginfo_snmp3_walk )
2011-01-31 19:41:33 +08:00
PHP_FE ( snmp3_real_walk , arginfo_snmp3_real_walk )
2008-07-02 03:14:28 +08:00
PHP_FE ( snmp3_set , arginfo_snmp3_set )
2011-01-31 19:41:33 +08:00
PHP_FE ( snmp_set_valueretrieval , arginfo_snmp_set_valueretrieval )
PHP_FE ( snmp_get_valueretrieval , arginfo_snmp_get_valueretrieval )
2008-07-02 03:14:28 +08:00
2011-01-31 19:41:33 +08:00
PHP_FE ( snmp_read_mib , arginfo_snmp_read_mib )
2011-07-25 19:35:02 +08:00
PHP_FE_END
1999-04-22 10:48:28 +08:00
} ;
2001-06-05 21:12:10 +08:00
/* }}} */
1999-04-22 10:48:28 +08:00
2011-01-31 19:41:33 +08:00
/* query an agent with GET method */
# define SNMP_CMD_GET (1<<0)
/* query an agent with GETNEXT method */
# define SNMP_CMD_GETNEXT (1<<1)
/* query an agent with SET method */
# define SNMP_CMD_SET (1<<2)
/* walk the mib */
# define SNMP_CMD_WALK (1<<3)
/* force values-only output */
# define SNMP_NUMERIC_KEYS (1<<7)
2011-05-08 21:58:37 +08:00
/* use user-supplied OID names for keys in array output mode in GET method */
# define SNMP_ORIGINAL_NAMES_AS_KEYS (1<<8)
/* use OID suffix (`index') for keys in array output mode in WALK method */
# define SNMP_USE_SUFFIX_AS_KEYS (1<<9)
1999-04-22 10:48:28 +08:00
2000-05-23 17:33:51 +08:00
# ifdef COMPILE_DL_SNMP
2000-05-02 08:30:36 +08:00
ZEND_GET_MODULE ( snmp )
1999-04-22 10:48:28 +08:00
# endif
1999-12-18 12:08:03 +08:00
/* THREAD_LS snmp_module php_snmp_module; - may need one of these at some point */
1999-04-22 10:48:28 +08:00
2006-06-13 21:12:20 +08:00
/* {{{ PHP_GINIT_FUNCTION
2003-07-15 01:11:04 +08:00
*/
2006-06-13 21:12:20 +08:00
static PHP_GINIT_FUNCTION ( snmp )
2003-07-15 01:11:04 +08:00
{
2007-07-09 21:41:45 +08:00
snmp_globals - > valueretrieval = SNMP_VALUE_LIBRARY ;
2003-07-15 01:11:04 +08:00
}
/* }}} */
2011-05-11 04:04:26 +08:00
# define PHP_SNMP_SESSION_FREE(a) { \
if ( ( * session ) - > a ) { \
efree ( ( * session ) - > a ) ; \
( * session ) - > a = NULL ; \
} \
}
2011-01-31 19:41:33 +08:00
static void netsnmp_session_free ( php_snmp_session * * session )
1999-12-18 12:08:03 +08:00
{
2011-01-31 19:41:33 +08:00
if ( * session ) {
2011-05-11 04:04:26 +08:00
PHP_SNMP_SESSION_FREE ( peername ) ;
PHP_SNMP_SESSION_FREE ( community ) ;
PHP_SNMP_SESSION_FREE ( securityName ) ;
PHP_SNMP_SESSION_FREE ( contextEngineID ) ;
2011-01-31 19:41:33 +08:00
efree ( * session ) ;
* session = NULL ;
}
}
2005-05-10 20:17:01 +08:00
2011-01-31 19:41:33 +08:00
static void php_snmp_session_destructor ( zend_rsrc_list_entry * rsrc TSRMLS_DC )
{
php_snmp_session * session = ( php_snmp_session * ) rsrc - > ptr ;
netsnmp_session_free ( & session ) ;
}
2006-07-27 13:12:33 +08:00
2011-01-31 19:41:33 +08:00
static void php_snmp_object_free_storage ( void * object TSRMLS_DC )
{
php_snmp_object * intern = ( php_snmp_object * ) object ;
if ( ! intern ) {
return ;
}
2003-07-15 01:11:04 +08:00
2011-01-31 19:41:33 +08:00
netsnmp_session_free ( & ( intern - > session ) ) ;
2003-07-15 01:11:04 +08:00
2011-01-31 19:41:33 +08:00
zend_object_std_dtor ( & intern - > zo TSRMLS_CC ) ;
2011-02-15 07:43:22 +08:00
efree ( intern ) ;
1999-04-22 10:48:28 +08:00
}
2011-01-31 19:41:33 +08:00
static zend_object_value php_snmp_object_new ( zend_class_entry * class_type TSRMLS_DC ) /* { { { */
2005-01-08 20:02:22 +08:00
{
2011-01-31 19:41:33 +08:00
zend_object_value retval ;
php_snmp_object * intern ;
2005-01-08 20:02:22 +08:00
2011-01-31 19:41:33 +08:00
/* Allocate memory for it */
intern = emalloc ( sizeof ( php_snmp_object ) ) ;
memset ( & intern - > zo , 0 , sizeof ( php_snmp_object ) ) ;
2005-01-08 20:02:22 +08:00
2011-01-31 19:41:33 +08:00
zend_object_std_init ( & intern - > zo , class_type TSRMLS_CC ) ;
2011-02-01 09:02:00 +08:00
object_properties_init ( & intern - > zo , class_type ) ;
2011-01-31 19:41:33 +08:00
2011-02-01 09:02:00 +08:00
retval . handle = zend_objects_store_put ( intern , ( zend_objects_store_dtor_t ) zend_objects_destroy_object , ( zend_objects_free_object_storage_t ) php_snmp_object_free_storage , NULL TSRMLS_CC ) ;
2011-01-31 19:41:33 +08:00
retval . handlers = ( zend_object_handlers * ) & php_snmp_object_handlers ;
return retval ;
1999-04-22 10:48:28 +08:00
}
2011-02-26 16:27:26 +08:00
/* {{{ php_snmp_error
*
* Record last SNMP - related error in object
*
*/
static void php_snmp_error ( zval * object , const char * docref TSRMLS_DC , int type , const char * format , . . . )
{
va_list args ;
php_snmp_object * snmp_object ;
if ( object ) {
snmp_object = ( php_snmp_object * ) zend_object_store_get_object ( object TSRMLS_CC ) ;
if ( type = = PHP_SNMP_ERRNO_NOERROR ) {
memset ( snmp_object - > snmp_errstr , 0 , sizeof ( snmp_object - > snmp_errstr ) ) ;
} else {
va_start ( args , format ) ;
vsnprintf ( snmp_object - > snmp_errstr , sizeof ( snmp_object - > snmp_errstr ) - 1 , format , args ) ;
va_end ( args ) ;
}
snmp_object - > snmp_errno = type ;
}
if ( type = = PHP_SNMP_ERRNO_NOERROR ) {
return ;
}
va_start ( args , format ) ;
php_verror ( docref , " " , E_WARNING , format , args TSRMLS_CC ) ;
va_end ( args ) ;
}
/* }}} */
2011-01-31 19:41:33 +08:00
/* {{{ php_snmp_getvalue
*
* SNMP value to zval converter
*
*/
static void php_snmp_getvalue ( struct variable_list * vars , zval * snmpval TSRMLS_DC , int valueretrieval )
2003-07-15 01:11:04 +08:00
{
zval * val ;
2011-04-05 20:20:35 +08:00
# ifdef BUGGY_SNMPRINT_VALUE
char sbuf [ 2048 ] ;
# else
2011-01-31 19:41:33 +08:00
char sbuf [ 64 ] ;
2011-04-05 20:20:35 +08:00
# endif
2011-01-31 19:41:33 +08:00
char * buf = & ( sbuf [ 0 ] ) ;
char * dbuf = ( char * ) NULL ;
int buflen = sizeof ( sbuf ) - 1 ;
int val_len = vars - > val_len ;
2011-07-18 01:18:31 +08:00
if ( ( valueretrieval & SNMP_VALUE_PLAIN ) = = 0 ) {
2011-01-31 19:41:33 +08:00
val_len + = 32 ; /* snprint_value will add type info into value, make some space for it */
}
/* use emalloc() for large values, use static array otherwize */
if ( val_len > buflen ) {
if ( ( dbuf = ( char * ) emalloc ( val_len + 1 ) ) ) {
buf = dbuf ;
buflen = val_len ;
} else {
2011-02-01 15:45:30 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " emalloc() failed: %s, fallback to static array " , strerror ( errno ) ) ;
2011-01-31 19:41:33 +08:00
}
}
2003-07-15 01:11:04 +08:00
2011-01-31 19:41:33 +08:00
* buf = 0 ;
2003-07-16 14:03:46 +08:00
2003-07-15 01:11:04 +08:00
MAKE_STD_ZVAL ( val ) ;
2011-07-18 01:18:31 +08:00
if ( valueretrieval & SNMP_VALUE_PLAIN ) {
switch ( vars - > type ) {
case ASN_BIT_STR : /* 0x03, asn1.h */
ZVAL_STRINGL ( val , ( char * ) vars - > val . bitstring , vars - > val_len , 1 ) ;
break ;
2003-07-15 01:11:04 +08:00
2011-07-18 01:18:31 +08:00
case ASN_OCTET_STR : /* 0x04, asn1.h */
case ASN_OPAQUE : /* 0x44, snmp_impl.h */
ZVAL_STRINGL ( val , ( char * ) vars - > val . string , vars - > val_len , 1 ) ;
break ;
2003-07-15 01:11:04 +08:00
2011-07-18 01:18:31 +08:00
case ASN_NULL : /* 0x05, asn1.h */
ZVAL_NULL ( val ) ;
break ;
2003-07-15 01:11:04 +08:00
2011-07-18 01:18:31 +08:00
case ASN_OBJECT_ID : /* 0x06, asn1.h */
snprint_objid ( buf , buflen , vars - > val . objid , vars - > val_len / sizeof ( oid ) ) ;
ZVAL_STRING ( val , buf , 1 ) ;
break ;
2003-07-15 01:11:04 +08:00
2011-07-18 01:18:31 +08:00
case ASN_IPADDRESS : /* 0x40, snmp_impl.h */
snprintf ( buf , buflen , " %d.%d.%d.%d " ,
( vars - > val . string ) [ 0 ] , ( vars - > val . string ) [ 1 ] ,
( vars - > val . string ) [ 2 ] , ( vars - > val . string ) [ 3 ] ) ;
buf [ buflen ] = 0 ;
ZVAL_STRING ( val , buf , 1 ) ;
break ;
2003-07-15 01:11:04 +08:00
2011-07-18 01:18:31 +08:00
case ASN_COUNTER : /* 0x41, snmp_impl.h */
case ASN_GAUGE : /* 0x42, snmp_impl.h */
/* ASN_UNSIGNED is the same as ASN_GAUGE */
case ASN_TIMETICKS : /* 0x43, snmp_impl.h */
case ASN_UINTEGER : /* 0x47, snmp_impl.h */
snprintf ( buf , buflen , " %lu " , * vars - > val . integer ) ;
buf [ buflen ] = 0 ;
ZVAL_STRING ( val , buf , 1 ) ;
break ;
case ASN_INTEGER : /* 0x02, asn1.h */
snprintf ( buf , buflen , " %ld " , * vars - > val . integer ) ;
buf [ buflen ] = 0 ;
ZVAL_STRING ( val , buf , 1 ) ;
break ;
2011-01-31 19:41:33 +08:00
2011-03-05 03:23:25 +08:00
# if defined(NETSNMP_WITH_OPAQUE_SPECIAL_TYPES) || defined(OPAQUE_SPECIAL_TYPES)
2011-07-18 01:18:31 +08:00
case ASN_OPAQUE_FLOAT : /* 0x78, asn1.h */
snprintf ( buf , buflen , " %f " , * vars - > val . floatVal ) ;
ZVAL_STRING ( val , buf , 1 ) ;
break ;
2011-01-31 19:41:33 +08:00
2011-07-18 01:18:31 +08:00
case ASN_OPAQUE_DOUBLE : /* 0x79, asn1.h */
snprintf ( buf , buflen , " %Lf " , * vars - > val . doubleVal ) ;
ZVAL_STRING ( val , buf , 1 ) ;
break ;
2011-01-31 19:41:33 +08:00
2011-07-18 01:18:31 +08:00
case ASN_OPAQUE_I64 : /* 0x80, asn1.h */
printI64 ( buf , vars - > val . counter64 ) ;
ZVAL_STRING ( val , buf , 1 ) ;
break ;
2003-07-15 01:11:04 +08:00
2011-07-18 01:18:31 +08:00
case ASN_OPAQUE_U64 : /* 0x81, asn1.h */
2011-01-31 19:41:33 +08:00
# endif
2011-07-18 01:18:31 +08:00
case ASN_COUNTER64 : /* 0x46, snmp_impl.h */
printU64 ( buf , vars - > val . counter64 ) ;
ZVAL_STRING ( val , buf , 1 ) ;
break ;
2003-07-15 01:11:04 +08:00
2011-07-18 01:18:31 +08:00
default :
ZVAL_STRING ( val , " Unknown value type " , 1 ) ;
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Unknown value type: %u " , vars - > type ) ;
break ;
}
} else /* use Net-SNMP value translation */ {
snprint_value ( buf , buflen , vars - > name , vars - > name_length , vars ) ;
ZVAL_STRING ( val , buf , 1 ) ;
2003-07-15 01:11:04 +08:00
}
2011-07-18 01:18:31 +08:00
if ( valueretrieval & SNMP_VALUE_OBJECT ) {
2003-07-15 01:11:04 +08:00
object_init ( snmpval ) ;
add_property_long ( snmpval , " type " , vars - > type ) ;
add_property_zval ( snmpval , " value " , val ) ;
2011-07-18 01:18:31 +08:00
} else {
* snmpval = * val ;
zval_copy_ctor ( snmpval ) ;
2003-07-15 01:11:04 +08:00
}
2011-02-27 03:08:55 +08:00
zval_ptr_dtor ( & val ) ;
2011-01-31 19:41:33 +08:00
if ( dbuf ) { /* malloc was used to store value */
efree ( dbuf ) ;
}
2003-07-15 01:11:04 +08:00
}
2011-01-31 19:41:33 +08:00
/* }}} */
2003-07-15 01:11:04 +08:00
2002-11-12 05:09:19 +08:00
/* {{{ php_snmp_internal
2001-06-05 21:12:10 +08:00
*
2011-01-31 19:41:33 +08:00
* SNMP object fetcher / setter for all SNMP versions
1999-08-05 04:56:33 +08:00
*
*/
2003-01-24 17:52:46 +08:00
static void php_snmp_internal ( INTERNAL_FUNCTION_PARAMETERS , int st ,
struct snmp_session * session ,
2011-02-19 19:13:10 +08:00
struct objid_query * objid_query )
2002-05-12 01:00:49 +08:00
{
2002-11-12 05:09:19 +08:00
struct snmp_session * ss ;
1999-04-22 10:48:28 +08:00
struct snmp_pdu * pdu = NULL , * response ;
struct variable_list * vars ;
2002-10-17 10:51:22 +08:00
oid name [ MAX_NAME_LEN ] ;
2005-04-16 06:06:03 +08:00
size_t name_length ;
1999-04-22 10:48:28 +08:00
oid root [ MAX_NAME_LEN ] ;
2005-04-16 06:06:03 +08:00
size_t rootlen = 0 ;
2002-11-12 05:09:19 +08:00
int gotroot = 0 ;
2011-05-08 21:58:37 +08:00
int status , count , found ;
1999-04-22 10:48:28 +08:00
char buf [ 2048 ] ;
char buf2 [ 2048 ] ;
int keepwalking = 1 ;
2002-11-12 05:09:19 +08:00
char * err ;
2003-07-16 14:03:46 +08:00
zval * snmpval = NULL ;
2011-01-31 19:41:33 +08:00
int snmp_errno ;
2000-03-26 03:58:19 +08:00
2011-01-31 19:41:33 +08:00
/* we start with retval=FALSE. If any actual data is aquired, retval will be set to appropriate type */
RETVAL_FALSE ;
2011-02-26 16:27:26 +08:00
/* reset errno and errstr */
php_snmp_error ( getThis ( ) , NULL TSRMLS_CC , PHP_SNMP_ERRNO_NOERROR , " " ) ;
2011-01-31 19:41:33 +08:00
if ( st & SNMP_CMD_WALK ) {
2011-02-19 19:13:10 +08:00
if ( objid_query - > count > 1 ) {
2011-01-31 19:41:33 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Multi OID walks are not supported! " ) ;
RETURN_FALSE ;
}
1999-04-22 10:48:28 +08:00
rootlen = MAX_NAME_LEN ;
2011-02-19 19:13:10 +08:00
if ( strlen ( objid_query - > vars [ 0 ] . oid ) ) { /* on a walk, an empty string means top of tree - no error */
if ( snmp_parse_oid ( objid_query - > vars [ 0 ] . oid , root , & rootlen ) ) {
1999-04-22 10:48:28 +08:00
gotroot = 1 ;
} else {
2011-02-19 19:13:10 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Invalid object identifier: %s " , objid_query - > vars [ 0 ] . oid ) ;
2011-02-27 03:08:55 +08:00
RETVAL_FALSE ;
return ;
1999-04-22 10:48:28 +08:00
}
}
2002-05-12 01:00:49 +08:00
if ( ! gotroot ) {
memmove ( ( char * ) root , ( char * ) objid_mib , sizeof ( objid_mib ) ) ;
1999-08-05 04:56:33 +08:00
rootlen = sizeof ( objid_mib ) / sizeof ( oid ) ;
gotroot = 1 ;
}
2011-01-31 19:41:33 +08:00
memmove ( ( char * ) name , ( char * ) root , rootlen * sizeof ( oid ) ) ;
name_length = rootlen ;
2011-02-19 19:13:10 +08:00
objid_query - > offset = objid_query - > count ;
2011-05-08 21:58:37 +08:00
memmove ( ( char * ) objid_query - > vars [ 0 ] . name , ( char * ) root , rootlen * sizeof ( oid ) ) ;
objid_query - > vars [ 0 ] . name_length = rootlen ;
1999-04-22 10:48:28 +08:00
}
2001-07-13 13:09:40 +08:00
2002-11-12 05:09:19 +08:00
if ( ( ss = snmp_open ( session ) ) = = NULL ) {
snmp_error ( session , NULL , NULL , & err ) ;
2002-10-16 10:33:47 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Could not open snmp connection: %s " , err ) ;
free ( err ) ;
2011-02-27 03:08:55 +08:00
RETVAL_FALSE ;
return ;
1999-04-22 10:48:28 +08:00
}
2000-03-26 03:58:19 +08:00
2002-05-12 01:00:49 +08:00
while ( keepwalking ) {
keepwalking = 0 ;
2011-01-31 19:41:33 +08:00
if ( st & ( SNMP_CMD_GET | SNMP_CMD_GETNEXT ) ) {
pdu = snmp_pdu_create ( ( st & SNMP_CMD_GET ) ? SNMP_MSG_GET : SNMP_MSG_GETNEXT ) ;
2011-02-19 19:13:10 +08:00
for ( count = 0 ; objid_query - > offset < objid_query - > count & & count < objid_query - > step ; objid_query - > offset + + , count + + ) {
2011-05-08 21:58:37 +08:00
objid_query - > vars [ objid_query - > offset ] . name_length = MAX_OID_LEN ;
if ( ! snmp_parse_oid ( objid_query - > vars [ objid_query - > offset ] . oid , objid_query - > vars [ objid_query - > offset ] . name , & ( objid_query - > vars [ objid_query - > offset ] . name_length ) ) ) {
2011-02-19 19:13:10 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Invalid object identifier: %s " , objid_query - > vars [ objid_query - > offset ] . oid ) ;
2011-01-31 19:41:33 +08:00
} else {
2011-05-08 21:58:37 +08:00
snmp_add_null_var ( pdu , objid_query - > vars [ objid_query - > offset ] . name , objid_query - > vars [ objid_query - > offset ] . name_length ) ;
2011-01-31 19:41:33 +08:00
}
1999-04-22 10:48:28 +08:00
}
2011-01-31 19:41:33 +08:00
if ( pdu - > variables = = NULL ) {
2008-09-06 15:58:09 +08:00
snmp_free_pdu ( pdu ) ;
2001-10-16 02:36:43 +08:00
snmp_close ( ss ) ;
2011-02-27 03:08:55 +08:00
RETVAL_FALSE ;
return ;
1999-08-05 04:56:33 +08:00
}
2011-01-31 19:41:33 +08:00
} else if ( st & SNMP_CMD_SET ) {
pdu = snmp_pdu_create ( SNMP_MSG_SET ) ;
2011-02-19 19:13:10 +08:00
for ( count = 0 ; objid_query - > offset < objid_query - > count & & count < objid_query - > step ; objid_query - > offset + + , count + + ) {
2011-05-08 21:58:37 +08:00
objid_query - > vars [ objid_query - > offset ] . name_length = MAX_OID_LEN ;
if ( ! snmp_parse_oid ( objid_query - > vars [ objid_query - > offset ] . oid , objid_query - > vars [ objid_query - > offset ] . name , & ( objid_query - > vars [ objid_query - > offset ] . name_length ) ) ) {
2011-02-19 19:13:10 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Invalid object identifier: %s " , objid_query - > vars [ objid_query - > offset ] . oid ) ;
2011-01-31 19:41:33 +08:00
snmp_free_pdu ( pdu ) ;
snmp_close ( ss ) ;
2011-02-27 03:08:55 +08:00
RETVAL_FALSE ;
return ;
2011-01-31 19:41:33 +08:00
} else {
2011-05-08 21:58:37 +08:00
if ( ( snmp_errno = snmp_add_var ( pdu , objid_query - > vars [ objid_query - > offset ] . name , objid_query - > vars [ objid_query - > offset ] . name_length , objid_query - > vars [ objid_query - > offset ] . type , objid_query - > vars [ objid_query - > offset ] . value ) ) ) {
snprint_objid ( buf , sizeof ( buf ) , objid_query - > vars [ objid_query - > offset ] . name , objid_query - > vars [ objid_query - > offset ] . name_length ) ;
2011-02-19 19:13:10 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Could not add variable: OID='%s' type='%c' value='%s': %s " , buf , objid_query - > vars [ objid_query - > offset ] . type , objid_query - > vars [ objid_query - > offset ] . value , snmp_api_errstring ( snmp_errno ) ) ;
2011-01-31 19:41:33 +08:00
snmp_free_pdu ( pdu ) ;
snmp_close ( ss ) ;
2011-02-27 03:08:55 +08:00
RETVAL_FALSE ;
return ;
2011-01-31 19:41:33 +08:00
}
}
}
} else if ( st & SNMP_CMD_WALK ) {
2006-07-27 13:12:33 +08:00
if ( session - > version = = SNMP_VERSION_1 ) {
2005-04-11 22:56:44 +08:00
pdu = snmp_pdu_create ( SNMP_MSG_GETNEXT ) ;
} else {
pdu = snmp_pdu_create ( SNMP_MSG_GETBULK ) ;
2011-02-19 19:13:10 +08:00
pdu - > non_repeaters = objid_query - > non_repeaters ;
pdu - > max_repetitions = objid_query - > max_repetitions ;
2006-07-27 13:12:33 +08:00
}
2000-03-26 03:58:19 +08:00
snmp_add_null_var ( pdu , name , name_length ) ;
1999-08-05 04:56:33 +08:00
}
2002-11-12 05:09:19 +08:00
1999-04-22 10:48:28 +08:00
retry :
status = snmp_synch_response ( ss , pdu , & response ) ;
if ( status = = STAT_SUCCESS ) {
if ( response - > errstat = = SNMP_ERR_NOERROR ) {
2011-01-31 19:41:33 +08:00
if ( st & SNMP_CMD_SET ) {
snmp_free_pdu ( response ) ;
snmp_close ( ss ) ;
2011-02-27 03:08:55 +08:00
RETVAL_TRUE ;
return ;
2011-01-31 19:41:33 +08:00
}
2000-03-26 03:58:19 +08:00
for ( vars = response - > variables ; vars ; vars = vars - > next_variable ) {
2011-01-31 19:41:33 +08:00
/* do not output errors as values */
if ( vars - > type = = SNMP_ENDOFMIBVIEW | |
vars - > type = = SNMP_NOSUCHOBJECT | |
vars - > type = = SNMP_NOSUCHINSTANCE ) {
if ( ( st & SNMP_CMD_WALK ) & & Z_TYPE_P ( return_value ) = = IS_ARRAY ) {
break ;
}
2011-04-05 20:20:35 +08:00
snprint_objid ( buf , sizeof ( buf ) , vars - > name , vars - > name_length ) ;
snprint_value ( buf2 , sizeof ( buf2 ) , vars - > name , vars - > name_length , vars ) ;
2011-02-26 16:27:26 +08:00
php_snmp_error ( getThis ( ) , NULL TSRMLS_CC , PHP_SNMP_ERRNO_ERROR_IN_REPLY , " Error in packet at '%s': %s " , buf , buf2 ) ;
2011-01-31 19:41:33 +08:00
continue ;
2000-03-26 03:58:19 +08:00
}
2011-01-31 19:41:33 +08:00
if ( ( st & SNMP_CMD_WALK ) & &
( vars - > name_length < rootlen | | memcmp ( root , vars - > name , rootlen * sizeof ( oid ) ) ) ) { /* not part of this subtree */
if ( Z_TYPE_P ( return_value ) = = IS_ARRAY ) { /* some records are fetched already, shut down further lookup */
keepwalking = 0 ;
} else {
/* first fetched OID is out of subtree, fallback to GET query */
st | = SNMP_CMD_GET ;
st ^ = SNMP_CMD_WALK ;
2011-02-19 19:13:10 +08:00
objid_query - > offset = 0 ;
2011-01-31 19:41:33 +08:00
keepwalking = 1 ;
}
break ;
2000-03-26 03:58:19 +08:00
}
2002-05-12 01:00:49 +08:00
2011-01-31 19:41:33 +08:00
MAKE_STD_ZVAL ( snmpval ) ;
2011-02-19 19:13:10 +08:00
php_snmp_getvalue ( vars , snmpval TSRMLS_CC , objid_query - > valueretrieval ) ;
2011-01-31 19:41:33 +08:00
2011-02-19 19:13:10 +08:00
if ( objid_query - > array_output ) {
2011-01-31 19:41:33 +08:00
if ( Z_TYPE_P ( return_value ) = = IS_BOOL ) {
array_init ( return_value ) ;
}
if ( st & SNMP_NUMERIC_KEYS ) {
add_next_index_zval ( return_value , snmpval ) ;
2011-05-08 21:58:37 +08:00
} else if ( st & SNMP_ORIGINAL_NAMES_AS_KEYS & & st & SNMP_CMD_GET ) {
found = 0 ;
for ( count = 0 ; count < objid_query - > count ; count + + ) {
if ( objid_query - > vars [ count ] . name_length = = vars - > name_length & & snmp_oid_compare ( objid_query - > vars [ count ] . name , objid_query - > vars [ count ] . name_length , vars - > name , vars - > name_length ) = = 0 ) {
found = 1 ;
objid_query - > vars [ count ] . name_length = 0 ; /* mark this name as used */
break ;
}
}
if ( found ) {
add_assoc_zval ( return_value , objid_query - > vars [ count ] . oid , snmpval ) ;
} else {
snprint_objid ( buf2 , sizeof ( buf2 ) , vars - > name , vars - > name_length ) ;
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Could not find original OID name for '%s' " , buf2 ) ;
}
} else if ( st & SNMP_USE_SUFFIX_AS_KEYS & & st & SNMP_CMD_WALK ) {
snprint_objid ( buf2 , sizeof ( buf2 ) , vars - > name , vars - > name_length ) ;
if ( objid_query - > vars [ 0 ] . name_length < = vars - > name_length & & snmp_oid_compare ( objid_query - > vars [ 0 ] . name , objid_query - > vars [ 0 ] . name_length , vars - > name , objid_query - > vars [ 0 ] . name_length ) = = 0 ) {
buf2 [ 0 ] = ' \0 ' ;
count = objid_query - > vars [ 0 ] . name_length ;
while ( count < vars - > name_length ) {
sprintf ( buf , " %lu. " , vars - > name [ count ] ) ;
strcat ( buf2 , buf ) ;
count + + ;
}
buf2 [ strlen ( buf2 ) - 1 ] = ' \0 ' ; /* remove trailing '.' */
}
add_assoc_zval ( return_value , buf2 , snmpval ) ;
2011-01-31 19:41:33 +08:00
} else {
2011-04-05 20:20:35 +08:00
snprint_objid ( buf2 , sizeof ( buf2 ) , vars - > name , vars - > name_length ) ;
2011-01-31 19:41:33 +08:00
add_assoc_zval ( return_value , buf2 , snmpval ) ;
}
} else {
2003-07-15 01:11:04 +08:00
* return_value = * snmpval ;
zval_copy_ctor ( return_value ) ;
2005-04-28 07:27:37 +08:00
zval_ptr_dtor ( & snmpval ) ;
2011-01-31 19:41:33 +08:00
break ;
1999-04-22 10:48:28 +08:00
}
2011-01-31 19:41:33 +08:00
/* OID increase check */
if ( st & SNMP_CMD_WALK ) {
2011-07-21 20:48:47 +08:00
if ( objid_query - > oid_increasing_check = = TRUE & & snmp_oid_compare ( name , name_length , vars - > name , vars - > name_length ) > = 0 ) {
2011-05-08 17:20:58 +08:00
snprint_objid ( buf2 , sizeof ( buf2 ) , vars - > name , vars - > name_length ) ;
php_snmp_error ( getThis ( ) , NULL TSRMLS_CC , PHP_SNMP_ERRNO_OID_NOT_INCREASING , " Error: OID not increasing: %s " , buf2 ) ;
2011-01-31 19:41:33 +08:00
keepwalking = 0 ;
} else {
2011-05-08 17:20:58 +08:00
memmove ( ( char * ) name , ( char * ) vars - > name , vars - > name_length * sizeof ( oid ) ) ;
2011-01-31 19:41:33 +08:00
name_length = vars - > name_length ;
keepwalking = 1 ;
1999-04-22 10:48:28 +08:00
}
}
2011-01-31 19:41:33 +08:00
}
2011-02-19 19:13:10 +08:00
if ( objid_query - > offset < objid_query - > count ) { /* we have unprocessed OIDs */
2011-01-31 19:41:33 +08:00
keepwalking = 1 ;
}
1999-04-22 10:48:28 +08:00
} else {
2011-01-31 19:41:33 +08:00
if ( ! ( st & SNMP_CMD_WALK ) | | response - > errstat ! = SNMP_ERR_NOSUCHNAME | | Z_TYPE_P ( return_value ) = = IS_BOOL ) {
for ( count = 1 , vars = response - > variables ;
vars & & count ! = response - > errindex ;
1999-08-05 04:56:33 +08:00
vars = vars - > next_variable , count + + ) ;
2011-01-31 19:41:33 +08:00
2011-02-19 19:13:10 +08:00
if ( st & ( SNMP_CMD_GET | SNMP_CMD_GETNEXT ) & & response - > errstat = = SNMP_ERR_TOOBIG & & objid_query - > step > 1 ) { /* Answer will not fit into single packet */
objid_query - > offset = ( ( objid_query - > offset > objid_query - > step ) ? ( objid_query - > offset - objid_query - > step ) : 0 ) ;
objid_query - > step / = 2 ;
2011-01-31 19:41:33 +08:00
snmp_free_pdu ( response ) ;
keepwalking = 1 ;
continue ;
1999-04-22 10:48:28 +08:00
}
2011-01-31 19:41:33 +08:00
if ( vars ) {
2011-04-05 20:20:35 +08:00
snprint_objid ( buf , sizeof ( buf ) , vars - > name , vars - > name_length ) ;
2011-02-26 16:27:26 +08:00
php_snmp_error ( getThis ( ) , NULL TSRMLS_CC , PHP_SNMP_ERRNO_ERROR_IN_REPLY , " Error in packet at '%s': %s " , buf , snmp_errstring ( response - > errstat ) ) ;
2011-01-31 19:41:33 +08:00
} else {
2011-02-26 16:27:26 +08:00
php_snmp_error ( getThis ( ) , NULL TSRMLS_CC , PHP_SNMP_ERRNO_ERROR_IN_REPLY , " Error in packet at %u object_id: %s " , response - > errindex , snmp_errstring ( response - > errstat ) ) ;
2011-01-31 19:41:33 +08:00
}
if ( st & ( SNMP_CMD_GET | SNMP_CMD_GETNEXT ) ) { /* cut out bogus OID and retry */
if ( ( pdu = snmp_fix_pdu ( response , ( ( st & SNMP_CMD_GET ) ? SNMP_MSG_GET : SNMP_MSG_GETNEXT ) ) ) ! = NULL ) {
2009-05-03 05:13:39 +08:00
snmp_free_pdu ( response ) ;
2005-04-11 22:56:44 +08:00
goto retry ;
}
1999-04-22 10:48:28 +08:00
}
2009-05-03 05:13:39 +08:00
snmp_free_pdu ( response ) ;
2001-10-16 02:36:43 +08:00
snmp_close ( ss ) ;
2011-02-19 19:13:10 +08:00
if ( objid_query - > array_output ) {
2006-07-27 13:12:33 +08:00
zval_dtor ( return_value ) ;
}
2011-02-27 03:08:55 +08:00
RETVAL_FALSE ;
return ;
1999-04-22 10:48:28 +08:00
}
}
} else if ( status = = STAT_TIMEOUT ) {
2011-02-26 16:27:26 +08:00
php_snmp_error ( getThis ( ) , NULL TSRMLS_CC , PHP_SNMP_ERRNO_TIMEOUT , " No response from %s " , session - > peername ) ;
2011-02-19 19:13:10 +08:00
if ( objid_query - > array_output ) {
2002-10-16 10:33:47 +08:00
zval_dtor ( return_value ) ;
}
2001-10-16 02:36:43 +08:00
snmp_close ( ss ) ;
2011-02-27 03:08:55 +08:00
RETVAL_FALSE ;
return ;
1999-04-22 10:48:28 +08:00
} else { /* status == STAT_ERROR */
2011-01-31 19:41:33 +08:00
snmp_error ( ss , NULL , NULL , & err ) ;
2011-02-26 16:27:26 +08:00
php_snmp_error ( getThis ( ) , NULL TSRMLS_CC , PHP_SNMP_ERRNO_GENERIC , " Fatal error: %s " , err ) ;
2011-01-31 19:41:33 +08:00
free ( err ) ;
2011-02-19 19:13:10 +08:00
if ( objid_query - > array_output ) {
2002-10-16 10:33:47 +08:00
zval_dtor ( return_value ) ;
}
2001-10-16 02:36:43 +08:00
snmp_close ( ss ) ;
2011-02-27 03:08:55 +08:00
RETVAL_FALSE ;
return ;
1999-04-22 10:48:28 +08:00
}
2000-03-26 03:58:19 +08:00
if ( response ) {
snmp_free_pdu ( response ) ;
}
1999-04-22 10:48:28 +08:00
} /* keepwalking */
snmp_close ( ss ) ;
}
2001-06-05 21:12:10 +08:00
/* }}} */
1999-04-22 10:48:28 +08:00
2011-01-31 19:41:33 +08:00
/* {{{ php_snmp_parse_oid
2002-11-12 05:09:19 +08:00
*
2011-01-31 19:41:33 +08:00
* OID parser ( and type , value for SNMP_SET command )
2002-11-12 05:09:19 +08:00
*/
2011-01-31 19:41:33 +08:00
2011-02-19 19:13:10 +08:00
static int php_snmp_parse_oid ( int st , struct objid_query * objid_query , zval * * oid , zval * * type , zval * * value TSRMLS_DC )
2002-11-12 05:09:19 +08:00
{
char * pptr ;
2011-01-31 19:41:33 +08:00
HashPosition pos_oid , pos_type , pos_value ;
zval * * tmp_oid , * * tmp_type , * * tmp_value ;
2002-11-12 05:09:19 +08:00
2011-01-31 19:41:33 +08:00
if ( Z_TYPE_PP ( oid ) ! = IS_ARRAY ) {
if ( Z_ISREF_PP ( oid ) ) {
SEPARATE_ZVAL ( oid ) ;
2008-08-13 22:05:55 +08:00
}
2011-01-31 19:41:33 +08:00
convert_to_string_ex ( oid ) ;
} else if ( Z_TYPE_PP ( oid ) = = IS_ARRAY ) {
zend_hash_internal_pointer_reset_ex ( Z_ARRVAL_PP ( oid ) , & pos_oid ) ;
2002-11-12 05:09:19 +08:00
}
2003-07-15 02:10:22 +08:00
2011-01-31 19:41:33 +08:00
if ( st & SNMP_CMD_SET ) {
if ( Z_TYPE_PP ( type ) ! = IS_ARRAY ) {
if ( Z_ISREF_PP ( type ) ) {
SEPARATE_ZVAL ( type ) ;
}
convert_to_string_ex ( type ) ;
} else if ( Z_TYPE_PP ( type ) = = IS_ARRAY ) {
zend_hash_internal_pointer_reset_ex ( Z_ARRVAL_PP ( type ) , & pos_type ) ;
}
1999-04-22 10:48:28 +08:00
2011-01-31 19:41:33 +08:00
if ( Z_TYPE_PP ( value ) ! = IS_ARRAY ) {
if ( Z_ISREF_PP ( value ) ) {
SEPARATE_ZVAL ( value ) ;
}
convert_to_string_ex ( value ) ;
} else if ( Z_TYPE_PP ( value ) = = IS_ARRAY ) {
zend_hash_internal_pointer_reset_ex ( Z_ARRVAL_PP ( value ) , & pos_value ) ;
}
2001-08-13 15:55:39 +08:00
}
2001-08-13 14:43:47 +08:00
2011-02-19 19:13:10 +08:00
objid_query - > count = 0 ;
objid_query - > array_output = ( ( st & SNMP_CMD_WALK ) ? TRUE : FALSE ) ;
2011-01-31 19:41:33 +08:00
if ( Z_TYPE_PP ( oid ) = = IS_STRING ) {
2011-02-19 19:13:10 +08:00
objid_query - > vars = ( snmpobjarg * ) emalloc ( sizeof ( snmpobjarg ) ) ;
if ( objid_query - > vars = = NULL ) {
2011-02-01 15:45:30 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " emalloc() failed while parsing oid: %s " , strerror ( errno ) ) ;
2011-02-27 03:08:55 +08:00
efree ( objid_query - > vars ) ;
2011-02-01 15:45:30 +08:00
return FALSE ;
}
2011-02-19 19:13:10 +08:00
objid_query - > vars [ objid_query - > count ] . oid = Z_STRVAL_PP ( oid ) ;
2011-01-31 19:41:33 +08:00
if ( st & SNMP_CMD_SET ) {
if ( Z_TYPE_PP ( type ) = = IS_STRING & & Z_TYPE_PP ( value ) = = IS_STRING ) {
if ( Z_STRLEN_PP ( type ) ! = 1 ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Bogus type '%s', should be single char, got %u " , Z_STRVAL_PP ( type ) , Z_STRLEN_PP ( type ) ) ;
2011-02-27 03:08:55 +08:00
efree ( objid_query - > vars ) ;
2011-01-31 19:41:33 +08:00
return FALSE ;
}
pptr = Z_STRVAL_PP ( type ) ;
2011-02-19 19:13:10 +08:00
objid_query - > vars [ objid_query - > count ] . type = * pptr ;
objid_query - > vars [ objid_query - > count ] . value = Z_STRVAL_PP ( value ) ;
2011-01-31 19:41:33 +08:00
} else {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Single objid and multiple type or values are not supported " ) ;
2011-02-27 03:08:55 +08:00
efree ( objid_query - > vars ) ;
2011-01-31 19:41:33 +08:00
return FALSE ;
}
}
2011-02-19 19:13:10 +08:00
objid_query - > count + + ;
2011-02-17 20:02:16 +08:00
} else if ( Z_TYPE_PP ( oid ) = = IS_ARRAY ) { /* we got objid array */
2011-01-31 19:41:33 +08:00
if ( zend_hash_num_elements ( Z_ARRVAL_PP ( oid ) ) = = 0 ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Got empty OID array " ) ;
return FALSE ;
}
2011-02-19 19:13:10 +08:00
objid_query - > vars = ( snmpobjarg * ) emalloc ( sizeof ( snmpobjarg ) * zend_hash_num_elements ( Z_ARRVAL_PP ( oid ) ) ) ;
if ( objid_query - > vars = = NULL ) {
2011-02-01 15:45:30 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " emalloc() failed while parsing oid array: %s " , strerror ( errno ) ) ;
2011-02-27 03:08:55 +08:00
efree ( objid_query - > vars ) ;
2011-02-01 15:45:30 +08:00
return FALSE ;
}
2011-02-19 19:13:10 +08:00
objid_query - > array_output = ( ( st & SNMP_CMD_SET ) ? FALSE : TRUE ) ;
2011-01-31 19:41:33 +08:00
for ( zend_hash_internal_pointer_reset_ex ( Z_ARRVAL_PP ( oid ) , & pos_oid ) ;
zend_hash_get_current_data_ex ( Z_ARRVAL_PP ( oid ) , ( void * * ) & tmp_oid , & pos_oid ) = = SUCCESS ;
zend_hash_move_forward_ex ( Z_ARRVAL_PP ( oid ) , & pos_oid ) ) {
convert_to_string_ex ( tmp_oid ) ;
2011-02-19 19:13:10 +08:00
objid_query - > vars [ objid_query - > count ] . oid = Z_STRVAL_PP ( tmp_oid ) ;
2011-01-31 19:41:33 +08:00
if ( st & SNMP_CMD_SET ) {
if ( Z_TYPE_PP ( type ) = = IS_STRING ) {
pptr = Z_STRVAL_PP ( type ) ;
2011-02-19 19:13:10 +08:00
objid_query - > vars [ objid_query - > count ] . type = * pptr ;
2011-01-31 19:41:33 +08:00
} else if ( Z_TYPE_PP ( type ) = = IS_ARRAY ) {
if ( SUCCESS = = zend_hash_get_current_data_ex ( Z_ARRVAL_PP ( type ) , ( void * * ) & tmp_type , & pos_type ) ) {
convert_to_string_ex ( tmp_type ) ;
if ( Z_STRLEN_PP ( tmp_type ) ! = 1 ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " '%s': bogus type '%s', should be single char, got %u " , Z_STRVAL_PP ( tmp_oid ) , Z_STRVAL_PP ( tmp_type ) , Z_STRLEN_PP ( tmp_type ) ) ;
2011-02-27 03:08:55 +08:00
efree ( objid_query - > vars ) ;
2011-01-31 19:41:33 +08:00
return FALSE ;
}
pptr = Z_STRVAL_PP ( tmp_type ) ;
2011-02-19 19:13:10 +08:00
objid_query - > vars [ objid_query - > count ] . type = * pptr ;
2011-01-31 19:41:33 +08:00
zend_hash_move_forward_ex ( Z_ARRVAL_PP ( type ) , & pos_type ) ;
} else {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " '%s': no type set " , Z_STRVAL_PP ( tmp_oid ) ) ;
2011-02-27 03:08:55 +08:00
efree ( objid_query - > vars ) ;
2011-01-31 19:41:33 +08:00
return FALSE ;
}
}
1999-08-05 04:56:33 +08:00
2011-01-31 19:41:33 +08:00
if ( Z_TYPE_PP ( value ) = = IS_STRING ) {
2011-02-19 19:13:10 +08:00
objid_query - > vars [ objid_query - > count ] . value = Z_STRVAL_PP ( value ) ;
2011-01-31 19:41:33 +08:00
} else if ( Z_TYPE_PP ( value ) = = IS_ARRAY ) {
if ( SUCCESS = = zend_hash_get_current_data_ex ( Z_ARRVAL_PP ( value ) , ( void * * ) & tmp_value , & pos_value ) ) {
convert_to_string_ex ( tmp_value ) ;
2011-02-19 19:13:10 +08:00
objid_query - > vars [ objid_query - > count ] . value = Z_STRVAL_PP ( tmp_value ) ;
2011-01-31 19:41:33 +08:00
zend_hash_move_forward_ex ( Z_ARRVAL_PP ( value ) , & pos_value ) ;
} else {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " '%s': no value set " , Z_STRVAL_PP ( tmp_oid ) ) ;
2011-02-27 03:08:55 +08:00
efree ( objid_query - > vars ) ;
2011-01-31 19:41:33 +08:00
return FALSE ;
}
}
}
2011-02-19 19:13:10 +08:00
objid_query - > count + + ;
2011-01-31 19:41:33 +08:00
}
2001-08-14 03:31:18 +08:00
}
2001-08-13 15:55:39 +08:00
2011-02-19 19:13:10 +08:00
objid_query - > offset = 0 ;
objid_query - > step = objid_query - > count ;
return ( objid_query - > count > 0 ) ;
1999-08-05 04:56:33 +08:00
}
/* }}} */
2011-01-31 19:41:33 +08:00
/* {{{ netsnmp_session_init
allocates memory for session and session - > peername , caller should free it manually using netsnmp_session_free ( ) and efree ( )
*/
2011-02-01 09:02:00 +08:00
static int netsnmp_session_init ( php_snmp_session * * session_p , int version , char * hostname , char * community , int timeout , int retries TSRMLS_DC )
2002-11-12 05:37:19 +08:00
{
2011-01-31 19:41:33 +08:00
int remote_port = SNMP_PORT ;
php_snmp_session * session ;
char * pptr ;
2002-11-12 05:37:19 +08:00
2011-01-31 19:41:33 +08:00
* session_p = ( php_snmp_session * ) emalloc ( sizeof ( php_snmp_session ) ) ;
session = * session_p ;
if ( session = = NULL ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " emalloc() failed allocating session " ) ;
return ( - 1 ) ;
2002-11-12 05:37:19 +08:00
}
2011-01-31 19:41:33 +08:00
snmp_sess_init ( session ) ;
2002-11-12 05:37:19 +08:00
2011-01-31 19:41:33 +08:00
session - > version = version ;
2002-11-12 05:37:19 +08:00
2011-01-31 19:41:33 +08:00
session - > peername = emalloc ( MAX_NAME_LEN ) ;
if ( session - > peername = = NULL ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " emalloc() failed while copying hostname " ) ;
netsnmp_session_free ( & session ) ;
return ( - 1 ) ;
2002-11-12 05:37:19 +08:00
}
2006-07-27 13:12:33 +08:00
2011-01-31 19:41:33 +08:00
/* Reading the hostname and its optional non-default port number */
strlcpy ( session - > peername , hostname , MAX_NAME_LEN ) ;
if ( ( pptr = strchr ( session - > peername , ' : ' ) ) ) {
remote_port = strtol ( pptr + 1 , NULL , 0 ) ;
2002-12-06 03:33:03 +08:00
}
2006-07-27 13:12:33 +08:00
2011-01-31 19:41:33 +08:00
session - > remote_port = remote_port ;
1999-08-05 04:56:33 +08:00
2011-01-31 19:41:33 +08:00
if ( version = = SNMP_VERSION_3 ) {
/* Setting the security name. */
2011-05-11 04:04:26 +08:00
session - > securityName = estrdup ( community ) ;
2011-01-31 19:41:33 +08:00
session - > securityNameLen = strlen ( session - > securityName ) ;
} else {
session - > authenticator = NULL ;
2011-05-11 04:04:26 +08:00
session - > community = ( u_char * ) estrdup ( community ) ;
2011-01-31 19:41:33 +08:00
session - > community_len = strlen ( community ) ;
2002-12-06 03:33:03 +08:00
}
2011-01-31 19:41:33 +08:00
session - > retries = retries ;
session - > timeout = timeout ;
return ( 0 ) ;
2002-11-12 05:09:19 +08:00
}
/* }}} */
2003-08-08 00:46:11 +08:00
/* {{{ int netsnmp_session_set_sec_level(struct snmp_session *s, char *level)
2002-12-06 03:33:03 +08:00
Set the security level in the snmpv3 session */
2011-02-01 09:02:00 +08:00
static int netsnmp_session_set_sec_level ( struct snmp_session * s , char * level )
2002-11-12 05:09:19 +08:00
{
2011-01-31 19:41:33 +08:00
if ( ! strcasecmp ( level , " noAuthNoPriv " ) | | ! strcasecmp ( level , " nanp " ) ) {
s - > securityLevel = SNMP_SEC_LEVEL_NOAUTH ;
} else if ( ! strcasecmp ( level , " authNoPriv " ) | | ! strcasecmp ( level , " anp " ) ) {
s - > securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV ;
} else if ( ! strcasecmp ( level , " authPriv " ) | | ! strcasecmp ( level , " ap " ) ) {
s - > securityLevel = SNMP_SEC_LEVEL_AUTHPRIV ;
} else {
return ( - 1 ) ;
2002-12-06 03:33:03 +08:00
}
2011-01-31 19:41:33 +08:00
return ( 0 ) ;
2002-11-12 05:09:19 +08:00
}
/* }}} */
2003-08-08 00:46:11 +08:00
/* {{{ int netsnmp_session_set_auth_protocol(struct snmp_session *s, char *prot)
2002-12-06 03:33:03 +08:00
Set the authentication protocol in the snmpv3 session */
2002-12-11 01:07:40 +08:00
static int netsnmp_session_set_auth_protocol ( struct snmp_session * s , char * prot TSRMLS_DC )
2002-11-12 05:09:19 +08:00
{
2011-01-31 19:41:33 +08:00
if ( ! strcasecmp ( prot , " MD5 " ) ) {
s - > securityAuthProto = usmHMACMD5AuthProtocol ;
2011-04-06 14:58:25 +08:00
s - > securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN ;
2011-01-31 19:41:33 +08:00
} else if ( ! strcasecmp ( prot , " SHA " ) ) {
s - > securityAuthProto = usmHMACSHA1AuthProtocol ;
2011-04-06 14:58:25 +08:00
s - > securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN ;
2011-01-31 19:41:33 +08:00
} else {
2011-02-01 09:02:00 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Unknown authentication protocol '%s' " , prot ) ;
2011-01-31 19:41:33 +08:00
return ( - 1 ) ;
2002-12-06 03:33:03 +08:00
}
2011-01-31 19:41:33 +08:00
return ( 0 ) ;
2002-11-12 05:09:19 +08:00
}
/* }}} */
2003-08-08 00:46:11 +08:00
/* {{{ int netsnmp_session_set_sec_protocol(struct snmp_session *s, char *prot)
2002-12-06 03:33:03 +08:00
Set the security protocol in the snmpv3 session */
2002-12-11 01:07:40 +08:00
static int netsnmp_session_set_sec_protocol ( struct snmp_session * s , char * prot TSRMLS_DC )
2002-11-12 05:09:19 +08:00
{
2011-01-31 19:41:33 +08:00
if ( ! strcasecmp ( prot , " DES " ) ) {
s - > securityPrivProto = usmDESPrivProtocol ;
2011-04-06 14:58:25 +08:00
s - > securityPrivProtoLen = USM_PRIV_PROTO_DES_LEN ;
2002-11-12 05:09:19 +08:00
# ifdef HAVE_AES
2011-04-06 14:58:25 +08:00
} else if ( ! strcasecmp ( prot , " AES128 " ) | | ! strcasecmp ( prot , " AES " ) ) {
s - > securityPrivProto = usmAESPrivProtocol ;
s - > securityPrivProtoLen = USM_PRIV_PROTO_AES_LEN ;
2002-11-12 05:09:19 +08:00
# endif
2011-01-31 19:41:33 +08:00
} else {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Unknown security protocol '%s' " , prot ) ;
return ( - 1 ) ;
2002-12-06 03:33:03 +08:00
}
2011-01-31 19:41:33 +08:00
return ( 0 ) ;
2002-11-12 05:09:19 +08:00
}
/* }}} */
2003-08-08 00:46:11 +08:00
/* {{{ int netsnmp_session_gen_auth_key(struct snmp_session *s, char *pass)
2002-12-06 03:33:03 +08:00
Make key from pass phrase in the snmpv3 session */
2002-12-11 01:07:40 +08:00
static int netsnmp_session_gen_auth_key ( struct snmp_session * s , char * pass TSRMLS_DC )
2002-11-12 05:09:19 +08:00
{
2011-01-31 19:41:33 +08:00
int snmp_errno ;
s - > securityAuthKeyLen = USM_AUTH_KU_LEN ;
if ( ( snmp_errno = generate_Ku ( s - > securityAuthProto , s - > securityAuthProtoLen ,
( u_char * ) pass , strlen ( pass ) ,
s - > securityAuthKey , & ( s - > securityAuthKeyLen ) ) ) ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Error generating a key for authentication pass phrase '%s': %s " , pass , snmp_api_errstring ( snmp_errno ) ) ;
return ( - 1 ) ;
2002-12-06 03:33:03 +08:00
}
2011-01-31 19:41:33 +08:00
return ( 0 ) ;
2002-11-12 05:09:19 +08:00
}
/* }}} */
2003-08-08 00:46:11 +08:00
/* {{{ int netsnmp_session_gen_sec_key(struct snmp_session *s, u_char *pass)
2002-12-06 03:33:03 +08:00
Make key from pass phrase in the snmpv3 session */
2011-02-27 03:08:55 +08:00
static int netsnmp_session_gen_sec_key ( struct snmp_session * s , char * pass TSRMLS_DC )
2002-11-12 05:09:19 +08:00
{
2011-01-31 19:41:33 +08:00
int snmp_errno ;
s - > securityPrivKeyLen = USM_PRIV_KU_LEN ;
if ( ( snmp_errno = generate_Ku ( s - > securityAuthProto , s - > securityAuthProtoLen ,
2011-02-27 03:08:55 +08:00
( u_char * ) pass , strlen ( pass ) ,
2011-01-31 19:41:33 +08:00
s - > securityPrivKey , & ( s - > securityPrivKeyLen ) ) ) ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Error generating a key for privacy pass phrase '%s': %s " , pass , snmp_api_errstring ( snmp_errno ) ) ;
return ( - 2 ) ;
2002-12-06 03:33:03 +08:00
}
2011-01-31 19:41:33 +08:00
return ( 0 ) ;
2002-11-12 05:09:19 +08:00
}
/* }}} */
2011-01-31 19:41:33 +08:00
/* {{{ in netsnmp_session_set_contextEngineID(struct snmp_session *s, u_char * contextEngineID)
Set context Engine Id in the snmpv3 session */
2011-02-27 03:08:55 +08:00
static int netsnmp_session_set_contextEngineID ( struct snmp_session * s , char * contextEngineID TSRMLS_DC )
2005-03-16 22:29:40 +08:00
{
2011-01-31 19:41:33 +08:00
size_t ebuf_len = 32 , eout_len = 0 ;
2011-05-11 04:04:26 +08:00
u_char * ebuf = ( u_char * ) emalloc ( ebuf_len ) ;
2005-03-16 22:29:40 +08:00
2011-01-31 19:41:33 +08:00
if ( ebuf = = NULL ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " malloc failure setting contextEngineID " ) ;
return ( - 1 ) ;
}
if ( ! snmp_hex_to_binary ( & ebuf , & ebuf_len , & eout_len , 1 , contextEngineID ) ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Bad engine ID value '%s' " , contextEngineID ) ;
2011-05-11 04:04:26 +08:00
efree ( ebuf ) ;
2011-01-31 19:41:33 +08:00
return ( - 1 ) ;
}
2011-02-05 08:29:31 +08:00
if ( s - > contextEngineID ) {
2011-05-11 04:04:26 +08:00
efree ( s - > contextEngineID ) ;
2011-02-05 08:29:31 +08:00
}
2011-01-31 19:41:33 +08:00
s - > contextEngineID = ebuf ;
s - > contextEngineIDLen = eout_len ;
return ( 0 ) ;
2005-03-16 22:29:40 +08:00
}
/* }}} */
2011-01-31 19:41:33 +08:00
/* {{{ php_set_security(struct snmp_session *session, char *sec_level, char *auth_protocol, char *auth_passphrase, char *priv_protocol, char *priv_passphrase, char *contextName, char *contextEngineID)
Set all snmpv3 - related security options */
2011-02-01 09:02:00 +08:00
static int netsnmp_session_set_security ( struct snmp_session * session , char * sec_level , char * auth_protocol , char * auth_passphrase , char * priv_protocol , char * priv_passphrase , char * contextName , char * contextEngineID TSRMLS_DC )
2005-03-16 22:29:40 +08:00
{
2011-01-31 19:41:33 +08:00
/* Setting the security level. */
if ( netsnmp_session_set_sec_level ( session , sec_level ) ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Invalid security level '%s' " , sec_level ) ;
return ( - 1 ) ;
}
2005-03-16 22:29:40 +08:00
2011-01-31 19:41:33 +08:00
if ( session - > securityLevel = = SNMP_SEC_LEVEL_AUTHNOPRIV | | session - > securityLevel = = SNMP_SEC_LEVEL_AUTHPRIV ) {
/* Setting the authentication protocol. */
2011-02-01 09:02:00 +08:00
if ( netsnmp_session_set_auth_protocol ( session , auth_protocol TSRMLS_CC ) ) {
2011-01-31 19:41:33 +08:00
/* Warning message sent already, just bail out */
return ( - 1 ) ;
}
/* Setting the authentication passphrase. */
2011-02-01 09:02:00 +08:00
if ( netsnmp_session_gen_auth_key ( session , auth_passphrase TSRMLS_CC ) ) {
2011-01-31 19:41:33 +08:00
/* Warning message sent already, just bail out */
return ( - 1 ) ;
}
if ( session - > securityLevel = = SNMP_SEC_LEVEL_AUTHPRIV ) {
/* Setting the security protocol. */
2011-02-01 09:02:00 +08:00
if ( netsnmp_session_set_sec_protocol ( session , priv_protocol TSRMLS_CC ) ) {
2011-01-31 19:41:33 +08:00
/* Warning message sent already, just bail out */
return ( - 1 ) ;
}
/* Setting the security protocol passphrase. */
2011-02-01 09:02:00 +08:00
if ( netsnmp_session_gen_sec_key ( session , priv_passphrase TSRMLS_CC ) ) {
2011-01-31 19:41:33 +08:00
/* Warning message sent already, just bail out */
return ( - 1 ) ;
}
}
}
/* Setting contextName if specified */
if ( contextName ) {
session - > contextName = contextName ;
session - > contextNameLen = strlen ( contextName ) ;
}
/* Setting contextEngineIS if specified */
2011-02-01 09:02:00 +08:00
if ( contextEngineID & & strlen ( contextEngineID ) & & netsnmp_session_set_contextEngineID ( session , contextEngineID TSRMLS_CC ) ) {
2011-01-31 19:41:33 +08:00
/* Warning message sent already, just bail out */
return ( - 1 ) ;
}
return ( 0 ) ;
2005-03-16 22:29:40 +08:00
}
/* }}} */
2002-11-12 05:09:19 +08:00
2011-01-31 19:41:33 +08:00
/* {{{ php_snmp
2002-11-12 05:09:19 +08:00
*
2011-01-31 19:41:33 +08:00
* Generic SNMP handler for all versions .
* This function makes use of the internal SNMP object fetcher .
* Used both in old ( non - OO ) and OO API
2002-11-12 05:09:19 +08:00
*
*/
2011-01-31 19:41:33 +08:00
static void php_snmp ( INTERNAL_FUNCTION_PARAMETERS , int st , int version )
2003-01-24 17:40:53 +08:00
{
2011-01-31 19:41:33 +08:00
zval * * oid , * * value , * * type ;
char * a1 , * a2 , * a3 , * a4 , * a5 , * a6 , * a7 ;
int a1_len , a2_len , a3_len , a4_len , a5_len , a6_len , a7_len ;
2011-05-08 21:58:37 +08:00
zend_bool use_orignames = 0 , suffix_keys = 0 ;
2008-08-13 22:05:55 +08:00
long timeout = SNMP_DEFAULT_TIMEOUT ;
long retries = SNMP_DEFAULT_RETRIES ;
int argc = ZEND_NUM_ARGS ( ) ;
2011-02-19 19:13:10 +08:00
struct objid_query objid_query ;
2011-01-31 19:41:33 +08:00
php_snmp_session * session ;
int session_less_mode = ( getThis ( ) = = NULL ) ;
php_snmp_object * snmp_object ;
php_snmp_object glob_snmp_object ;
2011-02-19 19:13:10 +08:00
objid_query . max_repetitions = - 1 ;
objid_query . non_repeaters = 0 ;
objid_query . valueretrieval = SNMP_G ( valueretrieval ) ;
2011-07-21 20:48:47 +08:00
objid_query . oid_increasing_check = TRUE ;
2011-01-31 19:41:33 +08:00
if ( session_less_mode ) {
if ( version = = SNMP_VERSION_3 ) {
if ( st & SNMP_CMD_SET ) {
if ( zend_parse_parameters ( argc TSRMLS_CC , " sssssssZZZ|ll " , & a1 , & a1_len , & a2 , & a2_len , & a3 , & a3_len ,
& a4 , & a4_len , & a5 , & a5_len , & a6 , & a6_len , & a7 , & a7_len , & oid , & type , & value , & timeout , & retries ) = = FAILURE ) {
RETURN_FALSE ;
}
} else {
/* SNMP_CMD_GET
* SNMP_CMD_GETNEXT
* SNMP_CMD_WALK
*/
if ( zend_parse_parameters ( argc TSRMLS_CC , " sssssssZ|ll " , & a1 , & a1_len , & a2 , & a2_len , & a3 , & a3_len ,
& a4 , & a4_len , & a5 , & a5_len , & a6 , & a6_len , & a7 , & a7_len , & oid , & timeout , & retries ) = = FAILURE ) {
RETURN_FALSE ;
}
}
} else {
if ( st & SNMP_CMD_SET ) {
if ( zend_parse_parameters ( argc TSRMLS_CC , " ssZZZ|ll " , & a1 , & a1_len , & a2 , & a2_len , & oid , & type , & value , & timeout , & retries ) = = FAILURE ) {
RETURN_FALSE ;
}
} else {
/* SNMP_CMD_GET
* SNMP_CMD_GETNEXT
* SNMP_CMD_WALK
*/
if ( zend_parse_parameters ( argc TSRMLS_CC , " ssZ|ll " , & a1 , & a1_len , & a2 , & a2_len , & oid , & timeout , & retries ) = = FAILURE ) {
RETURN_FALSE ;
}
}
2008-08-13 22:05:55 +08:00
}
} else {
2011-01-31 19:41:33 +08:00
if ( st & SNMP_CMD_SET ) {
if ( zend_parse_parameters ( argc TSRMLS_CC , " ZZZ " , & oid , & type , & value ) = = FAILURE ) {
RETURN_FALSE ;
}
} else if ( st & SNMP_CMD_WALK ) {
2011-07-18 02:14:38 +08:00
if ( zend_parse_parameters ( argc TSRMLS_CC , " Z|bll " , & oid , & suffix_keys , & ( objid_query . max_repetitions ) , & ( objid_query . non_repeaters ) ) = = FAILURE ) {
2011-01-31 19:41:33 +08:00
RETURN_FALSE ;
}
2011-05-08 21:58:37 +08:00
if ( suffix_keys ) {
st | = SNMP_USE_SUFFIX_AS_KEYS ;
}
} else if ( st & SNMP_CMD_GET ) {
if ( zend_parse_parameters ( argc TSRMLS_CC , " Z|b " , & oid , & use_orignames ) = = FAILURE ) {
RETURN_FALSE ;
}
if ( use_orignames ) {
st | = SNMP_ORIGINAL_NAMES_AS_KEYS ;
}
2011-01-31 19:41:33 +08:00
} else {
2011-05-08 21:58:37 +08:00
/* SNMP_CMD_GETNEXT
2011-01-31 19:41:33 +08:00
*/
if ( zend_parse_parameters ( argc TSRMLS_CC , " Z " , & oid ) = = FAILURE ) {
RETURN_FALSE ;
}
2008-08-13 22:05:55 +08:00
}
2002-11-12 05:09:19 +08:00
}
2011-02-19 19:13:10 +08:00
if ( ! php_snmp_parse_oid ( st , & objid_query , oid , type , value TSRMLS_CC ) ) {
2002-12-06 03:33:03 +08:00
RETURN_FALSE ;
}
2011-01-31 19:41:33 +08:00
if ( session_less_mode ) {
2011-02-01 09:02:00 +08:00
if ( netsnmp_session_init ( & session , version , a1 , a2 , timeout , retries TSRMLS_CC ) ) {
2011-02-27 03:08:55 +08:00
efree ( objid_query . vars ) ;
netsnmp_session_free ( & session ) ;
2011-01-31 19:41:33 +08:00
RETURN_FALSE ;
}
2011-02-01 09:02:00 +08:00
if ( version = = SNMP_VERSION_3 & & netsnmp_session_set_security ( session , a3 , a4 , a5 , a6 , a7 , NULL , NULL TSRMLS_CC ) ) {
2011-02-27 03:08:55 +08:00
efree ( objid_query . vars ) ;
netsnmp_session_free ( & session ) ;
2011-01-31 19:41:33 +08:00
/* Warning message sent already, just bail out */
RETURN_FALSE ;
}
} else {
zval * object = getThis ( ) ;
snmp_object = ( php_snmp_object * ) zend_object_store_get_object ( object TSRMLS_CC ) ;
2011-02-27 03:08:55 +08:00
session = snmp_object - > session ;
if ( ! session ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Invalid or uninitialized SNMP object " ) ;
efree ( objid_query . vars ) ;
RETURN_FALSE ;
}
2011-02-01 15:45:30 +08:00
if ( snmp_object - > max_oids > 0 ) {
2011-02-19 19:13:10 +08:00
objid_query . step = snmp_object - > max_oids ;
if ( objid_query . max_repetitions < 0 ) { /* unspecified in function call, use session-wise */
objid_query . max_repetitions = snmp_object - > max_oids ;
2011-02-01 15:45:30 +08:00
}
}
2011-07-21 20:48:47 +08:00
objid_query . oid_increasing_check = snmp_object - > oid_increasing_check ;
2011-02-19 19:13:10 +08:00
objid_query . valueretrieval = snmp_object - > valueretrieval ;
2011-01-31 19:41:33 +08:00
glob_snmp_object . enum_print = netsnmp_ds_get_boolean ( NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM ) ;
netsnmp_ds_set_boolean ( NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM , snmp_object - > enum_print ) ;
glob_snmp_object . quick_print = netsnmp_ds_get_boolean ( NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_QUICK_PRINT ) ;
netsnmp_ds_set_boolean ( NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_QUICK_PRINT , snmp_object - > quick_print ) ;
glob_snmp_object . oid_output_format = netsnmp_ds_get_int ( NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_OID_OUTPUT_FORMAT ) ;
netsnmp_ds_set_int ( NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_OID_OUTPUT_FORMAT , snmp_object - > oid_output_format ) ;
2002-12-06 03:33:03 +08:00
}
2003-01-24 17:40:53 +08:00
2011-02-19 19:13:10 +08:00
if ( objid_query . max_repetitions < 0 ) {
objid_query . max_repetitions = 20 ; /* provide correct default value */
2011-02-01 15:45:30 +08:00
}
2011-02-19 19:13:10 +08:00
php_snmp_internal ( INTERNAL_FUNCTION_PARAM_PASSTHRU , st , session , & objid_query ) ;
2011-02-01 15:45:30 +08:00
2011-02-19 19:13:10 +08:00
efree ( objid_query . vars ) ;
2002-11-12 05:09:19 +08:00
2011-01-31 19:41:33 +08:00
if ( session_less_mode ) {
netsnmp_session_free ( & session ) ;
} else {
netsnmp_ds_set_boolean ( NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM , glob_snmp_object . enum_print ) ;
netsnmp_ds_set_boolean ( NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_QUICK_PRINT , glob_snmp_object . quick_print ) ;
netsnmp_ds_set_int ( NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_OID_OUTPUT_FORMAT , glob_snmp_object . oid_output_format ) ;
2002-11-12 05:09:19 +08:00
}
}
/* }}} */
2011-01-31 19:41:33 +08:00
/* {{{ proto mixed snmpget(string host, string community, mixed object_id [, int timeout [, int retries]])
Fetch a SNMP object */
PHP_FUNCTION ( snmpget )
2002-11-12 05:09:19 +08:00
{
2011-01-31 19:41:33 +08:00
php_snmp ( INTERNAL_FUNCTION_PARAM_PASSTHRU , SNMP_CMD_GET , SNMP_VERSION_1 ) ;
2002-11-12 05:09:19 +08:00
}
/* }}} */
2011-01-31 19:41:33 +08:00
/* {{{ proto mixed snmpgetnext(string host, string community, mixed object_id [, int timeout [, int retries]])
Fetch a SNMP object */
PHP_FUNCTION ( snmpgetnext )
2003-07-15 02:10:22 +08:00
{
2011-01-31 19:41:33 +08:00
php_snmp ( INTERNAL_FUNCTION_PARAM_PASSTHRU , SNMP_CMD_GETNEXT , SNMP_VERSION_1 ) ;
2003-07-15 02:10:22 +08:00
}
/* }}} */
2011-01-31 19:41:33 +08:00
/* {{{ proto mixed snmpwalk(string host, string community, mixed object_id [, int timeout [, int retries]])
Return all objects under the specified object id */
PHP_FUNCTION ( snmpwalk )
2002-11-12 05:09:19 +08:00
{
2011-01-31 19:41:33 +08:00
php_snmp ( INTERNAL_FUNCTION_PARAM_PASSTHRU , ( SNMP_CMD_WALK | SNMP_NUMERIC_KEYS ) , SNMP_VERSION_1 ) ;
2002-11-12 05:09:19 +08:00
}
/* }}} */
2011-01-31 19:41:33 +08:00
/* {{{ proto mixed snmprealwalk(string host, string community, mixed object_id [, int timeout [, int retries]])
Return all objects including their respective object id withing the specified one */
PHP_FUNCTION ( snmprealwalk )
2002-11-12 05:09:19 +08:00
{
2011-01-31 19:41:33 +08:00
php_snmp ( INTERNAL_FUNCTION_PARAM_PASSTHRU , SNMP_CMD_WALK , SNMP_VERSION_1 ) ;
2002-11-12 05:09:19 +08:00
}
/* }}} */
2011-01-31 19:41:33 +08:00
/* {{{ proto bool snmpset(string host, string community, mixed object_id, mixed type, mixed value [, int timeout [, int retries]])
Set the value of a SNMP object */
PHP_FUNCTION ( snmpset )
2002-11-12 05:09:19 +08:00
{
2011-01-31 19:41:33 +08:00
php_snmp ( INTERNAL_FUNCTION_PARAM_PASSTHRU , SNMP_CMD_SET , SNMP_VERSION_1 ) ;
2002-11-12 05:09:19 +08:00
}
/* }}} */
2011-01-31 19:41:33 +08:00
/* {{{ proto bool snmp_get_quick_print(void)
Return the current status of quick_print */
PHP_FUNCTION ( snmp_get_quick_print )
2003-07-15 01:11:04 +08:00
{
2011-01-31 19:41:33 +08:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
return ;
}
RETURN_BOOL ( netsnmp_ds_get_boolean ( NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_QUICK_PRINT ) ) ;
}
/* }}} */
/* {{{ proto bool snmp_set_quick_print(int quick_print)
Return all objects including their respective object id withing the specified one */
PHP_FUNCTION ( snmp_set_quick_print )
{
long a1 ;
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " l " , & a1 ) = = FAILURE ) {
RETURN_FALSE ;
}
netsnmp_ds_set_boolean ( NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_QUICK_PRINT , ( int ) a1 ) ;
RETURN_TRUE ;
}
/* }}} */
/* {{{ proto bool snmp_set_enum_print(int enum_print)
Return all values that are enums with their enum value instead of the raw integer */
PHP_FUNCTION ( snmp_set_enum_print )
{
long a1 ;
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " l " , & a1 ) = = FAILURE ) {
RETURN_FALSE ;
}
netsnmp_ds_set_boolean ( NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM , ( int ) a1 ) ;
RETURN_TRUE ;
}
/* }}} */
/* {{{ proto bool snmp_set_oid_output_format(int oid_format)
Set the OID output format . */
PHP_FUNCTION ( snmp_set_oid_output_format )
{
long a1 ;
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " l " , & a1 ) = = FAILURE ) {
RETURN_FALSE ;
}
switch ( ( int ) a1 ) {
case NETSNMP_OID_OUTPUT_SUFFIX :
case NETSNMP_OID_OUTPUT_MODULE :
case NETSNMP_OID_OUTPUT_FULL :
case NETSNMP_OID_OUTPUT_NUMERIC :
case NETSNMP_OID_OUTPUT_UCD :
case NETSNMP_OID_OUTPUT_NONE :
netsnmp_ds_set_int ( NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_OID_OUTPUT_FORMAT , a1 ) ;
RETURN_TRUE ;
break ;
default :
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Unknown SNMP output print format '%d' " , ( int ) a1 ) ;
RETURN_FALSE ;
break ;
}
}
/* }}} */
/* {{{ proto mixed snmp2_get(string host, string community, mixed object_id [, int timeout [, int retries]])
Fetch a SNMP object */
PHP_FUNCTION ( snmp2_get )
{
php_snmp ( INTERNAL_FUNCTION_PARAM_PASSTHRU , SNMP_CMD_GET , SNMP_VERSION_2c ) ;
}
/* }}} */
/* {{{ proto mixed snmp2_getnext(string host, string community, mixed object_id [, int timeout [, int retries]])
Fetch a SNMP object */
PHP_FUNCTION ( snmp2_getnext )
{
php_snmp ( INTERNAL_FUNCTION_PARAM_PASSTHRU , SNMP_CMD_GETNEXT , SNMP_VERSION_2c ) ;
}
/* }}} */
/* {{{ proto mixed snmp2_walk(string host, string community, mixed object_id [, int timeout [, int retries]])
Return all objects under the specified object id */
PHP_FUNCTION ( snmp2_walk )
{
php_snmp ( INTERNAL_FUNCTION_PARAM_PASSTHRU , ( SNMP_CMD_WALK | SNMP_NUMERIC_KEYS ) , SNMP_VERSION_2c ) ;
}
/* }}} */
/* {{{ proto mixed snmp2_real_walk(string host, string community, mixed object_id [, int timeout [, int retries]])
Return all objects including their respective object id withing the specified one */
PHP_FUNCTION ( snmp2_real_walk )
{
php_snmp ( INTERNAL_FUNCTION_PARAM_PASSTHRU , SNMP_CMD_WALK , SNMP_VERSION_2c ) ;
}
/* }}} */
/* {{{ proto bool snmp2_set(string host, string community, mixed object_id, mixed type, mixed value [, int timeout [, int retries]])
Set the value of a SNMP object */
PHP_FUNCTION ( snmp2_set )
{
php_snmp ( INTERNAL_FUNCTION_PARAM_PASSTHRU , SNMP_CMD_SET , SNMP_VERSION_2c ) ;
}
/* }}} */
/* {{{ proto mixed snmp3_get(string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_protocol, string priv_passphrase, mixed object_id [, int timeout [, int retries]])
Fetch the value of a SNMP object */
PHP_FUNCTION ( snmp3_get )
{
php_snmp ( INTERNAL_FUNCTION_PARAM_PASSTHRU , SNMP_CMD_GET , SNMP_VERSION_3 ) ;
}
/* }}} */
/* {{{ proto mixed snmp3_getnext(string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_protocol, string priv_passphrase, mixed object_id [, int timeout [, int retries]])
Fetch the value of a SNMP object */
PHP_FUNCTION ( snmp3_getnext )
{
php_snmp ( INTERNAL_FUNCTION_PARAM_PASSTHRU , SNMP_CMD_GETNEXT , SNMP_VERSION_3 ) ;
}
/* }}} */
/* {{{ proto mixed snmp3_walk(string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_protocol, string priv_passphrase, mixed object_id [, int timeout [, int retries]])
Fetch the value of a SNMP object */
PHP_FUNCTION ( snmp3_walk )
{
php_snmp ( INTERNAL_FUNCTION_PARAM_PASSTHRU , ( SNMP_CMD_WALK | SNMP_NUMERIC_KEYS ) , SNMP_VERSION_3 ) ;
}
/* }}} */
/* {{{ proto mixed snmp3_real_walk(string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_protocol, string priv_passphrase, mixed object_id [, int timeout [, int retries]])
Fetch the value of a SNMP object */
PHP_FUNCTION ( snmp3_real_walk )
{
php_snmp ( INTERNAL_FUNCTION_PARAM_PASSTHRU , SNMP_CMD_WALK , SNMP_VERSION_3 ) ;
}
/* }}} */
/* {{{ proto bool snmp3_set(string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_protocol, string priv_passphrase, mixed object_id, mixed type, mixed value [, int timeout [, int retries]])
Fetch the value of a SNMP object */
PHP_FUNCTION ( snmp3_set )
{
php_snmp ( INTERNAL_FUNCTION_PARAM_PASSTHRU , SNMP_CMD_SET , SNMP_VERSION_3 ) ;
}
/* }}} */
/* {{{ proto bool snmp_set_valueretrieval(int method)
Specify the method how the SNMP values will be returned */
PHP_FUNCTION ( snmp_set_valueretrieval )
{
long method ;
2003-07-15 01:11:04 +08:00
2008-08-13 22:05:55 +08:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " l " , & method ) = = FAILURE ) {
2011-01-31 19:41:33 +08:00
RETURN_FALSE ;
2003-07-15 01:11:04 +08:00
}
2011-07-18 01:18:31 +08:00
if ( method > = 0 & & method < = ( SNMP_VALUE_LIBRARY | SNMP_VALUE_PLAIN | SNMP_VALUE_OBJECT ) ) {
2011-01-31 19:41:33 +08:00
SNMP_G ( valueretrieval ) = method ;
RETURN_TRUE ;
2011-07-18 01:18:31 +08:00
} else {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Unknown SNMP value retrieval method '%ld' " , method ) ;
RETURN_FALSE ;
2003-07-15 01:11:04 +08:00
}
}
/* }}} */
/* {{{ proto int snmp_get_valueretrieval()
Return the method how the SNMP values will be returned */
PHP_FUNCTION ( snmp_get_valueretrieval )
{
2011-07-18 01:18:31 +08:00
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
RETURN_FALSE ;
}
2003-07-15 01:11:04 +08:00
RETURN_LONG ( SNMP_G ( valueretrieval ) ) ;
}
/* }}} */
2011-01-31 19:41:33 +08:00
/* {{{ proto bool snmp_read_mib(string filename)
2003-07-16 17:28:47 +08:00
Reads and parses a MIB file into the active MIB tree . */
PHP_FUNCTION ( snmp_read_mib )
{
2008-08-13 22:05:55 +08:00
char * filename ;
int filename_len ;
2003-07-16 17:28:47 +08:00
2011-06-07 05:28:16 +08:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " p " , & filename , & filename_len ) = = FAILURE ) {
2011-01-31 19:41:33 +08:00
RETURN_FALSE ;
2003-07-16 17:28:47 +08:00
}
2008-08-13 22:05:55 +08:00
if ( ! read_mib ( filename ) ) {
2003-07-16 17:28:47 +08:00
char * error = strerror ( errno ) ;
2008-08-13 22:05:55 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Error while reading MIB file '%s': %s " , filename , error ) ;
2003-07-16 17:28:47 +08:00
RETURN_FALSE ;
}
RETURN_TRUE ;
}
/* }}} */
2011-05-10 03:34:07 +08:00
/* {{{ proto SNMP SNMP::__construct(int version, string hostname, string community|securityName [, long timeout [, long retries]])
2011-01-31 19:41:33 +08:00
Creates a new SNMP session to specified host . */
2011-05-10 03:34:07 +08:00
PHP_METHOD ( snmp , __construct )
2011-01-31 19:41:33 +08:00
{
php_snmp_object * snmp_object ;
zval * object = getThis ( ) ;
char * a1 , * a2 ;
int a1_len , a2_len ;
long timeout = SNMP_DEFAULT_TIMEOUT ;
long retries = SNMP_DEFAULT_RETRIES ;
2011-03-03 06:12:02 +08:00
long version = SNMP_DEFAULT_VERSION ;
2011-01-31 19:41:33 +08:00
int argc = ZEND_NUM_ARGS ( ) ;
zend_error_handling error_handling ;
snmp_object = ( php_snmp_object * ) zend_object_store_get_object ( object TSRMLS_CC ) ;
zend_replace_error_handling ( EH_THROW , NULL , & error_handling TSRMLS_CC ) ;
if ( zend_parse_parameters ( argc TSRMLS_CC , " lss|ll " , & version , & a1 , & a1_len , & a2 , & a2_len , & timeout , & retries ) = = FAILURE ) {
zend_restore_error_handling ( & error_handling TSRMLS_CC ) ;
return ;
}
zend_restore_error_handling ( & error_handling TSRMLS_CC ) ;
switch ( version ) {
case SNMP_VERSION_1 :
case SNMP_VERSION_2c :
case SNMP_VERSION_3 :
break ;
default :
zend_throw_exception ( zend_exception_get_default ( TSRMLS_C ) , " Unknown SNMP protocol version " , 0 TSRMLS_CC ) ;
return ;
}
2011-02-17 20:02:16 +08:00
/* handle re-open of snmp session */
if ( snmp_object - > session ) {
netsnmp_session_free ( & ( snmp_object - > session ) ) ;
}
2011-02-01 09:02:00 +08:00
if ( netsnmp_session_init ( & ( snmp_object - > session ) , version , a1 , a2 , timeout , retries TSRMLS_CC ) ) {
2011-01-31 19:41:33 +08:00
return ;
}
2011-02-01 15:45:30 +08:00
snmp_object - > max_oids = 0 ;
2011-01-31 19:41:33 +08:00
snmp_object - > valueretrieval = SNMP_G ( valueretrieval ) ;
snmp_object - > enum_print = netsnmp_ds_get_boolean ( NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM ) ;
snmp_object - > oid_output_format = netsnmp_ds_get_int ( NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_OID_OUTPUT_FORMAT ) ;
snmp_object - > quick_print = netsnmp_ds_get_boolean ( NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_QUICK_PRINT ) ;
2011-07-21 20:48:47 +08:00
snmp_object - > oid_increasing_check = TRUE ;
2011-01-31 19:41:33 +08:00
}
/* }}} */
/* {{{ proto bool SNMP::close()
Close SNMP session */
PHP_METHOD ( snmp , close )
{
php_snmp_object * snmp_object ;
zval * object = getThis ( ) ;
snmp_object = ( php_snmp_object * ) zend_object_store_get_object ( object TSRMLS_CC ) ;
if ( zend_parse_parameters_none ( ) = = FAILURE ) {
RETURN_FALSE ;
}
netsnmp_session_free ( & ( snmp_object - > session ) ) ;
RETURN_TRUE ;
}
/* }}} */
2011-07-17 22:06:05 +08:00
/* {{{ proto mixed SNMP::get(mixed object_id [, bool preserve_keys])
2011-01-31 19:41:33 +08:00
Fetch a SNMP object returing scalar for single OID and array of oid - > value pairs for multi OID request */
PHP_METHOD ( snmp , get )
{
php_snmp ( INTERNAL_FUNCTION_PARAM_PASSTHRU , SNMP_CMD_GET , ( - 1 ) ) ;
}
/* }}} */
2011-07-17 22:06:05 +08:00
/* {{{ proto mixed SNMP::getnext(mixed object_id)
2011-01-31 19:41:33 +08:00
Fetch a SNMP object returing scalar for single OID and array of oid - > value pairs for multi OID request */
PHP_METHOD ( snmp , getnext )
{
php_snmp ( INTERNAL_FUNCTION_PARAM_PASSTHRU , SNMP_CMD_GETNEXT , ( - 1 ) ) ;
}
/* }}} */
2011-07-18 02:14:38 +08:00
/* {{{ proto mixed SNMP::walk(mixed object_id [, bool $suffix_as_key = FALSE [, int $max_repetitions [, int $non_repeaters]])
2011-01-31 19:41:33 +08:00
Return all objects including their respective object id withing the specified one as array of oid - > value pairs */
PHP_METHOD ( snmp , walk )
{
php_snmp ( INTERNAL_FUNCTION_PARAM_PASSTHRU , SNMP_CMD_WALK , ( - 1 ) ) ;
}
/* }}} */
/* {{{ proto bool SNMP::set(mixed object_id, mixed type, mixed value)
Set the value of a SNMP object */
PHP_METHOD ( snmp , set )
{
php_snmp ( INTERNAL_FUNCTION_PARAM_PASSTHRU , SNMP_CMD_SET , ( - 1 ) ) ;
}
2011-06-12 20:10:01 +08:00
/* {{{ proto bool SNMP::setSecurity(string sec_level, [ string auth_protocol, string auth_passphrase [, string priv_protocol, string priv_passphrase [, string contextName [, string contextEngineID]]]])
2011-01-31 19:41:33 +08:00
Set SNMPv3 security - related session parameters */
2011-03-21 04:07:33 +08:00
PHP_METHOD ( snmp , setSecurity )
2011-01-31 19:41:33 +08:00
{
php_snmp_object * snmp_object ;
zval * object = getThis ( ) ;
char * a1 = " " , * a2 = " " , * a3 = " " , * a4 = " " , * a5 = " " , * a6 = " " , * a7 = " " ;
2011-03-03 06:12:02 +08:00
int a1_len = 0 , a2_len = 0 , a3_len = 0 , a4_len = 0 , a5_len = 0 , a6_len = 0 , a7_len = 0 ;
2011-01-31 19:41:33 +08:00
int argc = ZEND_NUM_ARGS ( ) ;
snmp_object = ( php_snmp_object * ) zend_object_store_get_object ( object TSRMLS_CC ) ;
if ( zend_parse_parameters ( argc TSRMLS_CC , " s|ssssss " , & a1 , & a1_len , & a2 , & a2_len , & a3 , & a3_len ,
& a4 , & a4_len , & a5 , & a5_len , & a6 , & a6_len , & a7 , & a7_len ) = = FAILURE ) {
RETURN_FALSE ;
}
2011-02-01 09:02:00 +08:00
if ( netsnmp_session_set_security ( snmp_object - > session , a1 , a2 , a3 , a4 , a5 , a6 , a7 TSRMLS_CC ) ) {
2011-01-31 19:41:33 +08:00
/* Warning message sent already, just bail out */
RETURN_FALSE ;
}
RETURN_TRUE ;
}
/* }}} */
2011-05-10 19:48:10 +08:00
/* {{{ proto long SNMP::getErrno()
2011-02-26 16:27:26 +08:00
Get last error code number */
2011-05-10 19:48:10 +08:00
PHP_METHOD ( snmp , getErrno )
2011-02-26 16:27:26 +08:00
{
php_snmp_object * snmp_object ;
zval * object = getThis ( ) ;
snmp_object = ( php_snmp_object * ) zend_object_store_get_object ( object TSRMLS_CC ) ;
RETVAL_LONG ( snmp_object - > snmp_errno ) ;
return ;
}
/* }}} */
2011-05-10 19:48:10 +08:00
/* {{{ proto long SNMP::getError()
2011-02-26 16:27:26 +08:00
Get last error message */
2011-05-10 19:48:10 +08:00
PHP_METHOD ( snmp , getError )
2011-02-26 16:27:26 +08:00
{
php_snmp_object * snmp_object ;
zval * object = getThis ( ) ;
snmp_object = ( php_snmp_object * ) zend_object_store_get_object ( object TSRMLS_CC ) ;
RETVAL_STRING ( snmp_object - > snmp_errstr , 1 ) ;
return ;
}
/* }}} */
2011-01-31 19:41:33 +08:00
/* {{{ */
void php_snmp_add_property ( HashTable * h , const char * name , size_t name_length , php_snmp_read_t read_func , php_snmp_write_t write_func TSRMLS_DC )
{
php_snmp_prop_handler p ;
p . name = ( char * ) name ;
p . name_length = name_length ;
p . read_func = ( read_func ) ? read_func : NULL ;
p . write_func = ( write_func ) ? write_func : NULL ;
zend_hash_add ( h , ( char * ) name , name_length + 1 , & p , sizeof ( php_snmp_prop_handler ) , NULL ) ;
}
/* }}} */
2011-02-01 16:20:13 +08:00
/* {{{ php_snmp_read_property(zval *object, zval *member, int type[, const zend_literal *key])
2011-01-31 19:41:33 +08:00
Generic object property reader */
2011-02-01 09:02:00 +08:00
zval * php_snmp_read_property ( zval * object , zval * member , int type , const zend_literal * key TSRMLS_DC )
2011-01-31 19:41:33 +08:00
{
zval tmp_member ;
zval * retval ;
php_snmp_object * obj ;
php_snmp_prop_handler * hnd ;
int ret ;
ret = FAILURE ;
obj = ( php_snmp_object * ) zend_objects_get_address ( object TSRMLS_CC ) ;
if ( Z_TYPE_P ( member ) ! = IS_STRING ) {
tmp_member = * member ;
zval_copy_ctor ( & tmp_member ) ;
convert_to_string ( & tmp_member ) ;
member = & tmp_member ;
}
ret = zend_hash_find ( & php_snmp_properties , Z_STRVAL_P ( member ) , Z_STRLEN_P ( member ) + 1 , ( void * * ) & hnd ) ;
if ( ret = = SUCCESS & & hnd - > read_func ) {
ret = hnd - > read_func ( obj , & retval TSRMLS_CC ) ;
if ( ret = = SUCCESS ) {
/* ensure we're creating a temporary variable */
Z_SET_REFCOUNT_P ( retval , 0 ) ;
} else {
retval = EG ( uninitialized_zval_ptr ) ;
}
} else {
zend_object_handlers * std_hnd = zend_get_std_object_handlers ( ) ;
2011-02-01 09:02:00 +08:00
retval = std_hnd - > read_property ( object , member , type , key TSRMLS_CC ) ;
2011-01-31 19:41:33 +08:00
}
if ( member = = & tmp_member ) {
zval_dtor ( member ) ;
}
return ( retval ) ;
}
/* }}} */
2011-02-01 16:20:13 +08:00
/* {{{ php_snmp_write_property(zval *object, zval *member, zval *value[, const zend_literal *key])
2011-01-31 19:41:33 +08:00
Generic object property writer */
2011-02-01 09:02:00 +08:00
void php_snmp_write_property ( zval * object , zval * member , zval * value , const zend_literal * key TSRMLS_DC )
2011-01-31 19:41:33 +08:00
{
zval tmp_member ;
php_snmp_object * obj ;
php_snmp_prop_handler * hnd ;
int ret ;
if ( Z_TYPE_P ( member ) ! = IS_STRING ) {
tmp_member = * member ;
zval_copy_ctor ( & tmp_member ) ;
convert_to_string ( & tmp_member ) ;
member = & tmp_member ;
}
ret = FAILURE ;
obj = ( php_snmp_object * ) zend_objects_get_address ( object TSRMLS_CC ) ;
ret = zend_hash_find ( & php_snmp_properties , Z_STRVAL_P ( member ) , Z_STRLEN_P ( member ) + 1 , ( void * * ) & hnd ) ;
if ( ret = = SUCCESS & & hnd - > write_func ) {
hnd - > write_func ( obj , value TSRMLS_CC ) ;
if ( ! PZVAL_IS_REF ( value ) & & Z_REFCOUNT_P ( value ) = = 0 ) {
Z_ADDREF_P ( value ) ;
zval_ptr_dtor ( & value ) ;
}
} else {
zend_object_handlers * std_hnd = zend_get_std_object_handlers ( ) ;
2011-02-01 09:02:00 +08:00
std_hnd - > write_property ( object , member , value , key TSRMLS_CC ) ;
2011-01-31 19:41:33 +08:00
}
if ( member = = & tmp_member ) {
zval_dtor ( member ) ;
}
}
/* }}} */
2011-02-01 16:20:13 +08:00
/* {{{ php_snmp_has_property(zval *object, zval *member, int has_set_exists[, const zend_literal *key])
2011-01-31 19:41:33 +08:00
Generic object property checker */
2011-02-01 09:02:00 +08:00
static int php_snmp_has_property ( zval * object , zval * member , int has_set_exists , const zend_literal * key TSRMLS_DC )
2011-01-31 19:41:33 +08:00
{
php_snmp_prop_handler * hnd ;
int ret = 0 ;
if ( zend_hash_find ( & php_snmp_properties , Z_STRVAL_P ( member ) , Z_STRLEN_P ( member ) + 1 , ( void * * ) & hnd ) = = SUCCESS ) {
switch ( has_set_exists ) {
case 2 :
ret = 1 ;
break ;
case 0 : {
2011-02-01 09:02:00 +08:00
zval * value = php_snmp_read_property ( object , member , BP_VAR_IS , key TSRMLS_CC ) ;
2011-01-31 19:41:33 +08:00
if ( value ! = EG ( uninitialized_zval_ptr ) ) {
ret = Z_TYPE_P ( value ) ! = IS_NULL ? 1 : 0 ;
/* refcount is 0 */
Z_ADDREF_P ( value ) ;
zval_ptr_dtor ( & value ) ;
}
break ;
}
default : {
2011-02-01 09:02:00 +08:00
zval * value = php_snmp_read_property ( object , member , BP_VAR_IS , key TSRMLS_CC ) ;
2011-01-31 19:41:33 +08:00
if ( value ! = EG ( uninitialized_zval_ptr ) ) {
convert_to_boolean ( value ) ;
ret = Z_BVAL_P ( value ) ? 1 : 0 ;
/* refcount is 0 */
Z_ADDREF_P ( value ) ;
zval_ptr_dtor ( & value ) ;
}
break ;
}
}
} else {
zend_object_handlers * std_hnd = zend_get_std_object_handlers ( ) ;
2011-02-01 09:02:00 +08:00
ret = std_hnd - > has_property ( object , member , has_set_exists , key TSRMLS_CC ) ;
2011-01-31 19:41:33 +08:00
}
return ret ;
}
/* }}} */
/* {{{ php_snmp_get_properties(zval *object)
Returns all object properties . Injects SNMP properties into object on first call */
static HashTable * php_snmp_get_properties ( zval * object TSRMLS_DC )
{
php_snmp_object * obj ;
php_snmp_prop_handler * hnd ;
HashTable * props ;
zval * val ;
char * key ;
uint key_len ;
HashPosition pos ;
ulong num_key ;
obj = ( php_snmp_object * ) zend_objects_get_address ( object TSRMLS_CC ) ;
2011-02-05 08:29:31 +08:00
props = zend_std_get_properties ( object TSRMLS_CC ) ;
2011-01-31 19:41:33 +08:00
zend_hash_internal_pointer_reset_ex ( & php_snmp_properties , & pos ) ;
while ( zend_hash_get_current_data_ex ( & php_snmp_properties , ( void * * ) & hnd , & pos ) = = SUCCESS ) {
zend_hash_get_current_key_ex ( & php_snmp_properties , & key , & key_len , & num_key , 0 , & pos ) ;
if ( ! hnd - > read_func | | hnd - > read_func ( obj , & val TSRMLS_CC ) ! = SUCCESS ) {
val = EG ( uninitialized_zval_ptr ) ;
2011-02-15 07:58:52 +08:00
Z_ADDREF_P ( val ) ;
2011-01-31 19:41:33 +08:00
}
zend_hash_update ( props , key , key_len , ( void * ) & val , sizeof ( zval * ) , NULL ) ;
zend_hash_move_forward_ex ( & php_snmp_properties , & pos ) ;
}
return obj - > zo . properties ;
}
/* }}} */
2011-02-05 08:29:31 +08:00
/* {{{ */
static int php_snmp_read_info ( php_snmp_object * snmp_object , zval * * retval TSRMLS_DC )
{
zval * val ;
MAKE_STD_ZVAL ( * retval ) ;
array_init ( * retval ) ;
2011-02-15 08:07:26 +08:00
if ( snmp_object - > session = = NULL ) {
return SUCCESS ;
}
2011-02-05 08:29:31 +08:00
MAKE_STD_ZVAL ( val ) ;
ZVAL_STRINGL ( val , snmp_object - > session - > peername , strlen ( snmp_object - > session - > peername ) , 1 ) ;
add_assoc_zval ( * retval , " hostname " , val ) ;
MAKE_STD_ZVAL ( val ) ;
ZVAL_LONG ( val , snmp_object - > session - > remote_port ) ;
add_assoc_zval ( * retval , " port " , val ) ;
MAKE_STD_ZVAL ( val ) ;
ZVAL_LONG ( val , snmp_object - > session - > timeout ) ;
add_assoc_zval ( * retval , " timeout " , val ) ;
MAKE_STD_ZVAL ( val ) ;
ZVAL_LONG ( val , snmp_object - > session - > retries ) ;
add_assoc_zval ( * retval , " retries " , val ) ;
return SUCCESS ;
}
/* }}} */
2011-02-01 15:45:30 +08:00
/* {{{ */
static int php_snmp_read_max_oids ( php_snmp_object * snmp_object , zval * * retval TSRMLS_DC )
{
MAKE_STD_ZVAL ( * retval ) ;
2011-02-26 16:27:26 +08:00
if ( snmp_object - > max_oids > 0 ) {
ZVAL_LONG ( * retval , snmp_object - > max_oids ) ;
} else {
ZVAL_NULL ( * retval ) ;
}
2011-02-01 15:45:30 +08:00
return SUCCESS ;
}
/* }}} */
2011-07-18 03:47:24 +08:00
# define PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(name) \
static int php_snmp_read_ # # name ( php_snmp_object * snmp_object , zval * * retval TSRMLS_DC ) \
{ \
MAKE_STD_ZVAL ( * retval ) ; \
ZVAL_BOOL ( * retval , snmp_object - > name ) ; \
return SUCCESS ; \
}
2011-01-31 19:41:33 +08:00
2011-07-21 20:48:47 +08:00
PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION ( oid_increasing_check )
2011-07-18 03:47:24 +08:00
PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION ( quick_print )
PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION ( enum_print )
2011-01-31 19:41:33 +08:00
2011-07-18 03:47:24 +08:00
# define PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(name) \
static int php_snmp_read_ # # name ( php_snmp_object * snmp_object , zval * * retval TSRMLS_DC ) \
{ \
MAKE_STD_ZVAL ( * retval ) ; \
ZVAL_LONG ( * retval , snmp_object - > name ) ; \
return SUCCESS ; \
}
2011-01-31 19:41:33 +08:00
2011-07-18 03:47:24 +08:00
PHP_SNMP_LONG_PROPERTY_READER_FUNCTION ( valueretrieval )
PHP_SNMP_LONG_PROPERTY_READER_FUNCTION ( oid_output_format )
2011-01-31 19:41:33 +08:00
2011-02-05 08:29:31 +08:00
/* {{{ */
static int php_snmp_write_info ( php_snmp_object * snmp_object , zval * newval TSRMLS_DC )
{
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " info property is read-only " ) ;
return FAILURE ;
}
/* }}} */
2011-02-01 15:45:30 +08:00
/* {{{ */
static int php_snmp_write_max_oids ( php_snmp_object * snmp_object , zval * newval TSRMLS_DC )
{
zval ztmp ;
int ret = SUCCESS ;
2011-02-26 16:27:26 +08:00
if ( Z_TYPE_P ( newval ) = = IS_NULL ) {
snmp_object - > max_oids = 0 ;
return ret ;
}
2011-02-01 15:45:30 +08:00
if ( Z_TYPE_P ( newval ) ! = IS_LONG ) {
ztmp = * newval ;
zval_copy_ctor ( & ztmp ) ;
convert_to_long ( & ztmp ) ;
newval = & ztmp ;
}
2011-02-26 16:27:26 +08:00
if ( Z_LVAL_P ( newval ) > 0 ) {
snmp_object - > max_oids = Z_LVAL_P ( newval ) ;
} else {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " max_oids should be positive integer or NULL, got %ld " , Z_LVAL_P ( newval ) ) ;
}
2011-02-01 15:45:30 +08:00
if ( newval = = & ztmp ) {
zval_dtor ( newval ) ;
}
return ret ;
}
/* }}} */
2011-01-31 19:41:33 +08:00
/* {{{ */
static int php_snmp_write_valueretrieval ( php_snmp_object * snmp_object , zval * newval TSRMLS_DC )
{
zval ztmp ;
int ret = SUCCESS ;
2011-02-26 16:27:26 +08:00
2011-01-31 19:41:33 +08:00
if ( Z_TYPE_P ( newval ) ! = IS_LONG ) {
ztmp = * newval ;
zval_copy_ctor ( & ztmp ) ;
convert_to_long ( & ztmp ) ;
newval = & ztmp ;
}
2011-07-18 01:18:31 +08:00
if ( Z_LVAL_P ( newval ) > = 0 & & Z_LVAL_P ( newval ) < = ( SNMP_VALUE_LIBRARY | SNMP_VALUE_PLAIN | SNMP_VALUE_OBJECT ) ) {
snmp_object - > valueretrieval = Z_LVAL_P ( newval ) ;
} else {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Unknown SNMP value retrieval method '%ld' " , Z_LVAL_P ( newval ) ) ;
ret = FAILURE ;
2011-01-31 19:41:33 +08:00
}
if ( newval = = & ztmp ) {
zval_dtor ( newval ) ;
}
return ret ;
}
/* }}} */
2011-07-18 03:47:24 +08:00
# define PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(name) \
static int php_snmp_write_ # # name ( php_snmp_object * snmp_object , zval * newval TSRMLS_DC ) \
{ \
zval ztmp ; \
if ( Z_TYPE_P ( newval ) ! = IS_BOOL ) { \
ztmp = * newval ; \
zval_copy_ctor ( & ztmp ) ; \
convert_to_boolean ( & ztmp ) ; \
newval = & ztmp ; \
} \
\
snmp_object - > name = Z_LVAL_P ( newval ) ; \
\
if ( newval = = & ztmp ) { \
zval_dtor ( newval ) ; \
} \
return SUCCESS ; \
2011-01-31 19:41:33 +08:00
}
2011-07-18 03:47:24 +08:00
PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION ( quick_print )
PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION ( enum_print )
2011-07-21 20:48:47 +08:00
PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION ( oid_increasing_check )
2011-01-31 19:41:33 +08:00
/* {{{ */
static int php_snmp_write_oid_output_format ( php_snmp_object * snmp_object , zval * newval TSRMLS_DC )
{
zval ztmp ;
int ret = SUCCESS ;
if ( Z_TYPE_P ( newval ) ! = IS_LONG ) {
ztmp = * newval ;
zval_copy_ctor ( & ztmp ) ;
convert_to_long ( & ztmp ) ;
newval = & ztmp ;
}
switch ( Z_LVAL_P ( newval ) ) {
case NETSNMP_OID_OUTPUT_SUFFIX :
case NETSNMP_OID_OUTPUT_MODULE :
case NETSNMP_OID_OUTPUT_FULL :
case NETSNMP_OID_OUTPUT_NUMERIC :
case NETSNMP_OID_OUTPUT_UCD :
case NETSNMP_OID_OUTPUT_NONE :
snmp_object - > oid_output_format = Z_LVAL_P ( newval ) ;
break ;
default :
2011-02-01 09:02:00 +08:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Unknown SNMP output print format '%ld' " , Z_LVAL_P ( newval ) ) ;
2011-01-31 19:41:33 +08:00
ret = FAILURE ;
break ;
}
if ( newval = = & ztmp ) {
zval_dtor ( newval ) ;
}
return ret ;
}
/* }}} */
/* {{{ php_snmp_class_methods[] */
static zend_function_entry php_snmp_class_methods [ ] = {
2011-05-10 03:34:07 +08:00
PHP_ME ( snmp , __construct , arginfo_snmp_create , ZEND_ACC_PUBLIC )
2011-01-31 19:41:33 +08:00
PHP_ME ( snmp , close , arginfo_snmp_void , ZEND_ACC_PUBLIC )
2011-03-21 04:07:33 +08:00
PHP_ME ( snmp , setSecurity , arginfo_snmp_setSecurity , ZEND_ACC_PUBLIC )
2011-01-31 19:41:33 +08:00
PHP_ME ( snmp , get , arginfo_snmp_get , ZEND_ACC_PUBLIC )
PHP_ME ( snmp , getnext , arginfo_snmp_get , ZEND_ACC_PUBLIC )
PHP_ME ( snmp , walk , arginfo_snmp_walk , ZEND_ACC_PUBLIC )
PHP_ME ( snmp , set , arginfo_snmp_set , ZEND_ACC_PUBLIC )
2011-05-10 19:48:10 +08:00
PHP_ME ( snmp , getErrno , arginfo_snmp_void , ZEND_ACC_PUBLIC )
PHP_ME ( snmp , getError , arginfo_snmp_void , ZEND_ACC_PUBLIC )
2011-01-31 19:41:33 +08:00
2011-07-25 19:35:02 +08:00
PHP_FE_END
2011-01-31 19:41:33 +08:00
} ;
# define PHP_SNMP_PROPERTY_ENTRY_RECORD(name) \
{ " " # name " " , sizeof ( " " # name " " ) - 1 , php_snmp_read_ # # name , php_snmp_write_ # # name }
const php_snmp_prop_handler php_snmp_property_entries [ ] = {
2011-02-05 08:29:31 +08:00
PHP_SNMP_PROPERTY_ENTRY_RECORD ( info ) ,
2011-02-01 15:45:30 +08:00
PHP_SNMP_PROPERTY_ENTRY_RECORD ( max_oids ) ,
2011-01-31 19:41:33 +08:00
PHP_SNMP_PROPERTY_ENTRY_RECORD ( valueretrieval ) ,
PHP_SNMP_PROPERTY_ENTRY_RECORD ( quick_print ) ,
PHP_SNMP_PROPERTY_ENTRY_RECORD ( enum_print ) ,
PHP_SNMP_PROPERTY_ENTRY_RECORD ( oid_output_format ) ,
2011-07-21 20:48:47 +08:00
PHP_SNMP_PROPERTY_ENTRY_RECORD ( oid_increasing_check ) ,
2011-01-31 19:41:33 +08:00
{ NULL , 0 , NULL , NULL }
} ;
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION ( snmp )
{
netsnmp_log_handler * logh ;
zend_class_entry ce ;
le_snmp_session = zend_register_list_destructors_ex ( php_snmp_session_destructor , NULL , PHP_SNMP_SESSION_RES_NAME , module_number ) ;
init_snmp ( " snmpapp " ) ;
# ifdef NETSNMP_DS_LIB_DONT_PERSIST_STATE
/* Prevent update of the snmpapp.conf file */
netsnmp_ds_set_boolean ( NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_DONT_PERSIST_STATE , 1 ) ;
# endif
/* Disable logging, use exit status'es and related variabled to detect errors */
shutdown_snmp_logging ( ) ;
logh = netsnmp_register_loghandler ( NETSNMP_LOGHANDLER_NONE , LOG_ERR ) ;
if ( logh ) {
logh - > pri_max = LOG_ERR ;
}
memcpy ( & php_snmp_object_handlers , zend_get_std_object_handlers ( ) , sizeof ( zend_object_handlers ) ) ;
php_snmp_object_handlers . read_property = php_snmp_read_property ;
php_snmp_object_handlers . write_property = php_snmp_write_property ;
php_snmp_object_handlers . has_property = php_snmp_has_property ;
php_snmp_object_handlers . get_properties = php_snmp_get_properties ;
/* Register SNMP Class */
INIT_CLASS_ENTRY ( ce , " SNMP " , php_snmp_class_methods ) ;
ce . create_object = php_snmp_object_new ;
php_snmp_object_handlers . clone_obj = NULL ;
php_snmp_class_entry = zend_register_internal_class ( & ce TSRMLS_CC ) ;
/* Register SNMP Class properties */
zend_hash_init ( & php_snmp_properties , 0 , NULL , NULL , 1 ) ;
PHP_SNMP_ADD_PROPERTIES ( & php_snmp_properties , php_snmp_property_entries ) ;
2011-02-26 16:27:26 +08:00
REGISTER_LONG_CONSTANT ( " SNMP_OID_OUTPUT_SUFFIX " , NETSNMP_OID_OUTPUT_SUFFIX , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " SNMP_OID_OUTPUT_MODULE " , NETSNMP_OID_OUTPUT_MODULE , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " SNMP_OID_OUTPUT_FULL " , NETSNMP_OID_OUTPUT_FULL , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " SNMP_OID_OUTPUT_NUMERIC " , NETSNMP_OID_OUTPUT_NUMERIC , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " SNMP_OID_OUTPUT_UCD " , NETSNMP_OID_OUTPUT_UCD , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " SNMP_OID_OUTPUT_NONE " , NETSNMP_OID_OUTPUT_NONE , CONST_CS | CONST_PERSISTENT ) ;
2011-01-31 19:41:33 +08:00
2011-02-26 16:27:26 +08:00
REGISTER_LONG_CONSTANT ( " SNMP_VALUE_LIBRARY " , SNMP_VALUE_LIBRARY , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " SNMP_VALUE_PLAIN " , SNMP_VALUE_PLAIN , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " SNMP_VALUE_OBJECT " , SNMP_VALUE_OBJECT , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " SNMP_BIT_STR " , ASN_BIT_STR , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " SNMP_OCTET_STR " , ASN_OCTET_STR , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " SNMP_OPAQUE " , ASN_OPAQUE , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " SNMP_NULL " , ASN_NULL , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " SNMP_OBJECT_ID " , ASN_OBJECT_ID , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " SNMP_IPADDRESS " , ASN_IPADDRESS , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " SNMP_COUNTER " , ASN_GAUGE , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " SNMP_UNSIGNED " , ASN_UNSIGNED , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " SNMP_TIMETICKS " , ASN_TIMETICKS , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " SNMP_UINTEGER " , ASN_UINTEGER , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " SNMP_INTEGER " , ASN_INTEGER , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " SNMP_COUNTER64 " , ASN_COUNTER64 , CONST_CS | CONST_PERSISTENT ) ;
2011-06-12 20:10:01 +08:00
REGISTER_SNMP_CLASS_CONST_LONG ( " VERSION_1 " , ( long ) SNMP_VERSION_1 ) ;
REGISTER_SNMP_CLASS_CONST_LONG ( " VERSION_2c " , ( long ) SNMP_VERSION_2c ) ;
REGISTER_SNMP_CLASS_CONST_LONG ( " VERSION_2C " , ( long ) SNMP_VERSION_2c ) ;
REGISTER_SNMP_CLASS_CONST_LONG ( " VERSION_3 " , ( long ) SNMP_VERSION_3 ) ;
2011-02-26 16:27:26 +08:00
2011-03-05 02:58:01 +08:00
REGISTER_SNMP_CLASS_CONST_LONG ( " ERRNO_NOERROR " , ( long ) PHP_SNMP_ERRNO_NOERROR ) ;
REGISTER_SNMP_CLASS_CONST_LONG ( " ERRNO_GENERIC " , ( long ) PHP_SNMP_ERRNO_GENERIC ) ;
REGISTER_SNMP_CLASS_CONST_LONG ( " ERRNO_TIMEOUT " , ( long ) PHP_SNMP_ERRNO_TIMEOUT ) ;
REGISTER_SNMP_CLASS_CONST_LONG ( " ERRNO_ERROR_IN_REPLY " , ( long ) PHP_SNMP_ERRNO_ERROR_IN_REPLY ) ;
REGISTER_SNMP_CLASS_CONST_LONG ( " ERRNO_OID_NOT_INCREASING " , ( long ) PHP_SNMP_ERRNO_OID_NOT_INCREASING ) ;
2011-01-31 19:41:33 +08:00
return SUCCESS ;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION ( snmp )
{
snmp_shutdown ( " snmpapp " ) ;
zend_hash_destroy ( & php_snmp_properties ) ;
return SUCCESS ;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION ( snmp )
{
php_info_print_table_start ( ) ;
php_info_print_table_row ( 2 , " NET-SNMP Support " , " enabled " ) ;
php_info_print_table_row ( 2 , " NET-SNMP Version " , netsnmp_get_version ( ) ) ;
php_info_print_table_row ( 2 , " PHP SNMP Version " , PHP_SNMP_VERSION ) ;
php_info_print_table_end ( ) ;
}
/* }}} */
/* {{{ snmp_module_entry
*/
zend_module_entry snmp_module_entry = {
STANDARD_MODULE_HEADER ,
" snmp " ,
snmp_functions ,
PHP_MINIT ( snmp ) ,
PHP_MSHUTDOWN ( snmp ) ,
NULL ,
NULL ,
PHP_MINFO ( snmp ) ,
PHP_SNMP_VERSION ,
PHP_MODULE_GLOBALS ( snmp ) ,
PHP_GINIT ( snmp ) ,
NULL ,
NULL ,
STANDARD_MODULE_PROPERTIES_EX
} ;
/* }}} */
1999-04-22 10:48:28 +08:00
# endif
/*
2001-06-05 21:12:10 +08:00
* Local variables :
* tab - width : 4
* c - basic - offset : 4
* End :
2001-09-09 21:29:31 +08:00
* vim600 : sw = 4 ts = 4 fdm = marker
* vim < 600 : sw = 4 ts = 4
2001-06-05 21:12:10 +08:00
*/