DnsQuery for NetworkSocketAPI

Fork of DnsQuery by NetworkSocketAPI

Committer:
sarahmarshy
Date:
Wed Aug 05 21:58:57 2015 +0000
Revision:
2:12d08f0f20cf
Parent:
1:5d978992a518
Child:
3:5705fdae6185
Removed ipAdress struct. Compatible with NetworkSocket API

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 "mbed.h"
sarahmarshy 0:fff4b9055396 19 #include "DnsQuery.h"
sarahmarshy 0:fff4b9055396 20
sarahmarshy 0:fff4b9055396 21 //Debug is disabled by default
sarahmarshy 0:fff4b9055396 22 #if 0
sarahmarshy 0:fff4b9055396 23 #define DBG(x, ...) printf("[DNS : DBG]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
sarahmarshy 0:fff4b9055396 24 #define WARN(x, ...) printf("[DNS : WARN]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
sarahmarshy 0:fff4b9055396 25 #define ERR(x, ...) printf("[DNS : ERR]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
sarahmarshy 0:fff4b9055396 26 #else
sarahmarshy 0:fff4b9055396 27 #define DBG(x, ...) //wait_us(10);
sarahmarshy 0:fff4b9055396 28 #define WARN(x, ...) //wait_us(10);
sarahmarshy 0:fff4b9055396 29 #define ERR(x, ...)
sarahmarshy 0:fff4b9055396 30 #endif
sarahmarshy 0:fff4b9055396 31
sarahmarshy 0:fff4b9055396 32 #if 0
sarahmarshy 0:fff4b9055396 33 #define INFO(x, ...) printf("[DNS : INFO]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
sarahmarshy 0:fff4b9055396 34 #else
sarahmarshy 0:fff4b9055396 35 #define INFO(x, ...)
sarahmarshy 0:fff4b9055396 36 #endif
sarahmarshy 0:fff4b9055396 37
sarahmarshy 0:fff4b9055396 38
sarahmarshy 0:fff4b9055396 39
sarahmarshy 1:5d978992a518 40 DnsQuery::DnsQuery(SocketInterface *sock)
sarahmarshy 0:fff4b9055396 41 {
sarahmarshy 1:5d978992a518 42 socket = sock;
sarahmarshy 0:fff4b9055396 43 }
sarahmarshy 0:fff4b9055396 44
sarahmarshy 2:12d08f0f20cf 45 bool DnsQuery::gethostbyname(const char* hostname, char* resolvedIp)
sarahmarshy 0:fff4b9055396 46 {
sarahmarshy 2:12d08f0f20cf 47 char * dnsIPs[] = {
sarahmarshy 2:12d08f0f20cf 48 "8.8.8.8",
sarahmarshy 2:12d08f0f20cf 49 "209.244.0.3",
sarahmarshy 2:12d08f0f20cf 50 "84.200.69.80",
sarahmarshy 2:12d08f0f20cf 51 "8.26.56.26",
sarahmarshy 2:12d08f0f20cf 52 "208.67.222.222"
sarahmarshy 2:12d08f0f20cf 53 };
sarahmarshy 0:fff4b9055396 54 for(int i = 0; i<5; i++){
sarahmarshy 2:12d08f0f20cf 55 _dnsip = dnsIPs[i];
sarahmarshy 2:12d08f0f20cf 56 if(this->getIP(hostname, resolvedIp)){
sarahmarshy 0:fff4b9055396 57 return true;
sarahmarshy 0:fff4b9055396 58 }
sarahmarshy 0:fff4b9055396 59
sarahmarshy 0:fff4b9055396 60 }
sarahmarshy 0:fff4b9055396 61 return false;
sarahmarshy 0:fff4b9055396 62
sarahmarshy 0:fff4b9055396 63 }
sarahmarshy 0:fff4b9055396 64
sarahmarshy 2:12d08f0f20cf 65 bool DnsQuery::getIP(const char* hostname, char* ipaddress)
sarahmarshy 0:fff4b9055396 66 {
sarahmarshy 0:fff4b9055396 67 INFO("%s", hostname);
sarahmarshy 0:fff4b9055396 68 int len = 0;
sarahmarshy 0:fff4b9055396 69 if (hostname == NULL)
sarahmarshy 0:fff4b9055396 70 return false;
sarahmarshy 0:fff4b9055396 71 len = strlen(hostname);
sarahmarshy 0:fff4b9055396 72 if ((len >128) || (len == 0))
sarahmarshy 0:fff4b9055396 73 return false;
sarahmarshy 0:fff4b9055396 74
sarahmarshy 0:fff4b9055396 75 int packetlen = /* size of HEADER structure */ 12 + /* size of QUESTION Structure */5 + len + 1;
sarahmarshy 0:fff4b9055396 76 char *packet = new char[packetlen]; /* this is the UDP packet to send to the DNS */
sarahmarshy 0:fff4b9055396 77 if (packet == NULL)
sarahmarshy 0:fff4b9055396 78 return false;
sarahmarshy 0:fff4b9055396 79
sarahmarshy 0:fff4b9055396 80 // Fill the header
sarahmarshy 0:fff4b9055396 81 memset(packet, 0, packetlen);
sarahmarshy 0:fff4b9055396 82 packet[1] = 1; // ID = 1
sarahmarshy 0:fff4b9055396 83 packet[5] = 1; // QDCOUNT = 1 (contains one question)
sarahmarshy 0:fff4b9055396 84 packet[2] = 1; // recursion requested
sarahmarshy 0:fff4b9055396 85
sarahmarshy 0:fff4b9055396 86 int c = 13; // point to NAME element in question section or request
sarahmarshy 0:fff4b9055396 87 int cnt = 12; // points to the counter of
sarahmarshy 0:fff4b9055396 88 packet[cnt] = 0;
sarahmarshy 0:fff4b9055396 89 for (int i = 0 ; i < len ; i++) {
sarahmarshy 0:fff4b9055396 90 if (hostname[i] != '.') {
sarahmarshy 0:fff4b9055396 91 // Copy the character and increment the character counter
sarahmarshy 0:fff4b9055396 92 packet[cnt]++;
sarahmarshy 0:fff4b9055396 93 packet[c++] = hostname[i];
sarahmarshy 0:fff4b9055396 94 } else {
sarahmarshy 0:fff4b9055396 95 // Finished with this part, so go to the next
sarahmarshy 0:fff4b9055396 96 cnt = c++;
sarahmarshy 0:fff4b9055396 97 packet[cnt] = 0;
sarahmarshy 0:fff4b9055396 98 }
sarahmarshy 0:fff4b9055396 99 }
sarahmarshy 0:fff4b9055396 100
sarahmarshy 0:fff4b9055396 101 // Terminate this domain name with a zero entry
sarahmarshy 0:fff4b9055396 102 packet[c++] = 0;
sarahmarshy 0:fff4b9055396 103
sarahmarshy 0:fff4b9055396 104 // Set QTYPE
sarahmarshy 0:fff4b9055396 105 packet[c++] = 0;
sarahmarshy 0:fff4b9055396 106 packet[c++] = 1;
sarahmarshy 0:fff4b9055396 107 // Set QCLASS
sarahmarshy 0:fff4b9055396 108 packet[c++] = 0;
sarahmarshy 0:fff4b9055396 109 packet[c++] = 1;
sarahmarshy 0:fff4b9055396 110
sarahmarshy 0:fff4b9055396 111
sarahmarshy 0:fff4b9055396 112 // Ready to send to DNS
sarahmarshy 2:12d08f0f20cf 113 socket->setAddressPort(_dnsip, 53);
sarahmarshy 1:5d978992a518 114 socket->open();
sarahmarshy 0:fff4b9055396 115
sarahmarshy 1:5d978992a518 116 INFO("Sending packet of length %d",packetlen);
sarahmarshy 1:5d978992a518 117 /*
sarahmarshy 1:5d978992a518 118 for( int i = 0 ; i < c ; i++) {
sarahmarshy 1:5d978992a518 119 printf("%02x ", packet[i]);
sarahmarshy 1:5d978992a518 120 }
sarahmarshy 1:5d978992a518 121 */
sarahmarshy 1:5d978992a518 122 if (socket->send(packet, packetlen) < 0) {
sarahmarshy 0:fff4b9055396 123 delete packet;
sarahmarshy 0:fff4b9055396 124 return false;
sarahmarshy 0:fff4b9055396 125 }
sarahmarshy 0:fff4b9055396 126 delete packet;
sarahmarshy 0:fff4b9055396 127
sarahmarshy 0:fff4b9055396 128 packet = new char [1024];
sarahmarshy 0:fff4b9055396 129
sarahmarshy 0:fff4b9055396 130 // Receive the answer from DNS
sarahmarshy 0:fff4b9055396 131 int response_length = 0;
sarahmarshy 2:12d08f0f20cf 132 INFO("Recieving");
sarahmarshy 2:12d08f0f20cf 133 response_length = socket->recv(packet, 1024);
sarahmarshy 1:5d978992a518 134 /*
sarahmarshy 1:5d978992a518 135 for( int i = 0 ; i < 1024; i++) {
sarahmarshy 1:5d978992a518 136 printf("%02x ", packet[i]);
sarahmarshy 1:5d978992a518 137 }
sarahmarshy 1:5d978992a518 138 */
sarahmarshy 2:12d08f0f20cf 139 if (response_length > 0 ) {
sarahmarshy 2:12d08f0f20cf 140 if (!resolve(packet, ipaddress)) {
sarahmarshy 2:12d08f0f20cf 141 socket->close();
sarahmarshy 0:fff4b9055396 142 delete packet;
sarahmarshy 2:12d08f0f20cf 143 ERR("NO IP FOUND\n");
sarahmarshy 2:12d08f0f20cf 144 return false;
sarahmarshy 0:fff4b9055396 145 }
sarahmarshy 2:12d08f0f20cf 146
sarahmarshy 2:12d08f0f20cf 147 // cleanup and return
sarahmarshy 2:12d08f0f20cf 148 delete packet;
sarahmarshy 2:12d08f0f20cf 149 socket->close();
sarahmarshy 2:12d08f0f20cf 150 return true;
sarahmarshy 2:12d08f0f20cf 151 } else {
sarahmarshy 2:12d08f0f20cf 152 ERR("SocketRecvFrom returned %d !", response_length);
sarahmarshy 2:12d08f0f20cf 153 }
sarahmarshy 1:5d978992a518 154 socket->close();
sarahmarshy 0:fff4b9055396 155 delete packet;
sarahmarshy 0:fff4b9055396 156 ERR("NO IP FOUND\n");
sarahmarshy 0:fff4b9055396 157 return false;
sarahmarshy 0:fff4b9055396 158 }
sarahmarshy 0:fff4b9055396 159
sarahmarshy 0:fff4b9055396 160
sarahmarshy 2:12d08f0f20cf 161 bool DnsQuery::resolve(char* resp, char* ipaddress)
sarahmarshy 0:fff4b9055396 162 {
sarahmarshy 0:fff4b9055396 163
sarahmarshy 0:fff4b9055396 164 int ID = (((int)resp[0]) <<8) + resp[1];
sarahmarshy 0:fff4b9055396 165 int QR = resp[2] >>7;
sarahmarshy 0:fff4b9055396 166 int Opcode = (resp[2]>>3) & 0x0F;
sarahmarshy 0:fff4b9055396 167 int RCODE = (resp[3] & 0x0F);
sarahmarshy 0:fff4b9055396 168 int ANCOUNT = (((int)resp[6])<<8)+ resp[7];
sarahmarshy 0:fff4b9055396 169
sarahmarshy 0:fff4b9055396 170 INFO("Resolving response : ID = %d, QR = %d, Opcode = %d, RCODE = %d", ID, QR, Opcode, RCODE);
sarahmarshy 0:fff4b9055396 171 if ((ID != 1) || (QR != 1) || (Opcode != 0) || (RCODE != 0)) {
sarahmarshy 0:fff4b9055396 172 ERR("Received non matching response from DNS !");
sarahmarshy 0:fff4b9055396 173 return false;
sarahmarshy 0:fff4b9055396 174 }
sarahmarshy 0:fff4b9055396 175
sarahmarshy 0:fff4b9055396 176 int c = 12;
sarahmarshy 0:fff4b9055396 177 int d;
sarahmarshy 0:fff4b9055396 178 // Skip domain question
sarahmarshy 0:fff4b9055396 179 while( (d=resp[c++]) != 0) {
sarahmarshy 0:fff4b9055396 180 c+=d;
sarahmarshy 0:fff4b9055396 181 }
sarahmarshy 0:fff4b9055396 182 c+= 4; // skip QTYPE and QCLASS
sarahmarshy 0:fff4b9055396 183
sarahmarshy 0:fff4b9055396 184 // Here comes the resource record
sarahmarshy 0:fff4b9055396 185 for (int ans = 0 ; ans < ANCOUNT; ans++) {
sarahmarshy 0:fff4b9055396 186 if (parseRR(resp, c, ipaddress)) {
sarahmarshy 0:fff4b9055396 187 return true;
sarahmarshy 0:fff4b9055396 188 }
sarahmarshy 0:fff4b9055396 189 }
sarahmarshy 0:fff4b9055396 190
sarahmarshy 0:fff4b9055396 191 return false;
sarahmarshy 0:fff4b9055396 192 }
sarahmarshy 0:fff4b9055396 193
sarahmarshy 2:12d08f0f20cf 194 bool DnsQuery::parseRR(char *resp, int& c, char* adr )
sarahmarshy 0:fff4b9055396 195 {
sarahmarshy 0:fff4b9055396 196 int n = 0;
sarahmarshy 0:fff4b9055396 197 while( (n=resp[c++]) != 0) {
sarahmarshy 0:fff4b9055396 198 if ((n & 0xc0) != 0) {
sarahmarshy 0:fff4b9055396 199 // This is a link
sarahmarshy 0:fff4b9055396 200 c++;
sarahmarshy 0:fff4b9055396 201 break;
sarahmarshy 0:fff4b9055396 202 } else {
sarahmarshy 0:fff4b9055396 203 c+= n; // skip this segment, not interested in string domain names
sarahmarshy 0:fff4b9055396 204 }
sarahmarshy 0:fff4b9055396 205 }
sarahmarshy 0:fff4b9055396 206
sarahmarshy 0:fff4b9055396 207 int TYPE = (((int)resp[c])<<8) + resp[c+1];
sarahmarshy 0:fff4b9055396 208 int CLASS = (((int)resp[c+2])<<8) + resp[c+3];
sarahmarshy 0:fff4b9055396 209 int RDLENGTH = (((int)resp[c+8])<<8) + resp[c+9];
sarahmarshy 0:fff4b9055396 210
sarahmarshy 0:fff4b9055396 211 INFO("Record of TYPE=%d and CLASS=%d detected !", TYPE, CLASS);
sarahmarshy 0:fff4b9055396 212 c+= 10;
sarahmarshy 0:fff4b9055396 213 if ((CLASS == 1) && (TYPE == 1)) {
sarahmarshy 2:12d08f0f20cf 214 sprintf(adr,"%d.%d.%d.%d", resp[c], resp[c+1], resp[c+2], resp[c+3]);
sarahmarshy 0:fff4b9055396 215 c+= RDLENGTH;
sarahmarshy 0:fff4b9055396 216 return true;
sarahmarshy 0:fff4b9055396 217 } else {
sarahmarshy 0:fff4b9055396 218 }
sarahmarshy 0:fff4b9055396 219 c+= RDLENGTH;
sarahmarshy 0:fff4b9055396 220
sarahmarshy 0:fff4b9055396 221 return false;
sarahmarshy 0:fff4b9055396 222 }