Skip to content

Commit 3b1e136

Browse files
geky0xc0170
authored andcommitted
Added IPv6 support to DNS query
To avoid complications with unexpected IP versions, the wanted IP version was added as an extra argument to DNS query functions (defaults to NSAPI_IPv4). Internal API changes: m nsapi_dns_query m nsapi_dns_query_multiple Interestingly, though multiple questions can be encoded in a single DNS packet, few, if any, nameservers respect any but the first question. This makes support for coelescing DNS queries useless.
1 parent ab9ea1f commit 3b1e136

File tree

2 files changed

+104
-40
lines changed

2 files changed

+104
-40
lines changed

features/net/network-socket/nsapi_dns.cpp

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static uint16_t dns_scan_word(const uint8_t **p)
6565
}
6666

6767

68-
static void dns_append_question(uint8_t **p, const char *host)
68+
static void dns_append_question(uint8_t **p, const char *host, nsapi_version_t version)
6969
{
7070
// fill the header
7171
dns_append_word(p, 1); // id = 1
@@ -85,8 +85,12 @@ static void dns_append_question(uint8_t **p, const char *host)
8585
dns_append_byte(p, 0);
8686

8787
// fill out question footer
88-
dns_append_word(p, 1); // qtype = 1
89-
dns_append_word(p, 1); // qclass = 1
88+
if (version == NSAPI_IPv4) {
89+
dns_append_word(p, 1); // qtype = ipv4
90+
} else {
91+
dns_append_word(p, 28); // qtype = ipv6
92+
}
93+
dns_append_word(p, 1); // qclass = 1
9094
}
9195

9296
static int dns_scan_response(const uint8_t **p, nsapi_addr_t *addr, unsigned addr_count)
@@ -144,26 +148,37 @@ static int dns_scan_response(const uint8_t **p, nsapi_addr_t *addr, unsigned add
144148
*p += 4; // ttl
145149
uint16_t rdlength = dns_scan_word(p); // rdlength
146150

147-
// verify response is an A record
148-
if (!(rtype == 1 && rclass == 1 && rdlength == NSAPI_IPv4_BYTES)) {
149-
*p += rdlength;
150-
continue;
151-
}
151+
if (rtype == 1 && rclass == 1 && rdlength == NSAPI_IPv4_BYTES) {
152+
// accept A record
153+
addr->version = NSAPI_IPv4;
154+
for (int i = 0; i < NSAPI_IPv4_BYTES; i++) {
155+
addr->bytes[i] = dns_scan_byte(p);
156+
}
152157

153-
addr->version = NSAPI_IPv4;
154-
for (int i = 0; i < NSAPI_IPv4_BYTES; i++) {
155-
addr->bytes[i] = dns_scan_byte(p);
156-
}
158+
addr += 1;
159+
count += 1;
160+
} else if (rtype == 28 && rclass == 1 && rdlength == NSAPI_IPv6_BYTES) {
161+
// accept AAAA record
162+
addr->version = NSAPI_IPv6;
163+
for (int i = 0; i < NSAPI_IPv6_BYTES; i++) {
164+
addr->bytes[i] = dns_scan_byte(p);
165+
}
157166

158-
addr += 1;
159-
count += 1;
167+
addr += 1;
168+
count += 1;
169+
} else {
170+
// skip unrecognized records
171+
*p += rdlength;
172+
}
160173
}
161174

162175
return count;
163176
}
164177

