]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
kres: preserve error code value along with the text explanation
authorMarek Vavruša <marek@vavrusa.com>
Thu, 15 Mar 2018 22:46:55 +0000 (15:46 -0700)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Mon, 23 Apr 2018 12:34:40 +0000 (14:34 +0200)
Wrapping the error code in a structure preserves both the numeric value
and the ability to convert it into textual format.

daemon/lua/kres.lua
tests/config/basic.test.lua

index 78c85569681d5c11572c814be8380c01f2ff33b1..b75f5c79a9bf50af8d15cdd2907be5d2cb9df40d 100644 (file)
@@ -44,6 +44,9 @@ struct sockaddr {
     uint16_t sa_family;
     uint8_t _stub[]; /* Do not touch */
 };
+struct knot_error {
+       int code;
+};
 
 /*
  * libc APIs
@@ -56,10 +59,14 @@ int gettimeofday(struct timeval *tv, struct timezone *tz);
 
 require('kres-gen')
 
--- Convert libknot error strings
-local function knot_strerror(r)
-       return ffi.string(knot.knot_strerror(r))
-end
+-- Error code representation
+local knot_error_t = ffi.typeof('struct knot_error')
+ffi.metatype(knot_error_t, {
+       -- Convert libknot error strings
+       __tostring = function(self)
+               return ffi.string(knot.knot_strerror(self.code))
+       end,
+});
 
 -- Constant tables
 local const_class = {
@@ -348,7 +355,7 @@ ffi.metatype( knot_rrset_t, {
                add_rdata = function (rr, rdata, rdlen, ttl)
                        assert(ffi.istype(knot_rrset_t, rr))
                        local ret = knot.knot_rrset_add_rdata(rr, rdata, tonumber(rdlen), tonumber(ttl or 0), nil)
-                       if ret ~= 0 then return nil, knot_strerror(ret) end
+                       if ret ~= 0 then return nil, knot_error_t(ret) end
                        return true
                end,
                -- Merge data from another RR set into the current one
@@ -356,7 +363,7 @@ ffi.metatype( knot_rrset_t, {
                        assert(ffi.istype(knot_rrset_t, rr))
                        assert(ffi.istype(knot_rrset_t, source))
                        local ret = knot.knot_rdataset_merge(rr.rrs, source.rrs, nil)
-                       if ret ~= 0 then return nil, knot_strerror(ret) end
+                       if ret ~= 0 then return nil, knot_error_t(ret) end
                        return true
                end,
                -- Return type covered by this RRSIG
@@ -544,25 +551,25 @@ ffi.metatype( knot_pkt_t, {
                        assert(section >= pkt.current, 'cannot rewind to already written section')
                        assert(const_section_str[section], string.format('invalid section: %s', section))
                        local ret = knot.knot_pkt_begin(pkt, section)
-                       if ret ~= 0 then return nil, knot_strerror(ret) end
+                       if ret ~= 0 then return nil, knot_error_t(ret) end
                        return true
                end,
                put = function (pkt, owner, ttl, rclass, rtype, rdata)
                        assert(ffi.istype(knot_pkt_t, pkt))
                        local ret = C.kr_pkt_put(pkt, owner, ttl, rclass, rtype, rdata, #rdata)
-                       if ret ~= 0 then return nil, knot_strerror(ret) end
+                       if ret ~= 0 then return nil, knot_error_t(ret) end
                        return true
                end,
                recycle = function (pkt)
                        assert(ffi.istype(knot_pkt_t, pkt))
                        local ret = C.kr_pkt_recycle(pkt)
-                       if ret ~= 0 then return nil, knot_strerror(ret) end
+                       if ret ~= 0 then return nil, knot_error_t(ret) end
                        return true
                end,
                clear_payload = function (pkt)
                        assert(ffi.istype(knot_pkt_t, pkt))
                        local ret = C.kr_pkt_clear_payload(pkt)
-                       if ret ~= 0 then return nil, knot_strerror(ret) end
+                       if ret ~= 0 then return nil, knot_error_t(ret) end
                        return true
                end,
                question = function(pkt, qname, qclass, qtype)
@@ -570,7 +577,7 @@ ffi.metatype( knot_pkt_t, {
                        assert(qclass ~= nil, string.format('invalid class: %s', qclass))
                        assert(qtype ~= nil, string.format('invalid type: %s', qtype))
                        local ret = C.knot_pkt_put_question(pkt, qname, qclass, qtype)
-                       if ret ~= 0 then return nil, knot_strerror(ret) end
+                       if ret ~= 0 then return nil, knot_error_t(ret) end
                        return true
                end,
                towire = function (pkt)
@@ -585,7 +592,7 @@ ffi.metatype( knot_pkt_t, {
                parse = function (pkt)
                        assert(ffi.istype(knot_pkt_t, pkt))
                        local ret = knot.knot_pkt_parse(pkt, 0)
-                       if ret ~= 0 then return nil, knot_strerror(ret) end
+                       if ret ~= 0 then return nil, knot_error_t(ret) end
                        return true
                end,
        },
@@ -679,13 +686,13 @@ ffi.metatype( kr_cache_t, {
                        end
                        -- Insert record into cache
                        local ret = C.kr_cache_insert_rr(self, rr, rrsig, tonumber(rank or 0), timestamp)
-                       if ret ~= 0 then return nil, knot_strerror(ret) end
+                       if ret ~= 0 then return nil, knot_error_t(ret) end
                        return true
                end,
                sync = function (self)
                        assert(ffi.istype(kr_cache_t, self))
                        local ret = C.kr_cache_sync(self)
-                       if ret ~= 0 then return nil, knot_strerror(ret) end
+                       if ret ~= 0 then return nil, knot_error_t(ret) end
                        return true
                end,
        },
index 47a06919a0079828e4d75d045e287fd43fb5d360..55f133393c5beed67ef16bc8de22ec6c023c74c1 100644 (file)
@@ -96,6 +96,10 @@ local function test_packet_functions()
        same(pkt:qclass(), kres.class.IN, 'reading QCLASS works')
        -- Test manipulating sections
        ok(pkt:begin(kres.section.ANSWER), 'switching sections works')
+       local res, err = pkt:put(nil, 0, 0, 0, '')
+       isnt(res, 'inserting nil entry doesnt work')
+       isnt(err.code, 0, 'error code is non-zero')
+       isnt(tostring(res), '', 'inserting nil returns invalid parameter')
        ok(pkt:put(pkt:qname(), 900, pkt:qclass(), kres.type.A, '\1\2\3\4'), 'adding rrsets works')
        boom(pkt.begin, {pkt, 10}, 'switching to invalid section doesnt work')
        ok(pkt:begin(kres.section.ADDITIONAL), 'switching to different section works')