net: support link-local IPv6 addresses in FaceUri

Change-Id: Ia986847e60b0a21a94bf2e4ce99d4a5a688a2006
Refs: #1428
diff --git a/src/net/address-converter.cpp b/src/net/address-converter.cpp
new file mode 100644
index 0000000..b6f2de2
--- /dev/null
+++ b/src/net/address-converter.cpp
@@ -0,0 +1,151 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2017 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 ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "address-converter.hpp"
+
+#if BOOST_VERSION < 105800
+#include <boost/algorithm/string.hpp>
+#include <boost/lexical_cast.hpp>
+#include <vector>
+#endif // BOOST_VERSION < 105800
+
+#include <net/if.h> // for if_nametoindex and if_indextoname
+
+namespace ndn {
+namespace ip {
+
+optional<std::string>
+scopeNameFromId(unsigned int scopeId)
+{
+  char buffer[IFNAMSIZ];
+  auto scopeName = if_indextoname(scopeId, buffer);
+  if (scopeName != nullptr) {
+    return std::string(scopeName);
+  }
+  return nullopt;
+}
+
+#if BOOST_VERSION < 105800
+static unsigned int
+scopeIdFromString(const std::string& scope)
+{
+  auto id = if_nametoindex(scope.c_str());
+  if (id != 0) {
+    return id;
+  }
+
+  // cannot find a corresponding index, assume it's not a name but an interface index
+  try {
+    return boost::lexical_cast<unsigned int>(scope);
+  }
+  catch (const boost::bad_lexical_cast&) {
+    return 0;
+  }
+}
+
+struct ParsedAddress
+{
+  boost::asio::ip::address addr;
+  std::string scope;
+};
+
+static ParsedAddress
+parseAddressFromString(const std::string& address, boost::system::error_code& ec)
+{
+  std::vector<std::string> parseResult;
+  boost::algorithm::split(parseResult, address, boost::is_any_of("%"));
+  auto addr = boost::asio::ip::address::from_string(parseResult[0], ec);
+
+  switch (parseResult.size()) {
+  case 1:
+    // regular address
+    return {addr, ""};
+  case 2:
+    // the presence of % in either an IPv4 address or a regular IPv6 address is invalid
+    if (!ec && addr.is_v6() && addr.to_v6().is_link_local()) {
+      return {addr, parseResult[1]};
+    }
+    // Fallthrough, if the presence of % is invalid set ec to invalid_argument
+  default:
+    ec = boost::asio::error::invalid_argument;
+    return {};
+  }
+}
+#endif // BOOST_VERSION < 105800
+
+boost::asio::ip::address
+addressFromString(const std::string& address, boost::system::error_code& ec)
+{
+  // boost < 1.58 cannot recognize scope-id in link-local IPv6 address
+#if BOOST_VERSION < 105800
+  auto parsedAddress = parseAddressFromString(address, ec);
+  if (ec || parsedAddress.addr.is_v4()) {
+    return parsedAddress.addr;
+  }
+  auto addr = parsedAddress.addr.to_v6();
+  addr.scope_id(scopeIdFromString(parsedAddress.scope));
+  return addr;
+#else
+  return boost::asio::ip::address::from_string(address, ec);
+#endif // BOOST_VERSION < 105800
+}
+
+boost::asio::ip::address
+addressFromString(const std::string& address)
+{
+  boost::system::error_code ec;
+  auto addr = addressFromString(address, ec);
+  if (ec) {
+    BOOST_THROW_EXCEPTION(boost::system::system_error(ec));
+  }
+  return addr;
+}
+
+boost::asio::ip::address_v6
+addressV6FromString(const std::string& address, boost::system::error_code& ec)
+{
+  auto addr = addressFromString(address, ec);
+  if (ec || addr.is_v4()) {
+    ec = boost::asio::error::invalid_argument;
+    return {};
+  }
+  return addr.to_v6();
+}
+
+boost::asio::ip::address_v6
+addressV6FromString(const std::string& address)
+{
+  boost::system::error_code ec;
+  auto addr = addressV6FromString(address, ec);
+  if (ec) {
+    BOOST_THROW_EXCEPTION(boost::system::system_error(ec));
+  }
+  return addr;
+}
+
+} // namespace ip
+} // namespace ndn
diff --git a/src/net/address-converter.hpp b/src/net/address-converter.hpp
new file mode 100644
index 0000000..b58cfff
--- /dev/null
+++ b/src/net/address-converter.hpp
@@ -0,0 +1,96 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2017 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 ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_NET_ADDRESS_CONVERTER_HPP
+#define NDN_NET_ADDRESS_CONVERTER_HPP
+
+#include "../common.hpp"
+
+#include <boost/asio/ip/address.hpp>
+#include <boost/system/error_code.hpp>
+
+namespace ndn {
+namespace ip {
+
+/**
+ * \brief Convert scope ID of IPv6 address into interface name
+ *
+ * \return interface name, or ndn::nullopt if \p scopeId cannot be converted
+ */
+optional<std::string>
+scopeNameFromId(unsigned int scopeId);
+
+/**
+ * \brief parse and convert the input string into an IP address
+ *
+ * \param str the string to parse
+ *
+ * \return the converted IP address
+ * \throw boost::system::system_error in case of failure
+ */
+boost::asio::ip::address
+addressFromString(const std::string& str);
+
+/**
+ * \brief parse and convert the input string into an IP address
+ *
+ * \param str the string to parse
+ * \param ec the error code of failure in conversion
+ *
+ * \return the converted IP address, or a default-constructed
+ *         `boost::asio::ip::address` in case of failure
+ */
+boost::asio::ip::address
+addressFromString(const std::string& str, boost::system::error_code& ec);
+
+/**
+ * \brief parse and convert the input string into an IPv6 address
+ *
+ * \param str the string to parse
+ *
+ * \return the converted IPv6 address
+ * \throw boost::system::system_error in case of failure
+ */
+boost::asio::ip::address_v6
+addressV6FromString(const std::string& str);
+
+/**
+ * \brief parse and convert the input string into an IPv6 address
+ *
+ * \param str the string to parse
+ * \param ec the error code of failure in conversion
+ *
+ * \return the converted IPv6 address, or a default-constructed
+ *         `boost::asio::ip::address_v6` in case of failure
+ */
+boost::asio::ip::address_v6
+addressV6FromString(const std::string& str, boost::system::error_code& ec);
+
+} // namespace ip
+} // namespace ndn
+
+#endif // NDN_NET_ADDRESS_CONVERTER_HPP
diff --git a/src/net/face-uri.cpp b/src/net/face-uri.cpp
index 11e4fe5..3e9f358 100644
--- a/src/net/face-uri.cpp
+++ b/src/net/face-uri.cpp
@@ -26,8 +26,12 @@
  */
 
 #include "face-uri.hpp"
