Fix ping of IP addresses (don't do DNS lookup)
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 4 May 2011 09:59:43 +0000 (11:59 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 4 May 2011 09:59:51 +0000 (11:59 +0200)
src/dns/dnsresolver.cpp
src/dns/dnsresolver.h

index 1aba3f2..a649c13 100644 (file)
@@ -40,6 +40,37 @@ DnsResolver::~DnsResolver()
 }
 
 /**
+ * @brief Check if hostname is already an IPv4 address.
+ *            Will insert it into #ResolvedHostAddressList.
+ *
+ * @param host_dns_address
+ * @return bool True if already an ip, false if not
+ **/
+bool DnsResolver::handle_ip_address()
+{
+    struct in_addr dummy_conversion;
+
+    // Convert string to IP address
+    // Note: IPv4 only
+    if (inet_aton(HostDnsAddress.c_str(), &dummy_conversion) != 0)
+    {
+        std::list<HostAddress> new_host_list;
+
+        HostAddress ip_host( HostDnsAddress,  86400 * 365);         // set fake TTL to one year
+        new_host_list.push_back( ip_host );
+
+        // Activate new host list
+        ResolvedHostAddressList.swap(new_host_list);
+
+        GlobalLogger.info() << "host is already an IP: " << HostDnsAddress << endl;
+
+        return true;
+    }
+
+    return false;
+}
+
+/**
  * Resolve the IPs from this DNS and build a list of these IPs.
  *
  * @return true if the host address could be resolved, or false otherwise.
@@ -49,6 +80,10 @@ bool DnsResolver::resolve()
     BOOST_ASSERT( !HostDnsAddress.empty() );
     BOOST_ASSERT( !NameServer.empty() );
 
+    // Handle IP addresses in HostDnsAddress
+    if (handle_ip_address())
+        return true;
+
     GlobalLogger.info() << "Resolved IP(s) for host : "
             << HostDnsAddress << endl;
 
index 6c46218..808e138 100644 (file)
@@ -37,6 +37,7 @@ private:
             std::list<HostAddress> *resolved_host_address_list
     );
 
+    bool handle_ip_address();
 private:
     /// the list of IPs available to the host DNS
     std::list<HostAddress> ResolvedHostAddressList;