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