]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
daemon: added basic bindings for modules, config and cache
authorMarek Vavruša <marek.vavrusa@nic.cz>
Fri, 27 Mar 2015 12:35:32 +0000 (13:35 +0100)
committerMarek Vavruša <marek.vavrusa@nic.cz>
Fri, 27 Mar 2015 12:35:32 +0000 (13:35 +0100)
daemon/bindings.c [new file with mode: 0644]
daemon/bindings.h [new file with mode: 0644]
daemon/daemon.mk
daemon/main.c

diff --git a/daemon/bindings.c b/daemon/bindings.c
new file mode 100644 (file)
index 0000000..1ba5e05
--- /dev/null
@@ -0,0 +1,130 @@
+/*  Copyright (C) 2015 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program 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 General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "lib/cache.h"
+#include "daemon/bindings.h"
+
+/** @internal Compatibility wrapper for Lua 5.0 - 5.2 */
+#if LUA_VERSION_NUM < 502
+#define register_lib(L, name, lib) \
+       luaL_openlib((L), (name), (lib), 0)
+#else
+#define register_lib(L, name, lib) \
+       luaL_newlib((L), (lib))
+#endif
+
+/** List loaded modules */
+static int mod_list(lua_State *L)
+{
+       struct engine *engine = engine_luaget(L);
+       for (unsigned i = 0; i < engine->modules.len; ++i) {
+               struct kr_module *module = &engine->modules.at[i];
+               lua_pushstring(L, module->name);
+       }
+       return engine->modules.len;
+}
+
+/** Load module. */
+static int mod_load(lua_State *L)
+{
+       /* Check parameters */
+       int n = lua_gettop(L);
+       if (n != 1 || !lua_isstring(L, 1)) {
+               lua_pushstring(L, "expected module name");
+               lua_error(L);
+       }
+       /* Load engine module */
+       struct engine *engine = engine_luaget(L);
+       int ret = engine_register(engine, lua_tostring(L, 1));
+       if (ret != 0) {
+               lua_pushstring(L, kr_strerror(ret));
+               lua_error(L);
+       }
+       return 0;
+}
+
+/** Unload module. */
+static int mod_unload(lua_State *L)
+{
+       lua_pushstring(L, "not implemented");
+       lua_error(L);
+       return 0;
+}
+
+int lib_modules(lua_State *L)
+{
+       static const luaL_Reg lib[] = {
+               { "list",   mod_list },
+               { "load",   mod_load },
+               { "unload", mod_unload },
+               { NULL, NULL }
+       };
+
+       register_lib(L, "modules", lib);
+       return 1;
+}
+
+int lib_config(lua_State *L)
+{
+       return 0;
+}
+
+/** Open cache */
+static int cache_open(lua_State *L)
+{
+       /* Check parameters */
+       int n = lua_gettop(L);
+       if (n < 2) {
+               lua_pushstring(L, "expected (string path, int size)");
+               lua_error(L);
+       }
+
+       /* Open resolution context cache */
+       struct engine *engine = engine_luaget(L);
+       engine->resolver.cache = kr_cache_open(lua_tostring(L, 1), engine->pool, lua_tointeger(L, 2));
+       if (engine->resolver.cache == NULL) {
+               lua_pushstring(L, "invalid cache directory: ");
+               lua_pushstring(L, lua_tostring(L, 1));
+               lua_concat(L, 2);
+               lua_error(L);
+       }
+
+       lua_pushboolean(L, 1);
+       return 1;
+}
+
+static int cache_close(lua_State *L)
+{
+       struct engine *engine = engine_luaget(L);
+       if (engine->resolver.cache != NULL) {
+               kr_cache_close(engine->resolver.cache);
+               engine->resolver.cache = NULL;
+       }
+
+       return 0;
+}
+
+int lib_cache(lua_State *L)
+{
+       static const luaL_Reg lib[] = {
+               { "open",   cache_open },
+               { "close",  cache_close },
+               { NULL, NULL }
+       };
+
+       register_lib(L, "cache", lib);
+       return 0;
+}
\ No newline at end of file
diff --git a/daemon/bindings.h b/daemon/bindings.h
new file mode 100644 (file)
index 0000000..bb2fc1f
--- /dev/null
@@ -0,0 +1,43 @@
+/*  Copyright (C) 2015 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program 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 General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * Bindings to engine services, see \a http://www.lua.org/manual/5.2/manual.html#luaL_newlib for the reference.
+ */
+#pragma once
+
+#include "daemon/engine.h"
+
+/**
+ * Load 'modules' package.
+ * @param  L scriptable
+ * @return   number of packages to load
+ */
+int lib_modules(lua_State *L);
+
+/**
+ * Load 'config' package.
+ * @param  L scriptable
+ * @return   number of packages to load
+ */
+int lib_config(lua_State *L);
+
+/**
+ * Load 'cache' package.
+ * @param  L scriptable
+ * @return   number of packages to load
+ */
+int lib_cache(lua_State *L);
\ No newline at end of file
index ccb127ba2e084b6b0006419a69d9637e6fb6d954..70218f4e4d03af8e787a5ecf970716c38e134954 100644 (file)
@@ -4,6 +4,7 @@ kresolved_SOURCES := \
        daemon/tcp.c         \
        daemon/engine.c      \
        daemon/worker.c      \
+       daemon/bindings.c    \
        daemon/main.c
 
 # Embed resources
index b791223281c5d2c3402a6040fba820a78c7d51f0..396bdaba7aaf485657088a6555bd9e1c0efccad7 100644 (file)
@@ -25,6 +25,7 @@
 #include "daemon/udp.h"
 #include "daemon/tcp.h"
 #include "daemon/engine.h"
+#include "daemon/bindings.h"
 
 static void tty_read(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
 {
@@ -141,6 +142,10 @@ int main(int argc, char **argv)
                return EXIT_FAILURE;
        }
 
+       /* Load bindings */
+       engine_lualib(&engine, "modules", lib_modules);
+       engine_lualib(&engine, "config", lib_config);
+       engine_lualib(&engine, "cache",  lib_cache);
 
        /* Create main worker. */
        struct worker_ctx worker = {