@@ -65,7 +65,7 @@ static uint16_t dns_scan_word(const uint8_t **p)
65
65
}
66
66
67
67
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 )
69
69
{
70
70
// fill the header
71
71
dns_append_word (p, 1 ); // id = 1
@@ -85,8 +85,12 @@ static void dns_append_question(uint8_t **p, const char *host)
85
85
dns_append_byte (p, 0 );
86
86
87
87
// 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
90
94
}
91
95
92
96
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
144
148
*p += 4 ; // ttl
145
149
uint16_t rdlength = dns_scan_word (p); // rdlength
146
150
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
+ }
152
157
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
+ }
157
166
158
- addr += 1 ;
159
- count += 1 ;
167
+ addr += 1 ;
168
+ count += 1 ;
169
+ } else {
170
+ // skip unrecognized records
171
+ *p += rdlength;
172
+ }
160
173
}
161
174
162
175
return count;
163
176
}
164
177
165
178
// 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)
167
182
{
168
183
// check for valid host name
169
184
int host_len = host ? strlen (host) : 0 ;
@@ -192,7 +207,7 @@ int nsapi_dns_query_multiple(NetworkStack *stack, nsapi_addr_t *addr, unsigned a
192
207
for (unsigned i = 0 ; i < DNS_SERVERS_LENGTH; i++) {
193
208
// send the question
194
209
uint8_t *p = packet;
195
- dns_append_question (&p, host);
210
+ dns_append_question (&p, host, version );
196
211
197
212
err = socket.sendto (SocketAddress (DNS_SERVERS[i], 53 ), packet, DNS_BUFFER_SIZE);
198
213
if (err == NSAPI_ERROR_WOULD_BLOCK) {
@@ -234,15 +249,28 @@ int nsapi_dns_query_multiple(NetworkStack *stack, nsapi_addr_t *addr, unsigned a
234
249
235
250
// convenience functions for other forms of queries
236
251
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)
238
263
{
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);
240
266
}
241
267
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)
243
271
{
244
272
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 );
246
274
247
275
if (result > 0 ) {
248
276
for (int i = 0 ; i < result; i++) {
@@ -255,22 +283,34 @@ int nsapi_dns_query_multiple(NetworkStack *stack, SocketAddress *addresses, unsi
255
283
}
256
284
257
285
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)
259
296
{
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);
261
299
return (result > 0 ) ? 0 : result;
262
300
}
263
301
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)
265
304
{
266
- int result = nsapi_dns_query_multiple (stack, addr, 1 , host);
305
+ int result = nsapi_dns_query_multiple (stack, addr, 1 , host, version );
267
306
return (result > 0 ) ? 0 : result;
268
307
}
269
308
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)
271
311
{
272
312
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 );
274
314
address->set_addr (addr);
275
315
return (result > 0 ) ? 0 : result;
276
316
}
0 commit comments