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

Generated by: LCOV version 1.10

Generated at Wed, 04 May 2016 01:00:57 +0000 (5 hours ago)

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