]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
fixup! modules/http DoH: allocate req.qsource on req.pool
authorVladimír Čunát <vladimir.cunat@nic.cz>
Tue, 9 Apr 2019 11:12:33 +0000 (13:12 +0200)
committerPetr Špaček <petr.spacek@nic.cz>
Thu, 11 Apr 2019 07:12:50 +0000 (09:12 +0200)
Change handling of the inability to get address;
it happens occasionally and the traces were disruptive.

modules/http/http_doh.lua

index fb4283ae1e1553e4f4dbf85335adbadf87c24732..8091250a0faedfdaca050834ec01320ce6dbf18e 100644 (file)
@@ -8,17 +8,6 @@ local function get_http_ttl(pkt)
        return ffi.C.packet_ttl(pkt, is_negative)
 end
 
-local function convert_sockaddr(pool, family, ipaddr, port)
-       local res = nil
-       if family and ipaddr and port then
-               res = ffi.C.kr_straddr_socket(ipaddr, port, pool)
-       end
-       if not res then
-               panic('failed to obtain peer IP address')
-       end
-       return res
-end
-
 -- Trace execution of DNS queries
 local function serve_doh(h, stream)
        local input
@@ -57,6 +46,14 @@ local function serve_doh(h, stream)
 --             return 406, 'only Accept: application/dns-message is supported'
 --     end
 
+       -- We get these values beforehand, because it's easier to handle errors now.
+       local _, peer_addr, peer_port = stream:peername()
+       local _, dst_addr, dst_port = stream:localname()
+       if not (peer_addr and peer_port and dst_addr and dst_port) then
+                -- The connection probably died in the meantime or something.
+               return 504, 'failed to determine your address'
+       end
+
        -- Output buffer
        local output
        local output_ttl
@@ -90,8 +87,9 @@ local function serve_doh(h, stream)
 
        -- set source address so filters can work
        local function init_cb(req)
-               req.qsource.addr = convert_sockaddr(req.pool, stream:peername())
-               req.qsource.dst_addr = convert_sockaddr(req.pool, stream:localname())
+               req.qsource.addr = ffi.C.kr_straddr_socket(peer_addr, peer_port, req.pool)
+               req.qsource.dst_addr = ffi.C.kr_straddr_socket(dst_addr, dst_port, req.pool)
+               assert(req.qsource.addr ~= nil and req.qsource.dst_addr ~= nil)
                req.qsource.flags.tcp = true
                req.qsource.flags.tls = (stream.connection:checktls() ~= nil)
                req.qsource.flags.http = true