-#include "dns.hpp"
 
+#include "address-converter.hpp"
+#include "dns.hpp"
+#include "util/string-helper.hpp"
+
+#include <boost/algorithm/string.hpp>
 #include <boost/lexical_cast.hpp>
 #include <boost/mpl/vector.hpp>
 #include <boost/mpl/for_each.hpp>
@@ -74,6 +78,9 @@
   const std::string& authority = protocolMatch[3];
   m_path = protocolMatch[4];
 
+  // pattern for IPv6 link local address enclosed in [ ], with optional port number
+  static const boost::regex v6LinkLocalExp("^\\[([a-fA-F0-9:]+)%([a-zA-Z0-9]+)\\]"
+                                           "(?:\\:(\\d+))?$");
   // pattern for IPv6 address enclosed in [ ], with optional port number
   static const boost::regex v6Exp("^\\[([a-fA-F0-9:]+)\\](?:\\:(\\d+))?$");
   // pattern for Ethernet address in standard hex-digits-and-colons notation
@@ -88,6 +95,13 @@
   }
   else {
     boost::smatch match;
+    if (boost::regex_match(authority, match, v6LinkLocalExp)) {
+      m_isV6 = true;
+      m_host = match[1] + "%" + match[2];
+      m_port = match[3];
+      return true;
+    }
+
     m_isV6 = boost::regex_match(authority, match, v6Exp);
     if (m_isV6 ||
         boost::regex_match(authority, match, etherExp) ||
@@ -256,18 +270,43 @@
     }
 
     boost::system::error_code ec;
-    boost::asio::ip::address addr;
-    if (faceUri.getScheme() == m_v4Scheme) {
-      addr = boost::asio::ip::address_v4::from_string(faceUri.getHost(), ec);
-    }
-    else if (faceUri.getScheme() == m_v6Scheme) {
-      addr = boost::asio::ip::address_v6::from_string(faceUri.getHost(), ec);
-    }
-    else {
+    auto addr = ip::addressFromString(unescapeHost(faceUri.getHost()), ec);
+    if (ec) {
       return false;
     }
 
-    return !ec && addr.to_string() == faceUri.getHost() && checkAddress(addr).first;
+    bool hasCorrectScheme = (faceUri.getScheme() == m_v4Scheme && addr.is_v4()) ||
+                            (faceUri.getScheme() == m_v6Scheme && addr.is_v6());
+    if (!hasCorrectScheme) {
+      return false;
+    }
+
+    auto checkAddressWithUri = [] (const boost::asio::ip::address& addr,
+                                   const FaceUri& faceUri) -> bool {
+      if (addr.is_v4() || !addr.to_v6().is_link_local()) {
+        return addr.to_string() == faceUri.getHost();
+      }
+
+      std::vector<std::string> addrFields, faceUriFields;
+      std::string addrString = addr.to_string();
+      std::string faceUriString = faceUri.getHost();
+
+      boost::algorithm::split(addrFields, addrString, boost::is_any_of("%"));
+      boost::algorithm::split(faceUriFields, faceUriString, boost::is_any_of("%"));
+      if (addrFields.size() != 2 || faceUriFields.size() != 2) {
+        return false;
+      }
+
+      if (faceUriFields[1].size() > 2 && faceUriFields[1].compare(0, 2, "25") == 0) {
+        // %25... is accepted, but not a canonical form
+        return false;
+      }
+
+      return addrFields[0] == faceUriFields[0] &&
+             addrFields[1] == faceUriFields[1];
+    };
+
+    return checkAddressWithUri(addr, faceUri) && checkAddress(addr).first;
   }
 
   void
@@ -284,7 +323,7 @@
     // make a copy because caller may modify faceUri
     auto uri = make_shared<FaceUri>(faceUri);
     boost::system::error_code ec;
-    auto ipAddress = boost::asio::ip::address::from_string(faceUri.getHost(), ec);
+    auto ipAddress = ip::addressFromString(unescapeHost(faceUri.getHost()), ec);
     if (!ec) {
       // No need to resolve IP address if host is already an IP
       if ((faceUri.getScheme() == m_v4Scheme && !ipAddress.is_v4()) ||
@@ -307,7 +346,7 @@
         addressSelector = dns::AnyAddress();
       }
 
-      dns::asyncResolve(faceUri.getHost(),
+      dns::asyncResolve(unescapeHost(faceUri.getHost()),
         bind(&IpHostCanonizeProvider<Protocol>::onDnsSuccess, this, uri, onSuccess, onFailure, _1),
         bind(&IpHostCanonizeProvider<Protocol>::onDnsFailure, this, uri, onFailure, _1),
         io, addressSelector, timeout);
@@ -377,6 +416,16 @@
     return {true, ""};
   }
 
+  static std::string
+  unescapeHost(std::string host)
+  {
+    auto escapePos = host.find("%25");
+    if (escapePos != std::string::npos && escapePos < host.size() - 3) {
+      host = unescape(host);
+    }
+    return host;
+  }
+
 private:
   std::string m_baseScheme;
   std::string m_v4Scheme;