Skip to content

Commit c5ad7a4

Browse files
committed
fix #23755, #23756 thanks to landy
1 parent 6a2ac70 commit c5ad7a4

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

Net/IPv6.php

+21-5
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,15 @@ public static function isInNetmask($ip, $netmask, $bits=null)
361361

362362
if (2 == count($elements)) {
363363

364-
$netmask = $elements[0];
365-
$bits = $elements[1];
364+
$netmask = $elements[0];
365+
$bits = $elements[1];
366+
// Correctly uncompress netmasks with prefixes, ie ::FFFF/96 == 0:0:0:0:0:FFFF:0:0/96
367+
// Need only for xxx::yyy/zz and ::yyy/zz
368+
if (preg_match('/^(\:\:[a-f\d]{1,4}|[a-f\d]{1,4}.*\:\:.*[a-f\d]{1,4})$/', $netmask) &&
369+
!preg_match('/\:0+$/', $netmask)) {
370+
$c_bits = intval((128 - $bits) / 16);
371+
$netmask .= str_repeat(':0', $c_bits);
372+
}
366373

367374
}
368375

@@ -537,7 +544,7 @@ public static function uncompress($ip, $leadingZeros = false)
537544

538545
} else {
539546

540-
$ip = Net_IPv6::removePrefixLength($ip);
547+
$ip = Net_IPv6::removeNetmaskSpec($ip);
541548
$prefix = '/'.$prefix;
542549

543550
}
@@ -697,7 +704,7 @@ public static function compress($ip, $force = false)
697704

698705
} else {
699706

700-
$ip = Net_IPv6::removePrefixLength($ip);
707+
$ip = Net_IPv6::removeNetmaskSpec($ip);
701708
$prefix = '/'.$prefix;
702709

703710
}
@@ -1052,7 +1059,16 @@ protected static function _ip2Bin($ip)
10521059
$binstr = '';
10531060

10541061
$ip = Net_IPv6::removeNetmaskSpec($ip);
1055-
$ip = Net_IPv6::Uncompress($ip);
1062+
1063+
// Correctly convert IPv4 mapped addresses (::ffff:x.x.x.x)
1064+
list(, $ipv4) = Net_IPv6::SplitV64($ip, FALSE);
1065+
if (strlen($ipv4)) {
1066+
$ipv4map = explode('.', $ipv4, 4);
1067+
$ipv4replace = dechex($ipv4map[0] * 256 + $ipv4map[1]) . ':' . dechex($ipv4map[2] * 256 + $ipv4map[3]);
1068+
$ip = str_replace($ipv4, $ipv4replace, $ip);
1069+
}
1070+
1071+
$ip = Net_IPv6::uncompress($ip);
10561072

10571073
$parts = explode(':', $ip);
10581074

tests/AllTests.php

+10
Original file line numberDiff line numberDiff line change
@@ -734,4 +734,14 @@ public function testSplitV64IPv6Only() {
734734

735735
}
736736

737+
/**
738+
* related to bug #23756
739+
* @author landy <[email protected]>
740+
*/
741+
public function testCompressedNetmasksIncorrectlyDetectedInIsInNetmask() {
742+
$netmask = '::ffff/96';
743+
$testip = '0:0:0:0:0:ffff:c000:22f';
744+
$this->assertTrue(Net_IPv6::isInNetmask($testip, $netmask));
745+
}
746+
737747
}

0 commit comments

Comments
 (0)