49#include <sys/socket.h>
50#include <netinet/in.h>
67#include <net-snmp/net-snmp-config.h>
68#include <net-snmp/net-snmp-includes.h>
73#ifndef HAVE_SHUTDOWN_SNMP_LOGGING
74extern netsnmp_log_handler *logh_head;
75#define shutdown_snmp_logging() \
78 while(NULL != logh_head) \
79 netsnmp_remove_loghandler( logh_head ); \
83typedef struct snmp_session php_snmp_session;
85#define PHP_SNMP_ADD_PROPERTIES(a, b) \
88 while (b[i].name != NULL) { \
89 php_snmp_add_property((a), (b)[i].name, (b)[i].name_length, \
90 (php_snmp_read_t)(b)[i].read_func, (php_snmp_write_t)(b)[i].write_func); \
99static const oid objid_mib[] = {1, 3, 6, 1, 2, 1};
119 bool oid_increasing_check;
124#define SNMP_CMD_GET (1<<0)
126#define SNMP_CMD_GETNEXT (1<<1)
128#define SNMP_CMD_SET (1<<2)
130#define SNMP_CMD_WALK (1<<3)
132#define SNMP_NUMERIC_KEYS (1<<7)
134#define SNMP_ORIGINAL_NAMES_AS_KEYS (1<<8)
136#define SNMP_USE_SUFFIX_AS_KEYS (1<<9)
138#ifdef COMPILE_DL_SNMP
149#define PHP_SNMP_SESSION_FREE(a) { \
150 if ((*session)->a) { \
151 efree((*session)->a); \
152 (*session)->a = NULL; \
156static void netsnmp_session_free(php_snmp_session **session)
159 PHP_SNMP_SESSION_FREE(peername);
160 PHP_SNMP_SESSION_FREE(community);
161 PHP_SNMP_SESSION_FREE(securityName);
162 PHP_SNMP_SESSION_FREE(contextEngineID);
169static void php_snmp_object_free_storage(
zend_object *
object)
171 php_snmp_object *intern = php_snmp_fetch_object(
object);
177 netsnmp_session_free(&(intern->session));
185 php_snmp_object *intern;
188 intern = zend_object_alloc(
sizeof(php_snmp_object), class_type);
203static void php_snmp_error(
zval *
object,
int type,
const char *format, ...)
206 php_snmp_object *snmp_object =
NULL;
209 snmp_object = Z_SNMP_P(
object);
210 if (
type == PHP_SNMP_ERRNO_NOERROR) {
211 memset(snmp_object->snmp_errstr, 0,
sizeof(snmp_object->snmp_errstr));
213 va_start(
args, format);
214 vsnprintf(snmp_object->snmp_errstr,
sizeof(snmp_object->snmp_errstr) - 1, format,
args);
217 snmp_object->snmp_errno =
type;
220 if (
type == PHP_SNMP_ERRNO_NOERROR) {
224 if (
object && (snmp_object->exceptions_enabled &
type)) {
227 va_start(
args, format);
240static void php_snmp_getvalue(
struct variable_list *vars,
zval *snmpval,
int valueretrieval)
244 char *
buf = &(sbuf[0]);
245 char *dbuf = (
char *)
NULL;
246 int buflen =
sizeof(sbuf) - 1;
247 int val_len = vars->val_len;
256 if (snprint_value(
buf, buflen, vars->name, vars->name_length, vars) == -1) {
257 if (val_len > 512*1024) {
268 dbuf = (
char *)
erealloc(dbuf, val_len + 1);
270 dbuf = (
char *)
emalloc(val_len + 1);
278 dbuf = (
char *)
emalloc(val_len + 1);
285 switch (vars->type) {
300 snprint_objid(
buf, buflen, vars->val.objid, vars->val_len /
sizeof(oid));
306 (vars->val.string)[0], (vars->val.string)[1],
307 (vars->val.string)[2], (vars->val.string)[3]);
328#if defined(NETSNMP_WITH_OPAQUE_SPECIAL_TYPES) || defined(OPAQUE_SPECIAL_TYPES)
329 case ASN_OPAQUE_FLOAT:
334 case ASN_OPAQUE_DOUBLE:
340 printI64(
buf, vars->val.counter64);
347 printU64(
buf, vars->val.counter64);
363 add_property_long(snmpval,
"type", vars->type);
364 add_property_zval(snmpval,
"value", &
val);
382 struct snmp_session *session,
383 struct objid_query *objid_query)
385 struct snmp_session *ss;
386 struct snmp_pdu *pdu=
NULL, *response;
387 struct variable_list *vars;
388 oid root[MAX_NAME_LEN];
393 bool keepwalking =
true;
402 php_snmp_error(
getThis(), PHP_SNMP_ERRNO_NOERROR,
"");
404 if (st & SNMP_CMD_WALK) {
405 memcpy((
char *)root, (
char *)(objid_query->vars[0].name), (objid_query->vars[0].name_length) *
sizeof(oid));
406 rootlen = objid_query->vars[0].name_length;
407 objid_query->offset = objid_query->count;
410 if ((ss = snmp_open(session)) ==
NULL) {
417 if ((st & SNMP_CMD_SET) && objid_query->count > objid_query->step) {
418 php_snmp_error(
getThis(), PHP_SNMP_ERRNO_MULTIPLE_SET_QUERIES,
"Cannot fit all OIDs for SET query into one packet, using multiple queries");
421 while (keepwalking) {
423 if (st & SNMP_CMD_WALK) {
424 if (session->version == SNMP_VERSION_1) {
425 pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
427 pdu = snmp_pdu_create(SNMP_MSG_GETBULK);
428 pdu->non_repeaters = objid_query->non_repeaters;
429 pdu->max_repetitions = objid_query->max_repetitions;
431 snmp_add_null_var(pdu, objid_query->vars[0].name, objid_query->vars[0].name_length);
433 if (st & SNMP_CMD_GET) {
434 pdu = snmp_pdu_create(SNMP_MSG_GET);
435 }
else if (st & SNMP_CMD_GETNEXT) {
436 pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
437 }
else if (st & SNMP_CMD_SET) {
438 pdu = snmp_pdu_create(SNMP_MSG_SET);
444 for (
count = 0; objid_query->offset < objid_query->count &&
count < objid_query->step; objid_query->offset++,
count++){
445 if (st & SNMP_CMD_SET) {
446 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))) {
447 snprint_objid(
buf,
sizeof(
buf), objid_query->vars[objid_query->offset].name, objid_query->vars[objid_query->offset].name_length);
448 php_snmp_error(
getThis(), 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));
454 snmp_add_null_var(pdu, objid_query->vars[objid_query->offset].name, objid_query->vars[objid_query->offset].name_length);
457 if(pdu->variables ==
NULL){
465 status = snmp_synch_response(ss, pdu, &response);
466 if (
status == STAT_SUCCESS) {
467 if (response->errstat == SNMP_ERR_NOERROR) {
468 if (st & SNMP_CMD_SET) {
469 if (objid_query->offset < objid_query->count) {
471 snmp_free_pdu(response);
474 snmp_free_pdu(response);
478 for (vars = response->variables; vars; vars = vars->next_variable) {
480 if ( vars->type == SNMP_ENDOFMIBVIEW ||
481 vars->type == SNMP_NOSUCHOBJECT ||
482 vars->type == SNMP_NOSUCHINSTANCE ) {
486 snprint_objid(
buf,
sizeof(
buf), vars->name, vars->name_length);
487 snprint_value(buf2,
sizeof(buf2), vars->name, vars->name_length, vars);
488 php_snmp_error(
getThis(), PHP_SNMP_ERRNO_ERROR_IN_REPLY,
"Error in packet at '%s': %s",
buf, buf2);
492 if ((st & SNMP_CMD_WALK) &&
493 (vars->name_length < rootlen || memcmp(root, vars->name, rootlen *
sizeof(oid)))) {
500 objid_query->offset = 0;
507 php_snmp_getvalue(vars, &snmpval, objid_query->valueretrieval);
509 if (objid_query->array_output) {
513 if (st & SNMP_NUMERIC_KEYS) {
515 }
else if (st & SNMP_ORIGINAL_NAMES_AS_KEYS && st & SNMP_CMD_GET) {
518 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) {
520 objid_query->vars[
count].name_length = 0;
527 snprint_objid(buf2,
sizeof(buf2), vars->name, vars->name_length);
530 }
else if (st & SNMP_USE_SUFFIX_AS_KEYS && st & SNMP_CMD_WALK) {
531 snprint_objid(buf2,
sizeof(buf2), vars->name, vars->name_length);
532 if (rootlen <= vars->name_length && snmp_oid_compare(root, rootlen, vars->name, rootlen) == 0) {
540 buf2[
strlen(buf2) - 1] =
'\0';
544 snprint_objid(buf2,
sizeof(buf2), vars->name, vars->name_length);
553 if (st & SNMP_CMD_WALK) {
554 if (objid_query->oid_increasing_check && snmp_oid_compare(objid_query->vars[0].name, objid_query->vars[0].name_length, vars->name, vars->name_length) >= 0) {
555 snprint_objid(buf2,
sizeof(buf2), vars->name, vars->name_length);
556 php_snmp_error(
getThis(), PHP_SNMP_ERRNO_OID_NOT_INCREASING,
"Error: OID not increasing: %s", buf2);
559 memcpy((
char *)(objid_query->vars[0].name), (
char *)vars->name, vars->name_length *
sizeof(oid));
560 objid_query->vars[0].name_length = vars->name_length;
565 if (objid_query->offset < objid_query->count) {
569 if (st & SNMP_CMD_WALK && response->errstat == SNMP_ERR_TOOBIG && objid_query->max_repetitions > 1) {
570 objid_query->max_repetitions /= 2;
571 snmp_free_pdu(response);
576 for (
count=1, vars = response->variables;
577 vars &&
count != response->errindex;
578 vars = vars->next_variable,
count++);
580 if (st & (SNMP_CMD_GET | SNMP_CMD_GETNEXT) && response->errstat == SNMP_ERR_TOOBIG && objid_query->step > 1) {
581 objid_query->offset = ((objid_query->offset > objid_query->step) ? (objid_query->offset - objid_query->step) : 0 );
582 objid_query->step /= 2;
583 snmp_free_pdu(response);
588 snprint_objid(
buf,
sizeof(
buf), vars->name, vars->name_length);
589 php_snmp_error(
getThis(), PHP_SNMP_ERRNO_ERROR_IN_REPLY,
"Error in packet at '%s': %s",
buf, snmp_errstring(response->errstat));
591 php_snmp_error(
getThis(), PHP_SNMP_ERRNO_ERROR_IN_REPLY,
"Error in packet at %u object_id: %s", response->errindex, snmp_errstring(response->errstat));
593 if (st & (SNMP_CMD_GET | SNMP_CMD_GETNEXT)) {
594 if ((pdu = snmp_fix_pdu(response, ((st & SNMP_CMD_GET) ? SNMP_MSG_GET : SNMP_MSG_GETNEXT) )) !=
NULL) {
595 snmp_free_pdu(response);
599 snmp_free_pdu(response);
601 if (objid_query->array_output) {
607 }
else if (
status == STAT_TIMEOUT) {
608 php_snmp_error(
getThis(), PHP_SNMP_ERRNO_TIMEOUT,
"No response from %s", session->peername);
609 if (objid_query->array_output) {
616 php_snmp_error(
getThis(), PHP_SNMP_ERRNO_GENERIC,
"Fatal error: %s",
err);
618 if (objid_query->array_output) {
625 snmp_free_pdu(response);
632static void php_snmp_zend_string_release_from_char_pointer(
char *
ptr) {
635 zend_string_release(pptr);
639static void php_free_objid_query(
struct objid_query *objid_query,
HashTable* oid_ht,
const HashTable *value_ht,
int st) {
641 uint32_t
count = zend_hash_num_elements(oid_ht);
643 for (uint32_t i = 0; i <
count; i ++) {
644 snmpobjarg *
arg = &objid_query->vars[i];
649 php_snmp_zend_string_release_from_char_pointer(
arg->value);
651 php_snmp_zend_string_release_from_char_pointer(
arg->oid);
654 efree(objid_query->vars);
661static bool php_snmp_parse_oid(
666 uint32_t idx_type = 0, idx_value = 0;
669 objid_query->count = 0;
670 objid_query->array_output = (st & SNMP_CMD_WALK) != 0;
672 objid_query->vars = (snmpobjarg *)
emalloc(
sizeof(snmpobjarg));
673 objid_query->vars[objid_query->count].oid =
ZSTR_VAL(oid_str);
674 if (st & SNMP_CMD_SET) {
676 zend_type_error(
"Type must be of type string when object ID is a string");
677 efree(objid_query->vars);
681 zend_type_error(
"Value must be of type string when object ID is a string");
682 efree(objid_query->vars);
691 efree(objid_query->vars);
695 objid_query->vars[objid_query->count].type = *pptr;
696 objid_query->vars[objid_query->count].value =
ZSTR_VAL(value_str);
698 objid_query->count++;
700 if (zend_hash_num_elements(oid_ht) == 0) {
704 objid_query->vars = (snmpobjarg *)
safe_emalloc(
sizeof(snmpobjarg), zend_hash_num_elements(oid_ht), 0);
705 memset(objid_query->vars, 0,
sizeof(snmpobjarg) * zend_hash_num_elements(oid_ht));
706 objid_query->array_output = (st & SNMP_CMD_SET) == 0;
710 php_free_objid_query(objid_query, oid_ht, value_ht, st);
713 objid_query->vars[objid_query->count].oid =
ZSTR_VAL(tmp);
714 if (st & SNMP_CMD_SET) {
717 objid_query->vars[objid_query->count].type = *pptr;
718 }
else if (type_ht) {
720 while (idx_type < type_ht->nNumUsed) {
721 tmp_type = &type_ht->
arPacked[idx_type];
728 while (idx_type < type_ht->nNumUsed) {
729 tmp_type = &type_ht->
arData[idx_type].
val;
736 if (idx_type < type_ht->nNumUsed) {
739 php_free_objid_query(objid_query, oid_ht, value_ht, st);
744 zend_string_release(
type);
747 php_free_objid_query(objid_query, oid_ht, value_ht, st);
750 objid_query->vars[objid_query->count].type = ptype;
754 php_free_objid_query(objid_query, oid_ht, value_ht, st);
760 objid_query->vars[objid_query->count].value =
ZSTR_VAL(value_str);
761 }
else if (value_ht) {
763 while (idx_value < value_ht->nNumUsed) {
771 while (idx_value < value_ht->nNumUsed) {
779 if (idx_value < value_ht->nNumUsed) {
782 php_free_objid_query(objid_query, oid_ht, value_ht, st);
785 objid_query->vars[objid_query->count].value =
ZSTR_VAL(tmp);
789 php_free_objid_query(objid_query, oid_ht, value_ht, st);
794 objid_query->count++;
799 if (st & SNMP_CMD_WALK) {
800 if (objid_query->count > 1) {
801 php_snmp_error(
object, PHP_SNMP_ERRNO_OID_PARSING_ERROR,
"Multi OID walks are not supported!");
802 php_free_objid_query(objid_query, oid_ht, value_ht, st);
805 objid_query->vars[0].name_length = MAX_NAME_LEN;
806 if (
strlen(objid_query->vars[0].oid)) {
807 if (!snmp_parse_oid(objid_query->vars[0].oid, objid_query->vars[0].name, &(objid_query->vars[0].name_length))) {
808 php_snmp_error(
object, PHP_SNMP_ERRNO_OID_PARSING_ERROR,
"Invalid object identifier: %s", objid_query->vars[0].oid);
809 php_free_objid_query(objid_query, oid_ht, value_ht, st);
813 memmove((
char *)objid_query->vars[0].name, (
const char *)objid_mib,
sizeof(objid_mib));
814 objid_query->vars[0].name_length =
sizeof(objid_mib) /
sizeof(oid);
817 for (objid_query->offset = 0; objid_query->offset < objid_query->count; objid_query->offset++) {
818 objid_query->vars[objid_query->offset].name_length = MAX_OID_LEN;
819 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))) {
820 php_snmp_error(
object, PHP_SNMP_ERRNO_OID_PARSING_ERROR,
"Invalid object identifier: %s", objid_query->vars[objid_query->offset].oid);
821 php_free_objid_query(objid_query, oid_ht, value_ht, st);
826 objid_query->offset = 0;
827 objid_query->step = objid_query->count;
828 return (objid_query->count > 0);
835static bool netsnmp_session_init(php_snmp_session **session_p,
int version,
zend_string *hostname,
zend_string *community,
int timeout,
int retries)
837 php_snmp_session *session;
838 char *pptr, *host_ptr;
839 bool force_ipv6 =
false;
841 struct sockaddr **psal;
842 struct sockaddr **
res;
844 unsigned remote_port = SNMP_PORT;
846 *session_p = (php_snmp_session *)
emalloc(
sizeof(php_snmp_session));
847 session = *session_p;
848 memset(session, 0,
sizeof(php_snmp_session));
850 snmp_sess_init(session);
852 session->version = version;
854 session->peername =
emalloc(MAX_NAME_LEN);
857 host_ptr = session->peername;
860 if (*host_ptr ==
'[') {
863 if ((pptr =
strchr(host_ptr,
']'))) {
864 if (pptr[1] ==
':') {
865 remote_port = atoi(pptr + 2);
873 if ((pptr =
strchr(host_ptr,
':'))) {
874 remote_port = atoi(pptr + 1);
887 *(session->peername) =
'\0';
890 pptr = session->peername;
891#if defined(HAVE_GETADDRINFO) && defined(HAVE_IPV6)
892 if (force_ipv6 && (*res)->sa_family !=
AF_INET6) {
896 if ((*res)->sa_family ==
AF_INET6) {
897 strcpy(session->peername,
"udp6:[");
898 pptr = session->peername +
strlen(session->peername);
899 inet_ntop((*res)->sa_family, &(((
struct sockaddr_in6*)(*
res))->sin6_addr), pptr, MAX_NAME_LEN);
901 }
else if ((*res)->sa_family ==
AF_INET) {
902 inet_ntop((*res)->sa_family, &(((
struct sockaddr_in*)(*
res))->sin_addr), pptr, MAX_NAME_LEN);
911 if (
strlen(session->peername) == 0) {
920 if (remote_port != SNMP_PORT) {
921 size_t peername_length =
strlen(session->peername);
922 pptr = session->peername + peername_length;
923 snprintf(pptr, MAX_NAME_LEN - peername_length,
":%d", remote_port);
928 if (version == SNMP_VERSION_3) {
931 session->securityNameLen =
ZSTR_LEN(community);
933 session->authenticator =
NULL;
935 session->community_len =
ZSTR_LEN(community);
938 session->retries = retries;
939 session->timeout = timeout;
945static bool netsnmp_session_set_sec_level(
struct snmp_session *
s,
zend_string *level)
948 s->securityLevel = SNMP_SEC_LEVEL_NOAUTH;
950 s->securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;
952 s->securityLevel = SNMP_SEC_LEVEL_AUTHPRIV;
954 zend_value_error(
"Security level must be one of \"noAuthNoPriv\", \"authNoPriv\", or \"authPriv\"");
962static bool netsnmp_session_set_auth_protocol(
struct snmp_session *
s,
zend_string *prot)
966 s->securityAuthProto = usmHMACMD5AuthProtocol;
967 s->securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN;
973 s->securityAuthProto = usmHMACSHA1AuthProtocol;
974 s->securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN;
978#ifdef HAVE_SNMP_SHA256
980 s->securityAuthProto = usmHMAC192SHA256AuthProtocol;
981 s->securityAuthProtoLen =
sizeof(usmHMAC192SHA256AuthProtocol) /
sizeof(oid);
986#ifdef HAVE_SNMP_SHA512
988 s->securityAuthProto = usmHMAC384SHA512AuthProtocol;
989 s->securityAuthProtoLen =
sizeof(usmHMAC384SHA512AuthProtocol) /
sizeof(oid);
997#ifdef HAVE_SNMP_SHA256
1000#ifdef HAVE_SNMP_SHA512
1006 smart_string_0(&
err);
1014static bool netsnmp_session_set_sec_protocol(
struct snmp_session *
s,
zend_string *prot)
1016#ifndef NETSNMP_DISABLE_DES
1018 s->securityPrivProto = usmDESPrivProtocol;
1019 s->securityPrivProtoLen = USM_PRIV_PROTO_DES_LEN;
1027 s->securityPrivProto = usmAESPrivProtocol;
1028 s->securityPrivProtoLen = USM_PRIV_PROTO_AES_LEN;
1034# ifndef NETSNMP_DISABLE_DES
1035 zend_value_error(
"Security protocol must be one of \"DES\", \"AES128\", or \"AES\"");
1037 zend_value_error(
"Security protocol must be one of \"AES128\", or \"AES\"");
1040# ifndef NETSNMP_DISABLE_DES
1051static bool netsnmp_session_gen_auth_key(
struct snmp_session *
s,
zend_string *
pass)
1054 s->securityAuthKeyLen = USM_AUTH_KU_LEN;
1055 if ((snmp_errno = generate_Ku(
s->securityAuthProto,
s->securityAuthProtoLen,
1057 s->securityAuthKey, &(
s->securityAuthKeyLen)))) {
1066static bool netsnmp_session_gen_sec_key(
struct snmp_session *
s,
zend_string *
pass)
1070 s->securityPrivKeyLen = USM_PRIV_KU_LEN;
1071 if ((snmp_errno = generate_Ku(
s->securityAuthProto,
s->securityAuthProtoLen,
1073 s->securityPrivKey, &(
s->securityPrivKeyLen)))) {
1082static bool netsnmp_session_set_contextEngineID(
struct snmp_session *
s,
zend_string * contextEngineID)
1084 size_t ebuf_len = 32, eout_len = 0;
1085 uint8_t *ebuf = (uint8_t *)
emalloc(ebuf_len);
1087 if (!snmp_hex_to_binary(&ebuf, &ebuf_len, &eout_len, 1,
ZSTR_VAL(contextEngineID))) {
1094 if (
s->contextEngineID) {
1095 efree(
s->contextEngineID);
1098 s->contextEngineID = ebuf;
1099 s->contextEngineIDLen = eout_len;
1105static bool netsnmp_session_set_security(
struct snmp_session *session,
zend_string *sec_level,
1111 if (!netsnmp_session_set_sec_level(session, sec_level)) {
1116 if (session->securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || session->securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
1119 if (!netsnmp_session_set_auth_protocol(session, auth_protocol)) {
1125 if (!netsnmp_session_gen_auth_key(session, auth_passphrase)) {
1130 if (session->securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
1132 if (!netsnmp_session_set_sec_protocol(session, priv_protocol)) {
1138 if (!netsnmp_session_gen_sec_key(session, priv_passphrase)) {
1147 session->contextName =
ZSTR_VAL(contextName);
1148 session->contextNameLen =
ZSTR_LEN(contextName);
1152 if (contextEngineID &&
ZSTR_LEN(contextEngineID) && !netsnmp_session_set_contextEngineID(session, contextEngineID)) {
1173 bool use_orignames = 0, suffix_keys = 0;
1174 zend_long timeout = SNMP_DEFAULT_TIMEOUT;
1175 zend_long retries = SNMP_DEFAULT_RETRIES;
1176 struct objid_query objid_query;
1177 php_snmp_session *session;
1179 php_snmp_object *snmp_object;
1180 php_snmp_object glob_snmp_object;
1182 objid_query.max_repetitions = -1;
1183 objid_query.non_repeaters = 0;
1184 objid_query.valueretrieval = SNMP_G(valueretrieval);
1185 objid_query.oid_increasing_check =
true;
1187 if (session_less_mode) {
1188 if (version == SNMP_VERSION_3) {
1189 if (st & SNMP_CMD_SET) {
1225 if (st & SNMP_CMD_SET) {
1252 if (st & SNMP_CMD_SET) {
1258 }
else if (st & SNMP_CMD_WALK) {
1267 st |= SNMP_USE_SUFFIX_AS_KEYS;
1269 }
else if (st & SNMP_CMD_GET) {
1275 if (use_orignames) {
1276 st |= SNMP_ORIGINAL_NAMES_AS_KEYS;
1287 if (!php_snmp_parse_oid(
getThis(), st, &objid_query, oid_str, oid_ht, type_str, type_ht, value_str, value_ht)) {
1291 if (session_less_mode) {
1292 if (!netsnmp_session_init(&session, version, a1, a2, timeout, retries)) {
1293 php_free_objid_query(&objid_query, oid_ht, value_ht, st);
1294 netsnmp_session_free(&session);
1297 if (version == SNMP_VERSION_3 && !netsnmp_session_set_security(session, a3, a4, a5, a6, a7,
NULL,
NULL)) {
1298 php_free_objid_query(&objid_query, oid_ht, value_ht, st);
1299 netsnmp_session_free(&session);
1305 snmp_object = Z_SNMP_P(
object);
1306 session = snmp_object->session;
1309 php_free_objid_query(&objid_query, oid_ht, value_ht, st);
1313 if (snmp_object->max_oids > 0) {
1314 objid_query.step = snmp_object->max_oids;
1315 if (objid_query.max_repetitions < 0) {
1316 objid_query.max_repetitions = snmp_object->max_oids;
1319 objid_query.oid_increasing_check = snmp_object->oid_increasing_check;
1320 objid_query.valueretrieval = snmp_object->valueretrieval;
1321 glob_snmp_object.enum_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM);
1322 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, snmp_object->enum_print);
1323 glob_snmp_object.quick_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT);
1324 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, snmp_object->quick_print);
1325 glob_snmp_object.oid_output_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
1326 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, snmp_object->oid_output_format);
1329 if (objid_query.max_repetitions < 0) {
1330 objid_query.max_repetitions = 20;
1335 php_free_objid_query(&objid_query, oid_ht, value_ht, st);
1337 if (session_less_mode) {
1338 netsnmp_session_free(&session);
1340 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, glob_snmp_object.enum_print);
1341 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, glob_snmp_object.quick_print);
1342 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, glob_snmp_object.oid_output_format);
1389 RETURN_BOOL(netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT));
1402 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, (
int)a1);
1416 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, (
int) a1);
1431 case NETSNMP_OID_OUTPUT_SUFFIX:
1432 case NETSNMP_OID_OUTPUT_MODULE:
1433 case NETSNMP_OID_OUTPUT_FULL:
1434 case NETSNMP_OID_OUTPUT_NUMERIC:
1435 case NETSNMP_OID_OUTPUT_UCD:
1436 case NETSNMP_OID_OUTPUT_NONE:
1437 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, a1);
1526 SNMP_G(valueretrieval) = method;
1556 if (!read_mib(filename)) {
1568 php_snmp_object *snmp_object;
1571 zend_long timeout = SNMP_DEFAULT_TIMEOUT;
1572 zend_long retries = SNMP_DEFAULT_RETRIES;
1573 zend_long version = SNMP_DEFAULT_VERSION;
1575 snmp_object = Z_SNMP_P(
object);
1582 case SNMP_VERSION_1:
1583 case SNMP_VERSION_2c:
1584 case SNMP_VERSION_3:
1592 if (snmp_object->session) {
1593 netsnmp_session_free(&(snmp_object->session));
1596 if (!netsnmp_session_init(&(snmp_object->session), version, a1, a2, timeout, retries)) {
1599 snmp_object->max_oids = 0;
1600 snmp_object->valueretrieval = SNMP_G(valueretrieval);
1601 snmp_object->enum_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM);
1602 snmp_object->oid_output_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
1603 snmp_object->quick_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT);
1604 snmp_object->oid_increasing_check =
true;
1605 snmp_object->exceptions_enabled = 0;
1612 php_snmp_object *snmp_object;
1615 snmp_object = Z_SNMP_P(
object);
1621 netsnmp_session_free(&(snmp_object->session));
1658 php_snmp_object *snmp_object;
1662 snmp_object = Z_SNMP_P(
object);
1663 if (!snmp_object->session) {
1672 if (!netsnmp_session_set_security(snmp_object->session, a1, a2, a3, a4, a5, a6, a7)) {
1683 php_snmp_object *snmp_object;
1686 snmp_object = Z_SNMP_P(
object);
1699 php_snmp_object *snmp_object;
1702 snmp_object = Z_SNMP_P(
object);
1713void 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)
1715 php_snmp_prop_handler
p;
1718 p.name = (
char*)
name;
1719 p.name_length = name_length;
1720 p.read_func = (read_func) ? read_func :
NULL;
1721 p.write_func = (write_func) ? write_func :
NULL;
1723 zend_hash_add_mem(h, str, &
p,
sizeof(php_snmp_prop_handler));
1733 php_snmp_object *obj;
1734 php_snmp_prop_handler *hnd;
1737 obj = php_snmp_fetch_object(
object);
1738 hnd = zend_hash_find_ptr(&php_snmp_properties,
name);
1740 if (hnd && hnd->read_func) {
1741 ret = hnd->read_func(obj,
rv);
1758 php_snmp_object *obj = php_snmp_fetch_object(
object);
1759 php_snmp_prop_handler *hnd = zend_hash_find_ptr(&php_snmp_properties,
name);
1762 if (!hnd->write_func) {
1764 return &
EG(error_zval);
1774 return &
EG(error_zval);
1776 hnd->write_func(obj, &tmp);
1779 hnd->write_func(obj,
value);
1793 php_snmp_prop_handler *hnd;
1796 if ((hnd = zend_hash_find_ptr(&php_snmp_properties,
name)) !=
NULL) {
1797 switch (has_set_exists) {
1803 if (
value != &
EG(uninitialized_zval)) {
1811 if (
value != &
EG(uninitialized_zval)) {
1837 php_snmp_object *obj;
1838 php_snmp_prop_handler *hnd;
1843 obj = php_snmp_fetch_object(
object);
1847 if (!hnd->read_func || hnd->read_func(obj, &
rv) !=
SUCCESS) {
1853 return obj->zo.properties;
1859 php_snmp_prop_handler *hnd = zend_hash_find_ptr(&php_snmp_properties,
name);
1865 cache_slot[0] = cache_slot[1] = cache_slot[2] =
NULL;
1871static int php_snmp_read_info(php_snmp_object *snmp_object,
zval *
retval)
1877 if (snmp_object->session ==
NULL) {
1882 add_assoc_zval(
retval,
"hostname", &
val);
1885 add_assoc_zval(
retval,
"timeout", &
val);
1888 add_assoc_zval(
retval,
"retries", &
val);
1895static int php_snmp_read_max_oids(php_snmp_object *snmp_object,
zval *
retval)
1897 if (snmp_object->max_oids > 0) {
1906#define PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(name) \
1907 static int php_snmp_read_##name(php_snmp_object *snmp_object, zval *retval) \
1909 ZVAL_BOOL(retval, snmp_object->name); \
1913PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(oid_increasing_check)
1914PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(quick_print)
1915PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(enum_print)
1917#define PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(name) \
1918 static int php_snmp_read_##name(php_snmp_object *snmp_object, zval *retval) \
1920 ZVAL_LONG(retval, snmp_object->name); \
1924PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(valueretrieval)
1925PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(oid_output_format)
1926PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(exceptions_enabled)
1929static int php_snmp_write_max_oids(php_snmp_object *snmp_object,
zval *newval)
1934 snmp_object->max_oids = 0;
1938 lval = zval_get_long(newval);
1944 snmp_object->max_oids = lval;
1951static int php_snmp_write_valueretrieval(php_snmp_object *snmp_object,
zval *newval)
1956 snmp_object->valueretrieval = lval;
1958 zend_value_error(
"SNMP retrieval method must be a bitmask of SNMP_VALUE_LIBRARY, SNMP_VALUE_PLAIN, and SNMP_VALUE_OBJECT");
1966#define PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(name) \
1967static int php_snmp_write_##name(php_snmp_object *snmp_object, zval *newval) \
1970 ZVAL_COPY(&ztmp, newval); \
1971 convert_to_boolean(&ztmp); \
1974 snmp_object->name = Z_TYPE_P(newval) == IS_TRUE? 1 : 0; \
1979PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(quick_print)
1980PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(enum_print)
1981PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(oid_increasing_check)
1984static int php_snmp_write_oid_output_format(php_snmp_object *snmp_object,
zval *newval)
1989 case NETSNMP_OID_OUTPUT_SUFFIX:
1990 case NETSNMP_OID_OUTPUT_MODULE:
1991 case NETSNMP_OID_OUTPUT_FULL:
1992 case NETSNMP_OID_OUTPUT_NUMERIC:
1993 case NETSNMP_OID_OUTPUT_UCD:
1994 case NETSNMP_OID_OUTPUT_NONE:
1995 snmp_object->oid_output_format = lval;
1998 zend_value_error(
"SNMP output print format must be an SNMP_OID_OUTPUT_* constant");
2005static int php_snmp_write_exceptions_enabled(php_snmp_object *snmp_object,
zval *newval)
2009 snmp_object->exceptions_enabled = zval_get_long(newval);
2015static void free_php_snmp_properties(
zval *el)
2021#define PHP_SNMP_PROPERTY_ENTRY_RECORD(name) \
2022 { "" #name "", sizeof("" #name "") - 1, php_snmp_read_##name, php_snmp_write_##name }
2024#define PHP_SNMP_READONLY_PROPERTY_ENTRY_RECORD(name) \
2025 { "" #name "", sizeof("" #name "") - 1, php_snmp_read_##name, NULL }
2027const php_snmp_prop_handler php_snmp_property_entries[] = {
2028 PHP_SNMP_READONLY_PROPERTY_ENTRY_RECORD(info),
2029 PHP_SNMP_PROPERTY_ENTRY_RECORD(max_oids),
2030 PHP_SNMP_PROPERTY_ENTRY_RECORD(valueretrieval),
2031 PHP_SNMP_PROPERTY_ENTRY_RECORD(quick_print),
2032 PHP_SNMP_PROPERTY_ENTRY_RECORD(enum_print),
2033 PHP_SNMP_PROPERTY_ENTRY_RECORD(oid_output_format),
2034 PHP_SNMP_PROPERTY_ENTRY_RECORD(oid_increasing_check),
2035 PHP_SNMP_PROPERTY_ENTRY_RECORD(exceptions_enabled),
2043 netsnmp_log_handler *logh;
2045 init_snmp(
"snmpapp");
2049#ifdef NETSNMP_DS_LIB_DONT_PERSIST_STATE
2051 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_PERSIST_STATE, 1);
2055 shutdown_snmp_logging();
2056 logh = netsnmp_register_loghandler(NETSNMP_LOGHANDLER_NONE,
LOG_ERR);
2062 php_snmp_object_handlers.
read_property = php_snmp_read_property;
2063 php_snmp_object_handlers.
write_property = php_snmp_write_property;
2065 php_snmp_object_handlers.
has_property = php_snmp_has_property;
2066 php_snmp_object_handlers.
get_properties = php_snmp_get_properties;
2067 php_snmp_object_handlers.
get_gc = php_snmp_get_gc;
2070 php_snmp_ce = register_class_SNMP();
2075 php_snmp_object_handlers.
free_obj = php_snmp_object_free_storage;
2079 PHP_SNMP_ADD_PROPERTIES(&php_snmp_properties, php_snmp_property_entries);
2084 register_snmp_symbols(module_number);
2093 snmp_shutdown(
"snmpapp");
count(Countable|array $value, int $mode=COUNT_NORMAL)
strchr(string $haystack, string $needle, bool $before_needle=false)
memset(ptr, 0, type->size)
zend_ffi_ctype_name_buf buf
#define pass(a, b, c, mul)
foreach($dp as $el) foreach( $dp as $el) if( $pass2< 2) echo ""
PHPAPI ZEND_COLD void php_error_docref(const char *docref, int type, const char *format,...)
PHPAPI ZEND_COLD void php_verror(const char *docref, const char *params, int type, const char *format, va_list args)
inet_ntop(AF_INET, addr, addr_str, sizeof(addr_str))
PHPAPI void php_network_freeaddresses(struct sockaddr **sal)
PHPAPI int php_network_getaddresses(const char *host, int socktype, struct sockaddr ***sal, zend_string **error_string)
php_info_print_table_start()
php_info_print_table_row(2, "PDO Driver for Firebird", "enabled")
php_info_print_table_end()
#define PHP_MSHUTDOWN_FUNCTION
#define PHP_MINIT_FUNCTION
#define PHP_MINFO_FUNCTION
#define PHP_GINIT_FUNCTION
#define PHP_MODULE_GLOBALS
unsigned char key[REFLECTION_KEY_LEN]
snmp_set_quick_print(bool $enable)
snmp_set_oid_output_format(int $format)
snmpgetnext(string $hostname, string $community, array|string $object_id, int $timeout=-1, int $retries=-1)
snmp2_set(string $hostname, string $community, array|string $object_id, array|string $type, array|string $value, int $timeout=-1, int $retries=-1)
snmp_set_valueretrieval(int $method)
snmp2_getnext(string $hostname, string $community, array|string $object_id, int $timeout=-1, int $retries=-1)
snmp3_walk(string $hostname, string $security_name, string $security_level, string $auth_protocol, string $auth_passphrase, string $privacy_protocol, string $privacy_passphrase, array|string $object_id, int $timeout=-1, int $retries=-1)
snmpwalk(string $hostname, string $community, array|string $object_id, int $timeout=-1, int $retries=-1)
snmp3_getnext(string $hostname, string $security_name, string $security_level, string $auth_protocol, string $auth_passphrase, string $privacy_protocol, string $privacy_passphrase, array|string $object_id, int $timeout=-1, int $retries=-1)
snmp_read_mib(string $filename)
snmp3_real_walk(string $hostname, string $security_name, string $security_level, string $auth_protocol, string $auth_passphrase, string $privacy_protocol, string $privacy_passphrase, array|string $object_id, int $timeout=-1, int $retries=-1)
snmp2_real_walk(string $hostname, string $community, array|string $object_id, int $timeout=-1, int $retries=-1)
snmp2_get(string $hostname, string $community, array|string $object_id, int $timeout=-1, int $retries=-1)
snmp_set_enum_print(bool $enable)
snmp3_set(string $hostname, string $security_name, string $security_level, string $auth_protocol, string $auth_passphrase, string $privacy_protocol, string $privacy_passphrase, array|string $object_id, array|string $type, array|string $value, int $timeout=-1, int $retries=-1)
snmp_get_valueretrieval()
snmpget(string $hostname, string $community, array|string $object_id, int $timeout=-1, int $retries=-1)
snmp3_get(string $hostname, string $security_name, string $security_level, string $auth_protocol, string $auth_passphrase, string $privacy_protocol, string $privacy_passphrase, array|string $object_id, int $timeout=-1, int $retries=-1)
snmp2_walk(string $hostname, string $community, array|string $object_id, int $timeout=-1, int $retries=-1)
snmpset(string $hostname, string $community, array|string $object_id, array|string $type, array|string $value, int $timeout=-1, int $retries=-1)
snmprealwalk(string $hostname, string $community, array|string $object_id, int $timeout=-1, int $retries=-1)
PHPAPI zend_class_entry * spl_ce_RuntimeException
zend_object *(* create_object)(zend_class_entry *class_type)
const zend_object_handlers * default_object_handlers
zend_object_write_property_t write_property
zend_object_get_property_ptr_ptr_t get_property_ptr_ptr
zend_object_free_obj_t free_obj
zend_object_get_gc_t get_gc
zend_object_read_property_t read_property
zend_object_has_property_t has_property
zend_object_clone_obj_t clone_obj
zend_object_get_properties_t get_properties
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format,...)
ZEND_API ZEND_COLD void zend_type_error(const char *format,...)
ZEND_API ZEND_COLD void zend_value_error(const char *format,...)
#define INTERNAL_FUNCTION_PARAMETERS
#define INTERNAL_FUNCTION_PARAM_PASSTHRU
ZEND_API zend_result zend_parse_parameters(uint32_t num_args, const char *type_spec,...)
ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type)
ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format,...)
ZEND_API void object_init(zval *arg)
#define ZEND_PARSE_PARAMETERS_END()
#define ZVAL_STRING(z, s)
#define ZEND_DECLARE_MODULE_GLOBALS(module_name)
#define ZEND_GET_MODULE(name)
#define zend_parse_parameters_none()
#define Z_PARAM_STR(dest)
#define ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args)
#define Z_PARAM_ARRAY_HT_OR_STR(dest_ht, dest_str)
#define Z_PARAM_LONG(dest)
#define Z_PARAM_BOOL(dest)
#define ZVAL_STRINGL(z, s, l)
#define pefree(ptr, persistent)
#define erealloc(ptr, size)
#define safe_emalloc(nmemb, size, offset)
zend_string_release_ex(func->internal_function.function_name, 0)
struct _zend_property_info zend_property_info
#define ZEND_CALL_USES_STRICT_TYPES(call)
ZEND_API ZEND_COLD zend_object * zend_throw_exception_ex(zend_class_entry *exception_ce, zend_long code, const char *format,...)
ZEND_API bool zend_never_inline zend_verify_property_type(const zend_property_info *info, zval *property, bool strict)
ZEND_API void ZEND_FASTCALL zend_hash_destroy(HashTable *ht)
ZEND_API zval *ZEND_FASTCALL zend_hash_update(HashTable *ht, zend_string *key, zval *pData)
#define zend_hash_init(ht, nSize, pHashFunction, pDestructor, persistent)
#define ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(ht, _key, _ptr)
#define ZEND_HASH_FOREACH_END()
#define ZEND_HASH_FOREACH_VAL(ht, _val)
struct _zend_string zend_string
struct _zend_module_dep zend_module_dep
struct _zend_module_entry zend_module_entry
#define ZEND_MOD_REQUIRED(name)
#define STANDARD_MODULE_PROPERTIES_EX
#define STANDARD_MODULE_HEADER_EX
ZEND_API zval * zend_std_get_property_ptr_ptr(zend_object *zobj, zend_string *name, int type, void **cache_slot)
ZEND_API HashTable * zend_std_get_properties(zend_object *zobj)
ZEND_API int zend_std_has_property(zend_object *zobj, zend_string *name, int has_set_exists, void **cache_slot)
ZEND_API const zend_object_handlers std_object_handlers
ZEND_API zval * zend_std_write_property(zend_object *zobj, zend_string *name, zval *value, void **cache_slot)
ZEND_API zend_property_info * zend_get_property_info(const zend_class_entry *ce, zend_string *member, int silent)
ZEND_API zval * zend_std_read_property(zend_object *zobj, zend_string *name, int type, void **cache_slot, zval *rv)
#define ZEND_PROPERTY_ISSET
#define ZEND_PROPERTY_EXISTS
ZEND_API void ZEND_FASTCALL zend_object_std_init(zend_object *object, zend_class_entry *ce)
ZEND_API void zend_object_std_dtor(zend_object *object)
ZEND_API void ZEND_FASTCALL convert_to_boolean(zval *op)
ZEND_API void zend_reset_lc_ctype_locale(void)
#define XtOffsetOf(s_type, field)
struct _zend_class_entry zend_class_entry
struct _zend_object zend_object
#define smart_string_free(s)
#define smart_string_appends(str, src)
ZEND_API zend_string_init_interned_func_t zend_string_init_interned
#define zend_string_equals_literal_ci(str, c)
struct _zend_array HashTable
#define ZEND_TYPE_IS_SET(t)
struct _zend_object_handlers zend_object_handlers
#define ZVAL_COPY_VALUE(z, v)
ZEND_API void zval_ptr_dtor(zval *zval_ptr)