Skip to content

Conversation

TheWitness
Copy link

This fix adds minor functionality to the php-snmp module to allow the caller to set the desired string output format for strings. The default SNMP behavior will check and if it finds that a character within a string that does not match a visible ASCII character, it will convert the entire string to a HEX-STRING: instead.

However, there are cases where the HEX-STRING is preferred even when the hex data within the string map to a ASCII character, say for example a MAC address that happens to fall 100% in the visible ASCII range.

This minor patch also includes a test to verify that the settings are working.

If the php team desires, I can backport this as applicable. Thanks!

- This function provides support for the SNMP Value Output Format when handling strings
- There are three options GUESS, HEX, and ASCII
- This functionality is available in the snmp tools command set, but not in PHP
Copy link
Member

@nikic nikic left a comment

Choose a reason for hiding this comment

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

I know nothing about SNMP, but this looks like a reasonable addition to me.

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()?


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 ;)

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.

--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.

@TheWitness
Copy link
Author

I just developed the test case and tested, and upon further inspection, the string output functions need quite a bit of work. I won't close the pull request, but it's not ready. Just debating ripping and replacing it vs. retrofitting what's in php-snmp at the moment.

@cmb69
Copy link
Member

cmb69 commented Aug 18, 2021

@TheWitness, I'm going to convert this PR to draft; please fix the merge conflicts and improve as necessary when you have time.

@cmb69 cmb69 marked this pull request as draft August 18, 2021 10:39
@TheWitness
Copy link
Author

Yea, that's fine. Unless there are some string formatting functions that I can find, I'll have to borrow code whole cloth.

@github-actions
Copy link

github-actions bot commented Feb 5, 2022

There has not been any recent activity in this PR. It will automatically be closed in 7 days if no further action is taken.

@github-actions github-actions bot added the Stale label Feb 5, 2022
@TheWitness
Copy link
Author

Let's close for now. I hope to take this forward, but have been busy with the day job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants