mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
IP::fromHexString handle ascii (#15504)
* IP::fromHexString handle ascii Some bad MIBs convert the hex to ascii on display. Attempt to decode that if the situation is detected. fixes #15501 * add ascii tests
This commit is contained in:
@@ -54,6 +54,12 @@ abstract class IP
|
|||||||
|
|
||||||
$hex = str_replace([':', '.'], '', $hex);
|
$hex = str_replace([':', '.'], '', $hex);
|
||||||
|
|
||||||
|
// check if hex was incorrectly converted to ascii
|
||||||
|
$len = strlen($hex);
|
||||||
|
if (($len == 4 || $len == 16) && preg_match('/[^0-9a-fA-F]/', $hex)) {
|
||||||
|
$hex = StringHelpers::asciiToHex($hex);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (strlen($hex) == 8) {
|
if (strlen($hex) == 8) {
|
||||||
return new IPv4(long2ip(hexdec($hex)));
|
return new IPv4(long2ip(hexdec($hex)));
|
||||||
|
@@ -162,4 +162,15 @@ class StringHelpers
|
|||||||
{
|
{
|
||||||
return $var === null || is_scalar($var) || (is_object($var) && method_exists($var, '__toString'));
|
return $var === null || is_scalar($var) || (is_object($var) && method_exists($var, '__toString'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function asciiToHex(string $ascii, string $seperator = ''): string
|
||||||
|
{
|
||||||
|
$hex = [];
|
||||||
|
$len = strlen($ascii);
|
||||||
|
for ($i = 0; $i < $len; $i++) {
|
||||||
|
$hex[] = str_pad(strtoupper(dechex(ord($ascii[$i]))), 2, '0', STR_PAD_LEFT);
|
||||||
|
}
|
||||||
|
|
||||||
|
return implode($seperator, $hex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -610,14 +610,7 @@ function report_this($message)
|
|||||||
function hytera_h2f($number, $nd)
|
function hytera_h2f($number, $nd)
|
||||||
{
|
{
|
||||||
if (strlen(str_replace(' ', '', $number)) == 4) {
|
if (strlen(str_replace(' ', '', $number)) == 4) {
|
||||||
$hex = '';
|
$number = \LibreNMS\Util\StringHelpers::asciiToHex($number, ' ');
|
||||||
for ($i = 0; $i < strlen($number); $i++) {
|
|
||||||
$byte = strtoupper(dechex(ord($number[$i])));
|
|
||||||
$byte = str_repeat('0', 2 - strlen($byte)) . $byte;
|
|
||||||
$hex .= $byte . ' ';
|
|
||||||
}
|
|
||||||
$number = $hex;
|
|
||||||
unset($hex);
|
|
||||||
}
|
}
|
||||||
$r = '';
|
$r = '';
|
||||||
$y = explode(' ', $number);
|
$y = explode(' ', $number);
|
||||||
|
@@ -93,6 +93,7 @@ class IpTest extends TestCase
|
|||||||
$this->assertEquals('192.168.1.254', IP::fromHexString('c0 a8 01 fe '));
|
$this->assertEquals('192.168.1.254', IP::fromHexString('c0 a8 01 fe '));
|
||||||
$this->assertEquals('192.168.1.254', IP::fromHexString('"c0 a8 01 fe"'));
|
$this->assertEquals('192.168.1.254', IP::fromHexString('"c0 a8 01 fe"'));
|
||||||
$this->assertEquals('192.168.1.254', IP::fromHexString('192.168.1.254'));
|
$this->assertEquals('192.168.1.254', IP::fromHexString('192.168.1.254'));
|
||||||
|
$this->assertEquals('62.40.125.125', IP::fromHexString('>(}}')); // stupid ascii encoded
|
||||||
|
|
||||||
$this->assertEquals('2001:db8::2:1', IP::fromHexString('2001:db8::2:1'));
|
$this->assertEquals('2001:db8::2:1', IP::fromHexString('2001:db8::2:1'));
|
||||||
$this->assertEquals('2001:db8::2:1', IP::fromHexString('20 01 0d b8 00 00 00 00 00 00 00 00 00 02 00 01'));
|
$this->assertEquals('2001:db8::2:1', IP::fromHexString('20 01 0d b8 00 00 00 00 00 00 00 00 00 02 00 01'));
|
||||||
@@ -100,6 +101,7 @@ class IpTest extends TestCase
|
|||||||
$this->assertEquals('2001:db8::2:1', IP::fromHexString('"20:01:0d:b8:00:00:00:00:00:00:00:00:00:02:00:01"'));
|
$this->assertEquals('2001:db8::2:1', IP::fromHexString('"20:01:0d:b8:00:00:00:00:00:00:00:00:00:02:00:01"'));
|
||||||
$this->assertEquals('2001:db8::2:1', IP::fromHexString('"20.01.0d.b8.00.00.00.00.00.00.00.00.00.02.00.01"'));
|
$this->assertEquals('2001:db8::2:1', IP::fromHexString('"20.01.0d.b8.00.00.00.00.00.00.00.00.00.02.00.01"'));
|
||||||
$this->assertEquals('2001:db8::2:1', IP::fromHexString('20010db8000000000000000000020001'));
|
$this->assertEquals('2001:db8::2:1', IP::fromHexString('20010db8000000000000000000020001'));
|
||||||
|
$this->assertEquals('3e28:7d7d:3e28:7d7d:3e28:7d7d:3e28:7d7d', IP::fromHexString('>(}}>(}}>(}}>(}}')); // stupid ascii encoded
|
||||||
|
|
||||||
$this->assertEquals('::', IP::fromHexString('00000000000000000000000000000000'));
|
$this->assertEquals('::', IP::fromHexString('00000000000000000000000000000000'));
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user