diff --git a/examples/paho-mqtt/Makefile b/examples/paho-mqtt/Makefile index f5928bd05c..fe47ad90fb 100644 --- a/examples/paho-mqtt/Makefile +++ b/examples/paho-mqtt/Makefile @@ -17,6 +17,12 @@ QUIET ?= 1 WIFI_SSID ?= "Your_WiFi_name" WIFI_PASS ?= "Your_secure_password" +# Optionally include remoteDNS support. This includes resolution of names at an +# upstream DNS server and the handling of RDNSS options in Router Advertisements +# to auto-configure that upstream DNS server. +# USEMODULE += sock_dns # include DNS client +# USEMODULE += gnrc_ipv6_nib_dns # include RDNSS option handling + ifneq (,$(DEFAULT_MQTT_CLIENT_ID)) CFLAGS += -DDEFAULT_MQTT_CLIENT_ID=\"$(DEFAULT_MQTT_CLIENT_ID)\" endif diff --git a/examples/paho-mqtt/main.c b/examples/paho-mqtt/main.c index f044a74430..583db16f07 100644 --- a/examples/paho-mqtt/main.c +++ b/examples/paho-mqtt/main.c @@ -166,7 +166,7 @@ static int _cmd_con(int argc, char **argv) printf("mqtt_example: Connecting to MQTT Broker from %s %d\n", remote_ip, port); - printf("mqtt_example: Trying to connect to %s , port: %d\n", + printf("mqtt_example: Trying to connect to %s, port: %d\n", remote_ip, port); ret = NetworkConnect(&network, remote_ip, port); if (ret < 0) { diff --git a/pkg/paho-mqtt/contrib/riot_iface.c b/pkg/paho-mqtt/contrib/riot_iface.c index b9af162c4f..3df9223c9d 100644 --- a/pkg/paho-mqtt/contrib/riot_iface.c +++ b/pkg/paho-mqtt/contrib/riot_iface.c @@ -22,6 +22,7 @@ #ifdef MODULE_IPV4_ADDR #include "net/ipv4/addr.h" #endif +#include "net/dns.h" #include "net/sock/tcp.h" #include "paho_mqtt.h" #include "MQTTClient.h" @@ -120,18 +121,20 @@ int NetworkConnect(Network *n, char *addr_ip, int port) { int ret =-1; sock_tcp_ep_t remote = SOCK_IPV4_EP_ANY; - char _local_ip[IP_MAX_LEN_ADDRESS]; - strncpy(_local_ip, addr_ip, sizeof(_local_ip)); - if (IS_USED(MODULE_IPV4_ADDR) && - ipv4_addr_from_str((ipv4_addr_t *)&remote.addr, _local_ip)) { + ret = dns_query(addr_ip, &remote.addr, AF_UNSPEC); + if (ret > 0) { + remote.port = port; + remote.family = ret == 4 ? AF_INET : AF_INET6; + } + + if (IS_USED(MODULE_IPV4_ADDR) && (remote.port == 0) && + ipv4_addr_from_str((ipv4_addr_t *)&remote.addr, addr_ip)) { remote.port = port; } - /* ipvN_addr_from_str modifies input buffer */ - strncpy(_local_ip, addr_ip, sizeof(_local_ip)); - if (IS_USED(MODULE_IPV6_ADDR) && (remote.port == 0) && - ipv6_addr_from_str((ipv6_addr_t *)&remote.addr, _local_ip)) { + if (IS_USED(MODULE_IPV6_ADDR) && (remote.port == 0) && + ipv6_addr_from_str((ipv6_addr_t *)&remote.addr, addr_ip)) { remote.port = port; remote.family = AF_INET6; } diff --git a/sys/include/net/dns.h b/sys/include/net/dns.h index f5296895be..bebaf8b62f 100644 --- a/sys/include/net/dns.h +++ b/sys/include/net/dns.h @@ -74,6 +74,15 @@ static inline int dns_query(const char *domain_name, void *addr_out, int family) { int res = -ENOTSUP; + if (family == AF_UNSPEC) { + if (!IS_USED(MODULE_IPV4_ADDR)) { + family = AF_INET6; + } + else if (!IS_USED(MODULE_IPV6_ADDR)) { + family = AF_INET; + } + } + if (res <= 0 && IS_USED(MODULE_GCOAP_DNS)) { res = gcoap_dns_query(domain_name, addr_out, family); }