Skip to content

Commit

Permalink
sock_dns_cache: move to dns_cache
Browse files Browse the repository at this point in the history
Also piggy-back some fixes to the unittests and submodule handling
  • Loading branch information
miri64 committed Jul 18, 2022
1 parent 1761726 commit 8bec9c1
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 98 deletions.
2 changes: 1 addition & 1 deletion makefiles/pseudomodules.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ PSEUDOMODULES += dhcpv6_client_ia_pd
PSEUDOMODULES += dhcpv6_client_ia_na
PSEUDOMODULES += dhcpv6_client_mud_url
PSEUDOMODULES += dhcpv6_relay
PSEUDOMODULES += dns_cache
PSEUDOMODULES += dns_msg
PSEUDOMODULES += ecc_%
PSEUDOMODULES += ethos_stdio
Expand Down Expand Up @@ -215,7 +216,6 @@ PSEUDOMODULES += sock_async
PSEUDOMODULES += sock_aux_local
PSEUDOMODULES += sock_aux_rssi
PSEUDOMODULES += sock_aux_timestamp
PSEUDOMODULES += sock_dns_cache
PSEUDOMODULES += sock_dtls
PSEUDOMODULES += sock_ip
PSEUDOMODULES += sock_tcp
Expand Down
3 changes: 1 addition & 2 deletions sys/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,7 @@ ifneq (,$(filter sock_dns,$(USEMODULE)))
USEMODULE += posix_headers
endif

ifneq (,$(filter sock_dns_cache,$(USEMODULE)))
USEMODULE += sock_dns
ifneq (,$(filter dns_cache,$(USEMODULE)))
USEMODULE += ztimer_msec
USEMODULE += checksum
endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
*/

/**
* @ingroup net_sock_dns
* @defgroup net_dns_cache DNS cache
* @ingroup net_dns
*
* @brief DNS cache
*
Expand All @@ -29,23 +30,39 @@
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*/

#ifndef DNS_CACHE_H
#define DNS_CACHE_H
#ifndef NET_DNS_CACHE_H
#define NET_DNS_CACHE_H

#include <stdint.h>

#include "kernel_defines.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Number of DNS cache entries
* @brief Maximum number of DNS cache entries
*/
#ifndef CONFIG_DNS_CACHE_SIZE
#define CONFIG_DNS_CACHE_SIZE 4
#endif

#if IS_USED(MODULE_SOCK_DNS_CACHE) || DOXYGEN
/**
* @brief Handle to cache A records
*/
#ifndef CONFIG_DNS_CACHE_A
#define CONFIG_DNS_CACHE_A IS_USED(MODULE_IPV4)
#endif

/**
* @brief Handle to cache AAAA records
*/
#ifndef CONFIG_DNS_CACHE_AAAA
#define CONFIG_DNS_CACHE_AAAA IS_USED(MODULE_IPV6)
#endif

#if IS_USED(MODULE_DNS_CACHE) || DOXYGEN
/**
* @brief Get IP address for a DNS name from the DNS cache
*
Expand All @@ -56,7 +73,7 @@ extern "C" {
* @return the size of the resolved address on success
* @return <= 0 otherwise
*/
int sock_dns_cache_query(const char *domain_name, void *addr_out, int family);
int dns_cache_query(const char *domain_name, void *addr_out, int family);

/**
* @brief Add an IP address for a DNS name to the DNS cache
Expand All @@ -66,19 +83,18 @@ int sock_dns_cache_query(const char *domain_name, void *addr_out, int family);
* @param[in] addr_len length of the address in bytes
* @param[in] ttl lifetime of the entry in seconds
*/
void sock_dns_cache_add(const char *domain_name, const void *addr, int addr_len, uint32_t ttl);
void dns_cache_add(const char *domain_name, const void *addr, int addr_len, uint32_t ttl);
#else
static inline int sock_dns_cache_query(const char *domain_name,
void *addr_out, int family)
static inline int dns_cache_query(const char *domain_name, void *addr_out, int family)
{
(void)domain_name;
(void)addr_out;
(void)family;
return 0;
}

