From 8963ad4cd5e7d590bcff34e12638154ad436dce2 Mon Sep 17 00:00:00 2001 From: Tomas Krizek Date: Thu, 25 Nov 2021 11:43:30 +0100 Subject: [PATCH] lua: add parse_rdata() utility function MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Credit for code goes to Vladimír Čunát --- NEWS | 4 ++++ daemon/lua/kres.lua | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/NEWS b/NEWS index 126615055..6b261155f 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,10 @@ Knot Resolver 5.4.3 (2021-mm-dd) ================================ +Improvements +------------ +- lua: add kres.parse_rdata() to parse RDATA from string to wire format (!1233) + Bugfixes -------- - policy.rpz: improve logs, fix origin detection in files without $ORIGIN diff --git a/daemon/lua/kres.lua b/daemon/lua/kres.lua index 140421660..3b00c2011 100644 --- a/daemon/lua/kres.lua +++ b/daemon/lua/kres.lua @@ -328,6 +328,28 @@ local function dname2wire(name) return ffi.string(name, knot.knot_dname_size(name)) end +-- Parse RDATA, from presentation to wire-format. +-- in: a table of strings, each a line describing RRTYPE+RDATA +-- out: a table of RDATA strings in wire-format +local function parse_rdata(strs, nothing) + local zonefile = require('zonefile') + if type(strs) ~= 'table' or nothing ~= nil then -- accidents like forgetting braces + error('a table of string(s) is expected', 2) + end + local res = {} + for _, line in ipairs(strs) do + if type(line) ~= 'string' then + error('table must contain strings', 2) + end + local rrs = zonefile.string('. ' .. line) + if #rrs == 0 then error('failed to parse line: ' .. line, 2) end + for _, rr in ipairs(rrs) do + table.insert(res, rr.rdata) + end + end + return res +end + -- RR sets created in Lua must have a destructor to release allocated memory local function rrset_free(rr) if rr._owner ~= nil then ffi.C.free(rr._owner) end @@ -1060,6 +1082,7 @@ kres = { end, dname2str = dname2str, dname2wire = dname2wire, + parse_rdata = parse_rdata, rr2str = rr2str, str2ip = function (ip) -- 2.47.3