sock_dns: remove some magic numbers

This commit is contained in:
Martine Lenders 2019-01-09 22:14:08 +01:00
parent 2840b3825e
commit 8ad5e44cba
2 changed files with 50 additions and 6 deletions

42
sys/include/net/dns.h Normal file
View File

@ -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 */
/** @} */

View File

@ -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;
}