[PATCHv2] perl: Add function to perform AS number lookups.

Message ID 20191002171021.6857-1-stefan.schantl@ipfire.org
State Accepted
Headers
Series [PATCHv2] perl: Add function to perform AS number lookups. |

Commit Message

Stefan Schantl Oct. 2, 2019, 5:10 p.m. UTC
  Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
---
 src/perl/Location.xs | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
  

Patch

diff --git a/src/perl/Location.xs b/src/perl/Location.xs
index cdbc7e7..b6c4dc5 100644
--- a/src/perl/Location.xs
+++ b/src/perl/Location.xs
@@ -113,6 +113,29 @@  lookup_country_code(db, address)
 	OUTPUT:
 		RETVAL
 
+SV*
+lookup_asn(db, address)
+	struct loc_database* db;
+	char* address;
+
+	CODE:
+		RETVAL = &PL_sv_undef;
+
+		// Lookup network
+		struct loc_network *network;
+		int err = loc_database_lookup_from_string(db, address, &network);
+		if (!err) {
+			// Extract the ASN
+			unsigned int as_number = loc_network_get_asn(network);
+			if (as_number > 0) {
+				RETVAL = newSViv(as_number);
+			}
+
+			loc_network_unref(network);
+		}
+	OUTPUT:
+		RETVAL
+
 void
 DESTROY(db)
 	struct loc_database* db;