From: Michael Tremer Date: Thu, 22 Feb 2024 16:25:21 +0000 (+0000) Subject: lua: network: Implement checking flags X-Git-Tag: 0.9.18~144 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=af7f7210d7b12d3bde705250e1c56538921fed22;p=people%2Fms%2Flibloc.git lua: network: Implement checking flags Signed-off-by: Michael Tremer --- diff --git a/src/lua/network.c b/src/lua/network.c index 12750d5..1d5e0af 100644 --- a/src/lua/network.c +++ b/src/lua/network.c @@ -136,11 +136,26 @@ static int Network_get_country_code(lua_State* L) { return 1; } +// Has Flag? + +static int Network_has_flag(lua_State* L) { + Network* self = luaL_checknetwork(L, 1); + + // Fetch flag + int flag = luaL_checknumber(L, 2); + + // Push result + lua_pushboolean(L, loc_network_has_flag(self->network, flag)); + + return 1; +} + static const struct luaL_Reg Network_functions[] = { { "new", Network_new }, { "get_asn", Network_get_asn }, - { "get_family", Network_get_family }, { "get_country_code", Network_get_country_code }, + { "get_family", Network_get_family }, + { "has_flag", Network_has_flag }, { "__gc", Network_gc }, { "__tostring", Network_tostring }, { NULL, NULL }, diff --git a/tests/lua/main.lua b/tests/lua/main.lua index d07ac45..4a7fc5d 100755 --- a/tests/lua/main.lua +++ b/tests/lua/main.lua @@ -58,11 +58,16 @@ function test_lookup() db = location.Database.open(ENV_TEST_DATABASE) -- Perform a lookup - network = db:lookup("81.3.27.32") + network1 = db:lookup("81.3.27.32") - luaunit.assertEquals(network:get_family(), 2) -- AF_INET - luaunit.assertEquals(network:get_country_code(), "DE") - luaunit.assertEquals(network:get_asn(), 24679) + luaunit.assertEquals(network1:get_family(), 2) -- AF_INET + luaunit.assertEquals(network1:get_country_code(), "DE") + luaunit.assertEquals(network1:get_asn(), 24679) + + -- Lookup something else + network2 = db:lookup("8.8.8.8") + luaunit.assertIsTrue(network2:has_flag(location.NETWORK_FLAG_ANYCAST)) + luaunit.assertIsFalse(network2:has_flag(location.NETWORK_FLAG_DROP)) end function test_network()