From: Vladimír Čunát Date: Wed, 21 Mar 2018 09:40:36 +0000 (+0100) Subject: nitpicks around older changes for stale-serving X-Git-Tag: v3.2.0~29^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=046a088fe72bebe7afe86e45578df629b111a411;p=thirdparty%2Fknot-resolver.git nitpicks around older changes for stale-serving --- diff --git a/daemon/bindings.c b/daemon/bindings.c index 6b4948e8b..259c6c7a4 100644 --- a/daemon/bindings.c +++ b/daemon/bindings.c @@ -1242,8 +1242,8 @@ static int cache_ns_tout(lua_State *L) lua_error(L); } - lua_Number interval_lua = lua_tonumber(L, 1); - if (!(interval_lua >= 0 && interval_lua < UINT_MAX)) { + lua_Integer interval_lua = lua_tointeger(L, 1); + if (!(interval_lua > 0 && interval_lua < UINT_MAX)) { format_error(L, "invalid interval specified, it must be in range > 0, < " xstr(UINT_MAX)); lua_error(L); } diff --git a/lib/generic/lru.h b/lib/generic/lru.h index 61f3ce085..b5c9bcd69 100644 --- a/lib/generic/lru.h +++ b/lib/generic/lru.h @@ -125,14 +125,14 @@ * @param table pointer to LRU * @param key_ lookup key * @param len_ key lengthkeys - * @param res pointer to bool to store result of operation + * @param is_new pointer to bool to store result of operation * (true if entry is newly added, false otherwise; can be NULL). * @return pointer to data or NULL (can be even if memory could be allocated!) */ -#define lru_get_new(table, key_, len_, res) \ +#define lru_get_new(table, key_, len_, is_new) \ (__typeof__((table)->pdata_t)) \ lru_get_impl(&(table)->lru, (key_), (len_), \ - sizeof(*(table)->pdata_t), true, res) + sizeof(*(table)->pdata_t), true, is_new) /** * @brief Apply a function to every item in LRU. diff --git a/lib/nsrep.c b/lib/nsrep.c index f5ef21ce3..ee5f34191 100644 --- a/lib/nsrep.c +++ b/lib/nsrep.c @@ -94,8 +94,8 @@ static unsigned eval_addr_set(const pack_t *addr_set, struct kr_context *ctx, uint64_t now = kr_now(); /* Name server is better candidate if it has address record. */ - uint8_t *it = pack_head(*addr_set); - while (it != pack_tail(*addr_set)) { + for (uint8_t *it = pack_head(*addr_set); it != pack_tail(*addr_set); + it = pack_obj_next(it)) { void *val = pack_obj_val(it); size_t len = pack_obj_len(it); unsigned favour = 0; @@ -112,10 +112,10 @@ static unsigned eval_addr_set(const pack_t *addr_set, struct kr_context *ctx, } if (!is_valid) { - goto get_next_iterator; + continue; } - /* Get RTT for this address (if known) */ + /* Get score for the current address. */ kr_nsrep_rtt_lru_entry_t *cached = rtt_cache ? lru_get_try(rtt_cache, val, len) : NULL; @@ -167,8 +167,6 @@ static unsigned eval_addr_set(const pack_t *addr_set, struct kr_context *ctx, break; } } - get_next_iterator: - it = pack_obj_next(it); } /* At this point, rtt_cache_entry_ptr contains up to KR_NSREP_MAXADDR @@ -380,7 +378,7 @@ int kr_nsrep_elect_addr(struct kr_query *qry, struct kr_context *ctx) int kr_nsrep_update_rtt(struct kr_nsrep *ns, const struct sockaddr *addr, unsigned score, kr_nsrep_rtt_lru_t *cache, int umode) { - if (!cache || umode > KR_NS_MAX) { + if (!cache || umode > KR_NS_MAX || umode < 0) { return kr_error(EINVAL); } @@ -409,15 +407,10 @@ int kr_nsrep_update_rtt(struct kr_nsrep *ns, const struct sockaddr *addr, if (score <= KR_NS_GLUED) { score = KR_NS_GLUED + 1; } - /* First update is always set unless KR_NS_UPDATE_NORESET mode used. */ - if (is_new_entry) { - if (umode == KR_NS_UPDATE_NORESET) { - /* Zero initial value. */ - cur->score = 0; - } else { - /* Force KR_NS_RESET otherwise. */ - umode = KR_NS_RESET; - } + /* If there's nothing to update, we reset it unless KR_NS_UPDATE_NORESET + * mode was requested. New items are zeroed by LRU automatically. */ + if (is_new_entry && umode != KR_NS_UPDATE_NORESET) { + umode = KR_NS_RESET; } unsigned new_score = 0; /* Update score, by default smooth over last two measurements. */ @@ -428,7 +421,7 @@ int kr_nsrep_update_rtt(struct kr_nsrep *ns, const struct sockaddr *addr, case KR_NS_RESET: new_score = score; break; case KR_NS_ADD: new_score = MIN(KR_NS_MAX_SCORE - 1, cur->score + score); break; case KR_NS_MAX: new_score = MAX(cur->score, score); break; - default: break; + default: return kr_error(EINVAL); } /* Score limits */ if (new_score > KR_NS_MAX_SCORE) {