|
| 1 | +/* Copyright (c) 2013 Henry Leinen (henry[dot]leinen [at] online [dot] de) |
| 2 | + * |
| 3 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| 4 | + * and associated documentation files (the "Software"), to deal in the Software without restriction, |
| 5 | + * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
| 6 | + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
| 7 | + * furnished to do so, subject to the following conditions: |
| 8 | + * |
| 9 | + * The above copyright notice and this permission notice shall be included in all copies or |
| 10 | + * substantial portions of the Software. |
| 11 | + * |
| 12 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| 13 | + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 14 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 15 | + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 16 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 17 | + */ |
| 18 | +#include "DnsQuery.h" |
| 19 | +#include <stdio.h> |
| 20 | +#include <string.h> |
| 21 | + |
| 22 | + |
| 23 | +#define DNS_COUNT (sizeof DNS_IPS / sizeof DNS_IPS[0]) |
| 24 | +const char *DNS_IPS[] = { |
| 25 | + "8.8.8.8", |
| 26 | + "209.244.0.3", |
| 27 | + "84.200.69.80", |
| 28 | + "8.26.56.26", |
| 29 | + "208.67.222.222" |
| 30 | +}; |
| 31 | + |
| 32 | +static bool isIP(const char *host) |
| 33 | +{ |
| 34 | + int i; |
| 35 | + |
| 36 | + for (i = 0; host[i]; i++) { |
| 37 | + if (!(host[i] >= '0' && host[i] <= '9') && host[i] != '.') { |
| 38 | + return false; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Ending with '.' garuntees host |
| 43 | + if (i > 0 && host[i-1] == '.') { |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + return true; |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +static bool parseRR(uint8_t *resp, int &c, char *adr) |
| 52 | +{ |
| 53 | + int n = 0; |
| 54 | + while((n=resp[c++]) != 0) { |
| 55 | + if ((n & 0xc0) != 0) { |
| 56 | + // This is a link |
| 57 | + c++; |
| 58 | + break; |
| 59 | + } else { |
| 60 | + // skip this segment, not interested in string domain names |
| 61 | + c+= n; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + int TYPE = (((int)resp[c])<<8) + resp[c+1]; |
| 66 | + int CLASS = (((int)resp[c+2])<<8) + resp[c+3]; |
| 67 | + int RDLENGTH = (((int)resp[c+8])<<8) + resp[c+9]; |
| 68 | + |
| 69 | + c+= 10; |
| 70 | + if ((CLASS == 1) && (TYPE == 1)) { |
| 71 | + sprintf(adr,"%d.%d.%d.%d", resp[c], resp[c+1], resp[c+2], resp[c+3]); |
| 72 | + c+= RDLENGTH; |
| 73 | + return true; |
| 74 | + } |
| 75 | + c+= RDLENGTH; |
| 76 | + |
| 77 | + return false; |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +static bool resolve(unsigned char *resp, char *ipaddress) |
| 82 | +{ |
| 83 | + |
| 84 | + int ID = (((int)resp[0]) <<8) + resp[1]; |
| 85 | + int QR = resp[2] >>7; |
| 86 | + int Opcode = (resp[2]>>3) & 0x0F; |
| 87 | + int RCODE = (resp[3] & 0x0F); |
| 88 | + int ANCOUNT = (((int)resp[6])<<8)+ resp[7]; |
| 89 | + |
| 90 | + if ((ID != 1) || (QR != 1) || (Opcode != 0) || (RCODE != 0)) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + int c = 12; |
| 95 | + int d; |
| 96 | + // Skip domain question |
| 97 | + while( (d=resp[c++]) != 0) { |
| 98 | + c+=d; |
| 99 | + } |
| 100 | + c+= 4; // skip QTYPE and QCLASS |
| 101 | + |
| 102 | + // Here comes the resource record |
| 103 | + for (int ans = 0 ; ans < ANCOUNT; ans++) { |
| 104 | + if (parseRR(resp, c, ipaddress)) { |
| 105 | + return true; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return false; |
| 110 | +} |
| 111 | + |
| 112 | +static int32_t query(UDPSocket *socket, const SocketAddress &addr, const char *hostname, char *ipaddress) |
| 113 | +{ |
| 114 | + int len = 0; |
| 115 | + if (hostname == NULL) { |
| 116 | + return false; |
| 117 | + } |
| 118 | + len = strlen(hostname); |
| 119 | + if ((len > 128) || (len == 0)) { |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + int packetlen = /* size of HEADER structure */ 12 + /* size of QUESTION Structure */5 + len + 1; |
| 124 | + uint8_t *packet = new uint8_t[packetlen]; /* this is the UDP packet to send to the DNS */ |
| 125 | + if (packet == NULL) { |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + // Fill the header |
| 130 | + memset(packet, 0, packetlen); |
| 131 | + packet[1] = 1; // ID = 1 |
| 132 | + packet[5] = 1; // QDCOUNT = 1 (contains one question) |
| 133 | + packet[2] = 1; // recursion requested |
| 134 | + |
| 135 | + int c = 13; // point to NAME element in question section or request |
| 136 | + int cnt = 12; // points to the counter of |
| 137 | + packet[cnt] = 0; |
| 138 | + for (int i = 0 ; i < len ; i++) { |
| 139 | + if (hostname[i] != '.') { |
| 140 | + // Copy the character and increment the character counter |
| 141 | + packet[cnt]++; |
| 142 | + packet[c++] = hostname[i]; |
| 143 | + } else { |
| 144 | + // Finished with this part, so go to the next |
| 145 | + cnt = c++; |
| 146 | + packet[cnt] = 0; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + // Terminate this domain name with a zero entry |
| 151 | + packet[c++] = 0; |
| 152 | + |
| 153 | + // Set QTYPE |
| 154 | + packet[c++] = 0; |
| 155 | + packet[c++] = 1; |
| 156 | + // Set QCLASS |
| 157 | + packet[c++] = 0; |
| 158 | + packet[c++] = 1; |
| 159 | + |
| 160 | + |
| 161 | + if (socket->sendto(addr, packet, packetlen) < 0) { |
| 162 | + delete packet; |
| 163 | + return false; |
| 164 | + } |
| 165 | + delete packet; |
| 166 | + |
| 167 | + packet = new uint8_t [1024]; |
| 168 | + |
| 169 | + // Receive the answer from DNS |
| 170 | + int response_length = 0; |
| 171 | + response_length = socket->recvfrom(NULL, packet, 1024); |
| 172 | + |
| 173 | + if (response_length > 0 ) { |
| 174 | + if (!resolve(packet, ipaddress)) { |
| 175 | + delete packet; |
| 176 | + return NSAPI_ERROR_DNS_FAILURE; |
| 177 | + } |
| 178 | + |
| 179 | + // cleanup and return |
| 180 | + delete packet; |
| 181 | + return 0; |
| 182 | + } |
| 183 | + |
| 184 | + delete packet; |
| 185 | + return NSAPI_ERROR_DNS_FAILURE; |
| 186 | +} |
| 187 | + |
| 188 | +int32_t dnsQuery(NetworkStack *iface, const char *host, char *ip) |
| 189 | +{ |
| 190 | + if (isIP(host)) { |
| 191 | + strcpy(ip, host); |
| 192 | + return 0; |
| 193 | + } |
| 194 | + |
| 195 | + UDPSocket sock(iface); |
| 196 | + |
| 197 | + for (unsigned i = 0; i < DNS_COUNT; i++) { |
| 198 | + return query(&sock, SocketAddress(DNS_IPS[0], 53), host, ip); |
| 199 | + } |
| 200 | + |
| 201 | + return NSAPI_ERROR_DNS_FAILURE; |
| 202 | +} |
| 203 | + |
| 204 | +int32_t dnsQuery(UDPSocket *socket, const char *host, char *ip) |
| 205 | +{ |
| 206 | + if (isIP(host)) { |
| 207 | + strcpy(ip, host); |
| 208 | + return 0; |
| 209 | + } |
| 210 | + |
| 211 | + for (unsigned i = 0; i < DNS_COUNT; i++) { |
| 212 | + return query(socket, SocketAddress(DNS_IPS[0], 53), host, ip); |
| 213 | + } |
| 214 | + |
| 215 | + return NSAPI_ERROR_DNS_FAILURE; |
| 216 | +} |
0 commit comments