]> git.ipfire.org Git - people/stevee/ipfire-2.x.git/commitdiff
ipset-functions.pl: Improve hashtype detection
authorStefan Schantl <stefan.schantl@ipfire.org>
Sat, 22 Apr 2023 07:30:14 +0000 (09:30 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Sun, 3 Mar 2024 11:56:03 +0000 (12:56 +0100)
Cleanup code and allow to detect hashtypes of bitmap:port.

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

index e4c10b41b3d9e4329a2cafbfa752f5a5ebcb68c5..293f8a8702647dc7283ba4377d58ab916f1913a5 100644 (file)
@@ -237,21 +237,28 @@ sub ipset_load_set($@) {
 sub detect_hashtype (@) {
        my @data = @_;
 
-       # XXX - Currently we only support sets with single addresses and/or networks.
-       # Improve the detection if neccessary at a later time.
+       # Currently we only support sets with single addresses, networks and
+       # ports. Improve the detection if neccessary at a later time.
        #
-       # Default to a hashtype of hash:ip for only single addresses.
-       my $hashtype = "hash:ip";
-
-       # Use perl grep to check if a "/" could be found in the data.
-       # In this case the list contains at least one network and we have to use the
-       # hash:net as hashtype.
-       if(grep(/\//, @data)) {
-               # The set contains a network, switching hashtype.
-               $hashtype = "hash:net";
-       }
+       # Check if the data contains a comma, which is used by the
+       # multip element sets.
+       return "Not supported" if(grep(/\,/, @data));
+
+       # Check if the data contains a slash, which assumes it contains
+       # at least one network.
+       return "hash:net" if(grep(/\//, @data));
+
+       # Check if the data contains dots, which assumes it contains
+       # IP addresses.
+       return "hash:ip" if(grep(/\./, @data));
+
+       # Check if the first data element is nummeric, in this case
+       # decide it contains only ports.
+       return "bitmap:port" if( $data[0] =~ /\d+/);
 
-       return $hashtype;
+       # If we got here, no rule matched and we could not determine
+       # a type for the given data.
+       return "Not detected";
 }
 
 #