Skip to content

Commit 25df294

Browse files
committed
net: lwm2m: add socket-based DNS support
Previously, the net_app layer handled DNS support as a part of network initialization. With the move to BSD-socket APIs, we need to add support for DNS to the LwM2M library. Signed-off-by: Michael Scott <[email protected]>
1 parent 0e4e690 commit 25df294

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

subsys/net/lib/lwm2m/lwm2m_engine.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
3838
#if defined(CONFIG_LWM2M_DTLS_SUPPORT)
3939
#include <net/tls_credentials.h>
4040
#endif
41+
#if defined(CONFIG_DNS_RESOLVER)
42+
#include <net/dns_resolve.h>
43+
#endif
4144

4245
#include "lwm2m_object.h"
4346
#include "lwm2m_engine.h"
@@ -4047,6 +4050,9 @@ int lwm2m_socket_start(struct lwm2m_ctx *client_ctx)
40474050
int lwm2m_parse_peerinfo(char *url, struct sockaddr *addr, bool *use_dtls)
40484051
{
40494052
struct http_parser_url parser;
4053+
#if defined(CONFIG_DNS_RESOLVER)
4054+
struct addrinfo hints, *res;
4055+
#endif
40504056
int ret;
40514057
u16_t off, len;
40524058
u8_t tmp;
@@ -4109,7 +4115,29 @@ int lwm2m_parse_peerinfo(char *url, struct sockaddr *addr, bool *use_dtls)
41094115
#endif /* CONFIG_NET_IPV4 */
41104116

41114117
if (ret < 0) {
4118+
#if defined(CONFIG_DNS_RESOLVER)
4119+
#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_IPV4)
4120+
hints.ai_family = AF_UNSPEC;
4121+
#elif defined(CONFIG_NET_IPV6)
4122+
hints.ai_family = AF_INET6;
4123+
#else
4124+
hints.ai_family = AF_INET;
4125+
#endif /* defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_IPV4) */
4126+
hints.ai_socktype = SOCK_DGRAM;
4127+
ret = getaddrinfo(url + off, NULL, &hints, &res);
4128+
if (ret != 0) {
4129+
LOG_ERR("Unable to resolve address");
4130+
/* DNS error codes don't align with normal errors */
4131+
ret = -ENOENT;
4132+
goto cleanup;
4133+
}
4134+
4135+
memcpy(addr, res->ai_addr, sizeof(*addr));
4136+
addr->sa_family = res->ai_family;
4137+
free(res);
4138+
#else
41124139
goto cleanup;
4140+
#endif /* CONFIG_DNS_RESOLVER */
41134141
}
41144142

41154143
/* set port */

0 commit comments

Comments
 (0)