1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 04:52:59 +01:00

Merge pull request #18441 from miri64/dns_cache/fix/ttl0

dns_cache: handle TTL=0 properly
This commit is contained in:
Martine Lenders 2022-08-23 02:58:13 +02:00 committed by GitHub
commit f15fbb3c15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 3 deletions

View File

@ -174,14 +174,21 @@ void dns_cache_add(const char *domain_name, const void *addr_out,
DEBUG("dns_cache: lifetime of %s is %"PRIu32" s\n", domain_name, ttl);
mutex_lock(&cache_mutex);
/* iterate even if TTL = 0 just in case we need to expire */
for (unsigned i = 0; i < CONFIG_DNS_CACHE_SIZE; ++i) {
if (now > cache[i].expires || _is_empty(i)) {
if (ttl && (now > cache[i].expires || _is_empty(i))) {
_add_entry(i, hash, addr_out, addr_len, now + ttl);
goto exit;
}
if (cache[i].hash == hash && _get_len(i) == addr_len) {
DEBUG("dns_cache[%u] update ttl\n", i);
cache[i].expires = now + ttl;
if (ttl) {
cache[i].expires = now + ttl;
}
else {
/* put one second into past so that it is immediately expired */
cache[i].expires = now - 1;
}
goto exit;
}
uint32_t _ttl = cache[i].expires - now;
@ -191,7 +198,7 @@ void dns_cache_add(const char *domain_name, const void *addr_out,
}
}
if (idx >= 0) {
if (ttl && idx >= 0) {
DEBUG("dns_cache: evict first entry to expire\n");
_add_entry(idx, hash, addr_out, addr_len, now + ttl);
}

View File

@ -37,10 +37,25 @@ static void test_dns_cache_add(void)
TEST_ASSERT_EQUAL_INT(0, dns_cache_query("example.com", &addr_out, AF_INET6));
}
static void test_dns_cache_add_ttl0(void)
{
ipv6_addr_t addr_in = IPV6_ADDR_ALL_NODES_IF_LOCAL;
ipv6_addr_t addr_out;
TEST_ASSERT_EQUAL_INT(0, dns_cache_query("example.com", &addr_out, AF_INET6));
dns_cache_add("example.com", &addr_in, sizeof(addr_in), 0);
TEST_ASSERT_EQUAL_INT(0, dns_cache_query("example.com", &addr_out, AF_INET6));
dns_cache_add("example.com", &addr_in, sizeof(addr_in), 1);
TEST_ASSERT_EQUAL_INT(sizeof(addr_out), dns_cache_query("example.com", &addr_out, AF_INET6));
dns_cache_add("example.com", &addr_in, sizeof(addr_in), 0);
TEST_ASSERT_EQUAL_INT(0, dns_cache_query("example.com", &addr_out, AF_INET6));
}
Test *tests_dns_cache_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_dns_cache_add),
new_TestFixture(test_dns_cache_add_ttl0),
};
EMB_UNIT_TESTCALLER(dns_cache_tests, NULL, NULL, fixtures);