Library to resolve text URLs to IP addresses (IPv4)
Dependents: NetworkSocketAPI NetworkSocketAPI Nucleo-AWS-IoT-mbed
Diff: DnsQuery.cpp
- Revision:
- 12:e23ef3dab4b9
- Parent:
- 10:0fe2c42a0261
- Child:
- 13:9c6e83b0ae7c
--- a/DnsQuery.cpp Mon Feb 29 15:05:24 2016 +0000 +++ b/DnsQuery.cpp Mon Feb 29 15:19:32 2016 +0000 @@ -19,23 +19,6 @@ #include <stdio.h> #include <string.h> -//Debug is disabled by default -#if 0 -#define DBG(x, ...) printf("[DNS : DBG]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); -#define WARN(x, ...) printf("[DNS : WARN]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); -#define ERR(x, ...) printf("[DNS : ERR]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); -#else -#define DBG(x, ...) //wait_us(10); -#define WARN(x, ...) //wait_us(10); -#define ERR(x, ...) -#endif - -#if 0 -#define INFO(x, ...) printf("[DNS : INFO]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); -#else -#define INFO(x, ...) -#endif - #define DNS_COUNT (sizeof DNS_IPS / sizeof DNS_IPS[0]) const char *DNS_IPS[] = { @@ -74,7 +57,8 @@ c++; break; } else { - c+= n; // skip this segment, not interested in string domain names + // skip this segment, not interested in string domain names + c+= n; } } @@ -82,7 +66,6 @@ int CLASS = (((int)resp[c+2])<<8) + resp[c+3]; int RDLENGTH = (((int)resp[c+8])<<8) + resp[c+9]; - INFO("Record of TYPE=%d and CLASS=%d detected !", TYPE, CLASS); c+= 10; if ((CLASS == 1) && (TYPE == 1)) { sprintf(adr,"%d.%d.%d.%d", resp[c], resp[c+1], resp[c+2], resp[c+3]); @@ -105,9 +88,7 @@ int RCODE = (resp[3] & 0x0F); int ANCOUNT = (((int)resp[6])<<8)+ resp[7]; - INFO("Resolving response : ID = %d, QR = %d, Opcode = %d, RCODE = %d", ID, QR, Opcode, RCODE); if ((ID != 1) || (QR != 1) || (Opcode != 0) || (RCODE != 0)) { - ERR("Received non matching response from DNS !"); return false; } @@ -161,7 +142,6 @@ int32_t dnsQuery(UDPSocket *socket, const char *hostname, char *ipaddress) { - INFO("%s", hostname); int len = 0; if (hostname == NULL) return false; @@ -205,13 +185,7 @@ packet[c++] = 0; packet[c++] = 1; - - INFO("Sending packet of length %d",packetlen); - /* - for( int i = 0 ; i < c ; i++) { - printf("%02x ", packet[i]); - } - */ + if (socket->send(packet, packetlen) < 0) { delete packet; return false; @@ -222,28 +196,19 @@ // Receive the answer from DNS int response_length = 0; - INFO("Recieving"); response_length = socket->recv(packet, 1024); - /* - for( int i = 0 ; i < 1024; i++) { - printf("%02x ", packet[i]); - } - */ + if (response_length > 0 ) { if (!resolve(packet, ipaddress)) { delete packet; - ERR("NO IP FOUND\n"); return NS_ERROR_DNS_FAILURE; } // cleanup and return delete packet; return 0; - } else { - ERR("SocketRecvFrom returned %d !", response_length); } delete packet; - ERR("NO IP FOUND\n"); return NS_ERROR_DNS_FAILURE; }