Using boost::asio::ip::address class instead of std::string in the HostAddress.
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 6 Mar 2012 10:45:38 +0000 (07:45 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 6 Mar 2012 10:45:38 +0000 (07:45 -0300)
- That class provides more functionality, so keep it until someone requires a string.

src/dns/dnsresolver.cpp
src/dns/hostaddress.cpp
src/dns/hostaddress.h

index 21f7f31..4ef6969 100644 (file)
@@ -35,6 +35,8 @@ on this file might be covered by the GNU General Public License.
 using namespace std;
 using boost::asio::io_service;
 using boost::asio::ip::address;
+using boost::asio::ip::address_v4;
+using boost::asio::ip::address_v6;
 using boost::net::dns::a_resource;
 using boost::net::dns::a6_resource;
 using boost::net::dns::resource_base_t;
@@ -109,8 +111,8 @@ bool DnsResolver::resolve()
     }
     catch ( const std::exception &ex )
     {
-        GlobalLogger.error() << "Error: host " << HostDnsAddress
-                << " could not be resolved. " << ex.what() << endl;
+        GlobalLogger.error() << "Error: host " << HostDnsAddress << " could not be resolved. "
+                << ex.what() << endl;
         return false;
     }
 
@@ -146,7 +148,7 @@ string DnsResolver::get_next_ip()
     size_t list_size_before = ResolvedHostAddressList.size();
 
     HostAddress host_address = ResolvedHostAddressList.front();
-    string destination_ip = host_address.get_ip();
+    string destination_ip = host_address.get_ip().to_string();
     uint32_t ttl = host_address.get_ttl().get_updated_value();
 
     rotate( ResolvedHostAddressList.begin(), ++ResolvedHostAddressList.begin(), ResolvedHostAddressList.end() );
@@ -201,7 +203,7 @@ bool DnsResolver::handle_ip_address()
         {
             std::list<HostAddress> new_host_list;
 
-            HostAddress ip_host( HostDnsAddress,  86400 * 365 ); // set fake TTL to one year
+            HostAddress ip_host( addr,  86400 * 365 ); // set fake TTL to one year
             new_host_list.push_back( ip_host );
 
             // Activate new host list
@@ -286,7 +288,7 @@ void DnsResolver::append_resolved_ipv4(
     a_resource *a_rr = dynamic_cast<a_resource *> ( resource_record.get() );
     if ( a_rr != NULL )
     {
-        string current_ip = a_rr->address().to_string();
+        address_v4 current_ip = a_rr->address();
         uint32_t current_ttl = a_rr->ttl();
 
         HostAddress resolved_host( current_ip, current_ttl );
@@ -306,7 +308,7 @@ void DnsResolver::append_resolved_ipv6(
     a6_resource *a6_rr = dynamic_cast<a6_resource *> ( resource_record.get() );
     if ( a6_rr != NULL )
     {
-        string current_ip = a6_rr->address().to_string();
+        address_v6 current_ip = a6_rr->address();
         uint32_t current_ttl = a6_rr->ttl();
 
         HostAddress resolved_host( current_ip, current_ttl );
index 9f1f6aa..099ccc3 100644 (file)
@@ -22,19 +22,20 @@ on this file might be covered by the GNU General Public License.
 #include <boost/assert.hpp>
 
 using namespace std;
+using boost::asio::ip::address;
 
 //-----------------------------------------------------------------------------
 // HostAddress
 //-----------------------------------------------------------------------------
 
 HostAddress::HostAddress() :
-    Ip( "" ),
+    Ip(),
     Ttl( 0 )
 {
 }
 
 HostAddress::HostAddress(
-        const string &ip,
+        const address &ip,
         uint32_t ttl
 ) :
     Ip( ip ),
@@ -46,14 +47,14 @@ HostAddress::~HostAddress()
 {
 }
 
-string HostAddress::get_ip() const
+address HostAddress::get_ip() const
 {
     return Ip;
 }
 
-void HostAddress::set_ip( const string &ip )
+void HostAddress::set_ip( const address &ip )
 {
-    BOOST_ASSERT( !ip.empty() );
+    BOOST_ASSERT( ip.is_unspecified() );
 
     Ip = ip;
 }
index 603630e..2a160e6 100644 (file)
@@ -23,7 +23,8 @@ on this file might be covered by the GNU General Public License.
 #include <stdint.h>
 
 #include <list>
-#include <string>
+
+#include <boost/asio.hpp>
 
 #include "dns/timetolive.h"
 
@@ -36,20 +37,20 @@ class HostAddress
 public:
     HostAddress();
     HostAddress(
-            const std::string &ip,
+            const boost::asio::ip::address &ip,
             uint32_t ttl
     );
     ~HostAddress();
 
-    std::string get_ip() const;
-    void set_ip( const std::string &ip );
+    boost::asio::ip::address get_ip() const;
+    void set_ip( const boost::asio::ip::address &ip );
 
     TimeToLive get_ttl() const;
     void set_ttl( const TimeToLive &ttl );
 
 private:
     /// IP address of the host
-    std::string Ip;
+    boost::asio::ip::address Ip;
     /// time-to-live of the host IP
     TimeToLive Ttl;