]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
unify packet to string conversion between C and Lua
authorPetr Špaček <petr.spacek@nic.cz>
Tue, 30 Oct 2018 12:37:18 +0000 (13:37 +0100)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Fri, 8 Mar 2019 14:23:20 +0000 (15:23 +0100)
There is no reason to have two distinct functions to do the same task,
especially if the Lua version did not print all the information.
(Found and amended a bit by vcunat.)

daemon/lua/kres-gen.lua
daemon/lua/kres-gen.sh
daemon/lua/kres.lua
lib/utils.h

index 0651f07d37c0d8650620de2e5de2e0f26e55af94..9a7ec341fc7f261deb5c38719f704550f204fd78 100644 (file)
@@ -303,6 +303,7 @@ int kr_pkt_recycle(knot_pkt_t *);
 int kr_pkt_clear_payload(knot_pkt_t *);
 uint16_t kr_pkt_qclass(const knot_pkt_t *);
 uint16_t kr_pkt_qtype(const knot_pkt_t *);
+char *kr_pkt_text(const knot_pkt_t *);
 void kr_rnd_buffered(void *, unsigned int);
 uint32_t kr_rrsig_sig_inception(const knot_rdata_t *);
 uint32_t kr_rrsig_sig_expiration(const knot_rdata_t *);
index b5a794f31afdf2479a6c6adf0c88e48d8b1f193b..3f17cd2b54df0a0a5ba81c4168ed149d4c1b5e8f 100755 (executable)
@@ -148,6 +148,7 @@ EOF
        kr_pkt_clear_payload
        kr_pkt_qclass
        kr_pkt_qtype
+       kr_pkt_text
        kr_rnd_buffered
        kr_rrsig_sig_inception
        kr_rrsig_sig_expiration
index 7352a9942993bde527773ee9af2c2dc15d24392d..22fd8fa2a9250606d652f880547864ad1e7e7c22 100644 (file)
@@ -492,51 +492,6 @@ local function knot_pkt_rr(section, i)
        return ret
 end
 
--- Helpers for converting packet to text
-local function section_tostring(pkt, section_id)
-       local data = {}
-       local section = pkt.sections + section_id
-       if section.count > 0 then
-               table.insert(data, string.format('\n;; %s\n', const_section_str[section_id]))
-               for j = 0, section.count - 1 do
-                       local rrset = knot_pkt_rr(section, j)
-                       local rrtype = rrset.type
-                       if rrtype ~= const_type.OPT and rrtype ~= const_type.TSIG then
-                               table.insert(data, rrset:txt_dump())
-                       end
-               end
-       end
-       return table.concat(data, '')
-end
-
-local function packet_tostring(pkt)
-       local hdr = string.format(';; ->>HEADER<<- opcode: %s; status: %s; id: %d\n',
-               const_opcode_str[pkt:opcode()], const_rcode_str[pkt:rcode()], pkt:id())
-       local flags = {}
-       for _,v in ipairs({'rd', 'tc', 'aa', 'qr', 'cd', 'ad', 'ra'}) do
-               if(pkt[v](pkt)) then table.insert(flags, v) end
-       end
-       local info = string.format(';; Flags: %s; QUERY: %d; ANSWER: %d; AUTHORITY: %d; ADDITIONAL: %d\n',
-               table.concat(flags, ' '), pkt:qdcount(), pkt:ancount(), pkt:nscount(), pkt:arcount())
-       local data = '\n'
-       if pkt.opt_rr ~= nil then
-               data = data..string.format(';; OPT PSEUDOSECTION:\n%s', pkt.opt_rr:tostring())
-       end
-       if pkt.tsig_rr ~= nil then
-               data = data..string.format(';; TSIG PSEUDOSECTION:\n%s', pkt.tsig_rr:tostring())
-       end
-       -- Zone transfer answers may omit question
-       if pkt:qdcount() > 0 then
-               data = data..string.format(';; QUESTION\n;; %s\t%s\t%s\n',
-                       dname2str(pkt:qname()), const_type_str[pkt:qtype()], const_class_str[pkt:qclass()])
-       end
-       local data_sec = {}
-       for i = const_section.ANSWER, const_section.ADDITIONAL do
-               table.insert(data_sec, section_tostring(pkt, i))
-       end
-       return hdr..info..data..table.concat(data_sec, '')
-end
-
 -- Metatype for packet
 ffi.metatype( knot_pkt_t, {
        __new = function (_, size, wire)
@@ -679,7 +634,7 @@ ffi.metatype( knot_pkt_t, {
                end,
                tostring = function(pkt)
                        assert(ffi.istype(knot_pkt_t, pkt))
-                       return packet_tostring(pkt)
+                       return ffi.string(ffi.gc(C.kr_pkt_text(pkt), C.free))
                end,
                -- Return number of remaining empty bytes in the packet
                -- This is generally useful to check if there's enough space
index 8dabd05da81c8b9e0dacc57c615ac45a3630a6a1..7939b091d1ed391b389242329cf4ea2a4c32a166 100644 (file)
@@ -420,7 +420,11 @@ int kr_ranked_rrarray_set_wire(ranked_rr_array_t *array, bool to_wire,
                               uint32_t qry_uid, bool check_dups,
                               bool (*extraCheck)(const ranked_rr_array_entry_t *));
 
-KR_PURE
+/**
+ * @return Newly allocated string representation of packet.
+ * Caller has to free() returned string.
+ */
+KR_EXPORT
 char *kr_pkt_text(const knot_pkt_t *pkt);
 
 KR_PURE