diff --git a/sys/net/application_layer/sock_dns/dns.c b/sys/net/application_layer/sock_dns/dns.c index 7ad4898040..b1c048bb44 100644 --- a/sys/net/application_layer/sock_dns/dns.c +++ b/sys/net/application_layer/sock_dns/dns.c @@ -20,6 +20,7 @@ #include #include +#include #include "net/dns.h" #include "net/dns/cache.h" @@ -27,6 +28,9 @@ #include "net/sock/udp.h" #include "net/sock/dns.h" +#define ENABLE_DEBUG 0 +#include "debug.h" + /* min domain name length is 1, so minimum record length is 7 */ #define DNS_MIN_REPLY_LEN (unsigned)(sizeof(dns_hdr_t) + 7) @@ -87,7 +91,7 @@ int sock_dns_query(const char *domain_name, void *addr_out, int family) res = sock_udp_create(&sock_dns, NULL, &sock_dns_server, 0); if (res) { - goto out; + return res; } uint16_t id = 0; @@ -96,25 +100,30 @@ int sock_dns_query(const char *domain_name, void *addr_out, int family) res = sock_udp_send(&sock_dns, dns_buf, buflen, NULL); if (res <= 0) { + DEBUG("sock_dns: can't send: %s\n", strerror(-res)); continue; } res = sock_udp_recv(&sock_dns, dns_buf, sizeof(dns_buf), 1000000LU, NULL); - if (res > 0) { - if (res > (int)DNS_MIN_REPLY_LEN) { - uint32_t ttl; - if ((res = dns_msg_parse_reply(dns_buf, res, family, - addr_out, &ttl)) > 0) { - dns_cache_add(domain_name, addr_out, res, ttl); - goto out; - } - } - else { - res = -EBADMSG; - } + if (res < 0) { + DEBUG("sock_dns: can't receive: %s\n", strerror(-res)); + continue; + } + if (res < (int)DNS_MIN_REPLY_LEN) { + DEBUG("sock_dns: reply too small (%d byte)\n", (int)res); + res = -EBADMSG; + continue; + } + + uint32_t ttl; + if ((res = dns_msg_parse_reply(dns_buf, res, family, + addr_out, &ttl)) > 0) { + dns_cache_add(domain_name, addr_out, res, ttl); + break; + } else { + DEBUG("sock_dns: can't parse response\n"); } } -out: sock_udp_close(&sock_dns); return res; }