Skip to content

Commit

Permalink
sock_dns: add debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Sep 9, 2024
1 parent d88d34c commit 352c37d
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions sys/net/application_layer/sock_dns/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@
#include <string.h>

#include <arpa/inet.h>
#include <string.h>

#include "net/dns.h"
#include "net/dns/cache.h"
#include "net/dns/msg.h"
#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)

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

0 comments on commit 352c37d

Please sign in to comment.