summaryrefslogtreecommitdiffstats
path: root/etc/inc/util.inc
diff options
context:
space:
mode:
Diffstat (limited to 'etc/inc/util.inc')
-rw-r--r--etc/inc/util.inc80
1 files changed, 78 insertions, 2 deletions
diff --git a/etc/inc/util.inc b/etc/inc/util.inc
index b7df86b..cf531ce 100644
--- a/etc/inc/util.inc
+++ b/etc/inc/util.inc
@@ -219,10 +219,18 @@ function is_module_loaded($module_name) {
function gen_subnet($ipaddr, $bits) {
if (!is_ipaddr($ipaddr) || !is_numeric($bits))
return "";
-
return long2ip(ip2long($ipaddr) & gen_subnet_mask_long($bits));
}
+/* return the subnet address given a host address and a subnet bit count */
+function gen_subnetv6($ipaddr, $bits) {
+ if (!is_ipaddrv6($ipaddr) || !is_numeric($bits))
+ return "";
+
+ $address = Net_IPv6::getNetmask($ipaddr, $bits);
+ return $address;
+}
+
/* return the highest (broadcast) address in the subnet given a host address and a subnet bit count */
function gen_subnet_max($ipaddr, $bits) {
if (!is_ipaddr($ipaddr) || !is_numeric($bits))
@@ -231,6 +239,49 @@ function gen_subnet_max($ipaddr, $bits) {
return long2ip32(ip2long($ipaddr) | ~gen_subnet_mask_long($bits));
}
+/* Generate end number for a given ipv6 subnet mask
+ * no, it does not perform math */
+function gen_subnetv6_max($ipaddr, $bits) {
+ if(!is_ipaddrv6($ipaddr))
+ return false;
+
+ $subnetstart = gen_subnetv6($ipaddr, $bits);
+ /* we should have a expanded full ipv6 subnet starting at 0.
+ * Now split those by the semicolon so we can do 16 bit math */
+ $parts = explode(":", $subnetstart);
+ if(count($parts) <> 8)
+ return false;
+
+ /* reverse the array, we start with the lsb */
+ $parts = array_reverse($parts);
+ /* set the itteration count properly */
+ $bitsleft = 128 - $bits;
+ $i = 0;
+ foreach($parts as $part) {
+ /* foreach 16 bits we go to the next part */
+ /* no this isn't proper hex math, neither does it overflow properly */
+ while($bitsleft > 0) {
+ if($part == "0") {
+ $part = "f";
+ } else {
+ $part = $part . "f";
+ }
+ $bitsleft = $bitsleft - 4;
+ $j++;
+ if($j == 4) {
+ $parts[$i] = $part;
+ $j = 0;
+ $i++;
+ continue 2;
+ }
+ }
+ $i++;
+ }
+ $parts = array_reverse($parts);
+ $subnet_end = implode(":", $parts);
+ return $subnet_end;
+}
+
/* returns a subnet mask (long given a bit count) */
function gen_subnet_mask_long($bits) {
$sm = 0;
@@ -381,8 +432,26 @@ function is_numericint($arg) {
return (preg_match("/[^0-9]/", $arg) ? false : true);
}
-/* returns true if $ipaddr is a valid dotted IPv4 address */
+
+/* returns true if $ipaddr is a valid dotted IPv4 address or a IPv6 */
function is_ipaddr($ipaddr) {
+ if(is_ipaddrv4($ipaddr)) {
+ return true;
+ }
+ if(is_ipaddrv6($ipaddr)) {
+ return true;
+ }
+ return false;
+}
+
+/* returns true if $ipaddr is a valid IPv6 address */
+function is_ipaddrv6($ipaddr) {
+ $result = Net_IPv6::checkIPv6($ipaddr);
+ return $result;
+}
+
+/* returns true if $ipaddr is a valid dotted IPv4 address */
+function is_ipaddrv4($ipaddr) {
if (!is_string($ipaddr))
return false;
@@ -952,6 +1021,13 @@ function ipcmp($a, $b) {
/* return true if $addr is in $subnet, false if not */
function ip_in_subnet($addr,$subnet) {
+ if(is_ipaddrv6($addr)) {
+ $result = Net_IPv6::IsInNetmask($addr, $subnet);
+ if($result)
+ return true;
+ else
+ return false;
+ }
list($ip, $mask) = explode('/', $subnet);
$mask = (0xffffffff << (32 - $mask)) & 0xffffffff;
return ((ip2long($addr) & $mask) == (ip2long($ip) & $mask));
OpenPOWER on IntegriCloud