Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a84f464 | 2017-08-23 16:14:51 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | b984e32 | 2018-01-24 19:40:07 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California, |
Junxiao Shi | d5c2f0c | 2017-04-04 09:52:11 +0000 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 12 | * |
| 13 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 14 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 15 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 18 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 19 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 20 | * |
| 21 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 22 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 23 | * <http://www.gnu.org/licenses/>. |
| 24 | * |
| 25 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 26 | */ |
| 27 | |
| 28 | #include "face-uri.hpp" |
| 29 | |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 30 | #include "address-converter.hpp" |
| 31 | #include "dns.hpp" |
| 32 | #include "util/string-helper.hpp" |
| 33 | |
| 34 | #include <boost/algorithm/string.hpp> |
Junxiao Shi | e3ef6ee | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 35 | #include <boost/lexical_cast.hpp> |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 36 | #include <boost/mpl/vector.hpp> |
| 37 | #include <boost/mpl/for_each.hpp> |
Davide Pesavento | 90db7ee | 2018-06-10 11:33:31 -0400 | [diff] [blame] | 38 | |
| 39 | #include <regex> |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 40 | #include <set> |
Davide Pesavento | a84f464 | 2017-08-23 16:14:51 -0400 | [diff] [blame] | 41 | #include <sstream> |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 42 | |
| 43 | namespace ndn { |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 44 | |
| 45 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<FaceUri>)); |
| 46 | |
| 47 | FaceUri::FaceUri() |
| 48 | : m_isV6(false) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | FaceUri::FaceUri(const std::string& uri) |
| 53 | { |
| 54 | if (!parse(uri)) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 55 | BOOST_THROW_EXCEPTION(Error("Malformed URI: " + uri)); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | FaceUri::FaceUri(const char* uri) |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 60 | : FaceUri(std::string(uri)) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 61 | { |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | bool |
| 65 | FaceUri::parse(const std::string& uri) |
| 66 | { |
| 67 | m_scheme.clear(); |
| 68 | m_host.clear(); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 69 | m_port.clear(); |
| 70 | m_path.clear(); |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 71 | m_isV6 = false; |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 72 | |
Davide Pesavento | 90db7ee | 2018-06-10 11:33:31 -0400 | [diff] [blame] | 73 | static const std::regex protocolExp("(\\w+\\d?(\\+\\w+)?)://([^/]*)(\\/[^?]*)?"); |
| 74 | std::smatch protocolMatch; |
| 75 | if (!std::regex_match(uri, protocolMatch, protocolExp)) { |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 76 | return false; |
| 77 | } |
| 78 | m_scheme = protocolMatch[1]; |
Davide Pesavento | 90db7ee | 2018-06-10 11:33:31 -0400 | [diff] [blame] | 79 | std::string authority = protocolMatch[3]; |
Weiwei Liu | d7f4fda | 2016-10-19 22:38:39 -0700 | [diff] [blame] | 80 | m_path = protocolMatch[4]; |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 81 | |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 82 | // pattern for IPv6 link local address enclosed in [ ], with optional port number |
Davide Pesavento | 90db7ee | 2018-06-10 11:33:31 -0400 | [diff] [blame] | 83 | static const std::regex v6LinkLocalExp("^\\[([a-fA-F0-9:]+)%([^\\s/:]+)\\](?:\\:(\\d+))?$"); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 84 | // pattern for IPv6 address enclosed in [ ], with optional port number |
Davide Pesavento | 90db7ee | 2018-06-10 11:33:31 -0400 | [diff] [blame] | 85 | static const std::regex v6Exp("^\\[([a-fA-F0-9:]+)\\](?:\\:(\\d+))?$"); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 86 | // pattern for Ethernet address in standard hex-digits-and-colons notation |
Davide Pesavento | 90db7ee | 2018-06-10 11:33:31 -0400 | [diff] [blame] | 87 | static const std::regex etherExp("^\\[((?:[a-fA-F0-9]{1,2}\\:){5}(?:[a-fA-F0-9]{1,2}))\\]$"); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 88 | // pattern for IPv4-mapped IPv6 address, with optional port number |
Davide Pesavento | 90db7ee | 2018-06-10 11:33:31 -0400 | [diff] [blame] | 89 | static const std::regex v4MappedV6Exp("^\\[::ffff:(\\d+(?:\\.\\d+){3})\\](?:\\:(\\d+))?$"); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 90 | // pattern for IPv4/hostname/fd/ifname, with optional port number |
Davide Pesavento | 90db7ee | 2018-06-10 11:33:31 -0400 | [diff] [blame] | 91 | static const std::regex v4HostExp("^([^:]+)(?:\\:(\\d+))?$"); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 92 | |
| 93 | if (authority.empty()) { |
| 94 | // UNIX, internal |
| 95 | } |
| 96 | else { |
Davide Pesavento | 90db7ee | 2018-06-10 11:33:31 -0400 | [diff] [blame] | 97 | std::smatch match; |
| 98 | if (std::regex_match(authority, match, v6LinkLocalExp)) { |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 99 | m_isV6 = true; |
Davide Pesavento | 90db7ee | 2018-06-10 11:33:31 -0400 | [diff] [blame] | 100 | m_host = match[1].str() + "%" + match[2].str(); |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 101 | m_port = match[3]; |
| 102 | return true; |
| 103 | } |
| 104 | |
Davide Pesavento | 90db7ee | 2018-06-10 11:33:31 -0400 | [diff] [blame] | 105 | m_isV6 = std::regex_match(authority, match, v6Exp); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 106 | if (m_isV6 || |
Davide Pesavento | 90db7ee | 2018-06-10 11:33:31 -0400 | [diff] [blame] | 107 | std::regex_match(authority, match, etherExp) || |
| 108 | std::regex_match(authority, match, v4MappedV6Exp) || |
| 109 | std::regex_match(authority, match, v4HostExp)) { |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 110 | m_host = match[1]; |
| 111 | m_port = match[2]; |
| 112 | } |
| 113 | else { |
| 114 | return false; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | FaceUri::FaceUri(const boost::asio::ip::udp::endpoint& endpoint) |
| 122 | { |
| 123 | m_isV6 = endpoint.address().is_v6(); |
| 124 | m_scheme = m_isV6 ? "udp6" : "udp4"; |
| 125 | m_host = endpoint.address().to_string(); |
Weiwei Liu | d7f4fda | 2016-10-19 22:38:39 -0700 | [diff] [blame] | 126 | m_port = to_string(endpoint.port()); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | FaceUri::FaceUri(const boost::asio::ip::tcp::endpoint& endpoint) |
| 130 | { |
| 131 | m_isV6 = endpoint.address().is_v6(); |
| 132 | m_scheme = m_isV6 ? "tcp6" : "tcp4"; |
| 133 | m_host = endpoint.address().to_string(); |
Weiwei Liu | d7f4fda | 2016-10-19 22:38:39 -0700 | [diff] [blame] | 134 | m_port = to_string(endpoint.port()); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | FaceUri::FaceUri(const boost::asio::ip::tcp::endpoint& endpoint, const std::string& scheme) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 138 | { |
| 139 | m_isV6 = endpoint.address().is_v6(); |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 140 | m_scheme = scheme; |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 141 | m_host = endpoint.address().to_string(); |
Weiwei Liu | d7f4fda | 2016-10-19 22:38:39 -0700 | [diff] [blame] | 142 | m_port = to_string(endpoint.port()); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | #ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS |
| 146 | FaceUri::FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint) |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 147 | : m_scheme("unix") |
| 148 | , m_path(endpoint.path()) |
| 149 | , m_isV6(false) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 150 | { |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 151 | } |
| 152 | #endif // BOOST_ASIO_HAS_LOCAL_SOCKETS |
| 153 | |
| 154 | FaceUri |
| 155 | FaceUri::fromFd(int fd) |
| 156 | { |
| 157 | FaceUri uri; |
| 158 | uri.m_scheme = "fd"; |
Weiwei Liu | d7f4fda | 2016-10-19 22:38:39 -0700 | [diff] [blame] | 159 | uri.m_host = to_string(fd); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 160 | return uri; |
| 161 | } |
| 162 | |
| 163 | FaceUri::FaceUri(const ethernet::Address& address) |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 164 | : m_scheme("ether") |
| 165 | , m_host(address.toString()) |
| 166 | , m_isV6(true) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 167 | { |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | FaceUri |
| 171 | FaceUri::fromDev(const std::string& ifname) |
| 172 | { |
| 173 | FaceUri uri; |
| 174 | uri.m_scheme = "dev"; |
| 175 | uri.m_host = ifname; |
| 176 | return uri; |
| 177 | } |
| 178 | |
Weiwei Liu | d7f4fda | 2016-10-19 22:38:39 -0700 | [diff] [blame] | 179 | FaceUri |
| 180 | FaceUri::fromUdpDev(const boost::asio::ip::udp::endpoint& endpoint, const std::string& ifname) |
| 181 | { |
| 182 | FaceUri uri; |
| 183 | uri.m_scheme = endpoint.address().is_v6() ? "udp6+dev" : "udp4+dev"; |
| 184 | uri.m_host = ifname; |
| 185 | uri.m_port = to_string(endpoint.port()); |
| 186 | return uri; |
| 187 | } |
| 188 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 189 | bool |
| 190 | FaceUri::operator==(const FaceUri& rhs) const |
| 191 | { |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 192 | return m_isV6 == rhs.m_isV6 && |
| 193 | m_scheme == rhs.m_scheme && |
| 194 | m_host == rhs.m_host && |
| 195 | m_port == rhs.m_port && |
| 196 | m_path == rhs.m_path; |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | bool |
| 200 | FaceUri::operator!=(const FaceUri& rhs) const |
| 201 | { |
| 202 | return !(*this == rhs); |
| 203 | } |
| 204 | |
| 205 | std::string |
| 206 | FaceUri::toString() const |
| 207 | { |
| 208 | std::ostringstream os; |
| 209 | os << *this; |
| 210 | return os.str(); |
| 211 | } |
| 212 | |
| 213 | std::ostream& |
| 214 | operator<<(std::ostream& os, const FaceUri& uri) |
| 215 | { |
| 216 | os << uri.m_scheme << "://"; |
| 217 | if (uri.m_isV6) { |
| 218 | os << "[" << uri.m_host << "]"; |
| 219 | } |
| 220 | else { |
| 221 | os << uri.m_host; |
| 222 | } |
| 223 | if (!uri.m_port.empty()) { |
| 224 | os << ":" << uri.m_port; |
| 225 | } |
| 226 | os << uri.m_path; |
| 227 | return os; |
| 228 | } |
| 229 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 230 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 231 | /** \brief a CanonizeProvider provides FaceUri canonization functionality for a group of schemes |
| 232 | */ |
| 233 | class CanonizeProvider : noncopyable |
| 234 | { |
| 235 | public: |
| 236 | virtual |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 237 | ~CanonizeProvider() = default; |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 238 | |
| 239 | virtual std::set<std::string> |
| 240 | getSchemes() const = 0; |
| 241 | |
| 242 | virtual bool |
| 243 | isCanonical(const FaceUri& faceUri) const = 0; |
| 244 | |
| 245 | virtual void |
| 246 | canonize(const FaceUri& faceUri, |
| 247 | const FaceUri::CanonizeSuccessCallback& onSuccess, |
| 248 | const FaceUri::CanonizeFailureCallback& onFailure, |
Junxiao Shi | d5c2f0c | 2017-04-04 09:52:11 +0000 | [diff] [blame] | 249 | boost::asio::io_service& io, time::nanoseconds timeout) const = 0; |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | template<typename Protocol> |
| 253 | class IpHostCanonizeProvider : public CanonizeProvider |
| 254 | { |
| 255 | public: |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 256 | std::set<std::string> |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 257 | getSchemes() const override |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 258 | { |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 259 | return {m_baseScheme, m_v4Scheme, m_v6Scheme}; |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 262 | bool |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 263 | isCanonical(const FaceUri& faceUri) const override |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 264 | { |
| 265 | if (faceUri.getPort().empty()) { |
| 266 | return false; |
| 267 | } |
| 268 | if (!faceUri.getPath().empty()) { |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | boost::system::error_code ec; |
Davide Pesavento | 90db7ee | 2018-06-10 11:33:31 -0400 | [diff] [blame] | 273 | auto addr = boost::asio::ip::address::from_string(unescapeHost(faceUri.getHost()), ec); |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 274 | if (ec) { |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 275 | return false; |
| 276 | } |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 277 | |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 278 | bool hasCorrectScheme = (faceUri.getScheme() == m_v4Scheme && addr.is_v4()) || |
| 279 | (faceUri.getScheme() == m_v6Scheme && addr.is_v6()); |
| 280 | if (!hasCorrectScheme) { |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | auto checkAddressWithUri = [] (const boost::asio::ip::address& addr, |
| 285 | const FaceUri& faceUri) -> bool { |
| 286 | if (addr.is_v4() || !addr.to_v6().is_link_local()) { |
| 287 | return addr.to_string() == faceUri.getHost(); |
| 288 | } |
| 289 | |
| 290 | std::vector<std::string> addrFields, faceUriFields; |
| 291 | std::string addrString = addr.to_string(); |
| 292 | std::string faceUriString = faceUri.getHost(); |
| 293 | |
| 294 | boost::algorithm::split(addrFields, addrString, boost::is_any_of("%")); |
| 295 | boost::algorithm::split(faceUriFields, faceUriString, boost::is_any_of("%")); |
| 296 | if (addrFields.size() != 2 || faceUriFields.size() != 2) { |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | if (faceUriFields[1].size() > 2 && faceUriFields[1].compare(0, 2, "25") == 0) { |
| 301 | // %25... is accepted, but not a canonical form |
| 302 | return false; |
| 303 | } |
| 304 | |
| 305 | return addrFields[0] == faceUriFields[0] && |
| 306 | addrFields[1] == faceUriFields[1]; |
| 307 | }; |
| 308 | |
| 309 | return checkAddressWithUri(addr, faceUri) && checkAddress(addr).first; |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 312 | void |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 313 | canonize(const FaceUri& faceUri, |
| 314 | const FaceUri::CanonizeSuccessCallback& onSuccess, |
| 315 | const FaceUri::CanonizeFailureCallback& onFailure, |
Junxiao Shi | d5c2f0c | 2017-04-04 09:52:11 +0000 | [diff] [blame] | 316 | boost::asio::io_service& io, time::nanoseconds timeout) const override |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 317 | { |
| 318 | if (this->isCanonical(faceUri)) { |
| 319 | onSuccess(faceUri); |
| 320 | return; |
| 321 | } |
| 322 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 323 | // make a copy because caller may modify faceUri |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 324 | auto uri = make_shared<FaceUri>(faceUri); |
Eric Newberry | 71aecae | 2017-05-29 00:22:39 -0700 | [diff] [blame] | 325 | boost::system::error_code ec; |
Davide Pesavento | 90db7ee | 2018-06-10 11:33:31 -0400 | [diff] [blame] | 326 | auto ipAddress = boost::asio::ip::address::from_string(unescapeHost(faceUri.getHost()), ec); |
Eric Newberry | 71aecae | 2017-05-29 00:22:39 -0700 | [diff] [blame] | 327 | if (!ec) { |
| 328 | // No need to resolve IP address if host is already an IP |
| 329 | if ((faceUri.getScheme() == m_v4Scheme && !ipAddress.is_v4()) || |
| 330 | (faceUri.getScheme() == m_v6Scheme && !ipAddress.is_v6())) { |
| 331 | return onFailure("IPv4/v6 mismatch"); |
| 332 | } |
| 333 | |
| 334 | onDnsSuccess(uri, onSuccess, onFailure, ipAddress); |
| 335 | } |
| 336 | else { |
| 337 | dns::AddressSelector addressSelector; |
| 338 | if (faceUri.getScheme() == m_v4Scheme) { |
| 339 | addressSelector = dns::Ipv4Only(); |
| 340 | } |
| 341 | else if (faceUri.getScheme() == m_v6Scheme) { |
| 342 | addressSelector = dns::Ipv6Only(); |
| 343 | } |
| 344 | else { |
| 345 | BOOST_ASSERT(faceUri.getScheme() == m_baseScheme); |
| 346 | addressSelector = dns::AnyAddress(); |
| 347 | } |
| 348 | |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 349 | dns::asyncResolve(unescapeHost(faceUri.getHost()), |
Eric Newberry | 71aecae | 2017-05-29 00:22:39 -0700 | [diff] [blame] | 350 | bind(&IpHostCanonizeProvider<Protocol>::onDnsSuccess, this, uri, onSuccess, onFailure, _1), |
| 351 | bind(&IpHostCanonizeProvider<Protocol>::onDnsFailure, this, uri, onFailure, _1), |
| 352 | io, addressSelector, timeout); |
| 353 | } |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | protected: |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 357 | explicit |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 358 | IpHostCanonizeProvider(const std::string& baseScheme, |
Davide Pesavento | f78cb70 | 2016-12-11 14:42:40 -0500 | [diff] [blame] | 359 | uint16_t defaultUnicastPort = 6363, |
| 360 | uint16_t defaultMulticastPort = 56363) |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 361 | : m_baseScheme(baseScheme) |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 362 | , m_v4Scheme(baseScheme + '4') |
| 363 | , m_v6Scheme(baseScheme + '6') |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 364 | , m_defaultUnicastPort(defaultUnicastPort) |
| 365 | , m_defaultMulticastPort(defaultMulticastPort) |
| 366 | { |
| 367 | } |
| 368 | |
| 369 | private: |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 370 | void |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 371 | onDnsSuccess(const shared_ptr<FaceUri>& faceUri, |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 372 | const FaceUri::CanonizeSuccessCallback& onSuccess, |
| 373 | const FaceUri::CanonizeFailureCallback& onFailure, |
| 374 | const dns::IpAddress& ipAddress) const |
| 375 | { |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 376 | bool isOk = false; |
| 377 | std::string reason; |
| 378 | std::tie(isOk, reason) = this->checkAddress(ipAddress); |
| 379 | if (!isOk) { |
| 380 | return onFailure(reason); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Davide Pesavento | f78cb70 | 2016-12-11 14:42:40 -0500 | [diff] [blame] | 383 | uint16_t port = 0; |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 384 | if (faceUri->getPort().empty()) { |
| 385 | port = ipAddress.is_multicast() ? m_defaultMulticastPort : m_defaultUnicastPort; |
| 386 | } |
| 387 | else { |
| 388 | try { |
Davide Pesavento | f78cb70 | 2016-12-11 14:42:40 -0500 | [diff] [blame] | 389 | port = boost::lexical_cast<uint16_t>(faceUri->getPort()); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 390 | } |
Davide Pesavento | f78cb70 | 2016-12-11 14:42:40 -0500 | [diff] [blame] | 391 | catch (const boost::bad_lexical_cast&) { |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 392 | return onFailure("invalid port number '" + faceUri->getPort() + "'"); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
| 396 | FaceUri canonicalUri(typename Protocol::endpoint(ipAddress, port)); |
| 397 | BOOST_ASSERT(canonicalUri.isCanonical()); |
| 398 | onSuccess(canonicalUri); |
| 399 | } |
| 400 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 401 | void |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 402 | onDnsFailure(const shared_ptr<FaceUri>& faceUri, |
| 403 | const FaceUri::CanonizeFailureCallback& onFailure, |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 404 | const std::string& reason) const |
| 405 | { |
| 406 | onFailure(reason); |
| 407 | } |
| 408 | |
| 409 | /** \brief when overriden in a subclass, check the IP address is allowable |
| 410 | * \return (true,ignored) if the address is allowable; |
| 411 | * (false,reason) if the address is not allowable. |
| 412 | */ |
| 413 | virtual std::pair<bool, std::string> |
| 414 | checkAddress(const dns::IpAddress& ipAddress) const |
| 415 | { |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 416 | return {true, ""}; |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 419 | static std::string |
| 420 | unescapeHost(std::string host) |
| 421 | { |
| 422 | auto escapePos = host.find("%25"); |
| 423 | if (escapePos != std::string::npos && escapePos < host.size() - 3) { |
| 424 | host = unescape(host); |
| 425 | } |
| 426 | return host; |
| 427 | } |
| 428 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 429 | private: |
| 430 | std::string m_baseScheme; |
| 431 | std::string m_v4Scheme; |
| 432 | std::string m_v6Scheme; |
Davide Pesavento | f78cb70 | 2016-12-11 14:42:40 -0500 | [diff] [blame] | 433 | uint16_t m_defaultUnicastPort; |
| 434 | uint16_t m_defaultMulticastPort; |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 435 | }; |
| 436 | |
| 437 | class UdpCanonizeProvider : public IpHostCanonizeProvider<boost::asio::ip::udp> |
| 438 | { |
| 439 | public: |
| 440 | UdpCanonizeProvider() |
| 441 | : IpHostCanonizeProvider("udp") |
| 442 | { |
| 443 | } |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 444 | }; |
| 445 | |
| 446 | class TcpCanonizeProvider : public IpHostCanonizeProvider<boost::asio::ip::tcp> |
| 447 | { |
| 448 | public: |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 449 | TcpCanonizeProvider() |
| 450 | : IpHostCanonizeProvider("tcp") |
| 451 | { |
| 452 | } |
| 453 | |
| 454 | protected: |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 455 | std::pair<bool, std::string> |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 456 | checkAddress(const dns::IpAddress& ipAddress) const override |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 457 | { |
| 458 | if (ipAddress.is_multicast()) { |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 459 | return {false, "cannot use multicast address"}; |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 460 | } |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 461 | return {true, ""}; |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 462 | } |
| 463 | }; |
| 464 | |
| 465 | class EtherCanonizeProvider : public CanonizeProvider |
| 466 | { |
| 467 | public: |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 468 | std::set<std::string> |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 469 | getSchemes() const override |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 470 | { |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 471 | return {"ether"}; |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 474 | bool |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 475 | isCanonical(const FaceUri& faceUri) const override |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 476 | { |
| 477 | if (!faceUri.getPort().empty()) { |
| 478 | return false; |
| 479 | } |
| 480 | if (!faceUri.getPath().empty()) { |
| 481 | return false; |
| 482 | } |
| 483 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 484 | auto addr = ethernet::Address::fromString(faceUri.getHost()); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 485 | return addr.toString() == faceUri.getHost(); |
| 486 | } |
| 487 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 488 | void |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 489 | canonize(const FaceUri& faceUri, |
| 490 | const FaceUri::CanonizeSuccessCallback& onSuccess, |
| 491 | const FaceUri::CanonizeFailureCallback& onFailure, |
Junxiao Shi | d5c2f0c | 2017-04-04 09:52:11 +0000 | [diff] [blame] | 492 | boost::asio::io_service& io, time::nanoseconds timeout) const override |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 493 | { |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 494 | auto addr = ethernet::Address::fromString(faceUri.getHost()); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 495 | if (addr.isNull()) { |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 496 | return onFailure("invalid ethernet address '" + faceUri.getHost() + "'"); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | FaceUri canonicalUri(addr); |
| 500 | BOOST_ASSERT(canonicalUri.isCanonical()); |
| 501 | onSuccess(canonicalUri); |
| 502 | } |
| 503 | }; |
| 504 | |
Junxiao Shi | d5c2f0c | 2017-04-04 09:52:11 +0000 | [diff] [blame] | 505 | class DevCanonizeProvider : public CanonizeProvider |
| 506 | { |
| 507 | public: |
| 508 | std::set<std::string> |
| 509 | getSchemes() const override |
| 510 | { |
| 511 | return {"dev"}; |
| 512 | } |
| 513 | |
| 514 | bool |
| 515 | isCanonical(const FaceUri& faceUri) const override |
| 516 | { |
| 517 | return !faceUri.getHost().empty() && faceUri.getPort().empty() && faceUri.getPath().empty(); |
| 518 | } |
| 519 | |
| 520 | void |
| 521 | canonize(const FaceUri& faceUri, |
| 522 | const FaceUri::CanonizeSuccessCallback& onSuccess, |
| 523 | const FaceUri::CanonizeFailureCallback& onFailure, |
| 524 | boost::asio::io_service& io, time::nanoseconds timeout) const override |
| 525 | { |
| 526 | if (faceUri.getHost().empty()) { |
| 527 | onFailure("network interface name is missing"); |
| 528 | return; |
| 529 | } |
| 530 | if (!faceUri.getPort().empty()) { |
| 531 | onFailure("port number is not allowed"); |
| 532 | return; |
| 533 | } |
| 534 | if (!faceUri.getPath().empty() && faceUri.getPath() != "/") { // permit trailing slash only |
| 535 | onFailure("path is not allowed"); |
| 536 | return; |
| 537 | } |
| 538 | |
| 539 | FaceUri canonicalUri = FaceUri::fromDev(faceUri.getHost()); |
| 540 | BOOST_ASSERT(canonicalUri.isCanonical()); |
| 541 | onSuccess(canonicalUri); |
| 542 | } |
| 543 | }; |
| 544 | |
Weiwei Liu | d7f4fda | 2016-10-19 22:38:39 -0700 | [diff] [blame] | 545 | class UdpDevCanonizeProvider : public CanonizeProvider |
| 546 | { |
| 547 | public: |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 548 | std::set<std::string> |
Weiwei Liu | d7f4fda | 2016-10-19 22:38:39 -0700 | [diff] [blame] | 549 | getSchemes() const override |
| 550 | { |
| 551 | return {"udp4+dev", "udp6+dev"}; |
| 552 | } |
| 553 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 554 | bool |
Weiwei Liu | d7f4fda | 2016-10-19 22:38:39 -0700 | [diff] [blame] | 555 | isCanonical(const FaceUri& faceUri) const override |
| 556 | { |
| 557 | if (faceUri.getPort().empty()) { |
| 558 | return false; |
| 559 | } |
| 560 | if (!faceUri.getPath().empty()) { |
| 561 | return false; |
| 562 | } |
| 563 | return true; |
| 564 | } |
| 565 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 566 | void |
Weiwei Liu | d7f4fda | 2016-10-19 22:38:39 -0700 | [diff] [blame] | 567 | canonize(const FaceUri& faceUri, |
| 568 | const FaceUri::CanonizeSuccessCallback& onSuccess, |
| 569 | const FaceUri::CanonizeFailureCallback& onFailure, |
Junxiao Shi | d5c2f0c | 2017-04-04 09:52:11 +0000 | [diff] [blame] | 570 | boost::asio::io_service& io, time::nanoseconds timeout) const override |
Weiwei Liu | d7f4fda | 2016-10-19 22:38:39 -0700 | [diff] [blame] | 571 | { |
| 572 | if (this->isCanonical(faceUri)) { |
| 573 | onSuccess(faceUri); |
| 574 | } |
| 575 | else { |
| 576 | onFailure("cannot canonize " + faceUri.toString()); |
| 577 | } |
| 578 | } |
| 579 | }; |
| 580 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 581 | using CanonizeProviders = boost::mpl::vector<UdpCanonizeProvider*, |
| 582 | TcpCanonizeProvider*, |
| 583 | EtherCanonizeProvider*, |
Junxiao Shi | d5c2f0c | 2017-04-04 09:52:11 +0000 | [diff] [blame] | 584 | DevCanonizeProvider*, |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 585 | UdpDevCanonizeProvider*>; |
| 586 | using CanonizeProviderTable = std::map<std::string, shared_ptr<CanonizeProvider>>; |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 587 | |
| 588 | class CanonizeProviderTableInitializer |
| 589 | { |
| 590 | public: |
| 591 | explicit |
| 592 | CanonizeProviderTableInitializer(CanonizeProviderTable& providerTable) |
| 593 | : m_providerTable(providerTable) |
| 594 | { |
| 595 | } |
| 596 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 597 | template<typename CP> |
| 598 | void |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 599 | operator()(CP*) |
| 600 | { |
| 601 | shared_ptr<CanonizeProvider> cp = make_shared<CP>(); |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 602 | auto schemes = cp->getSchemes(); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 603 | BOOST_ASSERT(!schemes.empty()); |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 604 | |
| 605 | for (const auto& scheme : schemes) { |
| 606 | BOOST_ASSERT(m_providerTable.count(scheme) == 0); |
| 607 | m_providerTable[scheme] = cp; |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
| 611 | private: |
| 612 | CanonizeProviderTable& m_providerTable; |
| 613 | }; |
| 614 | |
| 615 | static const CanonizeProvider* |
| 616 | getCanonizeProvider(const std::string& scheme) |
| 617 | { |
| 618 | static CanonizeProviderTable providerTable; |
| 619 | if (providerTable.empty()) { |
| 620 | boost::mpl::for_each<CanonizeProviders>(CanonizeProviderTableInitializer(providerTable)); |
| 621 | BOOST_ASSERT(!providerTable.empty()); |
| 622 | } |
| 623 | |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 624 | auto it = providerTable.find(scheme); |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 625 | return it == providerTable.end() ? nullptr : it->second.get(); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 628 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 629 | bool |
| 630 | FaceUri::canCanonize(const std::string& scheme) |
| 631 | { |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 632 | return getCanonizeProvider(scheme) != nullptr; |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | bool |
| 636 | FaceUri::isCanonical() const |
| 637 | { |
| 638 | const CanonizeProvider* cp = getCanonizeProvider(this->getScheme()); |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 639 | if (cp == nullptr) { |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 640 | return false; |
| 641 | } |
| 642 | |
| 643 | return cp->isCanonical(*this); |
| 644 | } |
| 645 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 646 | void |
| 647 | FaceUri::canonize(const CanonizeSuccessCallback& onSuccess, |
| 648 | const CanonizeFailureCallback& onFailure, |
Junxiao Shi | d5c2f0c | 2017-04-04 09:52:11 +0000 | [diff] [blame] | 649 | boost::asio::io_service& io, time::nanoseconds timeout) const |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 650 | { |
| 651 | const CanonizeProvider* cp = getCanonizeProvider(this->getScheme()); |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 652 | if (cp == nullptr) { |
| 653 | if (onFailure) { |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 654 | onFailure("scheme not supported"); |
| 655 | } |
| 656 | return; |
| 657 | } |
| 658 | |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 659 | static CanonizeSuccessCallback successNop = bind([]{}); |
| 660 | static CanonizeFailureCallback failureNop = bind([]{}); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 661 | cp->canonize(*this, |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 662 | onSuccess ? onSuccess : successNop, |
| 663 | onFailure ? onFailure : failureNop, |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 664 | io, timeout); |
| 665 | } |
| 666 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 667 | } // namespace ndn |