interfaces: avoid duplicating $vfaces array

The idea of list_interfaces.php was good so let's take it further
and inline the use into legacy_interfaces_details() and moving the
patterns to a shared function.  Also add a device name hint in case
the array key is not passed for some reason which can help when
inspecting debug data.  Since legacy_interfaces_details() is already
available in pluginctl -D replace the action command and drop the
obsolete script.
This commit is contained in:
Franco Fichtner 2023-09-01 09:42:08 +02:00
parent 001c382c51
commit 0ab708fb72
4 changed files with 31 additions and 92 deletions

1
plist
View File

@ -955,7 +955,6 @@
/usr/local/opnsense/scripts/interfaces/dhclient-script
/usr/local/opnsense/scripts/interfaces/ifctl.sh
/usr/local/opnsense/scripts/interfaces/list_arp.py
/usr/local/opnsense/scripts/interfaces/list_interfaces.php
/usr/local/opnsense/scripts/interfaces/list_macdb.py
/usr/local/opnsense/scripts/interfaces/list_ndp.py
/usr/local/opnsense/scripts/interfaces/list_sockstat.py

View File

@ -1,7 +1,7 @@
<?php
/*
* Copyright (c) 2015-2022 Franco Fichtner <franco@opnsense.org>
* Copyright (c) 2015-2023 Franco Fichtner <franco@opnsense.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -265,7 +265,9 @@ function legacy_interfaces_details($intf = null)
return $result;
}
$vfaces = interfaces_virtual_patterns();
$current_interface = null;
foreach ($ifconfig_data as $lineid => $line) {
$line_parts = explode(' ', $line);
if (strpos(trim($line), 'flags=') !== false && $line[0] != "\t") {
@ -278,6 +280,8 @@ function legacy_interfaces_details($intf = null)
$result[$current_interface]["ipv4"] = [];
$result[$current_interface]["ipv6"] = [];
$result[$current_interface]["supported_media"] = [];
$result[$current_interface]["is_physical"] = !count(array_intersect(preg_split('/\d+/', $current_interface), $vfaces));
$result[$current_interface]["device"] = $current_interface;
if (preg_match("/ mtu ([0-9]*).*$/", $line, $matches)) {
$result[$current_interface]["mtu"] = $matches[1];
}
@ -568,27 +572,10 @@ function configure_interface_hardware($ifs, $intf_details = null)
}
}
/*
* get_interface_list() - Return a list of all physical interfaces
* along with MAC, IPv4 and status.
*
* $only_active = false -- interfaces that are available in the system
* true -- interfaces that are physically connected
*
* $include_dmesg = false -- skip probing dmesg for more information
* true -- probe dmesg for more information
*/
function get_interface_list($only_active = false, $include_dmesg = false)
function interfaces_virtual_patterns()
{
$dmesg_arr = [];
$iflist = [];
if ($include_dmesg) {
exec('/sbin/dmesg', $dmesg_arr);
}
/* XXX list of virtual interface types, device 'pattern' actually registers them */
$vfaces = [
/* list of virtual device type patterns */
return [
'_stf',
'_vlan',
'_wlan',
@ -617,6 +604,28 @@ function get_interface_list($only_active = false, $include_dmesg = false)
'vlan',
'vxlan',
];
}
/*
* get_interface_list() - Return a list of all physical interfaces
* along with MAC, IPv4 and status.
*
* $only_active = false -- interfaces that are available in the system
* true -- interfaces that are physically connected
*
* $include_dmesg = false -- skip probing dmesg for more information
* true -- probe dmesg for more information
*/
function get_interface_list($only_active = false, $include_dmesg = false)
{
$dmesg_arr = [];
$iflist = [];
if ($include_dmesg) {
exec('/sbin/dmesg', $dmesg_arr);
}
$vfaces = interfaces_virtual_patterns();
$ifnames_wlan = legacy_interface_listget('wlan');
$ifnames_up = legacy_interface_listget('up');

View File

@ -1,69 +0,0 @@
#!/usr/local/bin/php
<?php
/*
* Copyright (C) 2018 Deciso B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
require_once("interfaces.inc");
$vfaces = [
'_stf',
'_vlan',
'_wlan',
'bridge',
'carp',
'enc',
'gif',
'gre',
'ipfw', /* ipfw logging device, not enabled by default */
'ipsec',
'l2tp',
'lagg',
'lo',
'ng',
'ovpnc',
'ovpns',
'pflog',
'pfsync',
'plip',
'ppp',
'pppoe',
'pptp',
'qinq',
'tap',
'tun',
'vlan',
'vxlan',
];
$response = legacy_interfaces_details();
foreach ($response as $ifname => &$intf) {
$tmp_ifnames = preg_split('/\d+/', $ifname);
$intf['is_physical'] = !count(array_intersect($tmp_ifnames, $vfaces));
}
echo json_encode($response);

View File

@ -84,7 +84,7 @@ type:script_output
message:request mac table
[list.ifconfig]
command:/usr/local/opnsense/scripts/interfaces/list_interfaces.php
command:/usr/local/sbin/pluginctl -D
parameters:
type:script_output
message:request ifconfig