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