mbed OS5

Fork of UIPEthernet by Zoltan Hudak

Committer:
hudakz
Date:
Fri Jun 30 19:51:28 2017 +0000
Revision:
8:4acb22344932
Parent:
7:1bc7e6120801
'UIPEthernet' renamed to 'uIPEthernet'; 'UIPEthernetClass' renamed to 'UIPEthernet'; added IPAddress::toString() to support printing; added print support for debugging; bugs preventing offline build with GCC ARM toolchain fixed.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 4:d774541a34da 1 // Arduino DNS client for Enc28J60-based Ethernet shield
hudakz 0:5350a66d5279 2 // (c) Copyright 2009-2010 MCQN Ltd.
hudakz 0:5350a66d5279 3 // Released under Apache License, version 2.0
hudakz 4:d774541a34da 4 #include "Udp.h"
hudakz 4:d774541a34da 5 #include "utility/util.h"
hudakz 4:d774541a34da 6 #include "utility/millis.h"
hudakz 0:5350a66d5279 7
hudakz 0:5350a66d5279 8 #include "Dns.h"
hudakz 0:5350a66d5279 9 #include <string.h>
hudakz 0:5350a66d5279 10 //#include <stdlib.h>
hudakz 0:5350a66d5279 11
hudakz 8:4acb22344932 12 #include "mbed.h"
hudakz 0:5350a66d5279 13
hudakz 0:5350a66d5279 14 #define SOCKET_NONE 255
hudakz 0:5350a66d5279 15 // Various flags and header field values for a DNS message
hudakz 0:5350a66d5279 16
hudakz 0:5350a66d5279 17 #define UDP_HEADER_SIZE 8
hudakz 0:5350a66d5279 18 #define DNS_HEADER_SIZE 12
hudakz 0:5350a66d5279 19 #define TTL_SIZE 4
hudakz 0:5350a66d5279 20 #define QUERY_FLAG (0)
hudakz 0:5350a66d5279 21 #define RESPONSE_FLAG (1 << 15)
hudakz 0:5350a66d5279 22 #define QUERY_RESPONSE_MASK (1 << 15)
hudakz 0:5350a66d5279 23 #define OPCODE_STANDARD_QUERY (0)
hudakz 0:5350a66d5279 24 #define OPCODE_INVERSE_QUERY (1 << 11)
hudakz 0:5350a66d5279 25 #define OPCODE_STATUS_REQUEST (2 << 11)
hudakz 0:5350a66d5279 26 #define OPCODE_MASK (15 << 11)
hudakz 0:5350a66d5279 27 #define AUTHORITATIVE_FLAG (1 << 10)
hudakz 0:5350a66d5279 28 #define TRUNCATION_FLAG (1 << 9)
hudakz 0:5350a66d5279 29 #define RECURSION_DESIRED_FLAG (1 << 8)
hudakz 0:5350a66d5279 30 #define RECURSION_AVAILABLE_FLAG (1 << 7)
hudakz 0:5350a66d5279 31 #define RESP_NO_ERROR (0)
hudakz 0:5350a66d5279 32 #define RESP_FORMAT_ERROR (1)
hudakz 0:5350a66d5279 33 #define RESP_SERVER_FAILURE (2)
hudakz 0:5350a66d5279 34 #define RESP_NAME_ERROR (3)
hudakz 0:5350a66d5279 35 #define RESP_NOT_IMPLEMENTED (4)
hudakz 0:5350a66d5279 36 #define RESP_REFUSED (5)
hudakz 0:5350a66d5279 37 #define RESP_MASK (15)
hudakz 0:5350a66d5279 38 #define TYPE_A (0x0001)
hudakz 0:5350a66d5279 39 #define CLASS_IN (0x0001)
hudakz 0:5350a66d5279 40 #define LABEL_COMPRESSION_MASK (0xC0)
hudakz 0:5350a66d5279 41 // Port number that DNS servers listen on
hudakz 0:5350a66d5279 42
hudakz 0:5350a66d5279 43 #define DNS_PORT 53
hudakz 0:5350a66d5279 44
hudakz 0:5350a66d5279 45 // Possible return codes from ProcessResponse
hudakz 0:5350a66d5279 46
hudakz 8:4acb22344932 47 #define SUCCESS 1
hudakz 8:4acb22344932 48 #define TIMED_OUT - 1
hudakz 8:4acb22344932 49 #define INVALID_SERVER - 2
hudakz 8:4acb22344932 50 #define TRUNCATED - 3
hudakz 8:4acb22344932 51 #define INVALID_RESPONSE - 4
hudakz 0:5350a66d5279 52
hudakz 0:5350a66d5279 53 /**
hudakz 0:5350a66d5279 54 * @brief
hudakz 0:5350a66d5279 55 * @note
hudakz 0:5350a66d5279 56 * @param
hudakz 0:5350a66d5279 57 * @retval
hudakz 0:5350a66d5279 58 */
hudakz 0:5350a66d5279 59 void DNSClient::begin(const IPAddress& aDNSServer) {
hudakz 0:5350a66d5279 60 iDNSServer = aDNSServer;
hudakz 0:5350a66d5279 61 iRequestId = 0;
hudakz 0:5350a66d5279 62 }
hudakz 0:5350a66d5279 63
hudakz 0:5350a66d5279 64 /**
hudakz 0:5350a66d5279 65 * @brief
hudakz 0:5350a66d5279 66 * @note
hudakz 0:5350a66d5279 67 * @param
hudakz 0:5350a66d5279 68 * @retval
hudakz 0:5350a66d5279 69 */
hudakz 0:5350a66d5279 70 int DNSClient::inet_aton(const char* aIPAddrString, IPAddress& aResult) {
hudakz 0:5350a66d5279 71
hudakz 0:5350a66d5279 72 // See if we've been given a valid IP address
hudakz 0:5350a66d5279 73 const char* p = aIPAddrString;
hudakz 8:4acb22344932 74 while (*p && ((*p == '.') || ((*p >= '0') && (*p <= '9')))) {
hudakz 0:5350a66d5279 75 p++;
hudakz 0:5350a66d5279 76 }
hudakz 0:5350a66d5279 77
hudakz 8:4acb22344932 78 if (*p == '\0') {
hudakz 0:5350a66d5279 79
hudakz 0:5350a66d5279 80 // It's looking promising, we haven't found any invalid characters
hudakz 0:5350a66d5279 81 p = aIPAddrString;
hudakz 0:5350a66d5279 82
hudakz 0:5350a66d5279 83 int segment = 0;
hudakz 0:5350a66d5279 84 int segmentValue = 0;
hudakz 8:4acb22344932 85 while (*p && (segment < 4)) {
hudakz 8:4acb22344932 86 if (*p == '.') {
hudakz 0:5350a66d5279 87
hudakz 0:5350a66d5279 88 // We've reached the end of a segment
hudakz 8:4acb22344932 89 if (segmentValue > 255) {
hudakz 0:5350a66d5279 90
hudakz 0:5350a66d5279 91 // You can't have IP address segments that don't fit in a byte
hudakz 0:5350a66d5279 92 return 0;
hudakz 0:5350a66d5279 93 }
hudakz 0:5350a66d5279 94 else {
hudakz 0:5350a66d5279 95 aResult[segment] = (uint8_t) segmentValue;
hudakz 0:5350a66d5279 96 segment++;
hudakz 0:5350a66d5279 97 segmentValue = 0;
hudakz 0:5350a66d5279 98 }
hudakz 0:5350a66d5279 99 }
hudakz 0:5350a66d5279 100 else {
hudakz 0:5350a66d5279 101
hudakz 0:5350a66d5279 102 // Next digit
hudakz 0:5350a66d5279 103 segmentValue = (segmentValue * 10) + (*p - '0');
hudakz 0:5350a66d5279 104 }
hudakz 0:5350a66d5279 105
hudakz 0:5350a66d5279 106 p++;
hudakz 0:5350a66d5279 107 }
hudakz 0:5350a66d5279 108
hudakz 0:5350a66d5279 109 // We've reached the end of address, but there'll still be the last
hudakz 0:5350a66d5279 110 // segment to deal with
hudakz 8:4acb22344932 111 if ((segmentValue > 255) || (segment > 3)) {
hudakz 0:5350a66d5279 112
hudakz 0:5350a66d5279 113 // You can't have IP address segments that don't fit in a byte,
hudakz 0:5350a66d5279 114 // or more than four segments
hudakz 0:5350a66d5279 115 return 0;
hudakz 0:5350a66d5279 116 }
hudakz 0:5350a66d5279 117 else {
hudakz 0:5350a66d5279 118 aResult[segment] = (uint8_t) segmentValue;
hudakz 0:5350a66d5279 119 return 1;
hudakz 0:5350a66d5279 120 }
hudakz 0:5350a66d5279 121 }
hudakz 0:5350a66d5279 122 else {
hudakz 0:5350a66d5279 123 return 0;
hudakz 0:5350a66d5279 124 }
hudakz 0:5350a66d5279 125 }
hudakz 0:5350a66d5279 126
hudakz 0:5350a66d5279 127 /**
hudakz 0:5350a66d5279 128 * @brief
hudakz 0:5350a66d5279 129 * @note
hudakz 0:5350a66d5279 130 * @param
hudakz 0:5350a66d5279 131 * @retval
hudakz 0:5350a66d5279 132 */
hudakz 0:5350a66d5279 133 int DNSClient::getHostByName(const char* aHostname, IPAddress& aResult) {
hudakz 0:5350a66d5279 134 int ret = 0;
hudakz 0:5350a66d5279 135
hudakz 0:5350a66d5279 136 // See if it's a numeric IP address
hudakz 0:5350a66d5279 137
hudakz 8:4acb22344932 138 if (inet_aton(aHostname, aResult)) {
hudakz 0:5350a66d5279 139
hudakz 0:5350a66d5279 140 // It is, our work here is done
hudakz 0:5350a66d5279 141 return 1;
hudakz 0:5350a66d5279 142 }
hudakz 0:5350a66d5279 143
hudakz 0:5350a66d5279 144 // Check we've got a valid DNS server to use
hudakz 8:4acb22344932 145 if (iDNSServer == INADDR_NONE) {
hudakz 0:5350a66d5279 146 return INVALID_SERVER;
hudakz 0:5350a66d5279 147 }
hudakz 0:5350a66d5279 148
hudakz 0:5350a66d5279 149 // Find a socket to use
hudakz 8:4acb22344932 150 if (iUdp.begin(1024 + (millis() & 0xF)) == 1) {
hudakz 0:5350a66d5279 151
hudakz 0:5350a66d5279 152 // Try up to three times
hudakz 0:5350a66d5279 153 int retries = 0;
hudakz 0:5350a66d5279 154 // while ((retries < 3) && (ret <= 0))
hudakz 0:5350a66d5279 155 {
hudakz 0:5350a66d5279 156 // Send DNS request
hudakz 0:5350a66d5279 157 ret = iUdp.beginPacket(iDNSServer, DNS_PORT);
hudakz 8:4acb22344932 158 if (ret != 0) {
hudakz 0:5350a66d5279 159
hudakz 0:5350a66d5279 160 // Now output the request data
hudakz 0:5350a66d5279 161 ret = BuildRequest(aHostname);
hudakz 8:4acb22344932 162 if (ret != 0) {
hudakz 0:5350a66d5279 163
hudakz 0:5350a66d5279 164 // And finally send the request
hudakz 0:5350a66d5279 165 ret = iUdp.endPacket();
hudakz 8:4acb22344932 166 if (ret != 0) {
hudakz 0:5350a66d5279 167
hudakz 0:5350a66d5279 168 // Now wait for a response
hudakz 0:5350a66d5279 169 int wait_retries = 0;
hudakz 0:5350a66d5279 170 ret = TIMED_OUT;
hudakz 8:4acb22344932 171 while ((wait_retries < 3) && (ret == TIMED_OUT)) {
hudakz 0:5350a66d5279 172 ret = ProcessResponse(5000, aResult);
hudakz 0:5350a66d5279 173 wait_retries++;
hudakz 0:5350a66d5279 174 }
hudakz 0:5350a66d5279 175 }
hudakz 0:5350a66d5279 176 }
hudakz 0:5350a66d5279 177 }
hudakz 0:5350a66d5279 178
hudakz 0:5350a66d5279 179 retries++;
hudakz 0:5350a66d5279 180 }
hudakz 0:5350a66d5279 181
hudakz 0:5350a66d5279 182 // We're done with the socket now
hudakz 0:5350a66d5279 183 iUdp.stop();
hudakz 0:5350a66d5279 184 }
hudakz 0:5350a66d5279 185
hudakz 0:5350a66d5279 186 return ret;
hudakz 0:5350a66d5279 187 }
hudakz 0:5350a66d5279 188
hudakz 0:5350a66d5279 189 /**
hudakz 0:5350a66d5279 190 * @brief
hudakz 0:5350a66d5279 191 * @note
hudakz 0:5350a66d5279 192 * @param
hudakz 0:5350a66d5279 193 * @retval
hudakz 0:5350a66d5279 194 */
hudakz 0:5350a66d5279 195 uint16_t DNSClient::BuildRequest(const char* aName) {
hudakz 0:5350a66d5279 196
hudakz 0:5350a66d5279 197 // Build header
hudakz 0:5350a66d5279 198
hudakz 0:5350a66d5279 199 // 1 1 1 1 1 1
hudakz 0:5350a66d5279 200 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
hudakz 0:5350a66d5279 201 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
hudakz 0:5350a66d5279 202 // | ID |
hudakz 0:5350a66d5279 203 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
hudakz 0:5350a66d5279 204 // |QR| Opcode |AA|TC|RD|RA| Z | RCODE |
hudakz 0:5350a66d5279 205 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
hudakz 0:5350a66d5279 206 // | QDCOUNT |
hudakz 0:5350a66d5279 207 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
hudakz 0:5350a66d5279 208 // | ANCOUNT |
hudakz 0:5350a66d5279 209 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
hudakz 0:5350a66d5279 210 // | NSCOUNT |
hudakz 0:5350a66d5279 211 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
hudakz 0:5350a66d5279 212 // | ARCOUNT |
hudakz 0:5350a66d5279 213 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
hudakz 0:5350a66d5279 214 // As we only support one request at a time at present, we can simplify
hudakz 0:5350a66d5279 215 // some of this header
hudakz 4:d774541a34da 216 iRequestId = millis(); // generate a random ID
hudakz 0:5350a66d5279 217 uint16_t twoByteBuffer;
hudakz 0:5350a66d5279 218
hudakz 0:5350a66d5279 219 // FIXME We should also check that there's enough space available to write to, rather
hudakz 0:5350a66d5279 220
hudakz 0:5350a66d5279 221 // FIXME than assume there's enough space (as the code does at present)
hudakz 0:5350a66d5279 222 iUdp.write((uint8_t*) &iRequestId, sizeof(iRequestId));
hudakz 0:5350a66d5279 223
hudakz 0:5350a66d5279 224 twoByteBuffer = htons(QUERY_FLAG | OPCODE_STANDARD_QUERY | RECURSION_DESIRED_FLAG);
hudakz 0:5350a66d5279 225 iUdp.write((uint8_t*) &twoByteBuffer, sizeof(twoByteBuffer));
hudakz 0:5350a66d5279 226
hudakz 0:5350a66d5279 227 twoByteBuffer = htons(1); // One question record
hudakz 0:5350a66d5279 228 iUdp.write((uint8_t*) &twoByteBuffer, sizeof(twoByteBuffer));
hudakz 0:5350a66d5279 229
hudakz 0:5350a66d5279 230 twoByteBuffer = 0; // Zero answer records
hudakz 0:5350a66d5279 231 iUdp.write((uint8_t*) &twoByteBuffer, sizeof(twoByteBuffer));
hudakz 0:5350a66d5279 232
hudakz 0:5350a66d5279 233 iUdp.write((uint8_t*) &twoByteBuffer, sizeof(twoByteBuffer));
hudakz 0:5350a66d5279 234
hudakz 0:5350a66d5279 235 // and zero additional records
hudakz 0:5350a66d5279 236 iUdp.write((uint8_t*) &twoByteBuffer, sizeof(twoByteBuffer));
hudakz 0:5350a66d5279 237
hudakz 0:5350a66d5279 238 // Build question
hudakz 0:5350a66d5279 239 const char* start = aName;
hudakz 0:5350a66d5279 240 const char* end = start;
hudakz 0:5350a66d5279 241 uint8_t len;
hudakz 0:5350a66d5279 242 // Run through the name being requested
hudakz 0:5350a66d5279 243
hudakz 8:4acb22344932 244 while (*end) {
hudakz 0:5350a66d5279 245
hudakz 0:5350a66d5279 246 // Find out how long this section of the name is
hudakz 0:5350a66d5279 247 end = start;
hudakz 8:4acb22344932 248 while (*end && (*end != '.')) {
hudakz 0:5350a66d5279 249 end++;
hudakz 0:5350a66d5279 250 }
hudakz 0:5350a66d5279 251
hudakz 8:4acb22344932 252 if (end - start > 0) {
hudakz 0:5350a66d5279 253
hudakz 0:5350a66d5279 254 // Write out the size of this section
hudakz 0:5350a66d5279 255 len = end - start;
hudakz 0:5350a66d5279 256 iUdp.write(&len, sizeof(len));
hudakz 0:5350a66d5279 257
hudakz 0:5350a66d5279 258 // And then write out the section
hudakz 0:5350a66d5279 259 iUdp.write((uint8_t*)start, end - start);
hudakz 0:5350a66d5279 260 }
hudakz 0:5350a66d5279 261
hudakz 0:5350a66d5279 262 start = end + 1;
hudakz 0:5350a66d5279 263 }
hudakz 0:5350a66d5279 264
hudakz 0:5350a66d5279 265 // We've got to the end of the question name, so
hudakz 0:5350a66d5279 266 // terminate it with a zero-length section
hudakz 0:5350a66d5279 267 len = 0;
hudakz 0:5350a66d5279 268 iUdp.write(&len, sizeof(len));
hudakz 0:5350a66d5279 269
hudakz 0:5350a66d5279 270 // Finally the type and class of question
hudakz 0:5350a66d5279 271 twoByteBuffer = htons(TYPE_A);
hudakz 0:5350a66d5279 272 iUdp.write((uint8_t*) &twoByteBuffer, sizeof(twoByteBuffer));
hudakz 0:5350a66d5279 273
hudakz 0:5350a66d5279 274 twoByteBuffer = htons(CLASS_IN); // Internet class of question
hudakz 0:5350a66d5279 275 iUdp.write((uint8_t*) &twoByteBuffer, sizeof(twoByteBuffer));
hudakz 0:5350a66d5279 276
hudakz 0:5350a66d5279 277 // Success! Everything buffered okay
hudakz 0:5350a66d5279 278 return 1;
hudakz 0:5350a66d5279 279 }
hudakz 0:5350a66d5279 280
hudakz 0:5350a66d5279 281 /**
hudakz 0:5350a66d5279 282 * @brief
hudakz 0:5350a66d5279 283 * @note
hudakz 0:5350a66d5279 284 * @param
hudakz 0:5350a66d5279 285 * @retval
hudakz 0:5350a66d5279 286 */
hudakz 0:5350a66d5279 287 uint16_t DNSClient::ProcessResponse(uint16_t aTimeout, IPAddress& aAddress) {
hudakz 4:d774541a34da 288 uint32_t startTime = millis();
hudakz 0:5350a66d5279 289
hudakz 0:5350a66d5279 290 // Wait for a response packet
hudakz 0:5350a66d5279 291
hudakz 8:4acb22344932 292 while (iUdp.parsePacket() <= 0) {
hudakz 8:4acb22344932 293 if ((millis() - startTime) > aTimeout)
hudakz 0:5350a66d5279 294 return TIMED_OUT;
hudakz 0:5350a66d5279 295 wait(0.050);
hudakz 0:5350a66d5279 296 }
hudakz 0:5350a66d5279 297
hudakz 0:5350a66d5279 298 // We've had a reply!
hudakz 0:5350a66d5279 299 // Read the UDP header
hudakz 0:5350a66d5279 300 uint8_t header[DNS_HEADER_SIZE]; // Enough space to reuse for the DNS header
hudakz 0:5350a66d5279 301
hudakz 0:5350a66d5279 302 // Check that it's a response from the right server and the right port
hudakz 8:4acb22344932 303 if ((iDNSServer != iUdp.remoteIP()) || (iUdp.remotePort() != DNS_PORT)) {
hudakz 0:5350a66d5279 304
hudakz 0:5350a66d5279 305 // It's not from who we expected
hudakz 0:5350a66d5279 306 return INVALID_SERVER;
hudakz 0:5350a66d5279 307 }
hudakz 0:5350a66d5279 308
hudakz 0:5350a66d5279 309 // Read through the rest of the response
hudakz 8:4acb22344932 310 if (iUdp.available() < DNS_HEADER_SIZE) {
hudakz 0:5350a66d5279 311 return TRUNCATED;
hudakz 0:5350a66d5279 312 }
hudakz 0:5350a66d5279 313
hudakz 0:5350a66d5279 314 iUdp.read(header, DNS_HEADER_SIZE);
hudakz 0:5350a66d5279 315
hudakz 0:5350a66d5279 316 uint16_t header_flags = htons(*((uint16_t*) &header[2]));
hudakz 0:5350a66d5279 317 // Check that it's a response to this request
hudakz 0:5350a66d5279 318
hudakz 0:5350a66d5279 319 if
hudakz 0:5350a66d5279 320 (
hudakz 0:5350a66d5279 321 (iRequestId != (*((uint16_t*) &header[0])))
hudakz 0:5350a66d5279 322 || ((header_flags & QUERY_RESPONSE_MASK) != (uint16_t) RESPONSE_FLAG)
hudakz 0:5350a66d5279 323 ) {
hudakz 0:5350a66d5279 324
hudakz 0:5350a66d5279 325 // Mark the entire packet as read
hudakz 0:5350a66d5279 326 iUdp.flush();
hudakz 0:5350a66d5279 327 return INVALID_RESPONSE;
hudakz 0:5350a66d5279 328 }
hudakz 0:5350a66d5279 329
hudakz 0:5350a66d5279 330 // Check for any errors in the response (or in our request)
hudakz 0:5350a66d5279 331 // although we don't do anything to get round these
hudakz 8:4acb22344932 332 if ((header_flags & TRUNCATION_FLAG) || (header_flags & RESP_MASK)) {
hudakz 0:5350a66d5279 333
hudakz 0:5350a66d5279 334 // Mark the entire packet as read
hudakz 0:5350a66d5279 335 iUdp.flush();
hudakz 0:5350a66d5279 336 return -5; //INVALID_RESPONSE;
hudakz 0:5350a66d5279 337 }
hudakz 0:5350a66d5279 338
hudakz 0:5350a66d5279 339 // And make sure we've got (at least) one answer
hudakz 0:5350a66d5279 340 uint16_t answerCount = htons(*((uint16_t*) &header[6]));
hudakz 8:4acb22344932 341 if (answerCount == 0) {
hudakz 0:5350a66d5279 342
hudakz 0:5350a66d5279 343 // Mark the entire packet as read
hudakz 0:5350a66d5279 344 iUdp.flush();
hudakz 0:5350a66d5279 345 return -6; //INVALID_RESPONSE;
hudakz 0:5350a66d5279 346 }
hudakz 0:5350a66d5279 347
hudakz 0:5350a66d5279 348 // Skip over any questions
hudakz 8:4acb22344932 349 for (uint16_t i = 0; i < htons(*((uint16_t*) &header[4])); i++) {
hudakz 0:5350a66d5279 350
hudakz 0:5350a66d5279 351 // Skip over the name
hudakz 0:5350a66d5279 352 uint8_t len;
hudakz 8:4acb22344932 353 do {
hudakz 0:5350a66d5279 354 iUdp.read(&len, sizeof(len));
hudakz 8:4acb22344932 355 if (len > 0) {
hudakz 0:5350a66d5279 356
hudakz 0:5350a66d5279 357 // Don't need to actually read the data out for the string, just
hudakz 0:5350a66d5279 358 // advance ptr to beyond it
hudakz 8:4acb22344932 359 while (len--) {
hudakz 0:5350a66d5279 360 iUdp.read(); // we don't care about the returned byte
hudakz 0:5350a66d5279 361 }
hudakz 0:5350a66d5279 362 }
hudakz 8:4acb22344932 363 } while (len != 0);
hudakz 0:5350a66d5279 364
hudakz 0:5350a66d5279 365 // Now jump over the type and class
hudakz 8:4acb22344932 366 for (int i = 0; i < 4; i++) {
hudakz 0:5350a66d5279 367 iUdp.read(); // we don't care about the returned byte
hudakz 0:5350a66d5279 368 }
hudakz 0:5350a66d5279 369 }
hudakz 0:5350a66d5279 370
hudakz 0:5350a66d5279 371 // Now we're up to the bit we're interested in, the answer
hudakz 0:5350a66d5279 372 // There might be more than one answer (although we'll just use the first
hudakz 0:5350a66d5279 373 // type A answer) and some authority and additional resource records but
hudakz 0:5350a66d5279 374 // we're going to ignore all of them.
hudakz 8:4acb22344932 375 for (uint16_t i = 0; i < answerCount; i++) {
hudakz 0:5350a66d5279 376
hudakz 0:5350a66d5279 377 // Skip the name
hudakz 0:5350a66d5279 378 uint8_t len;
hudakz 8:4acb22344932 379 do {
hudakz 0:5350a66d5279 380 iUdp.read(&len, sizeof(len));
hudakz 8:4acb22344932 381 if ((len & LABEL_COMPRESSION_MASK) == 0) {
hudakz 0:5350a66d5279 382
hudakz 0:5350a66d5279 383 // It's just a normal label
hudakz 8:4acb22344932 384 if (len > 0) {
hudakz 0:5350a66d5279 385
hudakz 0:5350a66d5279 386 // And it's got a length
hudakz 0:5350a66d5279 387 // Don't need to actually read the data out for the string,
hudakz 0:5350a66d5279 388 // just advance ptr to beyond it
hudakz 8:4acb22344932 389 while (len--) {
hudakz 0:5350a66d5279 390 iUdp.read(); // we don't care about the returned byte
hudakz 0:5350a66d5279 391 }
hudakz 0:5350a66d5279 392 }
hudakz 0:5350a66d5279 393 }
hudakz 0:5350a66d5279 394 else {
hudakz 0:5350a66d5279 395
hudakz 0:5350a66d5279 396 // This is a pointer to a somewhere else in the message for the
hudakz 0:5350a66d5279 397 // rest of the name. We don't care about the name, and RFC1035
hudakz 0:5350a66d5279 398 // says that a name is either a sequence of labels ended with a
hudakz 0:5350a66d5279 399 // 0 length octet or a pointer or a sequence of labels ending in
hudakz 0:5350a66d5279 400 // a pointer. Either way, when we get here we're at the end of
hudakz 0:5350a66d5279 401 // the name
hudakz 0:5350a66d5279 402 // Skip over the pointer
hudakz 0:5350a66d5279 403 iUdp.read(); // we don't care about the returned byte
hudakz 0:5350a66d5279 404
hudakz 0:5350a66d5279 405 // And set len so that we drop out of the name loop
hudakz 0:5350a66d5279 406 len = 0;
hudakz 0:5350a66d5279 407 }
hudakz 8:4acb22344932 408 } while (len != 0);
hudakz 0:5350a66d5279 409
hudakz 0:5350a66d5279 410 // Check the type and class
hudakz 0:5350a66d5279 411 uint16_t answerType;
hudakz 0:5350a66d5279 412 uint16_t answerClass;
hudakz 0:5350a66d5279 413 iUdp.read((uint8_t*) &answerType, sizeof(answerType));
hudakz 0:5350a66d5279 414 iUdp.read((uint8_t*) &answerClass, sizeof(answerClass));
hudakz 0:5350a66d5279 415
hudakz 0:5350a66d5279 416 // Ignore the Time-To-Live as we don't do any caching
hudakz 8:4acb22344932 417 for (int i = 0; i < TTL_SIZE; i++) {
hudakz 0:5350a66d5279 418 iUdp.read(); // we don't care about the returned byte
hudakz 0:5350a66d5279 419 }
hudakz 0:5350a66d5279 420
hudakz 0:5350a66d5279 421 // And read out the length of this answer
hudakz 0:5350a66d5279 422 // Don't need header_flags anymore, so we can reuse it here
hudakz 0:5350a66d5279 423 iUdp.read((uint8_t*) &header_flags, sizeof(header_flags));
hudakz 0:5350a66d5279 424
hudakz 8:4acb22344932 425 if ((htons(answerType) == TYPE_A) && (htons(answerClass) == CLASS_IN)) {
hudakz 8:4acb22344932 426 if (htons(header_flags) != 4) {
hudakz 0:5350a66d5279 427
hudakz 0:5350a66d5279 428 // It's a weird size
hudakz 0:5350a66d5279 429 // Mark the entire packet as read
hudakz 0:5350a66d5279 430 iUdp.flush();
hudakz 0:5350a66d5279 431 return -9; //INVALID_RESPONSE;
hudakz 0:5350a66d5279 432 }
hudakz 0:5350a66d5279 433
hudakz 0:5350a66d5279 434 iUdp.read(aAddress.raw_address(), 4);
hudakz 0:5350a66d5279 435 return SUCCESS;
hudakz 0:5350a66d5279 436 }
hudakz 0:5350a66d5279 437 else {
hudakz 0:5350a66d5279 438
hudakz 0:5350a66d5279 439 // This isn't an answer type we're after, move onto the next one
hudakz 8:4acb22344932 440 for (uint16_t i = 0; i < htons(header_flags); i++) {
hudakz 0:5350a66d5279 441 iUdp.read(); // we don't care about the returned byte
hudakz 0:5350a66d5279 442 }
hudakz 0:5350a66d5279 443 }
hudakz 0:5350a66d5279 444 }
hudakz 0:5350a66d5279 445
hudakz 0:5350a66d5279 446 // Mark the entire packet as read
hudakz 0:5350a66d5279 447 iUdp.flush();
hudakz 0:5350a66d5279 448
hudakz 0:5350a66d5279 449 // If we get here then we haven't found an answer
hudakz 0:5350a66d5279 450 return -10; //INVALID_RESPONSE;
hudakz 0:5350a66d5279 451 }