Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
OpnSense
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
OpnSense
Commits
ba62824a
Commit
ba62824a
authored
Apr 11, 2016
by
Ad Schellevis
Committed by
Franco Fichtner
Apr 27, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(legacy) whitespace and curly braces in util.inc
(cherry picked from commit
db254cb8
)
parent
e3b8f0f1
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
1015 additions
and
926 deletions
+1015
-926
util.inc
src/etc/inc/util.inc
+1015
-926
No files found.
src/etc/inc/util.inc
View file @
ba62824a
...
...
@@ -117,21 +117,26 @@ function unlock($cfglckkey = null)
}
/* 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 the subnet address given a host address and a subnet bit count */
function
gen_subnet
(
$ipaddr
,
$bits
)
{
if
(
!
is_ipaddr
(
$ipaddr
)
||
!
is_numeric
(
$bits
))
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
))
function
gen_subnetv6
(
$ipaddr
,
$bits
)
{
if
(
!
is_ipaddrv6
(
$ipaddr
)
||
!
is_numeric
(
$bits
))
{
return
""
;
}
$address
=
Net_IPv6
::
getNetmask
(
$ipaddr
,
$bits
);
$address
=
Net_IPv6
::
compress
(
$address
);
...
...
@@ -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 */
function
gen_subnet_max
(
$ipaddr
,
$bits
)
{
if
(
!
is_ipaddr
(
$ipaddr
)
||
!
is_numeric
(
$bits
))
if
(
!
is_ipaddr
(
$ipaddr
)
||
!
is_numeric
(
$bits
))
{
return
""
;
}
return
long2ip32
(
ip2long
(
$ipaddr
)
|
~
gen_subnet_mask_long
(
$bits
));
}
/* Generate end number for a given ipv6 subnet mask */
function
gen_subnetv6_max
(
$ipaddr
,
$bits
)
{
if
(
!
is_ipaddrv6
(
$ipaddr
))
function
gen_subnetv6_max
(
$ipaddr
,
$bits
)
{
if
(
!
is_ipaddrv6
(
$ipaddr
))
{
return
false
;
}
$mask
=
Net_IPv6
::
getNetmask
(
'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF'
,
$bits
);
$inet_ip
=
(
binary
)
inet_pton
(
$ipaddr
);
$inet_mask
=
(
binary
)
inet_pton
(
$mask
);
...
...
@@ -162,7 +169,8 @@ function gen_subnetv6_max($ipaddr, $bits) {
}
/* returns a subnet mask (long given a bit count) */
function
gen_subnet_mask_long
(
$bits
)
{
function
gen_subnet_mask_long
(
$bits
)
{
$sm
=
0
;
for
(
$i
=
0
;
$i
<
$bits
;
$i
++
)
{
$sm
>>=
1
;
...
...
@@ -172,29 +180,34 @@ function gen_subnet_mask_long($bits) {
}
/* same as above but returns a string */
function
gen_subnet_mask
(
$bits
)
{
function
gen_subnet_mask
(
$bits
)
{
return
long2ip
(
gen_subnet_mask_long
(
$bits
));
}
/* Convert long int to IP address, truncating to 32-bits. */
function
long2ip32
(
$ip
)
{
function
long2ip32
(
$ip
)
{
return
long2ip
(
$ip
&
0xFFFFFFFF
);
}
/* 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
);
}
/* Convert IP address to unsigned long int. */
function
ip2ulong
(
$ip
)
{
function
ip2ulong
(
$ip
)
{
return
sprintf
(
"%u"
,
ip2long32
(
$ip
));
}
/* 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
*/
function
ip_range_size
(
$startip
,
$endip
)
{
function
ip_range_size
(
$startip
,
$endip
)
{
if
(
is_ipaddr
(
$startip
)
&&
is_ipaddr
(
$endip
))
{
// Operate as unsigned long because otherwise it wouldn't work
// when crossing over from 127.255.255.255 / 128.0.0.0 barrier
...
...
@@ -206,7 +219,8 @@ function ip_range_size($startip, $endip) {
/* 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
*/
function
find_smallest_cidr
(
$number
)
{
function
find_smallest_cidr
(
$number
)
{
$smallest
=
1
;
for
(
$b
=
32
;
$b
>
0
;
$b
--
)
{
$smallest
=
(
$number
<=
pow
(
2
,
$b
))
?
$b
:
$smallest
;
...
...
@@ -215,31 +229,36 @@ function find_smallest_cidr($number) {
}
/* Return the previous IP address before the given address */
function
ip_before
(
$ip
)
{
function
ip_before
(
$ip
)
{
return
long2ip32
(
ip2long
(
$ip
)
-
1
);
}
/* Return the next IP address after the given address */
function
ip_after
(
$ip
)
{
function
ip_after
(
$ip
)
{
return
long2ip32
(
ip2long
(
$ip
)
+
1
);
}
/* 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
// crossing over from 127.255.255.255 / 128.0.0.0 barrier
return
ip2ulong
(
$ip1
)
<
ip2ulong
(
$ip2
);
}
/* 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
// when crossing over from 127.255.255.255 / 128.0.0.0 barrier
return
ip2ulong
(
$ip1
)
>
ip2ulong
(
$ip2
);
}
/* 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
))
{
return
array
();
}
...
...
@@ -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 */
function
is_ipaddr
(
$ipaddr
)
{
if
(
is_ipaddrv4
(
$ipaddr
))
{
function
is_ipaddr
(
$ipaddr
)
{
if
(
is_ipaddrv4
(
$ipaddr
))
{
return
true
;
}
if
(
is_ipaddrv6
(
$ipaddr
))
{
if
(
is_ipaddrv6
(
$ipaddr
))
{
return
true
;
}
return
false
;
}
/* returns true if $ipaddr is a valid IPv6 address */
function
is_ipaddrv6
(
$ipaddr
)
{
if
(
!
is_string
(
$ipaddr
)
||
empty
(
$ipaddr
))
function
is_ipaddrv6
(
$ipaddr
)
{
if
(
!
is_string
(
$ipaddr
)
||
empty
(
$ipaddr
))
{
return
false
;
}
if
(
strstr
(
$ipaddr
,
"%"
)
&&
is_linklocal
(
$ipaddr
))
{
$tmpip
=
explode
(
"%"
,
$ipaddr
);
$ipaddr
=
$tmpip
[
0
];
...
...
@@ -326,43 +348,51 @@ function is_ipaddrv6($ipaddr) {
}
/* returns true if $ipaddr is a valid dotted IPv4 address */
function
is_ipaddrv4
(
$ipaddr
)
{
if
(
!
is_string
(
$ipaddr
)
||
empty
(
$ipaddr
))
function
is_ipaddrv4
(
$ipaddr
)
{
if
(
!
is_string
(
$ipaddr
)
||
empty
(
$ipaddr
))
{
return
false
;
}
$ip_long
=
ip2long
(
$ipaddr
);
$ip_reverse
=
long2ip32
(
$ip_long
);
if
(
$ipaddr
==
$ip_reverse
)
if
(
$ipaddr
==
$ip_reverse
)
{
return
true
;
else
}
else
{
return
false
;
}
}
/* returns true if $ipaddr is a valid linklocal address */
function
is_linklocal
(
$ipaddr
)
{
function
is_linklocal
(
$ipaddr
)
{
return
(
strtolower
(
substr
(
$ipaddr
,
0
,
5
))
==
"fe80:"
);
}
/* returns scope of a linklocal address */
function
get_ll_scope
(
$addr
)
{
if
(
!
is_linklocal
(
$addr
)
||
!
strstr
(
$addr
,
"%"
))
function
get_ll_scope
(
$addr
)
{
if
(
!
is_linklocal
(
$addr
)
||
!
strstr
(
$addr
,
"%"
))
{
return
""
;
}
list
(
$ll
,
$scope
)
=
explode
(
"%"
,
$addr
);
return
$scope
;
}
/* returns true if $ipaddr is a valid literal IPv6 address */
function
is_literalipaddrv6
(
$ipaddr
)
{
if
(
preg_match
(
"/\[([0-9a-f:]+)\]/i"
,
$ipaddr
,
$match
))
function
is_literalipaddrv6
(
$ipaddr
)
{
if
(
preg_match
(
"/\[([0-9a-f:]+)\]/i"
,
$ipaddr
,
$match
))
{
$ipaddr
=
$match
[
1
];
else
}
else
{
return
false
;
}
return
is_ipaddrv6
(
$ipaddr
);
}
function
is_ipaddrwithport
(
$ipport
)
{
function
is_ipaddrwithport
(
$ipport
)
{
$parts
=
explode
(
":"
,
$ipport
);
$port
=
array_pop
(
$parts
);
if
(
count
(
$parts
)
==
1
)
{
...
...
@@ -374,7 +404,8 @@ function is_ipaddrwithport($ipport) {
}
}
function
is_hostnamewithport
(
$hostport
)
{
function
is_hostnamewithport
(
$hostport
)
{
$parts
=
explode
(
":"
,
$hostport
);
$port
=
array_pop
(
$parts
);
if
(
count
(
$parts
)
==
1
)
{
...
...
@@ -392,10 +423,11 @@ function is_ipaddroralias($ipaddr)
if
(
is_alias
(
$ipaddr
))
{
if
(
isset
(
$config
[
'aliases'
][
'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
false
;
}
...
...
@@ -405,50 +437,60 @@ function is_ipaddroralias($ipaddr)
/* returns true if $subnet is a valid IPv4 or IPv6 subnet in CIDR format
false - if not a valid 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_ipaddrv4
(
$parts
[
1
])
&&
$parts
[
3
]
<=
32
)
if
(
is_ipaddrv4
(
$parts
[
1
])
&&
$parts
[
3
]
<=
32
)
{
return
4
;
if
(
is_ipaddrv6
(
$parts
[
2
])
&&
$parts
[
3
]
<=
128
)
}
if
(
is_ipaddrv6
(
$parts
[
2
])
&&
$parts
[
3
]
<=
128
)
{
return
6
;
}
}
return
false
;
}
/* same as is_subnet() but accepts IPv4 only */
function
is_subnetv4
(
$subnet
)
{
function
is_subnetv4
(
$subnet
)
{
return
(
is_subnet
(
$subnet
)
==
4
);
}
/* same as is_subnet() but accepts IPv6 only */
function
is_subnetv6
(
$subnet
)
{
function
is_subnetv6
(
$subnet
)
{
return
(
is_subnet
(
$subnet
)
==
6
);
}
/* returns true if $hostname is a valid hostname */
function
is_hostname
(
$hostname
)
{
if
(
!
is_string
(
$hostname
))
function
is_hostname
(
$hostname
)
{
if
(
!
is_string
(
$hostname
))
{
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
;
else
}
else
{
return
false
;
}
}
/* returns true if $domain is a valid domain name */
function
is_domain
(
$domain
)
{
if
(
!
is_string
(
$domain
))
function
is_domain
(
$domain
)
{
if
(
!
is_string
(
$domain
))
{
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
;
else
}
else
{
return
false
;
}
}
/* 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'
;
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) {
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" */
function
is_validaliasname
(
$name
)
{
function
is_validaliasname
(
$name
)
{
/* Array of reserved words */
$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
;
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
true
;
}
/* returns true if $port is a valid TCP/UDP port */
function
is_port
(
$port
)
{
if
(
getservbyname
(
$port
,
"tcp"
)
||
getservbyname
(
$port
,
"udp"
))
function
is_port
(
$port
)
{
if
(
getservbyname
(
$port
,
"tcp"
)
||
getservbyname
(
$port
,
"udp"
))
{
return
true
;
if
(
!
ctype_digit
(
$port
))
}
if
(
!
ctype_digit
(
$port
))
{
return
false
;
else
if
((
intval
(
$port
)
<
1
)
||
(
intval
(
$port
)
>
65535
))
}
elseif
((
intval
(
$port
)
<
1
)
||
(
intval
(
$port
)
>
65535
))
{
return
false
;
}
return
true
;
}
/* returns true if $portrange is a valid TCP/UDP portrange ("<port>:<port>") */
function
is_portrange
(
$portrange
)
{
function
is_portrange
(
$portrange
)
{
$ports
=
explode
(
":"
,
$portrange
);
return
(
count
(
$ports
)
==
2
&&
is_port
(
$ports
[
0
])
&&
is_port
(
$ports
[
1
]));
}
...
...
@@ -498,10 +546,11 @@ function is_portoralias($port)
if
(
is_alias
(
$port
))
{
if
(
isset
(
$config
[
'aliases'
][
'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
false
;
}
...
...
@@ -509,9 +558,11 @@ function is_portoralias($port)
}
/* create ranges of sequential port numbers (200:215) and remove duplicates */
function
group_ports
(
$ports
)
{
if
(
!
is_array
(
$ports
)
||
empty
(
$ports
))
function
group_ports
(
$ports
)
{
if
(
!
is_array
(
$ports
)
||
empty
(
$ports
))
{
return
;
}
$uniq
=
array
();
foreach
(
$ports
as
$port
)
{
...
...
@@ -522,14 +573,17 @@ function group_ports($ports) {
$begin
=
$end
;
$end
=
$aux
;
}
for
(
$i
=
$begin
;
$i
<=
$end
;
$i
++
)
if
(
!
in_array
(
$i
,
$uniq
))
for
(
$i
=
$begin
;
$i
<=
$end
;
$i
++
)
{
if
(
!
in_array
(
$i
,
$uniq
))
{
$uniq
[]
=
$i
;
}
else
if
(
is_port
(
$port
))
{
if
(
!
in_array
(
$port
,
$uniq
))
}
}
}
elseif
(
is_port
(
$port
))
{
if
(
!
in_array
(
$port
,
$uniq
))
{
$uniq
[]
=
$port
;
}
}
}
sort
(
$uniq
,
SORT_NUMERIC
);
$result
=
array
();
...
...
@@ -540,11 +594,11 @@ function group_ports($ports) {
}
$last
=
end
(
$result
);
if
(
is_portrange
(
$last
))
if
(
is_portrange
(
$last
))
{
list
(
$begin
,
$end
)
=
explode
(
":"
,
$last
);
else
}
else
{
$begin
=
$end
=
$last
;
}
if
(
$port
==
(
$end
+
1
))
{
$end
++
;
$result
[
count
(
$result
)
-
1
]
=
"
{
$begin
}
:
{
$end
}
"
;
...
...
@@ -557,19 +611,23 @@ function group_ports($ports) {
}
/* returns true if $test is in the range between $start and $end */
function
is_inrange_v4
(
$test
,
$start
,
$end
)
{
if
(
(
ip2ulong
(
$test
)
<=
ip2ulong
(
$end
))
&&
(
ip2ulong
(
$test
)
>=
ip2ulong
(
$start
))
)
function
is_inrange_v4
(
$test
,
$start
,
$end
)
{
if
(
(
ip2ulong
(
$test
)
<=
ip2ulong
(
$end
))
&&
(
ip2ulong
(
$test
)
>=
ip2ulong
(
$start
))
)
{
return
true
;
else
}
else
{
return
false
;
}
}
/* returns true if $test is in the range between $start and $end */
function
is_inrange_v6
(
$test
,
$start
,
$end
)
{
if
(
(
inet_pton
(
$test
)
<=
inet_pton
(
$end
))
&&
(
inet_pton
(
$test
)
>=
inet_pton
(
$start
))
)
function
is_inrange_v6
(
$test
,
$start
,
$end
)
{
if
(
(
inet_pton
(
$test
)
<=
inet_pton
(
$end
))
&&
(
inet_pton
(
$test
)
>=
inet_pton
(
$start
))
)
{
return
true
;
else
}
else
{
return
false
;
}
}
/* 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
case
"carp"
:
if
(
!
empty
(
$carpinterface
))
{
if
(
$carpinterface
==
"
{
$vip
[
'interface'
]
}
_vip
{
$vip
[
'vhid'
]
}
"
)
{
if
(
$family
==
"inet"
&&
is_ipaddrv4
(
$vip
[
'subnet'
]))
if
(
$family
==
"inet"
&&
is_ipaddrv4
(
$vip
[
'subnet'
]))
{
return
$vip
[
'subnet'
];
else
if
(
$family
==
"inet6"
&&
is_ipaddrv6
(
$vip
[
'subnet'
]))
}
elseif
(
$family
==
"inet6"
&&
is_ipaddrv6
(
$vip
[
'subnet'
]))
{
return
$vip
[
'subnet'
];
}
}
}
else
{
$iflist
[
"
{
$vip
[
'interface'
]
}
_vip
{
$vip
[
'vhid'
]
}
"
]
=
$vip
[
'subnet'
];
}
...
...
@@ -618,14 +677,14 @@ function get_configured_ip_aliases_list($returnfullentry = false)
$viparr
=
&
$config
[
'virtualip'
][
'vip'
];
foreach
(
$viparr
as
$vip
)
{
if
(
$vip
[
'mode'
]
==
"ipalias"
)
{
if
(
$returnfullentry
)
if
(
$returnfullentry
)
{
$alias_list
[
$vip
[
'subnet'
]]
=
$vip
;
else
}
else
{
$alias_list
[
$vip
[
'subnet'
]]
=
$vip
[
'interface'
];
}
}
}
}
return
$alias_list
;
}
...
...
@@ -633,40 +692,40 @@ function get_configured_ip_aliases_list($returnfullentry = false)
function
get_configured_vips_list
()
{
global
$config
;
$alias_list
=
array
();
if
(
isset
(
$config
[
'virtualip'
][
'vip'
]))
{
$viparr
=
&
$config
[
'virtualip'
][
'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'
]
}
"
);
else
}
else
{
$alias_list
[]
=
array
(
"ipaddr"
=>
$vip
[
'subnet'
],
"if"
=>
$vip
[
'interface'
]);
}
}
}
return
$alias_list
;
}
/* comparison function for sorting by the order in which interfaces are normally created */
function
compare_interface_friendly_names
(
$a
,
$b
)
{
if
(
$a
==
$b
)
function
compare_interface_friendly_names
(
$a
,
$b
)
{
if
(
$a
==
$b
)
{
return
0
;
else
if
(
$a
==
'wan'
)
}
elseif
(
$a
==
'wan'
)
{
return
-
1
;
else
if
(
$b
==
'wan'
)
}
elseif
(
$b
==
'wan'
)
{
return
1
;
else
if
(
$a
==
'lan'
)
}
elseif
(
$a
==
'lan'
)
{
return
-
1
;
else
if
(
$b
==
'lan'
)
}
elseif
(
$b
==
'lan'
)
{
return
1
;
}
return
strnatcmp
(
$a
,
$b
);
}
/* 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
;
$iflist
=
array
();
...
...
@@ -687,47 +746,50 @@ function get_configured_interface_list($only_opt = false, $withdisabled = false)
}
/* 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
;
$iflist
=
array
();
/* if list */
if
(
isset
(
$config
[
'interfaces'
]))
{
foreach
(
$config
[
'interfaces'
]
as
$if
=>
$ifdetail
)
{
if
(
$only_opt
&&
(
$if
==
"wan"
||
$if
==
"lan"
))
if
(
$only_opt
&&
(
$if
==
"wan"
||
$if
==
"lan"
))
{
continue
;
}
if
(
isset
(
$ifdetail
[
'enable'
])
||
$withdisabled
==
true
)
{
$tmpif
=
get_real_interface
(
$if
);
if
(
!
empty
(
$tmpif
))
if
(
!
empty
(
$tmpif
))
{
$iflist
[
$tmpif
]
=
$if
;
}
}
}
}
return
$iflist
;
}
/* 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
;
$iflist
=
array
();
/* if list */
if
(
isset
(
$config
[
'interfaces'
]))
{
foreach
(
$config
[
'interfaces'
]
as
$if
=>
$ifdetail
)
{
if
(
$only_opt
&&
(
$if
==
"wan"
||
$if
==
"lan"
))
if
(
$only_opt
&&
(
$if
==
"wan"
||
$if
==
"lan"
))
{
continue
;
}
if
(
isset
(
$ifdetail
[
'enable'
])
||
$withdisabled
==
true
)
{
if
(
empty
(
$ifdetail
[
'descr'
]))
if
(
empty
(
$ifdetail
[
'descr'
]))
{
$iflist
[
$if
]
=
strtoupper
(
$if
);
else
}
else
{
$iflist
[
$if
]
=
strtoupper
(
$ifdetail
[
'descr'
]);
}
}
}
}
return
$iflist
;
}
...
...
@@ -736,7 +798,8 @@ function get_configured_interface_with_descr($only_opt = false, $withdisabled =
* interfaces IP Addresses
*
*/
function
get_configured_ip_addresses
()
{
function
get_configured_ip_addresses
()
{
global
$config
;
$ip_array
=
array
();
...
...
@@ -758,7 +821,7 @@ function get_configured_ip_addresses() {
if
(
isset
(
$config
[
'pppoes'
][
'pppoe'
]))
{
foreach
(
$config
[
'pppoes'
][
'pppoe'
]
as
$pppoe
)
{
if
(
$pppoe
[
'mode'
]
==
"server"
)
{
if
(
is_ipaddr
(
$pppoe
[
'localip'
]))
{
if
(
is_ipaddr
(
$pppoe
[
'localip'
]))
{
$int
=
"pppoes"
.
$pppoe
[
'pppoeid'
];
$ip_array
[
$int
]
=
$pppoe
[
'localip'
];
}
...
...
@@ -778,16 +841,18 @@ function get_configured_ipv6_addresses()
{
$ipv6_array
=
array
();
$interfaces
=
get_configured_interface_list
();
if
(
is_array
(
$interfaces
))
{
if
(
is_array
(
$interfaces
))
{
foreach
(
$interfaces
as
$int
)
{
$ipaddrv6
=
get_interface_ipv6
(
$int
);
$ipv6_array
[
$int
]
=
$ipaddrv6
;
}
}
$interfaces
=
get_configured_carp_interface_list
();
if
(
is_array
(
$interfaces
))
foreach
(
$interfaces
as
$int
=>
$ipaddrv6
)
if
(
is_array
(
$interfaces
))
{
foreach
(
$interfaces
as
$int
=>
$ipaddrv6
)
{
$ipv6_array
[
$int
]
=
$ipaddrv6
;
}
}
return
$ipv6_array
;
}
...
...
@@ -914,7 +979,8 @@ function log_error($error)
* NOTES
* This function returns the command's stdout and stderr.
******/
function
exec_command
(
$command
)
{
function
exec_command
(
$command
)
{
$output
=
array
();
exec
(
$command
.
' 2>&1'
,
$output
);
return
(
implode
(
"
\n
"
,
$output
));
...
...
@@ -963,26 +1029,30 @@ function mwexec_bg($command, $mute = false)
/* check if an alias exists */
function
is_alias
(
$name
)
{
function
is_alias
(
$name
)
{
global
$aliastable
;
return
array_key_exists
(
$name
,
$aliastable
);
}
function
alias_get_type
(
$name
)
{
function
alias_get_type
(
$name
)
{
global
$config
;
if
(
is_array
(
$config
[
'aliases'
][
'alias'
]))
{
foreach
(
$config
[
'aliases'
][
'alias'
]
as
$alias
)
{
if
(
$name
==
$alias
[
'name'
])
if
(
$name
==
$alias
[
'name'
])
{
return
$alias
[
'type'
];
}
}
}
return
""
;
}
/* expand a host or network alias, if necessary */
function
alias_expand
(
$name
)
{
function
alias_expand
(
$name
)
{
global
$aliastable
;
if
(
array_key_exists
(
$name
,
$aliastable
))
{
...
...
@@ -994,32 +1064,34 @@ function alias_expand($name) {
}
}
function
subnet_size
(
$subnet
)
{
function
subnet_size
(
$subnet
)
{
if
(
is_subnetv4
(
$subnet
))
{
list
(
$ip
,
$bits
)
=
explode
(
"/"
,
$subnet
);
return
round
(
exp
(
log
(
2
)
*
(
32
-
$bits
)));
}
else
if
(
is_subnetv6
(
$subnet
))
{
}
elseif
(
is_subnetv6
(
$subnet
))
{
list
(
$ip
,
$bits
)
=
explode
(
"/"
,
$subnet
);
return
round
(
exp
(
log
(
2
)
*
(
128
-
$bits
)));
}
else
{
}
else
{
return
0
;
}
}
/* find out whether two subnets overlap */
function
check_subnets_overlap
(
$subnet1
,
$bits1
,
$subnet2
,
$bits2
)
{
if
(
!
is_numeric
(
$bits1
))
function
check_subnets_overlap
(
$subnet1
,
$bits1
,
$subnet2
,
$bits2
)
{
if
(
!
is_numeric
(
$bits1
))
{
$bits1
=
32
;
if
(
!
is_numeric
(
$bits2
))
}
if
(
!
is_numeric
(
$bits2
))
{
$bits2
=
32
;
}
if
(
$bits1
<
$bits2
)
if
(
$bits1
<
$bits2
)
{
$relbits
=
$bits1
;
else
}
else
{
$relbits
=
$bits2
;
}
$sn1
=
gen_subnet_mask_long
(
$relbits
)
&
ip2long
(
$subnet1
);
$sn2
=
gen_subnet_mask_long
(
$relbits
)
&
ip2long
(
$subnet2
);
...
...
@@ -1028,18 +1100,21 @@ function check_subnets_overlap($subnet1, $bits1, $subnet2, $bits2) {
}
/* compare two IP addresses */
function
ipcmp
(
$a
,
$b
)
{
if
(
ip_less_than
(
$a
,
$b
))
function
ipcmp
(
$a
,
$b
)
{
if
(
ip_less_than
(
$a
,
$b
))
{
return
-
1
;
else
if
(
ip_greater_than
(
$a
,
$b
))
}
elseif
(
ip_greater_than
(
$a
,
$b
))
{
return
1
;
else
}
else
{
return
0
;
}
}
/* return true if $addr is in $subnet, false if not */
function
ip_in_subnet
(
$addr
,
$subnet
)
{
if
(
is_ipaddrv6
(
$addr
))
{
function
ip_in_subnet
(
$addr
,
$subnet
)
{
if
(
is_ipaddrv6
(
$addr
))
{
return
(
Net_IPv6
::
isInNetmask
(
$addr
,
$subnet
));
}
else
{
/* XXX: Maybe check for IPv4 */
list
(
$ip
,
$mask
)
=
explode
(
'/'
,
$subnet
);
...
...
@@ -1057,24 +1132,23 @@ function resolve_retry($hostname, $retries = 5)
for
(
$i
=
0
;
$i
<
$retries
;
$i
++
)
{
// FIXME: gethostbyname does not work for AAAA hostnames, boo, hiss
$ip
=
gethostbyname
(
$hostname
);
if
(
$ip
&&
$ip
!=
$hostname
)
{
/* success */
return
$ip
;
}
sleep
(
1
);
}
return
false
;
}
function
format_bytes
(
$bytes
)
{
function
format_bytes
(
$bytes
)
{
if
(
$bytes
>=
1073741824
)
{
return
sprintf
(
"%.2f GB"
,
$bytes
/
1073741824
);
}
else
if
(
$bytes
>=
1048576
)
{
}
else
if
(
$bytes
>=
1048576
)
{
return
sprintf
(
"%.2f MB"
,
$bytes
/
1048576
);
}
else
if
(
$bytes
>=
1024
)
{
}
else
if
(
$bytes
>=
1024
)
{
return
sprintf
(
"%.0f KB"
,
$bytes
/
1024
);
}
else
{
return
sprintf
(
"%d bytes"
,
$bytes
);
...
...
@@ -1105,17 +1179,19 @@ function update_filter_reload_status($text, $first = false)
* RESULT
* $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
();
if
(
is_dir
(
$dir
))
{
if
(
$dh
=
opendir
(
$dir
))
{
while
((
$file
=
readdir
(
$dh
))
!==
false
)
{
if
((
$file
==
"."
)
||
(
$file
==
".."
))
if
((
$file
==
"."
)
||
(
$file
==
".."
))
{
continue
;
if
(
empty
(
$filter_regex
)
||
preg_match
(
$filter_regex
,
$file
))
}
if
(
empty
(
$filter_regex
)
||
preg_match
(
$filter_regex
,
$file
))
{
array_push
(
$dir_array
,
$file
);
}
}
closedir
(
$dh
);
}
}
...
...
@@ -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
* name) and return an array of key/value pairs set for those that exist
*/
function
get_sysctl
(
$names
)
{
if
(
empty
(
$names
))
function
get_sysctl
(
$names
)
{
if
(
empty
(
$names
))
{
return
array
();
}
if
(
is_array
(
$names
))
{
$name_list
=
array
();
foreach
(
$names
as
$name
)
{
$name_list
[]
=
escapeshellarg
(
$name
);
}
}
else
}
else
{
$name_list
=
array
(
escapeshellarg
(
$names
));
}
exec
(
"/sbin/sysctl -i "
.
implode
(
" "
,
$name_list
),
$output
);
$values
=
array
();
foreach
(
$output
as
$line
)
{
$line
=
explode
(
": "
,
$line
,
2
);
if
(
count
(
$line
)
==
2
)
if
(
count
(
$line
)
==
2
)
{
$values
[
$line
[
0
]]
=
$line
[
1
];
}
}
return
$values
;
}
...
...
@@ -1155,14 +1235,15 @@ function get_sysctl($names) {
* 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
*/
function
get_single_sysctl
(
$name
)
{
if
(
empty
(
$name
))
function
get_single_sysctl
(
$name
)
{
if
(
empty
(
$name
))
{
return
""
;
}
$value
=
get_sysctl
(
$name
);
if
(
empty
(
$value
)
||
!
isset
(
$value
[
$name
]))
if
(
empty
(
$value
)
||
!
isset
(
$value
[
$name
]))
{
return
""
;
}
return
$value
[
$name
];
}
...
...
@@ -1171,9 +1252,11 @@ function get_single_sysctl($name) {
* Set sysctl OID's listed as key/value pairs and return
* an array with keys set for those that succeeded
*/
function
set_sysctl
(
$values
)
{
if
(
empty
(
$values
))
function
set_sysctl
(
$values
)
{
if
(
empty
(
$values
))
{
return
array
();
}
$value_list
=
array
();
foreach
(
$values
as
$key
=>
$value
)
{
...
...
@@ -1192,9 +1275,10 @@ function set_sysctl($values) {
$ret
=
array
();
foreach
(
$output
as
$line
)
{
$line
=
explode
(
": "
,
$line
,
2
);
if
(
count
(
$line
)
==
2
)
if
(
count
(
$line
)
==
2
)
{
$ret
[
$line
[
0
]]
=
true
;
}
}
return
$ret
;
}
...
...
@@ -1204,15 +1288,15 @@ function set_sysctl($values) {
* Wrapper to set_sysctl() to make it simple to set only one sysctl
* returns boolean meaning if it suceed
*/
function
set_single_sysctl
(
$name
,
$value
)
{
if
(
empty
(
$name
))
function
set_single_sysctl
(
$name
,
$value
)
{
if
(
empty
(
$name
))
{
return
false
;
}
$result
=
set_sysctl
(
array
(
$name
=>
$value
));
if
(
!
isset
(
$result
[
$name
])
||
$result
[
$name
]
!=
$value
)
if
(
!
isset
(
$result
[
$name
])
||
$result
[
$name
]
!=
$value
)
{
return
false
;
}
return
true
;
}
...
...
@@ -1240,7 +1324,8 @@ function unmute_kernel_msgs()
* RESULT
* returns newly sorted array
******/
function
msort
(
$array
,
$id
=
"id"
,
$sort_ascending
=
true
)
{
function
msort
(
$array
,
$id
=
"id"
,
$sort_ascending
=
true
)
{
$temp_array
=
array
();
while
(
count
(
$array
)
>
0
)
{
$lowest_id
=
0
;
...
...
@@ -1273,10 +1358,12 @@ function msort($array, $id="id", $sort_ascending=true) {
* RESULT
* 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
);
if
(
$match
)
if
(
$match
)
{
return
true
;
}
return
false
;
}
...
...
@@ -1294,19 +1381,20 @@ function get_staticroutes($returnsubnetsonly = false, $returnhostnames = false)
/* Loop through routes and expand aliases as we find them. */
foreach
(
$config
[
'staticroutes'
][
'route'
]
as
$route
)
{
if
(
is_alias
(
$route
[
'network'
]))
{
if
(
!
isset
(
$aliastable
[
$route
[
'network'
]]))
if
(
!
isset
(
$aliastable
[
$route
[
'network'
]]))
{
continue
;
}
$subnets
=
preg_split
(
'/\s+/'
,
$aliastable
[
$route
[
'network'
]]);
foreach
(
$subnets
as
$net
)
{
if
(
!
is_subnet
(
$net
))
{
if
(
is_ipaddrv4
(
$net
))
if
(
is_ipaddrv4
(
$net
))
{
$net
.=
"/32"
;
else
if
(
is_ipaddrv6
(
$net
))
}
elseif
(
is_ipaddrv6
(
$net
))
{
$net
.=
"/128"
;
else
if
(
$returnhostnames
===
false
||
!
is_fqdn
(
$net
))
}
elseif
(
$returnhostnames
===
false
||
!
is_fqdn
(
$net
))
{
continue
;
}
}
$temproute
=
$route
;
$temproute
[
'network'
]
=
$net
;
$allstaticroutes
[]
=
$temproute
;
...
...
@@ -1317,10 +1405,11 @@ function get_staticroutes($returnsubnetsonly = false, $returnhostnames = false)
$allsubnets
[]
=
$route
[
'network'
];
}
}
if
(
$returnsubnetsonly
)
if
(
$returnsubnetsonly
)
{
return
$allsubnets
;
else
}
else
{
return
$allstaticroutes
;
}
}
/****f* util/get_alias_list
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment