]> git.ipfire.org Git - location/libloc.git/commitdiff
lua: network: Implement checking flags
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 22 Feb 2024 16:25:21 +0000 (16:25 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 22 Feb 2024 16:25:21 +0000 (16:25 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/lua/network.c
tests/lua/main.lua

index 12750d55f44361b4c21cb18ff32e3933c5244a99..1d5e0af4d5c1faa83034827d31b47b067edf70bb 100644 (file)
@@ -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 },
index d07ac450824adb9a311a159a90b27e35827ab197..4a7fc5df7382aa8113b3fd25a5e684dbafc58477 100755 (executable)
@@ -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()