From 750a6967ee0f40eda9296992877c7de6805d217a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladim=C3=ADr=20=C4=8Cun=C3=A1t?= Date: Fri, 6 Sep 2019 09:54:32 +0200 Subject: [PATCH] add compatibility with libknot 2.9 It's fairly easy to keep keep compatible with both 2.8 and 2.9, so I'd go for that for now, as it may be practical. --- NEWS | 4 ++++ lib/cache/entry_list.c | 2 +- lib/cache/entry_rr.c | 25 ++++++++++--------------- lib/cache/impl.h | 17 ++++++++++++----- lib/zonecut.c | 2 +- lib/zonecut.h | 9 +++++++++ 6 files changed, 37 insertions(+), 22 deletions(-) diff --git a/NEWS b/NEWS index 72b55fd53..322f44a4b 100644 --- a/NEWS +++ b/NEWS @@ -8,6 +8,10 @@ Bugfixes (regression since 4.1.0 release, in less common cases) - prefill module: allow a different module-loading style (#506) +Improvements +------------ +- add compatibility with (future) libknot 2.9 + Knot Resolver 4.2.0 (2019-08-05) ================================ diff --git a/lib/cache/entry_list.c b/lib/cache/entry_list.c index 3d0d8165d..4969b5df5 100644 --- a/lib/cache/entry_list.c +++ b/lib/cache/entry_list.c @@ -132,7 +132,7 @@ static int entry_h_len(const knot_db_val_t val) if (!eh->is_packet) { /* Positive RRset + its RRsig set (may be empty). */ int sets = 2; while (sets-- > 0) { - d += rdataset_dematerialized_size(d); + d += KR_CACHE_RR_COUNT_SIZE + rdataset_dematerialized_size(d, NULL); if (d > data_bound) { assert(!EILSEQ); return kr_error(EILSEQ); diff --git a/lib/cache/entry_rr.c b/lib/cache/entry_rr.c index 94dd0e83e..f6d9168ad 100644 --- a/lib/cache/entry_rr.c +++ b/lib/cache/entry_rr.c @@ -55,26 +55,21 @@ static int rdataset_materialize(knot_rdataset_t * restrict rds, const uint8_t * /*&& !((size_t)data & 1)*/); assert(pool); /* not required, but that's our current usage; guard leaks */ const uint8_t *d = data; /* iterates over the cache data */ - { - uint16_t rr_count; - memcpy(&rr_count, d, sizeof(rr_count)); - d += sizeof(rr_count); - rds->count = rr_count; - if (!rr_count) { /* avoid mm_alloc(pool, 0); etc. */ - return d - data; - } - } /* First sum up the sizes for wire format length. */ - const knot_rdataset_t rds_tmp = { - .count = rds->count, - .rdata = (knot_rdata_t *)d, - }; - size_t rds_size = knot_rdataset_size(&rds_tmp); /* TODO: we might overrun here already, - but we need to trust cache anyway...*/ + /* TODO: we might overrun here already, but we need to trust cache anyway...*/ + const uint32_t rds_size = rdataset_dematerialized_size(d, &rds->count); + d += KR_CACHE_RR_COUNT_SIZE; + #if KNOT_VERSION_MINOR >= 9 + rds->size = rds_size; + #endif if (d + rds_size > data_bound) { VERBOSE_MSG(NULL, "materialize: EILSEQ!\n"); return kr_error(EILSEQ); } + if (!rds->count) { /* avoid mm_alloc(pool, 0); etc. */ + rds->rdata = NULL; + return d - data; + } rds->rdata = mm_alloc(pool, rds_size); if (!rds->rdata) { return kr_error(ENOMEM); diff --git a/lib/cache/impl.h b/lib/cache/impl.h index 6afd389a4..f38a93ca3 100644 --- a/lib/cache/impl.h +++ b/lib/cache/impl.h @@ -307,12 +307,19 @@ static inline int rdataset_dematerialize_size(const knot_rdataset_t *rds) return KR_CACHE_RR_COUNT_SIZE + (rds == NULL ? 0 : knot_rdataset_size(rds)); } -static inline int rdataset_dematerialized_size(const uint8_t *data) +/** Analyze the length of a dematerialized rdataset. + * Note that in the data it's KR_CACHE_RR_COUNT_SIZE and then this returned size. */ +static inline int rdataset_dematerialized_size(const uint8_t *data, uint16_t *rdataset_count) { - knot_rdataset_t rds; - memcpy(&rds.count, data, sizeof(rds.count)); - rds.rdata = (knot_rdata_t *)(data + sizeof(rds.count)); - return sizeof(rds.count) + knot_rdataset_size(&rds); + uint16_t count; + assert(sizeof(count) == KR_CACHE_RR_COUNT_SIZE); + memcpy(&count, data, sizeof(count)); + const uint8_t *rdata = data + sizeof(count); + if (rdataset_count) + *rdataset_count = count; + for (int i = 0; i < count; ++i) + rdata += knot_rdata_size(((knot_rdata_t *)rdata)->len); + return rdata - (data + sizeof(count)); } /** Serialize an rdataset. */ diff --git a/lib/zonecut.c b/lib/zonecut.c index 248a4c5ce..c8497787b 100644 --- a/lib/zonecut.c +++ b/lib/zonecut.c @@ -400,7 +400,7 @@ static int fetch_ns(struct kr_context *ctx, struct kr_zonecut *cut, return kr_error(ESTALE); } /* Materialize the rdataset temporarily, for simplicity. */ - knot_rdataset_t ns_rds = { 0, NULL }; + knot_rdataset_t ns_rds = { 0 }; ret = kr_cache_materialize(&ns_rds, &peek, cut->pool); if (ret < 0) { return ret; diff --git a/lib/zonecut.h b/lib/zonecut.h index 2808c66d1..4de4087c0 100644 --- a/lib/zonecut.h +++ b/lib/zonecut.h @@ -21,6 +21,15 @@ #include "lib/generic/pack.h" #include "lib/generic/trie.h" + +/* TMP: compatibility for using libknot 2.8 API with 2.9. */ +#if KNOT_VERSION_MINOR >= 9 +static inline size_t knot_rdataset_size(const knot_rdataset_t *rrs) +{ + return rrs->size; +} +#endif + struct kr_rplan; struct kr_context; -- 2.47.2