Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion ext/snmp/snmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,7 @@ PHP_FUNCTION(snmp_set_oid_output_format)
RETURN_THROWS();
}

switch((int) a1) {
switch(a1) {
case NETSNMP_OID_OUTPUT_SUFFIX:
case NETSNMP_OID_OUTPUT_MODULE:
case NETSNMP_OID_OUTPUT_FULL:
Expand All @@ -1374,6 +1374,28 @@ PHP_FUNCTION(snmp_set_oid_output_format)
}
/* }}} */

/* {{{ Set the string output format. */
PHP_FUNCTION(snmp_set_string_output_format)
{
zend_long a1;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &a1) == FAILURE) {
RETURN_THROWS();
}

switch(a1) {
case NETSNMP_STRING_OUTPUT_GUESS:
case NETSNMP_STRING_OUTPUT_ASCII:
case NETSNMP_STRING_OUTPUT_HEX:
netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_STRING_OUTPUT_FORMAT, a1);
RETURN_TRUE;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This return value doesn't make much sense to me. I think it would be better to make this a void function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment about snmp_set_oid_value_format() above.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, shall we also modify the snmp_set_oid_value_format()?

default:
zend_argument_value_error(1, "must be an SNMP_STRING_OUTPUT_* constant");
RETURN_THROWS();
}
}
/* }}} */

/* {{{ Fetch a SNMP object */
PHP_FUNCTION(snmp2_get)
{
Expand Down Expand Up @@ -2003,6 +2025,10 @@ PHP_MINIT_FUNCTION(snmp)
REGISTER_LONG_CONSTANT("SNMP_OID_OUTPUT_UCD", NETSNMP_OID_OUTPUT_UCD, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SNMP_OID_OUTPUT_NONE", NETSNMP_OID_OUTPUT_NONE, CONST_CS | CONST_PERSISTENT);

REGISTER_LONG_CONSTANT("SNMP_STRING_OUTPUT_GUESS", NETSNMP_STRING_OUTPUT_GUESS, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SNMP_STRING_OUTPUT_ASCII", NETSNMP_STRING_OUTPUT_ASCII, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SNMP_STRING_OUTPUT_HEX", NETSNMP_STRING_OUTPUT_HEX, CONST_CS | CONST_PERSISTENT);

REGISTER_LONG_CONSTANT("SNMP_VALUE_LIBRARY", SNMP_VALUE_LIBRARY, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SNMP_VALUE_PLAIN", SNMP_VALUE_PLAIN, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SNMP_VALUE_OBJECT", SNMP_VALUE_OBJECT, CONST_CS | CONST_PERSISTENT);
Expand Down
2 changes: 2 additions & 0 deletions ext/snmp/snmp.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ function snmp_set_enum_print(bool $enable): bool {}

function snmp_set_oid_output_format(int $format): bool {}

function snmp_set_string_output_format(int $format): bool {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, now I at least see why you picked the bool return value... I would still go for : void here, as the : bool is really a backwards-compatibility leftover from a time where these functions could return both true and false.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking your word for it, I'm the old new guy ;)


/** @alias snmp_set_oid_output_format */
function snmp_set_oid_numeric_print(int $format): bool {}

Expand Down
6 changes: 6 additions & 0 deletions ext/snmp/snmp_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ ZEND_END_ARG_INFO()

#define arginfo_snmp_set_oid_numeric_print arginfo_snmp_set_oid_output_format

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_snmp_set_string_output_format, 0, 1, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, format, IS_LONG, 0)
ZEND_END_ARG_INFO()

#define arginfo_snmp2_get arginfo_snmpget

#define arginfo_snmp2_getnext arginfo_snmpget
Expand Down Expand Up @@ -171,6 +175,7 @@ ZEND_FUNCTION(snmp_get_quick_print);
ZEND_FUNCTION(snmp_set_quick_print);
ZEND_FUNCTION(snmp_set_enum_print);
ZEND_FUNCTION(snmp_set_oid_output_format);
ZEND_FUNCTION(snmp_set_string_output_format);
ZEND_FUNCTION(snmp2_get);
ZEND_FUNCTION(snmp2_getnext);
ZEND_FUNCTION(snmp2_walk);
Expand Down Expand Up @@ -207,6 +212,7 @@ static const zend_function_entry ext_functions[] = {
ZEND_FE(snmp_set_enum_print, arginfo_snmp_set_enum_print)
ZEND_FE(snmp_set_oid_output_format, arginfo_snmp_set_oid_output_format)
ZEND_FALIAS(snmp_set_oid_numeric_print, snmp_set_oid_output_format, arginfo_snmp_set_oid_numeric_print)
ZEND_FE(snmp_set_string_output_format, arginfo_snmp_set_string_output_format)
ZEND_FE(snmp2_get, arginfo_snmp2_get)
ZEND_FE(snmp2_getnext, arginfo_snmp2_getnext)
ZEND_FE(snmp2_walk, arginfo_snmp2_walk)
Expand Down
46 changes: 24 additions & 22 deletions ext/snmp/tests/snmp_include.inc
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
<?php

/*
By default tests will try to access SNMP agent @ '127.0.0.1:161' and will use 'public' community for read
requests and 'private' community for write requests.
Default timeout is 1000ms and there will be one request performed.
*/
/**
* By default tests will try to access SNMP agent @ '127.0.0.1:161'
* and will use 'public' community for read requests
* and will use 'private' community for write requests.
* Default timeout is 1000ms and there will be one request performed.
*/

$hostname4 = getenv('SNMP_HOSTNAME') ?: '127.0.0.1';
$hostname6 = getenv('SNMP_HOSTNAME6') ?: '::1';
$port = getenv('SNMP_PORT') ?: '161';
$hostname = "$hostname4:$port";
$hostname4 = getenv('SNMP_HOSTNAME') ?: '127.0.0.1';
$hostname6 = getenv('SNMP_HOSTNAME6') ?: '::1';
$port = getenv('SNMP_PORT') ?: '161';

$hostname = "$hostname4:$port";
$hostname6_port = "[$hostname6]:$port";
$community = getenv('SNMP_COMMUNITY') ?: 'public';
$communityWrite = getenv('SNMP_COMMUNITY_WRITE')?:'private';

$timeout = getenv('SNMP_TIMEOUT') ?: -1;
$retries = getenv('SNMP_RETRIES') ?: 1;
$community = getenv('SNMP_COMMUNITY') ?: 'public';
$communityWrite = getenv('SNMP_COMMUNITY_WRITE') ?: 'private';

$timeout = getenv('SNMP_TIMEOUT') ?: -1;
$retries = getenv('SNMP_RETRIES') ?: 1;

if (stristr(PHP_OS, "FreeBSD")) {
$mibdir = getenv('SNMP_MIBDIR') ?: "/usr/local/share/snmp/mibs";
if (stristr(PHP_OS, 'FreeBSD')) {
$mibdir = getenv('SNMP_MIBDIR') ?: '/usr/local/share/snmp/mibs';
} else {
$mibdir = getenv('SNMP_MIBDIR') ?: "/usr/share/snmp/mibs";
$mibdir = getenv('SNMP_MIBDIR') ?: '/usr/share/snmp/mibs';
}


$user_noauth = getenv('SNMP_USER_NOAUTH') ?: 'noAuthUser';
$user_auth_prefix = getenv('SNMP_USER_PREFIX') ?: 'admin';
$rwuser = getenv('SNMP_RWUSER') ?: ($user_auth_prefix . 'MD5AES');
$auth_pass = getenv('SNMP_AUTH_PASS') ?: 'test1234';
$priv_pass = getenv('SNMP_PRIV_PASS') ?: 'test1234';
$user_noauth = getenv('SNMP_USER_NOAUTH') ?: 'noAuthUser';
$user_auth_prefix = getenv('SNMP_USER_PREFIX') ?: 'admin';
$rwuser = getenv('SNMP_RWUSER') ?: ($user_auth_prefix . 'MD5AES');
$auth_pass = getenv('SNMP_AUTH_PASS') ?: 'test1234';
$priv_pass = getenv('SNMP_PRIV_PASS') ?: 'test1234';
30 changes: 30 additions & 0 deletions ext/snmp/tests/snmp_set_string_output_format.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
Function snmp_set_string_output_format
--SKIPIF--
<?php
require_once(__DIR__.'/skipif.inc');
if (!function_exists('snmp_set_string_output_format')) die('skip This function is only available if using NET_SNMP');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, a separate check for the function is not needed here, just the skipif.inc include is enough. If the extension is loaded, then the function will always exist.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said before, I copied whole cloth from a similar function. Give it some thought as making that decision here should/will impact others as well.

?>
--FILE--
<?php
require_once(__DIR__.'/snmp_include.inc');

echo "Checking error handling\n";
try {
var_dump(snmp_set_string_output_format(123));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

echo "Checking working\n";
var_dump(snmp_set_string_output_format(SNMP_STRING_OUTPUT_GUESS));
var_dump(snmp_set_string_output_format(SNMP_STRING_OUTPUT_ASCII));
var_dump(snmp_set_string_output_format(SNMP_STRING_OUTPUT_HEX));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also fetch something here to see that the output format actually has some effect.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, the trick is, it'll be different on each system. I'll guess we can request that the sysLocation be set in the snmpd.conf and that way it'll always succeed. Add that to the test configuration.

?>
--EXPECT--
Checking error handling
snmp_set_string_output_format(): Argument #1 ($format) must be an SNMP_STRING_OUTPUT_* constant
Checking working
bool(true)
bool(true)
bool(true)