From: Michael Tremer Date: Thu, 22 Feb 2024 15:39:25 +0000 (+0000) Subject: lua: database: Implementing fetching AS objects X-Git-Tag: 0.9.18~151 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=77ee9330b7f0c2fefa006ac5483f455e27f00acf;p=location%2Flibloc.git lua: database: Implementing fetching AS objects Signed-off-by: Michael Tremer --- diff --git a/src/lua/database.c b/src/lua/database.c index efed6aa..65e7aa6 100644 --- a/src/lua/database.c +++ b/src/lua/database.c @@ -23,6 +23,7 @@ #include #include "location.h" +#include "as.h" #include "database.h" #include "network.h" @@ -83,6 +84,29 @@ static int Database_gc(lua_State* L) { return 0; } +static int Database_get_as(lua_State* L) { + struct loc_as* as = NULL; + int r; + + Database* self = luaL_checkdatabase(L, 1); + + // Fetch number + uint32_t asn = luaL_checknumber(L, 2); + + // Fetch the AS + r = loc_database_get_as(self->db, &as, asn); + if (r) { + lua_pushnil(L); + return 1; + } + + // Create a new AS object + r = create_as(L, as); + loc_as_unref(as); + + return r; +} + static int Database_lookup(lua_State* L) { struct loc_network* network = NULL; int r; @@ -105,6 +129,7 @@ static int Database_lookup(lua_State* L) { } static const struct luaL_Reg database_functions[] = { + { "get_as", Database_get_as }, { "open", Database_open }, { "lookup", Database_lookup }, { "__gc", Database_gc }, diff --git a/tests/lua/main.lua b/tests/lua/main.lua index ff904d4..2076202 100755 --- a/tests/lua/main.lua +++ b/tests/lua/main.lua @@ -77,6 +77,24 @@ function test_as() as = nil end +function test_fetch_as() + location = require("location") + + -- Open the database + db = location.Database.open(ENV_TEST_DATABASE) + + -- Fetch an AS + as = db:get_as(0) + + -- This should not exist + luaunit.assertNil(as) + + -- Fetch something that exists + as = db:get_as(204867) + luaunit.assertEquals(as:get_number(), 204867) + luaunit.assertEquals(as:get_name(), "Lightning Wire Labs GmbH") +end + function test_country() location = require("location")