Fix usable hosts for /32 subnet

This commit is contained in:
Maarten 2024-11-27 20:12:45 +01:00
parent a763bf1d8c
commit 45c25fa5f3

View file

@ -40,7 +40,7 @@ class Subnet
throw new Exception("CIDR must be between 0 and 32."); throw new Exception("CIDR must be between 0 and 32.");
} }
$hosts = (1 << (32 - $cidr)) - 2; // Excludes network and broadcast $hosts = ($cidr === 32) ? 1 : (1 << (32 - $cidr)) - 2; // Special case for /32
$ipLong = ip2long($ip); $ipLong = ip2long($ip);
$mask = -1 << (32 - $cidr); $mask = -1 << (32 - $cidr);
$network = $ipLong & $mask; $network = $ipLong & $mask;