Updated guess_interface_from_ip to more accurately identify the interface using the subnet with the largest mask in the route table. (#5281)

This commit is contained in:
Jason Crowley 2021-10-14 14:23:40 -05:00 committed by GitHub
parent a137d96af8
commit ab5cbcd3ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3773,14 +3773,25 @@ function guess_interface_from_ip($ipaddress)
/* 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])) {
return $fields[5];
list($ip, $mask) = explode('/', $fields[0]);
if ($mask > $largest_mask) {
$best_if = $fields[5];
$largest_mask = $mask;
}
}
}
}
if (isset($best_if)) {
return $best_if;
}
$ret = exec_command("/sbin/route -n get {$ipaddress} | /usr/bin/awk '/interface/ { print \$2; };'");
if (empty($ret)) {