165178
// core query function
166-
int nsapi_dns_query_multiple(NetworkStack *stack, nsapi_addr_t *addr, unsigned addr_count, const char *host)
179+
int nsapi_dns_query_multiple(NetworkStack *stack,
180+
nsapi_addr_t *addr, unsigned addr_count,
181+
const char *host, nsapi_version_t version)
167182
{
168183
// check for valid host name
169184
int host_len = host ? strlen(host) : 0;
@@ -192,7 +207,7 @@ int nsapi_dns_query_multiple(NetworkStack *stack, nsapi_addr_t *addr, unsigned a
192207
for (unsigned i = 0; i < DNS_SERVERS_LENGTH; i++) {
193208
// send the question
194209
uint8_t *p = packet;
195-
dns_append_question(&p, host);
210+
dns_append_question(&p, host, version);
196211

197212
err = socket.sendto(SocketAddress(DNS_SERVERS[i], 53), packet, DNS_BUFFER_SIZE);
198213
if (err == NSAPI_ERROR_WOULD_BLOCK) {
@@ -234,15 +249,28 @@ int nsapi_dns_query_multiple(NetworkStack *stack, nsapi_addr_t *addr, unsigned a
234249

235250
// convenience functions for other forms of queries
236251
NSAPI_C_LINKAGE
237-
int nsapi_dns_query_multiple(nsapi_stack_t *stack, nsapi_addr_t *addr, unsigned addr_count, const char *host)
252+
int nsapi_dns_query_multiple(nsapi_stack_t *stack,
253+
nsapi_addr_t *addr, unsigned addr_count,
254+
const char *host, nsapi_version_t version)
255+
{
256+
NetworkStack *nstack = nsapi_create_stack(stack);
257+
return nsapi_dns_query_multiple(nstack, addr, addr_count, host, version);
258+
}
259+
260+
int nsapi_dns_query_multiple(nsapi_stack_t *stack,
261+
nsapi_addr_t *addr, unsigned addr_count,
262+
const char *host)
238263
{
239-
return nsapi_dns_query_multiple(nsapi_create_stack(stack), addr, addr_count, host);
264+
NetworkStack *nstack = nsapi_create_stack(stack);
265+
return nsapi_dns_query_multiple(nstack, addr, addr_count, host, NSAPI_IPv4);
240266
}
241267

242-
int nsapi_dns_query_multiple(NetworkStack *stack, SocketAddress *addresses, unsigned addr_count, const char *host)
268+
int nsapi_dns_query_multiple(NetworkStack *stack,
269+
SocketAddress *addresses, unsigned addr_count,
270+
const char *host, nsapi_version_t version)
243271
{
244272
nsapi_addr_t *addrs = new nsapi_addr_t[addr_count];
245-
int result = nsapi_dns_query_multiple(stack, addrs, addr_count, host);
273+
int result = nsapi_dns_query_multiple(stack, addrs, addr_count, host, version);
246274

247275
if (result > 0) {
248276
for (int i = 0; i < result; i++) {
@@ -255,22 +283,34 @@ int nsapi_dns_query_multiple(NetworkStack *stack, SocketAddress *addresses, unsi
255283
}
256284

257285
NSAPI_C_LINKAGE
258-
int nsapi_dns_query(nsapi_stack_t *stack, nsapi_addr_t *addr, const char *host)
286+
int nsapi_dns_query(nsapi_stack_t *stack,
287+
nsapi_addr_t *addr, const char *host, nsapi_version_t version)
288+
{
289+
NetworkStack *nstack = nsapi_create_stack(stack);
290+
int result = nsapi_dns_query_multiple(nstack, addr, 1, host, version);
291+
return (result > 0) ? 0 : result;
292+
}
293+
294+
int nsapi_dns_query(nsapi_stack_t *stack,
295+
nsapi_addr_t *addr, const char *host)
259296
{
260-
int result = nsapi_dns_query_multiple(nsapi_create_stack(stack), addr, 1, host);
297+
NetworkStack *nstack = nsapi_create_stack(stack);
298+
int result = nsapi_dns_query_multiple(nstack, addr, 1, host, NSAPI_IPv4);
261299
return (result > 0) ? 0 : result;
262300
}
263301

264-
int nsapi_dns_query(NetworkStack *stack, nsapi_addr_t *addr, const char *host)
302+
int nsapi_dns_query(NetworkStack *stack,
303+
nsapi_addr_t *addr, const char *host, nsapi_version_t version)
265304
{
266-
int result = nsapi_dns_query_multiple(stack, addr, 1, host);
305+
int result = nsapi_dns_query_multiple(stack, addr, 1, host, version);
267306
return (result > 0) ? 0 : result;
268307
}
269308

270-
int nsapi_dns_query(NetworkStack *stack, SocketAddress *address, const char *host)
309+
int nsapi_dns_query(NetworkStack *stack,
310+
SocketAddress *address, const char *host, nsapi_version_t version)
271311
{
272312
nsapi_addr_t addr;
273-
int result = nsapi_dns_query_multiple(stack, &addr, 1, host);
313+
int result = nsapi_dns_query_multiple(stack, &addr, 1, host, version);
274314
address->set_addr(addr);
275315
return (result > 0) ? 0 : result;
276316
}

features/net/network-socket/nsapi_dns.h

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,34 @@
2626
* @param stack Network stack as target for DNS query
2727
* @param addr Destination for the host address
2828
* @param host Hostname to resolve
29+
* @param version IP version to resolve (defaults to NSAPI_IPv4)
2930
* @return 0 on success, negative error code on failure
3031
* NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
3132
*/
3233
NSAPI_C_LINKAGE
33-
int nsapi_dns_query(nsapi_stack_t *stack, nsapi_addr_t *addr, const char *host);
34+
int nsapi_dns_query(nsapi_stack_t *stack, nsapi_addr_t *addr,
35+
const char *host, nsapi_version_t version);
3436

3537
#ifdef __cplusplus
36-
int nsapi_dns_query(NetworkStack *stack, nsapi_addr_t *addr, const char *host);
37-
int nsapi_dns_query(NetworkStack *stack, SocketAddress *addr, const char *host);
38+
int nsapi_dns_query(nsapi_stack_t *stack, nsapi_addr_t *addr,
39+
const char *host);
40+
int nsapi_dns_query(NetworkStack *stack, nsapi_addr_t *addr,
41+
const char *host, nsapi_version_t version = NSAPI_IPv4);
42+
int nsapi_dns_query(NetworkStack *stack, SocketAddress *addr,
43+
const char *host, nsapi_version_t version = NSAPI_IPv4);
3844

3945
template <typename S>
40-
int nsapi_dns_query(S *stack, nsapi_addr_t *addr, const char *host)
46+
int nsapi_dns_query(S *stack, nsapi_addr_t *addr,
47+
const char *host, nsapi_version_t version = NSAPI_IPv4)
4148
{
42-
return nsapi_dns_query(nsapi_create_stack(stack), addr, host);
49+
return nsapi_dns_query(nsapi_create_stack(stack), addr, host, version);
4350
}
4451

4552
template <typename S>
46-
int nsapi_dns_query(S *stack, SocketAddress *addr, const char *host)
53+
int nsapi_dns_query(S *stack, SocketAddress *addr,
54+
const char *host, nsapi_version_t version = NSAPI_IPv4)
4755
{
48-
return nsapi_dns_query(nsapi_create_stack(stack), addr, host);
56+
return nsapi_dns_query(nsapi_create_stack(stack), addr, host, version);
4957
}
5058
#endif
5159

@@ -56,26 +64,42 @@ int nsapi_dns_query(S *stack, SocketAddress *addr, const char *host)
5664
* @param addr Array for the host addresses
5765
* @param addr_count Number of addresses allocated in the array
5866
* @param host Hostname to resolve
67+
* @param version IP version to resolve (defaults to NSAPI_IPv4)
5968
* @return Number of addresses found on success, negative error code on failure
6069
* NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
6170
*/
6271
NSAPI_C_LINKAGE
63-
int nsapi_dns_query_multiple(nsapi_stack_t *stack, nsapi_addr_t *addr, unsigned addr_count, const char *host);
72+
int nsapi_dns_query_multiple(nsapi_stack_t *stack,
73+
nsapi_addr_t *addr, unsigned addr_count,
74+
const char *host, nsapi_version_t version);
6475

6576
#ifdef __cplusplus
66-
int nsapi_dns_query_multiple(NetworkStack *stack, nsapi_addr_t *addr, unsigned addr_count, const char *host);
67-
int nsapi_dns_query_multiple(NetworkStack *stack, SocketAddress *addr, unsigned addr_count, const char *host);
77+
int nsapi_dns_query_multiple(nsapi_stack_t *stack,
78+
nsapi_addr_t *addr, unsigned addr_count,
79+
const char *host);
80+
int nsapi_dns_query_multiple(NetworkStack *stack,
81+
nsapi_addr_t *addr, unsigned addr_count,
82+
const char *host, nsapi_version_t version = NSAPI_IPv4);
83+
int nsapi_dns_query_multiple(NetworkStack *stack,
84+
SocketAddress *addr, unsigned addr_count,
85+
const char *host, nsapi_version_t version = NSAPI_IPv4);
6886

6987
template <typename S>
70-
int nsapi_dns_query_multiple(S *stack, nsapi_addr_t *addr, unsigned addr_count, const char *host)
88+
int nsapi_dns_query_multiple(S *stack,
89+
nsapi_addr_t *addr, unsigned addr_count,
90+
const char *host, nsapi_version_t version = NSAPI_IPv4)
7191
{
72-
return nsapi_dns_query_multiple(nsapi_create_stack(stack), addr, addr_count, host);
92+
return nsapi_dns_query_multiple(nsapi_create_stack(stack),
93+
addr, addr_count, host, version);
7394
}
7495

7596
template <typename S>
76-
int nsapi_dns_query_multiple(S *stack, SocketAddress *addr, unsigned addr_count, const char *host)
97+
int nsapi_dns_query_multiple(S *stack,
98+
SocketAddress *addr, unsigned addr_count,
99+
const char *host, nsapi_version_t version = NSAPI_IPv4)
77100
{
78-
return nsapi_dns_query_multiple(nsapi_create_stack(stack), addr, addr_count, host);
101+
return nsapi_dns_query_multiple(nsapi_create_stack(stack),
102+
addr, addr_count, host, version);
79103
}
80104
#endif
81105

0 commit comments

Comments
 (0)