Bring aboard the first scratch of a DNS resolver
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Fri, 11 Mar 2011 09:26:59 +0000 (10:26 +0100)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Fri, 11 Mar 2011 10:04:00 +0000 (11:04 +0100)
- given a host DNS, this class resolves and keeps a list of IPs for that host

src/CMakeLists.txt
src/dns/dnsresolver.cpp [new file with mode: 0644]
src/dns/dnsresolver.h [new file with mode: 0644]
src/dns/hostaddress.cpp [new file with mode: 0644]
src/dns/hostaddress.h [new file with mode: 0644]

index 287be48..ad8bca9 100644 (file)
@@ -1,6 +1,7 @@
 # include directories where the source code is located
 include_directories(
     config
+    dns
     icmp
     ping
     ${Boost_INCLUDE_DIRS}
@@ -11,6 +12,8 @@ set( SOURCES
     config/configuration.cpp
     config/configurationreader.cpp
     config/host.cpp
+    dns/dnsresolver.cpp
+    dns/hostaddress.cpp
     icmp/icmpheader.cpp
     icmp/icmppacket.cpp
     icmp/ipv4header.cpp
diff --git a/src/dns/dnsresolver.cpp b/src/dns/dnsresolver.cpp
new file mode 100644 (file)
index 0000000..9851135
--- /dev/null
@@ -0,0 +1,71 @@
+#include <boost/asio.hpp>
+#include <iostream>
+
+#include "hostaddress.h"
+
+#include "dnsresolver.h"
+
+using namespace std;
+using namespace boost::asio;
+using namespace boost::asio::ip;
+
+//-----------------------------------------------------------------------------
+// DnsResolver
+//-----------------------------------------------------------------------------
+
+DnsResolver::DnsResolver( string dns_address ) :
+        ResolvedHostAddressList()
+{
+    resolve( dns_address );
+}
+
+DnsResolver::~DnsResolver()
+{
+}
+
+string DnsResolver::get_next_ip()
+{
+    uint list_size_before = ResolvedHostAddressList.size();
+
+    HostAddress host_address = ResolvedHostAddressList.front();
+    ResolvedHostAddressList.pop_front();
+    string destination_ip = host_address.get_ip();
+    ResolvedHostAddressList.push_back( host_address );
+
+    uint list_size_after = ResolvedHostAddressList.size();
+    BOOST_ASSERT( list_size_before == list_size_after );
+
+    return destination_ip;
+}
+
+/**
+ * Resolve which IPs belong to the given DNS and build a list of these IPs.
+ *
+ * @param dns_address The DNS address to be query for IPs.
+ */
+void DnsResolver::resolve( string dns_address )
+{
+    BOOST_ASSERT( !dns_address.empty() );
+
+    cerr << " - Host : " << dns_address << endl;
+
+    io_service io_service;
+    tcp::resolver resolver( io_service );
+    tcp::resolver::query query( tcp::v4(), dns_address, "" );
+    tcp::resolver::iterator it_first = resolver.resolve( query );
+    tcp::resolver::iterator it_last = tcp::resolver::iterator();
+    while ( it_first != it_last )
+    {
+        string ip = (*it_first).endpoint().address().to_string();
+
+        HostAddress resolved_host;
+        resolved_host.set_ip( ip );
+        ResolvedHostAddressList.push_back( resolved_host );
+
+        cerr << "   |_ " << ip << endl;
+
+        ++it_first;
+    }
+
+    BOOST_ASSERT( 0 < ResolvedHostAddressList.size() );
+}
diff --git a/src/dns/dnsresolver.h b/src/dns/dnsresolver.h
new file mode 100644 (file)
index 0000000..656e07b
--- /dev/null
@@ -0,0 +1,29 @@
+#ifndef DNSRESOLVER_H
+#define DNSRESOLVER_H
+
+#include <string>
+#include <list>
+
+class HostAddress;
+
+//-----------------------------------------------------------------------------
+// DnsResolver
+//-----------------------------------------------------------------------------
+
+class DnsResolver
+{
+public:
+    DnsResolver( std::string dns_address );
+    virtual ~DnsResolver();
+
+    std::string get_next_ip();
+
+private:
+    void resolve( std::string dns_address );
+
+private:
+    std::list<HostAddress> ResolvedHostAddressList;
+
+};
+
+#endif /* DNSRESOLVER_H */
diff --git a/src/dns/hostaddress.cpp b/src/dns/hostaddress.cpp
new file mode 100644 (file)
index 0000000..edb696d
--- /dev/null
@@ -0,0 +1,38 @@
+#include "hostaddress.h"
+
+using namespace std;
+
+//-----------------------------------------------------------------------------
+// HostAddress
+//-----------------------------------------------------------------------------
+
+HostAddress::HostAddress() :
+    Ip( "" ),
+    Ttl( 0 )
+{
+}
+
+HostAddress::~HostAddress()
+{
+}
+
+void HostAddress::set_ip( string ip )
+{
+    this->Ip = ip;
+}
+
+string HostAddress::get_ip() const
+{
+    return Ip;
+}
+
+uint HostAddress::get_ttl() const
+{
+    return Ttl;
+}
+
+void HostAddress::set_ttl( uint ttl )
+{
+    this->Ttl = ttl;
+}
+
diff --git a/src/dns/hostaddress.h b/src/dns/hostaddress.h
new file mode 100644 (file)
index 0000000..9bbf3a2
--- /dev/null
@@ -0,0 +1,29 @@
+#ifndef HOSTADDRESS_H_
+#define HOSTADDRESS_H_
+
+#include <string>
+#include <sys/types.h>
+
+//-----------------------------------------------------------------------------
+// HostAddress
+//-----------------------------------------------------------------------------
+
+class HostAddress
+{
+public:
+    HostAddress();
+    virtual ~HostAddress();
+
+    std::string get_ip() const;
+    void set_ip( std::string ip );
+
+    uint get_ttl() const;
+    void set_ttl( uint ttl );
+
+private:
+    std::string Ip;
+    uint Ttl;
+
+};
+
+#endif /* HOSTADDRESS_H_ */