BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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