]> git.ipfire.org Git - location/libloc.git/commitdiff
lua: Add AS object
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 22 Feb 2024 15:31:40 +0000 (15:31 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 22 Feb 2024 15:31:57 +0000 (15:31 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/lua/as.c [new file with mode: 0644]
src/lua/as.h [new file with mode: 0644]
src/lua/location.c
tests/lua/main.lua

index f61997edbc3c56d312e0fde438ca0c1c7c2e22b4..2e691264e884289652a6dc352c341978f5b2ee20 100644 (file)
@@ -237,6 +237,8 @@ lua_LTLIBRARIES = \
 luadir = $(LUA_INSTALL_CMOD)
 
 src_lua_location_la_SOURCES = \
+       src/lua/as.c \
+       src/lua/as.h \
        src/lua/country.c \
        src/lua/country.h \
        src/lua/database.c \
@@ -263,6 +265,8 @@ src_lua_location_la_LIBADD = \
 endif
 
 EXTRA_DIST += \
+       src/lua/as.c \
+       src/lua/as.h \
        src/lua/country.c \
        src/lua/country.h \
        src/lua/database.c \
diff --git a/src/lua/as.c b/src/lua/as.c
new file mode 100644 (file)
index 0000000..d36f2d8
--- /dev/null
@@ -0,0 +1,135 @@
+/*
+       libloc - A library to determine the location of someone on the Internet
+
+       Copyright (C) 2024 IPFire Development Team <info@ipfire.org>
+
+       This library is free software; you can redistribute it and/or
+       modify it under the terms of the GNU Lesser General Public
+       License as published by the Free Software Foundation; either
+       version 2.1 of the License, or (at your option) any later version.
+
+       This library is distributed in the hope that it will be useful,
+       but WITHOUT ANY WARRANTY; without even the implied warranty of
+       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+       Lesser General Public License for more details.
+*/
+
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include <lua.h>
+#include <lauxlib.h>
+
+#include <libloc/as.h>
+
+#include "location.h"
+#include "as.h"
+
+typedef struct as {
+       struct loc_as* as;
+} AS;
+
+static AS* luaL_checkas(lua_State* L, int i) {
+       void* userdata = luaL_checkudata(L, i, "location.AS");
+
+       // Throw an error if the argument doesn't match
+       luaL_argcheck(L, userdata, i, "AS expected");
+
+       return (AS*)userdata;
+}
+
+int create_as(lua_State* L, struct loc_as* as) {
+       // Allocate a new object
+       AS* self = (AS*)lua_newuserdata(L, sizeof(*self));
+
+       // Set metatable
+       luaL_setmetatable(L, "location.AS");
+
+       // Store country
+       self->as = loc_as_ref(as);
+
+       return 1;
+}
+
+static int AS_new(lua_State* L) {
+       struct loc_as* as = NULL;
+       unsigned int n = 0;
+       int r;
+
+       // Fetch the number
+       n = luaL_checknumber(L, 1);
+
+       // Create the AS
+       r = loc_as_new(ctx, &as, n);
+       if (r)
+               return luaL_error(L, "Could not create AS %u: %s\n", n, strerror(errno));
+
+       // Return the AS
+       r = create_as(L, as);
+       loc_as_unref(as);
+
+       return r;
+}
+
+static int AS_gc(lua_State* L) {
+       AS* self = luaL_checkas(L, 1);
+
+       // Free AS
+       if (self->as) {
+               loc_as_unref(self->as);
+               self->as = NULL;
+       }
+
+       return 0;
+}
+
+static int AS_tostring(lua_State* L) {
+       AS* self = luaL_checkas(L, 1);
+
+       uint32_t number = loc_as_get_number(self->as);
+       const char* name = loc_as_get_name(self->as);
+
+       // Return string
+       if (name)
+               lua_pushfstring(L, "AS%d - %s", number, name);
+       else
+               lua_pushfstring(L, "AS%d", number);
+
+       return 1;
+}
+
+// Name
+
+static int AS_get_name(lua_State* L) {
+       AS* self = luaL_checkas(L, 1);
+
+       // Return the name
+       lua_pushstring(L, loc_as_get_name(self->as));
+
+       return 1;
+}
+
+// Number
+
+static int AS_get_number(lua_State* L) {
+       AS* self = luaL_checkas(L, 1);
+
+       // Return the number
+       lua_pushnumber(L, loc_as_get_number(self->as));
+
+       return 1;
+}
+
+static const struct luaL_Reg AS_functions[] = {
+       { "new", AS_new },
+       { "get_name", AS_get_name },
+       { "get_number", AS_get_number },
+       { "__gc", AS_gc },
+       { "__tostring", AS_tostring },
+       { NULL, NULL },
+};
+
+int register_as(lua_State* L) {
+       return register_class(L, "location.AS", AS_functions);
+}
diff --git a/src/lua/as.h b/src/lua/as.h
new file mode 100644 (file)
index 0000000..0ea34f9
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+       libloc - A library to determine the location of someone on the Internet
+
+       Copyright (C) 2024 IPFire Development Team <info@ipfire.org>
+
+       This library is free software; you can redistribute it and/or
+       modify it under the terms of the GNU Lesser General Public
+       License as published by the Free Software Foundation; either
+       version 2.1 of the License, or (at your option) any later version.
+
+       This library is distributed in the hope that it will be useful,
+       but WITHOUT ANY WARRANTY; without even the implied warranty of
+       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+       Lesser General Public License for more details.
+*/
+
+#ifndef LUA_LOCATION_AS_H
+#define LUA_LOCATION_AS_H
+
+#include <lua.h>
+#include <lauxlib.h>
+
+#include <libloc/as.h>
+
+int register_as(lua_State* L);
+
+int create_as(lua_State* L, struct loc_as* as);
+
+#endif /* LUA_LOCATION_AS_H */
index 797d2e81535f626c2788ac432a39bdce376a45bf..48466f6d00796f0fb8bcaa676987109e95fb29c5 100644 (file)
@@ -24,6 +24,7 @@
 #include <libloc/libloc.h>
 
 #include "location.h"
+#include "as.h"
 #include "country.h"
 #include "database.h"
 #include "network.h"
@@ -52,6 +53,11 @@ int luaopen_location(lua_State* L) {
        // Register functions
        luaL_newlib(L, location_functions);
 
+       // Register AS type
+       register_as(L);
+
+       lua_setfield(L, -2, "AS");
+
        // Register Country type
        register_country(L);
 
index 8fb178f695efe3d32d654b85d6fcd4bd2ddc95c5..ff904d46d1f1f9fcf92508a8454ec7581c1cad96 100755 (executable)
@@ -65,6 +65,18 @@ function test_network()
        luaunit.assertNil(n1:get_country_code())
 end
 
+function test_as()
+       location = require("location")
+
+       -- Create a new AS
+       as = location.AS.new(12345)
+       luaunit.assertEquals(as:get_number(), 12345)
+       luaunit.assertNil(as:get_name())
+
+       -- Reset
+       as = nil
+end
+
 function test_country()
        location = require("location")