From e8b681292ae469a64df6033965babbe63be0290b Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Thu, 10 Apr 2025 14:20:55 +0200 Subject: return matching CIDR for tracking --- it.class | 2 +- test/it.t | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/it.class b/it.class index a114e35..83e7d0e 100644 --- a/it.class +++ b/it.class @@ -423,7 +423,7 @@ static function cidr_match($ip, $cidrs) $lastbyte_matched = $bitmask ? (ord($ip_bin[$valid_bytes]) & $bitmask) == (ord($subnet_bin[$valid_bytes]) & $bitmask) : true; if (substr($ip_bin, 0, $valid_bytes) == substr($subnet_bin, 0, $valid_bytes) && $lastbyte_matched) - return true; + return $cidr; } return false; diff --git a/test/it.t b/test/it.t index 6ea1859..df1cba6 100755 --- a/test/it.t +++ b/test/it.t @@ -323,17 +323,17 @@ ini_set('default_charset', $oldcharset); # end of tests that must run with speci # it::cidr_match tests is(it::cidr_match('192.168.2.3', '192.168.2.5'), false, "cidr_match full IP no match no mask"); -is(it::cidr_match('192.168.2.3', '192.168.2.3'), true, "cidr_match full IP match no mask"); +is(it::cidr_match('192.168.2.3', '192.168.2.3'), '192.168.2.3', "cidr_match full IP match no mask"); is(it::cidr_match('192.168.2.3', '192.168.2.5/32'), false, "cidr_match full IP no match"); -is(it::cidr_match('192.168.2.5', '192.168.2.5/32'), true, "cidr_match full IP match"); +is(it::cidr_match('192.168.2.5', '192.168.2.5/32'), '192.168.2.5/32', "cidr_match full IP match"); is(it::cidr_match('192.168.1.1', '192.168.42.0/24'), false, "cidr_match no match"); -is(it::cidr_match('192.168.42.1', '192.168.42.0/24'), true, "cidr_match basic match"); -is(it::cidr_match('192.168.42.42', '192.168.0.0/16'), true, "cidr_match class b"); +is(it::cidr_match('192.168.42.1', '192.168.42.0/24'), '192.168.42.0/24', "cidr_match basic match"); +is(it::cidr_match('192.168.42.42', '192.168.0.0/16'), '192.168.0.0/16', "cidr_match class b"); is(it::cidr_match('192.168.42.42', '192.168.42.64/26'), false, "cidr_match offset no match"); -is(it::cidr_match('192.168.42.42', '192.168.42.32/27'), true, "cidr_match offset"); -is(it::cidr_match('2001:918:ff83:101:798e:77c0:b722:fe56', '2001:918:ff83:101::/64'), true, "cidr_match ipv6"); +is(it::cidr_match('192.168.42.42', '192.168.42.32/27'), '192.168.42.32/27', "cidr_match offset"); +is(it::cidr_match('2001:918:ff83:101:798e:77c0:b722:fe56', '2001:918:ff83:101::/64'), '2001:918:ff83:101::/64', "cidr_match ipv6"); is(it::cidr_match('2001:918:ff83:102:798e:77c0:b722:fe56', '2001:918:ff83:101::/64'), false, "cidr_match ipv6 no match" ); -is(it::cidr_match('10.11.12.13', ['10.0.0.0/8', '192.168.0.0./16']), true, "cidr_match array"); +is(it::cidr_match('10.11.12.13', ['10.0.0.0/8', '192.168.0.0./16']), '10.0.0.0/8', "cidr_match array"); # it::is_private_ip tests is(it::is_private_ip('192.168.2.3'), true, "is_private_ip 192.168/16 example"); -- cgit v1.2.3