mirror of
https://github.com/lucaspalomodevelop/core.git
synced 2026-03-13 08:09:41 +00:00
interfaces.inc: Improve guess_interface_from_ip() (#4523)
This commit is contained in:
parent
8ded045899
commit
703faa82ff
@ -3757,34 +3757,25 @@ function get_real_interface($interface = 'wan', $family = 'all')
|
||||
|
||||
function guess_interface_from_ip($ipaddress)
|
||||
{
|
||||
if (!is_ipaddr($ipaddress)) {
|
||||
if (is_ipaddrv4($ipaddress)) {
|
||||
$family = "inet";
|
||||
} elseif (is_ipaddrv6($ipaddress)) {
|
||||
$family = "inet6";
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
if (is_ipaddrv4($ipaddress)) {
|
||||
/* create a route table we can search */
|
||||
exec("/usr/bin/netstat -rnWf inet", $output, $ret);
|
||||
foreach ($output as $line) {
|
||||
if (preg_match("/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\/[0-9]+[ ]+link[#]/", $line)) {
|
||||
$fields = preg_split("/[ ]+/", $line);
|
||||
if (ip_in_subnet($ipaddress, $fields[0])) {
|
||||
return $fields[5];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/* FIXME: This works from cursory testing, regexp might need fine tuning */
|
||||
if (is_ipaddrv6($ipaddress)) {
|
||||
/* create a route table we can search */
|
||||
exec("/usr/bin/netstat -rnWf inet6", $output, $ret);
|
||||
foreach ($output as $line) {
|
||||
if (preg_match("/[0-9a-f]+[:]+[0-9a-f]+[:]+[\/][0-9]+/", $line)) {
|
||||
$fields = preg_split("/[ ]+/", $line);
|
||||
if (ip_in_subnet($ipaddress, $fields[0])) {
|
||||
return $fields[5];
|
||||
}
|
||||
|
||||
/* create a route table we can search */
|
||||
exec("/usr/bin/netstat -rnWf ".$family, $output, $ret);
|
||||
foreach ($output as $line) {
|
||||
$fields = preg_split("/\s+/", $line);
|
||||
if (is_subnet($fields[0])) {
|
||||
if (ip_in_subnet($ipaddress, $fields[0])) {
|
||||
return $fields[5];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$ret = exec_command("/sbin/route -n get {$ipaddress} | /usr/bin/awk '/interface/ { print \$2; };'");
|
||||
if (empty($ret)) {
|
||||
return false;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user