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();
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);
}
}