From: Marek VavruĊĦa Date: Sun, 19 Apr 2015 19:28:40 +0000 (+0200) Subject: lib: implemented deletion for zonecut X-Git-Tag: v1.0.0-beta1~236^2~3 X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=984fab967253e520d6a862c383d3a9ad7bfa486f;p=thirdparty%2Fknot-resolver.git lib: implemented deletion for zonecut --- diff --git a/lib/zonecut.c b/lib/zonecut.c index 5e229e6dd..fdfebed3e 100644 --- a/lib/zonecut.c +++ b/lib/zonecut.c @@ -112,11 +112,10 @@ int kr_zonecut_add(struct kr_zonecut *cut, const knot_dname_t *ns, const knot_rd } /* Fetch/insert nameserver. */ - const char *key = (const char *)ns; - pack_t *pack = map_get(&cut->nsset, key); + pack_t *pack = kr_zonecut_find(cut, ns); if (pack == NULL) { pack = mm_alloc(cut->pool, sizeof(*pack)); - if (!pack || (map_set(&cut->nsset, key, pack) != 0)) { + if (!pack || (map_set(&cut->nsset, (const char *)ns, pack) != 0)) { mm_free(cut->pool, pack); return kr_error(ENOMEM); } @@ -143,9 +142,7 @@ int kr_zonecut_del(struct kr_zonecut *cut, const knot_dname_t *ns, const knot_rd } /* Find the address list. */ - const char *key = (const char *)ns; - map_t *nsset = &cut->nsset; - pack_t *pack = map_get(nsset, key); + pack_t *pack = kr_zonecut_find(cut, ns); if (pack == NULL) { return kr_error(ENOENT); } @@ -154,13 +151,24 @@ int kr_zonecut_del(struct kr_zonecut *cut, const knot_dname_t *ns, const knot_rd int ret = pack_obj_del(pack, knot_rdata_data(rdata), knot_rdata_rdlen(rdata)); if (pack->len == 0) { /* No servers left, remove NS from the set. */ - free_addr_set(key, pack, cut->pool); - return map_del(nsset, key); + free_addr_set((const char *)ns, pack, cut->pool); + return map_del(&cut->nsset, (const char *)ns); } return ret; } +pack_t *kr_zonecut_find(struct kr_zonecut *cut, const knot_dname_t *ns) +{ + if (cut == NULL || ns == NULL) { + return NULL; + } + + const char *key = (const char *)ns; + map_t *nsset = &cut->nsset; + return map_get(nsset, key); +} + int kr_zonecut_set_sbelt(struct kr_zonecut *cut) { if (cut == NULL) { diff --git a/lib/zonecut.h b/lib/zonecut.h index d7e4163d8..ed2cf3765 100644 --- a/lib/zonecut.h +++ b/lib/zonecut.h @@ -22,6 +22,7 @@ #include "lib/cache.h" #include "lib/generic/map.h" +#include "lib/generic/pack.h" struct kr_rplan; @@ -51,7 +52,6 @@ void kr_zonecut_deinit(struct kr_zonecut *cut); /** * Reset zone cut to given name and clear address list. - * @note This preserves already-allocated memory. * @note This clears the address list even if the name doesn't change. * @param cut zone cut to be set * @param name new zone cut name @@ -80,6 +80,18 @@ int kr_zonecut_add(struct kr_zonecut *cut, const knot_dname_t *ns, const knot_rd */ int kr_zonecut_del(struct kr_zonecut *cut, const knot_dname_t *ns, const knot_rdata_t *rdata); +/** + * Find nameserver address list in the zone cut. + * + * @note This can be used for membership test, a non-null pack is returned + * if the nameserver name exists. + * + * @param cut + * @param ns name server name + * @return pack of addresses or NULL + */ +pack_t *kr_zonecut_find(struct kr_zonecut *cut, const knot_dname_t *ns); + /** * Populate zone cut with a root zone using SBELT :rfc:`1034` *