From 0ab708fb72248761336178b1ba280501b1513888 Mon Sep 17 00:00:00 2001 From: Franco Fichtner Date: Fri, 1 Sep 2023 09:42:08 +0200 Subject: [PATCH] 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. --- plist | 1 - src/etc/inc/interfaces.lib.inc | 51 ++++++++------ .../scripts/interfaces/list_interfaces.php | 69 ------------------- .../conf/actions.d/actions_interface.conf | 2 +- 4 files changed, 31 insertions(+), 92 deletions(-) delete mode 100755 src/opnsense/scripts/interfaces/list_interfaces.php diff --git a/plist b/plist index 68ce33a6b..a29805c3b 100644 --- a/plist +++ b/plist @@ -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 diff --git a/src/etc/inc/interfaces.lib.inc b/src/etc/inc/interfaces.lib.inc index b2644b797..af63c6d00 100644 --- a/src/etc/inc/interfaces.lib.inc +++ b/src/etc/inc/interfaces.lib.inc @@ -1,7 +1,7 @@ + * Copyright (c) 2015-2023 Franco Fichtner * 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'); diff --git a/src/opnsense/scripts/interfaces/list_interfaces.php b/src/opnsense/scripts/interfaces/list_interfaces.php deleted file mode 100755 index 89084d0e1..000000000 --- a/src/opnsense/scripts/interfaces/list_interfaces.php +++ /dev/null @@ -1,69 +0,0 @@ -#!/usr/local/bin/php - &$intf) { - $tmp_ifnames = preg_split('/\d+/', $ifname); - $intf['is_physical'] = !count(array_intersect($tmp_ifnames, $vfaces)); -} - -echo json_encode($response); diff --git a/src/opnsense/service/conf/actions.d/actions_interface.conf b/src/opnsense/service/conf/actions.d/actions_interface.conf index b992dfcad..945124bf2 100644 --- a/src/opnsense/service/conf/actions.d/actions_interface.conf +++ b/src/opnsense/service/conf/actions.d/actions_interface.conf @@ -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