Library to resolve text URLs to IP addresses (IPv4)

Dependents:   NetworkSocketAPI NetworkSocketAPI Nucleo-AWS-IoT-mbed

Committer:
geky
Date:
Mon Feb 29 15:19:32 2016 +0000
Revision:
12:e23ef3dab4b9
Parent:
10:0fe2c42a0261
Child:
13:9c6e83b0ae7c
Removed debug macros

Who changed what in which revision?

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