]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
cache: move setting EDE "Stale Answer" to the the serve_stale module.
authormenakite <29005531+menakite@users.noreply.github.com>
Wed, 14 Aug 2024 17:36:54 +0000 (19:36 +0200)
committermenakite <29005531+menakite@users.noreply.github.com>
Mon, 19 Aug 2024 16:03:59 +0000 (18:03 +0200)
It is not guaranteed yet that the request will finish in state DONE.
This prevents other EDE codes from being applied to the request and in
case the request ends in FAIL state it produces a SERVFAIL answer with
EDE "Stale Answer", which is a bit weird.
Move setting EDEs in answer_finalize in the serve_stale module, where
the proper EDE in case of NXDOMAIN is set too.

lib/cache/api.c
modules/serve_stale/serve_stale.lua

index 0cd185346f7217d95dbe71fffbedb1a0ade3e0fe..046dae20335edafa990c7d0c115c2cd524358f5f 100644 (file)
@@ -237,9 +237,7 @@ int32_t get_new_ttl(const struct entry_h *entry, const struct kr_query *qry,
                int res_stale = qry->stale_cb(res, owner, type, qry);
                if (res_stale >= 0) {
                        VERBOSE_MSG(qry, "responding with stale answer\n");
-                       /* LATER: Perhaps we could use a more specific Stale
-                        * NXDOMAIN Answer code for applicable responses. */
-                       kr_request_set_extended_error(qry->request, KNOT_EDNS_EDE_STALE, "6Q6X");
+                       qry->request->stale_accounted = true;
                        return res_stale;
                }
        }
index c1528e804d11ca071e979237b449c4dea7b7dd29..7aa2ee787a6848a9687b925b3a6d3ae583cfc055 100644 (file)
@@ -11,7 +11,6 @@ M.callback = ffi.cast("kr_stale_cb",
        function (ttl, _, _, qry)
                --log_debug(ffi.C.SRVSTALE, '   => called back with TTL: ' .. tostring(ttl))
                if ttl + 3600 * 24 > 0 then -- at most one day stale
-                       qry.request.stale_accounted = true
                        return 1
                else
                        return -1
@@ -39,6 +38,23 @@ M.layer = {
 
                return state
        end,
+
+       answer_finalize = function (state, req)
+               local qry = req:resolved()
+               if state ~= kres.DONE or qry == nil then
+                       return state
+               end
+
+               if req.stale_accounted and qry.stale_cb ~= nil then
+                       if req.answer:rcode() == kres.rcode.NOERROR then
+                               req:set_extended_error(kres.extended_error.STALE, 'WFAC')
+                       elseif req.answer:rcode() == kres.rcode.NXDOMAIN then
+                               req:set_extended_error(kres.extended_error.STALE_NXD, 'QSF6')
+                       end
+               end
+
+               return state
+       end,
 }
 
 return M