DnsQuery for NetworkSocketAPI

Fork of DnsQuery by NetworkSocketAPI

Revision:
1:5d978992a518
Parent:
0:fff4b9055396
Child:
2:12d08f0f20cf
--- a/DnsQuery.cpp	Tue Jul 14 20:48:12 2015 +0000
+++ b/DnsQuery.cpp	Fri Jul 24 22:43:25 2015 +0000
@@ -17,7 +17,6 @@
  */
 #include "mbed.h"
 #include "DnsQuery.h"
-#include "UDPSocket.h"
 
 //Debug is disabled by default
 #if 0
@@ -38,35 +37,27 @@
 
 
 
-DnsQuery::DnsQuery(IPADDRESS_t *dnsip)
+DnsQuery::DnsQuery(SocketInterface *sock)
 {
-    _dnsip.sin_addr.o1 = dnsip->sin_addr.o1;
-    _dnsip.sin_addr.o2 = dnsip->sin_addr.o2;
-    _dnsip.sin_addr.o3 = dnsip->sin_addr.o3;
-    _dnsip.sin_addr.o4 = dnsip->sin_addr.o4;
-    
-    INFO("Setting   DNS = %d.%d.%d.%d !", dnsip->sin_addr.o1, dnsip->sin_addr.o2, dnsip->sin_addr.o3, dnsip->sin_addr.o4);
-    INFO("Accepting DNS = %s !",_dnsip.string_format());
+    socket = sock;
 }
 
-bool DnsQuery::gethostbyname(char* hostname, IPADDRESS_t &ipaddress)
+bool DnsQuery::gethostbyname(const char* hostname, IPADDRESS_t &ipaddress)
 {
     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};
-    IPADDRESS_t dnsIp;
     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 ;  
+            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 ;  
         }
-        DnsQuery dns = DnsQuery(&dnsIp);
-        if(dns.getIP(hostname, ipaddress)){
+        if(this->getIP(hostname, ipaddress)){
             return true;   
         }
            
@@ -75,7 +66,7 @@
     
 }
 
-bool DnsQuery::getIP(char* hostname, IPADDRESS_t &ipaddress)
+bool DnsQuery::getIP(const char* hostname, IPADDRESS_t &ipaddress)
 {
     INFO("%s", hostname);
     int len = 0;
@@ -124,16 +115,16 @@
     
     
     //  Ready to send to DNS
-    UDPSocket sock = UDPSocket();
-    Endpoint e = Endpoint();
-    //INFO("Using IP: %s",_dnsip.string_format());
-    char ip[7];
-    sprintf(ip,"%s",_dnsip.string_format());
-    e.set_address(ip, 53);
-    delete ip;
+    socket->setAddressPort(_dnsip.string_format(), 53);
+    socket->open();
    
-    INFO("Sending packet"); 
-    if (sock.sendTo(e, packet, packetlen) < 0) {
+    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;
     }
@@ -151,30 +142,27 @@
     int response_length = 0;
     while (t.read_ms() < 10000) {
         INFO("Recieving");
-        response_length = sock.receiveFrom(e, packet, 1024);
+        response_length = socket->recv(packet, 1024);
+        /*
+        for( int i = 0 ; i < 1024; i++) {
+            printf("%02x ", packet[i]);
+        }
+        */
         if (response_length > 0 ) {
-            int offset = packetlen;
-            for(int i = offset; i < 1024; i++) {
-                if(0x3A==packet[i]){
-                    offset = i+1;
-                    break;
-                }
-            }
-            char *offset_packet = packet + offset;
-            if (!resolve(offset_packet, ipaddress)) {
+            if (!resolve(packet, ipaddress)) {
                 break;
             }
                         
             //  cleanup and return
             delete packet;
-            sock.close();
+            socket->close();
             //INFO("Found this IP:%s",ipaddress.string_format());
             return true;
         } else {
             ERR("SocketRecvFrom returned %d !", response_length);
         }
     }    
-    sock.close();
+    socket->close();
     delete packet;
     ERR("NO IP FOUND\n");
     return false;