From aa5acb424f4d05efd15ceed1b9e71d6a34dac674 Mon Sep 17 00:00:00 2001 From: Ermal Date: Wed, 12 Nov 2014 14:57:12 +0100 Subject: [PATCH] Use route command directly rather than trying to make a route search on php thorugh netstat. It Fixes #4000 --- etc/inc/interfaces.inc | 49 +++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/etc/inc/interfaces.inc b/etc/inc/interfaces.inc index e01d8cc7ab..7bb983ecc1 100644 --- a/etc/inc/interfaces.inc +++ b/etc/inc/interfaces.inc @@ -4443,39 +4443,24 @@ function get_real_interface($interface = "wan", $family = "all", $realv6iface = /* Guess the physical interface by providing a IP address */ function guess_interface_from_ip($ipaddress) { - if(! is_ipaddr($ipaddress)) { + + $family = ''; + if(is_ipaddrv4($ipaddress)) + $family = 'inet'; + if (empty($family) && is_ipaddrv6($ipaddress)) + $family = 'inet6'; + + if (empty($family)) 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]; - } - } - } - } - $ret = exec_command("/sbin/route -n get {$ipaddress} | /usr/bin/awk '/interface/ { print \$2; };'"); - if(empty($ret)) { - return false; - } - return $ret; + + /* create a route table we can search */ + $output = ''; + $_gb = exec("/sbin/route -n get -{$family} " . escapeshellarg($ipaddress) . " | /usr/bin/awk '/interface/ { print \$2; };'", $output); + $output[0] = trim($output[0], " \n"); + if (!empty($output[0])) + return $output[0]; + + return false; } /*