//cidr stuff

This commit is contained in:
flash 2018-12-22 18:03:58 +01:00
parent 774ce1f966
commit a37f48fdd6
5 changed files with 112 additions and 19 deletions

View file

@ -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';

View file

@ -1,22 +1,53 @@
<?php
require_once '../misuzu.php';
if (!empty($_GET['pdump'])) {
header('Content-Type: text/plain');
if (MSZ_DEBUG) {
if (!empty($_GET['pdump'])) {
header('Content-Type: text/plain');
for ($i = 0; $i < 10; $i++) {
$perms = [];
for ($i = 0; $i < 10; $i++) {
$perms = [];
echo "# USER {$i}\n";
echo "# USER {$i}\n";
foreach (MSZ_PERM_MODES as $mode) {
$perms = decbin(perms_get_user($mode, $i));
echo "{$mode}: {$perms}\n";
foreach (MSZ_PERM_MODES as $mode) {
$perms = decbin(perms_get_user($mode, $i));
echo "{$mode}: {$perms}\n";
}
echo "\n";
}
echo "\n";
return;
}
if (!empty($_GET['cidr'])) {
header('Content-Type: text/plain');
$checks = [
[
'cidr' => '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')) {

View file

@ -1,5 +0,0 @@
<?php
function cidr_range_to_mask(string $startAddress, string $endAddress, bool $isRaw = false): array
{
return [];
}

View file

@ -40,15 +40,72 @@ function ip_detect_string_version(string $address): int
return MSZ_IP_UNKNOWN;
}
function ip_detect_raw_version(string $raw): int
function ip_detect_raw_version(string $raw, bool $returnWidth = false): int
{
$rawLength = strlen($raw);
foreach (MSZ_IP_SIZES as $version => $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;
}

View file

@ -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;
}