Refactor: moved the 'A' resource record filling to a dedicated method.
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Thu, 23 Feb 2012 02:37:21 +0000 (00:37 -0200)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Thu, 23 Feb 2012 02:37:21 +0000 (00:37 -0200)
src/dns/dnsresolver.cpp
src/dns/dnsresolver.h

index eebd9c8..219c35c 100644 (file)
@@ -21,6 +21,7 @@ on this file might be covered by the GNU General Public License.
 
 #include <iostream>
 
+#include <boost/assert.hpp>
 #include <boost/asio.hpp>
 #include <boost/foreach.hpp>
 #include <boost/net/resolve.hpp>
@@ -101,7 +102,6 @@ bool DnsResolver::resolve()
         ResolvedHostAddressList.clear();
 
         fill_resolved_ip_list( answers_list, &ResolvedHostAddressList );
-
     }
     catch ( const std::exception &ex )
     {
@@ -244,22 +244,17 @@ void DnsResolver::fill_resolved_ip_list(
 
     BOOST_FOREACH( shared_ptr<resource_base_t> resource_record, answers_list )
     {
-        if ( resource_record &&
-             ( resource_record->rtype() == boost::net::dns::type_a ) )
+        if ( resource_record )
         {
-            a_resource *a_rr = dynamic_cast<a_resource *> (
-                    resource_record.get()
-            );
-            if ( a_rr != NULL )
+            switch ( resource_record->rtype() )
             {
-                string current_ip = a_rr->address().to_string();
-                uint32_t current_ttl = a_rr->ttl();
-
-                HostAddress resolved_host( current_ip, current_ttl );
-                resolved_host_address_list->push_back( resolved_host );
-
-                GlobalLogger.info() << "- " << current_ip
-                        << " [" << current_ttl << "s]" << endl;
+                case boost::net::dns::type_a:
+                    // 'A' resource records carry IPv4 addresses
+                    append_resolved_ipv4( resource_record, resolved_host_address_list );
+                    break;
+                default:
+                    // other types of resource records
+                    break;
             }
         }
     }
@@ -270,3 +265,23 @@ void DnsResolver::fill_resolved_ip_list(
     bool size_equal_minus_cname = ( resolve_host_list_size == ( answer_list_size - 1 ) );
     BOOST_ASSERT( size_equal || size_equal_minus_cname );
 }
+
+void DnsResolver::append_resolved_ipv4(
+        const shared_ptr<resource_base_t> resource_record,
+        list<HostAddress> *resolved_host_address_list
+) const
+{
+    BOOST_ASSERT( resource_record->rtype() == boost::net::dns::type_a );
+
+    a_resource *a_rr = dynamic_cast<a_resource *> ( resource_record.get() );
+    if ( a_rr != NULL )
+    {
+        string current_ip = a_rr->address().to_string();
+        uint32_t current_ttl = a_rr->ttl();
+
+        HostAddress resolved_host( current_ip, current_ttl );
+        resolved_host_address_list->push_back( resolved_host );
+
+        GlobalLogger.info() << "- " << current_ip << " [" << current_ttl << "s]" << endl;
+    }
+}
index 249e356..1c3dd0a 100644 (file)
@@ -59,6 +59,10 @@ private:
             const boost::net::dns::rr_list_t& answers_list,
             std::list<HostAddress> *resolved_host_address_list
     ) const;
+    void append_resolved_ipv4(
+            const shared_ptr<boost::net::dns::resource_base_t> resource_record,
+            std::list<HostAddress> *resolved_host_address_list
+    ) const;
 
 private:
     /// The list of IPs available to the host DNS.