Skip to content

Commit 8ad5e44

Browse files
committed
sock_dns: remove some magic numbers
1 parent 2840b38 commit 8ad5e44

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

sys/include/net/dns.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (C) 2019 Freie Universität Berlin
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser
5+
* General Public License v2.1. See the file LICENSE in the top level
6+
* directory for more details.
7+
*/
8+
9+
/**
10+
* @defgroup net_dns DNS defines
11+
* @ingroup net
12+
* @brief Generic DNS values
13+
* @{
14+
*
15+
* @file
16+
* @brief Generic DNS values
17+
*
18+
* @author Martine Lenders <[email protected]>
19+
*/
20+
#ifndef NET_DNS_H
21+
#define NET_DNS_H
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
/**
28+
* @name Field lengths
29+
* @{
30+
*/
31+
#define RR_TYPE_LENGTH (2U)
32+
#define RR_CLASS_LENGTH (2U)
33+
#define RR_TTL_LENGTH (4U)
34+
#define RR_RDLENGTH_LENGTH (2U)
35+
/** @} */
36+
37+
#ifdef __cplusplus
38+
}
39+
#endif
40+
41+
#endif /* NET_DNS_H */
42+
/** @} */

sys/net/application_layer/dns/dns.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <string.h>
2020
#include <stdio.h>
2121

22+
#include "net/dns.h"
2223
#include "net/sock/udp.h"
2324
#include "net/sock/dns.h"
2425

@@ -114,7 +115,8 @@ static int _parse_dns_reply(uint8_t *buf, size_t len, void* addr_out, int family
114115
return tmp;
115116
}
116117
bufpos += tmp;
117-
bufpos += 4; /* skip type and class of query */
118+
/* skip type and class of query */
119+
bufpos += (RR_TYPE_LENGTH + RR_CLASS_LENGTH);
118120
}
119121

120122
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
123125
return tmp;
124126
}
125127
bufpos += tmp;
126-
if ((bufpos + 2 + 2 + 4) >= buflim) {
128+
if ((bufpos + RR_TYPE_LENGTH + RR_CLASS_LENGTH + RR_TTL_LENGTH) >= buflim) {
127129
return -EBADMSG;
128130
}
129131
uint16_t _type = ntohs(_get_short(bufpos));
130-
bufpos += 2;
132+
bufpos += RR_TYPE_LENGTH;
131133
uint16_t class = ntohs(_get_short(bufpos));
132-
bufpos += 2;
133-
bufpos += 4; /* skip ttl */
134+
bufpos += RR_CLASS_LENGTH;
135+
bufpos += RR_TTL_LENGTH; /* skip ttl */
134136

135137
unsigned addrlen = ntohs(_get_short(bufpos));
136138
/* skip unwanted answers */
@@ -154,7 +156,7 @@ static int _parse_dns_reply(uint8_t *buf, size_t len, void* addr_out, int family
154156
(family == AF_UNSPEC))) {
155157
return -EBADMSG;
156158
}
157-
bufpos += 2;
159+
bufpos += RR_RDLENGTH_LENGTH;
158160
if ((bufpos + addrlen) >= buflim) {
159161
return -EBADMSG;
160162
}

0 commit comments

Comments
 (0)