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: 925 1011 91.5 %
Date: 2022-01-16 Functions: 74 75 98.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sun, 16 Jan 2022 08:19:20 +0000 (6 days ago)

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