Skip to content
Merged
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
52 changes: 26 additions & 26 deletions lib/mactrack_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2317,46 +2317,46 @@ function xform_stripped_oid($oid, &$device, $snmp_readstring = '', $hex = false)

/**
* Normalizes an IP address value returned by SNMP (which may be a
* binary-encoded string, or prefixed with the literal text "Network
* Address:") into standard dotted-decimal notation.
* binary-encoded string, hexadecimal octets, or prefixed with the literal
* text "Network Address:") into standard IPv4 or IPv6 text.
*
* @param string $ip_address The raw IP address value from SNMP.
* @param mixed $ip_address The raw IP address value from SNMP.
*
* @return string The normalized dotted-decimal IP address.
* @return string The normalized IP address.
*/
function xform_net_address($ip_address): string {
$ip_address = trim($ip_address);
$ip_address = (string) $ip_address;
$text = trim(str_replace('Network Address:', '', $ip_address));
$length = strlen($ip_address);

if (substr_count($ip_address, 'Network Address:')) {
$ip_address = trim(str_replace('Network Address:', '', $ip_address));
// A raw 4 or 16 byte OctetString is binary, and any of its bytes may be
// whitespace, so it is converted untrimmed. A 16 character value can also
// be IPv6 text, or the bare "Network Address:" prefix.
if (($length == 4 || $length == 16) && strpos($ip_address, 'Network Address:') === false && filter_var($text, FILTER_VALIDATE_IP) === false) {
return (string) inet_ntop($ip_address);
}

// Handle the binary format first
$length = strlen($ip_address);
$ip_address = $text;

if ($length == 4 || $length == 16) {
return (string) inet_ntop(pack('A' . $length, $ip_address));
} else {
// Adjust for HEX IP in form "0A 09 15 72"
$ip_address = str_replace(' ', ':', $ip_address);

if (substr_count($ip_address, ':') != 0) {
if (strlen($ip_address) > 11) {
// ipv6, don't alter
} else {
$newaddr = '';
$address = explode(':', $ip_address);
// valid IPv4 or IPv6 text, including compressed IPv6, is already done
if (filter_var($ip_address, FILTER_VALIDATE_IP) !== false) {
return $ip_address;
}

foreach ($address as $index => $part) {
$newaddr .= ($index == 0 ? '' : '.') . hexdec($part);
}
// hexadecimal IPv4 octets in the form "0A 09 15 72" or "0A:09:15:72"
$octets = explode(':', str_replace(' ', ':', $ip_address));

$ip_address = $newaddr;
if (count($octets) == 4) {
foreach ($octets as $octet) {
if (strlen($octet) < 1 || strlen($octet) > 2 || !ctype_xdigit($octet)) {
return $ip_address;
}
}

return $ip_address;
return implode('.', array_map('hexdec', $octets));
}

return $ip_address;
}

/** xform_mac_address - This function will take a variable that is either formatted as
Expand Down
71 changes: 71 additions & 0 deletions tests/Unit/XformNetAddressTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/

/*
* xform_net_address() turns the address forms SNMP agents return into IPv4
* or IPv6 text. Raw binary values are converted before anything is trimmed
* (#356), valid IP text is returned untouched (#355), and a missing value is
* an empty string rather than a trim(null) deprecation (#351).
*/

require_once dirname(__DIR__, 2) . '/lib/mactrack_functions.php';

test('IP text is returned unchanged', function ($input) {
expect(xform_net_address($input))->toBe($input);
})->with([
'192.0.2.10',
'2001:db8::1',
'fe80::1',
'::1',
'2001:db8:85a3::1',
'2001:db8:0:0:0:0:0:1',
]);

test('raw binary addresses are converted, whatever their last byte', function ($hex, $expected) {
expect(xform_net_address(hex2bin($hex)))->toBe($expected);
})->with([
['c0a8010a', '192.168.1.10'],
['c0a80120', '192.168.1.32'],
['0a000009', '10.0.0.9'],
['c0a80100', '192.168.1.0'],
['20010db8000000000000000000000001', '2001:db8::1'],
['20010db800000000000000000000000a', '2001:db8::a'],
]);

test('hexadecimal IPv4 octets are converted', function ($input, $expected) {
expect(xform_net_address($input))->toBe($expected);
})->with([
['0A 09 15 72', '10.9.21.114'],
['0A:09:15:72', '10.9.21.114'],
['a:9:15:72', '10.9.21.114'],
]);

test('the Network Address prefix and surrounding whitespace are removed', function ($input, $expected) {
expect(xform_net_address($input))->toBe($expected);
})->with([
['Network Address: 192.0.2.10', '192.0.2.10'],
['Network Address:0A:09:15:72', '10.9.21.114'],
[" 192.0.2.10\n", '192.0.2.10'],
['Network Address:', ''],
]);

test('a missing value is an empty string', function ($input) {
expect(xform_net_address($input))->toBe('');
})->with([[null], [''], [' ']]);

test('other text is left as it is', function ($input) {
expect(xform_net_address($input))->toBe($input);
})->with(['not-an-address', '1:2:3:4:5', 'GG:00:00:01']);
Loading