]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
doh: code cleanup, merging resolve_pkt and resolve
authorPetr Špaček <petr.spacek@nic.cz>
Thu, 4 Apr 2019 10:38:53 +0000 (12:38 +0200)
committerPetr Špaček <petr.spacek@nic.cz>
Thu, 11 Apr 2019 07:12:49 +0000 (09:12 +0200)
daemon/bindings/worker.c
daemon/lua/sandbox.lua.in
modules/http/http_doh.lua

index d4761a45646344cdb8473e22d72ae4f95100d91f..3850895caa5565494411f9899c3a5c558e8fdad5 100644 (file)
@@ -18,6 +18,7 @@
 
 #include "daemon/worker.h"
 
+/** resolve_pkt(pkt, options, init_cb) */
 static int wrk_resolve_pkt(lua_State *L)
 {
        struct worker_ctx *worker = wrk_luaget(L);
@@ -25,22 +26,24 @@ static int wrk_resolve_pkt(lua_State *L)
                return 0;
        }
 
-       // FIXME: merge with wrk_resolve
-       const struct kr_qflags options = {};
        knot_pkt_t *pkt = *(knot_pkt_t **)lua_topointer(L, 1);
        if (!pkt)
                lua_error_maybe(L, ENOMEM);
 
-       /* Create task and start with a first question */
+       /* Add query options */
+       const struct kr_qflags *options = lua_topointer(L, 2);
+       if (!options) /* but we rely on the lua wrapper when dereferencing non-NULL */
+               lua_error_p(L, "invalid options");
 
-       struct qr_task *task = worker_resolve_start(worker, pkt, options);
+       /* Create task and start with a first question */
+       struct qr_task *task = worker_resolve_start(worker, pkt, *options);
        if (!task) {
                lua_error_p(L, "couldn't create a resolution request");
        }
 
        /* Add initialisation callback */
-       if (lua_isfunction(L, 2)) {
-               lua_pushvalue(L, 2);
+       if (lua_isfunction(L, 3)) {
+               lua_pushvalue(L, 3);
                lua_pushlightuserdata(L, worker_task_request(task));
                (void) execute_callback(L, 1);
        }
@@ -51,6 +54,7 @@ static int wrk_resolve_pkt(lua_State *L)
        return 1;
 }
 
+/** resolve(qname, qtype, qclass, options, init_cb) */
 static int wrk_resolve(lua_State *L)
 {
        struct worker_ctx *worker = wrk_luaget(L);
@@ -99,24 +103,12 @@ static int wrk_resolve(lua_State *L)
                knot_wire_set_cd(pkt->wire);
        }
 
-       /* Create task and start with a first question */
-       struct qr_task *task = worker_resolve_start(worker, pkt, *options);
-       if (!task) {
-               knot_rrset_free(pkt->opt_rr, NULL);
-               knot_pkt_free(pkt);
-               lua_error_p(L, "couldn't create a resolution request");
-       }
-
-       /* Add initialisation callback */
-       if (lua_isfunction(L, 5)) {
-               lua_pushvalue(L, 5);
-               lua_pushlightuserdata(L, worker_task_request(task));
-               (void) execute_callback(L, 1);
-       }
+       lua_pushcfunction(L, wrk_resolve_pkt);
+       lua_pushlightuserdata(L, &pkt);
+       lua_pushvalue(L, 4);  /* options */
+       lua_pushvalue(L, 5);  /* init_cb */
+       lua_call(L, 3, 1);  /* leaves return value on stack */
 
-       /* Start execution */
-       int ret = worker_resolve_exec(task, pkt);
-       lua_pushboolean(L, ret == 0);
        knot_rrset_free(pkt->opt_rr, NULL);
        knot_pkt_free(pkt);
        return 1;
index 481f019518fa7871855f546885fc4f2db1b852ef..56af63cfaa1f22c0d366e5c50d54015581f25c5e 100644 (file)
@@ -32,19 +32,7 @@ if rawget(kres, 'str2dname') ~= nil then
        todname = kres.str2dname
 end
 
--- Compatibility wrapper for query flags.
-worker.resolve = function (qname, qtype, qclass, options, finish, init)
-       -- Alternatively use named arguments
-       if type(qname) == 'table' then
-               local t = qname
-               qname = t.name
-               qtype = t.type or kres.type.A
-               qclass = t.class or kres.class.IN
-               options = t.options
-               finish = t.finish
-               init = t.init
-       end
-
+local function prep_resolve_cb(finish, init)
        local init_cb, finish_cb = init, nil
        if finish then
                -- Create callback for finalization
@@ -60,33 +48,33 @@ worker.resolve = function (qname, qtype, qclass, options, finish, init)
                        req.trace_finish = finish_cb
                end
        end
-
-       -- Translate options and resolve
-       options = kres.mk_qflags(options)
-       return worker.resolve_unwrapped(qname, qtype, qclass, options, init_cb)
+       return init_cb
 end
 
-worker.resolve_pkt = function (pkt, finish, init)
-       local init_cb, finish_cb = init, nil
-       if finish then
-               -- Create callback for finalization
-               finish_cb = ffi.cast('trace_callback_f', function (req)
-                       req = kres.request_t(req)
-                       finish(req.answer, req)
-                       finish_cb:free()
-               end)
-               -- Wrap initialiser to install finish callback
-               init_cb = function (req)
-                       req = kres.request_t(req)
-                       if init then init(req) end
-                       req.trace_finish = finish_cb
-               end
+-- Compatibility wrapper for query flags.
+worker.resolve = function (qname, qtype, qclass, options, finish, init)
+       -- Alternatively use named arguments
+       if type(qname) == 'table' then
+               local t = qname
+               qname = t.name
+               qtype = t.type or kres.type.A
+               qclass = t.class or kres.class.IN
+               options = t.options
+               finish = t.finish
+               init = t.init
        end
+       local init_cb = prep_resolve_cb(finish, init)
 
        -- Translate options and resolve
-       return worker.resolve_unwrapped_pkt(pkt, init_cb)
+       options = kres.mk_qflags(options)
+       return worker.resolve_unwrapped(qname, qtype, qclass, options, init_cb)
 end
 
+worker.resolve_pkt = function (pkt, options, finish, init)
+       local init_cb = prep_resolve_cb(finish, init)
+       options = kres.mk_qflags(options)
+       return worker.resolve_unwrapped_pkt(pkt, options, init_cb)
+end
 
 resolve = worker.resolve
 
index d5d67a08550a1f3a2e44ee674dce3421eaa31d4e..ae33edbd78b41500e79f52f75be0860bbb92fa3a 100644 (file)
@@ -95,7 +95,7 @@ local function serve_doh(h, stream)
        end
 
        -- resolve query
-       worker.resolve_pkt(pkt, finish_cb, init_cb)
+       worker.resolve_pkt(pkt, {}, finish_cb, init_cb)
        if not done then
                waiting = true
                cond:wait()