From a37f48fdd6d0fd00d722ef29f3a0ad3b2dfbe826 Mon Sep 17 00:00:00 2001 From: flashwave Date: Sat, 22 Dec 2018 18:03:58 +0100 Subject: [PATCH] //cidr stuff --- misuzu.php | 1 - public/index.php | 53 ++++++++++++++++++++++++++++++++--------- src/Net/cidr.php | 5 ---- src/Net/ip.php | 61 ++++++++++++++++++++++++++++++++++++++++++++++-- utility.php | 11 +++++++++ 5 files changed, 112 insertions(+), 19 deletions(-) delete mode 100644 src/Net/cidr.php diff --git a/misuzu.php b/misuzu.php index 7834c535..ebfa5e58 100644 --- a/misuzu.php +++ b/misuzu.php @@ -51,7 +51,6 @@ require_once 'src/Forum/perms.php'; require_once 'src/Forum/post.php'; require_once 'src/Forum/topic.php'; require_once 'src/Forum/validate.php'; -require_once 'src/Net/cidr.php'; require_once 'src/Net/geoip.php'; require_once 'src/Net/ip.php'; require_once 'src/Parsers/parse.php'; diff --git a/public/index.php b/public/index.php index 3a7d285d..e6bbc42a 100644 --- a/public/index.php +++ b/public/index.php @@ -1,22 +1,53 @@ '104.16.0.0/12', + 'addrs' => [ + '104.28.8.4', + '104.28.9.4', + '94.211.73.13', + ], + ], + ]; + + foreach ($checks as $check) { + $mask = ip_cidr_to_mask($check['cidr']); + + echo 'MASK> ' . inet_ntop($mask) . "\t" . decbin_str($mask) . PHP_EOL; + + foreach ($check['addrs'] as $addr) { + $addr = inet_pton($addr); + echo 'ADDR> ' . inet_ntop($addr) . "\t" . decbin_str($addr) . "\t" . ip_match_mask($addr, $mask) . PHP_EOL; + } + + echo PHP_EOL; + } + return; } - return; } if (config_get_default(false, 'Site', 'embed_linked_data')) { diff --git a/src/Net/cidr.php b/src/Net/cidr.php deleted file mode 100644 index f2550f06..00000000 --- a/src/Net/cidr.php +++ /dev/null @@ -1,5 +0,0 @@ - $length) { if ($rawLength === $length) { - return $version; + return $returnWidth ? $length : $version; } } return MSZ_IP_UNKNOWN; } + +function ip_get_raw_width(int $version): int +{ + return MSZ_IP_SIZES[$version] ?? 0; +} + +// Takes 1.2.3.4/n notation, returns subnet mask in raw bytes +function ip_cidr_to_mask(string $ipRange): string +{ + [$address, $bits] = explode('/', $ipRange, 2); + + $address = inet_pton($address); + $width = ip_detect_raw_version($address, true) * 8; + + if ($bits < 1 || $bits > $width) { + return str_repeat(chr(0), $width); + } + + $mask = ''; + + for ($i = 0; $i < floor($width / 8); $i++) { + $addressByte = ord($address[$i]); + $maskByte = 0; + + for ($j = 0; $j < 8; $j++) { + $offset = (8 * $i) + $j; + $bit = 0x80 >> $j; + + if ($offset < $bits && ($addressByte & $bit) > 0) { + $maskByte |= $bit; + } else { + $maskByte &= ~$bit; + } + } + + $mask .= chr($maskByte); + } + + return $mask; +} + +// Takes a RAW IP and a RAW MASK +function ip_match_mask(string $ipAddress, string $mask): int +{ + $width = strlen($mask); + $result = false; + + if (strlen($ipAddress) !== $width) { + return $result; + } + + for ($i = 0; $i < $width; $i++) { + $result &= ($ipAddress[$i] & ~$mask[$i]) === 0; + } + + return $result; +} diff --git a/utility.php b/utility.php index caf29b6d..841b39b0 100644 --- a/utility.php +++ b/utility.php @@ -342,3 +342,14 @@ function is_user_int($value): bool { return ctype_digit(strval($value)); } + +function decbin_str(string $str): string +{ + $out = ''; + + for ($i = 0; $i < strlen($str); $i++) { + $out .= str_pad(decbin(ord($str[$i])), 8, '0'); + } + + return $out; +}