]> git.ipfire.org Git - location/libloc.git/commitdiff
perl: Add get_as_name()
authorStefan Schantl <stefan.schantl@ipfire.org>
Sat, 22 Aug 2020 14:02:31 +0000 (16:02 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 24 Aug 2020 14:24:08 +0000 (14:24 +0000)
This function can be use to give the numer of an Autonomous System and
get back the stored name from the used location database.

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

index 3c347db88f2331f574e55c4b1bea5d2ebe7e9ac9..7afa3e788ba61f77d1458856919d26236c0db32e 100644 (file)
@@ -229,6 +229,29 @@ get_continent_code(db, ccode)
        OUTPUT:
                RETVAL
 
+SV*
+get_as_name(db, as_number)
+       struct loc_database* db;
+       unsigned int as_number;
+
+       CODE:
+               RETVAL = &PL_sv_undef;
+
+               // Lookup AS.
+               struct loc_as *as;
+               int err = loc_database_get_as(db, &as, as_number);
+               if(!err) {
+                       // Get the name of the given AS number.
+                       const char* as_name = loc_as_get_name(as);
+
+                       RETVAL = newSVpv(as_name, strlen(as_name));
+
+                       loc_as_unref(as);
+               }
+
+       OUTPUT:
+               RETVAL
+
 void
 DESTROY(db)
        struct loc_database* db;
index ec7f1e0aabc99d411682b5ab65cd241690208e46..0d9a1a5c67bb3bf20e227b5509be38ff4371364e 100644 (file)
@@ -12,7 +12,7 @@ use warnings;
 my $testdb = $ENV{'database'};
 my $keyfile = $ENV{'keyfile'};
 
-use Test::More tests => 8;
+use Test::More tests => 9;
 BEGIN { use_ok('Location') };
 
 #########################
@@ -57,5 +57,8 @@ if(defined($as_number)) { fail("Test 8 - Lookup Autonomous System Number for add
 $as_number = &Location::lookup_asn($db, "a.b.c.d");
 if(defined($as_number)) { fail("Test 9 - Lookup Autonomous System Number for invalid address.") }
 
+my $as_name = &Location::get_as_name($db, "204867");
+ok($as_name eq "Lightning Wire Labs GmbH", "Test 10 - Get name for AS204867.");
+
 my @locations = &Location::database_countries($db);
-ok(@locations != 0, "Test 10 - Get database countries.");
+ok(@locations != 0, "Test 11 - Get database countries.");