Development mbed library for MAX32630FTHR

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

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