Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: HelloESP8266Interface_mine
Fork of DnsQuery by
Diff: DnsQuery.cpp
- Revision:
- 2:12d08f0f20cf
- Parent:
- 1:5d978992a518
- Child:
- 3:5705fdae6185
--- a/DnsQuery.cpp Fri Jul 24 22:43:25 2015 +0000
+++ b/DnsQuery.cpp Wed Aug 05 21:58:57 2015 +0000
@@ -42,22 +42,18 @@
socket = sock;
}
-bool DnsQuery::gethostbyname(const char* hostname, IPADDRESS_t &ipaddress)
+bool DnsQuery::gethostbyname(const char* hostname, char* resolvedIp)
{
- IPADDRESS_t dnsIp_1 = {209,244,0,3};
- IPADDRESS_t dnsIp_2 = {8,8,8,8};
- IPADDRESS_t dnsIp_3 = {84,200,69,80};
- IPADDRESS_t dnsIp_4 = {8,26,56,26};
- IPADDRESS_t dnsIp_5 = {208,67,222,222};
+ char * dnsIPs[] = {
+ "8.8.8.8",
+ "209.244.0.3",
+ "84.200.69.80",
+ "8.26.56.26",
+ "208.67.222.222"
+ };
for(int i = 0; i<5; i++){
- switch(i) {
- case 0 : _dnsip= dnsIp_1; break ;
- case 1 : _dnsip= dnsIp_2; break ;
- case 2 : _dnsip= dnsIp_3; break ;
- case 3 : _dnsip= dnsIp_4; break ;
- case 4 : _dnsip= dnsIp_5; break ;
- }
- if(this->getIP(hostname, ipaddress)){
+ _dnsip = dnsIPs[i];
+ if(this->getIP(hostname, resolvedIp)){
return true;
}
@@ -66,7 +62,7 @@
}
-bool DnsQuery::getIP(const char* hostname, IPADDRESS_t &ipaddress)
+bool DnsQuery::getIP(const char* hostname, char* ipaddress)
{
INFO("%s", hostname);
int len = 0;
@@ -75,7 +71,6 @@
len = strlen(hostname);
if ((len >128) || (len == 0))
return false;
- memset (&ipaddress, 0, sizeof(ipaddress));
int packetlen = /* size of HEADER structure */ 12 + /* size of QUESTION Structure */5 + len + 1;
char *packet = new char[packetlen]; /* this is the UDP packet to send to the DNS */
@@ -115,7 +110,7 @@
// Ready to send to DNS
- socket->setAddressPort(_dnsip.string_format(), 53);
+ socket->setAddressPort(_dnsip, 53);
socket->open();
INFO("Sending packet of length %d",packetlen);
@@ -128,40 +123,34 @@
delete packet;
return false;
}
-
delete packet;
packet = new char [1024];
- if (packet == NULL) {
- return false;
- }
// Receive the answer from DNS
- Timer t;
- t.start();
int response_length = 0;
- while (t.read_ms() < 10000) {
- INFO("Recieving");
- response_length = socket->recv(packet, 1024);
+ 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)) {
- break;
- }
-
- // cleanup and return
+ if (response_length > 0 ) {
+ if (!resolve(packet, ipaddress)) {
+ socket->close();
delete packet;
- socket->close();
- //INFO("Found this IP:%s",ipaddress.string_format());
- return true;
- } else {
- ERR("SocketRecvFrom returned %d !", response_length);
+ ERR("NO IP FOUND\n");
+ return false;
}
- }
+
+ // cleanup and return
+ delete packet;
+ socket->close();
+ return true;
+ } else {
+ ERR("SocketRecvFrom returned %d !", response_length);
+ }
socket->close();
delete packet;
ERR("NO IP FOUND\n");
@@ -169,7 +158,7 @@
}
-bool DnsQuery::resolve(char* resp, IPADDRESS_t &ipaddress)
+bool DnsQuery::resolve(char* resp, char* ipaddress)
{
int ID = (((int)resp[0]) <<8) + resp[1];
@@ -202,10 +191,9 @@
return false;
}
-bool DnsQuery::parseRR(char *resp, int& c, IPADDRESS_t& adr )
+bool DnsQuery::parseRR(char *resp, int& c, char* adr )
{
int n = 0;
-
while( (n=resp[c++]) != 0) {
if ((n & 0xc0) != 0) {
// This is a link
@@ -223,10 +211,7 @@
INFO("Record of TYPE=%d and CLASS=%d detected !", TYPE, CLASS);
c+= 10;
if ((CLASS == 1) && (TYPE == 1)) {
- adr.sin_addr.o1 = resp[c];
- adr.sin_addr.o2 = resp[c+1];
- adr.sin_addr.o3 = resp[c+2];
- adr.sin_addr.o4 = resp[c+3];
+ sprintf(adr,"%d.%d.%d.%d", resp[c], resp[c+1], resp[c+2], resp[c+3]);
c+= RDLENGTH;
return true;
} else {