static inline void sock_dns_cache_add(const char *domain_name, const void *addr,
int addr_len, uint32_t ttl)
static inline void dns_cache_add(const char *domain_name, const void *addr,
int addr_len, uint32_t ttl)
{
(void)domain_name;
(void)addr;
Expand All @@ -91,5 +107,5 @@ static inline void sock_dns_cache_add(const char *domain_name, const void *addr,
}
#endif

#endif /* DNS_CACHE_H */
#endif /* NET_DNS_CACHE_H */
/** @} */
24 changes: 24 additions & 0 deletions sys/net/application_layer/dns/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,28 @@ config DNS_MSG_LEN
default 128

endif # KCONFIG_USEMODULE_DNS_MSG

menuconfig KCONFIG_USEMODULE_DNS_CACHE
bool "Configure DNS cache"
depends on USEMODULE_DNS_CACHE
help
Configure DNS cache using Kconfig.

if KCONFIG_USEMODULE_DNS_CACHE

config DNS_CACHE_SIZE
int "Maximum number of DNS cache entries"
default 4

config DNS_CACHE_A
bool "Handle to cache A records"
default y if USEMODULE_IPV4
default n

config DNS_CACHE_AAAA
bool "Handle to cache AAAA records"
default y if USEMODULE_IPV6
default n

endif # KCONFIG_USEMODULE_DNS_SIZE
endif # KCONFIG_USEMODULE_DNS
2 changes: 1 addition & 1 deletion sys/net/application_layer/dns/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SRC :=

SUBMODULE := 1
SUBMODULES := 1

include $(RIOTBASE)/Makefile.base
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

/**
* @ingroup net_sock_dns
* @ingroup net_dns_cache
* @{
* @file
* @brief DNS cache implementation
Expand All @@ -17,9 +17,11 @@

#include "bitfield.h"
#include "checksum/fletcher32.h"
#include "net/sock/dns.h"
#include "time_units.h"
#include "dns_cache.h"
#include "net/af.h"
#include "net/dns/cache.h"
#include "net/ipv4/addr.h"
#include "net/ipv6/addr.h"
#include "ztimer.h"

#define ENABLE_DEBUG 0
Expand All @@ -29,29 +31,29 @@ static struct dns_cache_entry {
uint32_t hash;
uint32_t expires;
union {
#if IS_ACTIVE(SOCK_HAS_IPV4)
#if IS_ACTIVE(CONFIG_DNS_CACHE_A)
ipv4_addr_t v4;
#endif
#if IS_ACTIVE(SOCK_HAS_IPV6)
#if IS_ACTIVE(CONFIG_DNS_CACHE_AAAA)
ipv6_addr_t v6;
#endif
} addr;
} cache[CONFIG_DNS_CACHE_SIZE];

#if IS_ACTIVE(SOCK_HAS_IPV4) && IS_ACTIVE(SOCK_HAS_IPV6)
#if IS_ACTIVE(CONFIG_DNS_CACHE_A) && IS_ACTIVE(CONFIG_DNS_CACHE_AAAA)
BITFIELD(cache_is_v6, CONFIG_DNS_CACHE_SIZE);

static inline uint8_t _get_len(unsigned idx)
{
return bf_isset(cache_is_v6, idx) ? 16 : 4;
}
#elif IS_ACTIVE(SOCK_HAS_IPV4)
#elif IS_ACTIVE(CONFIG_DNS_CACHE_A)
static inline uint8_t _get_len(unsigned idx)
{
(void)idx;
return 4;
}
#elif IS_ACTIVE(SOCK_HAS_IPV6)
#elif IS_ACTIVE(CONFIG_DNS_CACHE_AAAA)
static inline uint8_t _get_len(unsigned idx)
{
(void)idx;
Expand All @@ -61,7 +63,7 @@ static inline uint8_t _get_len(unsigned idx)

static void _set_len(unsigned idx, uint8_t len)
{
#if IS_ACTIVE(SOCK_HAS_IPV4) && IS_ACTIVE(SOCK_HAS_IPV6)
#if IS_ACTIVE(CONFIG_DNS_CACHE_A) && IS_ACTIVE(CONFIG_DNS_CACHE_AAAA)
if (len == 16) {
bf_set(cache_is_v6, idx);
} else {
Expand Down Expand Up @@ -94,11 +96,11 @@ static void _set_empty(unsigned idx)
static uint8_t _addr_len(int family)
{
switch (family) {
#if IS_ACTIVE(SOCK_HAS_IPV4)
#if IS_ACTIVE(CONFIG_DNS_CACHE_A)
case AF_INET:
return sizeof(ipv4_addr_t);
#endif
#if IS_ACTIVE(SOCK_HAS_IPV6)
#if IS_ACTIVE(CONFIG_DNS_CACHE_AAAA)
case AF_INET6:
return sizeof(ipv6_addr_t);
#endif
Expand All @@ -114,7 +116,7 @@ static uint32_t _hash(const void *data, size_t len)
return fletcher32(data, (len + 1) / 2);
}

int sock_dns_cache_query(const char *domain_name, void *addr_out, int family)
int dns_cache_query(const char *domain_name, void *addr_out, int family)
{
uint32_t now = ztimer_now(ZTIMER_MSEC) / MS_PER_SEC;
uint32_t hash = _hash(domain_name, strlen(domain_name));
Expand Down Expand Up @@ -153,7 +155,7 @@ static void _add_entry(uint8_t i, uint32_t hash, const void *addr_out,
_set_len(i, addr_len);
}

void sock_dns_cache_add(const char *domain_name, const void *addr_out,
void dns_cache_add(const char *domain_name, const void *addr_out,
int addr_len, uint32_t ttl)
{
uint32_t now = ztimer_now(ZTIMER_MSEC) / MS_PER_SEC;
Expand Down
4 changes: 0 additions & 4 deletions sys/net/application_layer/sock_dns/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
SRC += dns.c

ifneq (,$(filter sock_dns_cache, $(USEMODULE)))
SRC += dns_cache.c
endif

include $(RIOTBASE)/Makefile.base
6 changes: 3 additions & 3 deletions sys/net/application_layer/sock_dns/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
#include <arpa/inet.h>

#include "net/dns.h"
#include "net/dns/cache.h"
#include "net/dns/msg.h"
#include "net/sock/udp.h"
#include "net/sock/dns.h"
#include "dns_cache.h"

/* min domain name length is 1, so minimum record length is 7 */
#define DNS_MIN_REPLY_LEN (unsigned)(sizeof(dns_hdr_t) + 7)
Expand Down Expand Up @@ -80,7 +80,7 @@ int sock_dns_query(const char *domain_name, void *addr_out, int family)
return -ENOSPC;
}

