Sarah Marsh / DnsQuery

Dependents:   TempTower

Files at this revision

API Documentation at this revision

Comitter:
sarahmarshy
Date:
Wed Aug 05 21:58:57 2015 +0000
Parent:
1:5d978992a518
Child:
3:5705fdae6185
Commit message:
Removed ipAdress struct. Compatible with NetworkSocket API

Changed in this revision

DnsQuery.cpp Show annotated file Show diff for this revision Revisions of this file
DnsQuery.h Show annotated file Show diff for this revision Revisions of this file
--- 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 {
--- a/DnsQuery.h	Fri Jul 24 22:43:25 2015 +0000
+++ b/DnsQuery.h	Wed Aug 05 21:58:57 2015 +0000
@@ -1,4 +1,4 @@
-/* Copyright (c) 2013 Henry Leinen (henry[dot]leinen [at] online [dot] de)
+/* 
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  * and associated documentation files (the "Software"), to deal in the Software without restriction,
@@ -19,70 +19,33 @@
 #define __DNSQUERY_H__
 
 #include "SocketInterface.h"
-
-typedef struct {
-    union {
-        struct {
-            char o1, o2, o3, o4;
-        };
-        char o[4];
-    } sin_addr;
-    operator char* () { return (char*)this; }
-    char* string_format() { 
-        char ip[7];
-        sprintf(ip,"%d.%d.%d.%d", this->sin_addr.o1, this->sin_addr.o2, this->sin_addr.o3, this->sin_addr.o4);
-        return ip;
-    }
-} IPADDRESS_t;
-
-/** Class DnsQuery implements DNS over UDP functionality.
-  * Example as a typical use case :
-  * @code
-  * #include "mbed.h"
-  * #include "DnsQuery.h"
-  *
-  * void main(void)
-  * {
-  *     IPADDRESS_t ipAddress;  // will receive the ip address of the host
-  *     IPADDRESS_t dnsIp = { 192, 168, 178, 1 };      // Ip Address of the DNS server
-  *     
-  *     DnsQuery dns(Wifi::getInstance(), &dnsIp);
-  *     if (dns.gethostbyname("mbed.org", ipAddress)) {
-  *         printf("Ip-Address of mbed.org is %d.%d.%d.%d\n", ipAddress.sin_addr.o1, ipAddress.sin_addr.o2, ipAddress.sin_addr.o3, ipAddress.sin_addr.o4);
-  *     } else {
-  *         printf("Unable to obtain IP-Address\n");
-  *     }
-  * }
-  * @endcode
-  */
 class DnsQuery
 {
 public:
     /** Constructor to instantiate a DnsQuery object.
-      * @param wifi : A valid pointer to a Wifi Object, which can be used to obtain a UDP socket object.
-      * @param dnsip : A valid pointer which holds the IPAddress of the DNS server to query.
+      * @param wifi : A valid pointer to a UDP socket
       */
     DnsQuery(SocketInterface* sock);
     
     
     /** Function gethostbyname implements the functionality to query a domain name server for an IP-Address of a given hostname.
-      * @param hostname : the hostname of interest as a string.
+      * @param hostname : the hostname of interest as a string - format must be without http:// or www. IE google.com, mbed.org, etc
       * @param ipaddress : a reference to a IPADDRESS_t object which will receive the resolved IP Address of the host in question.
-      * @returns true if successfull, or false otherwise.
+      * @returns true if successful, or false otherwise.
       */
-    bool gethostbyname(const char* hostname, IPADDRESS_t &ipaddress);
+    bool gethostbyname(const char* hostname, char* ipaddress);
     /** Function gethostbyname implements the functionality to query a domain name server for an IP-Address of a given hostname.
       * @param hostname : the hostname of interest as a string.
       * @param ipaddress : a reference to a IPADDRESS_t object which will receive the resolved IP Address of the host in question.
-      * @returns true if successfull, or false otherwise.
+      * @returns true if successful, or false otherwise.
       */
-    bool getIP(const char* hostname, IPADDRESS_t &ipaddress);
+    bool getIP(const char* hostname, char* ipaddress);
 protected:
-    bool resolve(char* resp, IPADDRESS_t &ipaddress);
-    bool parseRR(char *resp, int& c, IPADDRESS_t& adr );
+    bool resolve(char* resp, char* ipaddress);
+    bool parseRR(char *resp, int& c, char* adr );
 
 protected:
-    IPADDRESS_t _dnsip;
+    char* _dnsip;
     char* _string_ip;
     SocketInterface* socket;