mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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