]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
modules/http DoH: allocate req.qsource on req.pool
authorVladimír Čunát <vladimir.cunat@nic.cz>
Mon, 8 Apr 2019 13:54:43 +0000 (15:54 +0200)
committerPetr Špaček <petr.spacek@nic.cz>
Thu, 11 Apr 2019 07:12:50 +0000 (09:12 +0200)
The problem is that C structures are not scanned by lua GC,
so we'd have to keep these alive in some other way; therefore
it's simpler to just use the mempool.

modules/http/http_doh.lua

index 95bf02e3dfcfe92d1a7dc50392cf55db8677d0c8..e7a8f2a860bb9988d12c30763a425a2dd29cd2d5 100644 (file)
@@ -8,11 +8,12 @@ local function get_http_ttl(pkt)
        return ffi.C.packet_ttl(pkt, is_negative)
 end
 
-local function convert_sockaddr(family, ipaddr, port)
-       if not (family and ipaddr and port) then
+local function convert_sockaddr(pool, family, ipaddr, port)
+       local res = ffi.C.kr_straddr_socket(ipaddr, port, pool) -- resilient to bad parameters
+       if not (family and ipaddr and port and res ~= nil) then
                panic('failed to obtain peer IP address')
        end
-       return ffi.gc(ffi.C.kr_straddr_socket(ipaddr, port, nil), ffi.C.free)
+       return res
 end
 
 -- Trace execution of DNS queries
@@ -86,8 +87,8 @@ local function serve_doh(h, stream)
 
        -- set source address so filters can work
        local function init_cb(req)
-               req.qsource.addr = convert_sockaddr(stream:peername())
-               req.qsource.dst_addr = convert_sockaddr(stream:localname())
+               req.qsource.addr = convert_sockaddr(req.pool, stream:peername())
+               req.qsource.dst_addr = convert_sockaddr(req.pool, stream:localname())
                req.qsource.flags.tcp = true
                req.qsource.flags.tls = (stream.connection:checktls() ~= nil)
                req.qsource.flags.http = true