interfaces: move function to only caller

This commit is contained in:
Franco Fichtner 2025-02-12 08:34:03 +01:00
parent 6759113b9d
commit 07e1223e34
2 changed files with 42 additions and 42 deletions

View File

@ -3571,48 +3571,6 @@ function get_real_interface($interface = 'wan', $family = 'all')
return $realif;
}
function guess_interface_from_ip($ipaddress)
{
if (is_ipaddrv4($ipaddress)) {
$family = "inet";
} elseif (is_ipaddrv6($ipaddress)) {
$family = "inet6";
} else {
return false;
}
/* create a route table we can search */
exec("/usr/bin/netstat -rnWf " . $family, $output, $ret);
/* search for the route with the largest subnet mask */
$largest_mask = 0;
$best_if = null;
foreach ($output as $line) {
$fields = preg_split("/\s+/", $line);
if (is_subnet($fields[0])) {
if (ip_in_subnet($ipaddress, $fields[0])) {
list($ip, $mask) = explode('/', $fields[0]);
if ($mask > $largest_mask) {
$best_if = $fields[5];
$largest_mask = $mask;
}
}
}
}
if (!empty($best_if)) {
return $best_if;
}
$ret = shell_safe('/sbin/route -n get %s | /usr/bin/awk \'/interface/ { print $2; };\'', $ipaddress);
if (empty($ret)) {
return false;
}
return $ret;
}
function get_interface_number_track6($wanif, $targetif)
{
$list = link_interface_to_track6($wanif);

View File

@ -40,6 +40,48 @@ require_once("XMLRPC_Client.inc");
exit_on_bootup();
function guess_interface_from_ip($ipaddress)
{
if (is_ipaddrv4($ipaddress)) {
$family = "inet";
} elseif (is_ipaddrv6($ipaddress)) {
$family = "inet6";
} else {
return false;
}
/* create a route table we can search */
exec("/usr/bin/netstat -rnWf " . $family, $output, $ret);
/* search for the route with the largest subnet mask */
$largest_mask = 0;
$best_if = null;
foreach ($output as $line) {
$fields = preg_split("/\s+/", $line);
if (is_subnet($fields[0])) {
if (ip_in_subnet($ipaddress, $fields[0])) {
list($ip, $mask) = explode('/', $fields[0]);
if ($mask > $largest_mask) {
$best_if = $fields[5];
$largest_mask = $mask;
}
}
}
}
if (!empty($best_if)) {
return $best_if;
}
$ret = shell_safe('/sbin/route -n get %s | /usr/bin/awk \'/interface/ { print $2; };\'', $ipaddress);
if (empty($ret)) {
return false;
}
return $ret;
}
/**
* fetch carp vips from config with modified advskew for the backup host to use
* @return array