You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Services/Cloud/CloudIpPrefix.php

132 lines
3.9 KiB
PHP

<?php
namespace App\Services\Cloud;
/** Canonical address/prefix pair with family-safe containment operations. */
final class CloudIpPrefix
{
private function __construct(
private readonly CloudIpAddress $address,
private readonly CloudIpAddress $network,
private readonly int $prefixLength
) {
}
public static function address(mixed $value): self
{
return self::parse($value, false);
}
public static function network(mixed $value): self
{
return self::parse($value, true);
}
private static function parse(mixed $value, bool $requireNetwork): self
{
if (!is_string($value) || substr_count($value, '/') !== 1
|| trim($value) !== $value) {
throw new \InvalidArgumentException('IP prefix is invalid.');
}
[$rawAddress, $rawPrefix] = explode('/', $value, 2);
if ($rawPrefix === '' || preg_match('/^(?:0|[1-9][0-9]{0,2})$/D',
$rawPrefix) !== 1) {
throw new \InvalidArgumentException('IP prefix is invalid.');
}
$address = CloudIpAddress::parse($rawAddress);
$prefix = (int) $rawPrefix;
$maximum = $address->family() === 4 ? 32 : 128;
if ($prefix < 1 || $prefix > $maximum) {
throw new \InvalidArgumentException('IP prefix length is invalid.');
}
$network = CloudIpAddress::fromBinary(
self::maskedBinary($address->binary(), $prefix)
);
if ($requireNetwork && !$address->equals($network)) {
throw new \InvalidArgumentException('Network prefix has host bits set.');
}
return new self($address, $network, $prefix);
}
public function family(): int
{
return $this->address->family();
}
public function prefixLength(): int
{
return $this->prefixLength;
}
public function addressValue(): CloudIpAddress
{
return $this->address;
}
public function networkAddress(): CloudIpAddress
{
return $this->network;
}
public function canonical(): string
{
return $this->address->text() . '/' . $this->prefixLength;
}
public function networkCidr(): string
{
return $this->network->text() . '/' . $this->prefixLength;
}
public function contains(CloudIpAddress $address): bool
{
return $address->family() === $this->family()
&& hash_equals(
$this->network->binary(),
self::maskedBinary($address->binary(), $this->prefixLength)
);
}
public function overlaps(self $other): bool
{
if ($other->family() !== $this->family()) {
return false;
}
$bits = min($this->prefixLength, $other->prefixLength);
return hash_equals(
self::maskedBinary($this->network->binary(), $bits),
self::maskedBinary($other->network->binary(), $bits)
);
}
public function lastAddress(): CloudIpAddress
{
$bytes = array_values(unpack('C*', $this->network->binary()));
$totalBits = count($bytes) * 8;
for ($bit = $this->prefixLength; $bit < $totalBits; $bit++) {
$index = intdiv($bit, 8);
$offset = 7 - ($bit % 8);
$bytes[$index] |= 1 << $offset;
}
return CloudIpAddress::fromBinary(pack('C*', ...$bytes));
}
private static function maskedBinary(string $binary, int $prefix): string
{
$bytes = array_values(unpack('C*', $binary));
foreach ($bytes as $index => $byte) {
$start = $index * 8;
$remaining = $prefix - $start;
if ($remaining >= 8) {
continue;
}
if ($remaining <= 0) {
$bytes[$index] = 0;
continue;
}
$bytes[$index] = $byte & (0xff << (8 - $remaining));
}
return pack('C*', ...$bytes);
}
}