]> git.ipfire.org Git - people/stevee/guardian.git/blobdiff - modules/Base.pm
Add IPv6 support to IPtables module.
[people/stevee/guardian.git] / modules / Base.pm
index fbda49ac76ebdf7fc21e1df36d47f4d2b4846e9d..f29f5432c67616ee624eaf7daa0bf9f81eb2d076 100644 (file)
@@ -4,36 +4,9 @@ use warnings;
 
 use Exporter qw(import);
 
-our @EXPORT_OK = qw(GenerateMonitoredFiles FilePositions);
+our @EXPORT_OK = qw(GenerateMonitoredFiles DetectIPProtocolVersion FilePositions);
 
-#
-## Function for fileposition initialization.
-#
-## This function is used to get the cursor position of the end of file (EOF) of
-## a specified file.
-#
-## In order to prevent from permanently read and keep files opened, or dealing
-## 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 ($) {
-       my $file = $_[0];
-
-       # Open the file.
-       open(FILE, $file) or die "Could not open $file. $!";
-
-       # Just seek to the end of the file (EOF).
-       seek(FILE, 0, 2);
-
-       # Get and store the position.
-       my $position = tell(FILE),
-
-       # Close the file again.
-       close(FILE);
-
-       # Return the position.
-       return $position;
-}
+use Net::IP;
 
 #
 ## Function to generate a hash of monitored files and their file positions.
@@ -134,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 = &_initFileposition($file);
 
                        # Add filename and position to the temporary hash.
                        $new_file_positions{$file} = $position;
@@ -145,4 +118,103 @@ sub FilePositions (\%\%) {
        return %new_file_positions;
 }
 
+#
+## Address/Network to binary format caluculator function.
+#
+## This function is used to convert a given single IP address
+## or network into a binary format.
+#
+## The used Net::IP module is not able to directly detect
+## single addresses or network ranges. Only an element which may be
+## a single address or a whole network can be assigned, for which a
+## lot of different values can be calculated. In case the input has
+## been a single address, the module will calculate the same binary
+## address (intip) and last address for the network range (last_int)
+## because internally it uses a /32 bit prefix for IPv4 and a /128 prefix
+## on IPv6 addresses.
+#
+## So a single address can be detected by just comparing both calculated
+## addresses if they are equal.
+#
+sub IPOrNet2Int($) {
+       my $address = shift;
+
+       # Assign and validate the given address, or directly return
+       # nothing (False) and exit the function.
+       my $ip = new Net::IP ($address) || return;
+
+       # Convert the given address into integer format.
+       my $first .= $ip->intip();
+
+       # Calculate last address for the given network.
+       my $last .= $ip->last_int();
+
+       # Check whether the first address equals the last address.
+       # If this is true, a single IP address has been passed.
+       if ($first eq $last) {
+               # Return the binary converted single address.
+               return $first;
+       }
+
+       # If both addresses are not equal a network has been passed.
+       #
+       # Check if the converted first address is less than the calculated last
+       # address of the network.
+       elsif ($first < $last) {
+               # Return the binary converted first and last address of
+               # the given network.
+               return $first, $last;
+       }
+
+       # If we got here, something strange happend, return nothing (False).
+       else {
+               return;
+       }
+}
+
+#
+## 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 for fileposition initialization.
+#
+## This function is used to get the cursor position of the end of file (EOF) of
+## a specified file.
+#
+## In order to prevent from permanently read and keep files opened, or dealing
+## 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 ($) {
+       my $file = $_[0];
+
+       # Open the file.
+       open(FILE, $file) or die "Could not open $file. $!";
+
+       # Just seek to the end of the file (EOF).
+       seek(FILE, 0, 2);
+
+       # Get and store the position.
+       my $position = tell(FILE),
+
+       # Close the file again.
+       close(FILE);
+
+       # Return the position.
+       return $position;
+}
+
 1;