Skip to content

Commit

Permalink
Merge pull request #13450 from benpicco/dns-fixes
Browse files Browse the repository at this point in the history
fix DNS resolution in ping6
  • Loading branch information
benpicco committed Feb 24, 2020
2 parents 77d6eaa + fa44472 commit d044800
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
8 changes: 4 additions & 4 deletions sys/include/net/sock/dns.h
Expand Up @@ -57,8 +57,8 @@ typedef struct {
#define SOCK_DNS_PORT (53)
#define SOCK_DNS_RETRIES (2)

#define SOCK_DNS_MAX_NAME_LEN (64U) /* we're in embedded context. */
#define SOCK_DNS_QUERYBUF_LEN (sizeof(sock_dns_hdr_t) + 4 + SOCK_DNS_MAX_NAME_LEN)
#define SOCK_DNS_BUF_LEN (128) /* we're in embedded context. */
#define SOCK_DNS_MAX_NAME_LEN (SOCK_DNS_BUF_LEN - sizeof(sock_dns_hdr_t) - 4)
/** @} */

/**
Expand All @@ -80,8 +80,8 @@ typedef struct {
* @param[out] addr_out buffer to write result into
* @param[in] family Either AF_INET, AF_INET6 or AF_UNSPEC
*
* @return 0 on success
* @return !=0 otherwise
* @return the size of the resolved address on success
* @return < 0 otherwise
*/
int sock_dns_query(const char *domain_name, void *addr_out, int family);

Expand Down
66 changes: 34 additions & 32 deletions sys/net/application_layer/dns/dns.c
Expand Up @@ -157,7 +157,7 @@ static int _parse_dns_reply(uint8_t *buf, size_t len, void* addr_out, int family
return -EBADMSG;
}
bufpos += RR_RDLENGTH_LENGTH;
if ((bufpos + addrlen) >= buflim) {
if ((bufpos + addrlen) > buflim) {
return -EBADMSG;
}

Expand All @@ -170,8 +170,7 @@ static int _parse_dns_reply(uint8_t *buf, size_t len, void* addr_out, int family

int sock_dns_query(const char *domain_name, void *addr_out, int family)
{
uint8_t buf[SOCK_DNS_QUERYBUF_LEN];
uint8_t reply_buf[512];
static uint8_t dns_buf[SOCK_DNS_BUF_LEN];

if (sock_dns_server.port == 0) {
return -ECONNREFUSED;
Expand All @@ -181,49 +180,52 @@ int sock_dns_query(const char *domain_name, void *addr_out, int family)
return -ENOSPC;
}

sock_dns_hdr_t *hdr = (sock_dns_hdr_t*) buf;
memset(hdr, 0, sizeof(*hdr));
hdr->id = 0; /* random? */
hdr->flags = htons(0x0120);
hdr->qdcount = htons(1 + (family == AF_UNSPEC));

uint8_t *bufpos = buf + sizeof(*hdr);

unsigned _name_ptr;
if ((family == AF_INET6) || (family == AF_UNSPEC)) {
_name_ptr = (bufpos - buf);
bufpos += _enc_domain_name(bufpos, domain_name);
bufpos += _put_short(bufpos, htons(DNS_TYPE_AAAA));
bufpos += _put_short(bufpos, htons(DNS_CLASS_IN));
}

if ((family == AF_INET) || (family == AF_UNSPEC)) {
if (family == AF_UNSPEC) {
bufpos += _put_short(bufpos, htons((0xc000) | (_name_ptr)));
}
else {
bufpos += _enc_domain_name(bufpos, domain_name);
}
bufpos += _put_short(bufpos, htons(DNS_TYPE_A));
bufpos += _put_short(bufpos, htons(DNS_CLASS_IN));
}

sock_udp_t sock_dns;

ssize_t res = sock_udp_create(&sock_dns, NULL, &sock_dns_server, 0);
if (res) {
goto out;
}

uint16_t id = 0; /* random? */
for (int i = 0; i < SOCK_DNS_RETRIES; i++) {
uint8_t *buf = dns_buf;

sock_dns_hdr_t *hdr = (sock_dns_hdr_t*) buf;
memset(hdr, 0, sizeof(*hdr));
hdr->id = id;
hdr->flags = htons(0x0120);
hdr->qdcount = htons(1 + (family == AF_UNSPEC));

uint8_t *bufpos = buf + sizeof(*hdr);

unsigned _name_ptr;
if ((family == AF_INET6) || (family == AF_UNSPEC)) {
_name_ptr = (bufpos - buf);
bufpos += _enc_domain_name(bufpos, domain_name);
bufpos += _put_short(bufpos, htons(DNS_TYPE_AAAA));
bufpos += _put_short(bufpos, htons(DNS_CLASS_IN));
}

if ((family == AF_INET) || (family == AF_UNSPEC)) {
if (family == AF_UNSPEC) {
bufpos += _put_short(bufpos, htons((0xc000) | (_name_ptr)));
}
else {
bufpos += _enc_domain_name(bufpos, domain_name);
}
bufpos += _put_short(bufpos, htons(DNS_TYPE_A));
bufpos += _put_short(bufpos, htons(DNS_CLASS_IN));
}

res = sock_udp_send(&sock_dns, buf, (bufpos-buf), NULL);
if (res <= 0) {
continue;
}
res = sock_udp_recv(&sock_dns, reply_buf, sizeof(reply_buf), 1000000LU, NULL);
res = sock_udp_recv(&sock_dns, dns_buf, sizeof(dns_buf), 1000000LU, NULL);
if (res > 0) {
if (res > (int)DNS_MIN_REPLY_LEN) {
if ((res = _parse_dns_reply(reply_buf, res, addr_out,
if ((res = _parse_dns_reply(dns_buf, res, addr_out,
family)) > 0) {
goto out;
}
Expand Down
4 changes: 3 additions & 1 deletion sys/shell/commands/sc_gnrc_icmpv6_echo.c
Expand Up @@ -177,7 +177,9 @@ static int _configure(int argc, char **argv, _ping_data_t *data)

data->hostname = arg;
#ifdef MODULE_SOCK_DNS
if (sock_dns_query(data->hostname, &data->host, AF_INET6) == 0) {
if (strchr(data->hostname, ':') == NULL &&
sock_dns_query(data->hostname, &data->host, AF_INET6) > 0) {
res = 0;
continue;
}
#endif
Expand Down

0 comments on commit d044800

Please sign in to comment.