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