From 1761726024b16f625bc4b2cedeb63764cbc05dda Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Mon, 18 Jul 2022 11:43:35 +0200 Subject: [PATCH 1/2] net/dns: provide Kconfig --- sys/net/application_layer/Kconfig | 1 + sys/net/application_layer/dns/Kconfig | 28 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 sys/net/application_layer/dns/Kconfig diff --git a/sys/net/application_layer/Kconfig b/sys/net/application_layer/Kconfig index 3f690d17f2..59c6701441 100644 --- a/sys/net/application_layer/Kconfig +++ b/sys/net/application_layer/Kconfig @@ -14,6 +14,7 @@ endmenu # CoAP rsource "cord/Kconfig" rsource "dhcpv6/Kconfig" +rsource "dns/Kconfig" menu "MQTT-SN" diff --git a/sys/net/application_layer/dns/Kconfig b/sys/net/application_layer/dns/Kconfig new file mode 100644 index 0000000000..f9e234d612 --- /dev/null +++ b/sys/net/application_layer/dns/Kconfig @@ -0,0 +1,28 @@ +# Copyright (c) 2022 Freie Universitaet Berlin +# +# 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. +# + +menuconfig KCONFIG_USEMODULE_DNS + bool "Configure DNS" + depends on USEMODULE_DNS + help + Configure DNS using Kconfig. + +if KCONFIG_USEMODULE_DNS +menuconfig KCONFIG_USEMODULE_DNS_MSG + bool "Configure DNS message parser and composer" + depends on USEMODULE_DNS_MSG + help + Configure DNS message parser and composer using Kconfig. + +if KCONFIG_USEMODULE_DNS_MSG + +config DNS_MSG_LEN + int "Maximum DNS message length" + default 128 + +endif # KCONFIG_USEMODULE_DNS_MSG +endif # KCONFIG_USEMODULE_DNS From 8bec9c11156d9cb7bbdd1fe09ceb3bb659fd74e0 Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Mon, 18 Jul 2022 11:47:57 +0200 Subject: [PATCH 2/2] sock_dns_cache: move to dns_cache Also piggy-back some fixes to the unittests and submodule handling --- makefiles/pseudomodules.inc.mk | 2 +- sys/Makefile.dep | 3 +- .../dns_cache.h => include/net/dns/cache.h} | 40 +++++++++----- sys/net/application_layer/dns/Kconfig | 24 +++++++++ sys/net/application_layer/dns/Makefile | 2 +- .../{sock_dns/dns_cache.c => dns/cache.c} | 28 +++++----- sys/net/application_layer/sock_dns/Makefile | 4 -- sys/net/application_layer/sock_dns/dns.c | 6 +-- .../Makefile | 0 .../tests-dns_cache/Makefile.include | 4 ++ .../tests-dns_cache/tests-dns_cache.c | 54 +++++++++++++++++++ .../tests-dns_cache.h} | 14 ++--- .../tests-sock_dns_cache/Makefile.include | 2 - .../tests-sock_dns_cache.c | 53 ------------------ 14 files changed, 138 insertions(+), 98 deletions(-) rename sys/{net/application_layer/sock_dns/dns_cache.h => include/net/dns/cache.h} (66%) rename sys/net/application_layer/{sock_dns/dns_cache.c => dns/cache.c} (86%) rename tests/unittests/{tests-sock_dns_cache => tests-dns_cache}/Makefile (100%) create mode 100644 tests/unittests/tests-dns_cache/Makefile.include create mode 100644 tests/unittests/tests-dns_cache/tests-dns_cache.c rename tests/unittests/{tests-sock_dns_cache/tests-sock_dns_cache.h => tests-dns_cache/tests-dns_cache.h} (67%) delete mode 100644 tests/unittests/tests-sock_dns_cache/Makefile.include delete mode 100644 tests/unittests/tests-sock_dns_cache/tests-sock_dns_cache.c diff --git a/makefiles/pseudomodules.inc.mk b/makefiles/pseudomodules.inc.mk index 992828f0c2..85b06c3021 100644 --- a/makefiles/pseudomodules.inc.mk +++ b/makefiles/pseudomodules.inc.mk @@ -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 @@ -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 diff --git a/sys/Makefile.dep b/sys/Makefile.dep index 203ecd093f..7860bd77e9 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -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 diff --git a/sys/net/application_layer/sock_dns/dns_cache.h b/sys/include/net/dns/cache.h similarity index 66% rename from sys/net/application_layer/sock_dns/dns_cache.h rename to sys/include/net/dns/cache.h index ffa05eba20..91288a18ec 100644 --- a/sys/net/application_layer/sock_dns/dns_cache.h +++ b/sys/include/net/dns/cache.h @@ -7,7 +7,8 @@ */ /** - * @ingroup net_sock_dns + * @defgroup net_dns_cache DNS cache + * @ingroup net_dns * * @brief DNS cache * @@ -29,23 +30,39 @@ * @author Benjamin Valentin */ -#ifndef DNS_CACHE_H -#define DNS_CACHE_H +#ifndef NET_DNS_CACHE_H +#define NET_DNS_CACHE_H #include +#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 * @@ -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 @@ -66,10 +83,9 @@ 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; @@ -77,8 +93,8 @@ static inline int sock_dns_cache_query(const char *domain_name, 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; @@ -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 */ /** @} */ diff --git a/sys/net/application_layer/dns/Kconfig b/sys/net/application_layer/dns/Kconfig index f9e234d612..3d1a6835db 100644 --- a/sys/net/application_layer/dns/Kconfig +++ b/sys/net/application_layer/dns/Kconfig @@ -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 diff --git a/sys/net/application_layer/dns/Makefile b/sys/net/application_layer/dns/Makefile index 8e2c538ab6..fdd0613c14 100644 --- a/sys/net/application_layer/dns/Makefile +++ b/sys/net/application_layer/dns/Makefile @@ -1,5 +1,5 @@ SRC := -SUBMODULE := 1 +SUBMODULES := 1 include $(RIOTBASE)/Makefile.base diff --git a/sys/net/application_layer/sock_dns/dns_cache.c b/sys/net/application_layer/dns/cache.c similarity index 86% rename from sys/net/application_layer/sock_dns/dns_cache.c rename to sys/net/application_layer/dns/cache.c index 8325d829c4..deb3b82e85 100644 --- a/sys/net/application_layer/sock_dns/dns_cache.c +++ b/sys/net/application_layer/dns/cache.c @@ -7,7 +7,7 @@ */ /** - * @ingroup net_sock_dns + * @ingroup net_dns_cache * @{ * @file * @brief DNS cache implementation @@ -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 @@ -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; @@ -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 { @@ -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 @@ -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)); @@ -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; diff --git a/sys/net/application_layer/sock_dns/Makefile b/sys/net/application_layer/sock_dns/Makefile index 61406763ce..8979bafbb3 100644 --- a/sys/net/application_layer/sock_dns/Makefile +++ b/sys/net/application_layer/sock_dns/Makefile @@ -1,7 +1,3 @@ SRC += dns.c -ifneq (,$(filter sock_dns_cache, $(USEMODULE))) - SRC += dns_cache.c -endif - include $(RIOTBASE)/Makefile.base diff --git a/sys/net/application_layer/sock_dns/dns.c b/sys/net/application_layer/sock_dns/dns.c index 8b857e6c82..7ad4898040 100644 --- a/sys/net/application_layer/sock_dns/dns.c +++ b/sys/net/application_layer/sock_dns/dns.c @@ -22,10 +22,10 @@ #include #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) @@ -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; } @@ -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; } } diff --git a/tests/unittests/tests-sock_dns_cache/Makefile b/tests/unittests/tests-dns_cache/Makefile similarity index 100% rename from tests/unittests/tests-sock_dns_cache/Makefile rename to tests/unittests/tests-dns_cache/Makefile diff --git a/tests/unittests/tests-dns_cache/Makefile.include b/tests/unittests/tests-dns_cache/Makefile.include new file mode 100644 index 0000000000..735814c3be --- /dev/null +++ b/tests/unittests/tests-dns_cache/Makefile.include @@ -0,0 +1,4 @@ +USEMODULE += dns_cache +USEMODULE += ipv4 +USEMODULE += ipv6 +USEMODULE += ztimer_usec diff --git a/tests/unittests/tests-dns_cache/tests-dns_cache.c b/tests/unittests/tests-dns_cache/tests-dns_cache.c new file mode 100644 index 0000000000..4aacc0acfa --- /dev/null +++ b/tests/unittests/tests-dns_cache/tests-dns_cache.c @@ -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 +#include +#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()); +} diff --git a/tests/unittests/tests-sock_dns_cache/tests-sock_dns_cache.h b/tests/unittests/tests-dns_cache/tests-dns_cache.h similarity index 67% rename from tests/unittests/tests-sock_dns_cache/tests-sock_dns_cache.h rename to tests/unittests/tests-dns_cache/tests-dns_cache.h index e19940513a..a8040f40dc 100644 --- a/tests/unittests/tests-sock_dns_cache/tests-sock_dns_cache.h +++ b/tests/unittests/tests-dns_cache/tests-dns_cache.h @@ -11,12 +11,12 @@ * @{ * * @file - * @brief Unittests for the ``sock_dns_cache`` module + * @brief Unittests for the ``dns_cache`` module * * @author Benjamin Valentin */ -#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" @@ -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 */ /** @} */ diff --git a/tests/unittests/tests-sock_dns_cache/Makefile.include b/tests/unittests/tests-sock_dns_cache/Makefile.include deleted file mode 100644 index 1942dd65b0..0000000000 --- a/tests/unittests/tests-sock_dns_cache/Makefile.include +++ /dev/null @@ -1,2 +0,0 @@ -USEMODULE += gnrc_ipv6 -USEMODULE += sock_dns_cache diff --git a/tests/unittests/tests-sock_dns_cache/tests-sock_dns_cache.c b/tests/unittests/tests-sock_dns_cache/tests-sock_dns_cache.c deleted file mode 100644 index 96f3ce9f78..0000000000 --- a/tests/unittests/tests-sock_dns_cache/tests-sock_dns_cache.c +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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 -#include -#include "net/af.h" -#include "net/ipv6.h" -#include "ztimer.h" - -#include "tests-sock_dns_cache.h" -#include "../net/application_layer/sock_dns/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, sock_dns_cache_query("example.com", &addr_out, AF_INET6)); - - /* add DNS entry, set it to expire in 1s */ - sock_dns_cache_add("example.com", &addr_in, sizeof(addr_in), 1); - TEST_ASSERT_EQUAL_INT(sizeof(addr_out), sock_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, sock_dns_cache_query("example.com", &addr_out, AF_INET)); - TEST_ASSERT_EQUAL_INT(0, sock_dns_cache_query("alt.example.com", &addr_out, AF_INET6)); - TEST_ASSERT_EQUAL_INT(0, sock_dns_cache_query("example.comm", &addr_out, AF_INET6)); - TEST_ASSERT_EQUAL_INT(0, sock_dns_cache_query("example.co", &addr_out, AF_INET6)); - - ztimer_sleep(ZTIMER_USEC, 2000000); - TEST_ASSERT_EQUAL_INT(0, sock_dns_cache_query("example.com", &addr_out, AF_INET6)); -} - -Test *tests_sock_dns_cache_tests(void) -{ - EMB_UNIT_TESTFIXTURES(fixtures) { - new_TestFixture(test_dns_cache_add), - }; - - EMB_UNIT_TESTCALLER(sock_dns_cache_tests, NULL, NULL, fixtures); - - return (Test *)&sock_dns_cache_tests; -} - -void tests_sock_dns_cache(void) -{ - TESTS_RUN(tests_sock_dns_cache_tests()); -}