[2/2] network-functions.pl: Improve zone configuration functions

Message ID 20210117142004.984-2-hofmann@leo-andres.de
State Accepted
Commit eea288bc1a55ac99cac868b00367999455cecde5
Headers
Series [1/2] Refactor "get_available_network_zones", move to network-functions.pl |

Commit Message

Leo-Andres Hofmann Jan. 17, 2021, 2:20 p.m. UTC
  Cache ethernet configuration in public variable "ethernet_settings",
add functions to simplify working with the network configuration.

Signed-off-by: Leo-Andres Hofmann <hofmann@leo-andres.de>
---
 config/cfgroot/network-functions.pl | 40 +++++++++++++++++++++++++----
 1 file changed, 35 insertions(+), 5 deletions(-)
  

Patch

diff --git a/config/cfgroot/network-functions.pl b/config/cfgroot/network-functions.pl
index 622731f96..7bd6466e0 100644
--- a/config/cfgroot/network-functions.pl
+++ b/config/cfgroot/network-functions.pl
@@ -27,6 +27,14 @@  require "/var/ipfire/general-functions.pl";
 
 use Socket;
 
+# System ethernet configuration
+our %ethernet_settings = ();
+&General::readhash("${General::swroot}/ethernet/settings", \%ethernet_settings);
+
+# List of all possible network zones that can be configured
+our @known_network_zones = ("red", "green", "orange", "blue");
+
+# IPv4 netmask CIDR to dotted decimal notation conversion table
 my %PREFIX2NETMASK = (
 	32 => "255.255.255.255",
 	31 => "255.255.255.254",
@@ -448,12 +456,8 @@  sub get_mac_by_name($) {
 ## Function to get a list of all available network zones.
 #
 sub get_available_network_zones () {
-	# Get netsettings.
-	my %netsettings = ();
-	&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
-
 	# Obtain the configuration type from the netsettings hash.
-	my $config_type = $netsettings{'CONFIG_TYPE'};
+	my $config_type = $ethernet_settings{'CONFIG_TYPE'};
 
 	# Hash which contains the conversation from the config mode
 	# to the existing network interface names. They are stored like
@@ -480,6 +484,32 @@  sub get_available_network_zones () {
 	return @network_zones;
 }
 
+#
+## Function to check if a network zone is available in the current configuration
+#
+sub is_zone_available() {
+	my $zone = lc shift;
+	
+	# Make sure the zone is valid
+	die("Unknown network zone '$zone'") unless ($zone ~~ @known_network_zones);
+	
+	# Get available zones and return result
+	my @available_zones = get_available_network_zones();
+	return ($zone ~~ @available_zones);
+}
+
+#
+## Function to determine if the RED zone is in standard IP (or modem, PPP, VDSL, ...) mode
+#
+sub is_red_mode_ip() {
+	# Obtain the settings from the netsettings hash
+	my $config_type = $ethernet_settings{'CONFIG_TYPE'};
+	my $red_type = $ethernet_settings{'RED_TYPE'};
+
+	# RED must be a network device (configuration 1-4) with dynamic or static IP
+	return (($config_type ~~ [1..4]) && ($red_type ~~ ["DHCP", "STATIC"]));
+}
+
 1;
 
 # Remove the next line to enable the testsuite