Pinned to some recent date
features/netsocket/nsapi_dns.cpp@0:fb7af294d5d9, 2016-11-17 (annotated)
- Committer:
- Simon Cooksey
- Date:
- Thu Nov 17 16:43:53 2016 +0000
- Revision:
- 0:fb7af294d5d9
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Simon Cooksey |
0:fb7af294d5d9 | 1 | /* nsapi_dns.cpp |
Simon Cooksey |
0:fb7af294d5d9 | 2 | * Original work Copyright (c) 2013 Henry Leinen (henry[dot]leinen [at] online [dot] de) |
Simon Cooksey |
0:fb7af294d5d9 | 3 | * Modified work Copyright (c) 2015 ARM Limited |
Simon Cooksey |
0:fb7af294d5d9 | 4 | * |
Simon Cooksey |
0:fb7af294d5d9 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Simon Cooksey |
0:fb7af294d5d9 | 6 | * you may not use this file except in compliance with the License. |
Simon Cooksey |
0:fb7af294d5d9 | 7 | * You may obtain a copy of the License at |
Simon Cooksey |
0:fb7af294d5d9 | 8 | * |
Simon Cooksey |
0:fb7af294d5d9 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Simon Cooksey |
0:fb7af294d5d9 | 10 | * |
Simon Cooksey |
0:fb7af294d5d9 | 11 | * Unless required by applicable law or agreed to in writing, software |
Simon Cooksey |
0:fb7af294d5d9 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
Simon Cooksey |
0:fb7af294d5d9 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Simon Cooksey |
0:fb7af294d5d9 | 14 | * See the License for the specific language governing permissions and |
Simon Cooksey |
0:fb7af294d5d9 | 15 | * limitations under the License. |
Simon Cooksey |
0:fb7af294d5d9 | 16 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 17 | #include "nsapi_dns.h" |
Simon Cooksey |
0:fb7af294d5d9 | 18 | #include "netsocket/UDPSocket.h" |
Simon Cooksey |
0:fb7af294d5d9 | 19 | #include <string.h> |
Simon Cooksey |
0:fb7af294d5d9 | 20 | #include <stdlib.h> |
Simon Cooksey |
0:fb7af294d5d9 | 21 | #include <stdio.h> |
Simon Cooksey |
0:fb7af294d5d9 | 22 | |
Simon Cooksey |
0:fb7af294d5d9 | 23 | #define CLASS_IN 1 |
Simon Cooksey |
0:fb7af294d5d9 | 24 | |
Simon Cooksey |
0:fb7af294d5d9 | 25 | #define RR_A 1 |
Simon Cooksey |
0:fb7af294d5d9 | 26 | #define RR_AAAA 28 |
Simon Cooksey |
0:fb7af294d5d9 | 27 | |
Simon Cooksey |
0:fb7af294d5d9 | 28 | // DNS options |
Simon Cooksey |
0:fb7af294d5d9 | 29 | #define DNS_BUFFER_SIZE 512 |
Simon Cooksey |
0:fb7af294d5d9 | 30 | #define DNS_TIMEOUT 5000 |
Simon Cooksey |
0:fb7af294d5d9 | 31 | #define DNS_SERVERS_SIZE 5 |
Simon Cooksey |
0:fb7af294d5d9 | 32 | |
Simon Cooksey |
0:fb7af294d5d9 | 33 | nsapi_addr_t dns_servers[DNS_SERVERS_SIZE] = { |
Simon Cooksey |
0:fb7af294d5d9 | 34 | {NSAPI_IPv4, {8, 8, 8, 8}}, |
Simon Cooksey |
0:fb7af294d5d9 | 35 | {NSAPI_IPv4, {209, 244, 0, 3}}, |
Simon Cooksey |
0:fb7af294d5d9 | 36 | {NSAPI_IPv4, {84, 200, 69, 80}}, |
Simon Cooksey |
0:fb7af294d5d9 | 37 | {NSAPI_IPv4, {8, 26, 56, 26}}, |
Simon Cooksey |
0:fb7af294d5d9 | 38 | {NSAPI_IPv4, {208, 67, 222, 222}}, |
Simon Cooksey |
0:fb7af294d5d9 | 39 | }; |
Simon Cooksey |
0:fb7af294d5d9 | 40 | |
Simon Cooksey |
0:fb7af294d5d9 | 41 | // DNS server configuration |
Simon Cooksey |
0:fb7af294d5d9 | 42 | extern "C" int nsapi_dns_add_server(nsapi_addr_t addr) |
Simon Cooksey |
0:fb7af294d5d9 | 43 | { |
Simon Cooksey |
0:fb7af294d5d9 | 44 | memmove(&dns_servers[1], &dns_servers[0], |
Simon Cooksey |
0:fb7af294d5d9 | 45 | (DNS_SERVERS_SIZE-1)*sizeof(nsapi_addr_t)); |
Simon Cooksey |
0:fb7af294d5d9 | 46 | |
Simon Cooksey |
0:fb7af294d5d9 | 47 | dns_servers[0] = addr; |
Simon Cooksey |
0:fb7af294d5d9 | 48 | return 0; |
Simon Cooksey |
0:fb7af294d5d9 | 49 | } |
Simon Cooksey |
0:fb7af294d5d9 | 50 | |
Simon Cooksey |
0:fb7af294d5d9 | 51 | |
Simon Cooksey |
0:fb7af294d5d9 | 52 | // DNS packet parsing |
Simon Cooksey |
0:fb7af294d5d9 | 53 | static void dns_append_byte(uint8_t **p, uint8_t byte) |
Simon Cooksey |
0:fb7af294d5d9 | 54 | { |
Simon Cooksey |
0:fb7af294d5d9 | 55 | *(*p)++ = byte; |
Simon Cooksey |
0:fb7af294d5d9 | 56 | } |
Simon Cooksey |
0:fb7af294d5d9 | 57 | |
Simon Cooksey |
0:fb7af294d5d9 | 58 | static void dns_append_word(uint8_t **p, uint16_t word) |
Simon Cooksey |
0:fb7af294d5d9 | 59 | { |
Simon Cooksey |
0:fb7af294d5d9 | 60 | |
Simon Cooksey |
0:fb7af294d5d9 | 61 | dns_append_byte(p, 0xff & (word >> 8)); |
Simon Cooksey |
0:fb7af294d5d9 | 62 | dns_append_byte(p, 0xff & (word >> 0)); |
Simon Cooksey |
0:fb7af294d5d9 | 63 | } |
Simon Cooksey |
0:fb7af294d5d9 | 64 | |
Simon Cooksey |
0:fb7af294d5d9 | 65 | static void dns_append_name(uint8_t **p, const char *name, uint8_t len) |
Simon Cooksey |
0:fb7af294d5d9 | 66 | { |
Simon Cooksey |
0:fb7af294d5d9 | 67 | dns_append_byte(p, len); |
Simon Cooksey |
0:fb7af294d5d9 | 68 | memcpy(*p, name, len); |
Simon Cooksey |
0:fb7af294d5d9 | 69 | *p += len; |
Simon Cooksey |
0:fb7af294d5d9 | 70 | } |
Simon Cooksey |
0:fb7af294d5d9 | 71 | |
Simon Cooksey |
0:fb7af294d5d9 | 72 | static uint8_t dns_scan_byte(const uint8_t **p) |
Simon Cooksey |
0:fb7af294d5d9 | 73 | { |
Simon Cooksey |
0:fb7af294d5d9 | 74 | return *(*p)++; |
Simon Cooksey |
0:fb7af294d5d9 | 75 | } |
Simon Cooksey |
0:fb7af294d5d9 | 76 | |
Simon Cooksey |
0:fb7af294d5d9 | 77 | static uint16_t dns_scan_word(const uint8_t **p) |
Simon Cooksey |
0:fb7af294d5d9 | 78 | { |
Simon Cooksey |
0:fb7af294d5d9 | 79 | uint16_t a = dns_scan_byte(p); |
Simon Cooksey |
0:fb7af294d5d9 | 80 | uint16_t b = dns_scan_byte(p); |
Simon Cooksey |
0:fb7af294d5d9 | 81 | return (a << 8) | b; |
Simon Cooksey |
0:fb7af294d5d9 | 82 | } |
Simon Cooksey |
0:fb7af294d5d9 | 83 | |
Simon Cooksey |
0:fb7af294d5d9 | 84 | |
Simon Cooksey |
0:fb7af294d5d9 | 85 | static void dns_append_question(uint8_t **p, const char *host, nsapi_version_t version) |
Simon Cooksey |
0:fb7af294d5d9 | 86 | { |
Simon Cooksey |
0:fb7af294d5d9 | 87 | // fill the header |
Simon Cooksey |
0:fb7af294d5d9 | 88 | dns_append_word(p, 1); // id = 1 |
Simon Cooksey |
0:fb7af294d5d9 | 89 | dns_append_word(p, 0x0100); // flags = recursion required |
Simon Cooksey |
0:fb7af294d5d9 | 90 | dns_append_word(p, 1); // qdcount = 1 |
Simon Cooksey |
0:fb7af294d5d9 | 91 | dns_append_word(p, 0); // ancount = 0 |
Simon Cooksey |
0:fb7af294d5d9 | 92 | dns_append_word(p, 0); // nscount = 0 |
Simon Cooksey |
0:fb7af294d5d9 | 93 | dns_append_word(p, 0); // arcount = 0 |
Simon Cooksey |
0:fb7af294d5d9 | 94 | |
Simon Cooksey |
0:fb7af294d5d9 | 95 | // fill out the question names |
Simon Cooksey |
0:fb7af294d5d9 | 96 | while (host[0]) { |
Simon Cooksey |
0:fb7af294d5d9 | 97 | size_t label_len = strcspn(host, "."); |
Simon Cooksey |
0:fb7af294d5d9 | 98 | dns_append_name(p, host, label_len); |
Simon Cooksey |
0:fb7af294d5d9 | 99 | host += label_len + (host[label_len] == '.'); |
Simon Cooksey |
0:fb7af294d5d9 | 100 | } |
Simon Cooksey |
0:fb7af294d5d9 | 101 | |
Simon Cooksey |
0:fb7af294d5d9 | 102 | dns_append_byte(p, 0); |
Simon Cooksey |
0:fb7af294d5d9 | 103 | |
Simon Cooksey |
0:fb7af294d5d9 | 104 | // fill out question footer |
Simon Cooksey |
0:fb7af294d5d9 | 105 | if (version != NSAPI_IPv6) { |
Simon Cooksey |
0:fb7af294d5d9 | 106 | dns_append_word(p, RR_A); // qtype = ipv4 |
Simon Cooksey |
0:fb7af294d5d9 | 107 | } else { |
Simon Cooksey |
0:fb7af294d5d9 | 108 | dns_append_word(p, RR_AAAA); // qtype = ipv6 |
Simon Cooksey |
0:fb7af294d5d9 | 109 | } |
Simon Cooksey |
0:fb7af294d5d9 | 110 | dns_append_word(p, CLASS_IN); |
Simon Cooksey |
0:fb7af294d5d9 | 111 | } |
Simon Cooksey |
0:fb7af294d5d9 | 112 | |
Simon Cooksey |
0:fb7af294d5d9 | 113 | static int dns_scan_response(const uint8_t **p, nsapi_addr_t *addr, unsigned addr_count) |
Simon Cooksey |
0:fb7af294d5d9 | 114 | { |
Simon Cooksey |
0:fb7af294d5d9 | 115 | // scan header |
Simon Cooksey |
0:fb7af294d5d9 | 116 | uint16_t id = dns_scan_word(p); |
Simon Cooksey |
0:fb7af294d5d9 | 117 | uint16_t flags = dns_scan_word(p); |
Simon Cooksey |
0:fb7af294d5d9 | 118 | bool qr = 0x1 & (flags >> 15); |
Simon Cooksey |
0:fb7af294d5d9 | 119 | uint8_t opcode = 0xf & (flags >> 11); |
Simon Cooksey |
0:fb7af294d5d9 | 120 | uint8_t rcode = 0xf & (flags >> 0); |
Simon Cooksey |
0:fb7af294d5d9 | 121 | |
Simon Cooksey |
0:fb7af294d5d9 | 122 | uint16_t qdcount = dns_scan_word(p); // qdcount |
Simon Cooksey |
0:fb7af294d5d9 | 123 | uint16_t ancount = dns_scan_word(p); // ancount |
Simon Cooksey |
0:fb7af294d5d9 | 124 | dns_scan_word(p); // nscount |
Simon Cooksey |
0:fb7af294d5d9 | 125 | dns_scan_word(p); // arcount |
Simon Cooksey |
0:fb7af294d5d9 | 126 | |
Simon Cooksey |
0:fb7af294d5d9 | 127 | // verify header is response to query |
Simon Cooksey |
0:fb7af294d5d9 | 128 | if (!(id == 1 && qr && opcode == 0 && rcode == 0)) { |
Simon Cooksey |
0:fb7af294d5d9 | 129 | return 0; |
Simon Cooksey |
0:fb7af294d5d9 | 130 | } |
Simon Cooksey |
0:fb7af294d5d9 | 131 | |
Simon Cooksey |
0:fb7af294d5d9 | 132 | // skip questions |
Simon Cooksey |
0:fb7af294d5d9 | 133 | for (int i = 0; i < qdcount; i++) { |
Simon Cooksey |
0:fb7af294d5d9 | 134 | while (true) { |
Simon Cooksey |
0:fb7af294d5d9 | 135 | uint8_t len = dns_scan_byte(p); |
Simon Cooksey |
0:fb7af294d5d9 | 136 | if (len == 0) { |
Simon Cooksey |
0:fb7af294d5d9 | 137 | break; |
Simon Cooksey |
0:fb7af294d5d9 | 138 | } |
Simon Cooksey |
0:fb7af294d5d9 | 139 | |
Simon Cooksey |
0:fb7af294d5d9 | 140 | *p += len; |
Simon Cooksey |
0:fb7af294d5d9 | 141 | } |
Simon Cooksey |
0:fb7af294d5d9 | 142 | |
Simon Cooksey |
0:fb7af294d5d9 | 143 | dns_scan_word(p); // qtype |
Simon Cooksey |
0:fb7af294d5d9 | 144 | dns_scan_word(p); // qclass |
Simon Cooksey |
0:fb7af294d5d9 | 145 | } |
Simon Cooksey |
0:fb7af294d5d9 | 146 | |
Simon Cooksey |
0:fb7af294d5d9 | 147 | // scan each response |
Simon Cooksey |
0:fb7af294d5d9 | 148 | unsigned count = 0; |
Simon Cooksey |
0:fb7af294d5d9 | 149 | |
Simon Cooksey |
0:fb7af294d5d9 | 150 | for (int i = 0; i < ancount && count < addr_count; i++) { |
Simon Cooksey |
0:fb7af294d5d9 | 151 | while (true) { |
Simon Cooksey |
0:fb7af294d5d9 | 152 | uint8_t len = dns_scan_byte(p); |
Simon Cooksey |
0:fb7af294d5d9 | 153 | if (len == 0) { |
Simon Cooksey |
0:fb7af294d5d9 | 154 | break; |
Simon Cooksey |
0:fb7af294d5d9 | 155 | } else if (len & 0xc0) { // this is link |
Simon Cooksey |
0:fb7af294d5d9 | 156 | dns_scan_byte(p); |
Simon Cooksey |
0:fb7af294d5d9 | 157 | break; |
Simon Cooksey |
0:fb7af294d5d9 | 158 | } |
Simon Cooksey |
0:fb7af294d5d9 | 159 | |
Simon Cooksey |
0:fb7af294d5d9 | 160 | *p += len; |
Simon Cooksey |
0:fb7af294d5d9 | 161 | } |
Simon Cooksey |
0:fb7af294d5d9 | 162 | |
Simon Cooksey |
0:fb7af294d5d9 | 163 | uint16_t rtype = dns_scan_word(p); // rtype |
Simon Cooksey |
0:fb7af294d5d9 | 164 | uint16_t rclass = dns_scan_word(p); // rclass |
Simon Cooksey |
0:fb7af294d5d9 | 165 | *p += 4; // ttl |
Simon Cooksey |
0:fb7af294d5d9 | 166 | uint16_t rdlength = dns_scan_word(p); // rdlength |
Simon Cooksey |
0:fb7af294d5d9 | 167 | |
Simon Cooksey |
0:fb7af294d5d9 | 168 | if (rtype == RR_A && rclass == CLASS_IN && rdlength == NSAPI_IPv4_BYTES) { |
Simon Cooksey |
0:fb7af294d5d9 | 169 | // accept A record |
Simon Cooksey |
0:fb7af294d5d9 | 170 | addr->version = NSAPI_IPv4; |
Simon Cooksey |
0:fb7af294d5d9 | 171 | for (int i = 0; i < NSAPI_IPv4_BYTES; i++) { |
Simon Cooksey |
0:fb7af294d5d9 | 172 | addr->bytes[i] = dns_scan_byte(p); |
Simon Cooksey |
0:fb7af294d5d9 | 173 | } |
Simon Cooksey |
0:fb7af294d5d9 | 174 | |
Simon Cooksey |
0:fb7af294d5d9 | 175 | addr += 1; |
Simon Cooksey |
0:fb7af294d5d9 | 176 | count += 1; |
Simon Cooksey |
0:fb7af294d5d9 | 177 | } else if (rtype == RR_AAAA && rclass == CLASS_IN && rdlength == NSAPI_IPv6_BYTES) { |
Simon Cooksey |
0:fb7af294d5d9 | 178 | // accept AAAA record |
Simon Cooksey |
0:fb7af294d5d9 | 179 | addr->version = NSAPI_IPv6; |
Simon Cooksey |
0:fb7af294d5d9 | 180 | for (int i = 0; i < NSAPI_IPv6_BYTES; i++) { |
Simon Cooksey |
0:fb7af294d5d9 | 181 | addr->bytes[i] = dns_scan_byte(p); |
Simon Cooksey |
0:fb7af294d5d9 | 182 | } |
Simon Cooksey |
0:fb7af294d5d9 | 183 | |
Simon Cooksey |
0:fb7af294d5d9 | 184 | addr += 1; |
Simon Cooksey |
0:fb7af294d5d9 | 185 | count += 1; |
Simon Cooksey |
0:fb7af294d5d9 | 186 | } else { |
Simon Cooksey |
0:fb7af294d5d9 | 187 | // skip unrecognized records |
Simon Cooksey |
0:fb7af294d5d9 | 188 | *p += rdlength; |
Simon Cooksey |
0:fb7af294d5d9 | 189 | } |
Simon Cooksey |
0:fb7af294d5d9 | 190 | } |
Simon Cooksey |
0:fb7af294d5d9 | 191 | |
Simon Cooksey |
0:fb7af294d5d9 | 192 | return count; |
Simon Cooksey |
0:fb7af294d5d9 | 193 | } |
Simon Cooksey |
0:fb7af294d5d9 | 194 | |
Simon Cooksey |
0:fb7af294d5d9 | 195 | // core query function |
Simon Cooksey |
0:fb7af294d5d9 | 196 | static int nsapi_dns_query_multiple(NetworkStack *stack, const char *host, |
Simon Cooksey |
0:fb7af294d5d9 | 197 | nsapi_addr_t *addr, unsigned addr_count, nsapi_version_t version) |
Simon Cooksey |
0:fb7af294d5d9 | 198 | { |
Simon Cooksey |
0:fb7af294d5d9 | 199 | // check for valid host name |
Simon Cooksey |
0:fb7af294d5d9 | 200 | int host_len = host ? strlen(host) : 0; |
Simon Cooksey |
0:fb7af294d5d9 | 201 | if (host_len > 128 || host_len == 0) { |
Simon Cooksey |
0:fb7af294d5d9 | 202 | return NSAPI_ERROR_PARAMETER; |
Simon Cooksey |
0:fb7af294d5d9 | 203 | } |
Simon Cooksey |
0:fb7af294d5d9 | 204 | |
Simon Cooksey |
0:fb7af294d5d9 | 205 | // create a udp socket |
Simon Cooksey |
0:fb7af294d5d9 | 206 | UDPSocket socket; |
Simon Cooksey |
0:fb7af294d5d9 | 207 | int err = socket.open(stack); |
Simon Cooksey |
0:fb7af294d5d9 | 208 | if (err) { |
Simon Cooksey |
0:fb7af294d5d9 | 209 | return err; |
Simon Cooksey |
0:fb7af294d5d9 | 210 | } |
Simon Cooksey |
0:fb7af294d5d9 | 211 | |
Simon Cooksey |
0:fb7af294d5d9 | 212 | socket.set_timeout(DNS_TIMEOUT); |
Simon Cooksey |
0:fb7af294d5d9 | 213 | |
Simon Cooksey |
0:fb7af294d5d9 | 214 | // create network packet |
Simon Cooksey |
0:fb7af294d5d9 | 215 | uint8_t *packet = (uint8_t *)malloc(DNS_BUFFER_SIZE); |
Simon Cooksey |
0:fb7af294d5d9 | 216 | if (!packet) { |
Simon Cooksey |
0:fb7af294d5d9 | 217 | return NSAPI_ERROR_NO_MEMORY; |
Simon Cooksey |
0:fb7af294d5d9 | 218 | } |
Simon Cooksey |
0:fb7af294d5d9 | 219 | |
Simon Cooksey |
0:fb7af294d5d9 | 220 | int result = NSAPI_ERROR_DNS_FAILURE; |
Simon Cooksey |
0:fb7af294d5d9 | 221 | |
Simon Cooksey |
0:fb7af294d5d9 | 222 | // check against each dns server |
Simon Cooksey |
0:fb7af294d5d9 | 223 | for (unsigned i = 0; i < DNS_SERVERS_SIZE; i++) { |
Simon Cooksey |
0:fb7af294d5d9 | 224 | // send the question |
Simon Cooksey |
0:fb7af294d5d9 | 225 | uint8_t *question = packet; |
Simon Cooksey |
0:fb7af294d5d9 | 226 | dns_append_question(&question, host, version); |
Simon Cooksey |
0:fb7af294d5d9 | 227 | |
Simon Cooksey |
0:fb7af294d5d9 | 228 | err = socket.sendto(SocketAddress(dns_servers[i], 53), packet, DNS_BUFFER_SIZE); |
Simon Cooksey |
0:fb7af294d5d9 | 229 | if (err == NSAPI_ERROR_WOULD_BLOCK) { |
Simon Cooksey |
0:fb7af294d5d9 | 230 | continue; |
Simon Cooksey |
0:fb7af294d5d9 | 231 | } else if (err < 0) { |
Simon Cooksey |
0:fb7af294d5d9 | 232 | result = err; |
Simon Cooksey |
0:fb7af294d5d9 | 233 | break; |
Simon Cooksey |
0:fb7af294d5d9 | 234 | } |
Simon Cooksey |
0:fb7af294d5d9 | 235 | |
Simon Cooksey |
0:fb7af294d5d9 | 236 | // recv the response |
Simon Cooksey |
0:fb7af294d5d9 | 237 | err = socket.recvfrom(NULL, packet, DNS_BUFFER_SIZE); |
Simon Cooksey |
0:fb7af294d5d9 | 238 | if (err == NSAPI_ERROR_WOULD_BLOCK) { |
Simon Cooksey |
0:fb7af294d5d9 | 239 | continue; |
Simon Cooksey |
0:fb7af294d5d9 | 240 | } else if (err < 0) { |
Simon Cooksey |
0:fb7af294d5d9 | 241 | result = err; |
Simon Cooksey |
0:fb7af294d5d9 | 242 | break; |
Simon Cooksey |
0:fb7af294d5d9 | 243 | } |
Simon Cooksey |
0:fb7af294d5d9 | 244 | |
Simon Cooksey |
0:fb7af294d5d9 | 245 | const uint8_t *response = packet; |
Simon Cooksey |
0:fb7af294d5d9 | 246 | if (dns_scan_response(&response, addr, addr_count) > 0) { |
Simon Cooksey |
0:fb7af294d5d9 | 247 | result = NSAPI_ERROR_OK; |
Simon Cooksey |
0:fb7af294d5d9 | 248 | } |
Simon Cooksey |
0:fb7af294d5d9 | 249 | |
Simon Cooksey |
0:fb7af294d5d9 | 250 | /* The DNS response is final, no need to check other servers */ |
Simon Cooksey |
0:fb7af294d5d9 | 251 | break; |
Simon Cooksey |
0:fb7af294d5d9 | 252 | } |
Simon Cooksey |
0:fb7af294d5d9 | 253 | |
Simon Cooksey |
0:fb7af294d5d9 | 254 | // clean up packet |
Simon Cooksey |
0:fb7af294d5d9 | 255 | free(packet); |
Simon Cooksey |
0:fb7af294d5d9 | 256 | |
Simon Cooksey |
0:fb7af294d5d9 | 257 | // clean up udp |
Simon Cooksey |
0:fb7af294d5d9 | 258 | err = socket.close(); |
Simon Cooksey |
0:fb7af294d5d9 | 259 | if (err) { |
Simon Cooksey |
0:fb7af294d5d9 | 260 | return err; |
Simon Cooksey |
0:fb7af294d5d9 | 261 | } |
Simon Cooksey |
0:fb7af294d5d9 | 262 | |
Simon Cooksey |
0:fb7af294d5d9 | 263 | // return result |
Simon Cooksey |
0:fb7af294d5d9 | 264 | return result; |
Simon Cooksey |
0:fb7af294d5d9 | 265 | } |
Simon Cooksey |
0:fb7af294d5d9 | 266 | |
Simon Cooksey |
0:fb7af294d5d9 | 267 | // convenience functions for other forms of queries |
Simon Cooksey |
0:fb7af294d5d9 | 268 | extern "C" int nsapi_dns_query_multiple(nsapi_stack_t *stack, const char *host, |
Simon Cooksey |
0:fb7af294d5d9 | 269 | nsapi_addr_t *addr, unsigned addr_count, nsapi_version_t version) |
Simon Cooksey |
0:fb7af294d5d9 | 270 | { |
Simon Cooksey |
0:fb7af294d5d9 | 271 | NetworkStack *nstack = nsapi_create_stack(stack); |
Simon Cooksey |
0:fb7af294d5d9 | 272 | return nsapi_dns_query_multiple(nstack, host, addr, addr_count, version); |
Simon Cooksey |
0:fb7af294d5d9 | 273 | } |
Simon Cooksey |
0:fb7af294d5d9 | 274 | |
Simon Cooksey |
0:fb7af294d5d9 | 275 | int nsapi_dns_query_multiple(NetworkStack *stack, const char *host, |
Simon Cooksey |
0:fb7af294d5d9 | 276 | SocketAddress *addresses, unsigned addr_count, nsapi_version_t version) |
Simon Cooksey |
0:fb7af294d5d9 | 277 | { |
Simon Cooksey |
0:fb7af294d5d9 | 278 | nsapi_addr_t *addrs = new nsapi_addr_t[addr_count]; |
Simon Cooksey |
0:fb7af294d5d9 | 279 | int result = nsapi_dns_query_multiple(stack, host, addrs, addr_count, version); |
Simon Cooksey |
0:fb7af294d5d9 | 280 | |
Simon Cooksey |
0:fb7af294d5d9 | 281 | if (result > 0) { |
Simon Cooksey |
0:fb7af294d5d9 | 282 | for (int i = 0; i < result; i++) { |
Simon Cooksey |
0:fb7af294d5d9 | 283 | addresses[i].set_addr(addrs[i]); |
Simon Cooksey |
0:fb7af294d5d9 | 284 | } |
Simon Cooksey |
0:fb7af294d5d9 | 285 | } |
Simon Cooksey |
0:fb7af294d5d9 | 286 | |
Simon Cooksey |
0:fb7af294d5d9 | 287 | delete[] addrs; |
Simon Cooksey |
0:fb7af294d5d9 | 288 | return result; |
Simon Cooksey |
0:fb7af294d5d9 | 289 | } |
Simon Cooksey |
0:fb7af294d5d9 | 290 | |
Simon Cooksey |
0:fb7af294d5d9 | 291 | extern "C" int nsapi_dns_query(nsapi_stack_t *stack, const char *host, |
Simon Cooksey |
0:fb7af294d5d9 | 292 | nsapi_addr_t *addr, nsapi_version_t version) |
Simon Cooksey |
0:fb7af294d5d9 | 293 | { |
Simon Cooksey |
0:fb7af294d5d9 | 294 | NetworkStack *nstack = nsapi_create_stack(stack); |
Simon Cooksey |
0:fb7af294d5d9 | 295 | int result = nsapi_dns_query_multiple(nstack, host, addr, 1, version); |
Simon Cooksey |
0:fb7af294d5d9 | 296 | return (result > 0) ? 0 : result; |
Simon Cooksey |
0:fb7af294d5d9 | 297 | } |
Simon Cooksey |
0:fb7af294d5d9 | 298 | |
Simon Cooksey |
0:fb7af294d5d9 | 299 | int nsapi_dns_query(NetworkStack *stack, const char *host, |
Simon Cooksey |
0:fb7af294d5d9 | 300 | SocketAddress *address, nsapi_version_t version) |
Simon Cooksey |
0:fb7af294d5d9 | 301 | { |
Simon Cooksey |
0:fb7af294d5d9 | 302 | nsapi_addr_t addr; |
Simon Cooksey |
0:fb7af294d5d9 | 303 | int result = nsapi_dns_query_multiple(stack, host, &addr, 1, version); |
Simon Cooksey |
0:fb7af294d5d9 | 304 | address->set_addr(addr); |
Simon Cooksey |
0:fb7af294d5d9 | 305 | return (result > 0) ? 0 : result; |
Simon Cooksey |
0:fb7af294d5d9 | 306 | } |