From: Petr Špaček Date: Tue, 30 Oct 2018 12:37:18 +0000 (+0100) Subject: unify packet to string conversion between C and Lua X-Git-Tag: v4.0.0~25^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=498f3c7428afde0df55cdc89b53f90bbb24ce4e0;p=thirdparty%2Fknot-resolver.git unify packet to string conversion between C and Lua 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.) --- diff --git a/daemon/lua/kres-gen.lua b/daemon/lua/kres-gen.lua index 0651f07d3..9a7ec341f 100644 --- a/daemon/lua/kres-gen.lua +++ b/daemon/lua/kres-gen.lua @@ -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 *); diff --git a/daemon/lua/kres-gen.sh b/daemon/lua/kres-gen.sh index b5a794f31..3f17cd2b5 100755 --- a/daemon/lua/kres-gen.sh +++ b/daemon/lua/kres-gen.sh @@ -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 diff --git a/daemon/lua/kres.lua b/daemon/lua/kres.lua index 7352a9942..22fd8fa2a 100644 --- a/daemon/lua/kres.lua +++ b/daemon/lua/kres.lua @@ -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 diff --git a/lib/utils.h b/lib/utils.h index 8dabd05da..7939b091d 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -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