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 @@
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)
{
while (!isset($key)) {
......@@ -55,9 +47,7 @@ function timeout($timer = 9)
return $key;
}
function set_networking_interfaces_ports()
function set_networking_interfaces_ports($probe = false)
{
global $config;
......@@ -66,8 +56,12 @@ function set_networking_interfaces_ports()
$interactive = true;
$key = null;
/* kernel messages clobber stty probing on ifconfig up */
mute_kernel_msgs();
$iflist = get_interface_list();
if ($probe) {
echo PHP_EOL . gettext('Press any key to start the manual interface assignment: ');
$key = timeout(7);
......@@ -78,6 +72,7 @@ function set_networking_interfaces_ports()
if ($key != "\n") {
echo PHP_EOL;
}
}
echo <<<EOD
......@@ -90,8 +85,8 @@ EOD;
$iflist = array();
} else {
foreach ($iflist as $iface => $ifa) {
echo sprintf("% -7s%s %s %s\n", $iface, $ifa['mac'],
$ifa['up'] ? " (up)" : "(down)", $ifa['dmesg']);
interfaces_bring_up($iface);
echo sprintf("%-16s %s %s\n", $iface, $ifa['mac'], $ifa['dmesg']);
}
}
......@@ -149,6 +144,9 @@ EOD;
if ($wanif == 'a') {
$wanif = autodetect_interface('WAN', $fp);
if (!$wanif) {
continue;
}
}
if (!array_key_exists($wanif, $iflist)) {
......@@ -176,6 +174,9 @@ EOD;
if ($lanif == 'a') {
$lanif = autodetect_interface('LAN', $fp);
if (!$lanif) {
continue;
}
}
if (!array_key_exists($lanif, $iflist)) {
......@@ -227,7 +228,7 @@ EOD;
}
if ($optif[$i] == 'a') {
$ad = autodetect_interface(gettext("Optional") . " " . $io, $fp);
$ad = autodetect_interface('OPT' . $io, $fp);
if (!$ad) {
unset($optif[$i]);
continue;
......@@ -288,6 +289,8 @@ EOD;
}
if (!in_array($key, array('y', 'Y'))) {
unmute_kernel_msgs();
fclose($fp);
return false;
}
......@@ -416,33 +419,36 @@ EOD;
write_config("Console assignment of interfaces");
printf(gettext("done.%s"), "\n");
unmute_kernel_msgs();
fclose($fp);
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
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.
EOD;
fgets($fp);
$iflist = get_interface_list("media");
foreach ($iflist_prev as $ifn => $ifa) {
if (!$ifa['up'] && $iflist[$ifn]['up']) {
printf(gettext("Detected link-up on interface %s.%s"), $ifn, "\n");
$iflist = get_interface_list(true);
foreach ($iflist as $ifn => $ifa) {
if (!isset($iflist_prev[$ifn])) {
printf(gettext("Detected link-up: %s%s"), $ifn, "\n");
return $ifn;
}
}
printf(gettext("No link-up detected.%s"), "\n");
return null;
return false;
}
function vlan_setup($iflist, $fp)
......
......@@ -813,98 +813,81 @@ function get_configured_ipv6_addresses()
/*
* 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
* "media" - use ifconfig to check physical connection
* status (much slower)
* $only_active = false -- interfaces that are available in the system
* true -- interfaces that are physically connected
*/
function get_interface_list($mode = "active", $keyby = "physical", $vfaces = "") {
function get_interface_list($only_active = false)
{
global $config;
$upints = array();
/* get a list of virtual interface types */
if(!$vfaces) {
$vfaces = array (
/* list of virtual interface types */
$vfaces = array(
'_vlan',
'_wlan',
'bridge',
'ppp',
'pppoe',
'pptp',
'l2tp',
'sl',
'carp',
'enc',
'faith',
'gif',
'gre',
'faith',
'ipfw',
'l2tp',
'lagg',
'lo',
'ng',
'_vlan',
'_wlan',
'pflog',
'plip',
'ppp',
'pppoe',
'pptp',
'pfsync',
'enc',
'sl',
'tun',
'carp',
'lagg',
'vip',
'ipfw'
'vip'
);
$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:
$upints = legacy_interface_listget();
break;
$ifnames = $_ifnames;
}
/* build interface list with netstat */
$linkinfo = "";
exec("/usr/bin/netstat -inW -f link | awk '{ print $1, $4 }'", $linkinfo);
array_shift($linkinfo);
/* build ip address list with netstat */
$ipinfo = "";
exec("/usr/bin/netstat -inW -f inet | awk '{ print $1, $4 }'", $ipinfo);
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")) {
foreach ($ifnames as $ifname) {
if (in_array(array_shift(preg_split('/\d/', $ifname)), $vfaces)) {
continue;
}
$ifdata = pfSense_get_interface_addresses($ifname);
$toput = array(
"mac" => trim($alink[1]),
"up" => in_array($ifname, $upints)
'up' => in_array($ifname, $ifnames_up),
'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'])) {
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();
exec("/sbin/dmesg |grep $ifname | head -n1", $dmesg_arr);
preg_match_all("/<(.*?)>/i", $dmesg_arr[0], $dmesg);
......@@ -913,19 +896,10 @@ function get_interface_list($mode = "active", $keyby = "physical", $vfaces = "")
} else {
$toput['dmesg'] = null;
}
$iflist[$ifname] = $toput;
break;
case "ppp":
case "friendly":
if($friendly != "") {
$toput['if'] = $ifname;
$iflist[$friendly] = $toput;
}
break;
}
}
$iflist[$ifname] = $toput;
}
return $iflist;
}
......
......@@ -173,7 +173,7 @@ if (is_interface_mismatch()) {
echo PHP_EOL . gettext('Default interfaces not found -- Running interface assignment option.') . PHP_EOL;
led_assigninterfaces();
while (!set_networking_interfaces_ports());
while (!set_networking_interfaces_ports(true));
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