From: Stefan Schantl Date: Sat, 22 Apr 2023 07:32:09 +0000 (+0200) Subject: ipset-functions.pl: Proper map add_to_set() to the correct add function X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=cf8fab4b7660edc8a1a09d27a749c8e2cd34391e;p=people%2Fstevee%2Fipfire-2.x.git ipset-functions.pl: Proper map add_to_set() to the correct add function 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 --- diff --git a/config/cfgroot/ipset-functions.pl b/config/cfgroot/ipset-functions.pl index 293f8a870..e6d4907a4 100644 --- a/config/cfgroot/ipset-functions.pl +++ b/config/cfgroot/ipset-functions.pl @@ -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); } }