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