PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/snmp - snmp.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 899 979 91.8 %
Date: 2014-10-22 Functions: 73 74 98.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Rasmus Lerdorf <rasmus@php.net>                             |
      16             :    |          Mike Jackson <mhjack@tscnet.com>                            |
      17             :    |          Steven Lawrance <slawrance@technologist.com>                |
      18             :    |          Harrie Hazewinkel <harrie@lisanza.net>                      |
      19             :    |          Johann Hanne <jonny@nurfuerspam.de>                         |
      20             :    |          Boris Lytockin <lytboris@gmail.com>                         |
      21             :    +----------------------------------------------------------------------+
      22             :  */
      23             : 
      24             : /* $Id$ */
      25             : 
      26             : #ifdef HAVE_CONFIG_H
      27             : #include "config.h"
      28             : #endif
      29             : 
      30             : #include "php.h"
      31             : #include "main/php_network.h"
      32             : #include "ext/standard/info.h"
      33             : #include "php_snmp.h"
      34             : 
      35             : #include "zend_exceptions.h"
      36             : 
      37             : #if HAVE_SPL
      38             : #include "ext/spl/spl_exceptions.h"
      39             : #endif
      40             : 
      41             : #if HAVE_SNMP
      42             : 
      43             : #include <sys/types.h>
      44             : #ifdef PHP_WIN32
      45             : #include <winsock2.h>
      46             : #include <errno.h>
      47             : #include <process.h>
      48             : #include "win32/time.h"
      49             : #elif defined(NETWARE)
      50             : #ifdef USE_WINSOCK
      51             : #include <novsock2.h>
      52             : #else
      53             : #include <sys/socket.h>
      54             : #endif
      55             : #include <errno.h>
      56             : #include <sys/timeval.h>
      57             : #else
      58             : #include <sys/socket.h>
      59             : #include <netinet/in.h>
      60             : #include <arpa/inet.h>
      61             : #ifndef _OSD_POSIX
      62             : #include <sys/errno.h>
      63             : #else
      64             : #include <errno.h>  /* BS2000/OSD uses <errno.h>, not <sys/errno.h> */
      65             : #endif
      66             : #include <netdb.h>
      67             : #endif
      68             : #ifdef HAVE_UNISTD_H
      69             : #include <unistd.h>
      70             : #endif
      71             : 
      72             : #ifndef __P
      73             : #ifdef __GNUC__
      74             : #define __P(args) args
      75             : #else
      76             : #define __P(args) ()
      77             : #endif
      78             : #endif
      79             : 
      80             : #include <net-snmp/net-snmp-config.h>
      81             : #include <net-snmp/net-snmp-includes.h>
      82             : 
      83             : /* For net-snmp prior to 5.4 */
      84             : #ifndef HAVE_SHUTDOWN_SNMP_LOGGING
      85             : extern netsnmp_log_handler *logh_head;
      86             : #define shutdown_snmp_logging() \
      87             :         { \
      88             :                 snmp_disable_log(); \
      89             :                 while(NULL != logh_head) \
      90             :                         netsnmp_remove_loghandler( logh_head ); \
      91             :         }
      92             : #endif
      93             : 
      94             : #define SNMP_VALUE_LIBRARY      (0 << 0)
      95             : #define SNMP_VALUE_PLAIN        (1 << 0)
      96             : #define SNMP_VALUE_OBJECT       (1 << 1)
      97             : 
      98             : typedef struct snmp_session php_snmp_session;
      99             : #define PHP_SNMP_SESSION_RES_NAME "SNMP session"
     100             : 
     101             : #define PHP_SNMP_ADD_PROPERTIES(a, b) \
     102             : { \
     103             :         int i = 0; \
     104             :         while (b[i].name != NULL) { \
     105             :                 php_snmp_add_property((a), (b)[i].name, (b)[i].name_length, \
     106             :                                                         (php_snmp_read_t)(b)[i].read_func, (php_snmp_write_t)(b)[i].write_func TSRMLS_CC); \
     107             :                 i++; \
     108             :         } \
     109             : }
     110             : 
     111             : #define PHP_SNMP_ERRNO_NOERROR                  0
     112             : #define PHP_SNMP_ERRNO_GENERIC                  (1 << 1)
     113             : #define PHP_SNMP_ERRNO_TIMEOUT                  (1 << 2)
     114             : #define PHP_SNMP_ERRNO_ERROR_IN_REPLY           (1 << 3)
     115             : #define PHP_SNMP_ERRNO_OID_NOT_INCREASING       (1 << 4)
     116             : #define PHP_SNMP_ERRNO_OID_PARSING_ERROR        (1 << 5)
     117             : #define PHP_SNMP_ERRNO_MULTIPLE_SET_QUERIES     (1 << 6)
     118             : #define PHP_SNMP_ERRNO_ANY      ( \
     119             :                 PHP_SNMP_ERRNO_GENERIC | \
     120             :                 PHP_SNMP_ERRNO_TIMEOUT | \
     121             :                 PHP_SNMP_ERRNO_ERROR_IN_REPLY | \
     122             :                 PHP_SNMP_ERRNO_OID_NOT_INCREASING | \
     123             :                 PHP_SNMP_ERRNO_OID_PARSING_ERROR | \
     124             :                 PHP_SNMP_ERRNO_MULTIPLE_SET_QUERIES | \
     125             :                 PHP_SNMP_ERRNO_NOERROR \
     126             :         )
     127             : 
     128             : ZEND_DECLARE_MODULE_GLOBALS(snmp)
     129             : static PHP_GINIT_FUNCTION(snmp);
     130             : 
     131             : /* constant - can be shared among threads */
     132             : static oid objid_mib[] = {1, 3, 6, 1, 2, 1};
     133             : 
     134             : static int le_snmp_session;
     135             : 
     136             : /* Handlers */
     137             : static zend_object_handlers php_snmp_object_handlers;
     138             : 
     139             : /* Class entries */
     140             : zend_class_entry *php_snmp_ce;
     141             : zend_class_entry *php_snmp_exception_ce;
     142             : 
     143             : /* Class object properties */
     144             : static HashTable php_snmp_properties;
     145             : 
     146             : /* {{{ arginfo */
     147             : 
     148             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpget, 0, 0, 3)
     149             :         ZEND_ARG_INFO(0, host)
     150             :         ZEND_ARG_INFO(0, community)
     151             :         ZEND_ARG_INFO(0, object_id)
     152             :         ZEND_ARG_INFO(0, timeout)
     153             :         ZEND_ARG_INFO(0, retries)
     154             : ZEND_END_ARG_INFO()
     155             : 
     156             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpgetnext, 0, 0, 3)
     157             :         ZEND_ARG_INFO(0, host)
     158             :         ZEND_ARG_INFO(0, community)
     159             :         ZEND_ARG_INFO(0, object_id)
     160             :         ZEND_ARG_INFO(0, timeout)
     161             :         ZEND_ARG_INFO(0, retries)
     162             : ZEND_END_ARG_INFO()
     163             : 
     164             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpwalk, 0, 0, 3)
     165             :         ZEND_ARG_INFO(0, host)
     166             :         ZEND_ARG_INFO(0, community)
     167             :         ZEND_ARG_INFO(0, object_id)
     168             :         ZEND_ARG_INFO(0, timeout)
     169             :         ZEND_ARG_INFO(0, retries)
     170             : ZEND_END_ARG_INFO()
     171             : 
     172             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmprealwalk, 0, 0, 3)
     173             :         ZEND_ARG_INFO(0, host)
     174             :         ZEND_ARG_INFO(0, community)
     175             :         ZEND_ARG_INFO(0, object_id)
     176             :         ZEND_ARG_INFO(0, timeout)
     177             :         ZEND_ARG_INFO(0, retries)
     178             : ZEND_END_ARG_INFO()
     179             : 
     180             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpset, 0, 0, 5)
     181             :         ZEND_ARG_INFO(0, host)
     182             :         ZEND_ARG_INFO(0, community)
     183             :         ZEND_ARG_INFO(0, object_id)
     184             :         ZEND_ARG_INFO(0, type)
     185             :         ZEND_ARG_INFO(0, value)
     186             :         ZEND_ARG_INFO(0, timeout)
     187             :         ZEND_ARG_INFO(0, retries)
     188             : ZEND_END_ARG_INFO()
     189             : 
     190             : 
     191             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_get_quick_print, 0, 0, 1)
     192             :         ZEND_ARG_INFO(0, d)
     193             : ZEND_END_ARG_INFO()
     194             : 
     195             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set_quick_print, 0, 0, 1)
     196             :         ZEND_ARG_INFO(0, quick_print)
     197             : ZEND_END_ARG_INFO()
     198             : 
     199             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set_enum_print, 0, 0, 1)
     200             :         ZEND_ARG_INFO(0, enum_print)
     201             : ZEND_END_ARG_INFO()
     202             : 
     203             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set_oid_output_format, 0, 0, 1)
     204             :         ZEND_ARG_INFO(0, oid_format)
     205             : ZEND_END_ARG_INFO()
     206             : 
     207             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_get, 0, 0, 3)
     208             :         ZEND_ARG_INFO(0, host)
     209             :         ZEND_ARG_INFO(0, community)
     210             :         ZEND_ARG_INFO(0, object_id)
     211             :         ZEND_ARG_INFO(0, timeout)
     212             :         ZEND_ARG_INFO(0, retries)
     213             : ZEND_END_ARG_INFO()
     214             : 
     215             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_getnext, 0, 0, 3)
     216             :         ZEND_ARG_INFO(0, host)
     217             :         ZEND_ARG_INFO(0, community)
     218             :         ZEND_ARG_INFO(0, object_id)
     219             :         ZEND_ARG_INFO(0, timeout)
     220             :         ZEND_ARG_INFO(0, retries)
     221             : ZEND_END_ARG_INFO()
     222             : 
     223             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_walk, 0, 0, 3)
     224             :         ZEND_ARG_INFO(0, host)
     225             :         ZEND_ARG_INFO(0, community)
     226             :         ZEND_ARG_INFO(0, object_id)
     227             :         ZEND_ARG_INFO(0, timeout)
     228             :         ZEND_ARG_INFO(0, retries)
     229             : ZEND_END_ARG_INFO()
     230             : 
     231             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_real_walk, 0, 0, 3)
     232             :         ZEND_ARG_INFO(0, host)
     233             :         ZEND_ARG_INFO(0, community)
     234             :         ZEND_ARG_INFO(0, object_id)
     235             :         ZEND_ARG_INFO(0, timeout)
     236             :         ZEND_ARG_INFO(0, retries)
     237             : ZEND_END_ARG_INFO()
     238             : 
     239             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_set, 0, 0, 5)
     240             :         ZEND_ARG_INFO(0, host)
     241             :         ZEND_ARG_INFO(0, community)
     242             :         ZEND_ARG_INFO(0, object_id)
     243             :         ZEND_ARG_INFO(0, type)
     244             :         ZEND_ARG_INFO(0, value)
     245             :         ZEND_ARG_INFO(0, timeout)
     246             :         ZEND_ARG_INFO(0, retries)
     247             : ZEND_END_ARG_INFO()
     248             : 
     249             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_get, 0, 0, 8)
     250             :         ZEND_ARG_INFO(0, host)
     251             :         ZEND_ARG_INFO(0, sec_name)
     252             :         ZEND_ARG_INFO(0, sec_level)
     253             :         ZEND_ARG_INFO(0, auth_protocol)
     254             :         ZEND_ARG_INFO(0, auth_passphrase)
     255             :         ZEND_ARG_INFO(0, priv_protocol)
     256             :         ZEND_ARG_INFO(0, priv_passphrase)
     257             :         ZEND_ARG_INFO(0, object_id)
     258             :         ZEND_ARG_INFO(0, timeout)
     259             :         ZEND_ARG_INFO(0, retries)
     260             : ZEND_END_ARG_INFO()
     261             : 
     262             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_getnext, 0, 0, 8)
     263             :         ZEND_ARG_INFO(0, host)
     264             :         ZEND_ARG_INFO(0, sec_name)
     265             :         ZEND_ARG_INFO(0, sec_level)
     266             :         ZEND_ARG_INFO(0, auth_protocol)
     267             :         ZEND_ARG_INFO(0, auth_passphrase)
     268             :         ZEND_ARG_INFO(0, priv_protocol)
     269             :         ZEND_ARG_INFO(0, priv_passphrase)
     270             :         ZEND_ARG_INFO(0, object_id)
     271             :         ZEND_ARG_INFO(0, timeout)
     272             :         ZEND_ARG_INFO(0, retries)
     273             : ZEND_END_ARG_INFO()
     274             : 
     275             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_walk, 0, 0, 8)
     276             :         ZEND_ARG_INFO(0, host)
     277             :         ZEND_ARG_INFO(0, sec_name)
     278             :         ZEND_ARG_INFO(0, sec_level)
     279             :         ZEND_ARG_INFO(0, auth_protocol)
     280             :         ZEND_ARG_INFO(0, auth_passphrase)
     281             :         ZEND_ARG_INFO(0, priv_protocol)
     282             :         ZEND_ARG_INFO(0, priv_passphrase)
     283             :         ZEND_ARG_INFO(0, object_id)
     284             :         ZEND_ARG_INFO(0, timeout)
     285             :         ZEND_ARG_INFO(0, retries)
     286             : ZEND_END_ARG_INFO()
     287             : 
     288             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_real_walk, 0, 0, 8)
     289             :         ZEND_ARG_INFO(0, host)
     290             :         ZEND_ARG_INFO(0, sec_name)
     291             :         ZEND_ARG_INFO(0, sec_level)
     292             :         ZEND_ARG_INFO(0, auth_protocol)
     293             :         ZEND_ARG_INFO(0, auth_passphrase)
     294             :         ZEND_ARG_INFO(0, priv_protocol)
     295             :         ZEND_ARG_INFO(0, priv_passphrase)
     296             :         ZEND_ARG_INFO(0, object_id)
     297             :         ZEND_ARG_INFO(0, timeout)
     298             :         ZEND_ARG_INFO(0, retries)
     299             : ZEND_END_ARG_INFO()
     300             : 
     301             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_set, 0, 0, 10)
     302             :         ZEND_ARG_INFO(0, host)
     303             :         ZEND_ARG_INFO(0, sec_name)
     304             :         ZEND_ARG_INFO(0, sec_level)
     305             :         ZEND_ARG_INFO(0, auth_protocol)
     306             :         ZEND_ARG_INFO(0, auth_passphrase)
     307             :         ZEND_ARG_INFO(0, priv_protocol)
     308             :         ZEND_ARG_INFO(0, priv_passphrase)
     309             :         ZEND_ARG_INFO(0, object_id)
     310             :         ZEND_ARG_INFO(0, type)
     311             :         ZEND_ARG_INFO(0, value)
     312             :         ZEND_ARG_INFO(0, timeout)
     313             :         ZEND_ARG_INFO(0, retries)
     314             : ZEND_END_ARG_INFO()
     315             : 
     316             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set_valueretrieval, 0, 0, 1)
     317             :         ZEND_ARG_INFO(0, method)
     318             : ZEND_END_ARG_INFO()
     319             : 
     320             : ZEND_BEGIN_ARG_INFO(arginfo_snmp_get_valueretrieval, 0)
     321             : ZEND_END_ARG_INFO()
     322             : 
     323             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_read_mib, 0, 0, 1)
     324             :         ZEND_ARG_INFO(0, filename)
     325             : ZEND_END_ARG_INFO()
     326             : 
     327             : /* OO arginfo */
     328             : 
     329             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_create, 0, 0, 3)
     330             :         ZEND_ARG_INFO(0, version)
     331             :         ZEND_ARG_INFO(0, host)
     332             :         ZEND_ARG_INFO(0, community)
     333             :         ZEND_ARG_INFO(0, timeout)
     334             :         ZEND_ARG_INFO(0, retries)
     335             : ZEND_END_ARG_INFO()
     336             : 
     337             : ZEND_BEGIN_ARG_INFO(arginfo_snmp_void, 0)
     338             : ZEND_END_ARG_INFO()
     339             : 
     340             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_setSecurity, 0, 0, 8)
     341             :         ZEND_ARG_INFO(0, session)
     342             :         ZEND_ARG_INFO(0, sec_level)
     343             :         ZEND_ARG_INFO(0, auth_protocol)
     344             :         ZEND_ARG_INFO(0, auth_passphrase)
     345             :         ZEND_ARG_INFO(0, priv_protocol)
     346             :         ZEND_ARG_INFO(0, priv_passphrase)
     347             :         ZEND_ARG_INFO(0, contextName)
     348             :         ZEND_ARG_INFO(0, contextEngineID)
     349             :         ZEND_ARG_INFO(0, )
     350             : ZEND_END_ARG_INFO()
     351             : 
     352             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_get, 0, 0, 1)
     353             :         ZEND_ARG_INFO(0, object_id)
     354             :         ZEND_ARG_INFO(0, use_orignames)
     355             : ZEND_END_ARG_INFO()
     356             : 
     357             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_walk, 0, 0, 4)
     358             :         ZEND_ARG_INFO(0, object_id)
     359             :         ZEND_ARG_INFO(0, suffix_keys)
     360             :         ZEND_ARG_INFO(0, max_repetitions)
     361             :         ZEND_ARG_INFO(0, non_repeaters)
     362             : ZEND_END_ARG_INFO()
     363             : 
     364             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set, 0, 0, 3)
     365             :         ZEND_ARG_INFO(0, object_id)
     366             :         ZEND_ARG_INFO(0, type)
     367             :         ZEND_ARG_INFO(0, value)
     368             : ZEND_END_ARG_INFO()
     369             : 
     370             : ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_class_set_quick_print, 0, 0, 1)
     371             :         ZEND_ARG_INFO(0, quick_print)
     372             : ZEND_END_ARG_INFO()
     373             : /* }}} */
     374             : 
     375             : struct objid_query {
     376             :         int count;
     377             :         int offset;
     378             :         int step;
     379             :         zend_long non_repeaters;
     380             :         zend_long max_repetitions;
     381             :         int valueretrieval;
     382             :         int array_output;
     383             :         int oid_increasing_check;
     384             :         snmpobjarg *vars;
     385             : };
     386             : 
     387             : /* {{{ snmp_functions[]
     388             :  */
     389             : const zend_function_entry snmp_functions[] = {
     390             :         PHP_FE(snmpget,                                 arginfo_snmpget)
     391             :         PHP_FE(snmpgetnext,                             arginfo_snmpgetnext)
     392             :         PHP_FE(snmpwalk,                                arginfo_snmpwalk)
     393             :         PHP_FE(snmprealwalk,                            arginfo_snmprealwalk)
     394             :         PHP_FALIAS(snmpwalkoid, snmprealwalk,           arginfo_snmprealwalk)
     395             :         PHP_FE(snmpset,                                 arginfo_snmpset)
     396             :         PHP_FE(snmp_get_quick_print,                    arginfo_snmp_get_quick_print)
     397             :         PHP_FE(snmp_set_quick_print,                    arginfo_snmp_set_quick_print)
     398             :         PHP_FE(snmp_set_enum_print,                     arginfo_snmp_set_enum_print)
     399             :         PHP_FE(snmp_set_oid_output_format,              arginfo_snmp_set_oid_output_format)
     400             :         PHP_FALIAS(snmp_set_oid_numeric_print, snmp_set_oid_output_format, arginfo_snmp_set_oid_output_format)
     401             : 
     402             :         PHP_FE(snmp2_get,                               arginfo_snmp2_get)
     403             :         PHP_FE(snmp2_getnext,                           arginfo_snmp2_getnext)
     404             :         PHP_FE(snmp2_walk,                              arginfo_snmp2_walk)
     405             :         PHP_FE(snmp2_real_walk,                         arginfo_snmp2_real_walk)
     406             :         PHP_FE(snmp2_set,                               arginfo_snmp2_set)
     407             : 
     408             :         PHP_FE(snmp3_get,                               arginfo_snmp3_get)
     409             :         PHP_FE(snmp3_getnext,                           arginfo_snmp3_getnext)
     410             :         PHP_FE(snmp3_walk,                              arginfo_snmp3_walk)
     411             :         PHP_FE(snmp3_real_walk,                         arginfo_snmp3_real_walk)
     412             :         PHP_FE(snmp3_set,                               arginfo_snmp3_set)
     413             :         PHP_FE(snmp_set_valueretrieval,                 arginfo_snmp_set_valueretrieval)
     414             :         PHP_FE(snmp_get_valueretrieval,                 arginfo_snmp_get_valueretrieval)
     415             : 
     416             :         PHP_FE(snmp_read_mib,                           arginfo_snmp_read_mib)
     417             :         PHP_FE_END
     418             : };
     419             : /* }}} */
     420             : 
     421             : /* query an agent with GET method */
     422             : #define SNMP_CMD_GET            (1<<0)
     423             : /* query an agent with GETNEXT method */
     424             : #define SNMP_CMD_GETNEXT        (1<<1)
     425             : /* query an agent with SET method */
     426             : #define SNMP_CMD_SET            (1<<2)
     427             : /* walk the mib */
     428             : #define SNMP_CMD_WALK           (1<<3)
     429             : /* force values-only output */
     430             : #define SNMP_NUMERIC_KEYS       (1<<7)
     431             : /* use user-supplied OID names for keys in array output mode in GET method */
     432             : #define SNMP_ORIGINAL_NAMES_AS_KEYS     (1<<8)
     433             : /* use OID suffix (`index') for keys in array output mode in WALK  method */
     434             : #define SNMP_USE_SUFFIX_AS_KEYS (1<<9)
     435             : 
     436             : #ifdef COMPILE_DL_SNMP
     437             : ZEND_GET_MODULE(snmp)
     438             : #endif
     439             : 
     440             : /* THREAD_LS snmp_module php_snmp_module; - may need one of these at some point */
     441             : 
     442             : /* {{{ PHP_GINIT_FUNCTION
     443             :  */
     444       20423 : static PHP_GINIT_FUNCTION(snmp)
     445             : {
     446       20423 :         snmp_globals->valueretrieval = SNMP_VALUE_LIBRARY;
     447       20423 : }
     448             : /* }}} */
     449             : 
     450             : #define PHP_SNMP_SESSION_FREE(a) { \
     451             :         if ((*session)->a) { \
     452             :                 efree((*session)->a); \
     453             :                 (*session)->a = NULL; \
     454             :         } \
     455             : }
     456             : 
     457         279 : static void netsnmp_session_free(php_snmp_session **session) /* {{{ */
     458             : {
     459         279 :         if (*session) {
     460         248 :                 PHP_SNMP_SESSION_FREE(peername);
     461         248 :                 PHP_SNMP_SESSION_FREE(community);
     462         248 :                 PHP_SNMP_SESSION_FREE(securityName);
     463         248 :                 PHP_SNMP_SESSION_FREE(contextEngineID);
     464         248 :                 efree(*session);
     465         248 :                 *session = NULL;
     466             :         }
     467         279 : }
     468             : /* }}} */
     469             : 
     470           0 : static void php_snmp_session_destructor(zend_resource *rsrc TSRMLS_DC) /* {{{ */
     471             : {
     472           0 :         php_snmp_session *session = (php_snmp_session *)rsrc->ptr;
     473           0 :         netsnmp_session_free(&session);
     474           0 : }
     475             : /* }}} */
     476             : 
     477          33 : static void php_snmp_object_free_storage(zend_object *object TSRMLS_DC) /* {{{ */
     478             : {
     479          33 :         php_snmp_object *intern = php_snmp_fetch_object(object);
     480             :         
     481          33 :         if (!intern) {
     482           0 :                 return;
     483             :         }
     484             : 
     485          33 :         netsnmp_session_free(&(intern->session));
     486             : 
     487          33 :         zend_object_std_dtor(&intern->zo TSRMLS_CC);
     488             : }
     489             : /* }}} */
     490             : 
     491          33 : static zend_object *php_snmp_object_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
     492             : {
     493             :         php_snmp_object *intern;
     494             : 
     495             :         /* Allocate memory for it */
     496          33 :         intern = ecalloc(1, sizeof(php_snmp_object) + sizeof(zval) *  (class_type->default_properties_count - 1));
     497             : 
     498          33 :         zend_object_std_init(&intern->zo, class_type TSRMLS_CC);
     499          33 :         object_properties_init(&intern->zo, class_type);
     500             : 
     501          33 :         intern->zo.handlers = &php_snmp_object_handlers;
     502             : 
     503          33 :         return &intern->zo;
     504             :         
     505             : }
     506             : /* }}} */
     507             : 
     508             : /* {{{ php_snmp_error
     509             :  *
     510             :  * Record last SNMP-related error in object
     511             :  *
     512             :  */
     513         318 : static void php_snmp_error(zval *object, const char *docref TSRMLS_DC, int type, const char *format, ...)
     514             : {
     515             :         va_list args;
     516         318 :         php_snmp_object *snmp_object = NULL;
     517             : 
     518         318 :         if (object) {
     519          59 :                 snmp_object = Z_SNMP_P(object);
     520          59 :                 if (type == PHP_SNMP_ERRNO_NOERROR) {
     521          45 :                         memset(snmp_object->snmp_errstr, 0, sizeof(snmp_object->snmp_errstr));
     522             :                 } else {
     523          14 :                         va_start(args, format);
     524          14 :                         vsnprintf(snmp_object->snmp_errstr, sizeof(snmp_object->snmp_errstr) - 1, format, args);
     525          14 :                         va_end(args);
     526             :                 }
     527          59 :                 snmp_object->snmp_errno = type;
     528             :         }
     529             : 
     530         318 :         if (type == PHP_SNMP_ERRNO_NOERROR) {
     531         254 :                 return;
     532             :         }
     533             : 
     534          65 :         if (object && (snmp_object->exceptions_enabled & type)) {
     535           1 :                 zend_throw_exception_ex(php_snmp_exception_ce, type TSRMLS_CC, snmp_object->snmp_errstr);
     536             :         } else {
     537          63 :                 va_start(args, format);
     538          63 :                 php_verror(docref, "", E_WARNING, format, args TSRMLS_CC);
     539          63 :                 va_end(args);
     540             :         }
     541             : }
     542             : 
     543             : /* }}} */
     544             : 
     545             : /* {{{ php_snmp_getvalue
     546             : *
     547             : * SNMP value to zval converter
     548             : *
     549             : */
     550       21714 : static void php_snmp_getvalue(struct variable_list *vars, zval *snmpval TSRMLS_DC, int valueretrieval)
     551             : {
     552             :         zval val;
     553             :         char sbuf[512];
     554       21714 :         char *buf = &(sbuf[0]);
     555       21714 :         char *dbuf = (char *)NULL;
     556       21714 :         int buflen = sizeof(sbuf) - 1;
     557       21714 :         int val_len = vars->val_len;
     558             :         
     559             :         /* use emalloc() for large values, use static array otherwize */
     560             : 
     561             :         /* There is no way to know the size of buffer snprint_value() needs in order to print a value there.
     562             :          * So we are forced to probe it
     563             :          */
     564       43430 :         while ((valueretrieval & SNMP_VALUE_PLAIN) == 0) {
     565          41 :                 *buf = '\0';
     566          41 :                 if (snprint_value(buf, buflen, vars->name, vars->name_length, vars) == -1) {
     567           2 :                         if (val_len > 512*1024) {
     568           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "snprint_value() asks for a buffer more than 512k, Net-SNMP bug?");
     569           0 :                                 break;
     570             :                         }
     571             :                          /* buffer is not long enough to hold full output, double it */
     572           2 :                         val_len *= 2;
     573             :                 } else {
     574          39 :                         break;
     575             :                 }
     576             : 
     577           2 :                 if (buf == dbuf) {
     578           1 :                         dbuf = (char *)erealloc(dbuf, val_len + 1);
     579             :                 } else {
     580           1 :                         dbuf = (char *)emalloc(val_len + 1);
     581             :                 }
     582             : 
     583           2 :                 if (!dbuf) {
     584           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "emalloc() failed: %s, fallback to static buffer", strerror(errno));
     585           0 :                         buf = &(sbuf[0]);
     586           0 :                         buflen = sizeof(sbuf) - 1;
     587           0 :                         break;
     588             :                 }
     589             : 
     590           2 :                 buf = dbuf;
     591           2 :                 buflen = val_len;
     592             :         }
     593             : 
     594       21714 :         if((valueretrieval & SNMP_VALUE_PLAIN) && val_len > buflen){
     595           5 :                 if ((dbuf = (char *)emalloc(val_len + 1))) {
     596           5 :                         buf = dbuf;
     597           5 :                         buflen = val_len;
     598             :                 } else {
     599           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "emalloc() failed: %s, fallback to static buffer", strerror(errno));
     600             :                 }
     601             :         }
     602             : 
     603       21714 :         if (valueretrieval & SNMP_VALUE_PLAIN) {
     604       21675 :                 *buf = 0;
     605       21675 :                 switch (vars->type) {
     606             :                 case ASN_BIT_STR:               /* 0x03, asn1.h */
     607           0 :                         ZVAL_STRINGL(&val, (char *)vars->val.bitstring, vars->val_len);
     608           0 :                         break;
     609             : 
     610             :                 case ASN_OCTET_STR:             /* 0x04, asn1.h */
     611             :                 case ASN_OPAQUE:                /* 0x44, snmp_impl.h */
     612       14816 :                         ZVAL_STRINGL(&val, (char *)vars->val.string, vars->val_len);
     613        7408 :                         break;
     614             : 
     615             :                 case ASN_NULL:                  /* 0x05, asn1.h */
     616           0 :                         ZVAL_NULL(&val);
     617           0 :                         break;
     618             : 
     619             :                 case ASN_OBJECT_ID:             /* 0x06, asn1.h */
     620        2971 :                         snprint_objid(buf, buflen, vars->val.objid, vars->val_len / sizeof(oid));
     621        5942 :                         ZVAL_STRING(&val, buf);
     622        2971 :                         break;
     623             : 
     624             :                 case ASN_IPADDRESS:             /* 0x40, snmp_impl.h */
     625         620 :                         snprintf(buf, buflen, "%d.%d.%d.%d",
     626         310 :                                  (vars->val.string)[0], (vars->val.string)[1],
     627         310 :                                  (vars->val.string)[2], (vars->val.string)[3]);
     628         155 :                         buf[buflen]=0;
     629         310 :                         ZVAL_STRING(&val, buf);
     630         155 :                         break;
     631             : 
     632             :                 case ASN_COUNTER:               /* 0x41, snmp_impl.h */
     633             :                 case ASN_GAUGE:                 /* 0x42, snmp_impl.h */
     634             :                 /* ASN_UNSIGNED is the same as ASN_GAUGE */
     635             :                 case ASN_TIMETICKS:             /* 0x43, snmp_impl.h */
     636             :                 case ASN_UINTEGER:              /* 0x47, snmp_impl.h */
     637        1674 :                         snprintf(buf, buflen, "%lu", *vars->val.integer);
     638        1674 :                         buf[buflen]=0;
     639        3348 :                         ZVAL_STRING(&val, buf);
     640        1674 :                         break;
     641             : 
     642             :                 case ASN_INTEGER:               /* 0x02, asn1.h */
     643        9320 :                         snprintf(buf, buflen, "%ld", *vars->val.integer);
     644        9320 :                         buf[buflen]=0;
     645       18640 :                         ZVAL_STRING(&val, buf);
     646        9320 :                         break;
     647             : 
     648             : #if defined(NETSNMP_WITH_OPAQUE_SPECIAL_TYPES) || defined(OPAQUE_SPECIAL_TYPES)
     649             :                 case ASN_OPAQUE_FLOAT:          /* 0x78, asn1.h */
     650           3 :                         snprintf(buf, buflen, "%f", *vars->val.floatVal);
     651           6 :                         ZVAL_STRING(&val, buf);
     652           3 :                         break;
     653             : 
     654             :                 case ASN_OPAQUE_DOUBLE:         /* 0x79, asn1.h */
     655           0 :                         snprintf(buf, buflen, "%Lf", *vars->val.doubleVal);
     656           0 :                         ZVAL_STRING(&val, buf);
     657           0 :                         break;
     658             : 
     659             :                 case ASN_OPAQUE_I64:            /* 0x80, asn1.h */
     660           0 :                         printI64(buf, vars->val.counter64);
     661           0 :                         ZVAL_STRING(&val, buf);
     662           0 :                         break;
     663             : 
     664             :                 case ASN_OPAQUE_U64:            /* 0x81, asn1.h */
     665             : #endif
     666             :                 case ASN_COUNTER64:             /* 0x46, snmp_impl.h */
     667         144 :                         printU64(buf, vars->val.counter64);
     668         288 :                         ZVAL_STRING(&val, buf);
     669         144 :                         break;
     670             : 
     671             :                 default:
     672           0 :                         ZVAL_STRING(&val, "Unknown value type");
     673           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown value type: %u", vars->type);
     674             :                         break;
     675             :                 }
     676             :         } else /* use Net-SNMP value translation */ {
     677             :                 /* we have desired string in buffer, just use it */
     678          78 :                 ZVAL_STRING(&val, buf);
     679             :         }
     680             : 
     681       21714 :         if (valueretrieval & SNMP_VALUE_OBJECT) {
     682           4 :                 object_init(snmpval);
     683           4 :                 add_property_long(snmpval, "type", vars->type);
     684           4 :                 add_property_zval(snmpval, "value", &val);
     685             :         } else  {
     686       21710 :                 ZVAL_COPY(snmpval, &val);
     687             :         }
     688       21714 :         zval_ptr_dtor(&val);
     689             : 
     690       21714 :         if (dbuf){ /* malloc was used to store value */
     691           6 :                 efree(dbuf);
     692             :         }
     693       21714 : }
     694             : /* }}} */
     695             : 
     696             : /* {{{ php_snmp_internal
     697             : *
     698             : * SNMP object fetcher/setter for all SNMP versions
     699             : *
     700             : */
     701         254 : static void php_snmp_internal(INTERNAL_FUNCTION_PARAMETERS, int st, 
     702             :                                                         struct snmp_session *session,
     703             :                                                         struct objid_query *objid_query)
     704             : {
     705             :         struct snmp_session *ss;
     706         254 :         struct snmp_pdu *pdu=NULL, *response;
     707             :         struct variable_list *vars;
     708             :         oid root[MAX_NAME_LEN];
     709         254 :         size_t rootlen = 0;
     710             :         int status, count, found;
     711             :         char buf[2048];
     712             :         char buf2[2048];
     713         254 :         int keepwalking=1;
     714             :         char *err;
     715             :         zval snmpval;
     716             :         int snmp_errno;
     717             : 
     718             :         /* we start with retval=FALSE. If any actual data is acquired, retval will be set to appropriate type */
     719         254 :         RETVAL_FALSE;
     720             :         
     721             :         /* reset errno and errstr */
     722         254 :         php_snmp_error(getThis(), NULL TSRMLS_CC, PHP_SNMP_ERRNO_NOERROR, "");
     723             : 
     724         254 :         if (st & SNMP_CMD_WALK) { /* remember root OID */
     725          25 :                 memmove((char *)root, (char *)(objid_query->vars[0].name), (objid_query->vars[0].name_length) * sizeof(oid));
     726          25 :                 rootlen = objid_query->vars[0].name_length;
     727          25 :                 objid_query->offset = objid_query->count;
     728             :         }
     729             : 
     730         254 :         if ((ss = snmp_open(session)) == NULL) {
     731           0 :                 snmp_error(session, NULL, NULL, &err);
     732           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not open snmp connection: %s", err);
     733           0 :                 free(err);
     734           0 :                 RETVAL_FALSE;
     735           0 :                 return;
     736             :         }
     737             : 
     738         254 :         if ((st & SNMP_CMD_SET) && objid_query->count > objid_query->step) {
     739           4 :                 php_snmp_error(getThis(), NULL TSRMLS_CC, PHP_SNMP_ERRNO_MULTIPLE_SET_QUERIES, "Can not fit all OIDs for SET query into one packet, using multiple queries");
     740             :         }
     741             : 
     742       13867 :         while (keepwalking) {
     743       13412 :                 keepwalking = 0;
     744       13412 :                 if (st & SNMP_CMD_WALK) {
     745       13176 :                         if (session->version == SNMP_VERSION_1) {
     746       12724 :                                 pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
     747             :                         } else {
     748         452 :                                 pdu = snmp_pdu_create(SNMP_MSG_GETBULK);
     749         452 :                                 pdu->non_repeaters = objid_query->non_repeaters;
     750         452 :                                 pdu->max_repetitions = objid_query->max_repetitions;
     751             :                         }
     752       13176 :                         snmp_add_null_var(pdu, objid_query->vars[0].name, objid_query->vars[0].name_length);
     753             :                 } else {
     754         236 :                         if (st & SNMP_CMD_GET) {
     755         177 :                                 pdu = snmp_pdu_create(SNMP_MSG_GET);
     756          59 :                         } else if (st & SNMP_CMD_GETNEXT) {
     757          12 :                                 pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
     758          47 :                         } else if (st & SNMP_CMD_SET) {
     759          47 :                                 pdu = snmp_pdu_create(SNMP_MSG_SET);
     760             :                         } else {
     761           0 :                                 snmp_close(ss);
     762           0 :                                 php_error_docref(NULL TSRMLS_CC, E_ERROR, "Unknown SNMP command (internals)");
     763           0 :                                 RETVAL_FALSE;
     764           0 :                                 return;
     765             :                         }
     766         492 :                         for (count = 0; objid_query->offset < objid_query->count && count < objid_query->step; objid_query->offset++, count++){
     767         263 :                                 if (st & SNMP_CMD_SET) {
     768          65 :                                         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))) {
     769           7 :                                                 snprint_objid(buf, sizeof(buf), objid_query->vars[objid_query->offset].name, objid_query->vars[objid_query->offset].name_length);
     770           7 :                                                 php_snmp_error(getThis(), NULL TSRMLS_CC, PHP_SNMP_ERRNO_OID_PARSING_ERROR, "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));
     771           7 :                                                 snmp_free_pdu(pdu);
     772           7 :                                                 snmp_close(ss);
     773           7 :                                                 RETVAL_FALSE;
     774           7 :                                                 return;
     775             :                                         }
     776             :                                 } else {
     777         198 :                                         snmp_add_null_var(pdu, objid_query->vars[objid_query->offset].name, objid_query->vars[objid_query->offset].name_length);
     778             :                                 }
     779             :                         }
     780         229 :                         if(pdu->variables == NULL){
     781           0 :                                 snmp_free_pdu(pdu);
     782           0 :                                 snmp_close(ss);
     783           0 :                                 RETVAL_FALSE;
     784           0 :                                 return;
     785             :                         }
     786             :                 }
     787             : 
     788             : retry:
     789       13406 :                 status = snmp_synch_response(ss, pdu, &response);
     790       13406 :                 if (status == STAT_SUCCESS) {
     791       13400 :                         if (response->errstat == SNMP_ERR_NOERROR) {
     792       13387 :                                 if (st & SNMP_CMD_SET) {
     793          32 :                                         if (objid_query->offset < objid_query->count) { /* we have unprocessed OIDs */
     794           4 :                                                 keepwalking = 1;
     795           4 :                                                 continue;
     796             :                                         }
     797          28 :                                         snmp_free_pdu(response);
     798          28 :                                         snmp_close(ss);
     799          28 :                                         RETVAL_TRUE;
     800          28 :                                         return;
     801             :                                 }
     802       34913 :                                 for (vars = response->variables; vars; vars = vars->next_variable) {
     803             :                                         /* do not output errors as values */
     804       65218 :                                         if (    vars->type == SNMP_ENDOFMIBVIEW || 
     805       21738 :                                                 vars->type == SNMP_NOSUCHOBJECT || 
     806       21738 :                                                 vars->type == SNMP_NOSUCHINSTANCE ) {
     807          12 :                                                 if ((st & SNMP_CMD_WALK) && Z_TYPE_P(return_value) == IS_ARRAY) {
     808           2 :                                                         break;
     809             :                                                 }
     810           6 :                                                 snprint_objid(buf, sizeof(buf), vars->name, vars->name_length);
     811           6 :                                                 snprint_value(buf2, sizeof(buf2), vars->name, vars->name_length, vars);
     812           6 :                                                 php_snmp_error(getThis(), NULL TSRMLS_CC, PHP_SNMP_ERRNO_ERROR_IN_REPLY, "Error in packet at '%s': %s", buf, buf2);
     813           6 :                                                 continue;
     814             :                                         }
     815             :                                         
     816       64832 :                                         if ((st & SNMP_CMD_WALK) && 
     817       43098 :                                                 (vars->name_length < rootlen || memcmp(root, vars->name, rootlen * sizeof(oid)))) { /* not part of this subtree */
     818          20 :                                                 if (Z_TYPE_P(return_value) == IS_ARRAY) { /* some records are fetched already, shut down further lookup */
     819          17 :                                                         keepwalking = 0;
     820             :                                                 } else {
     821             :                                                         /* first fetched OID is out of subtree, fallback to GET query */
     822           3 :                                                         st |= SNMP_CMD_GET;
     823           3 :                                                         st ^= SNMP_CMD_WALK;
     824           3 :                                                         objid_query->offset = 0;
     825           3 :                                                         keepwalking = 1;
     826             :                                                 }
     827          20 :                                                 break;
     828             :                                         }
     829             : 
     830       21714 :                                         ZVAL_NULL(&snmpval);
     831       21714 :                                         php_snmp_getvalue(vars, &snmpval TSRMLS_CC, objid_query->valueretrieval);
     832             : 
     833       21714 :                                         if (objid_query->array_output) {
     834       43104 :                                                 if (Z_TYPE_P(return_value) == IS_TRUE || Z_TYPE_P(return_value) == IS_FALSE) {
     835          34 :                                                         array_init(return_value);
     836             :                                                 }
     837       21552 :                                                 if (st & SNMP_NUMERIC_KEYS) {
     838       21220 :                                                         add_next_index_zval(return_value, &snmpval);
     839         334 :                                                 } else if (st & SNMP_ORIGINAL_NAMES_AS_KEYS && st & SNMP_CMD_GET) {
     840           2 :                                                         found = 0;
     841           3 :                                                         for (count = 0; count < objid_query->count; count++) {
     842           3 :                                                                 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) {
     843           2 :                                                                         found = 1;
     844           2 :                                                                         objid_query->vars[count].name_length = 0; /* mark this name as used */
     845           2 :                                                                         break;
     846             :                                                                 }
     847             :                                                         }
     848           2 :                                                         if (found) {
     849           2 :                                                                 add_assoc_zval(return_value, objid_query->vars[count].oid, &snmpval);
     850             :                                                         } else {
     851           0 :                                                                 snprint_objid(buf2, sizeof(buf2), vars->name, vars->name_length);
     852           0 :                                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not find original OID name for '%s'", buf2);
     853             :                                                         }
     854         361 :                                                 } else if (st & SNMP_USE_SUFFIX_AS_KEYS && st & SNMP_CMD_WALK) {
     855          31 :                                                         snprint_objid(buf2, sizeof(buf2), vars->name, vars->name_length);
     856          31 :                                                         if (rootlen <= vars->name_length && snmp_oid_compare(root, rootlen, vars->name, rootlen) == 0) {
     857          31 :                                                                 buf2[0] = '\0';
     858          31 :                                                                 count = rootlen;
     859         172 :                                                                 while(count < vars->name_length){
     860         110 :                                                                         sprintf(buf, "%lu.", vars->name[count]);
     861         110 :                                                                         strcat(buf2, buf);
     862         110 :                                                                         count++;
     863             :                                                                 }
     864          31 :                                                                 buf2[strlen(buf2) - 1] = '\0'; /* remove trailing '.' */
     865             :                                                         }
     866          31 :                                                         add_assoc_zval(return_value, buf2, &snmpval);
     867             :                                                 } else {
     868         299 :                                                         snprint_objid(buf2, sizeof(buf2), vars->name, vars->name_length);
     869         299 :                                                         add_assoc_zval(return_value, buf2, &snmpval);
     870             :                                                 }
     871             :                                         } else {
     872         162 :                                                 ZVAL_COPY_VALUE(return_value, &snmpval);
     873         162 :                                                 break;
     874             :                                         }
     875             : 
     876             :                                         /* OID increase check */
     877       21552 :                                         if (st & SNMP_CMD_WALK) {
     878       21529 :                                                 if (objid_query->oid_increasing_check == TRUE && snmp_oid_compare(objid_query->vars[0].name, objid_query->vars[0].name_length, vars->name, vars->name_length) >= 0) {
     879           0 :                                                         snprint_objid(buf2, sizeof(buf2), vars->name, vars->name_length);
     880           0 :                                                         php_snmp_error(getThis(), NULL TSRMLS_CC, PHP_SNMP_ERRNO_OID_NOT_INCREASING, "Error: OID not increasing: %s", buf2);
     881           0 :                                                         keepwalking = 0;
     882             :                                                 } else {
     883       21529 :                                                         memmove((char *)(objid_query->vars[0].name), (char *)vars->name, vars->name_length * sizeof(oid));
     884       21529 :                                                         objid_query->vars[0].name_length = vars->name_length;
     885       21529 :                                                         keepwalking = 1;
     886             :                                                 }
     887             :                                         }
     888             :                                 }
     889       13355 :                                 if (objid_query->offset < objid_query->count) { /* we have unprocessed OIDs */
     890           3 :                                         keepwalking = 1;
     891             :                                 }
     892             :                         } else {
     893          13 :                                 if (st & SNMP_CMD_WALK && response->errstat == SNMP_ERR_TOOBIG && objid_query->max_repetitions > 1) { /* Answer will not fit into single packet */
     894           0 :                                         objid_query->max_repetitions /= 2;
     895           0 :                                         snmp_free_pdu(response);
     896           0 :                                         keepwalking = 1;
     897           0 :                                         continue;
     898             :                                 }
     899          17 :                                 if (!(st & SNMP_CMD_WALK) || response->errstat != SNMP_ERR_NOSUCHNAME || Z_TYPE_P(return_value) == IS_TRUE || Z_TYPE_P(return_value) == IS_FALSE) {
     900          45 :                                         for (count=1, vars = response->variables;
     901          16 :                                                 vars && count != response->errindex;
     902           3 :                                                 vars = vars->next_variable, count++);
     903             : 
     904          13 :                                         if (st & (SNMP_CMD_GET | SNMP_CMD_GETNEXT) && response->errstat == SNMP_ERR_TOOBIG && objid_query->step > 1) { /* Answer will not fit into single packet */
     905           0 :                                                 objid_query->offset = ((objid_query->offset > objid_query->step) ? (objid_query->offset - objid_query->step) : 0 );
     906           0 :                                                 objid_query->step /= 2;
     907           0 :                                                 snmp_free_pdu(response);
     908           0 :                                                 keepwalking = 1;
     909           0 :                                                 continue;
     910             :                                         }
     911          13 :                                         if (vars) {
     912          13 :                                                 snprint_objid(buf, sizeof(buf), vars->name, vars->name_length);
     913          13 :                                                 php_snmp_error(getThis(), NULL TSRMLS_CC, PHP_SNMP_ERRNO_ERROR_IN_REPLY, "Error in packet at '%s': %s", buf, snmp_errstring(response->errstat));
     914             :                                         } else {
     915           0 :                                                 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));
     916             :                                         }
     917          13 :                                         if (st & (SNMP_CMD_GET | SNMP_CMD_GETNEXT)) { /* cut out bogus OID and retry */
     918           3 :                                                 if ((pdu = snmp_fix_pdu(response, ((st & SNMP_CMD_GET) ? SNMP_MSG_GET : SNMP_MSG_GETNEXT) )) != NULL) {
     919           1 :                                                         snmp_free_pdu(response);
     920           1 :                                                         goto retry;
     921             :                                                 }
     922             :                                         }
     923          12 :                                         snmp_free_pdu(response);
     924          12 :                                         snmp_close(ss);
     925          12 :                                         if (objid_query->array_output) {
     926           3 :                                                 zval_ptr_dtor(return_value);
     927             :                                         }
     928          12 :                                         RETVAL_FALSE;
     929          12 :                                         return;
     930             :                                 }
     931             :                         }
     932           6 :                 } else if (status == STAT_TIMEOUT) {
     933           3 :                         php_snmp_error(getThis(), NULL TSRMLS_CC, PHP_SNMP_ERRNO_TIMEOUT, "No response from %s", session->peername);
     934           3 :                         if (objid_query->array_output) {
     935           1 :                                 zval_ptr_dtor(return_value);
     936             :                         }
     937           3 :                         snmp_close(ss);
     938           3 :                         RETVAL_FALSE;
     939           3 :                         return;
     940             :                 } else {    /* status == STAT_ERROR */
     941           3 :                         snmp_error(ss, NULL, NULL, &err);
     942           3 :                         php_snmp_error(getThis(), NULL TSRMLS_CC, PHP_SNMP_ERRNO_GENERIC, "Fatal error: %s", err);
     943           3 :                         free(err);
     944           3 :                         if (objid_query->array_output) {
     945           1 :                                 zval_ptr_dtor(return_value);
     946             :                         }
     947           3 :                         snmp_close(ss);
     948           3 :                         RETVAL_FALSE;
     949           3 :                         return;
     950             :                 }
     951       13355 :                 if (response) {
     952       13355 :                         snmp_free_pdu(response);
     953             :                 }
     954             :         } /* keepwalking */
     955         201 :         snmp_close(ss);
     956             : }
     957             : /* }}} */
     958             : 
     959             : /* {{{ php_snmp_parse_oid
     960             : *
     961             : * OID parser (and type, value for SNMP_SET command)
     962             : */
     963             : 
     964         309 : static int php_snmp_parse_oid(zval *object, int st, struct objid_query *objid_query, zval *oid, zval *type, zval *value TSRMLS_DC)
     965             : {
     966             :         char *pptr;
     967             :         HashPosition pos_oid, pos_type, pos_value;
     968             :         zval *tmp_oid, *tmp_type, *tmp_value;
     969             : 
     970         309 :         if (Z_TYPE_P(oid) != IS_ARRAY) {
     971             :                 /*
     972             :                 if (Z_ISREF_PP(oid)) {
     973             :                         SEPARATE_ZVAL(oid);
     974             :                 }
     975             :                 */
     976         234 :                 convert_to_string_ex(oid);
     977          75 :         } else if (Z_TYPE_P(oid) == IS_ARRAY) {
     978          75 :                 zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(oid), &pos_oid);
     979             :         }
     980             : 
     981         309 :         if (st & SNMP_CMD_SET) {
     982          61 :                 if (Z_TYPE_P(type) != IS_ARRAY) {
     983             :                         /*
     984             :                         if (Z_ISREF_PP(type)) {
     985             :                                 SEPARATE_ZVAL(type);
     986             :                         }
     987             :                         */
     988          36 :                         convert_to_string_ex(type);
     989          27 :                 } else if (Z_TYPE_P(type) == IS_ARRAY) {
     990          27 :                         zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(type), &pos_type);
     991             :                 }
     992             : 
     993          61 :                 if (Z_TYPE_P(value) != IS_ARRAY) {
     994             :                         /*
     995             :                         if (Z_ISREF_PP(value)) {
     996             :                                 SEPARATE_ZVAL(value);
     997             :                         }
     998             :                         */
     999          33 :                         convert_to_string_ex(value);
    1000          32 :                 } else if (Z_TYPE_P(value) == IS_ARRAY) {
    1001          32 :                         zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(value), &pos_value);
    1002             :                 }
    1003             :         }
    1004             : 
    1005         309 :         objid_query->count = 0;
    1006         309 :         objid_query->array_output = ((st & SNMP_CMD_WALK) ? TRUE : FALSE);
    1007         309 :         if (Z_TYPE_P(oid) == IS_STRING) {
    1008         234 :                 objid_query->vars = (snmpobjarg *)emalloc(sizeof(snmpobjarg));
    1009         234 :                 if (objid_query->vars == NULL) {
    1010           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "emalloc() failed while parsing oid: %s", strerror(errno));
    1011           0 :                         efree(objid_query->vars);
    1012           0 :                         return FALSE;
    1013             :                 }
    1014         234 :                 objid_query->vars[objid_query->count].oid = Z_STRVAL_P(oid);
    1015         234 :                 if (st & SNMP_CMD_SET) {
    1016          77 :                         if (Z_TYPE_P(type) == IS_STRING && Z_TYPE_P(value) == IS_STRING) {
    1017          24 :                                 if (Z_STRLEN_P(type) != 1) {
    1018           2 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bogus type '%s', should be single char, got %u", Z_STRVAL_P(type), Z_STRLEN_P(type));
    1019           2 :                                         efree(objid_query->vars);
    1020           2 :                                         return FALSE;
    1021             :                                 }
    1022          22 :                                 pptr = Z_STRVAL_P(type);
    1023          22 :                                 objid_query->vars[objid_query->count].type = *pptr;
    1024          22 :                                 objid_query->vars[objid_query->count].value = Z_STRVAL_P(value);
    1025             :                         } else {
    1026           5 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Single objid and multiple type or values are not supported");
    1027           5 :                                 efree(objid_query->vars);
    1028           5 :                                 return FALSE;
    1029             :                         }
    1030             :                 }
    1031         227 :                 objid_query->count++;
    1032          75 :         } else if (Z_TYPE_P(oid) == IS_ARRAY) { /* we got objid array */
    1033          75 :                 if (zend_hash_num_elements(Z_ARRVAL_P(oid)) == 0) {
    1034           1 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Got empty OID array");
    1035           1 :                         return FALSE;
    1036             :                 }
    1037          74 :                 objid_query->vars = (snmpobjarg *)emalloc(sizeof(snmpobjarg) * zend_hash_num_elements(Z_ARRVAL_P(oid)));
    1038          74 :                 if (objid_query->vars == NULL) {
    1039           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "emalloc() failed while parsing oid array: %s", strerror(errno));
    1040           0 :                         efree(objid_query->vars);
    1041           0 :                         return FALSE;
    1042             :                 }
    1043          74 :                 objid_query->array_output = ( (st & SNMP_CMD_SET) ? FALSE : TRUE );
    1044         268 :                 for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(oid), &pos_oid);
    1045         194 :                         (tmp_oid = zend_hash_get_current_data_ex(Z_ARRVAL_P(oid), &pos_oid)) != NULL;
    1046         120 :                         zend_hash_move_forward_ex(Z_ARRVAL_P(oid), &pos_oid) ) {
    1047             : 
    1048         128 :                         convert_to_string_ex(tmp_oid);
    1049         128 :                         objid_query->vars[objid_query->count].oid = Z_STRVAL_P(tmp_oid);
    1050         128 :                         if (st & SNMP_CMD_SET) {
    1051          62 :                                 if (Z_TYPE_P(type) == IS_STRING) {
    1052          16 :                                         pptr = Z_STRVAL_P(type);
    1053          16 :                                         objid_query->vars[objid_query->count].type = *pptr;
    1054          46 :                                 } else if (Z_TYPE_P(type) == IS_ARRAY) {
    1055          46 :                                         if ((tmp_type = zend_hash_get_current_data_ex(Z_ARRVAL_P(type), &pos_type)) != NULL) {
    1056          44 :                                                 convert_to_string_ex(tmp_type);
    1057          44 :                                                 if (Z_STRLEN_P(tmp_type) != 1) {
    1058           4 :                                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s': bogus type '%s', should be single char, got %u", Z_STRVAL_P(tmp_oid), Z_STRVAL_P(tmp_type), Z_STRLEN_P(tmp_type));
    1059           4 :                                                         efree(objid_query->vars);
    1060           4 :                                                         return FALSE;
    1061             :                                                 }
    1062          40 :                                                 pptr = Z_STRVAL_P(tmp_type);
    1063          40 :                                                 objid_query->vars[objid_query->count].type = *pptr;
    1064          40 :                                                 zend_hash_move_forward_ex(Z_ARRVAL_P(type), &pos_type);
    1065             :                                         } else {
    1066           2 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s': no type set", Z_STRVAL_P(tmp_oid));
    1067           2 :                                                 efree(objid_query->vars);
    1068           2 :                                                 return FALSE;
    1069             :                                         }
    1070             :                                 }
    1071             : 
    1072          56 :                                 if (Z_TYPE_P(value) == IS_STRING) {
    1073           4 :                                         objid_query->vars[objid_query->count].value = Z_STRVAL_P(value);
    1074          52 :                                 } else if (Z_TYPE_P(value) == IS_ARRAY) {
    1075          52 :                                         if ((tmp_value = zend_hash_get_current_data_ex(Z_ARRVAL_P(value), &pos_value)) != NULL) {
    1076          50 :                                                 convert_to_string_ex(tmp_value);
    1077          50 :                                                 objid_query->vars[objid_query->count].value = Z_STRVAL_P(tmp_value);
    1078          50 :                                                 zend_hash_move_forward_ex(Z_ARRVAL_P(value), &pos_value);
    1079             :                                         } else {
    1080           2 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s': no value set", Z_STRVAL_P(tmp_oid));
    1081           2 :                                                 efree(objid_query->vars);
    1082           2 :                                                 return FALSE;
    1083             :                                         }
    1084             :                                 }
    1085             :                         }
    1086         120 :                         objid_query->count++;
    1087             :                 }
    1088             :         }
    1089             : 
    1090             :         /* now parse all OIDs */
    1091         293 :         if (st & SNMP_CMD_WALK) {
    1092          40 :                 if (objid_query->count > 1) {
    1093          12 :                         php_snmp_error(object, NULL TSRMLS_CC, PHP_SNMP_ERRNO_OID_PARSING_ERROR, "Multi OID walks are not supported!");
    1094          12 :                         efree(objid_query->vars);
    1095          12 :                         return FALSE;
    1096             :                 }
    1097          28 :                 objid_query->vars[0].name_length = MAX_NAME_LEN;
    1098          28 :                 if (strlen(objid_query->vars[0].oid)) { /* on a walk, an empty string means top of tree - no error */
    1099          26 :                         if (!snmp_parse_oid(objid_query->vars[0].oid, objid_query->vars[0].name, &(objid_query->vars[0].name_length))) {
    1100           3 :                                 php_snmp_error(object, NULL TSRMLS_CC, PHP_SNMP_ERRNO_OID_PARSING_ERROR, "Invalid object identifier: %s", objid_query->vars[0].oid);
    1101           3 :                                 efree(objid_query->vars);
    1102           3 :                                 return FALSE;
    1103             :                         }
    1104             :                 } else { 
    1105           2 :                         memmove((char *)objid_query->vars[0].name, (char *)objid_mib, sizeof(objid_mib));
    1106           2 :                         objid_query->vars[0].name_length = sizeof(objid_mib) / sizeof(oid);
    1107             :                 }
    1108             :         } else {
    1109         527 :                 for (objid_query->offset = 0; objid_query->offset < objid_query->count; objid_query->offset++) {
    1110         287 :                         objid_query->vars[objid_query->offset].name_length = MAX_OID_LEN;
    1111         287 :                         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))) {
    1112          13 :                                 php_snmp_error(object, NULL TSRMLS_CC, PHP_SNMP_ERRNO_OID_PARSING_ERROR, "Invalid object identifier: %s", objid_query->vars[objid_query->offset].oid);
    1113          13 :                                 efree(objid_query->vars);
    1114          13 :                                 return FALSE;
    1115             :                         }
    1116             :                 }
    1117             :         }
    1118         265 :         objid_query->offset = 0;
    1119         265 :         objid_query->step = objid_query->count;
    1120         265 :         return (objid_query->count > 0);
    1121             : }
    1122             : /* }}} */
    1123             : 
    1124             : /* {{{ netsnmp_session_init
    1125             :         allocates memory for session and session->peername, caller should free it manually using netsnmp_session_free() and efree()
    1126             : */
    1127         248 : static int netsnmp_session_init(php_snmp_session **session_p, int version, char *hostname, char *community, int timeout, int retries TSRMLS_DC)
    1128             : {
    1129             :         php_snmp_session *session;
    1130             :         char *pptr, *host_ptr;
    1131         248 :         int force_ipv6 = FALSE;
    1132             :         int n;
    1133             :         struct sockaddr **psal;
    1134             :         struct sockaddr **res;
    1135             : 
    1136         248 :         *session_p = (php_snmp_session *)emalloc(sizeof(php_snmp_session));
    1137         248 :         session = *session_p;
    1138         248 :         if (session == NULL) {
    1139           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "emalloc() failed allocating session");
    1140           0 :                 return (-1);
    1141             :         }
    1142         248 :         memset(session, 0, sizeof(php_snmp_session));
    1143             : 
    1144         248 :         snmp_sess_init(session);
    1145             : 
    1146         248 :         session->version = version;
    1147         248 :         session->remote_port = SNMP_PORT;
    1148             : 
    1149         248 :         session->peername = emalloc(MAX_NAME_LEN);
    1150         248 :         if (session->peername == NULL) {
    1151           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "emalloc() failed while copying hostname");
    1152           0 :                 return (-1);
    1153             :         }
    1154             :         /* we copy original hostname for further processing */
    1155         248 :         strlcpy(session->peername, hostname, MAX_NAME_LEN);
    1156         248 :         host_ptr = session->peername;
    1157             : 
    1158             :         /* Reading the hostname and its optional non-default port number */
    1159         248 :         if (*host_ptr == '[') { /* IPv6 address */
    1160           5 :                 force_ipv6 = TRUE;
    1161           5 :                 host_ptr++;
    1162           5 :                 if ((pptr = strchr(host_ptr, ']'))) {
    1163           4 :                         if (pptr[1] == ':') {
    1164           4 :                                 session->remote_port = atoi(pptr + 2);
    1165             :                         }
    1166           4 :                         *pptr = '\0';
    1167             :                 } else {
    1168           1 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "malformed IPv6 address, closing square bracket missing");
    1169           1 :                         return (-1);
    1170             :                 }
    1171             :         } else { /* IPv4 address */
    1172         243 :                 if ((pptr = strchr(host_ptr, ':'))) {
    1173         242 :                         session->remote_port = atoi(pptr + 1);
    1174         242 :                         *pptr = '\0';
    1175             :                 }
    1176             :         }
    1177             : 
    1178             :         /* since Net-SNMP library requires 'udp6:' prefix for all IPv6 addresses (in FQDN form too) we need to
    1179             :            perform possible name resolution before running any SNMP queries */
    1180         247 :         if ((n = php_network_getaddresses(host_ptr, SOCK_DGRAM, &psal, NULL TSRMLS_CC)) == 0) { /* some resolver error */
    1181             :                 /* warnings sent, bailing out */
    1182           1 :                 return (-1);
    1183             :         }
    1184             : 
    1185             :         /* we have everything we need in psal, flush peername and fill it properly */
    1186         246 :         *(session->peername) = '\0';
    1187         246 :         res = psal;
    1188         492 :         while (n-- > 0) {
    1189         246 :                 pptr = session->peername;
    1190             : #if HAVE_GETADDRINFO && HAVE_IPV6 && HAVE_INET_NTOP
    1191         246 :                 if (force_ipv6 && (*res)->sa_family != AF_INET6) {
    1192           0 :                         res++;
    1193           0 :                         continue;
    1194             :                 }
    1195         246 :                 if ((*res)->sa_family == AF_INET6) {
    1196           4 :                         strcpy(session->peername, "udp6:[");
    1197           4 :                         pptr = session->peername + strlen(session->peername);
    1198           4 :                         inet_ntop((*res)->sa_family, &(((struct sockaddr_in6*)(*res))->sin6_addr), pptr, MAX_NAME_LEN);
    1199           4 :                         strcat(pptr, "]");
    1200         242 :                 } else if ((*res)->sa_family == AF_INET) {
    1201         242 :                         inet_ntop((*res)->sa_family, &(((struct sockaddr_in*)(*res))->sin_addr), pptr, MAX_NAME_LEN);
    1202             :                 } else {
    1203           0 :                         res++;
    1204           0 :                         continue;
    1205             :                 }
    1206             : #else
    1207             :                 if ((*res)->sa_family != AF_INET) {
    1208             :                         res++;
    1209             :                         continue;
    1210             :                 }
    1211             :                 strcat(pptr, inet_ntoa(((struct sockaddr_in*)(*res))->sin_addr));
    1212             : #endif
    1213         246 :                 break;
    1214             :         }
    1215             : 
    1216         246 :         if (strlen(session->peername) == 0) {
    1217           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown failure while resolving '%s'", hostname);
    1218           0 :                 return (-1);
    1219             :         }
    1220             :         /* XXX FIXME
    1221             :                 There should be check for non-empty session->peername!
    1222             :         */
    1223             : 
    1224             :         /* put back non-standard SNMP port */
    1225         246 :         if (session->remote_port != SNMP_PORT) {
    1226           1 :                 pptr = session->peername + strlen(session->peername);
    1227           1 :                 sprintf(pptr, ":%d", session->remote_port);
    1228             :         }
    1229             : 
    1230         246 :         php_network_freeaddresses(psal);
    1231             : 
    1232         246 :         if (version == SNMP_VERSION_3) {
    1233             :                 /* Setting the security name. */
    1234          38 :                 session->securityName = estrdup(community);
    1235          38 :                 session->securityNameLen = strlen(session->securityName);
    1236             :         } else {
    1237         208 :                 session->authenticator = NULL;
    1238         208 :                 session->community = (u_char *)estrdup(community);
    1239         208 :                 session->community_len = strlen(community);
    1240             :         }
    1241             : 
    1242         246 :         session->retries = retries;
    1243         246 :         session->timeout = timeout;
    1244         246 :         return (0);
    1245             : }
    1246             : /* }}} */
    1247             : 
    1248             : /* {{{ int netsnmp_session_set_sec_level(struct snmp_session *s, char *level)
    1249             :    Set the security level in the snmpv3 session */
    1250          44 : static int netsnmp_session_set_sec_level(struct snmp_session *s, char *level)
    1251             : {
    1252          47 :         if (!strcasecmp(level, "noAuthNoPriv") || !strcasecmp(level, "nanp")) {
    1253           3 :                 s->securityLevel = SNMP_SEC_LEVEL_NOAUTH;
    1254          55 :         } else if (!strcasecmp(level, "authNoPriv") || !strcasecmp(level, "anp")) {
    1255          14 :                 s->securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;
    1256          50 :         } else if (!strcasecmp(level, "authPriv") || !strcasecmp(level, "ap")) {
    1257          23 :                 s->securityLevel = SNMP_SEC_LEVEL_AUTHPRIV;
    1258             :         } else {
    1259           4 :                 return (-1);
    1260             :         }
    1261          40 :         return (0);
    1262             : }
    1263             : /* }}} */
    1264             : 
    1265             : /* {{{ int netsnmp_session_set_auth_protocol(struct snmp_session *s, char *prot)
    1266             :    Set the authentication protocol in the snmpv3 session */
    1267          37 : static int netsnmp_session_set_auth_protocol(struct snmp_session *s, char *prot TSRMLS_DC)
    1268             : {
    1269          37 :         if (!strcasecmp(prot, "MD5")) {
    1270          34 :                 s->securityAuthProto = usmHMACMD5AuthProtocol;
    1271          34 :                 s->securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN;
    1272           3 :         } else if (!strcasecmp(prot, "SHA")) {
    1273           1 :                 s->securityAuthProto = usmHMACSHA1AuthProtocol;
    1274           1 :                 s->securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN;
    1275             :         } else {
    1276           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown authentication protocol '%s'", prot);
    1277           2 :                 return (-1);
    1278             :         }
    1279          35 :         return (0);
    1280             : }
    1281             : /* }}} */
    1282             : 
    1283             : /* {{{ int netsnmp_session_set_sec_protocol(struct snmp_session *s, char *prot)
    1284             :    Set the security protocol in the snmpv3 session */
    1285          23 : static int netsnmp_session_set_sec_protocol(struct snmp_session *s, char *prot TSRMLS_DC)
    1286             : {
    1287          23 :         if (!strcasecmp(prot, "DES")) {
    1288           1 :                 s->securityPrivProto = usmDESPrivProtocol;
    1289           1 :                 s->securityPrivProtoLen = USM_PRIV_PROTO_DES_LEN;
    1290             : #ifdef HAVE_AES
    1291          42 :         } else if (!strcasecmp(prot, "AES128") || !strcasecmp(prot, "AES")) {
    1292          20 :                 s->securityPrivProto = usmAESPrivProtocol;
    1293          20 :                 s->securityPrivProtoLen = USM_PRIV_PROTO_AES_LEN;
    1294             : #endif
    1295             :         } else {
    1296           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown security protocol '%s'", prot);
    1297           2 :                 return (-1);
    1298             :         }
    1299          21 :         return (0);
    1300             : }
    1301             : /* }}} */
    1302             : 
    1303             : /* {{{ int netsnmp_session_gen_auth_key(struct snmp_session *s, char *pass)
    1304             :    Make key from pass phrase in the snmpv3 session */
    1305          35 : static int netsnmp_session_gen_auth_key(struct snmp_session *s, char *pass TSRMLS_DC)
    1306             : {
    1307             :         int snmp_errno;
    1308          35 :         s->securityAuthKeyLen = USM_AUTH_KU_LEN;
    1309          35 :         if ((snmp_errno = generate_Ku(s->securityAuthProto, s->securityAuthProtoLen,
    1310             :                         (u_char *) pass, strlen(pass),
    1311             :                         s->securityAuthKey, &(s->securityAuthKeyLen)))) {
    1312           4 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error generating a key for authentication pass phrase '%s': %s", pass, snmp_api_errstring(snmp_errno));
    1313           4 :                 return (-1);
    1314             :         }
    1315          31 :         return (0);
    1316             : }
    1317             : /* }}} */
    1318             : 
    1319             : /* {{{ int netsnmp_session_gen_sec_key(struct snmp_session *s, u_char *pass)
    1320             :    Make key from pass phrase in the snmpv3 session */
    1321          21 : static int netsnmp_session_gen_sec_key(struct snmp_session *s, char *pass TSRMLS_DC)
    1322             : {
    1323             :         int snmp_errno;
    1324             : 
    1325          21 :         s->securityPrivKeyLen = USM_PRIV_KU_LEN;
    1326          21 :         if ((snmp_errno = generate_Ku(s->securityAuthProto, s->securityAuthProtoLen,
    1327             :                         (u_char *)pass, strlen(pass),
    1328             :                         s->securityPrivKey, &(s->securityPrivKeyLen)))) {
    1329           4 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error generating a key for privacy pass phrase '%s': %s", pass, snmp_api_errstring(snmp_errno));
    1330           4 :                 return (-2);
    1331             :         }
    1332          17 :         return (0);
    1333             : }
    1334             : /* }}} */
    1335             : 
    1336             : /* {{{ in netsnmp_session_set_contextEngineID(struct snmp_session *s, u_char * contextEngineID)
    1337             :    Set context Engine Id in the snmpv3 session */
    1338           2 : static int netsnmp_session_set_contextEngineID(struct snmp_session *s, char * contextEngineID TSRMLS_DC)
    1339             : {
    1340           2 :         size_t  ebuf_len = 32, eout_len = 0;
    1341           2 :         u_char  *ebuf = (u_char *) emalloc(ebuf_len);
    1342             : 
    1343           2 :         if (ebuf == NULL) {
    1344           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "malloc failure setting contextEngineID");
    1345           0 :                 return (-1);
    1346             :         }
    1347           2 :         if (!snmp_hex_to_binary(&ebuf, &ebuf_len, &eout_len, 1, contextEngineID)) {
    1348           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad engine ID value '%s'", contextEngineID);
    1349           1 :                 efree(ebuf);
    1350           1 :                 return (-1);
    1351             :         }
    1352             : 
    1353           1 :         if (s->contextEngineID) {
    1354           0 :                 efree(s->contextEngineID);
    1355             :         }
    1356             : 
    1357           1 :         s->contextEngineID = ebuf;
    1358           1 :         s->contextEngineIDLen = eout_len;
    1359           1 :         return (0);
    1360             : }
    1361             : /* }}} */
    1362             : 
    1363             : /* {{{ 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)
    1364             :    Set all snmpv3-related security options */
    1365          44 : 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)
    1366             : {
    1367             : 
    1368             :         /* Setting the security level. */
    1369          44 :         if (netsnmp_session_set_sec_level(session, sec_level)) {
    1370           4 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid security level '%s'", sec_level);
    1371           4 :                 return (-1);
    1372             :         }
    1373             : 
    1374          40 :         if (session->securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || session->securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
    1375             : 
    1376             :                 /* Setting the authentication protocol. */
    1377          37 :                 if (netsnmp_session_set_auth_protocol(session, auth_protocol TSRMLS_CC)) {
    1378             :                         /* Warning message sent already, just bail out */
    1379           2 :                         return (-1);
    1380             :                 }
    1381             : 
    1382             :                 /* Setting the authentication passphrase. */
    1383          35 :                 if (netsnmp_session_gen_auth_key(session, auth_passphrase TSRMLS_CC)) {
    1384             :                         /* Warning message sent already, just bail out */
    1385           4 :                         return (-1);
    1386             :                 }
    1387             : 
    1388          31 :                 if (session->securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
    1389             :                         /* Setting the security protocol. */
    1390          23 :                         if (netsnmp_session_set_sec_protocol(session, priv_protocol TSRMLS_CC)) {
    1391             :                                 /* Warning message sent already, just bail out */
    1392           2 :                                 return (-1);
    1393             :                         }
    1394             : 
    1395             :                         /* Setting the security protocol passphrase. */
    1396          21 :                         if (netsnmp_session_gen_sec_key(session, priv_passphrase TSRMLS_CC)) {
    1397             :                                 /* Warning message sent already, just bail out */
    1398           4 :                                 return (-1);
    1399             :                         }
    1400             :                 }
    1401             :         }
    1402             : 
    1403             :         /* Setting contextName if specified */
    1404          28 :         if (contextName) {
    1405          10 :                 session->contextName = contextName;
    1406          10 :                 session->contextNameLen = strlen(contextName);
    1407             :         }
    1408             : 
    1409             :         /* Setting contextEngineIS if specified */
    1410          28 :         if (contextEngineID && strlen(contextEngineID) && netsnmp_session_set_contextEngineID(session, contextEngineID TSRMLS_CC)) {
    1411             :                 /* Warning message sent already, just bail out */
    1412           1 :                 return (-1);
    1413             :         }
    1414             : 
    1415          27 :         return (0);
    1416             : }
    1417             : /* }}} */
    1418             : 
    1419             : /* {{{ php_snmp
    1420             : *
    1421             : * Generic SNMP handler for all versions.
    1422             : * This function makes use of the internal SNMP object fetcher.
    1423             : * Used both in old (non-OO) and OO API
    1424             : *
    1425             : */
    1426         330 : static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
    1427             : {
    1428             :         zval *oid, *value, *type;
    1429             :         char *a1, *a2, *a3, *a4, *a5, *a6, *a7;
    1430             :         size_t a1_len, a2_len, a3_len, a4_len, a5_len, a6_len, a7_len;
    1431         330 :         zend_bool use_orignames = 0, suffix_keys = 0;
    1432         330 :         zend_long timeout = SNMP_DEFAULT_TIMEOUT;
    1433         330 :         zend_long retries = SNMP_DEFAULT_RETRIES;
    1434         330 :         int argc = ZEND_NUM_ARGS();
    1435             :         struct objid_query objid_query;
    1436             :         php_snmp_session *session;
    1437         330 :         int session_less_mode = (getThis() == NULL);
    1438             :         php_snmp_object *snmp_object;
    1439             :         php_snmp_object glob_snmp_object;
    1440             :         
    1441         330 :         objid_query.max_repetitions = -1;
    1442         330 :         objid_query.non_repeaters = 0;
    1443         330 :         objid_query.valueretrieval = SNMP_G(valueretrieval);
    1444         330 :         objid_query.oid_increasing_check = TRUE;
    1445             : 
    1446         330 :         if (session_less_mode) {
    1447         274 :                 if (version == SNMP_VERSION_3) {
    1448          30 :                         if (st & SNMP_CMD_SET) {
    1449           5 :                                 if (zend_parse_parameters(argc TSRMLS_CC, "ssssssszzz|ll", &a1, &a1_len, &a2, &a2_len, &a3, &a3_len,
    1450             :                                         &a4, &a4_len, &a5, &a5_len, &a6, &a6_len, &a7, &a7_len, &oid, &type, &value, &timeout, &retries) == FAILURE) {
    1451           1 :                                         RETURN_FALSE;
    1452             :                                 }
    1453             :                         } else {
    1454             :                                 /* SNMP_CMD_GET
    1455             :                                  * SNMP_CMD_GETNEXT
    1456             :                                  * SNMP_CMD_WALK
    1457             :                                  */
    1458          25 :                                 if (zend_parse_parameters(argc TSRMLS_CC, "sssssssz|ll", &a1, &a1_len, &a2, &a2_len, &a3, &a3_len,
    1459             :                                         &a4, &a4_len, &a5, &a5_len, &a6, &a6_len, &a7, &a7_len, &oid, &timeout, &retries) == FAILURE) {
    1460           1 :                                         RETURN_FALSE;
    1461             :                                 }
    1462             :                         }
    1463             :                 } else {
    1464         244 :                         if (st & SNMP_CMD_SET) {
    1465          52 :                                 if (zend_parse_parameters(argc TSRMLS_CC, "sszzz|ll", &a1, &a1_len, &a2, &a2_len, &oid, &type, &value, &timeout, &retries) == FAILURE) {
    1466           2 :                                         RETURN_FALSE;
    1467             :                                 }
    1468             :                         } else {
    1469             :                                 /* SNMP_CMD_GET
    1470             :                                  * SNMP_CMD_GETNEXT
    1471             :                                  * SNMP_CMD_WALK
    1472             :                                  */
    1473         192 :                                 if (zend_parse_parameters(argc TSRMLS_CC, "ssz|ll", &a1, &a1_len, &a2, &a2_len, &oid, &timeout, &retries) == FAILURE) {
    1474          12 :                                         RETURN_FALSE;
    1475             :                                 }
    1476             :                         }
    1477             :                 }
    1478             :         } else {
    1479          56 :                 if (st & SNMP_CMD_SET) {
    1480           8 :                         if (zend_parse_parameters(argc TSRMLS_CC, "zzz", &oid, &type, &value) == FAILURE) {
    1481           1 :                                 RETURN_FALSE;
    1482             :                         }
    1483          48 :                 } else if (st & SNMP_CMD_WALK) {
    1484           9 :                         if (zend_parse_parameters(argc TSRMLS_CC, "z|bll", &oid, &suffix_keys, &(objid_query.max_repetitions), &(objid_query.non_repeaters)) == FAILURE) {
    1485           2 :                                 RETURN_FALSE;
    1486             :                         }
    1487           7 :                         if (suffix_keys) {
    1488           1 :                                 st |= SNMP_USE_SUFFIX_AS_KEYS;
    1489             :                         }
    1490          39 :                 } else if (st & SNMP_CMD_GET) {
    1491          33 :                         if (zend_parse_parameters(argc TSRMLS_CC, "z|b", &oid, &use_orignames) == FAILURE) {
    1492           1 :                                 RETURN_FALSE;
    1493             :                         }
    1494          32 :                         if (use_orignames) {
    1495           1 :                                 st |= SNMP_ORIGINAL_NAMES_AS_KEYS;
    1496             :                         }
    1497             :                 } else {
    1498             :                         /* SNMP_CMD_GETNEXT
    1499             :                          */
    1500           6 :                         if (zend_parse_parameters(argc TSRMLS_CC, "z", &oid) == FAILURE) {
    1501           1 :                                 RETURN_FALSE;
    1502             :                         }
    1503             :                 }
    1504             :         }
    1505             : 
    1506         309 :         if (!php_snmp_parse_oid(getThis(), st, &objid_query, oid, type, value TSRMLS_CC)) {
    1507          44 :                 RETURN_FALSE;
    1508             :         }
    1509             : 
    1510         265 :         if (session_less_mode) {
    1511         219 :                 if (netsnmp_session_init(&session, version, a1, a2, timeout, retries TSRMLS_CC)) {
    1512           2 :                         efree(objid_query.vars);
    1513           2 :                         netsnmp_session_free(&session);
    1514           2 :                         RETURN_FALSE;
    1515             :                 }
    1516         217 :                 if (version == SNMP_VERSION_3 && netsnmp_session_set_security(session, a3, a4, a5, a6, a7, NULL, NULL TSRMLS_CC)) {
    1517           8 :                         efree(objid_query.vars);
    1518           8 :                         netsnmp_session_free(&session);
    1519             :                         /* Warning message sent already, just bail out */
    1520           8 :                         RETURN_FALSE;
    1521             :                 }
    1522             :         } else {
    1523          46 :                 zval *object = getThis();
    1524          46 :                 snmp_object = Z_SNMP_P(object);
    1525          46 :                 session = snmp_object->session;
    1526          46 :                 if (!session) {
    1527           1 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid or uninitialized SNMP object");
    1528           1 :                         efree(objid_query.vars);
    1529           1 :                         RETURN_FALSE;
    1530             :                 }
    1531             : 
    1532          45 :                 if (snmp_object->max_oids > 0) {
    1533          16 :                         objid_query.step = snmp_object->max_oids;
    1534          16 :                         if (objid_query.max_repetitions < 0) { /* unspecified in function call, use session-wise */
    1535          16 :                                 objid_query.max_repetitions = snmp_object->max_oids;
    1536             :                         }
    1537             :                 }
    1538          45 :                 objid_query.oid_increasing_check = snmp_object->oid_increasing_check;
    1539          45 :                 objid_query.valueretrieval = snmp_object->valueretrieval;
    1540          45 :                 glob_snmp_object.enum_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM);
    1541          45 :                 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, snmp_object->enum_print);
    1542          45 :                 glob_snmp_object.quick_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT);
    1543          45 :                 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, snmp_object->quick_print);
    1544          45 :                 glob_snmp_object.oid_output_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
    1545          45 :                 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, snmp_object->oid_output_format);
    1546             :         }
    1547             : 
    1548         254 :         if (objid_query.max_repetitions < 0) {
    1549         236 :                 objid_query.max_repetitions = 20; /* provide correct default value */
    1550             :         }
    1551             : 
    1552         254 :         php_snmp_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU, st, session, &objid_query);
    1553             :         
    1554         254 :         efree(objid_query.vars);
    1555             : 
    1556         254 :         if (session_less_mode) {
    1557         209 :                 netsnmp_session_free(&session);
    1558             :         } else {
    1559          45 :                 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, glob_snmp_object.enum_print);
    1560          45 :                 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, glob_snmp_object.quick_print);
    1561          45 :                 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, glob_snmp_object.oid_output_format);
    1562             :         }
    1563             : }
    1564             : /* }}} */
    1565             : 
    1566             : /* {{{ proto mixed snmpget(string host, string community, mixed object_id [, int timeout [, int retries]]) 
    1567             :    Fetch a SNMP object */
    1568         137 : PHP_FUNCTION(snmpget)
    1569             : {
    1570         137 :         php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GET, SNMP_VERSION_1);
    1571         137 : }
    1572             : /* }}} */
    1573             : 
    1574             : /* {{{ proto mixed snmpgetnext(string host, string community, mixed object_id [, int timeout [, int retries]]) 
    1575             :    Fetch a SNMP object */
    1576           3 : PHP_FUNCTION(snmpgetnext)
    1577             : {
    1578           3 :         php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GETNEXT, SNMP_VERSION_1);
    1579           3 : }
    1580             : /* }}} */
    1581             : 
    1582             : /* {{{ proto mixed snmpwalk(string host, string community, mixed object_id [, int timeout [, int retries]]) 
    1583             :    Return all objects under the specified object id */
    1584          12 : PHP_FUNCTION(snmpwalk)
    1585             : {
    1586          12 :         php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, (SNMP_CMD_WALK | SNMP_NUMERIC_KEYS), SNMP_VERSION_1);
    1587          12 : }
    1588             : /* }}} */
    1589             : 
    1590             : /* {{{ proto mixed snmprealwalk(string host, string community, mixed object_id [, int timeout [, int retries]])
    1591             :    Return all objects including their respective object id withing the specified one */
    1592           8 : PHP_FUNCTION(snmprealwalk)
    1593             : {
    1594           8 :         php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_WALK, SNMP_VERSION_1);
    1595           8 : }
    1596             : /* }}} */
    1597             : 
    1598             : /* {{{ proto bool snmpset(string host, string community, mixed object_id, mixed type, mixed value [, int timeout [, int retries]]) 
    1599             :    Set the value of a SNMP object */
    1600          26 : PHP_FUNCTION(snmpset)
    1601             : {
    1602          26 :         php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_SET, SNMP_VERSION_1);
    1603          26 : }
    1604             : /* }}} */
    1605             : 
    1606             : /* {{{ proto bool snmp_get_quick_print(void)
    1607             :    Return the current status of quick_print */
    1608           4 : PHP_FUNCTION(snmp_get_quick_print)
    1609             : {
    1610           4 :         if (zend_parse_parameters_none() == FAILURE) {
    1611           1 :                 return;
    1612             :         }
    1613             : 
    1614           3 :         RETURN_BOOL(netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT));
    1615             : }
    1616             : /* }}} */
    1617             : 
    1618             : /* {{{ proto bool snmp_set_quick_print(int quick_print)
    1619             :    Return all objects including their respective object id withing the specified one */
    1620          27 : PHP_FUNCTION(snmp_set_quick_print)
    1621             : {
    1622             :         zend_long a1;
    1623             : 
    1624          27 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &a1) == FAILURE) {
    1625           2 :                 RETURN_FALSE;
    1626             :         }
    1627             : 
    1628          25 :         netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, (int)a1);
    1629          25 :         RETURN_TRUE;
    1630             : }
    1631             : /* }}} */
    1632             : 
    1633             : /* {{{ proto bool snmp_set_enum_print(int enum_print)
    1634             :    Return all values that are enums with their enum value instead of the raw integer */
    1635           6 : PHP_FUNCTION(snmp_set_enum_print)
    1636             : {
    1637             :         zend_long a1;
    1638             : 
    1639           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &a1) == FAILURE) {
    1640           1 :                 RETURN_FALSE;
    1641             :         }
    1642             : 
    1643           5 :         netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, (int) a1);
    1644           5 :         RETURN_TRUE;
    1645             : } 
    1646             : /* }}} */
    1647             : 
    1648             : /* {{{ proto bool snmp_set_oid_output_format(int oid_format)
    1649             :    Set the OID output format. */
    1650           7 : PHP_FUNCTION(snmp_set_oid_output_format)
    1651             : {
    1652             :         zend_long a1;
    1653             : 
    1654           7 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &a1) == FAILURE) {
    1655           1 :                 RETURN_FALSE;
    1656             :         }
    1657             : 
    1658           6 :         switch((int) a1) {
    1659             :                 case NETSNMP_OID_OUTPUT_SUFFIX:
    1660             :                 case NETSNMP_OID_OUTPUT_MODULE:
    1661             :                 case NETSNMP_OID_OUTPUT_FULL:
    1662             :                 case NETSNMP_OID_OUTPUT_NUMERIC:
    1663             :                 case NETSNMP_OID_OUTPUT_UCD:
    1664             :                 case NETSNMP_OID_OUTPUT_NONE:
    1665           5 :                         netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, a1);
    1666           5 :                         RETURN_TRUE;
    1667             :                         break;
    1668             :                 default:
    1669           1 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown SNMP output print format '%d'", (int) a1);
    1670           1 :                         RETURN_FALSE;
    1671             :                         break;
    1672             :         }
    1673             : } 
    1674             : /* }}} */
    1675             : 
    1676             : /* {{{ proto mixed snmp2_get(string host, string community, mixed object_id [, int timeout [, int retries]]) 
    1677             :    Fetch a SNMP object */
    1678          12 : PHP_FUNCTION(snmp2_get)
    1679             : {
    1680          12 :         php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GET, SNMP_VERSION_2c);
    1681          12 : }
    1682             : /* }}} */
    1683             : 
    1684             : /* {{{ proto mixed snmp2_getnext(string host, string community, mixed object_id [, int timeout [, int retries]]) 
    1685             :    Fetch a SNMP object */
    1686           3 : PHP_FUNCTION(snmp2_getnext)
    1687             : {
    1688           3 :         php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GETNEXT, SNMP_VERSION_2c);
    1689           3 : }
    1690             : /* }}} */
    1691             : 
    1692             : /* {{{ proto mixed snmp2_walk(string host, string community, mixed object_id [, int timeout [, int retries]]) 
    1693             :    Return all objects under the specified object id */
    1694           9 : PHP_FUNCTION(snmp2_walk)
    1695             : {
    1696           9 :         php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, (SNMP_CMD_WALK | SNMP_NUMERIC_KEYS), SNMP_VERSION_2c);
    1697           9 : }
    1698             : /* }}} */
    1699             : 
    1700             : /* {{{ proto mixed snmp2_real_walk(string host, string community, mixed object_id [, int timeout [, int retries]])
    1701             :    Return all objects including their respective object id withing the specified one */
    1702           8 : PHP_FUNCTION(snmp2_real_walk)
    1703             : {
    1704           8 :         php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_WALK, SNMP_VERSION_2c);
    1705           8 : }
    1706             : /* }}} */
    1707             : 
    1708             : /* {{{ proto bool snmp2_set(string host, string community, mixed object_id, mixed type, mixed value [, int timeout [, int retries]]) 
    1709             :    Set the value of a SNMP object */
    1710          26 : PHP_FUNCTION(snmp2_set)
    1711             : {
    1712          26 :         php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_SET, SNMP_VERSION_2c);
    1713          26 : }
    1714             : /* }}} */
    1715             : 
    1716             : /* {{{ 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]])
    1717             :    Fetch the value of a SNMP object */
    1718          20 : PHP_FUNCTION(snmp3_get)
    1719             : {
    1720          20 :         php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GET, SNMP_VERSION_3);
    1721          20 : }
    1722             : /* }}} */
    1723             : 
    1724             : /* {{{ 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]])
    1725             :    Fetch the value of a SNMP object */
    1726           1 : PHP_FUNCTION(snmp3_getnext)
    1727             : {
    1728           1 :         php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GETNEXT, SNMP_VERSION_3);
    1729           1 : }
    1730             : /* }}} */
    1731             : 
    1732             : /* {{{ 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]])
    1733             :    Fetch the value of a SNMP object */
    1734           2 : PHP_FUNCTION(snmp3_walk)
    1735             : {
    1736           2 :         php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, (SNMP_CMD_WALK | SNMP_NUMERIC_KEYS), SNMP_VERSION_3);
    1737           2 : }
    1738             : /* }}} */
    1739             : 
    1740             : /* {{{ 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]])
    1741             :    Fetch the value of a SNMP object */
    1742           2 : PHP_FUNCTION(snmp3_real_walk)
    1743             : {
    1744           2 :         php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_WALK, SNMP_VERSION_3);
    1745           2 : }
    1746             : /* }}} */
    1747             : 
    1748             : /* {{{ 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]])
    1749             :    Fetch the value of a SNMP object */
    1750           5 : PHP_FUNCTION(snmp3_set)
    1751             : {
    1752           5 :         php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_SET, SNMP_VERSION_3);
    1753           5 : }
    1754             : /* }}} */
    1755             : 
    1756             : /* {{{ proto bool snmp_set_valueretrieval(int method)
    1757             :    Specify the method how the SNMP values will be returned */
    1758          38 : PHP_FUNCTION(snmp_set_valueretrieval)
    1759             : {
    1760             :         zend_long method;
    1761             : 
    1762          38 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &method) == FAILURE) {
    1763           2 :                 RETURN_FALSE;
    1764             :         }
    1765             : 
    1766          36 :         if (method >= 0 && method <= (SNMP_VALUE_LIBRARY|SNMP_VALUE_PLAIN|SNMP_VALUE_OBJECT)) {
    1767          35 :                         SNMP_G(valueretrieval) = method;
    1768          35 :                         RETURN_TRUE;
    1769             :         } else {
    1770           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown SNMP value retrieval method '%pd'", method);
    1771           1 :                 RETURN_FALSE;
    1772             :         }
    1773             : }
    1774             : /* }}} */
    1775             : 
    1776             : /* {{{ proto int snmp_get_valueretrieval()
    1777             :    Return the method how the SNMP values will be returned */
    1778           7 : PHP_FUNCTION(snmp_get_valueretrieval)
    1779             : {
    1780           7 :         if (zend_parse_parameters_none() == FAILURE) {
    1781           1 :                 RETURN_FALSE;
    1782             :         }
    1783             : 
    1784           6 :         RETURN_LONG(SNMP_G(valueretrieval));
    1785             : }
    1786             : /* }}} */
    1787             : 
    1788             : /* {{{ proto bool snmp_read_mib(string filename)
    1789             :    Reads and parses a MIB file into the active MIB tree. */
    1790           3 : PHP_FUNCTION(snmp_read_mib)
    1791             : {
    1792             :         char *filename;
    1793             :         size_t filename_len;
    1794             : 
    1795           3 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p", &filename, &filename_len) == FAILURE) {
    1796           1 :                 RETURN_FALSE;
    1797             :         }
    1798             : 
    1799           2 :         if (!read_mib(filename)) {
    1800           1 :                 char *error = strerror(errno);
    1801           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error while reading MIB file '%s': %s", filename, error);
    1802           1 :                 RETURN_FALSE;
    1803             :         }
    1804           1 :         RETURN_TRUE;
    1805             : }
    1806             : /* }}} */
    1807             : 
    1808             : /* {{{ proto SNMP SNMP::__construct(int version, string hostname, string community|securityName [, long timeout [, long retries]])
    1809             :         Creates a new SNMP session to specified host. */
    1810          33 : PHP_METHOD(snmp, __construct)
    1811             : {
    1812             :         php_snmp_object *snmp_object;
    1813          33 :         zval *object = getThis();
    1814             :         char *a1, *a2;
    1815             :         size_t a1_len, a2_len;
    1816          33 :         zend_long timeout = SNMP_DEFAULT_TIMEOUT;
    1817          33 :         zend_long retries = SNMP_DEFAULT_RETRIES;
    1818          33 :         zend_long version = SNMP_DEFAULT_VERSION;
    1819          33 :         int argc = ZEND_NUM_ARGS();
    1820             :         zend_error_handling error_handling;
    1821             : 
    1822          33 :         snmp_object = Z_SNMP_P(object);
    1823          33 :         zend_replace_error_handling(EH_THROW, NULL, &error_handling TSRMLS_CC);
    1824             :         
    1825          33 :         if (zend_parse_parameters(argc TSRMLS_CC, "lss|ll", &version, &a1, &a1_len, &a2, &a2_len, &timeout, &retries) == FAILURE) {
    1826           3 :                 zend_restore_error_handling(&error_handling TSRMLS_CC);
    1827           3 :                 return;
    1828             :         }
    1829             : 
    1830          30 :         zend_restore_error_handling(&error_handling TSRMLS_CC);
    1831             : 
    1832          30 :         switch(version) {
    1833             :                 case SNMP_VERSION_1:
    1834             :                 case SNMP_VERSION_2c:
    1835             :                 case SNMP_VERSION_3:
    1836          29 :                         break;
    1837             :                 default:
    1838           1 :                         zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Unknown SNMP protocol version", 0 TSRMLS_CC);
    1839           1 :                         return;
    1840             :         }
    1841             : 
    1842             :         /* handle re-open of snmp session */
    1843          29 :         if (snmp_object->session) {
    1844           0 :                 netsnmp_session_free(&(snmp_object->session));
    1845             :         }
    1846             :         
    1847          29 :         if (netsnmp_session_init(&(snmp_object->session), version, a1, a2, timeout, retries TSRMLS_CC)) {
    1848           0 :                 return;
    1849             :         }
    1850          29 :         snmp_object->max_oids = 0;
    1851          29 :         snmp_object->valueretrieval = SNMP_G(valueretrieval);
    1852          29 :         snmp_object->enum_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM);
    1853          29 :         snmp_object->oid_output_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
    1854          29 :         snmp_object->quick_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT);
    1855          29 :         snmp_object->oid_increasing_check = TRUE;
    1856          29 :         snmp_object->exceptions_enabled = 0;
    1857             : }
    1858             : /* }}} */
    1859             : 
    1860             : /* {{{ proto bool SNMP::close() 
    1861             :         Close SNMP session */
    1862          28 : PHP_METHOD(snmp, close)
    1863             : {
    1864             :         php_snmp_object *snmp_object;
    1865          28 :         zval *object = getThis();
    1866             : 
    1867          28 :         snmp_object = Z_SNMP_P(object);
    1868             : 
    1869          28 :         if (zend_parse_parameters_none() == FAILURE) {
    1870           1 :                 RETURN_FALSE;
    1871             :         }
    1872             : 
    1873          27 :         netsnmp_session_free(&(snmp_object->session));
    1874             : 
    1875          27 :         RETURN_TRUE;
    1876             : }
    1877             : /* }}} */
    1878             : 
    1879             : /* {{{ proto mixed SNMP::get(mixed object_id [, bool preserve_keys])
    1880             :    Fetch a SNMP object returning scalar for single OID and array of oid->value pairs for multi OID request */
    1881          33 : PHP_METHOD(snmp, get)
    1882             : {
    1883          33 :         php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GET, (-1));
    1884          33 : }
    1885             : /* }}} */
    1886             : 
    1887             : /* {{{ proto mixed SNMP::getnext(mixed object_id)
    1888             :    Fetch a SNMP object returning scalar for single OID and array of oid->value pairs for multi OID request */
    1889           6 : PHP_METHOD(snmp, getnext)
    1890             : {
    1891           6 :         php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GETNEXT, (-1));
    1892           6 : }
    1893             : /* }}} */
    1894             : 
    1895             : /* {{{ proto mixed SNMP::walk(mixed object_id [, bool $suffix_as_key = FALSE [, int $max_repetitions [, int $non_repeaters]])
    1896             :    Return all objects including their respective object id withing the specified one as array of oid->value pairs */
    1897           9 : PHP_METHOD(snmp, walk)
    1898             : {
    1899           9 :         php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_WALK, (-1));
    1900           9 : }
    1901             : /* }}} */
    1902             : 
    1903             : /* {{{ proto bool SNMP::set(mixed object_id, mixed type, mixed value) 
    1904             :    Set the value of a SNMP object */
    1905           8 : PHP_METHOD(snmp, set)
    1906             : {
    1907           8 :         php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_SET, (-1));
    1908           8 : }
    1909             : /* }}} */
    1910             : 
    1911             : /* {{{ proto bool SNMP::setSecurity(string sec_level, [ string auth_protocol, string auth_passphrase [, string priv_protocol, string priv_passphrase [, string contextName [, string contextEngineID]]]])
    1912             :         Set SNMPv3 security-related session parameters */
    1913          19 : PHP_METHOD(snmp, setSecurity)
    1914             : {
    1915             :         php_snmp_object *snmp_object;
    1916          19 :         zval *object = getThis();
    1917          19 :         char *a1 = "", *a2 = "", *a3 = "", *a4 = "", *a5 = "", *a6 = "", *a7 = "";
    1918          19 :         size_t a1_len = 0, a2_len = 0, a3_len = 0, a4_len = 0, a5_len = 0, a6_len = 0, a7_len = 0;
    1919          19 :         int argc = ZEND_NUM_ARGS();
    1920             : 
    1921          19 :         snmp_object = Z_SNMP_P(object);
    1922             :         
    1923          19 :         if (zend_parse_parameters(argc TSRMLS_CC, "s|ssssss", &a1, &a1_len, &a2, &a2_len, &a3, &a3_len,
    1924             :                 &a4, &a4_len, &a5, &a5_len, &a6, &a6_len, &a7, &a7_len) == FAILURE) {
    1925           1 :                 RETURN_FALSE;
    1926             :         }
    1927             : 
    1928          18 :         if (netsnmp_session_set_security(snmp_object->session, a1, a2, a3, a4, a5, a6, a7 TSRMLS_CC)) {
    1929             :                 /* Warning message sent already, just bail out */
    1930           9 :                 RETURN_FALSE;
    1931             :         }
    1932           9 :         RETURN_TRUE;
    1933             : }
    1934             : /* }}} */
    1935             : 
    1936             : /* {{{ proto long SNMP::getErrno() 
    1937             :         Get last error code number */
    1938          10 : PHP_METHOD(snmp, getErrno)
    1939             : {
    1940             :         php_snmp_object *snmp_object;
    1941          10 :         zval *object = getThis();
    1942             : 
    1943          10 :         snmp_object = Z_SNMP_P(object);
    1944             : 
    1945          10 :         RETVAL_LONG(snmp_object->snmp_errno);
    1946          10 :         return;
    1947             : }
    1948             : /* }}} */
    1949             : 
    1950             : /* {{{ proto long SNMP::getError() 
    1951             :         Get last error message */
    1952          10 : PHP_METHOD(snmp, getError)
    1953             : {
    1954             :         php_snmp_object *snmp_object;
    1955          10 :         zval *object = getThis();
    1956             : 
    1957          10 :         snmp_object = Z_SNMP_P(object);
    1958             : 
    1959          20 :         RETURN_STRING(snmp_object->snmp_errstr);
    1960             : }
    1961             : /* }}} */
    1962             : 
    1963             : /* {{{ */
    1964      163384 : 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)
    1965             : {
    1966             :         php_snmp_prop_handler p;
    1967             : 
    1968      163384 :         p.name = (char*) name;
    1969      163384 :         p.name_length = name_length;
    1970      163384 :         p.read_func = (read_func) ? read_func : NULL;
    1971      163384 :         p.write_func = (write_func) ? write_func : NULL;
    1972             :         zend_hash_str_add_mem(h, (char *)name, name_length, &p, sizeof(php_snmp_prop_handler));
    1973      163384 : }
    1974             : /* }}} */
    1975             : 
    1976             : /* {{{ php_snmp_read_property(zval *object, zval *member, int type[, const zend_literal *key])
    1977             :    Generic object property reader */
    1978          12 : zval *php_snmp_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv TSRMLS_DC)
    1979             : {
    1980             :         zval tmp_member;
    1981             :         zval *retval;
    1982             :         php_snmp_object *obj;
    1983             :         php_snmp_prop_handler *hnd;
    1984             :         int ret;
    1985             : 
    1986          12 :         obj = Z_SNMP_P(object); 
    1987             : 
    1988          12 :         if (Z_TYPE_P(member) != IS_STRING) {
    1989           1 :                 ZVAL_COPY(&tmp_member, member);
    1990           1 :                 convert_to_string(&tmp_member);
    1991           1 :                 member = &tmp_member;
    1992             :         }
    1993             : 
    1994          24 :         hnd = zend_hash_find_ptr(&php_snmp_properties, Z_STR_P(member));
    1995             : 
    1996          22 :         if (hnd && hnd->read_func) {
    1997          10 :                 ret = hnd->read_func(obj, rv TSRMLS_CC);
    1998          10 :                 if (ret == SUCCESS) {
    1999          10 :                         retval = rv;
    2000             :                 } else {
    2001           0 :                         retval = &EG(uninitialized_zval);
    2002             :                 }
    2003             :         } else {
    2004           2 :                 zend_object_handlers * std_hnd = zend_get_std_object_handlers();
    2005           2 :                 retval = std_hnd->read_property(object, member, type, cache_slot, rv TSRMLS_CC);
    2006             :         }
    2007             : 
    2008          12 :         if (member == &tmp_member) {
    2009           1 :                 zval_ptr_dtor(member);
    2010             :         }
    2011             : 
    2012          12 :         return retval;
    2013             : }
    2014             : /* }}} */
    2015             : 
    2016             : /* {{{ php_snmp_write_property(zval *object, zval *member, zval *value[, const zend_literal *key])
    2017             :    Generic object property writer */
    2018          26 : void php_snmp_write_property(zval *object, zval *member, zval *value, void **cache_slot TSRMLS_DC)
    2019             : {
    2020             :         zval tmp_member;
    2021             :         php_snmp_object *obj;
    2022             :         php_snmp_prop_handler *hnd;
    2023             : 
    2024          26 :         if (Z_TYPE_P(member) != IS_STRING) {
    2025           1 :                 ZVAL_COPY(&tmp_member, member);
    2026           1 :                 convert_to_string(&tmp_member);
    2027           1 :                 member = &tmp_member;
    2028             :         }
    2029             : 
    2030          26 :         obj = Z_SNMP_P(object);
    2031             : 
    2032          52 :         hnd = zend_hash_find_ptr(&php_snmp_properties, Z_STR_P(member));
    2033             : 
    2034          51 :         if (hnd && hnd->write_func) {
    2035          25 :                 hnd->write_func(obj, value TSRMLS_CC);
    2036             :                 /*
    2037             :                 if (!PZVAL_IS_REF(value) && Z_REFCOUNT_P(value) == 0) {
    2038             :                         Z_ADDREF_P(value);
    2039             :                         zval_ptr_dtor(&value);
    2040             :                 }
    2041             :                 */
    2042             :         } else {
    2043           1 :                 zend_object_handlers * std_hnd = zend_get_std_object_handlers();
    2044           1 :                 std_hnd->write_property(object, member, value, cache_slot TSRMLS_CC);
    2045             :         }
    2046             : 
    2047          26 :         if (member == &tmp_member) {
    2048           1 :                 zval_ptr_dtor(member);
    2049             :         }
    2050          26 : }
    2051             : /* }}} */
    2052             : 
    2053             : /* {{{ php_snmp_has_property(zval *object, zval *member, int has_set_exists[, const zend_literal *key])
    2054             :    Generic object property checker */
    2055           5 : static int php_snmp_has_property(zval *object, zval *member, int has_set_exists, void **cache_slot TSRMLS_DC)
    2056             : {
    2057             :         zval rv;
    2058             :         php_snmp_prop_handler *hnd;
    2059           5 :         int ret = 0;
    2060             : 
    2061          10 :         if ((hnd = zend_hash_find_ptr(&php_snmp_properties, Z_STR_P(member))) != NULL) {
    2062           3 :                 switch (has_set_exists) {
    2063             :                         case 2:
    2064           1 :                                 ret = 1;
    2065           1 :                                 break;
    2066             :                         case 0: {
    2067           1 :                                 zval *value = php_snmp_read_property(object, member, BP_VAR_IS, cache_slot, &rv TSRMLS_CC);
    2068           1 :                                 if (value != &EG(uninitialized_zval)) {
    2069           1 :                                         ret = Z_TYPE_P(value) != IS_NULL? 1 : 0;
    2070           1 :                                         zval_ptr_dtor(value);
    2071             :                                 }
    2072           1 :                                 break;
    2073             :                         }
    2074             :                         default: {
    2075           1 :                                 zval *value = php_snmp_read_property(object, member, BP_VAR_IS, cache_slot, &rv TSRMLS_CC);
    2076           1 :                                 if (value != &EG(uninitialized_zval)) {
    2077           1 :                                         convert_to_boolean(value);
    2078           1 :                                         ret = Z_TYPE_P(value) == IS_TRUE? 1:0;
    2079             :                                 }
    2080             :                                 break;
    2081             :                         }
    2082             :                 }
    2083             :         } else {
    2084           2 :                 zend_object_handlers *std_hnd = zend_get_std_object_handlers();
    2085           2 :                 ret = std_hnd->has_property(object, member, has_set_exists, cache_slot TSRMLS_CC);
    2086             :         }
    2087           5 :         return ret;
    2088             : }
    2089             : /* }}} */
    2090             : 
    2091             : /* {{{ php_snmp_get_properties(zval *object)
    2092             :    Returns all object properties. Injects SNMP properties into object on first call */
    2093           4 : static HashTable *php_snmp_get_properties(zval *object TSRMLS_DC)
    2094             : {
    2095             :         php_snmp_object *obj;
    2096             :         php_snmp_prop_handler *hnd;
    2097             :         HashTable *props;
    2098             :         zval rv;
    2099             :         zend_string *key;
    2100             :         zend_ulong num_key;
    2101             : 
    2102           4 :         obj = Z_SNMP_P(object);
    2103           4 :         props = zend_std_get_properties(object TSRMLS_CC);
    2104             : 
    2105          68 :         ZEND_HASH_FOREACH_KEY_PTR(&php_snmp_properties, num_key, key, hnd) {
    2106          32 :                 if (!hnd->read_func || hnd->read_func(obj, &rv TSRMLS_CC) != SUCCESS) {
    2107           0 :                         ZVAL_NULL(&rv);
    2108             :                 }
    2109          32 :                 zend_hash_update(props, key, &rv);
    2110             :         } ZEND_HASH_FOREACH_END();
    2111             : 
    2112           4 :         return obj->zo.properties;
    2113             : }
    2114             : /* }}} */
    2115             : 
    2116             : /* {{{ */
    2117           6 : static int php_snmp_read_info(php_snmp_object *snmp_object, zval *retval TSRMLS_DC)
    2118             : {
    2119             :         zval val;
    2120             : 
    2121           6 :         array_init(retval);
    2122             : 
    2123           6 :         if (snmp_object->session == NULL) {
    2124           0 :                 return SUCCESS;
    2125             :         }
    2126             :                 
    2127          12 :         ZVAL_STRINGL(&val, snmp_object->session->peername, strlen(snmp_object->session->peername));
    2128           6 :         add_assoc_zval(retval, "hostname", &val);
    2129             :         
    2130           6 :         ZVAL_LONG(&val, snmp_object->session->remote_port);
    2131           6 :         add_assoc_zval(retval, "port", &val);
    2132             :         
    2133           6 :         ZVAL_LONG(&val, snmp_object->session->timeout);
    2134           6 :         add_assoc_zval(retval, "timeout", &val);
    2135             :         
    2136           6 :         ZVAL_LONG(&val, snmp_object->session->retries);
    2137           6 :         add_assoc_zval(retval, "retries", &val);
    2138             :         
    2139           6 :         return SUCCESS;
    2140             : }
    2141             : /* }}} */
    2142             : 
    2143             : /* {{{ */
    2144           7 : static int php_snmp_read_max_oids(php_snmp_object *snmp_object, zval *retval TSRMLS_DC)
    2145             : {
    2146           7 :         if (snmp_object->max_oids > 0) {
    2147           3 :                 ZVAL_LONG(retval, snmp_object->max_oids);
    2148             :         } else {
    2149           4 :                 ZVAL_NULL(retval);
    2150             :         }
    2151           7 :         return SUCCESS;
    2152             : }
    2153             : /* }}} */
    2154             : 
    2155             : #define PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(name) \
    2156             :         static int php_snmp_read_##name(php_snmp_object *snmp_object, zval *retval TSRMLS_DC) \
    2157             :         { \
    2158             :                 ZVAL_BOOL(retval, snmp_object->name); \
    2159             :                 return SUCCESS; \
    2160             :         }
    2161             : 
    2162           4 : PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(oid_increasing_check)
    2163           4 : PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(quick_print)
    2164           6 : PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(enum_print)
    2165             : 
    2166             : #define PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(name) \
    2167             :         static int php_snmp_read_##name(php_snmp_object *snmp_object, zval *retval TSRMLS_DC) \
    2168             :         { \
    2169             :                 ZVAL_LONG(retval, snmp_object->name); \
    2170             :                 return SUCCESS; \
    2171             :         }
    2172             : 
    2173           6 : PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(valueretrieval)
    2174           5 : PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(oid_output_format)
    2175           4 : PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(exceptions_enabled)
    2176             : 
    2177             : /* {{{ */
    2178           1 : static int php_snmp_write_info(php_snmp_object *snmp_object, zval *newval TSRMLS_DC)
    2179             : {
    2180           1 :         php_error_docref(NULL TSRMLS_CC, E_WARNING, "info property is read-only");
    2181           1 :         return FAILURE;
    2182             : }
    2183             : /* }}} */
    2184             : 
    2185             : /* {{{ */
    2186           8 : static int php_snmp_write_max_oids(php_snmp_object *snmp_object, zval *newval TSRMLS_DC)
    2187             : {
    2188             :         zval ztmp;
    2189           8 :         int ret = SUCCESS;
    2190             : 
    2191           8 :         if (Z_TYPE_P(newval) == IS_NULL) {
    2192           1 :                 snmp_object->max_oids = 0;
    2193           1 :                 return ret;
    2194             :         }
    2195             : 
    2196           7 :         if (Z_TYPE_P(newval) != IS_LONG) {
    2197           2 :                 ztmp = *newval;
    2198             :                 zval_copy_ctor(&ztmp);
    2199           2 :                 convert_to_long(&ztmp);
    2200           2 :                 newval = &ztmp;
    2201             :         }
    2202             : 
    2203           7 :         if (Z_LVAL_P(newval) > 0) {
    2204           5 :                 snmp_object->max_oids = Z_LVAL_P(newval);
    2205             :         } else {
    2206           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "max_oids should be positive integer or NULL, got %pd", Z_LVAL_P(newval));
    2207             :         }
    2208             :         
    2209           7 :         if (newval == &ztmp) {
    2210             :                 zval_dtor(newval);
    2211             :         }
    2212             : 
    2213           7 :         return ret;
    2214             : }
    2215             : /* }}} */
    2216             : 
    2217             : /* {{{ */
    2218           6 : static int php_snmp_write_valueretrieval(php_snmp_object *snmp_object, zval *newval TSRMLS_DC)
    2219             : {
    2220             :         zval ztmp;
    2221           6 :         int ret = SUCCESS;
    2222             : 
    2223           6 :         if (Z_TYPE_P(newval) != IS_LONG) {
    2224           1 :                 ztmp = *newval;
    2225             :                 zval_copy_ctor(&ztmp);
    2226           1 :                 convert_to_long(&ztmp);
    2227           1 :                 newval = &ztmp;
    2228             :         }
    2229             : 
    2230          10 :         if (Z_LVAL_P(newval) >= 0 && Z_LVAL_P(newval) <= (SNMP_VALUE_LIBRARY|SNMP_VALUE_PLAIN|SNMP_VALUE_OBJECT)) {
    2231           4 :                 snmp_object->valueretrieval = Z_LVAL_P(newval);
    2232             :         } else {
    2233           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown SNMP value retrieval method '%pd'", Z_LVAL_P(newval));
    2234           2 :                 ret = FAILURE;
    2235             :         }
    2236             :         
    2237           6 :         if (newval == &ztmp) {
    2238             :                 zval_dtor(newval);
    2239             :         }
    2240             : 
    2241           6 :         return ret;
    2242             : }
    2243             : /* }}} */
    2244             : 
    2245             : #define PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(name) \
    2246             : static int php_snmp_write_##name(php_snmp_object *snmp_object, zval *newval TSRMLS_DC) \
    2247             : { \
    2248             :         zval ztmp; \
    2249             :         ZVAL_COPY(&ztmp, newval); \
    2250             :         convert_to_boolean(&ztmp); \
    2251             :         newval = &ztmp; \
    2252             : \
    2253             :         snmp_object->name = Z_TYPE_P(newval) == IS_TRUE? 1 : 0; \
    2254             : \
    2255             :         return SUCCESS; \
    2256             : }
    2257             : 
    2258           6 : PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(quick_print)
    2259           6 : PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(enum_print)
    2260           6 : PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(oid_increasing_check)
    2261             : 
    2262             : /* {{{ */
    2263           3 : static int php_snmp_write_oid_output_format(php_snmp_object *snmp_object, zval *newval TSRMLS_DC)
    2264             : {
    2265             :         zval ztmp;
    2266           3 :         int ret = SUCCESS;
    2267           3 :         if (Z_TYPE_P(newval) != IS_LONG) {
    2268           1 :                 ZVAL_COPY(&ztmp, newval);
    2269           1 :                 convert_to_long(&ztmp);
    2270           1 :                 newval = &ztmp;
    2271             :         }
    2272             :         
    2273           3 :         switch(Z_LVAL_P(newval)) {
    2274             :                 case NETSNMP_OID_OUTPUT_SUFFIX:
    2275             :                 case NETSNMP_OID_OUTPUT_MODULE:
    2276             :                 case NETSNMP_OID_OUTPUT_FULL:
    2277             :                 case NETSNMP_OID_OUTPUT_NUMERIC:
    2278             :                 case NETSNMP_OID_OUTPUT_UCD:
    2279             :                 case NETSNMP_OID_OUTPUT_NONE:
    2280           2 :                         snmp_object->oid_output_format = Z_LVAL_P(newval);
    2281           2 :                         break;
    2282             :                 default:
    2283           1 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown SNMP output print format '%pd'", Z_LVAL_P(newval));
    2284           1 :                         ret = FAILURE;
    2285             :                         break;
    2286             :         }
    2287             : 
    2288           3 :         if (newval == &ztmp) {
    2289           1 :                 zval_ptr_dtor(newval);
    2290             :         }
    2291           3 :         return ret;
    2292             : }
    2293             : /* }}} */
    2294             : 
    2295             : /* {{{ */
    2296           1 : static int php_snmp_write_exceptions_enabled(php_snmp_object *snmp_object, zval *newval TSRMLS_DC)
    2297             : {
    2298             :         zval ztmp;
    2299           1 :         int ret = SUCCESS;
    2300           1 :         if (Z_TYPE_P(newval) != IS_LONG) {
    2301           0 :                 ZVAL_COPY(&ztmp, newval);
    2302           0 :                 convert_to_long(&ztmp);
    2303           0 :                 newval = &ztmp;
    2304             :         }
    2305             : 
    2306           1 :         snmp_object->exceptions_enabled = Z_LVAL_P(newval);  
    2307             : 
    2308           1 :         if (newval == &ztmp) {
    2309           0 :                 zval_ptr_dtor(newval);
    2310             :         }
    2311           1 :         return ret;
    2312             : }
    2313             : /* }}} */
    2314             : 
    2315      163656 : static void free_php_snmp_properties(zval *el)  /* {{{ */
    2316             : {
    2317      163656 :         pefree(Z_PTR_P(el), 1);
    2318      163656 : }
    2319             : /* }}} */
    2320             : 
    2321             : /* {{{ php_snmp_class_methods[] */
    2322             : static zend_function_entry php_snmp_class_methods[] = {
    2323             :         PHP_ME(snmp,     __construct,                   arginfo_snmp_create,            ZEND_ACC_PUBLIC)
    2324             :         PHP_ME(snmp,     close,                         arginfo_snmp_void,              ZEND_ACC_PUBLIC)
    2325             :         PHP_ME(snmp,     setSecurity,                   arginfo_snmp_setSecurity,       ZEND_ACC_PUBLIC)
    2326             : 
    2327             :         PHP_ME(snmp,     get,                           arginfo_snmp_get,               ZEND_ACC_PUBLIC)
    2328             :         PHP_ME(snmp,     getnext,                       arginfo_snmp_get,               ZEND_ACC_PUBLIC)
    2329             :         PHP_ME(snmp,     walk,                          arginfo_snmp_walk,              ZEND_ACC_PUBLIC)
    2330             :         PHP_ME(snmp,     set,                           arginfo_snmp_set,               ZEND_ACC_PUBLIC)
    2331             :         PHP_ME(snmp,     getErrno,                      arginfo_snmp_void,              ZEND_ACC_PUBLIC)
    2332             :         PHP_ME(snmp,     getError,                      arginfo_snmp_void,              ZEND_ACC_PUBLIC)
    2333             : 
    2334             :         PHP_FE_END
    2335             : };
    2336             : 
    2337             : #define PHP_SNMP_PROPERTY_ENTRY_RECORD(name) \
    2338             :         { "" #name "",              sizeof("" #name "") - 1,    php_snmp_read_##name,   php_snmp_write_##name }
    2339             : 
    2340             : const php_snmp_prop_handler php_snmp_property_entries[] = {
    2341             :         PHP_SNMP_PROPERTY_ENTRY_RECORD(info),
    2342             :         PHP_SNMP_PROPERTY_ENTRY_RECORD(max_oids),
    2343             :         PHP_SNMP_PROPERTY_ENTRY_RECORD(valueretrieval),
    2344             :         PHP_SNMP_PROPERTY_ENTRY_RECORD(quick_print),
    2345             :         PHP_SNMP_PROPERTY_ENTRY_RECORD(enum_print),
    2346             :         PHP_SNMP_PROPERTY_ENTRY_RECORD(oid_output_format),
    2347             :         PHP_SNMP_PROPERTY_ENTRY_RECORD(oid_increasing_check),
    2348             :         PHP_SNMP_PROPERTY_ENTRY_RECORD(exceptions_enabled),
    2349             :         { NULL, 0, NULL, NULL}
    2350             : };
    2351             : /* }}} */
    2352             : 
    2353             : /* {{{ PHP_MINIT_FUNCTION
    2354             :  */
    2355       20423 : PHP_MINIT_FUNCTION(snmp)
    2356             : {
    2357             :         netsnmp_log_handler *logh;
    2358             :         zend_class_entry ce, cex;
    2359             : 
    2360       20423 :         le_snmp_session = zend_register_list_destructors_ex(php_snmp_session_destructor, NULL, PHP_SNMP_SESSION_RES_NAME, module_number);
    2361             : 
    2362       20423 :         init_snmp("snmpapp");
    2363             : 
    2364             : #ifdef NETSNMP_DS_LIB_DONT_PERSIST_STATE
    2365             :         /* Prevent update of the snmpapp.conf file */
    2366       20423 :         netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_PERSIST_STATE, 1);
    2367             : #endif
    2368             : 
    2369             :         /* Disable logging, use exit status'es and related variabled to detect errors */
    2370       20423 :         shutdown_snmp_logging();
    2371       20423 :         logh = netsnmp_register_loghandler(NETSNMP_LOGHANDLER_NONE, LOG_ERR);
    2372       20423 :         if (logh) {
    2373       20423 :                 logh->pri_max = LOG_ERR;
    2374             :         }
    2375             : 
    2376       20423 :         memcpy(&php_snmp_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    2377       20423 :         php_snmp_object_handlers.read_property = php_snmp_read_property;
    2378       20423 :         php_snmp_object_handlers.write_property = php_snmp_write_property;
    2379       20423 :         php_snmp_object_handlers.has_property = php_snmp_has_property;
    2380       20423 :         php_snmp_object_handlers.get_properties = php_snmp_get_properties;
    2381             : 
    2382             :         /* Register SNMP Class */
    2383       20423 :         INIT_CLASS_ENTRY(ce, "SNMP", php_snmp_class_methods);
    2384       20423 :         ce.create_object = php_snmp_object_new;
    2385       20423 :         php_snmp_object_handlers.offset = XtOffsetOf(php_snmp_object, zo);
    2386       20423 :         php_snmp_object_handlers.clone_obj = NULL;
    2387       20423 :         php_snmp_object_handlers.free_obj = php_snmp_object_free_storage;
    2388       20423 :         php_snmp_ce = zend_register_internal_class(&ce TSRMLS_CC);
    2389             : 
    2390             :         /* Register SNMP Class properties */
    2391       20423 :         zend_hash_init(&php_snmp_properties, 0, NULL, free_php_snmp_properties, 1);
    2392       20423 :         PHP_SNMP_ADD_PROPERTIES(&php_snmp_properties, php_snmp_property_entries);
    2393             : 
    2394       20423 :         REGISTER_LONG_CONSTANT("SNMP_OID_OUTPUT_SUFFIX",      NETSNMP_OID_OUTPUT_SUFFIX,      CONST_CS | CONST_PERSISTENT);
    2395       20423 :         REGISTER_LONG_CONSTANT("SNMP_OID_OUTPUT_MODULE",      NETSNMP_OID_OUTPUT_MODULE,      CONST_CS | CONST_PERSISTENT);
    2396       20423 :         REGISTER_LONG_CONSTANT("SNMP_OID_OUTPUT_FULL",                NETSNMP_OID_OUTPUT_FULL,        CONST_CS | CONST_PERSISTENT);
    2397       20423 :         REGISTER_LONG_CONSTANT("SNMP_OID_OUTPUT_NUMERIC",     NETSNMP_OID_OUTPUT_NUMERIC,     CONST_CS | CONST_PERSISTENT);
    2398       20423 :         REGISTER_LONG_CONSTANT("SNMP_OID_OUTPUT_UCD",         NETSNMP_OID_OUTPUT_UCD,         CONST_CS | CONST_PERSISTENT);
    2399       20423 :         REGISTER_LONG_CONSTANT("SNMP_OID_OUTPUT_NONE",                NETSNMP_OID_OUTPUT_NONE,        CONST_CS | CONST_PERSISTENT);
    2400             : 
    2401       20423 :         REGISTER_LONG_CONSTANT("SNMP_VALUE_LIBRARY",  SNMP_VALUE_LIBRARY,     CONST_CS | CONST_PERSISTENT);
    2402       20423 :         REGISTER_LONG_CONSTANT("SNMP_VALUE_PLAIN",    SNMP_VALUE_PLAIN,       CONST_CS | CONST_PERSISTENT);
    2403       20423 :         REGISTER_LONG_CONSTANT("SNMP_VALUE_OBJECT",   SNMP_VALUE_OBJECT,      CONST_CS | CONST_PERSISTENT);
    2404             : 
    2405       20423 :         REGISTER_LONG_CONSTANT("SNMP_BIT_STR",                ASN_BIT_STR,    CONST_CS | CONST_PERSISTENT);
    2406       20423 :         REGISTER_LONG_CONSTANT("SNMP_OCTET_STR",      ASN_OCTET_STR,  CONST_CS | CONST_PERSISTENT);
    2407       20423 :         REGISTER_LONG_CONSTANT("SNMP_OPAQUE",         ASN_OPAQUE,     CONST_CS | CONST_PERSISTENT);
    2408       20423 :         REGISTER_LONG_CONSTANT("SNMP_NULL",           ASN_NULL,       CONST_CS | CONST_PERSISTENT);
    2409       20423 :         REGISTER_LONG_CONSTANT("SNMP_OBJECT_ID",      ASN_OBJECT_ID,  CONST_CS | CONST_PERSISTENT);
    2410       20423 :         REGISTER_LONG_CONSTANT("SNMP_IPADDRESS",      ASN_IPADDRESS,  CONST_CS | CONST_PERSISTENT);
    2411       20423 :         REGISTER_LONG_CONSTANT("SNMP_COUNTER",                ASN_GAUGE,      CONST_CS | CONST_PERSISTENT);
    2412       20423 :         REGISTER_LONG_CONSTANT("SNMP_UNSIGNED",               ASN_UNSIGNED,   CONST_CS | CONST_PERSISTENT);
    2413       20423 :         REGISTER_LONG_CONSTANT("SNMP_TIMETICKS",      ASN_TIMETICKS,  CONST_CS | CONST_PERSISTENT);
    2414       20423 :         REGISTER_LONG_CONSTANT("SNMP_UINTEGER",               ASN_UINTEGER,   CONST_CS | CONST_PERSISTENT);
    2415       20423 :         REGISTER_LONG_CONSTANT("SNMP_INTEGER",                ASN_INTEGER,    CONST_CS | CONST_PERSISTENT);
    2416       20423 :         REGISTER_LONG_CONSTANT("SNMP_COUNTER64",      ASN_COUNTER64,  CONST_CS | CONST_PERSISTENT);
    2417             : 
    2418       20423 :         REGISTER_SNMP_CLASS_CONST_LONG("VERSION_1",                   SNMP_VERSION_1);
    2419       20423 :         REGISTER_SNMP_CLASS_CONST_LONG("VERSION_2c",                  SNMP_VERSION_2c);
    2420       20423 :         REGISTER_SNMP_CLASS_CONST_LONG("VERSION_2C",                  SNMP_VERSION_2c);
    2421       20423 :         REGISTER_SNMP_CLASS_CONST_LONG("VERSION_3",                   SNMP_VERSION_3);
    2422             : 
    2423       20423 :         REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_NOERROR",                       PHP_SNMP_ERRNO_NOERROR);
    2424       20423 :         REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_ANY",                   PHP_SNMP_ERRNO_ANY);
    2425       20423 :         REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_GENERIC",                       PHP_SNMP_ERRNO_GENERIC);
    2426       20423 :         REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_TIMEOUT",                       PHP_SNMP_ERRNO_TIMEOUT);
    2427       20423 :         REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_ERROR_IN_REPLY",                PHP_SNMP_ERRNO_ERROR_IN_REPLY);
    2428       20423 :         REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_OID_NOT_INCREASING",    PHP_SNMP_ERRNO_OID_NOT_INCREASING);
    2429       20423 :         REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_OID_PARSING_ERROR",     PHP_SNMP_ERRNO_OID_PARSING_ERROR);
    2430       20423 :         REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_MULTIPLE_SET_QUERIES",  PHP_SNMP_ERRNO_MULTIPLE_SET_QUERIES);
    2431             : 
    2432             :         /* Register SNMPException class */
    2433       20423 :         INIT_CLASS_ENTRY(cex, "SNMPException", NULL);
    2434             : #ifdef HAVE_SPL
    2435       20423 :         php_snmp_exception_ce = zend_register_internal_class_ex(&cex, spl_ce_RuntimeException TSRMLS_CC);
    2436             : #else
    2437             :         php_snmp_exception_ce = zend_register_internal_class_ex(&cex, zend_exception_get_default(TSRMLS_C) TSRMLS_CC);
    2438             : #endif
    2439             : 
    2440       20423 :         return SUCCESS;
    2441             : }
    2442             : /* }}} */
    2443             : 
    2444             : /* {{{ PHP_MSHUTDOWN_FUNCTION
    2445             :  */
    2446       20457 : PHP_MSHUTDOWN_FUNCTION(snmp)
    2447             : {
    2448       20457 :         snmp_shutdown("snmpapp");
    2449             :         
    2450       20457 :         zend_hash_destroy(&php_snmp_properties);
    2451             : 
    2452       20457 :         return SUCCESS;
    2453             : }
    2454             : /* }}} */
    2455             : 
    2456             : /* {{{ PHP_MINFO_FUNCTION
    2457             :  */
    2458         143 : PHP_MINFO_FUNCTION(snmp)
    2459             : {
    2460         143 :         php_info_print_table_start();
    2461         143 :         php_info_print_table_row(2, "NET-SNMP Support", "enabled");
    2462         143 :         php_info_print_table_row(2, "NET-SNMP Version", netsnmp_get_version());
    2463         143 :         php_info_print_table_row(2, "PHP SNMP Version", PHP_SNMP_VERSION);
    2464         143 :         php_info_print_table_end();
    2465         143 : }
    2466             : /* }}} */
    2467             : 
    2468             : /* {{{ snmp_module_deps[]
    2469             :  */
    2470             : #if ZEND_MODULE_API_NO >= 20050922
    2471             : static const zend_module_dep snmp_module_deps[] = {
    2472             : #ifdef HAVE_SPL
    2473             :         ZEND_MOD_REQUIRED("spl")
    2474             : #endif
    2475             :         ZEND_MOD_END
    2476             : };
    2477             : #endif
    2478             : /* }}} */
    2479             : 
    2480             : /* {{{ snmp_module_entry
    2481             :  */
    2482             : zend_module_entry snmp_module_entry = {
    2483             : #if ZEND_MODULE_API_NO >= 20050922
    2484             :         STANDARD_MODULE_HEADER_EX,
    2485             :         NULL,
    2486             :         snmp_module_deps,
    2487             : #else
    2488             :         STANDARD_MODULE_HEADER,
    2489             : #endif
    2490             :         "snmp",
    2491             :         snmp_functions,
    2492             :         PHP_MINIT(snmp),
    2493             :         PHP_MSHUTDOWN(snmp),
    2494             :         NULL,
    2495             :         NULL,
    2496             :         PHP_MINFO(snmp),
    2497             :         PHP_SNMP_VERSION,
    2498             :         PHP_MODULE_GLOBALS(snmp),
    2499             :         PHP_GINIT(snmp),
    2500             :         NULL,
    2501             :         NULL,
    2502             :         STANDARD_MODULE_PROPERTIES_EX
    2503             : };
    2504             : /* }}} */
    2505             : 
    2506             : #endif
    2507             : 
    2508             : /*
    2509             :  * Local variables:
    2510             :  * tab-width: 4
    2511             :  * c-basic-offset: 4
    2512             :  * End:
    2513             :  * vim600: sw=4 ts=4 fdm=marker
    2514             :  * vim<600: sw=4 ts=4
    2515             :  */

Generated by: LCOV version 1.10

Generated at Wed, 22 Oct 2014 07:24:56 +0000 (3 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.