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