]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
perl: Add lookup_network_has_flag() function.
authorStefan Schantl <stefan.schantl@ipfire.org>
Sun, 30 Aug 2020 10:36:00 +0000 (12:36 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 31 Aug 2020 10:02:10 +0000 (10:02 +0000)
This function can be used to check if a given address or network has one
of the following network flags.

* LOC_NETWORK_FLAG_ANONYMOUS_PROXY
* LOC_NETWORK_FLAG_SATELLITE_PROVIDER
* LOC_NETWORK_FLAG_ANYCAST

It will return true if the given flag is set.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/perl/Location.xs

index 7afa3e788ba61f77d1458856919d26236c0db32e..1cb2e21bbfbcaa3f9b53901c6d2aec314c123758 100644 (file)
@@ -181,6 +181,42 @@ lookup_country_code(db, address)
        OUTPUT:
                RETVAL
 
+bool
+lookup_network_has_flag(db, address, flag)
+       struct loc_database* db;
+       char* address;
+       char* flag;
+
+       CODE:
+               RETVAL = false;
+
+               enum loc_network_flags iv = 0;
+
+               if (strcmp("LOC_NETWORK_FLAG_ANONYMOUS_PROXY", flag) == 0)
+                       iv |= LOC_NETWORK_FLAG_ANONYMOUS_PROXY;
+               else if (strcmp("LOC_NETWORK_FLAG_SATELLITE_PROVIDER", flag) == 0)
+                       iv |= LOC_NETWORK_FLAG_SATELLITE_PROVIDER;
+               else if (strcmp("LOC_NETWORK_FLAG_ANYCAST", flag) == 0)
+                       iv |= LOC_NETWORK_FLAG_ANYCAST;
+               else
+                       croak("Invalid flag");
+
+               // Lookup network
+               struct loc_network *network;
+               int err = loc_database_lookup_from_string(db, address, &network);
+
+               if (!err) {
+                       // Check if the network has the given flag.
+                       if (loc_network_has_flag(network, iv)) {
+                               RETVAL = true;
+                       }
+
+                       loc_network_unref(network);
+               }
+
+       OUTPUT:
+               RETVAL
+
 SV*
 lookup_asn(db, address)
        struct loc_database* db;