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