From 1c8d846574652f392f4a33c98550443df97a53c0 Mon Sep 17 00:00:00 2001 From: daniel Date: Thu, 17 Jan 2019 15:49:41 +0100 Subject: [PATCH] WIP IPAM Supernet Former-commit-id: 9da8df381292bc79a49c1416fc6d2820f4b814ac --- pandora_console/include/functions.php | 56 +++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/pandora_console/include/functions.php b/pandora_console/include/functions.php index f2056f058e..e8130b6e4d 100644 --- a/pandora_console/include/functions.php +++ b/pandora_console/include/functions.php @@ -3435,4 +3435,60 @@ function pandora_xhprof_display_result($key = "", $method = "link") { } } + +/** + * From a network with a mask remove the smallest ip and the highest + * + * @param string address to identify the network. + * @param string mask to identify the mask network + * @return array or false with smallest ip and highest ip + */ +function range_ips_for_network($address, $mask) { + if(!isset($address) || !isset($mask)){ + return false; + } + + //convert ip addresses to long form + $address_long = ip2long($address); + $mask_long = ip2long($mask); + + //caculate first usable address + $ip_host_first = ((~$mask_long) & $address_long); + $ip_first = ($address_long ^ $ip_host_first) + 1; + + //caculate last usable address + $ip_broadcast_invert = ~$mask_long; + $ip_last = ($address_long | $ip_broadcast_invert) - 1; + + $range = array( + 'first' => long2ip($ip_first), + 'last' => long2ip($ip_last) + ); + + return $range; +} + +/** + * from two ips find out if there is such an ip + * + * @param string ip ip wont validate + * @param string ip_lower + * @param string ip_upper + * @return bool true or false if the ip is between the two ips + */ +function is_in_network ($ip, $ip_lower, $ip_upper) { + if(!isset($ip) || !isset($ip_lower) || !isset($ip_upper)){ + return false; + } + + $ip = (float)sprintf("%u",ip2long($ip)); + $ip_lower = (float)sprintf("%u",ip2long($ip_lower)); + $ip_upper = (float)sprintf("%u",ip2long($ip_upper)); + + if ($ip >= $ip_lower && $ip <= $ip_upper){ + return true; + } else { + return false; + } +} ?>