From 0b0910f82ad7b3c2f3b21abae3f298eed2851314 Mon Sep 17 00:00:00 2001 From: Hendrik van Essen Date: Sat, 1 Jan 2022 23:39:04 +0100 Subject: [PATCH] sys/net/application_layer/sock_dns: add pseudomodule auto_init_sock_dns --- sys/Makefile.dep | 9 ++++++ sys/auto_init/auto_init.c | 6 ++++ sys/include/net/sock/dns.h | 36 ++++++++++++++++++++++ sys/net/application_layer/sock_dns/dns.c | 38 +++++++++++++++++++++++- 4 files changed, 88 insertions(+), 1 deletion(-) diff --git a/sys/Makefile.dep b/sys/Makefile.dep index a1d665190876..8f2789d88663 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -24,6 +24,15 @@ ifneq (,$(filter auto_init_gnrc_netif,$(USEMODULE))) USEMODULE += gnrc_netif_init_devs endif +ifneq (,$(filter auto_init_sock_dns,$(USEMODULE))) + ifneq (,$(filter ipv4,$(USEMODULE))) + USEMODULE += ipv4_addr + endif + ifneq (,$(filter ipv6,$(USEMODULE))) + USEMODULE += ipv6_addr + endif +endif + ifneq (,$(filter congure_%,$(USEMODULE))) USEMODULE += congure endif diff --git a/sys/auto_init/auto_init.c b/sys/auto_init/auto_init.c index e94e3c5e3671..987b8c1036e7 100644 --- a/sys/auto_init/auto_init.c +++ b/sys/auto_init/auto_init.c @@ -319,4 +319,10 @@ void auto_init(void) extern void benchmark_udp_auto_init(void); benchmark_udp_auto_init(); } + + if (IS_USED(MODULE_AUTO_INIT_SOCK_DNS)) { + LOG_DEBUG("Auto init sock_dns.\n"); + extern void auto_init_sock_dns(void); + auto_init_sock_dns(); + } } diff --git a/sys/include/net/sock/dns.h b/sys/include/net/sock/dns.h index 5a9db35ea67f..466f26fa5267 100644 --- a/sys/include/net/sock/dns.h +++ b/sys/include/net/sock/dns.h @@ -18,6 +18,7 @@ * @brief DNS sock definitions * * @author Kaspar Schleiser + * @author Hendrik van Essen */ #ifndef NET_SOCK_DNS_H @@ -35,6 +36,41 @@ extern "C" { #endif +#ifdef MODULE_AUTO_INIT_SOCK_DNS +/** + * @brief IP version of the address provided with CONFIG_AUTO_INIT_SOCK_DNS_SERVER_ADDR + */ +#ifndef CONFIG_AUTO_INIT_SOCK_DNS_IP_VERSION + /* IPv6 is preferred */ + #if defined(SOCK_HAS_IPV6) + #define CONFIG_AUTO_INIT_SOCK_DNS_IP_VERSION 6 + #elif defined(SOCK_HAS_IPV4) + #define CONFIG_AUTO_INIT_SOCK_DNS_IP_VERSION 4 + #else + #error "Neither IPv4 nor IPv6 included in build" + #endif +#endif + +/** + * @brief Address of the DNS server + */ +#ifndef CONFIG_AUTO_INIT_SOCK_DNS_SERVER_ADDR + /* Default to Google Public DNS */ + #if CONFIG_AUTO_INIT_SOCK_DNS_IP_VERSION == 6 + #define CONFIG_AUTO_INIT_SOCK_DNS_SERVER_ADDR "2001:4860:4860::8888" + #elif CONFIG_AUTO_INIT_SOCK_DNS_IP_VERSION == 4 + #define CONFIG_AUTO_INIT_SOCK_DNS_SERVER_ADDR "8.8.8.8" + #endif +#endif + +/** + * @brief Port of the DNS server + */ +#ifndef CONFIG_AUTO_INIT_SOCK_DNS_SERVER_PORT +#define CONFIG_AUTO_INIT_SOCK_DNS_SERVER_PORT SOCK_DNS_PORT +#endif +#endif /* MODULE_AUTO_INIT_SOCK_DNS */ + /** * @name DNS defines * @{ diff --git a/sys/net/application_layer/sock_dns/dns.c b/sys/net/application_layer/sock_dns/dns.c index 664e52af7516..86e96cfba565 100644 --- a/sys/net/application_layer/sock_dns/dns.c +++ b/sys/net/application_layer/sock_dns/dns.c @@ -12,23 +12,59 @@ * @file * @brief sock DNS client implementation * @author Kaspar Schleiser + * @author Hendrik van Essen * @} */ #include #include +#include + #include "net/dns.h" #include "net/dns/msg.h" #include "net/sock/udp.h" #include "net/sock/dns.h" /* min domain name length is 1, so minimum record length is 7 */ -#define DNS_MIN_REPLY_LEN (unsigned)(sizeof(dns_hdr_t ) + 7) +#define DNS_MIN_REPLY_LEN (unsigned)(sizeof(dns_hdr_t) + 7) /* global DNS server UDP endpoint */ sock_udp_ep_t sock_dns_server; +#ifdef MODULE_AUTO_INIT_SOCK_DNS +void auto_init_sock_dns(void) +{ + assert( CONFIG_AUTO_INIT_SOCK_DNS_IP_VERSION == 4 + || CONFIG_AUTO_INIT_SOCK_DNS_IP_VERSION == 6); + + assert( CONFIG_AUTO_INIT_SOCK_DNS_SERVER_PORT > 0 + && CONFIG_AUTO_INIT_SOCK_DNS_SERVER_PORT <= 0xffff); + + switch (CONFIG_AUTO_INIT_SOCK_DNS_IP_VERSION) { +#ifdef SOCK_HAS_IPV4 + case 4: + inet_pton(AF_INET, CONFIG_AUTO_INIT_SOCK_DNS_SERVER_ADDR, + sock_dns_server.addr.ipv4); + sock_dns_server.family = AF_INET; + break; +#endif +#ifdef SOCK_HAS_IPV6 + case 6: + inet_pton(AF_INET6, CONFIG_AUTO_INIT_SOCK_DNS_SERVER_ADDR, + sock_dns_server.addr.ipv6); + sock_dns_server.family = AF_INET6; + break; +#endif + default: + assert(0); + return; + } + + sock_dns_server.port = CONFIG_AUTO_INIT_SOCK_DNS_SERVER_PORT; +} +#endif /* MODULE_AUTO_INIT_SOCK_DNS */ + int sock_dns_query(const char *domain_name, void *addr_out, int family) { static uint8_t dns_buf[CONFIG_DNS_MSG_LEN];