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: 900 982 91.6 %
Date: 2015-04-14 Functions: 73 74 98.6 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Tue, 14 Apr 2015 11:48:50 +0000 (4 days ago)

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