core: use ndn::dns from ndn-cxx

refs #2207

Change-Id: Ice97c9203283b5b89982049cb29c0ccfed0ef422
diff --git a/daemon/face/tcp-factory.cpp b/daemon/face/tcp-factory.cpp
index 598a2c4..0a152b1 100644
--- a/daemon/face/tcp-factory.cpp
+++ b/daemon/face/tcp-factory.cpp
@@ -1,12 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014,  Regents of the University of California,
- *                      Arizona Board of Regents,
- *                      Colorado State University,
- *                      University Pierre & Marie Curie, Sorbonne University,
- *                      Washington University in St. Louis,
- *                      Beijing Institute of Technology,
- *                      The University of Memphis
+ * Copyright (c) 2014-2015,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -24,9 +24,10 @@
  */
 
 #include "tcp-factory.hpp"
-#include "core/resolver.hpp"
 #include "core/logger.hpp"
 #include "core/network-interface.hpp"
+#include "core/global-io.hpp"
+#include <ndn-cxx/util/dns.hpp>
 
 NFD_LOG_INIT("TcpFactory");
 
@@ -110,7 +111,9 @@
 shared_ptr<TcpChannel>
 TcpFactory::createChannel(const std::string& localHost, const std::string& localPort)
 {
-  return createChannel(TcpResolver::syncResolve(localHost, localPort));
+  tcp::Endpoint endpoint(ndn::dns::syncResolve(localHost, getGlobalIoService()),
+                         boost::lexical_cast<uint16_t>(localPort));
+  return createChannel(endpoint);
 }
 
 shared_ptr<TcpChannel>
@@ -128,30 +131,10 @@
                        const FaceCreatedCallback& onCreated,
                        const FaceConnectFailedCallback& onConnectFailed)
 {
-  resolver::AddressSelector addressSelector = resolver::AnyAddress();
-  if (uri.getScheme() == "tcp4")
-    addressSelector = resolver::Ipv4Address();
-  else if (uri.getScheme() == "tcp6")
-    addressSelector = resolver::Ipv6Address();
+  BOOST_ASSERT(uri.isCanonical());
+  boost::asio::ip::address ipAddress = boost::asio::ip::address::from_string(uri.getHost());
+  tcp::Endpoint endpoint(ipAddress, boost::lexical_cast<uint16_t>(uri.getPort()));
 
-  if (!uri.getPath().empty() && uri.getPath() != "/")
-    {
-      onConnectFailed("Invalid URI");
-    }
-
-  TcpResolver::asyncResolve(uri.getHost(),
-                            uri.getPort().empty() ? m_defaultPort : uri.getPort(),
-                            bind(&TcpFactory::continueCreateFaceAfterResolve, this, _1,
-                                 onCreated, onConnectFailed),
-                            onConnectFailed,
-                            addressSelector);
-}
-
-void
-TcpFactory::continueCreateFaceAfterResolve(const tcp::Endpoint& endpoint,
-                                           const FaceCreatedCallback& onCreated,
-                                           const FaceConnectFailedCallback& onConnectFailed)
-{
   if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end())
     {
       onConnectFailed("Requested endpoint is prohibited "
@@ -160,7 +143,6 @@
     }
 
   // very simple logic for now
-
   for (ChannelMap::iterator channel = m_channels.begin();
        channel != m_channels.end();
        ++channel)
diff --git a/daemon/face/tcp-factory.hpp b/daemon/face/tcp-factory.hpp
index e2ebda5..e50bf66 100644
--- a/daemon/face/tcp-factory.hpp
+++ b/daemon/face/tcp-factory.hpp
@@ -1,12 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014,  Regents of the University of California,
- *                      Arizona Board of Regents,
- *                      Colorado State University,
- *                      University Pierre & Marie Curie, Sorbonne University,
- *                      Washington University in St. Louis,
- *                      Beijing Institute of Technology,
- *                      The University of Memphis
+ * Copyright (c) 2014-2015,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -29,6 +29,7 @@
 #include "protocol-factory.hpp"
 #include "tcp-channel.hpp"
 
+
 namespace nfd {
 
 class TcpFactory : public ProtocolFactory
@@ -110,11 +111,6 @@
   shared_ptr<TcpChannel>
   findChannel(const tcp::Endpoint& localEndpoint);
 
-  void
-  continueCreateFaceAfterResolve(const tcp::Endpoint& endpoint,
-                                 const FaceCreatedCallback& onCreated,
-                                 const FaceConnectFailedCallback& onConnectFailed);
-
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   typedef std::map< tcp::Endpoint, shared_ptr<TcpChannel> > ChannelMap;
   ChannelMap m_channels;
diff --git a/daemon/face/udp-factory.cpp b/daemon/face/udp-factory.cpp
index 74064c3..4ef4e17 100644
--- a/daemon/face/udp-factory.cpp
+++ b/daemon/face/udp-factory.cpp
@@ -25,8 +25,8 @@
 
 #include "udp-factory.hpp"
 #include "core/global-io.hpp"
-#include "core/resolver.hpp"
 #include "core/network-interface.hpp"
+#include <ndn-cxx/util/dns.hpp>
 
 #if defined(__linux__)
 #include <sys/socket.h>
@@ -140,7 +140,9 @@
                           const std::string& localPort,
                           const time::seconds& timeout)
 {
-  return createChannel(UdpResolver::syncResolve(localHost, localPort), timeout);
+  udp::Endpoint endPoint(ndn::dns::syncResolve(localHost, getGlobalIoService()),
+                         boost::lexical_cast<uint16_t>(localPort));
+  return createChannel(endPoint, timeout);
 }
 
 shared_ptr<MulticastUdpFace>
@@ -251,12 +253,13 @@
                                 const std::string& multicastPort,
                                 const std::string& networkInterfaceName /* "" */)
 {
+  udp::Endpoint localEndpoint(ndn::dns::syncResolve(localIp, getGlobalIoService()),
+                              boost::lexical_cast<uint16_t>(multicastPort));
 
-  return createMulticastFace(UdpResolver::syncResolve(localIp,
-                                                      multicastPort),
-                             UdpResolver::syncResolve(multicastIp,
-                                                      multicastPort),
-                             networkInterfaceName);
+  udp::Endpoint multicastEndpoint(ndn::dns::syncResolve(multicastIp, getGlobalIoService()),
+                                  boost::lexical_cast<uint16_t>(multicastPort));
+
+  return createMulticastFace(localEndpoint, multicastEndpoint, networkInterfaceName);
 }
 
 void
