mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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