Commit ba62824a authored by Ad Schellevis's avatar Ad Schellevis Committed by Franco Fichtner

(legacy) whitespace and curly braces in util.inc

(cherry picked from commit db254cb8)
parent e3b8f0f1
...@@ -117,21 +117,26 @@ function unlock($cfglckkey = null) ...@@ -117,21 +117,26 @@ function unlock($cfglckkey = null)
} }
/* validate non-negative numeric string, or equivalent numeric variable */ /* validate non-negative numeric string, or equivalent numeric variable */
function is_numericint($arg) { function is_numericint($arg)
{
return (((is_int($arg) && $arg >= 0) || (is_string($arg) && strlen($arg) > 0 && ctype_digit($arg))) ? true : false); return (((is_int($arg) && $arg >= 0) || (is_string($arg) && strlen($arg) > 0 && ctype_digit($arg))) ? true : false);
} }
/* return the subnet address given a host address and a subnet bit count */ /* return the subnet address given a host address and a subnet bit count */
function gen_subnet($ipaddr, $bits) { function gen_subnet($ipaddr, $bits)
if (!is_ipaddr($ipaddr) || !is_numeric($bits)) {
if (!is_ipaddr($ipaddr) || !is_numeric($bits)) {
return ""; return "";
}
return long2ip(ip2long($ipaddr) & gen_subnet_mask_long($bits)); return long2ip(ip2long($ipaddr) & gen_subnet_mask_long($bits));
} }
/* return the subnet address given a host address and a subnet bit count */ /* return the subnet address given a host address and a subnet bit count */
function gen_subnetv6($ipaddr, $bits) { function gen_subnetv6($ipaddr, $bits)
if (!is_ipaddrv6($ipaddr) || !is_numeric($bits)) {
if (!is_ipaddrv6($ipaddr) || !is_numeric($bits)) {
return ""; return "";
}
$address = Net_IPv6::getNetmask($ipaddr, $bits); $address = Net_IPv6::getNetmask($ipaddr, $bits);
$address = Net_IPv6::compress($address); $address = Net_IPv6::compress($address);
...@@ -140,19 +145,21 @@ function gen_subnetv6($ipaddr, $bits) { ...@@ -140,19 +145,21 @@ function gen_subnetv6($ipaddr, $bits) {
/* return the highest (broadcast) address in the subnet given a host address and a subnet bit count */ /* return the highest (broadcast) address in the subnet given a host address and a subnet bit count */
function gen_subnet_max($ipaddr, $bits) { function gen_subnet_max($ipaddr, $bits) {
if (!is_ipaddr($ipaddr) || !is_numeric($bits)) if (!is_ipaddr($ipaddr) || !is_numeric($bits)) {
return ""; return "";
}
return long2ip32(ip2long($ipaddr) | ~gen_subnet_mask_long($bits)); return long2ip32(ip2long($ipaddr) | ~gen_subnet_mask_long($bits));
} }
/* Generate end number for a given ipv6 subnet mask */ /* Generate end number for a given ipv6 subnet mask */
function gen_subnetv6_max($ipaddr, $bits) { function gen_subnetv6_max($ipaddr, $bits)
if(!is_ipaddrv6($ipaddr)) {
if (!is_ipaddrv6($ipaddr)) {
return false; return false;
}
$mask = Net_IPv6::getNetmask('FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',$bits); $mask = Net_IPv6::getNetmask('FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',$bits);
$inet_ip = (binary)inet_pton($ipaddr); $inet_ip = (binary)inet_pton($ipaddr);
$inet_mask = (binary)inet_pton($mask); $inet_mask = (binary)inet_pton($mask);
...@@ -162,7 +169,8 @@ function gen_subnetv6_max($ipaddr, $bits) { ...@@ -162,7 +169,8 @@ function gen_subnetv6_max($ipaddr, $bits) {
} }
/* returns a subnet mask (long given a bit count) */ /* returns a subnet mask (long given a bit count) */
function gen_subnet_mask_long($bits) { function gen_subnet_mask_long($bits)
{
$sm = 0; $sm = 0;
for ($i = 0; $i < $bits; $i++) { for ($i = 0; $i < $bits; $i++) {
$sm >>= 1; $sm >>= 1;
...@@ -172,29 +180,34 @@ function gen_subnet_mask_long($bits) { ...@@ -172,29 +180,34 @@ function gen_subnet_mask_long($bits) {
} }
/* same as above but returns a string */ /* same as above but returns a string */
function gen_subnet_mask($bits) { function gen_subnet_mask($bits)
{
return long2ip(gen_subnet_mask_long($bits)); return long2ip(gen_subnet_mask_long($bits));
} }
/* Convert long int to IP address, truncating to 32-bits. */ /* Convert long int to IP address, truncating to 32-bits. */
function long2ip32($ip) { function long2ip32($ip)
{
return long2ip($ip & 0xFFFFFFFF); return long2ip($ip & 0xFFFFFFFF);
} }
/* Convert IP address to long int, truncated to 32-bits to avoid sign extension on 64-bit platforms. */ /* Convert IP address to long int, truncated to 32-bits to avoid sign extension on 64-bit platforms. */
function ip2long32($ip) { function ip2long32($ip)
{
return ( ip2long($ip) & 0xFFFFFFFF ); return ( ip2long($ip) & 0xFFFFFFFF );
} }
/* Convert IP address to unsigned long int. */ /* Convert IP address to unsigned long int. */
function ip2ulong($ip) { function ip2ulong($ip)
{
return sprintf("%u", ip2long32($ip)); return sprintf("%u", ip2long32($ip));
} }
/* Find out how many IPs are contained within a given IP range /* Find out how many IPs are contained within a given IP range
* e.g. 192.168.0.0 to 192.168.0.255 returns 256 * e.g. 192.168.0.0 to 192.168.0.255 returns 256
*/ */
function ip_range_size($startip, $endip) { function ip_range_size($startip, $endip)
{
if (is_ipaddr($startip) && is_ipaddr($endip)) { if (is_ipaddr($startip) && is_ipaddr($endip)) {
// Operate as unsigned long because otherwise it wouldn't work // Operate as unsigned long because otherwise it wouldn't work
// when crossing over from 127.255.255.255 / 128.0.0.0 barrier // when crossing over from 127.255.255.255 / 128.0.0.0 barrier
...@@ -206,7 +219,8 @@ function ip_range_size($startip, $endip) { ...@@ -206,7 +219,8 @@ function ip_range_size($startip, $endip) {
/* Find the smallest possible subnet mask which can contain a given number of IPs /* Find the smallest possible subnet mask which can contain a given number of IPs
* e.g. 512 IPs can fit in a /23, but 513 IPs need a /22 * e.g. 512 IPs can fit in a /23, but 513 IPs need a /22
*/ */
function find_smallest_cidr($number) { function find_smallest_cidr($number)
{
$smallest = 1; $smallest = 1;
for ($b=32; $b > 0; $b--) { for ($b=32; $b > 0; $b--) {
$smallest = ($number <= pow(2,$b)) ? $b : $smallest; $smallest = ($number <= pow(2,$b)) ? $b : $smallest;
...@@ -215,31 +229,36 @@ function find_smallest_cidr($number) { ...@@ -215,31 +229,36 @@ function find_smallest_cidr($number) {
} }
/* Return the previous IP address before the given address */ /* Return the previous IP address before the given address */
function ip_before($ip) { function ip_before($ip)
{
return long2ip32(ip2long($ip)-1); return long2ip32(ip2long($ip)-1);
} }
/* Return the next IP address after the given address */ /* Return the next IP address after the given address */
function ip_after($ip) { function ip_after($ip)
{
return long2ip32(ip2long($ip)+1); return long2ip32(ip2long($ip)+1);
} }
/* Return true if the first IP is 'before' the second */ /* Return true if the first IP is 'before' the second */
function ip_less_than($ip1, $ip2) { function ip_less_than($ip1, $ip2)
{
// Compare as unsigned long because otherwise it wouldn't work when // Compare as unsigned long because otherwise it wouldn't work when
// crossing over from 127.255.255.255 / 128.0.0.0 barrier // crossing over from 127.255.255.255 / 128.0.0.0 barrier
return ip2ulong($ip1) < ip2ulong($ip2); return ip2ulong($ip1) < ip2ulong($ip2);
} }
/* Return true if the first IP is 'after' the second */ /* Return true if the first IP is 'after' the second */
function ip_greater_than($ip1, $ip2) { function ip_greater_than($ip1, $ip2)
{
// Compare as unsigned long because otherwise it wouldn't work // Compare as unsigned long because otherwise it wouldn't work
// when crossing over from 127.255.255.255 / 128.0.0.0 barrier // when crossing over from 127.255.255.255 / 128.0.0.0 barrier
return ip2ulong($ip1) > ip2ulong($ip2); return ip2ulong($ip1) > ip2ulong($ip2);
} }
/* Convert a range of IPs to an array of subnets which can contain the range. */ /* Convert a range of IPs to an array of subnets which can contain the range. */
function ip_range_to_subnet_array($startip, $endip) { function ip_range_to_subnet_array($startip, $endip)
{
if (!is_ipaddr($startip) || !is_ipaddr($endip)) { if (!is_ipaddr($startip) || !is_ipaddr($endip)) {
return array(); return array();
} }
...@@ -300,20 +319,23 @@ function ip_range_to_subnet_array($startip, $endip) { ...@@ -300,20 +319,23 @@ function ip_range_to_subnet_array($startip, $endip) {
} }
/* returns true if $ipaddr is a valid dotted IPv4 address or a IPv6 */ /* returns true if $ipaddr is a valid dotted IPv4 address or a IPv6 */
function is_ipaddr($ipaddr) { function is_ipaddr($ipaddr)
if(is_ipaddrv4($ipaddr)) { {
if (is_ipaddrv4($ipaddr)) {
return true; return true;
} }
if(is_ipaddrv6($ipaddr)) { if (is_ipaddrv6($ipaddr)) {
return true; return true;
} }
return false; return false;
} }
/* returns true if $ipaddr is a valid IPv6 address */ /* returns true if $ipaddr is a valid IPv6 address */
function is_ipaddrv6($ipaddr) { function is_ipaddrv6($ipaddr)
if (!is_string($ipaddr) || empty($ipaddr)) {
if (!is_string($ipaddr) || empty($ipaddr)) {
return false; return false;
}
if (strstr($ipaddr, "%") && is_linklocal($ipaddr)) { if (strstr($ipaddr, "%") && is_linklocal($ipaddr)) {
$tmpip = explode("%", $ipaddr); $tmpip = explode("%", $ipaddr);
$ipaddr = $tmpip[0]; $ipaddr = $tmpip[0];
...@@ -326,43 +348,51 @@ function is_ipaddrv6($ipaddr) { ...@@ -326,43 +348,51 @@ function is_ipaddrv6($ipaddr) {
} }
/* returns true if $ipaddr is a valid dotted IPv4 address */ /* returns true if $ipaddr is a valid dotted IPv4 address */
function is_ipaddrv4($ipaddr) { function is_ipaddrv4($ipaddr)
if (!is_string($ipaddr) || empty($ipaddr)) {
if (!is_string($ipaddr) || empty($ipaddr)) {
return false; return false;
}
$ip_long = ip2long($ipaddr); $ip_long = ip2long($ipaddr);
$ip_reverse = long2ip32($ip_long); $ip_reverse = long2ip32($ip_long);
if ($ipaddr == $ip_reverse) if ($ipaddr == $ip_reverse) {
return true; return true;
else } else {
return false; return false;
}
} }
/* returns true if $ipaddr is a valid linklocal address */ /* returns true if $ipaddr is a valid linklocal address */
function is_linklocal($ipaddr) { function is_linklocal($ipaddr)
{
return (strtolower(substr($ipaddr, 0, 5)) == "fe80:"); return (strtolower(substr($ipaddr, 0, 5)) == "fe80:");
} }
/* returns scope of a linklocal address */ /* returns scope of a linklocal address */
function get_ll_scope($addr) { function get_ll_scope($addr)
if (!is_linklocal($addr) || !strstr($addr, "%")) {
if (!is_linklocal($addr) || !strstr($addr, "%")) {
return ""; return "";
}
list ($ll, $scope) = explode("%", $addr); list ($ll, $scope) = explode("%", $addr);
return $scope; return $scope;
} }
/* returns true if $ipaddr is a valid literal IPv6 address */ /* returns true if $ipaddr is a valid literal IPv6 address */
function is_literalipaddrv6($ipaddr) { function is_literalipaddrv6($ipaddr)
if(preg_match("/\[([0-9a-f:]+)\]/i", $ipaddr, $match)) {
if (preg_match("/\[([0-9a-f:]+)\]/i", $ipaddr, $match)) {
$ipaddr = $match[1]; $ipaddr = $match[1];
else } else {
return false; return false;
}
return is_ipaddrv6($ipaddr); return is_ipaddrv6($ipaddr);
} }
function is_ipaddrwithport($ipport) { function is_ipaddrwithport($ipport)
{
$parts = explode(":", $ipport); $parts = explode(":", $ipport);
$port = array_pop($parts); $port = array_pop($parts);
if (count($parts) == 1) { if (count($parts) == 1) {
...@@ -374,7 +404,8 @@ function is_ipaddrwithport($ipport) { ...@@ -374,7 +404,8 @@ function is_ipaddrwithport($ipport) {
} }
} }
function is_hostnamewithport($hostport) { function is_hostnamewithport($hostport)
{
$parts = explode(":", $hostport); $parts = explode(":", $hostport);
$port = array_pop($parts); $port = array_pop($parts);
if (count($parts) == 1) { if (count($parts) == 1) {
...@@ -392,10 +423,11 @@ function is_ipaddroralias($ipaddr) ...@@ -392,10 +423,11 @@ function is_ipaddroralias($ipaddr)
if (is_alias($ipaddr)) { if (is_alias($ipaddr)) {
if (isset($config['aliases']['alias'])) { if (isset($config['aliases']['alias'])) {
foreach ($config['aliases']['alias'] as $alias) { foreach ($config['aliases']['alias'] as $alias) {
if ($alias['name'] == $ipaddr && !preg_match("/port/i", $alias['type'])) if ($alias['name'] == $ipaddr && !preg_match("/port/i", $alias['type'])) {
return true; return true;
} }
} }
}
return false; return false;
} }
...@@ -405,50 +437,60 @@ function is_ipaddroralias($ipaddr) ...@@ -405,50 +437,60 @@ function is_ipaddroralias($ipaddr)
/* returns true if $subnet is a valid IPv4 or IPv6 subnet in CIDR format /* returns true if $subnet is a valid IPv4 or IPv6 subnet in CIDR format
false - if not a valid subnet false - if not a valid subnet
true (numeric 4 or 6) - if valid, gives type of subnet */ true (numeric 4 or 6) - if valid, gives type of subnet */
function is_subnet($subnet) { function is_subnet($subnet)
{
if (is_string($subnet) && preg_match('/^(?:([0-9.]{7,15})|([0-9a-f:]{2,39}))\/(\d{1,3})$/i', $subnet, $parts)) { if (is_string($subnet) && preg_match('/^(?:([0-9.]{7,15})|([0-9a-f:]{2,39}))\/(\d{1,3})$/i', $subnet, $parts)) {
if (is_ipaddrv4($parts[1]) && $parts[3] <= 32) if (is_ipaddrv4($parts[1]) && $parts[3] <= 32) {
return 4; return 4;
if (is_ipaddrv6($parts[2]) && $parts[3] <= 128) }
if (is_ipaddrv6($parts[2]) && $parts[3] <= 128) {
return 6; return 6;
} }
}
return false; return false;
} }
/* same as is_subnet() but accepts IPv4 only */ /* same as is_subnet() but accepts IPv4 only */
function is_subnetv4($subnet) { function is_subnetv4($subnet)
{
return (is_subnet($subnet) == 4); return (is_subnet($subnet) == 4);
} }
/* same as is_subnet() but accepts IPv6 only */ /* same as is_subnet() but accepts IPv6 only */
function is_subnetv6($subnet) { function is_subnetv6($subnet)
{
return (is_subnet($subnet) == 6); return (is_subnet($subnet) == 6);
} }
/* returns true if $hostname is a valid hostname */ /* returns true if $hostname is a valid hostname */
function is_hostname($hostname) { function is_hostname($hostname)
if (!is_string($hostname)) {
if (!is_string($hostname)) {
return false; return false;
}
if (preg_match('/^(?:(?:[a-z0-9_]|[a-z0-9_][a-z0-9_\-]*[a-z0-9_])\.)*(?:[a-z0-9_]|[a-z0-9_][a-z0-9_\-]*[a-z0-9_])$/i', $hostname)) if (preg_match('/^(?:(?:[a-z0-9_]|[a-z0-9_][a-z0-9_\-]*[a-z0-9_])\.)*(?:[a-z0-9_]|[a-z0-9_][a-z0-9_\-]*[a-z0-9_])$/i', $hostname)) {
return true; return true;
else } else {
return false; return false;
}
} }
/* returns true if $domain is a valid domain name */ /* returns true if $domain is a valid domain name */
function is_domain($domain) { function is_domain($domain)
if (!is_string($domain)) {
if (!is_string($domain)) {
return false; return false;
}
if (preg_match('/^(?:(?:[a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])\.)*(?:[a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])$/i', $domain)) if (preg_match('/^(?:(?:[a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])\.)*(?:[a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])$/i', $domain)) {
return true; return true;
else } else {
return false; return false;
}
} }
/* returns true if $macaddr is a valid MAC address */ /* returns true if $macaddr is a valid MAC address */
function is_macaddr($macaddr, $partial=false) { function is_macaddr($macaddr, $partial=false)
{
$repeat = ($partial) ? '1,5' : '5'; $repeat = ($partial) ? '1,5' : '5';
return preg_match('/^[0-9A-F]{2}(?:[:][0-9A-F]{2}){'.$repeat.'}$/i', $macaddr) == 1 ? true : false; return preg_match('/^[0-9A-F]{2}(?:[:][0-9A-F]{2}){'.$repeat.'}$/i', $macaddr) == 1 ? true : false;
} }
...@@ -461,32 +503,38 @@ function is_macaddr($macaddr, $partial=false) { ...@@ -461,32 +503,38 @@ function is_macaddr($macaddr, $partial=false) {
bad names: empty string, pure numeric, pure underscore bad names: empty string, pure numeric, pure underscore
reserved words: pre-defined service/protocol/port names which should not be ambiguous, and the words "port" and "pass" */ reserved words: pre-defined service/protocol/port names which should not be ambiguous, and the words "port" and "pass" */
function is_validaliasname($name) { function is_validaliasname($name)
{
/* Array of reserved words */ /* Array of reserved words */
$reserved = array("port", "pass"); $reserved = array("port", "pass");
if (!is_string($name) || strlen($name) >= 32 || preg_match('/(^_*$|^\d*$|[^a-z0-9_])/i', $name)) if (!is_string($name) || strlen($name) >= 32 || preg_match('/(^_*$|^\d*$|[^a-z0-9_])/i', $name)) {
return false; return false;
if (in_array($name, $reserved, true) || getservbyname($name, "tcp") || getservbyname($name, "udp") || getprotobyname($name)) }
if (in_array($name, $reserved, true) || getservbyname($name, "tcp") || getservbyname($name, "udp") || getprotobyname($name)) {
return; /* return NULL */ return; /* return NULL */
}
return true; return true;
} }
/* returns true if $port is a valid TCP/UDP port */ /* returns true if $port is a valid TCP/UDP port */
function is_port($port) { function is_port($port)
if (getservbyname($port, "tcp") || getservbyname($port, "udp")) {
if (getservbyname($port, "tcp") || getservbyname($port, "udp")) {
return true; return true;
if (!ctype_digit($port)) }
if (!ctype_digit($port)) {
return false; return false;
else if ((intval($port) < 1) || (intval($port) > 65535)) } elseif ((intval($port) < 1) || (intval($port) > 65535)) {
return false; return false;
}
return true; return true;
} }
/* returns true if $portrange is a valid TCP/UDP portrange ("<port>:<port>") */ /* returns true if $portrange is a valid TCP/UDP portrange ("<port>:<port>") */
function is_portrange($portrange) { function is_portrange($portrange)
{
$ports = explode(":", $portrange); $ports = explode(":", $portrange);
return (count($ports) == 2 && is_port($ports[0]) && is_port($ports[1])); return (count($ports) == 2 && is_port($ports[0]) && is_port($ports[1]));
} }
...@@ -498,10 +546,11 @@ function is_portoralias($port) ...@@ -498,10 +546,11 @@ function is_portoralias($port)
if (is_alias($port)) { if (is_alias($port)) {
if (isset($config['aliases']['alias'])) { if (isset($config['aliases']['alias'])) {
foreach ($config['aliases']['alias'] as $alias) { foreach ($config['aliases']['alias'] as $alias) {
if ($alias['name'] == $port && preg_match("/port/i", $alias['type'])) if ($alias['name'] == $port && preg_match("/port/i", $alias['type'])) {
return true; return true;
} }
} }
}
return false; return false;
} }
...@@ -509,9 +558,11 @@ function is_portoralias($port) ...@@ -509,9 +558,11 @@ function is_portoralias($port)
} }
/* create ranges of sequential port numbers (200:215) and remove duplicates */ /* create ranges of sequential port numbers (200:215) and remove duplicates */
function group_ports($ports) { function group_ports($ports)
if (!is_array($ports) || empty($ports)) {
if (!is_array($ports) || empty($ports)) {
return; return;
}
$uniq = array(); $uniq = array();
foreach ($ports as $port) { foreach ($ports as $port) {
...@@ -522,14 +573,17 @@ function group_ports($ports) { ...@@ -522,14 +573,17 @@ function group_ports($ports) {
$begin = $end; $begin = $end;
$end = $aux; $end = $aux;
} }
for ($i = $begin; $i <= $end; $i++) for ($i = $begin; $i <= $end; $i++) {
if (!in_array($i, $uniq)) if (!in_array($i, $uniq)) {
$uniq[] = $i; $uniq[] = $i;
} else if (is_port($port)) { }
if (!in_array($port, $uniq)) }
} elseif (is_port($port)) {
if (!in_array($port, $uniq)) {
$uniq[] = $port; $uniq[] = $port;
} }
} }
}
sort($uniq, SORT_NUMERIC); sort($uniq, SORT_NUMERIC);
$result = array(); $result = array();
...@@ -540,11 +594,11 @@ function group_ports($ports) { ...@@ -540,11 +594,11 @@ function group_ports($ports) {
} }
$last = end($result); $last = end($result);
if (is_portrange($last)) if (is_portrange($last)) {
list($begin, $end) = explode(":", $last); list($begin, $end) = explode(":", $last);
else } else {
$begin = $end = $last; $begin = $end = $last;
}
if ($port == ($end+1)) { if ($port == ($end+1)) {
$end++; $end++;
$result[count($result)-1] = "{$begin}:{$end}"; $result[count($result)-1] = "{$begin}:{$end}";
...@@ -557,19 +611,23 @@ function group_ports($ports) { ...@@ -557,19 +611,23 @@ function group_ports($ports) {
} }
/* returns true if $test is in the range between $start and $end */ /* returns true if $test is in the range between $start and $end */
function is_inrange_v4($test, $start, $end) { function is_inrange_v4($test, $start, $end)
if ( (ip2ulong($test) <= ip2ulong($end)) && (ip2ulong($test) >= ip2ulong($start)) ) {
if ( (ip2ulong($test) <= ip2ulong($end)) && (ip2ulong($test) >= ip2ulong($start)) ) {
return true; return true;
else } else {
return false; return false;
}
} }
/* returns true if $test is in the range between $start and $end */ /* returns true if $test is in the range between $start and $end */
function is_inrange_v6($test, $start, $end) { function is_inrange_v6($test, $start, $end)
if ( (inet_pton($test) <= inet_pton($end)) && (inet_pton($test) >= inet_pton($start)) ) {
if ( (inet_pton($test) <= inet_pton($end)) && (inet_pton($test) >= inet_pton($start)) ) {
return true; return true;
else } else {
return false; return false;
}
} }
/* returns true if $test is in the range between $start and $end */ /* returns true if $test is in the range between $start and $end */
...@@ -591,11 +649,12 @@ function get_configured_carp_interface_list($carpinterface = '', $family = 'inet ...@@ -591,11 +649,12 @@ function get_configured_carp_interface_list($carpinterface = '', $family = 'inet
case "carp": case "carp":
if (!empty($carpinterface)) { if (!empty($carpinterface)) {
if ($carpinterface == "{$vip['interface']}_vip{$vip['vhid']}") { if ($carpinterface == "{$vip['interface']}_vip{$vip['vhid']}") {
if ($family == "inet" && is_ipaddrv4($vip['subnet'])) if ($family == "inet" && is_ipaddrv4($vip['subnet'])) {
return $vip['subnet']; return $vip['subnet'];
else if ($family == "inet6" && is_ipaddrv6($vip['subnet'])) } elseif ($family == "inet6" && is_ipaddrv6($vip['subnet'])) {
return $vip['subnet']; return $vip['subnet'];
} }
}
} else { } else {
$iflist["{$vip['interface']}_vip{$vip['vhid']}"] = $vip['subnet']; $iflist["{$vip['interface']}_vip{$vip['vhid']}"] = $vip['subnet'];
} }
...@@ -618,14 +677,14 @@ function get_configured_ip_aliases_list($returnfullentry = false) ...@@ -618,14 +677,14 @@ function get_configured_ip_aliases_list($returnfullentry = false)
$viparr = &$config['virtualip']['vip']; $viparr = &$config['virtualip']['vip'];
foreach ($viparr as $vip) { foreach ($viparr as $vip) {
if ($vip['mode']=="ipalias") { if ($vip['mode']=="ipalias") {
if ($returnfullentry) if ($returnfullentry) {
$alias_list[$vip['subnet']] = $vip; $alias_list[$vip['subnet']] = $vip;
else } else {
$alias_list[$vip['subnet']] = $vip['interface']; $alias_list[$vip['subnet']] = $vip['interface'];
} }
} }
} }
}
return $alias_list; return $alias_list;
} }
...@@ -633,40 +692,40 @@ function get_configured_ip_aliases_list($returnfullentry = false) ...@@ -633,40 +692,40 @@ function get_configured_ip_aliases_list($returnfullentry = false)
function get_configured_vips_list() function get_configured_vips_list()
{ {
global $config; global $config;
$alias_list=array(); $alias_list=array();
if (isset($config['virtualip']['vip'])) { if (isset($config['virtualip']['vip'])) {
$viparr = &$config['virtualip']['vip']; $viparr = &$config['virtualip']['vip'];
foreach ($viparr as $vip) { foreach ($viparr as $vip) {
if ($vip['mode'] == "carp") if ($vip['mode'] == "carp") {
$alias_list[] = array("ipaddr" => $vip['subnet'], "if" => "{$vip['interface']}_vip{$vip['vhid']}"); $alias_list[] = array("ipaddr" => $vip['subnet'], "if" => "{$vip['interface']}_vip{$vip['vhid']}");
else } else {
$alias_list[] = array("ipaddr" => $vip['subnet'], "if" => $vip['interface']); $alias_list[] = array("ipaddr" => $vip['subnet'], "if" => $vip['interface']);
} }
} }
}
return $alias_list; return $alias_list;
} }
/* comparison function for sorting by the order in which interfaces are normally created */ /* comparison function for sorting by the order in which interfaces are normally created */
function compare_interface_friendly_names($a, $b) { function compare_interface_friendly_names($a, $b)
if ($a == $b) {
if ($a == $b) {
return 0; return 0;
else if ($a == 'wan') } elseif ($a == 'wan') {
return -1; return -1;
else if ($b == 'wan') } elseif ($b == 'wan') {
return 1; return 1;
else if ($a == 'lan') } elseif ($a == 'lan') {
return -1; return -1;
else if ($b == 'lan') } elseif ($b == 'lan') {
return 1; return 1;
}
return strnatcmp($a, $b); return strnatcmp($a, $b);
} }
/* return the configured interfaces list. */ /* return the configured interfaces list. */
function get_configured_interface_list($only_opt = false, $withdisabled = false) { function get_configured_interface_list($only_opt = false, $withdisabled = false)
{
global $config; global $config;
$iflist = array(); $iflist = array();
...@@ -687,47 +746,50 @@ function get_configured_interface_list($only_opt = false, $withdisabled = false) ...@@ -687,47 +746,50 @@ function get_configured_interface_list($only_opt = false, $withdisabled = false)
} }
/* return the configured interfaces list. */ /* return the configured interfaces list. */
function get_configured_interface_list_by_realif($only_opt = false, $withdisabled = false) { function get_configured_interface_list_by_realif ($only_opt = false, $withdisabled = false)
{
global $config; global $config;
$iflist = array(); $iflist = array();
/* if list */ /* if list */
if (isset($config['interfaces'])) { if (isset($config['interfaces'])) {
foreach($config['interfaces'] as $if => $ifdetail) { foreach($config['interfaces'] as $if => $ifdetail) {
if ($only_opt && ($if == "wan" || $if == "lan")) if ($only_opt && ($if == "wan" || $if == "lan")) {
continue; continue;
}
if (isset($ifdetail['enable']) || $withdisabled == true) { if (isset($ifdetail['enable']) || $withdisabled == true) {
$tmpif = get_real_interface($if); $tmpif = get_real_interface($if);
if (!empty($tmpif)) if (!empty($tmpif)) {
$iflist[$tmpif] = $if; $iflist[$tmpif] = $if;
} }
} }
} }
}
return $iflist; return $iflist;
} }
/* return the configured interfaces list with their description. */ /* return the configured interfaces list with their description. */
function get_configured_interface_with_descr($only_opt = false, $withdisabled = false) { function get_configured_interface_with_descr($only_opt = false, $withdisabled = false)
{
global $config; global $config;
$iflist = array(); $iflist = array();
/* if list */ /* if list */
if (isset($config['interfaces'])) { if (isset($config['interfaces'])) {
foreach($config['interfaces'] as $if => $ifdetail) { foreach($config['interfaces'] as $if => $ifdetail) {
if ($only_opt && ($if == "wan" || $if == "lan")) if ($only_opt && ($if == "wan" || $if == "lan")) {
continue; continue;
}
if (isset($ifdetail['enable']) || $withdisabled == true) { if (isset($ifdetail['enable']) || $withdisabled == true) {
if(empty($ifdetail['descr'])) if (empty($ifdetail['descr'])) {
$iflist[$if] = strtoupper($if); $iflist[$if] = strtoupper($if);
else } else {
$iflist[$if] = strtoupper($ifdetail['descr']); $iflist[$if] = strtoupper($ifdetail['descr']);
} }
} }
} }
}
return $iflist; return $iflist;
} }
...@@ -736,7 +798,8 @@ function get_configured_interface_with_descr($only_opt = false, $withdisabled = ...@@ -736,7 +798,8 @@ function get_configured_interface_with_descr($only_opt = false, $withdisabled =
* interfaces IP Addresses * interfaces IP Addresses
* *
*/ */
function get_configured_ip_addresses() { function get_configured_ip_addresses()
{
global $config; global $config;
$ip_array = array(); $ip_array = array();
...@@ -758,7 +821,7 @@ function get_configured_ip_addresses() { ...@@ -758,7 +821,7 @@ function get_configured_ip_addresses() {
if (isset($config['pppoes']['pppoe'])) { if (isset($config['pppoes']['pppoe'])) {
foreach($config['pppoes']['pppoe'] as $pppoe) { foreach($config['pppoes']['pppoe'] as $pppoe) {
if ($pppoe['mode'] == "server") { if ($pppoe['mode'] == "server") {
if(is_ipaddr($pppoe['localip'])) { if (is_ipaddr($pppoe['localip'])) {
$int = "pppoes". $pppoe['pppoeid']; $int = "pppoes". $pppoe['pppoeid'];
$ip_array[$int] = $pppoe['localip']; $ip_array[$int] = $pppoe['localip'];
} }
...@@ -778,16 +841,18 @@ function get_configured_ipv6_addresses() ...@@ -778,16 +841,18 @@ function get_configured_ipv6_addresses()
{ {
$ipv6_array = array(); $ipv6_array = array();
$interfaces = get_configured_interface_list(); $interfaces = get_configured_interface_list();
if(is_array($interfaces)) { if (is_array($interfaces)) {
foreach($interfaces as $int) { foreach($interfaces as $int) {
$ipaddrv6 = get_interface_ipv6($int); $ipaddrv6 = get_interface_ipv6($int);
$ipv6_array[$int] = $ipaddrv6; $ipv6_array[$int] = $ipaddrv6;
} }
} }
$interfaces = get_configured_carp_interface_list(); $interfaces = get_configured_carp_interface_list();
if(is_array($interfaces)) if (is_array($interfaces)) {
foreach($interfaces as $int => $ipaddrv6) foreach($interfaces as $int => $ipaddrv6) {
$ipv6_array[$int] = $ipaddrv6; $ipv6_array[$int] = $ipaddrv6;
}
}
return $ipv6_array; return $ipv6_array;
} }
...@@ -914,7 +979,8 @@ function log_error($error) ...@@ -914,7 +979,8 @@ function log_error($error)
* NOTES * NOTES
* This function returns the command's stdout and stderr. * This function returns the command's stdout and stderr.
******/ ******/
function exec_command($command) { function exec_command($command)
{
$output = array(); $output = array();
exec($command . ' 2>&1', $output); exec($command . ' 2>&1', $output);
return(implode("\n", $output)); return(implode("\n", $output));
...@@ -963,26 +1029,30 @@ function mwexec_bg($command, $mute = false) ...@@ -963,26 +1029,30 @@ function mwexec_bg($command, $mute = false)
/* check if an alias exists */ /* check if an alias exists */
function is_alias($name) { function is_alias($name)
{
global $aliastable; global $aliastable;
return array_key_exists($name, $aliastable); return array_key_exists($name, $aliastable);
} }
function alias_get_type($name) { function alias_get_type($name)
{
global $config; global $config;
if (is_array($config['aliases']['alias'])) { if (is_array($config['aliases']['alias'])) {
foreach ($config['aliases']['alias'] as $alias) { foreach ($config['aliases']['alias'] as $alias) {
if ($name == $alias['name']) if ($name == $alias['name']) {
return $alias['type']; return $alias['type'];
} }
} }
}
return ""; return "";
} }
/* expand a host or network alias, if necessary */ /* expand a host or network alias, if necessary */
function alias_expand($name) { function alias_expand($name)
{
global $aliastable; global $aliastable;
if (array_key_exists($name, $aliastable)) { if (array_key_exists($name, $aliastable)) {
...@@ -994,32 +1064,34 @@ function alias_expand($name) { ...@@ -994,32 +1064,34 @@ function alias_expand($name) {
} }
} }
function subnet_size($subnet) { function subnet_size($subnet)
{
if (is_subnetv4($subnet)) { if (is_subnetv4($subnet)) {
list ($ip, $bits) = explode("/", $subnet); list ($ip, $bits) = explode("/", $subnet);
return round(exp(log(2) * (32 - $bits))); return round(exp(log(2) * (32 - $bits)));
} } elseif (is_subnetv6($subnet)) {
else if (is_subnetv6($subnet)) {
list ($ip, $bits) = explode("/", $subnet); list ($ip, $bits) = explode("/", $subnet);
return round(exp(log(2) * (128 - $bits))); return round(exp(log(2) * (128 - $bits)));
} } else {
else {
return 0; return 0;
} }
} }
/* find out whether two subnets overlap */ /* find out whether two subnets overlap */
function check_subnets_overlap($subnet1, $bits1, $subnet2, $bits2) { function check_subnets_overlap($subnet1, $bits1, $subnet2, $bits2)
{
if (!is_numeric($bits1)) if (!is_numeric($bits1)) {
$bits1 = 32; $bits1 = 32;
if (!is_numeric($bits2)) }
if (!is_numeric($bits2)) {
$bits2 = 32; $bits2 = 32;
}
if ($bits1 < $bits2) if ($bits1 < $bits2) {
$relbits = $bits1; $relbits = $bits1;
else } else {
$relbits = $bits2; $relbits = $bits2;
}
$sn1 = gen_subnet_mask_long($relbits) & ip2long($subnet1); $sn1 = gen_subnet_mask_long($relbits) & ip2long($subnet1);
$sn2 = gen_subnet_mask_long($relbits) & ip2long($subnet2); $sn2 = gen_subnet_mask_long($relbits) & ip2long($subnet2);
...@@ -1028,18 +1100,21 @@ function check_subnets_overlap($subnet1, $bits1, $subnet2, $bits2) { ...@@ -1028,18 +1100,21 @@ function check_subnets_overlap($subnet1, $bits1, $subnet2, $bits2) {
} }
/* compare two IP addresses */ /* compare two IP addresses */
function ipcmp($a, $b) { function ipcmp($a, $b)
if (ip_less_than($a, $b)) {
if (ip_less_than($a, $b)) {
return -1; return -1;
else if (ip_greater_than($a, $b)) } elseif (ip_greater_than($a, $b)) {
return 1; return 1;
else } else {
return 0; return 0;
}
} }
/* return true if $addr is in $subnet, false if not */ /* return true if $addr is in $subnet, false if not */
function ip_in_subnet($addr,$subnet) { function ip_in_subnet($addr,$subnet)
if(is_ipaddrv6($addr)) { {
if (is_ipaddrv6($addr)) {
return (Net_IPv6::isInNetmask($addr, $subnet)); return (Net_IPv6::isInNetmask($addr, $subnet));
} else { /* XXX: Maybe check for IPv4 */ } else { /* XXX: Maybe check for IPv4 */
list($ip, $mask) = explode('/', $subnet); list($ip, $mask) = explode('/', $subnet);
...@@ -1057,24 +1132,23 @@ function resolve_retry($hostname, $retries = 5) ...@@ -1057,24 +1132,23 @@ function resolve_retry($hostname, $retries = 5)
for ($i = 0; $i < $retries; $i++) { for ($i = 0; $i < $retries; $i++) {
// FIXME: gethostbyname does not work for AAAA hostnames, boo, hiss // FIXME: gethostbyname does not work for AAAA hostnames, boo, hiss
$ip = gethostbyname($hostname); $ip = gethostbyname($hostname);
if ($ip && $ip != $hostname) { if ($ip && $ip != $hostname) {
/* success */ /* success */
return $ip; return $ip;
} }
sleep(1); sleep(1);
} }
return false; return false;
} }
function format_bytes($bytes) { function format_bytes($bytes)
{
if ($bytes >= 1073741824) { if ($bytes >= 1073741824) {
return sprintf("%.2f GB", $bytes/1073741824); return sprintf("%.2f GB", $bytes/1073741824);
} else if ($bytes >= 1048576) { } elseif ($bytes >= 1048576) {
return sprintf("%.2f MB", $bytes/1048576); return sprintf("%.2f MB", $bytes/1048576);
} else if ($bytes >= 1024) { } elseif ($bytes >= 1024) {
return sprintf("%.0f KB", $bytes/1024); return sprintf("%.0f KB", $bytes/1024);
} else { } else {
return sprintf("%d bytes", $bytes); return sprintf("%d bytes", $bytes);
...@@ -1105,17 +1179,19 @@ function update_filter_reload_status($text, $first = false) ...@@ -1105,17 +1179,19 @@ function update_filter_reload_status($text, $first = false)
* RESULT * RESULT
* $dir_array - array containing the directory's contents. This array will be empty if the path specified is invalid. * $dir_array - array containing the directory's contents. This array will be empty if the path specified is invalid.
******/ ******/
function return_dir_as_array($dir, $filter_regex = '') { function return_dir_as_array($dir, $filter_regex = '')
{
$dir_array = array(); $dir_array = array();
if (is_dir($dir)) { if (is_dir($dir)) {
if ($dh = opendir($dir)) { if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) { while (($file = readdir($dh)) !== false) {
if (($file == ".") || ($file == "..")) if (($file == ".") || ($file == "..")) {
continue; continue;
}
if (empty($filter_regex) || preg_match($filter_regex, $file)) if (empty($filter_regex) || preg_match($filter_regex, $file)) {
array_push($dir_array, $file); array_push($dir_array, $file);
} }
}
closedir($dh); closedir($dh);
} }
} }
...@@ -1127,25 +1203,29 @@ function return_dir_as_array($dir, $filter_regex = '') { ...@@ -1127,25 +1203,29 @@ function return_dir_as_array($dir, $filter_regex = '') {
* Get values of sysctl OID's listed in $names (accepts an array or a single * Get values of sysctl OID's listed in $names (accepts an array or a single
* name) and return an array of key/value pairs set for those that exist * name) and return an array of key/value pairs set for those that exist
*/ */
function get_sysctl($names) { function get_sysctl($names)
if (empty($names)) {
if (empty($names)) {
return array(); return array();
}
if (is_array($names)) { if (is_array($names)) {
$name_list = array(); $name_list = array();
foreach ($names as $name) { foreach ($names as $name) {
$name_list[] = escapeshellarg($name); $name_list[] = escapeshellarg($name);
} }
} else } else {
$name_list = array(escapeshellarg($names)); $name_list = array(escapeshellarg($names));
}
exec("/sbin/sysctl -i " . implode(" ", $name_list), $output); exec("/sbin/sysctl -i " . implode(" ", $name_list), $output);
$values = array(); $values = array();
foreach ($output as $line) { foreach ($output as $line) {
$line = explode(": ", $line, 2); $line = explode(": ", $line, 2);
if (count($line) == 2) if (count($line) == 2) {
$values[$line[0]] = $line[1]; $values[$line[0]] = $line[1];
} }
}
return $values; return $values;
} }
...@@ -1155,14 +1235,15 @@ function get_sysctl($names) { ...@@ -1155,14 +1235,15 @@ function get_sysctl($names) {
* Wrapper for get_sysctl() to simplify read of a single sysctl value * Wrapper for get_sysctl() to simplify read of a single sysctl value
* return the value for sysctl $name or empty string if it doesn't exist * return the value for sysctl $name or empty string if it doesn't exist
*/ */
function get_single_sysctl($name) { function get_single_sysctl($name)
if (empty($name)) {
if (empty($name)) {
return ""; return "";
}
$value = get_sysctl($name); $value = get_sysctl($name);
if (empty($value) || !isset($value[$name])) if (empty($value) || !isset($value[$name])) {
return ""; return "";
}
return $value[$name]; return $value[$name];
} }
...@@ -1171,9 +1252,11 @@ function get_single_sysctl($name) { ...@@ -1171,9 +1252,11 @@ function get_single_sysctl($name) {
* Set sysctl OID's listed as key/value pairs and return * Set sysctl OID's listed as key/value pairs and return
* an array with keys set for those that succeeded * an array with keys set for those that succeeded
*/ */
function set_sysctl($values) { function set_sysctl($values)
if (empty($values)) {
if (empty($values)) {
return array(); return array();
}
$value_list = array(); $value_list = array();
foreach ($values as $key => $value) { foreach ($values as $key => $value) {
...@@ -1192,9 +1275,10 @@ function set_sysctl($values) { ...@@ -1192,9 +1275,10 @@ function set_sysctl($values) {
$ret = array(); $ret = array();
foreach ($output as $line) { foreach ($output as $line) {
$line = explode(": ", $line, 2); $line = explode(": ", $line, 2);
if (count($line) == 2) if (count($line) == 2) {
$ret[$line[0]] = true; $ret[$line[0]] = true;
} }
}
return $ret; return $ret;
} }
...@@ -1204,15 +1288,15 @@ function set_sysctl($values) { ...@@ -1204,15 +1288,15 @@ function set_sysctl($values) {
* Wrapper to set_sysctl() to make it simple to set only one sysctl * Wrapper to set_sysctl() to make it simple to set only one sysctl
* returns boolean meaning if it suceed * returns boolean meaning if it suceed
*/ */
function set_single_sysctl($name, $value) { function set_single_sysctl($name, $value)
if (empty($name)) {
if (empty($name)) {
return false; return false;
}
$result = set_sysctl(array($name => $value)); $result = set_sysctl(array($name => $value));
if (!isset($result[$name]) || $result[$name] != $value) {
if (!isset($result[$name]) || $result[$name] != $value)
return false; return false;
}
return true; return true;
} }
...@@ -1240,7 +1324,8 @@ function unmute_kernel_msgs() ...@@ -1240,7 +1324,8 @@ function unmute_kernel_msgs()
* RESULT * RESULT
* returns newly sorted array * returns newly sorted array
******/ ******/
function msort($array, $id="id", $sort_ascending=true) { function msort($array, $id="id", $sort_ascending=true)
{
$temp_array = array(); $temp_array = array();
while(count($array)>0) { while(count($array)>0) {
$lowest_id = 0; $lowest_id = 0;
...@@ -1273,10 +1358,12 @@ function msort($array, $id="id", $sort_ascending=true) { ...@@ -1273,10 +1358,12 @@ function msort($array, $id="id", $sort_ascending=true) {
* RESULT * RESULT
* Returns true if item is a URL * Returns true if item is a URL
******/ ******/
function is_URL($url) { function is_URL($url)
{
$match = preg_match("'\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))'", $url); $match = preg_match("'\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))'", $url);
if($match) if ($match) {
return true; return true;
}
return false; return false;
} }
...@@ -1294,19 +1381,20 @@ function get_staticroutes($returnsubnetsonly = false, $returnhostnames = false) ...@@ -1294,19 +1381,20 @@ function get_staticroutes($returnsubnetsonly = false, $returnhostnames = false)
/* Loop through routes and expand aliases as we find them. */ /* Loop through routes and expand aliases as we find them. */
foreach ($config['staticroutes']['route'] as $route) { foreach ($config['staticroutes']['route'] as $route) {
if (is_alias($route['network'])) { if (is_alias($route['network'])) {
if (!isset($aliastable[$route['network']])) if (!isset($aliastable[$route['network']])) {
continue; continue;
}
$subnets = preg_split('/\s+/', $aliastable[$route['network']]); $subnets = preg_split('/\s+/', $aliastable[$route['network']]);
foreach ($subnets as $net) { foreach ($subnets as $net) {
if (!is_subnet($net)) { if (!is_subnet($net)) {
if (is_ipaddrv4($net)) if (is_ipaddrv4($net)) {
$net .= "/32"; $net .= "/32";
else if (is_ipaddrv6($net)) } elseif (is_ipaddrv6($net)) {
$net .= "/128"; $net .= "/128";
else if ($returnhostnames === false || !is_fqdn($net)) } elseif ($returnhostnames === false || !is_fqdn($net)) {
continue; continue;
} }
}
$temproute = $route; $temproute = $route;
$temproute['network'] = $net; $temproute['network'] = $net;
$allstaticroutes[] = $temproute; $allstaticroutes[] = $temproute;
...@@ -1317,10 +1405,11 @@ function get_staticroutes($returnsubnetsonly = false, $returnhostnames = false) ...@@ -1317,10 +1405,11 @@ function get_staticroutes($returnsubnetsonly = false, $returnhostnames = false)
$allsubnets[] = $route['network']; $allsubnets[] = $route['network'];
} }
} }
if ($returnsubnetsonly) if ($returnsubnetsonly) {
return $allsubnets; return $allsubnets;
else } else {
return $allstaticroutes; return $allstaticroutes;
}
} }
/****f* util/get_alias_list /****f* util/get_alias_list
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment