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