]> git.ipfire.org Git - people/stevee/ipfire-2.x.git/commitdiff
ipset-functions.pl: Add ipset_load_set function.
authorStefan Schantl <stefan.schantl@ipfire.org>
Fri, 14 Apr 2023 17:36:41 +0000 (19:36 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Sun, 3 Mar 2024 11:56:03 +0000 (12:56 +0100)
This function can be used to load/update a given set and
it's data.

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

index 85f8640481bcda3c56910e4a3e8b04db3c198ef1..e4c10b41b3d9e4329a2cafbfa752f5a5ebcb68c5 100644 (file)
@@ -160,6 +160,77 @@ sub ipset_set_data($) {
        return &IPSet::get_set_data($session, $name);
 }
 
+#
+## Load ipset set function.
+##
+## This function is responsible to load/update a given set.
+##
+## It uses the ipset perl bindings to directly interact with ipset
+## and load the list into the kernel.
+##
+## Params:
+##     The setname as a string as fist argument
+##     The data to load as an array as second argument
+##
+## Returns:
+##     The error message as a string if an error happend
+#
+sub ipset_load_set($@) {
+       my ($name, @data) = @_;
+
+       # Check if the given set exists.
+       my $exists = &ipset_exists($name);
+
+       # Call functions to detect and calculate the type and hashsize and
+       # omit the maxelems.
+       my $setname = $name;
+       my $type = &detect_hashtype(@data);
+       my $maxelem = @data;
+       my $hashsize = &calculate_hashsize($maxelem);
+
+       # Get a a random set name.
+       my $rand_name = &random_setname();
+
+       # If the given set allready exists, assign the random generated name.
+       $setname = $rand_name if($exists);
+
+       # Create a new empty set.
+       my $error = &ipset_create($setname, $type, $hashsize, $maxelem);
+
+       # Abort and return the error if there was one.
+       return $error if(($error) && ($error ne "1"));
+
+       # Add the data to the recently created set.
+       &add_to_set($setname, @data);
+
+       # We are finished here and can return, if the set has not exist yet.
+       return unless($exists);
+
+       # Get the data of the existing set.
+       my $set_data = &ipset_set_data($name);
+
+       # Return if the data of the set could not be grabbed.
+       return "No data" unless($set_data);
+
+       # Check if the old and the new types are eqal.
+       if ($set_data->{type} eq $type) {
+               # The type is the same and we easily can swap the sets.
+               &ipset_swap($setname, $name);
+
+               # Remove the old one.
+               &ipset_remove($setname);
+       } else {
+               # Abort if the set is currently in use.
+               return "Set in use" if($set_data->{references} gt 0);
+
+               # Drop the old set first.
+               &ipset_remove($name);
+
+               # Rename the set
+               &ipset_rename($setname, $name);
+       }
+}
+
 #
 ## Tiny helper function to detect the hashtype, based on the given data.
 #