Skip to content

Commit

Permalink
Replace get_configured_ip_addresses() and get_configured_ipv6_address…
Browse files Browse the repository at this point in the history
…es() with a more pragmatic approach, both are used to detect if an address is already configured, which gets quite sloppy when having a large number of interface

s configured.

This change probes all configured interfaces at once, and returns the full list, stripping quite some magic in the process.

Because the indexing is changed, we need to change the consumers as well here.
  • Loading branch information
AdSchellevis committed May 26, 2017
1 parent bb80ad0 commit 9bba209
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 84 deletions.
8 changes: 1 addition & 7 deletions src/etc/inc/auth.inc
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,11 @@ function isAuthLocalIP($http_host)
{
global $config;
$interface_list_ips = get_configured_ip_addresses();
foreach ($interface_list_ips as $ilips) {
foreach ($interface_list_ips as $ilips => $ifname) {
if (strcasecmp($http_host, $ilips) == 0) {
return true;
}
}
$interface_list_ipv6s = get_configured_ipv6_addresses();
foreach ($interface_list_ipv6s as $ilipv6s) {
if (strcasecmp($http_host, $ilipv6s) == 0) {
return true;
}
}
if (isset($config['virtualip']['vip'])) {
foreach ($config['virtualip']['vip'] as $vip) {
if ($vip['subnet'] == $http_host) {
Expand Down
31 changes: 5 additions & 26 deletions src/etc/inc/interfaces.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5028,35 +5028,14 @@ function get_ppp_uptime($port)
function is_ipaddr_configured($ipaddr, $ignore_if = '')
{
global $config;
$if = get_real_interface($ignore_if);
$interface_list_ips = get_configured_ip_addresses();

$isipv6 = is_ipaddrv6($ipaddr);
if ($isipv6 === true) {
$interface_list_ips = get_configured_ipv6_addresses();
if (empty($interface_list_ips[$ipaddr]) || $interface_list_ips[$ipaddr] == $if) {
return false;
} else {
$interface_list_ips = get_configured_ip_addresses();
}

foreach($interface_list_ips as $if => $ilips) {
/* Also ignore CARP interfaces, it'll be checked below */
if ($ignore_if == $if || strstr($ignore_if, "_vip")) {
continue;
}
if (strcasecmp($ipaddr, $ilips) == 0) {
return true;
}
}

$interface_list_vips = get_configured_vips_list(true);
foreach ($interface_list_vips as $id => $vip) {
if ($ignore_if == $vip['if']) {
continue;
}
if (strcasecmp($ipaddr, $vip['ipaddr']) == 0) {
return true;
}
return true;
}

return false;
}

/*
Expand Down
55 changes: 8 additions & 47 deletions src/etc/inc/util.inc
Original file line number Diff line number Diff line change
Expand Up @@ -827,67 +827,28 @@ function get_configured_interface_with_descr($only_opt = false, $withdisabled =

/*
* get_configured_ip_addresses() - Return a list of all configured
* interfaces IP Addresses
* interfaces IP Addresses (ipv4+ipv6)
*
*/
function get_configured_ip_addresses()
{
global $config;

$ip_array = array();
$interfaces = get_configured_interface_list();
if (is_array($interfaces)) {
foreach($interfaces as $int) {
$ipaddr = get_interface_ip($int);
$ip_array[$int] = $ipaddr;
}
}
$interfaces = get_configured_carp_interface_list();
if (is_array($interfaces)) {
foreach($interfaces as $int => $ipaddr) {
$ip_array[$int] = $ipaddr;
}
}

/* pppoe server */
if (isset($config['pppoes']['pppoe'])) {
foreach($config['pppoes']['pppoe'] as $pppoe) {
if ($pppoe['mode'] == "server") {
if (is_ipaddr($pppoe['localip'])) {
$int = "pppoes". $pppoe['pppoeid'];
$ip_array[$int] = $pppoe['localip'];
foreach (legacy_interfaces_details() as $ifname => $if) {
foreach (array('ipv4', 'ipv6') as $iptype) {
if (!empty($if[$iptype])) {
foreach ($if[$iptype] as $addr) {
if (!empty($addr['ipaddr'])) {
$ip_array[$addr['ipaddr']] = $ifname;
}
}
}
}
}

return $ip_array;
}

/*
* get_configured_ipv6_addresses() - Return a list of all configured
* interfaces IPv6 Addresses
*
*/
function get_configured_ipv6_addresses()
{
$ipv6_array = array();
$interfaces = get_configured_interface_list();
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) {
$ipv6_array[$int] = $ipaddrv6;
}
}
return $ipv6_array;
}

/*
* get_interface_list() - Return a list of all physical interfaces
* along with MAC, IPv4 and status.
Expand Down
3 changes: 0 additions & 3 deletions src/www/firewall_virtual_ip_edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ function find_last_used_vhid() {
$input_errors[] = gettext("A valid IP address must be specified.");
} else {
$ignore_if = isset($id) ? $a_vip[$id]['interface'] : $pconfig['interface'];
if ($pconfig['mode'] == 'carp') {
$ignore_if .= "_vip{$pconfig['vhid']}";
}
if (is_ipaddr_configured($pconfig['subnet'], $ignore_if)) {
$input_errors[] = gettext("This IP address is being used by another interface or VIP.");
}
Expand Down
2 changes: 1 addition & 1 deletion src/www/services_dnsmasq_domainoverride_edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
if (!empty($pconfig['port']) && !is_port($pconfig['port'])) {
$input_errors[] = gettext("A valid port number must be specified.");
}
if (!empty($pconfig['dnssrcip']) && !in_array($pconfig['dnssrcip'], get_configured_ip_addresses())) {
if (!empty($pconfig['dnssrcip']) && !in_array($pconfig['dnssrcip'], array_keys(get_configured_ip_addresses()))) {
$input_errors[] = gettext("An interface IP address must be specified for the DNS query source.");
}
if (count($input_errors) == 0) {
Expand Down

0 comments on commit 9bba209

Please sign in to comment.