diff --git a/sys/include/net/sock/dns.h b/sys/include/net/sock/dns.h index a1b39a4439..8bf2a1da86 100644 --- a/sys/include/net/sock/dns.h +++ b/sys/include/net/sock/dns.h @@ -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) /** @} */ /** diff --git a/sys/net/application_layer/dns/dns.c b/sys/net/application_layer/dns/dns.c index 990aaf50a6..f42ce3360f 100644 --- a/sys/net/application_layer/dns/dns.c +++ b/sys/net/application_layer/dns/dns.c @@ -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; @@ -181,33 +180,6 @@ 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); @@ -215,15 +187,45 @@ int sock_dns_query(const char *domain_name, void *addr_out, int family) 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; }