17
17
#include " nsapi_dns.h"
18
18
#include " network-socket/UDPSocket.h"
19
19
#include < string.h>
20
+ #include < stdlib.h>
20
21
21
22
22
23
#define DNS_BUFFER_SIZE 256
@@ -88,7 +89,7 @@ static void dns_append_question(uint8_t **p, const char *host)
88
89
dns_append_word (p, 1 ); // qclass = 1
89
90
}
90
91
91
- static bool dns_scan_response (const uint8_t **p, nsapi_addr_t *addr)
92
+ static int dns_scan_response (const uint8_t **p, nsapi_addr_t *addr, unsigned addr_count )
92
93
{
93
94
// scan header
94
95
uint16_t id = dns_scan_word (p);
@@ -104,7 +105,7 @@ static bool dns_scan_response(const uint8_t **p, nsapi_addr_t *addr)
104
105
105
106
// verify header is response to query
106
107
if (!(id == 1 && qr && opcode == 0 && rcode == 0 )) {
107
- return false ;
108
+ return 0 ;
108
109
}
109
110
110
111
// skip questions
@@ -123,7 +124,9 @@ static bool dns_scan_response(const uint8_t **p, nsapi_addr_t *addr)
123
124
}
124
125
125
126
// scan each response
126
- for (int i = 0 ; i < ancount; i++) {
127
+ unsigned count = 0 ;
128
+
129
+ for (int i = 0 ; i < ancount && count < addr_count; i++) {
127
130
while (true ) {
128
131
uint8_t len = dns_scan_byte (p);
129
132
if (len == 0 ) {
@@ -152,14 +155,15 @@ static bool dns_scan_response(const uint8_t **p, nsapi_addr_t *addr)
152
155
addr->bytes [i] = dns_scan_byte (p);
153
156
}
154
157
155
- return true ;
158
+ addr += 1 ;
159
+ count += 1 ;
156
160
}
157
161
158
- return false ;
162
+ return count ;
159
163
}
160
164
161
-
162
- int nsapi_dns_query (NetworkStack *stack, nsapi_addr_t *addr, const char *host)
165
+ // core query function
166
+ int nsapi_dns_query_multiple (NetworkStack *stack, nsapi_addr_t *addr, unsigned addr_count , const char *host)
163
167
{
164
168
// check for valid host name
165
169
int host_len = host ? strlen (host) : 0 ;
@@ -208,9 +212,9 @@ int nsapi_dns_query(NetworkStack *stack, nsapi_addr_t *addr, const char *host)
208
212
}
209
213
210
214
p = packet;
211
- bool found = dns_scan_response ((const uint8_t **)&p, addr);
215
+ int found = dns_scan_response ((const uint8_t **)&p, addr, addr_count );
212
216
if (found) {
213
- result = 0 ;
217
+ result = found ;
214
218
break ;
215
219
}
216
220
}
@@ -228,16 +232,45 @@ int nsapi_dns_query(NetworkStack *stack, nsapi_addr_t *addr, const char *host)
228
232
return result;
229
233
}
230
234
235
+ // convenience functions for other forms of queries
236
+ NSAPI_C_LINKAGE
237
+ int nsapi_dns_query_multiple (nsapi_stack_t *stack, nsapi_addr_t *addr, unsigned addr_count, const char *host)
238
+ {
239
+ return nsapi_dns_query_multiple (nsapi_create_stack (stack), addr, addr_count, host);
240
+ }
241
+
242
+ int nsapi_dns_query_multiple (NetworkStack *stack, SocketAddress *addresses, unsigned addr_count, const char *host)
243
+ {
244
+ nsapi_addr_t *addrs = new nsapi_addr_t [addr_count];
245
+ int result = nsapi_dns_query_multiple (stack, addrs, addr_count, host);
246
+
247
+ if (result > 0 ) {
248
+ for (int i = 0 ; i < result; i++) {
249
+ addresses[i].set_addr (addrs[i]);
250
+ }
251
+ }
252
+
253
+ delete[] addrs;
254
+ return result;
255
+ }
256
+
231
257
NSAPI_C_LINKAGE
232
258
int nsapi_dns_query (nsapi_stack_t *stack, nsapi_addr_t *addr, const char *host)
233
259
{
234
- return nsapi_dns_query (nsapi_create_stack (stack), addr, host);
260
+ int result = nsapi_dns_query_multiple (nsapi_create_stack (stack), addr, 1 , host);
261
+ return (result > 0 ) ? 0 : result;
262
+ }
263
+
264
+ int nsapi_dns_query (NetworkStack *stack, nsapi_addr_t *addr, const char *host)
265
+ {
266
+ int result = nsapi_dns_query_multiple (stack, addr, 1 , host);
267
+ return (result > 0 ) ? 0 : result;
235
268
}
236
269
237
270
int nsapi_dns_query (NetworkStack *stack, SocketAddress *address, const char *host)
238
271
{
239
272
nsapi_addr_t addr;
240
- int err = nsapi_dns_query (stack, &addr, host);
273
+ int result = nsapi_dns_query_multiple (stack, &addr, 1 , host);
241
274
address->set_addr (addr);
242
- return err ;
275
+ return (result > 0 ) ? 0 : result ;
243
276
}
0 commit comments