]> git.ipfire.org Git - people/stevee/guardian.git/blobdiff - modules/Base.pm
Introduce SortAddressHash function.
[people/stevee/guardian.git] / modules / Base.pm
index 194cb9692a62cb115f44b22dfc8a905847706403..41ac981f9eb41786fba4430392cef8eac851a4f1 100644 (file)
@@ -4,7 +4,7 @@ use warnings;
 
 use Exporter qw(import);
 
-our @EXPORT_OK = qw(GenerateMonitoredFiles FilePositions);
+our @EXPORT_OK = qw(GenerateMonitoredFiles GetFileposition DetectIPProtocolVersion FilePositions);
 
 use Net::IP;
 
@@ -107,7 +107,7 @@ sub FilePositions (\%\%) {
                        $new_file_positions{$file} = $current_file_positions{$file};
                } else {
                        # Call function to obtain the file position.
-                       my $position = &_initFileposition($file);
+                       my $position = &GetFileposition($file);
 
                        # Add filename and position to the temporary hash.
                        $new_file_positions{$file} = $position;
@@ -118,23 +118,6 @@ sub FilePositions (\%\%) {
        return %new_file_positions;
 }
 
-#
-## Wrapper function for IP address and network validation.
-#
-## This wrapper function uses the external Net::IP perl module to
-## check if a given input is a valid IPv4/IPv6 address or network.
-#
-sub IsValidAddressOrNetwork ($) {
-       my $address = shift;
-
-       # Check if the address is a valid IPv4/IPv6 address or network.
-       # Return "undef" False if the address is not valid.
-       my $ip = new Net::IP ($address) || return undef;
-
-       # If we got here, the address is valid. Return True.
-       return 1;
-}
-
 #
 ## Address/Network to binary format caluculator function.
 #
@@ -190,7 +173,23 @@ sub IPOrNet2Int($) {
 }
 
 #
-## Function for fileposition initialization.
+## DetectIPProtocolVersion function.
+#
+## Wrapper function for determining the used protocol version (4/6)
+## for a given IP address.
+#
+sub DetectIPProtocolVersion ($) {
+       my $address = shift;
+
+       # Call external perl module to detect the used IP protocol version.
+       my $version = &Net::IP::ip_get_version($address);
+
+       # Return the detected version.
+       return $version;
+}
+
+#
+## Function to get the current (EOF) cursor postion.
 #
 ## This function is used to get the cursor position of the end of file (EOF) of
 ## a specified file.
@@ -199,7 +198,7 @@ sub IPOrNet2Int($) {
 ## with huge logfiles, at initialization time of the worker processes, the file will
 ## be opened once and the cursor position of the end of file (EOF) get stored.
 #
-sub _initFileposition ($) {
+sub GetFileposition ($) {
        my $file = $_[0];
 
        # Open the file.
@@ -218,4 +217,34 @@ sub _initFileposition ($) {
        return $position;
 }
 
+#
+## The SortAddressHash function.
+#
+# This function requires a reference to an hash containing
+# IP-addresses, will sort them into a nice looking order
+# and return the soreted result as an array.
+#
+sub SortAddressHash (\%) {
+       # Dereference the given hash reference and store it
+       # in a new temporary hash.
+       my %addresshash = %{ $_[0] };
+
+       # Loop through the entire hash keys.
+       foreach my $address (keys(%addresshash)) {
+               # Convert the address or subnet into binary format.
+               my @bin_address = &IPOrNet2Int($address);
+
+               # Only store the first result if there are multiple
+               # one in case of a given subnet.
+               $addresshash{$address} = $bin_address[0];
+        }
+
+       # Sort the addresshash by the binary addresses
+        # of the stored addresses and save them is an array.
+        my @sorted_addresses = sort { $addresshash{$a} <=> $addresshash{$b} } keys %addresshash;
+
+        # Return the sorted address array.
+        return @sorted_addresses;
+}
+
 1;