]> git.ipfire.org Git - people/stevee/ipfire-2.x.git/commitdiff
location-functions.pl: Introduce load_location() function.
authorStefan Schantl <stefan.schantl@ipfire.org>
Fri, 14 Apr 2023 18:24:03 +0000 (20:24 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Sun, 3 Mar 2024 11:56:03 +0000 (12:56 +0100)
This function uses the ipset-functions library to create/update
a location list.

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

index 46e27c04a670b80eab493d315b02c5ac0a3d4f50..2f65de986e0418dd3984ab5774c48102339c45cd 100644 (file)
@@ -23,6 +23,8 @@ package Location::Functions;
 
 use Location;
 
+require '/var/ipfire/ipset-functions.pl';
+
 # Hash which contains country codes and their names which are special or not
 # part of ISO 3166-1.
 my %not_iso_3166_location = (
@@ -53,7 +55,10 @@ our $database = "$location_dir/database.db";
 our $keyfile = "$location_dir/signing-key.pem";
 
 # Directory which contains the exported databases.
-our $ipset_db_directory = "$location_dir/ipset";
+our $exported_db_directory = "$location_dir/exported";
+
+# File extension of the exported databases.
+our $exported_extension = ".networks";
 
 # Create libloc database handle.
 my $db_handle = &init();
@@ -259,6 +264,45 @@ sub get_as_name($) {
        return $as_name;
 }
 
+#
+## Function to load a given location.
+##
+## This function uses the ipset-functions library to
+## load a given location set into the kernel.
+#
+sub load_location ($) {
+       my ($location) = @_;
+
+       # Generate path and file where the database for
+       # the given location lives.
+       my $file = "$exported_db_directory/$location";
+
+       # The database files have an "v4" as part of their name.
+       $file = $file . "v4";
+
+       # Add the file extension
+       $file = $file . "$exported_extension";
+
+       # Open the exported database file for reading.
+       open (FILE, $file) if (-f $file);
+
+       # Read-in the file content.
+       my @data = <FILE>;
+
+       # Close the file handle.
+       close(FILE);
+
+       # Return an error if the file could not be read or
+       # is empty.
+       return "No data" unless(@data);
+
+       # Call the ipset_load_set function to load the set.
+       my $error = &IPSet::Functions::ipset_load_set($location, @data);
+
+       # Return the error message if there was one.
+       return $error if($error);
+}
+
 # Custom END declaration which will be executed when perl
 # ends, to release the database handle to libloc.
 END {