Commit c85a9b0b authored by Franco Fichtner's avatar Franco Fichtner

interfaces: merge assignment rework and function rewrite from master

parent 0b6c6af8
...@@ -28,14 +28,6 @@ ...@@ -28,14 +28,6 @@
POSSIBILITY OF SUCH DAMAGE. POSSIBILITY OF SUCH DAMAGE.
*/ */
/****f* util/timeout
* NAME
* timeout - console input with timeout countdown. Note: erases 2 char of screen for timer. Leave space.
* INPUTS
* optional, seconds to wait before timeout. Default 9 seconds.
* RESULT
* returns 1 char of user input or null if no input.
******/
function timeout($timer = 9) function timeout($timer = 9)
{ {
while (!isset($key)) { while (!isset($key)) {
...@@ -55,9 +47,7 @@ function timeout($timer = 9) ...@@ -55,9 +47,7 @@ function timeout($timer = 9)
return $key; return $key;
} }
function set_networking_interfaces_ports($probe = false)
function set_networking_interfaces_ports()
{ {
global $config; global $config;
...@@ -66,8 +56,12 @@ function set_networking_interfaces_ports() ...@@ -66,8 +56,12 @@ function set_networking_interfaces_ports()
$interactive = true; $interactive = true;
$key = null; $key = null;
/* kernel messages clobber stty probing on ifconfig up */
mute_kernel_msgs();
$iflist = get_interface_list(); $iflist = get_interface_list();
if ($probe) {
echo PHP_EOL . gettext('Press any key to start the manual interface assignment: '); echo PHP_EOL . gettext('Press any key to start the manual interface assignment: ');
$key = timeout(7); $key = timeout(7);
...@@ -78,6 +72,7 @@ function set_networking_interfaces_ports() ...@@ -78,6 +72,7 @@ function set_networking_interfaces_ports()
if ($key != "\n") { if ($key != "\n") {
echo PHP_EOL; echo PHP_EOL;
} }
}
echo <<<EOD echo <<<EOD
...@@ -90,8 +85,8 @@ EOD; ...@@ -90,8 +85,8 @@ EOD;
$iflist = array(); $iflist = array();
} else { } else {
foreach ($iflist as $iface => $ifa) { foreach ($iflist as $iface => $ifa) {
echo sprintf("% -7s%s %s %s\n", $iface, $ifa['mac'], interfaces_bring_up($iface);
$ifa['up'] ? " (up)" : "(down)", $ifa['dmesg']); echo sprintf("%-16s %s %s\n", $iface, $ifa['mac'], $ifa['dmesg']);
} }
} }
...@@ -149,6 +144,9 @@ EOD; ...@@ -149,6 +144,9 @@ EOD;
if ($wanif == 'a') { if ($wanif == 'a') {
$wanif = autodetect_interface('WAN', $fp); $wanif = autodetect_interface('WAN', $fp);
if (!$wanif) {
continue;
}
} }
if (!array_key_exists($wanif, $iflist)) { if (!array_key_exists($wanif, $iflist)) {
...@@ -176,6 +174,9 @@ EOD; ...@@ -176,6 +174,9 @@ EOD;
if ($lanif == 'a') { if ($lanif == 'a') {
$lanif = autodetect_interface('LAN', $fp); $lanif = autodetect_interface('LAN', $fp);
if (!$lanif) {
continue;
}
} }
if (!array_key_exists($lanif, $iflist)) { if (!array_key_exists($lanif, $iflist)) {
...@@ -227,7 +228,7 @@ EOD; ...@@ -227,7 +228,7 @@ EOD;
} }
if ($optif[$i] == 'a') { if ($optif[$i] == 'a') {
$ad = autodetect_interface(gettext("Optional") . " " . $io, $fp); $ad = autodetect_interface('OPT' . $io, $fp);
if (!$ad) { if (!$ad) {
unset($optif[$i]); unset($optif[$i]);
continue; continue;
...@@ -288,6 +289,8 @@ EOD; ...@@ -288,6 +289,8 @@ EOD;
} }
if (!in_array($key, array('y', 'Y'))) { if (!in_array($key, array('y', 'Y'))) {
unmute_kernel_msgs();
fclose($fp);
return false; return false;
} }
...@@ -416,33 +419,36 @@ EOD; ...@@ -416,33 +419,36 @@ EOD;
write_config("Console assignment of interfaces"); write_config("Console assignment of interfaces");
printf(gettext("done.%s"), "\n"); printf(gettext("done.%s"), "\n");
unmute_kernel_msgs();
fclose($fp); fclose($fp);
return true; return true;
} }
function autodetect_interface($ifname, $fp) function autodetect_interface($name, $fp)
{ {
$iflist_prev = get_interface_list('media'); $iflist_prev = get_interface_list(true);
echo <<<EOD echo <<<EOD
Connect the {$ifname} interface now and make sure that the link is up. Connect the {$name} interface now and make sure that the link is up.
Then press ENTER to continue. Then press ENTER to continue.
EOD; EOD;
fgets($fp); fgets($fp);
$iflist = get_interface_list("media");
foreach ($iflist_prev as $ifn => $ifa) { $iflist = get_interface_list(true);
if (!$ifa['up'] && $iflist[$ifn]['up']) {
printf(gettext("Detected link-up on interface %s.%s"), $ifn, "\n"); foreach ($iflist as $ifn => $ifa) {
if (!isset($iflist_prev[$ifn])) {
printf(gettext("Detected link-up: %s%s"), $ifn, "\n");
return $ifn; return $ifn;
} }
} }
printf(gettext("No link-up detected.%s"), "\n"); printf(gettext("No link-up detected.%s"), "\n");
return null; return false;
} }
function vlan_setup($iflist, $fp) function vlan_setup($iflist, $fp)
......
...@@ -813,98 +813,81 @@ function get_configured_ipv6_addresses() ...@@ -813,98 +813,81 @@ function get_configured_ipv6_addresses()
/* /*
* get_interface_list() - Return a list of all physical interfaces * get_interface_list() - Return a list of all physical interfaces
* along with MAC and status. * along with MAC, IPv4 and status.
* *
* $mode = "active" - use ifconfig -lu * $only_active = false -- interfaces that are available in the system
* "media" - use ifconfig to check physical connection * true -- interfaces that are physically connected
* status (much slower)
*/ */
function get_interface_list($mode = "active", $keyby = "physical", $vfaces = "") { function get_interface_list($only_active = false)
{
global $config; global $config;
$upints = array();
/* get a list of virtual interface types */ /* list of virtual interface types */
if(!$vfaces) { $vfaces = array(
$vfaces = array ( '_vlan',
'_wlan',
'bridge', 'bridge',
'ppp', 'carp',
'pppoe', 'enc',
'pptp', 'faith',
'l2tp',
'sl',
'gif', 'gif',
'gre', 'gre',
'faith', 'ipfw',
'l2tp',
'lagg',
'lo', 'lo',
'ng', 'ng',
'_vlan',
'_wlan',
'pflog', 'pflog',
'plip', 'plip',
'ppp',
'pppoe',
'pptp',
'pfsync', 'pfsync',
'enc', 'sl',
'tun', 'tun',
'carp', 'vip'
'lagg',
'vip',
'ipfw'
); );
$ifnames_up = legacy_interface_listget('up');
$ifnames = legacy_interface_listget();
if ($only_active) {
$_ifnames = array();
foreach ($ifnames as $ifname) {
$ifinfo = legacy_interface_stats($ifname);
if (isset($ifinfo['link state'])) {
if ($ifinfo['link state'] == '2') {
$_ifnames[] = $ifname;
} }
switch($mode) {
case 'active':
$upints = legacy_interface_listget('up');
break;
case 'media':
$intlist = legacy_interface_listget();
$ifconfig = "";
exec("/sbin/ifconfig -a", $ifconfig);
$regexp = '/(' . implode('|', $intlist) . '):\s/';
$ifstatus = preg_grep('/status:/', $ifconfig);
foreach ($ifstatus as $status) {
$int = array_shift($intlist);
if (stristr($status, 'active')) {
$upints[] = $int;
} }
} }
break;
default: $ifnames = $_ifnames;
$upints = legacy_interface_listget();
break;
} }
/* build interface list with netstat */
$linkinfo = ""; foreach ($ifnames as $ifname) {
exec("/usr/bin/netstat -inW -f link | awk '{ print $1, $4 }'", $linkinfo); if (in_array(array_shift(preg_split('/\d/', $ifname)), $vfaces)) {
array_shift($linkinfo); continue;
/* build ip address list with netstat */ }
$ipinfo = "";
exec("/usr/bin/netstat -inW -f inet | awk '{ print $1, $4 }'", $ipinfo); $ifdata = pfSense_get_interface_addresses($ifname);
array_shift($ipinfo);
foreach($linkinfo as $link) {
$friendly = "";
$alink = explode(" ", $link);
$ifname = rtrim(trim($alink[0]), '*');
/* trim out all numbers before checking for vfaces */
if (!in_array(array_shift(preg_split('/\d/', $ifname)), $vfaces) &&
!stristr($ifname, "_vlan") && !stristr($ifname, "_wlan")) {
$toput = array( $toput = array(
"mac" => trim($alink[1]), 'up' => in_array($ifname, $ifnames_up),
"up" => in_array($ifname, $upints) 'ipaddr' => $ifdata['ipaddr'],
'mac' => $ifdata['macaddr']
); );
foreach($ipinfo as $ip) {
$aip = explode(" ", $ip);
if($aip[0] == $ifname) {
$toput['ipaddr'] = $aip[1];
}
}
if (isset($config['interfaces'])) { if (isset($config['interfaces'])) {
foreach($config['interfaces'] as $name => $int) { foreach($config['interfaces'] as $name => $int) {
if($int['if'] == $ifname) $friendly = $name; if ($int['if'] == $ifname) {
$toput['friendly'] = $name;
break;
} }
} }
switch($keyby) {
case "physical":
if($friendly != "") {
$toput['friendly'] = $friendly;
} }
$dmesg_arr = array(); $dmesg_arr = array();
exec("/sbin/dmesg |grep $ifname | head -n1", $dmesg_arr); exec("/sbin/dmesg |grep $ifname | head -n1", $dmesg_arr);
preg_match_all("/<(.*?)>/i", $dmesg_arr[0], $dmesg); preg_match_all("/<(.*?)>/i", $dmesg_arr[0], $dmesg);
...@@ -913,19 +896,10 @@ function get_interface_list($mode = "active", $keyby = "physical", $vfaces = "") ...@@ -913,19 +896,10 @@ function get_interface_list($mode = "active", $keyby = "physical", $vfaces = "")
} else { } else {
$toput['dmesg'] = null; $toput['dmesg'] = null;
} }
$iflist[$ifname] = $toput;
break;
case "ppp":
case "friendly": $iflist[$ifname] = $toput;
if($friendly != "") {
$toput['if'] = $ifname;
$iflist[$friendly] = $toput;
}
break;
}
}
} }
return $iflist; return $iflist;
} }
......
...@@ -173,7 +173,7 @@ if (is_interface_mismatch()) { ...@@ -173,7 +173,7 @@ if (is_interface_mismatch()) {
echo PHP_EOL . gettext('Default interfaces not found -- Running interface assignment option.') . PHP_EOL; echo PHP_EOL . gettext('Default interfaces not found -- Running interface assignment option.') . PHP_EOL;
led_assigninterfaces(); led_assigninterfaces();
while (!set_networking_interfaces_ports()); while (!set_networking_interfaces_ports(true));
led_kitt(); led_kitt();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment