From: Guilherme Maciel Ferreira Date: Mon, 5 Mar 2012 00:03:45 +0000 (-0300) Subject: Bring aboard a factory to create DNS resolvers. X-Git-Tag: v1.5~1^2~20 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=71a09a579894f45cbfb9dbb9f3b9a62b01504f54;p=pingcheck Bring aboard a factory to create DNS resolvers. --- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index befa52e..89f4aeb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -56,6 +56,7 @@ set(SOURCES config/option/statusnotifiercmdoption.cpp config/option/versionoption.cpp dns/dnsresolver.cpp + dns/dnsresolverfactory.cpp dns/hostaddress.cpp dns/timetolive.cpp host/hoststatus.cpp diff --git a/src/dns/dnsresolverfactory.cpp b/src/dns/dnsresolverfactory.cpp new file mode 100644 index 0000000..12f8da2 --- /dev/null +++ b/src/dns/dnsresolverfactory.cpp @@ -0,0 +1,76 @@ +/* + The software in this package is distributed under the GNU General + Public License version 2 (with a special exception described below). + + A copy of GNU General Public License (GPL) is included in this distribution, + in the file COPYING.GPL. + + As a special exception, if other files instantiate templates or use macros + or inline functions from this file, or you compile this file and link it + with other works to produce a work based on this file, this file + does not by itself cause the resulting work to be covered + by the GNU General Public License. + + However the source code for this file must still be made available + in accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work based + on this file might be covered by the GNU General Public License. + */ + +#include "dns/dnsresolverfactory.h" + +#include + +using namespace std; +using boost::net::dns::type_t; + +//----------------------------------------------------------------------------- +// DnsResolverFactory +//----------------------------------------------------------------------------- + +/** + * @brief Default constructor. + */ +DnsResolverFactory::DnsResolverFactory() +{ +} + +/** + * @brief Destructor. + */ +DnsResolverFactory::~DnsResolverFactory() +{ +} + +/** + * @brief Creates a DNS resolver suitable to the given protocol. + * + * @param destination_address The remote address to ping. + * @param nameserver Server to resolve the addresses. + * @param ping_protocol The protocol used to ping, in order to find the DNS resolver more + * appropriated to resolve the addresses for this protocol. + */ +DnsResolverItem DnsResolverFactory::createResolver( + const string &destination_address, + const string &nameserver, + const PingProtocol ping_protocol +) +{ + switch ( ping_protocol ) + { + case PingProtocol_ICMP: + case PingProtocol_TCP: + return DnsResolverItem( + new DnsResolver( destination_address, nameserver, boost::net::dns::type_a ) + ); + case PingProtocol_ICMPv6: + case PingProtocol_TCP_IPv6: + return DnsResolverItem( + new DnsResolver( destination_address, nameserver, boost::net::dns::type_a6 ) + ); + default: + BOOST_ASSERT( !"Try to create a DNS from an invalid address resource record" ); //lint !e506 + return DnsResolverItem(); //lint !e527 + } +} diff --git a/src/dns/dnsresolverfactory.h b/src/dns/dnsresolverfactory.h new file mode 100644 index 0000000..ff698be --- /dev/null +++ b/src/dns/dnsresolverfactory.h @@ -0,0 +1,48 @@ +/* + The software in this package is distributed under the GNU General + Public License version 2 (with a special exception described below). + + A copy of GNU General Public License (GPL) is included in this distribution, + in the file COPYING.GPL. + + As a special exception, if other files instantiate templates or use macros + or inline functions from this file, or you compile this file and link it + with other works to produce a work based on this file, this file + does not by itself cause the resulting work to be covered + by the GNU General Public License. + + However the source code for this file must still be made available + in accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work based + on this file might be covered by the GNU General Public License. + */ + +#ifndef DNS_RESOLVER_FACTORY_H +#define DNS_RESOLVER_FACTORY_H + +#include + +#include "dns/dnsresolver.h" +#include "host/pingprotocol.h" + +//----------------------------------------------------------------------------- +// DnsResolverFactory +//----------------------------------------------------------------------------- + +class DnsResolverFactory +{ +public: + static DnsResolverItem createResolver( + const std::string &destination_address, + const std::string &nameserver, + const PingProtocol ping_protocol + ); + +private: + DnsResolverFactory(); + ~DnsResolverFactory(); + +}; + +#endif // DNS_RESOLVER_FACTORY_H