@@ -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
9296static 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
236251NSAPI_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
257285NSAPI_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}
0 commit comments