diff --git a/sys/include/net/dns.h b/sys/include/net/dns.h new file mode 100644 index 0000000000000000000000000000000000000000..09ee148e16f072c5d5cdb7cd5c870e9d3b658c04 --- /dev/null +++ b/sys/include/net/dns.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2019 Freie Universität Berlin + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @defgroup net_dns DNS defines + * @ingroup net + * @brief Generic DNS values + * @{ + * + * @file + * @brief Generic DNS values + * + * @author Martine Lenders <m.lenders@fu-berlin.de> + */ +#ifndef NET_DNS_H +#define NET_DNS_H + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @name Field lengths + * @{ + */ +#define RR_TYPE_LENGTH (2U) +#define RR_CLASS_LENGTH (2U) +#define RR_TTL_LENGTH (4U) +#define RR_RDLENGTH_LENGTH (2U) +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* NET_DNS_H */ +/** @} */ diff --git a/sys/net/application_layer/dns/dns.c b/sys/net/application_layer/dns/dns.c index 8bda172367539eec1b894e524e125c2443a51dc9..55a1f63e541f15f92b2ff5b28d494e53cb23addc 100644 --- a/sys/net/application_layer/dns/dns.c +++ b/sys/net/application_layer/dns/dns.c @@ -19,6 +19,7 @@ #include <string.h> #include <stdio.h> +#include "net/dns.h" #include "net/sock/udp.h" #include "net/sock/dns.h" @@ -114,7 +115,8 @@ static int _parse_dns_reply(uint8_t *buf, size_t len, void* addr_out, int family return tmp; } bufpos += tmp; - bufpos += 4; /* skip type and class of query */ + /* skip type and class of query */ + bufpos += (RR_TYPE_LENGTH + RR_CLASS_LENGTH); } for (unsigned n = 0; n < ntohs(hdr->ancount); n++) { @@ -123,14 +125,14 @@ static int _parse_dns_reply(uint8_t *buf, size_t len, void* addr_out, int family return tmp; } bufpos += tmp; - if ((bufpos + 2 + 2 + 4) >= buflim) { + if ((bufpos + RR_TYPE_LENGTH + RR_CLASS_LENGTH + RR_TTL_LENGTH) >= buflim) { return -EBADMSG; } uint16_t _type = ntohs(_get_short(bufpos)); - bufpos += 2; + bufpos += RR_TYPE_LENGTH; uint16_t class = ntohs(_get_short(bufpos)); - bufpos += 2; - bufpos += 4; /* skip ttl */ + bufpos += RR_CLASS_LENGTH; + bufpos += RR_TTL_LENGTH; /* skip ttl */ unsigned addrlen = ntohs(_get_short(bufpos)); /* skip unwanted answers */ @@ -154,7 +156,7 @@ static int _parse_dns_reply(uint8_t *buf, size_t len, void* addr_out, int family (family == AF_UNSPEC))) { return -EBADMSG; } - bufpos += 2; + bufpos += RR_RDLENGTH_LENGTH; if ((bufpos + addrlen) >= buflim) { return -EBADMSG; }