mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
sock_dns: use the same buffer for request & reply
Saving RAM is more important than saving a few cycles used by re-creating the request buffer in the error case. Also reduce the size of the buffer to 128 bytes. If we are just requesting the AAAA record it is unlikely for the reply to take up the maximum size of 512 bytes. We were already placing restrictions on the domain name length, those are now actually a bit more relaxed (112 bytes instead of 64)
This commit is contained in:
parent
367f19d390
commit
1de14931b8
@ -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)
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user