@@ -264,31 +267,10 @@
                        const FaceCreatedCallback& onCreated,
                        const FaceConnectFailedCallback& onConnectFailed)
 {
-  resolver::AddressSelector addressSelector = resolver::AnyAddress();
-  if (uri.getScheme() == "udp4")
-    addressSelector = resolver::Ipv4Address();
-  else if (uri.getScheme() == "udp6")
-    addressSelector = resolver::Ipv6Address();
+  BOOST_ASSERT(uri.isCanonical());
+  boost::asio::ip::address ipAddress = boost::asio::ip::address::from_string(uri.getHost());
+  udp::Endpoint endpoint(ipAddress, boost::lexical_cast<uint16_t>(uri.getPort()));
 
-  if (!uri.getPath().empty() && uri.getPath() != "/")
-    {
-      onConnectFailed("Invalid URI");
-    }
-
-  UdpResolver::asyncResolve(uri.getHost(),
-                            uri.getPort().empty() ? m_defaultPort : uri.getPort(),
-                            bind(&UdpFactory::continueCreateFaceAfterResolve, this, _1,
-                                 onCreated, onConnectFailed),
-                            onConnectFailed,
-                            addressSelector);
-
-}
-
-void
-UdpFactory::continueCreateFaceAfterResolve(const udp::Endpoint& endpoint,
-                                           const FaceCreatedCallback& onCreated,
-                                           const FaceConnectFailedCallback& onConnectFailed)
-{
   if (endpoint.address().is_multicast()) {
     onConnectFailed("The provided address is multicast. Please use createMulticastFace method");
     return;
diff --git a/daemon/face/udp-factory.hpp b/daemon/face/udp-factory.hpp
index 1f7f7de..db4760b 100644
--- a/daemon/face/udp-factory.hpp
+++ b/daemon/face/udp-factory.hpp
@@ -1,12 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014,  Regents of the University of California,
- *                      Arizona Board of Regents,
- *                      Colorado State University,
- *                      University Pierre & Marie Curie, Sorbonne University,
- *                      Washington University in St. Louis,
- *                      Beijing Institute of Technology,
- *                      The University of Memphis
+ * Copyright (c) 2014-2015,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -197,11 +197,6 @@
   shared_ptr<MulticastUdpFace>
   findMulticastFace(const udp::Endpoint& localEndpoint);
 
-  void
-  continueCreateFaceAfterResolve(const udp::Endpoint& endpoint,
-                                 const FaceCreatedCallback& onCreated,
-                                 const FaceConnectFailedCallback& onConnectFailed);
-
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   typedef std::map< udp::Endpoint, shared_ptr<UdpChannel> > ChannelMap;
 
diff --git a/daemon/face/websocket-factory.cpp b/daemon/face/websocket-factory.cpp
index 30f3afd..49f28b0 100644
--- a/daemon/face/websocket-factory.cpp
+++ b/daemon/face/websocket-factory.cpp
@@ -1,12 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014,  Regents of the University of California,
- *                      Arizona Board of Regents,
- *                      Colorado State University,
- *                      University Pierre & Marie Curie, Sorbonne University,
- *                      Washington University in St. Louis,
- *                      Beijing Institute of Technology,
- *                      The University of Memphis
+ * Copyright (c) 2014-2015,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -24,7 +24,7 @@
  */
 
 #include "websocket-factory.hpp"
-#include "core/resolver.hpp"
+#include <ndn-cxx/util/dns.hpp>
 
 namespace nfd {
 
@@ -53,7 +53,8 @@
 shared_ptr<WebSocketChannel>
 WebSocketFactory::createChannel(const std::string& host, const std::string& port)
 {
-  ip::tcp::endpoint tcpEndpoint = TcpResolver::syncResolve(host, port);
+  ip::tcp::endpoint tcpEndpoint(ndn::dns::syncResolve(host, getGlobalIoService()),
+                                boost::lexical_cast<uint16_t>(port));
   websocket::Endpoint endpoint(tcpEndpoint.address(), tcpEndpoint.port());
   return createChannel(endpoint);
 }