[03/11] location-functions.pl: Add address_has_flag() function.

Message ID 20200922182509.18643-3-stefan.schantl@ipfire.org
State Accepted
Commit d443f504aabe31a0961c3eddff368364964e8e3e
Headers
Series [01/11] location-functions.pl: Refactor get_locations() function to use the Location module. |

Commit Message

Stefan Schantl Sept. 22, 2020, 6:25 p.m. UTC
  This function can be used to check if a given address has
one of the known flags like "Anonymous Proxy".

If this is true, the mapped special country code will be returned.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
---
 config/cfgroot/location-functions.pl | 29 ++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
  

Patch

diff --git a/config/cfgroot/location-functions.pl b/config/cfgroot/location-functions.pl
index c8ccd94c2..b0b8cd086 100644
--- a/config/cfgroot/location-functions.pl
+++ b/config/cfgroot/location-functions.pl
@@ -33,6 +33,13 @@  my %not_iso_3166_location = (
 	"A3" => "Worldwide Anycast Instance",
 );
 
+# Hash which contains possible network flags and their mapped location codes.
+my %network_flags = (
+	"LOC_NETWORK_FLAG_ANONYMOUS_PROXY" => "A1",
+	"LOC_NETWORK_FLAG_SATELLITE_PROVIDER" => "A2",
+	"LOC_NETWORK_FLAG_ANYCAST" => "A3",
+);
+
 # Array which contains special country codes.
 my @special_locations = ( "A1", "A2", "A3" );
 
@@ -183,4 +190,26 @@  sub get_locations() {
 	return @sorted_locations;
 }
 
+# Function to check if a given address has a special flag.
+sub address_has_flag($) {
+	my ($address) = @_;
+
+	# Init libloc database handle.
+	my $db_handle = &init();
+
+	# Loop through the hash of possible network flags.
+	foreach my $flag (keys(%network_flags)) {
+		# Check if the address has the current flag.
+		if (&Location::lookup_network_has_flag($db_handle, $address, $flag)) {
+			# The given address has the requested flag.
+			#
+			# Grab the mapped location code for this flag.
+			$mapped_code = $network_flags{$flag};
+
+			# Return the code.
+			return $mapped_code;
+		}
+	}
+}
+
 1;