From 8ad5e44cbaea616885bfc5cb032d911089383fe5 Mon Sep 17 00:00:00 2001 From: Martine Lenders <mail@martine-lenders.eu> Date: Wed, 9 Jan 2019 22:14:08 +0100 Subject: [PATCH] sock_dns: remove some magic numbers --- sys/include/net/dns.h | 42 +++++++++++++++++++++++++++++ sys/net/application_layer/dns/dns.c | 14 +++++----- 2 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 sys/include/net/dns.h diff --git a/sys/include/net/dns.h b/sys/include/net/dns.h new file mode 100644 index 0000000000..09ee148e16 --- /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 8bda172367..55a1f63e54 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; } -- GitLab