]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
lua: add parse_rdata() utility function
authorTomas Krizek <tomas.krizek@nic.cz>
Thu, 25 Nov 2021 10:43:30 +0000 (11:43 +0100)
committerTomas Krizek <tomas.krizek@nic.cz>
Thu, 25 Nov 2021 12:13:41 +0000 (13:13 +0100)
Credit for code goes to Vladimír Čunát

NEWS
daemon/lua/kres.lua

diff --git a/NEWS b/NEWS
index 12661505563b7778f8cc9e0aaf5b13f925d8e908..6b261155fd9db6c6146f8d663900bc5934398b20 100644 (file)
--- 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
index 140421660214b456b833121e80cd3746c1d03f8a..3b00c2011ba8668567f7d79bb9ca6262743564c0 100644 (file)
@@ -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)