| 253 | |
| 254 | |
| 255 | uint16_t DNSClient::ProcessResponse(uint16_t aTimeout, IPAddress& aAddress) |
| 256 | { |
| 257 | uint32_t startTime = millis(); |
| 258 | |
| 259 | // Wait for a response packet |
| 260 | while(iUdp.parsePacket() <= 0) |
| 261 | { |
| 262 | if((millis() - startTime) > aTimeout) |
| 263 | return TIMED_OUT; |
| 264 | delay(50); |
| 265 | } |
| 266 | |
| 267 | // We've had a reply! |
| 268 | // Read the UDP header |
| 269 | uint8_t header[DNS_HEADER_SIZE]; // Enough space to reuse for the DNS header |
| 270 | // Check that it's a response from the right server and the right port |
| 271 | if ( (iDNSServer != iUdp.remoteIP()) || |
| 272 | (iUdp.remotePort() != DNS_PORT) ) |
| 273 | { |
| 274 | // It's not from who we expected |
| 275 | return INVALID_SERVER; |
| 276 | } |
| 277 | |
| 278 | // Read through the rest of the response |
| 279 | if (iUdp.available() < DNS_HEADER_SIZE) |
| 280 | { |
| 281 | return TRUNCATED; |
| 282 | } |
| 283 | iUdp.read(header, DNS_HEADER_SIZE); |
| 284 | |
| 285 | uint16_t header_flags = htons(*((uint16_t*)&header[2])); |
| 286 | // Check that it's a response to this request |
| 287 | if ( ( iRequestId != (*((uint16_t*)&header[0])) ) || |
| 288 | ((header_flags & QUERY_RESPONSE_MASK) != (uint16_t)RESPONSE_FLAG) ) |
| 289 | { |
| 290 | // Mark the entire packet as read |
| 291 | iUdp.flush(); |
| 292 | return INVALID_RESPONSE; |
| 293 | } |
| 294 | // Check for any errors in the response (or in our request) |
| 295 | // although we don't do anything to get round these |
| 296 | if ( (header_flags & TRUNCATION_FLAG) || (header_flags & RESP_MASK) ) |
| 297 | { |
| 298 | // Mark the entire packet as read |
| 299 | iUdp.flush(); |
| 300 | return -5; //INVALID_RESPONSE; |
| 301 | } |
| 302 | |
| 303 | // And make sure we've got (at least) one answer |
| 304 | uint16_t answerCount = htons(*((uint16_t*)&header[6])); |
| 305 | if (answerCount == 0 ) |
| 306 | { |
| 307 | // Mark the entire packet as read |
| 308 | iUdp.flush(); |
| 309 | return -6; //INVALID_RESPONSE; |
| 310 | } |
| 311 | |
| 312 | // Skip over any questions |
nothing calls this directly
no test coverage detected