summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian A. Weber2016-02-01 14:57:43 +0100
committerChristian A. Weber2016-02-01 14:57:43 +0100
commit2ba8c034b134be1af855e1bfcb0b5acadc58cc2d (patch)
tree0ecb5e9b4e587637b56fdcf34197303b1ab0535e
parentec4949b848522a1e71ccdfbd186ffc03fc9be328 (diff)
downloaditools-2ba8c034b134be1af855e1bfcb0b5acadc58cc2d.tar.gz
itools-2ba8c034b134be1af855e1bfcb0b5acadc58cc2d.tar.bz2
itools-2ba8c034b134be1af855e1bfcb0b5acadc58cc2d.zip
fix cidr_match() without /mask
-rw-r--r--it.class2
-rwxr-xr-xtests/it.t2
2 files changed, 3 insertions, 1 deletions
diff --git a/it.class b/it.class
index 38421b4..cfad675 100644
--- a/it.class
+++ b/it.class
@@ -325,7 +325,7 @@ function cidr_match($ip, $cidr)
list($subnet, $mask) = explode('/', $cidr);
$ip_bin = inet_pton($ip);
$subnet_bin = inet_pton($subnet);
- $valid_bytes = $mask >> 3;
+ $valid_bytes = $mask ? $mask >> 3 : 42;
$bitmask = 256 - (1 << (8 - ($mask & 7)));
$lastbyte_matched = $bitmask ? (ord($ip_bin{$valid_bytes}) & $bitmask) == (ord($subnet_bin{$valid_bytes}) & $bitmask) : true;
return substr($ip_bin, 0, $valid_bytes) == substr($subnet_bin, 0, $valid_bytes) && $lastbyte_matched;
diff --git a/tests/it.t b/tests/it.t
index b5b7705..74ac57a 100755
--- a/tests/it.t
+++ b/tests/it.t
@@ -316,6 +316,8 @@ setlocale(LC_CTYPE, $oldlocale);
ini_set('default_charset', $oldcharset); # end of tests that must run with specific charset
# 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.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.1.1', '192.168.42.0/24'), false, "cidr_match no match");