perl: Add function to perform AS number lookups.

Message ID 20191001061651.4737-1-stefan.schantl@ipfire.org
State New
Headers show
Series
  • perl: Add function to perform AS number lookups.
Related show

Commit Message

Stefan Schantl Oct. 1, 2019, 6:16 a.m. UTC
Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
---
 src/perl/Location.xs | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

Comments

Michael Tremer Oct. 1, 2019, 11:59 a.m. UTC | #1
> On 1 Oct 2019, at 07:16, Stefan Schantl <stefan.schantl@ipfire.org> wrote:
> 
> Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
> ---
> src/perl/Location.xs | 26 ++++++++++++++++++++++++++
> 1 file changed, 26 insertions(+)
> 
> diff --git a/src/perl/Location.xs b/src/perl/Location.xs
> index ab27f0b..63410c4 100644
> --- a/src/perl/Location.xs
> +++ b/src/perl/Location.xs
> @@ -117,6 +117,32 @@ lookup_country_code(db, address)
> 	OUTPUT:
> 		RETVAL
> 
> +unsigned int
> +lookup_asn(db, address)
> +	struct loc_database* db;
> +	char* address;
> +
> +	CODE:
> +		// Lookup network
> +		struct loc_network *network;

I like to initialise variables like this with NULL.

Not sure if that is only considered style.

> +		int err = loc_database_lookup_from_string(db, address, &network);
> +		if (err) {
> +			croak("Could not look up for %s\n", address);
> +		}
> +
> +		// Extract the ASN
> +		unsigned int as_number = loc_network_get_asn(network);
> +		loc_network_unref(network);
> +
> +		if (as_number) {
> +			RETVAL = as_number;
> +		} else {
> +			//RETVAL = NULL;
> +			RETVAL = 0;
> +		}

So either you just put the return value of loc_network_get_asn into RETVAL, or you use NULL.

I would prefer NULL here, because 0 is a reserved ASN. Will most likely never be used, but we should follow the RFCs here.

-Michael

> +	OUTPUT:
> +		RETVAL
> +
> void
> DESTROY(db)
> 	struct loc_database* db;
> -- 
> 2.20.1
>

Patch

diff --git a/src/perl/Location.xs b/src/perl/Location.xs
index ab27f0b..63410c4 100644
--- a/src/perl/Location.xs
+++ b/src/perl/Location.xs
@@ -117,6 +117,32 @@  lookup_country_code(db, address)
 	OUTPUT:
 		RETVAL
 
+unsigned int
+lookup_asn(db, address)
+	struct loc_database* db;
+	char* address;
+
+	CODE:
+		// Lookup network
+		struct loc_network *network;
+		int err = loc_database_lookup_from_string(db, address, &network);
+		if (err) {
+			croak("Could not look up for %s\n", address);
+		}
+
+		// Extract the ASN
+		unsigned int as_number = loc_network_get_asn(network);
+		loc_network_unref(network);
+
+		if (as_number) {
+			RETVAL = as_number;
+		} else {
+			//RETVAL = NULL;
+			RETVAL = 0;
+		}
+	OUTPUT:
+		RETVAL
+
 void
 DESTROY(db)
 	struct loc_database* db;