core: Generic resolver implementation with option to select address filter

Change-Id: I467ed9f49a55392e89417aa9925f30b2d7cbeda9
diff --git a/daemon/core/resolver.hpp b/daemon/core/resolver.hpp
new file mode 100644
index 0000000..bd61f10
--- /dev/null
+++ b/daemon/core/resolver.hpp
@@ -0,0 +1,191 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NFD_CORE_RESOLVER_H
+#define NFD_CORE_RESOLVER_H
+
+#include "common.hpp"
+#include "core/global-io.hpp"
+#include "core/time.hpp"
+#include "core/scheduler.hpp"
+
+namespace nfd {
+namespace resolver {
+
+typedef function<bool (const boost::asio::ip::address& address)> AddressSelector;
+
+struct AnyAddress {
+  bool
+  operator()(const boost::asio::ip::address& address)
+  {
+    return true;
+  }
+};
+
+struct Ipv4Address {
+  bool
+  operator()(const boost::asio::ip::address& address)
+  {
+    return address.is_v4();
+  }
+};
+
+struct Ipv6Address {
+  bool
+  operator()(const boost::asio::ip::address& address)
+  {
+    return address.is_v6();
+  }
+};
+
+} // namespace resolver
+
+template<class Protocol>
+class Resolver
+{
+public:
+  struct Error : public std::runtime_error
+  {
+    Error(const std::string& what) : std::runtime_error(what) {}
+  };
+
+  typedef function<void (const typename Protocol::endpoint& endpoint)> SuccessCallback;
+  typedef function<void (const std::string& reason)> ErrorCallback;
+
+  typedef boost::asio::ip::basic_resolver< Protocol > resolver;
+
+  /** \brief Asynchronously resolve host and port
+   *
+   * If an address selector predicate is specified, then each resolved IP address
+   * is checked against the predicate.
+   *
+   * Available address selector predicates:
+   *
+   * - resolver::AnyAddress()
+   * - resolver::Ipv4Address()
+   * - resolver::Ipv6Address()
+   */
+  static void
+  asyncResolve(const std::string& host, const std::string& port,
+               const SuccessCallback& onSuccess,
+               const ErrorCallback& onError,
+               const nfd::resolver::AddressSelector& addressSelector = nfd::resolver::AnyAddress(),
+               const time::Duration& timeout = time::seconds(4.0))
+  {
+    shared_ptr<Resolver> resolver =
+      shared_ptr<Resolver>(new Resolver(onSuccess, onError,
+                                        addressSelector));
+
+    resolver->asyncResolve(host, port, timeout, resolver);
+    // resolver will be destroyed when async operation finishes or global IO service stops
+  }
+
+  /** \brief Synchronously resolve host and port
+   *
+   * If an address selector predicate is specified, then each resolved IP address
+   * is checked against the predicate.
+   *
+   * Available address selector predicates:
+   *
+   * - resolver::AnyAddress()
+   * - resolver::Ipv4Address()
+   * - resolver::Ipv6Address()
+   */
+  static typename Protocol::endpoint
+  syncResolve(const std::string& host, const std::string& port,
+              const nfd::resolver::AddressSelector& addressSelector = nfd::resolver::AnyAddress())
+  {
+    Resolver resolver(SuccessCallback(), ErrorCallback(), addressSelector);
+
+    typename resolver::query query(host, port,
+                                   resolver::query::all_matching);
+
+    typename resolver::iterator remoteEndpoint = resolver.m_resolver.resolve(query);
+    typename resolver::iterator end;
+    for (; remoteEndpoint != end; ++remoteEndpoint)
+      {
+        if (addressSelector(typename Protocol::endpoint(*remoteEndpoint).address()))
+          return *remoteEndpoint;
+      }
+    throw Error("No endpoint matching the specified address selector found");
+  }
+
+private:
+  Resolver(const SuccessCallback& onSuccess,
+           const ErrorCallback& onError,
+           const nfd::resolver::AddressSelector& addressSelector)
+    : m_resolver(getGlobalIoService())
+    , m_addressSelector(addressSelector)
+    , m_onSuccess(onSuccess)
+    , m_onError(onError)
+  {
+  }
+
+  void
+  asyncResolve(const std::string& host, const std::string& port,
+               const time::Duration& timeout,
+               const shared_ptr<Resolver>& self)
+  {
+    typename resolver::query query(host, port,
+                                   resolver::query::all_matching);
+
+    m_resolver.async_resolve(query,
+                             bind(&Resolver::onResolveSuccess, this, _1, _2, self));
+
+    m_resolveTimeout = scheduler::schedule(timeout,
+                                           bind(&Resolver::onResolveError, this,
+                                                "Timeout", self));
+  }
+
+  void
+  onResolveSuccess(const boost::system::error_code& error,
+                   typename resolver::iterator remoteEndpoint,
+                   const shared_ptr<Resolver>& self)
+  {
+    scheduler::cancel(m_resolveTimeout);
+
+    if (error)
+      {
+        if (error == boost::system::errc::operation_canceled)
+          return;
+
+        return m_onError("Remote endpoint hostname or port cannot be resolved: " +
+                         error.category().message(error.value()));
+      }
+
+    typename resolver::iterator end;
+    for (; remoteEndpoint != end; ++remoteEndpoint)
+      {
+        if (m_addressSelector(typename Protocol::endpoint(*remoteEndpoint).address()))
+          return m_onSuccess(*remoteEndpoint);
+      }
+
+    m_onError("No endpoint matching the specified address selector found");
+  }
+
+  void
+  onResolveError(const std::string& errorInfo,
+                 const shared_ptr<Resolver>& self)
+  {
+    m_resolver.cancel();
+    m_onError(errorInfo);
+  }
+
+private:
+  resolver m_resolver;
+  EventId m_resolveTimeout;
+
+  nfd::resolver::AddressSelector m_addressSelector;
+  SuccessCallback m_onSuccess;
+  ErrorCallback m_onError;
+};
+
+typedef Resolver<boost::asio::ip::tcp> TcpResolver;
+typedef Resolver<boost::asio::ip::udp> UdpResolver;
+
+} // namespace nfd
+
+#endif // NFD_CORE_RESOLVER_H
diff --git a/tests/core/resolver.cpp b/tests/core/resolver.cpp
new file mode 100644
index 0000000..4d7460f
--- /dev/null
+++ b/tests/core/resolver.cpp
@@ -0,0 +1,229 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "core/resolver.hpp"
+#include "core/logger.hpp"
+#include <boost/test/unit_test.hpp>
+
+#include "tests/test-common.hpp"
+
+namespace nfd {
+namespace tests {
+
+NFD_LOG_INIT("tests.CoreResolver");
+
+using boost::asio::ip::address_v4;
+using boost::asio::ip::address_v6;
+using boost::asio::ip::tcp;
+using boost::asio::ip::udp;
+
+BOOST_FIXTURE_TEST_SUITE(CoreResolver, BaseFixture)
+
+template<class Protocol>
+class ResolverFixture : protected BaseFixture
+{
+public:
+  ResolverFixture()
+    : m_nFailures(0)
+    , m_nSuccesses(0)
+  {
+  }
+
+  void
+  onSuccess(const typename Protocol::endpoint& resolvedEndpoint,
+            const typename Protocol::endpoint& expectedEndpoint,
+            bool isValid, bool wantCheckAddress = false)
+  {
+    NFD_LOG_DEBUG("Resolved to: " << resolvedEndpoint);
+    ++m_nSuccesses;
+
+    if (!isValid)
+      {
+        BOOST_FAIL("Resolved to " + boost::lexical_cast<std::string>(resolvedEndpoint)
+                   + ", but it should have failed");
+      }
+
+    BOOST_CHECK_EQUAL(resolvedEndpoint.port(), expectedEndpoint.port());
+
+    BOOST_CHECK_EQUAL(resolvedEndpoint.address().is_v4(), expectedEndpoint.address().is_v4());
+
+    // checking address is not deterministic and should be enabled only
+    // if only one IP address will be returned by resolution
+    if (wantCheckAddress)
+      {
+        BOOST_CHECK_EQUAL(resolvedEndpoint.address(), expectedEndpoint.address());
+      }
+  }
+
+  void
+  onFailure(bool isValid)
+  {
+    ++m_nFailures;
+
+    if (!isValid)
+      BOOST_FAIL("Resolution should not have failed");
+
+    BOOST_CHECK_MESSAGE(true, "Resolution failed as expected");
+  }
+
+
+  uint32_t m_nFailures;
+  uint32_t m_nSuccesses;
+};
+
+BOOST_FIXTURE_TEST_CASE(Tcp, ResolverFixture<tcp>)
+{
+  TcpResolver::asyncResolve("www.named-data.net", "6363",
+                            bind(&ResolverFixture<tcp>::onSuccess, this, _1,
+                                 tcp::endpoint(address_v4(), 6363), true, false),
+                            bind(&ResolverFixture<tcp>::onFailure, this, false));
+
+  TcpResolver::asyncResolve("www.named-data.net", "notport",
+                            bind(&ResolverFixture<tcp>::onSuccess, this, _1,
+                                 tcp::endpoint(address_v4(), 0), false, false),
+                            bind(&ResolverFixture<tcp>::onFailure, this, true)); // should fail
+
+
+  TcpResolver::asyncResolve("nothost.nothost.nothost.arpa", "6363",
+                            bind(&ResolverFixture<tcp>::onSuccess, this, _1,
+                                 tcp::endpoint(address_v4(), 6363), false, false),
+                            bind(&ResolverFixture<tcp>::onFailure, this, true)); // should fail
+
+  TcpResolver::asyncResolve("www.google.com", "80",
+                            bind(&ResolverFixture<tcp>::onSuccess, this, _1,
+                                 tcp::endpoint(address_v4(), 80), true, false),
+                            bind(&ResolverFixture<tcp>::onFailure, this, false),
+                            resolver::Ipv4Address()); // request IPv4 address
+
+  TcpResolver::asyncResolve("www.google.com", "80",
+                            bind(&ResolverFixture<tcp>::onSuccess, this, _1,
+                                 tcp::endpoint(address_v6(), 80), true, false),
+                            bind(&ResolverFixture<tcp>::onFailure, this, false),
+                            resolver::Ipv6Address()); // request IPv6 address
+
+  TcpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available
+                            bind(&ResolverFixture<tcp>::onSuccess, this, _1,
+                                 tcp::endpoint(address_v6(), 80), true, false),
+                            bind(&ResolverFixture<tcp>::onFailure, this, false));
+
+  TcpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available
+                            bind(&ResolverFixture<tcp>::onSuccess, this, _1,
+                                 tcp::endpoint(address_v6(), 80), true, false),
+                            bind(&ResolverFixture<tcp>::onFailure, this, false),
+                            resolver::Ipv6Address());
+
+  TcpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available
+                            bind(&ResolverFixture<tcp>::onSuccess, this, _1,
+                                 tcp::endpoint(address_v6(), 80), false, false),
+                            bind(&ResolverFixture<tcp>::onFailure, this, true), // should fail
+                            resolver::Ipv4Address());
+
+  TcpResolver::asyncResolve("192.0.2.1", "80",
+                            bind(&ResolverFixture<tcp>::onSuccess, this, _1,
+                                 tcp::endpoint(address_v4::from_string("192.0.2.1"), 80), true, true),
+                            bind(&ResolverFixture<tcp>::onFailure, this, false));
+
+  TcpResolver::asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3", "80",
+                            bind(&ResolverFixture<tcp>::onSuccess, this, _1,
+                                 tcp::endpoint(address_v6::
+                                               from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3"),
+                                               80), true, true),
+                            bind(&ResolverFixture<tcp>::onFailure, this, false));
+
+  g_io.run();
+
+  BOOST_CHECK_EQUAL(m_nFailures, 3);
+  BOOST_CHECK_EQUAL(m_nSuccesses, 7);
+}
+
+BOOST_AUTO_TEST_CASE(SyncTcp)
+{
+  tcp::endpoint endpoint;
+  BOOST_CHECK_NO_THROW(endpoint = TcpResolver::syncResolve("www.named-data.net", "6363"));
+  NFD_LOG_DEBUG("Resolved to: " << endpoint);
+  BOOST_CHECK_EQUAL(endpoint.address().is_v4(), true);
+  BOOST_CHECK_EQUAL(endpoint.port(), 6363);
+}
+
+BOOST_FIXTURE_TEST_CASE(Udp, ResolverFixture<udp>)
+{
+  UdpResolver::asyncResolve("www.named-data.net", "6363",
+                            bind(&ResolverFixture<udp>::onSuccess, this, _1,
+                                 udp::endpoint(address_v4(), 6363), true, false),
+                            bind(&ResolverFixture<udp>::onFailure, this, false));
+
+  UdpResolver::asyncResolve("www.named-data.net", "notport",
+                            bind(&ResolverFixture<udp>::onSuccess, this, _1,
+                                 udp::endpoint(address_v4(), 0), false, false),
+                            bind(&ResolverFixture<udp>::onFailure, this, true)); // should fail
+
+
+  UdpResolver::asyncResolve("nothost.nothost.nothost.arpa", "6363",
+                            bind(&ResolverFixture<udp>::onSuccess, this, _1,
+                                 udp::endpoint(address_v4(), 6363), false, false),
+                            bind(&ResolverFixture<udp>::onFailure, this, true)); // should fail
+
+  UdpResolver::asyncResolve("www.google.com", "80",
+                            bind(&ResolverFixture<udp>::onSuccess, this, _1,
+                                 udp::endpoint(address_v4(), 80), true, false),
+                            bind(&ResolverFixture<udp>::onFailure, this, false),
+                            resolver::Ipv4Address()); // request IPv4 address
+
+  UdpResolver::asyncResolve("www.google.com", "80",
+                            bind(&ResolverFixture<udp>::onSuccess, this, _1,
+                                 udp::endpoint(address_v6(), 80), true, false),
+                            bind(&ResolverFixture<udp>::onFailure, this, false),
+                            resolver::Ipv6Address()); // request IPv6 address
+
+  UdpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available
+                            bind(&ResolverFixture<udp>::onSuccess, this, _1,
+                                 udp::endpoint(address_v6(), 80), true, false),
+                            bind(&ResolverFixture<udp>::onFailure, this, false));
+
+  UdpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available
+                            bind(&ResolverFixture<udp>::onSuccess, this, _1,
+                                 udp::endpoint(address_v6(), 80), true, false),
+                            bind(&ResolverFixture<udp>::onFailure, this, false),
+                            resolver::Ipv6Address());
+
+  UdpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available
+                            bind(&ResolverFixture<udp>::onSuccess, this, _1,
+                                 udp::endpoint(address_v6(), 80), false, false),
+                            bind(&ResolverFixture<udp>::onFailure, this, true), // should fail
+                            resolver::Ipv4Address());
+
+  UdpResolver::asyncResolve("192.0.2.1", "80",
+                            bind(&ResolverFixture<udp>::onSuccess, this, _1,
+                                 udp::endpoint(address_v4::from_string("192.0.2.1"), 80), true, true),
+                            bind(&ResolverFixture<udp>::onFailure, this, false));
+
+  UdpResolver::asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3", "80",
+                            bind(&ResolverFixture<udp>::onSuccess, this, _1,
+                                 udp::endpoint(address_v6::
+                                               from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3"),
+                                               80), true, true),
+                            bind(&ResolverFixture<udp>::onFailure, this, false));
+
+  g_io.run();
+
+  BOOST_CHECK_EQUAL(m_nFailures, 3);
+  BOOST_CHECK_EQUAL(m_nSuccesses, 7);
+}
+
+BOOST_AUTO_TEST_CASE(SyncUdp)
+{
+  udp::endpoint endpoint;
+  BOOST_CHECK_NO_THROW(endpoint = UdpResolver::syncResolve("www.named-data.net", "6363"));
+  NFD_LOG_DEBUG("Resolved to: " << endpoint);
+  BOOST_CHECK_EQUAL(endpoint.address().is_v4(), true);
+  BOOST_CHECK_EQUAL(endpoint.port(), 6363);
+}
+
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace nfd