Commit 998cd5fb authored by Vladimír Čunát's avatar Vladimír Čunát

lua: most changes needed for knot 2.7

When at it, switch to generating the libzscanner bindings.
parent 07d183cb
......@@ -22,15 +22,28 @@ typedef struct {
uint16_t compress_ptr[16];
} knot_rrinfo_t;
typedef unsigned char knot_dname_t;
typedef unsigned char knot_rdata_t;
typedef struct knot_rdataset knot_rdataset_t;
struct knot_rdataset {
typedef struct {
uint16_t len;
uint8_t data[];
} knot_rdata_t;
typedef struct {
uint16_t rr_count;
knot_rdata_t *data;
};
typedef struct knot_rrset knot_rrset_t;
} knot_rdataset_t;
typedef struct {
knot_dname_t *_owner;
uint32_t _ttl;
uint16_t type;
uint16_t rclass;
knot_rdataset_t rrs;
void *additional;
} knot_rrset_t;
typedef struct knot_pkt knot_pkt_t;
typedef struct {
struct knot_pkt *pkt;
uint8_t *ptr[15];
} knot_edns_options_t;
typedef struct {
knot_pkt_t *pkt;
uint16_t pos;
uint16_t count;
} knot_pktsection_t;
......@@ -54,6 +67,7 @@ struct knot_pkt {
uint16_t flags;
knot_rrset_t *opt_rr;
knot_rrset_t *tsig_rr;
knot_edns_options_t *edns_opts;
struct {
uint8_t *pos;
size_t len;
......@@ -66,7 +80,6 @@ struct knot_pkt {
knot_mm_t mm;
knot_compr_t compr;
};
typedef struct knot_pkt knot_pkt_t;
typedef struct {
void *root;
struct knot_mm *pool;
......@@ -201,13 +214,9 @@ struct kr_cache {
typedef int32_t (*kr_stale_cb)(int32_t ttl, const knot_dname_t *owner, uint16_t type,
const struct kr_query *qry);
struct knot_rrset {
knot_dname_t *_owner;
uint16_t type;
uint16_t rclass;
knot_rdataset_t rrs;
void *additional;
};
void kr_rrset_init(knot_rrset_t *rrset, knot_dname_t *owner,
uint16_t type, uint16_t rclass, uint32_t ttl);
struct kr_nsrep {
unsigned int score;
unsigned int reputation;
......@@ -251,31 +260,20 @@ knot_dname_t *knot_dname_copy(const knot_dname_t *, knot_mm_t *);
knot_dname_t *knot_dname_from_str(uint8_t *, const char *, size_t);
_Bool knot_dname_is_equal(const knot_dname_t *, const knot_dname_t *);
_Bool knot_dname_is_sub(const knot_dname_t *, const knot_dname_t *);
int knot_dname_labels(const uint8_t *, const uint8_t *);
int knot_dname_size(const knot_dname_t *);
size_t knot_dname_labels(const uint8_t *, const uint8_t *);
size_t knot_dname_size(const knot_dname_t *);
char *knot_dname_to_str(char *, const knot_dname_t *, size_t);
size_t knot_rdata_array_size(uint16_t);
knot_rdata_t *knot_rdataset_at(const knot_rdataset_t *, size_t);
knot_rdata_t *knot_rdataset_at(const knot_rdataset_t *, uint16_t);
int knot_rdataset_merge(knot_rdataset_t *, const knot_rdataset_t *, knot_mm_t *);
int knot_rrset_add_rdata(knot_rrset_t *, const uint8_t *, const uint16_t, const uint32_t, knot_mm_t *);
void knot_rrset_init_empty(knot_rrset_t *);
uint32_t knot_rrset_ttl(const knot_rrset_t *);
int knot_rrset_add_rdata(knot_rrset_t *, const uint8_t *, uint16_t, knot_mm_t *);
int knot_rrset_txt_dump(const knot_rrset_t *, char **, size_t *, const knot_dump_style_t *);
int knot_rrset_txt_dump_data(const knot_rrset_t *, const size_t, char *, const size_t, const knot_dump_style_t *);
size_t knot_rrset_size(const knot_rrset_t *);
uint16_t knot_rrsig_type_covered(const knot_rdataset_t *, size_t);
uint32_t knot_rrsig_sig_expiration(const knot_rdataset_t *, size_t);
uint32_t knot_rrsig_sig_inception(const knot_rdataset_t *, size_t);
const knot_dname_t *knot_pkt_qname(const knot_pkt_t *);
uint16_t knot_pkt_qtype(const knot_pkt_t *);
uint16_t knot_pkt_qclass(const knot_pkt_t *);
int knot_pkt_begin(knot_pkt_t *, knot_section_t);
int knot_pkt_put_question(knot_pkt_t *, const knot_dname_t *, uint16_t, uint16_t);
int knot_pkt_put(knot_pkt_t *, uint16_t, const knot_rrset_t *, uint16_t);
const knot_rrset_t *knot_pkt_rr(const knot_pktsection_t *, uint16_t);
const knot_pktsection_t *knot_pkt_section(const knot_pkt_t *, knot_section_t);
int knot_pkt_put_rotate(knot_pkt_t *, uint16_t, const knot_rrset_t *, uint16_t, uint16_t);
knot_pkt_t *knot_pkt_new(void *, uint16_t, knot_mm_t *);
void knot_pkt_free(knot_pkt_t **);
void knot_pkt_free(knot_pkt_t *);
int knot_pkt_parse(knot_pkt_t *, unsigned int);
struct kr_rplan *kr_resolve_plan(struct kr_request *);
knot_mm_t *kr_resolve_pool(struct kr_request *);
......@@ -290,6 +288,10 @@ void kr_pkt_make_auth_header(knot_pkt_t *);
int kr_pkt_put(knot_pkt_t *, const knot_dname_t *, uint32_t, uint16_t, uint16_t, const uint8_t *, uint16_t);
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 *);
uint32_t kr_rrsig_sig_inception(const knot_rdataset_t *, size_t);
uint32_t kr_rrsig_sig_expiration(const knot_rdataset_t *, size_t);
const char *kr_inaddr(const struct sockaddr *);
int kr_inaddr_family(const struct sockaddr *);
int kr_inaddr_len(const struct sockaddr *);
......
......@@ -46,13 +46,22 @@ typedef void (*trace_callback_f)(struct kr_request *);
knot_dname_t
knot_rdata_t
knot_rdataset_t
struct knot_rdataset
knot_rrset_t
EOF
genResType() {
echo "$1" | ./scripts/gen-cdefs.sh libkres types
}
# No simple way to fixup this rename in ./kres.lua AFAIK.
genResType "knot_rrset_t" | sed 's/\<owner\>/_owner/; s/\<ttl\>/_ttl/'
./scripts/gen-cdefs.sh libkres types <<-EOF
knot_pkt_t
knot_edns_options_t
knot_pktsection_t
struct knot_compr
knot_compr_t
struct knot_pkt
knot_pkt_t
# generics
map_t
# libkres
......@@ -73,14 +82,10 @@ EOF
printf "
typedef int32_t (*kr_stale_cb)(int32_t ttl, const knot_dname_t *owner, uint16_t type,
const struct kr_query *qry);
"
genResType() {
echo "$1" | ./scripts/gen-cdefs.sh libkres types
}
# No simple way to fixup this rename in ./kres.lua AFAIK.
genResType "struct knot_rrset" | sed 's/\<owner\>/_owner/'
void kr_rrset_init(knot_rrset_t *rrset, knot_dname_t *owner,
uint16_t type, uint16_t rclass, uint32_t ttl);
"
## Some definitions would need too many deps, so shorten them.
......@@ -105,26 +110,16 @@ printf "\tchar _stub[];\n};\n"
knot_dname_size
knot_dname_to_str
# Resource records
knot_rdata_array_size
knot_rdataset_at
knot_rdataset_merge
knot_rrset_add_rdata
knot_rrset_init_empty
knot_rrset_txt_dump
knot_rrset_txt_dump_data
knot_rrset_size
knot_rrsig_type_covered
knot_rrsig_sig_expiration
knot_rrsig_sig_inception
# Packet
knot_pkt_qname
knot_pkt_qtype
knot_pkt_qclass
knot_pkt_begin
knot_pkt_put_question
knot_pkt_put
knot_pkt_rr
knot_pkt_section
knot_pkt_put_rotate
knot_pkt_new
knot_pkt_free
knot_pkt_parse
......@@ -149,6 +144,10 @@ EOF
kr_pkt_put
kr_pkt_recycle
kr_pkt_clear_payload
kr_pkt_qclass
kr_pkt_qtype
kr_rrsig_sig_inception
kr_rrsig_sig_expiration
kr_inaddr
kr_inaddr_family
kr_inaddr_len
......
......@@ -340,12 +340,13 @@ local knot_rrset_pt = ffi.typeof('knot_rrset_t *')
local knot_rrset_t = ffi.typeof('knot_rrset_t')
ffi.metatype( knot_rrset_t, {
-- Create a new empty RR set object with an allocated owner and a destructor
__new = function (ct, owner, rrtype, rrclass)
__new = function (ct, owner, rrtype, rrclass, ttl)
local rr = ffi.new(ct)
knot.knot_rrset_init_empty(rr)
rr._owner = owner and knot.knot_dname_copy(owner, nil)
rr.type = rrtype or 0
rr.rclass = rrclass or const_class.IN
C.kr_rrset_init(rr,
owner and knot.knot_dname_copy(owner, nil),
rrtype or 0,
rrclass or const_class.IN,
ttl or 0)
return ffi.gc(rr, rrset_free)
end,
-- beware: `owner` and `rdata` are typed as a plain lua strings
......@@ -361,7 +362,7 @@ ffi.metatype( knot_rrset_t, {
end,
ttl = function(rr)
assert(ffi.istype(knot_rrset_t, rr))
return tonumber(rr.ttl)
return tonumber(rr._ttl)
end,
class = function(rr, val)
assert(ffi.istype(knot_rrset_t, rr))
......@@ -477,14 +478,19 @@ local function pkt_bit(pkt, byteoff, bitmask, val)
return (bit.band(pkt.wire[byteoff], bitmask) ~= 0)
end
local function knot_pkt_rr(section, i)
assert(section and ffi.istype('knot_pktsection_t', section))
return section.pkt.rr + section.pos + i;
end
-- Helpers for converting packet to text
local function section_tostring(pkt, section_id)
local data = {}
local section = knot.knot_pkt_section(pkt, section_id)
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.knot_pkt_rr(section, j)
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())
......@@ -581,23 +587,24 @@ ffi.metatype( knot_pkt_t, {
-- Question
qname = function(pkt)
assert(ffi.istype(knot_pkt_t, pkt))
local qname = knot.knot_pkt_qname(pkt)
return dname2wire(qname)
-- inlined knot_pkt_qname(), basically
if pkt == nil or pkt.qname_size == 0 then return nil end
return ffi.string(pkt.wire + 12, pkt.qname_size)
end,
qclass = function(pkt)
assert(ffi.istype(knot_pkt_t, pkt))
return knot.knot_pkt_qclass(pkt)
return C.kr_pkt_qclass(pkt)
end,
qtype = function(pkt)
assert(ffi.istype(knot_pkt_t, pkt))
return knot.knot_pkt_qtype(pkt)
return C.kr_pkt_qtype(pkt)
end,
rrsets = function (pkt, section_id)
assert(ffi.istype(knot_pkt_t, pkt))
local records = {}
local section = knot.knot_pkt_section(pkt, section_id)
local section = pkt.sections[section_id]
for i = 1, section.count do
local rrset = knot.knot_pkt_rr(section, i - 1)
local rrset = knot_pkt_rr(section, i - 1)
table.insert(records, ffi.cast(knot_rrset_pt, rrset))
end
return records
......@@ -605,9 +612,9 @@ ffi.metatype( knot_pkt_t, {
section = function (pkt, section_id)
assert(ffi.istype(knot_pkt_t, pkt))
local records = {}
local section = knot.knot_pkt_section(pkt, section_id)
local section = pkt.sections[section_id]
for i = 1, section.count do
local rrset = knot.knot_pkt_rr(section, i - 1)
local rrset = knot_pkt_rr(section, i - 1)
for k = 1, rrset:rdcount() do
table.insert(records, rrset:get(k - 1))
end
......@@ -633,7 +640,7 @@ ffi.metatype( knot_pkt_t, {
put_rr = function (pkt, rr)
assert(ffi.istype(knot_pkt_t, pkt))
assert(ffi.istype(knot_rrset_t, rr))
local ret = C.knot_pkt_put(pkt, 0, rr, 0)
local ret = C.knot_pkt_put_rotate(pkt, 0, rr, 0, 0)
if ret ~= 0 then return nil, knot_error_t(ret) end
return true
end,
......@@ -915,6 +922,8 @@ kres = {
return ffi.string(addr_buf, C.kr_family_len(family))
end,
context = function () return ffi.cast('struct kr_context *', __engine) end,
knot_pkt_rr = knot_pkt_rr,
}
return kres
This diff is collapsed.
......@@ -1010,3 +1010,26 @@ int knot_dname_lf2wire(knot_dname_t * const dst, uint8_t len, const uint8_t *lf)
++d;
return d - dst;
}
void kr_rrset_init(knot_rrset_t *rrset, knot_dname_t *owner,
uint16_t type, uint16_t rclass, uint32_t ttl)
{
assert(rrset);
knot_rrset_init(rrset, owner, type, rclass, ttl);
}
uint16_t kr_pkt_qclass(const knot_pkt_t *pkt)
{
return knot_pkt_qclass(pkt);
}
uint16_t kr_pkt_qtype(const knot_pkt_t *pkt)
{
return knot_pkt_qtype(pkt);
}
uint32_t kr_rrsig_sig_inception(const knot_rdataset_t *rrs, size_t pos)
{
return knot_rrsig_sig_inception(rrs, pos);
}
uint32_t kr_rrsig_sig_expiration(const knot_rdataset_t *rrs, size_t pos)
{
return knot_rrsig_sig_expiration(rrs, pos);
}
......@@ -436,3 +436,11 @@ static inline int kr_dname_lf(uint8_t *dst, const knot_dname_t *src, bool add_wi
dst[0] = len;
return KNOT_EOK;
};
/* Trivial non-inline wrappers, to be used in lua. */
KR_EXPORT void kr_rrset_init(knot_rrset_t *rrset, knot_dname_t *owner,
uint16_t type, uint16_t rclass, uint32_t ttl);
KR_EXPORT uint16_t kr_pkt_qclass(const knot_pkt_t *pkt);
KR_EXPORT uint16_t kr_pkt_qtype(const knot_pkt_t *pkt);
KR_EXPORT uint32_t kr_rrsig_sig_inception(const knot_rdataset_t *rrs, size_t pos);
KR_EXPORT uint32_t kr_rrsig_sig_expiration(const knot_rdataset_t *rrs, size_t pos);
-- Module interface
local ffi = require('ffi')
local knot = ffi.load(libknot_SONAME)
local mod = {}
local event_id = nil
......@@ -26,8 +25,8 @@ local function check_time_callback(pkt, req)
if rr.type == kres.type.RRSIG then
for k = 0, rr.rrs.rr_count - 1 do
seen_rrsigs = seen_rrsigs + 1
inception = knot.knot_rrsig_sig_inception(rr.rrs, k)
expiration = knot.knot_rrsig_sig_expiration(rr.rrs, k)
inception = ffi.C.kr_rrsig_sig_inception(rr.rrs, k)
expiration = ffi.C.kr_rrsig_sig_expiration(rr.rrs, k)
if now > expiration then
-- possitive value = in the future
time_diff = now - expiration
......
......@@ -79,15 +79,15 @@ function M.layer.consume(state, req, pkt)
local rank = neg_rank
if orig.rank < rank then rank = orig.rank end
-- Disable GC, as this object doesn't own owner or RDATA, it's just a reference
local rrs = ffi.gc(kres.rrset(nil, kres.type.AAAA, orig.rr.rclass), nil)
rrs._owner = ffi.cast('knot_dname_t *', orig.rr:owner()) -- explicit cast needed here
for k = 1, orig.rr.rrs.rr_count do
local ttl = orig.rr:ttl()
if ttl > max_ttl then ttl = max_ttl end
local rrs = ffi.gc(kres.rrset(nil, kres.type.AAAA, orig.rr.rclass, ttl), nil)
rrs._owner = orig.rr._owner
for k = 1, orig.rr.rrs.count do
local rdata = orig.rr:rdata( k - 1 )
ffi.copy(addr_buf, M.proxy, 12)
ffi.copy(addr_buf + 12, rdata, 4)
local ttl = orig.rr:ttl()
if ttl > max_ttl then ttl = max_ttl end
ffi.C.knot_rrset_add_rdata(rrs, ffi.string(addr_buf, 16), 16, ttl, req.pool)
ffi.C.knot_rrset_add_rdata(rrs, ffi.string(addr_buf, 16), 16, req.pool)
end
ffi.C.kr_ranked_rrarray_add(
req.answ_selected,
......
......@@ -90,7 +90,7 @@ local function priming_callback(pkt, req)
internal.to_resolve = internal.to_resolve + 2 * rr.rrs.rr_count
for k = 0, rr.rrs.rr_count-1 do
local nsname_text = rr:tostring(k)
local nsname_wire = rr:rdata(k)
local nsname_wire = rr:rdata(k) -- FIXME: something is wrong
internal.nsset[nsname_wire] = {}
resolve(nsname_text, kres.type.A, kres.class.IN, 0, address_callback)
resolve(nsname_text, kres.type.AAAA, kres.class.IN, 0, address_callback)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment