perl: Add get_as_name()

Message ID 20200822140231.3405-1-stefan.schantl@ipfire.org
State Accepted
Commit 30c8e5289adce18327d6d41a27d7fc6eadd74705
Headers show
Series
  • perl: Add get_as_name()
Related show

Commit Message

Stefan Schantl Aug. 22, 2020, 2:02 p.m. UTC
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>
---
 src/perl/Location.xs  | 23 +++++++++++++++++++++++
 src/perl/t/Location.t |  7 +++++--
 2 files changed, 28 insertions(+), 2 deletions(-)

Patch

diff --git a/src/perl/Location.xs b/src/perl/Location.xs
index 3c347db..7afa3e7 100644
--- a/src/perl/Location.xs
+++ b/src/perl/Location.xs
@@ -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;
diff --git a/src/perl/t/Location.t b/src/perl/t/Location.t
index ec7f1e0..0d9a1a5 100644
--- a/src/perl/t/Location.t
+++ b/src/perl/t/Location.t
@@ -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.");