From: Stefan Schantl Date: Sat, 15 Apr 2023 13:13:57 +0000 (+0200) Subject: rules.pl: Introduce set_loader mechanism X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c5051c56f1bcbe627080407c9ca11195b5afac9f;p=people%2Fstevee%2Fipfire-2.x.git rules.pl: Introduce set_loader mechanism This introduces a mechanism where each setname and the function which is used to proper load this set are stored in a hash. The load_set function will use this information to call the correct function. To register a set loader the register_set_loader function can be used by specifying the function name and an array of affected set names. Signed-off-by: Stefan Schantl --- diff --git a/config/firewall/rules.pl b/config/firewall/rules.pl index 1fccf5d35..e0039c068 100644 --- a/config/firewall/rules.pl +++ b/config/firewall/rules.pl @@ -81,6 +81,7 @@ my %blocklistsettings= ( ); my %ipset_loaded_sets = (); +my %set_loader = (); my $configfwdfw = "${General::swroot}/firewall/config"; my $configinput = "${General::swroot}/firewall/input"; @@ -129,6 +130,10 @@ my $POLICY_INPUT_ACTION = $fwoptions{"FWPOLICY2"}; my $POLICY_FORWARD_ACTION = $fwoptions{"FWPOLICY"}; my $POLICY_OUTPUT_ACTION = $fwoptions{"FWPOLICY1"}; +# Register set loaders. +®ister_set_loader("Location::Functions::load_location", @locations); +®ister_set_loader("IPblocklist::Functions::load_blocklist", @blocklists); + #workaround to suppress a warning when a variable is used only once my @dummy = ( $Location::Functions::ipset_db_directory ); undef (@dummy); @@ -1043,3 +1048,38 @@ sub firewall_chain_exists ($) { return $ret; } + +sub load_set($) { + my ($setname) = @_; + + # Skip the set if the requested allready has been loaded during + # this script run. + return if($ipset_loaded_sets{$setname}); + + # Print a message if debug is enabled. + print "Loading set $setname\n" if ($DEBUG); + + # Obtain the correct loader for the requested set. + my $loader = &get_set_loader($setname); + + # Load the set. + &$loader($setname); + + # Mark the set as loaded. + $ipset_loaded_sets{$setname} = "1"; +} + +sub register_set_loader ($@) { + my ($function, @elements) = @_; + + # Loop through the given array. + foreach my $element (@elements) { + $set_loader{$element} = \&$function; + } +} + +sub get_set_loader ($) { + my ($element) = @_; + + return $set_loader{$element}; +}