res = sock_dns_cache_query(domain_name, addr_out, family);
res = dns_cache_query(domain_name, addr_out, family);
if (res) {
return res;
}
Expand All @@ -104,7 +104,7 @@ int sock_dns_query(const char *domain_name, void *addr_out, int family)
uint32_t ttl;
if ((res = dns_msg_parse_reply(dns_buf, res, family,
addr_out, &ttl)) > 0) {
sock_dns_cache_add(domain_name, addr_out, res, ttl);
dns_cache_add(domain_name, addr_out, res, ttl);
goto out;
}
}
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions tests/unittests/tests-dns_cache/Makefile.include
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
USEMODULE += dns_cache
USEMODULE += ipv4
USEMODULE += ipv6
USEMODULE += ztimer_usec
54 changes: 54 additions & 0 deletions tests/unittests/tests-dns_cache/tests-dns_cache.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2022 ML!PA Consulting GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

#include <stdint.h>
#include <string.h>
#include "net/af.h"
#include "net/ipv6.h"
#include "ztimer.h"

#include "net/dns/cache.h"

#include "tests-dns_cache.h"

static void test_dns_cache_add(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));

/* add DNS entry, set it to expire in 1s */
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));
TEST_ASSERT_EQUAL_INT(0, memcmp(&addr_in, &addr_out, sizeof(addr_in)));

TEST_ASSERT_EQUAL_INT(0, dns_cache_query("example.com", &addr_out, AF_INET));
TEST_ASSERT_EQUAL_INT(0, dns_cache_query("alt.example.com", &addr_out, AF_INET6));
TEST_ASSERT_EQUAL_INT(0, dns_cache_query("example.comm", &addr_out, AF_INET6));
TEST_ASSERT_EQUAL_INT(0, dns_cache_query("example.co", &addr_out, AF_INET6));

ztimer_sleep(ZTIMER_USEC, 2000000);
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),
};

EMB_UNIT_TESTCALLER(dns_cache_tests, NULL, NULL, fixtures);

return (Test *)&dns_cache_tests;
}

void tests_dns_cache(void)
{
TESTS_RUN(tests_dns_cache_tests());
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
* @{
*
* @file
* @brief Unittests for the ``sock_dns_cache`` module
* @brief Unittests for the ``dns_cache`` module
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*/
#ifndef TESTS_SOCK_DNS_CACHE_H
#define TESTS_SOCK_DNS_CACHE_H
#ifndef TESTS_DNS_CACHE_H
#define TESTS_DNS_CACHE_H

#include "embUnit.h"

Expand All @@ -27,18 +27,18 @@ extern "C" {
/**
* @brief The entry point of this test suite.
*/
void tests_sock_dns_cache(void);
void tests_dns_cache(void);

/**
* @brief Generates tests for sock_dns_cache
* @brief Generates tests for dns_cache
*
* @return embUnit tests if successful, NULL if not.
*/
Test *tests_sock_dns_cache_tests(void);
Test *tests_dns_cache_tests(void);

#ifdef __cplusplus
}
#endif

#endif /* TESTS_SOCK_DNS_CACHE_H */
#endif /* TESTS_DNS_CACHE_H */
/** @} */
2 changes: 0 additions & 2 deletions tests/unittests/tests-sock_dns_cache/Makefile.include

This file was deleted.

Loading

0 comments on commit 8bec9c1

Please sign in to comment.