From fbe88ccd2d823da7d754939a1f114479f31cef3e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladim=C3=ADr=20=C4=8Cun=C3=A1t?= Date: Thu, 15 Jun 2017 16:33:33 +0200 Subject: [PATCH] hints: change the handling of repeated hints (again) In reverse (PTR) records, now the last definition wins, and non-canonical names don't get (shadowed) records. --- lib/generic/pack.h | 19 +++++++++++++++++++ modules/hints/hints.c | 8 ++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/generic/pack.h b/lib/generic/pack.h index 54c515394..b42fd18fc 100644 --- a/lib/generic/pack.h +++ b/lib/generic/pack.h @@ -23,6 +23,8 @@ * backed by an array. * * @note Maximum object size is 2^16 bytes, see ::pack_objlen_t + * @TODO If some mistake happens somewhere, the access may end up in an infinite loop. + * (equality comparison on pointers) * * # Example usage: * @@ -113,6 +115,23 @@ static inline uint8_t *pack_obj_next(uint8_t *it) return pack_obj_val(it) + pack_obj_len(it); } +/** Return pointer to the last packed object. */ +static inline uint8_t *pack_last(pack_t pack) +{ + if (pack.len == 0) { + return NULL; + } + uint8_t *it = pack_head(pack); + uint8_t *tail = pack_tail(pack); + while (true) { + uint8_t *next = pack_obj_next(it); + if (next == tail) { + return it; + } + it = next; + } +} + /** Push object to the end of the pack * @return 0 on success, negative number on failure */ diff --git a/modules/hints/hints.c b/modules/hints/hints.c index c83fe6562..45cd5e5df 100644 --- a/modules/hints/hints.c +++ b/modules/hints/hints.c @@ -83,7 +83,7 @@ static int satisfy_reverse(struct kr_zonecut *hints, knot_pkt_t *pkt, struct kr_ knot_rrset_init(&rr, qname, KNOT_RRTYPE_PTR, KNOT_CLASS_IN); /* Append address records from hints */ - uint8_t *addr = pack_head(*addr_set); + uint8_t *addr = pack_last(*addr_set); if (addr != NULL) { size_t len = pack_obj_len(addr); void *addr_val = pack_obj_val(addr); @@ -334,8 +334,12 @@ static int load_map(struct hints_data *data, FILE *fp) continue; } char *name_tok = strtok_r(NULL, " \t\n", &saveptr); + bool canonical_name = true; while (name_tok != NULL) { - add_reverse_pair(&data->reverse_hints, name_tok, tok); + if (canonical_name) { + add_reverse_pair(&data->reverse_hints, name_tok, tok); + canonical_name = false; + } if (add_pair(&data->hints, name_tok, tok) == 0) { count += 1; } -- 2.47.2