1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

sock_dns: add debug output

This commit is contained in:
Benjamin Valentin 2024-09-09 14:37:07 +02:00
parent bc2ad626f3
commit 344d4b80bf

View File

@ -20,6 +20,7 @@
#include <string.h>
#include <arpa/inet.h>
#include <string.h>
#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;
}