]> git.ipfire.org Git - people/stevee/ipfire-2.x.git/commitdiff
ipset-functions.pl: Proper map add_to_set() to the correct add function
authorStefan Schantl <stefan.schantl@ipfire.org>
Sat, 22 Apr 2023 07:32:09 +0000 (09:32 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Sun, 3 Mar 2024 11:56:03 +0000 (12:56 +0100)
Use a hash to map to the correct function to call for adding elements,
to a set, based on the given and detected data.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
config/cfgroot/ipset-functions.pl

index 293f8a8702647dc7283ba4377d58ab916f1913a5..e6d4907a43aae6fce5318e24d854116489c61335 100644 (file)
@@ -24,6 +24,14 @@ package IPSet::Functions;
 use IPSet;
 use strict;
 
+# Hash which contains supported hash types and the functions
+# which have to be called to add elements to such a set.
+my %add_functions = (
+       "hash:ip" => \&IPSet::add_address,
+       "hash:net" => \&IPSet::add_address,
+       "bitmap:port" => \&IPSet::add_port,
+);
+
 # Create ipset session
 my $session = &init();
 
@@ -137,16 +145,23 @@ sub ipset_remove($) {
 sub add_to_set ($@) {
        my ($set, @data) = @_;
 
-       # XXX - Currently only adding IPv4 addresses to a set is supported.
-       # Add more allowed datatypes at a later time if neccessary.
-       #
+       # Detect the data type.
+       my $data_type = &detect_hashtype(@data);
+
+       # Omit the function which needs to be called for adding.
+       my $add_function = $add_functions{$data_type};
+
+       # Abort if no function could be determined.
+       return "No add_function" unless($add_function);
+
        # Loop through the data array.
        foreach my $element (@data) {
                # Remove any newlines.
                chomp($element);
 
-               # Add the address to the given set.
-               &IPSet::add_address($session, $set, $element);
+               # Call correct function to add the data to the
+               # set.
+               &$add_function($session, $set, $element);
        }
 }