Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | f748a4e | 2017-07-05 23:41:48 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [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. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "ndn-fch-discovery.hpp" |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 27 | |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 28 | #include <boost/algorithm/string.hpp> |
Davide Pesavento | 45898d2 | 2018-04-17 21:59:54 -0400 | [diff] [blame] | 29 | #include <boost/asio/ip/tcp.hpp> |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 30 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 31 | #include <regex> |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 32 | #include <sstream> |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 33 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 34 | namespace ndn::autoconfig { |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * A partial and specialized copy of ndn::FaceUri implementation |
| 38 | * |
| 39 | * Consider removing in favor of a library-provided URL parsing, if project |
| 40 | * includes such a library. |
| 41 | */ |
| 42 | class Url |
| 43 | { |
| 44 | public: |
| 45 | Url(const std::string& url) |
| 46 | : m_isValid(false) |
| 47 | { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 48 | static const std::regex protocolExp("(\\w+\\d?(\\+\\w+)?)://([^/]*)(\\/[^?]*)?"); |
| 49 | std::smatch protocolMatch; |
| 50 | if (!std::regex_match(url, protocolMatch, protocolExp)) { |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 51 | return; |
| 52 | } |
| 53 | m_scheme = protocolMatch[1]; |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 54 | std::string authority = protocolMatch[3]; |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 55 | m_path = protocolMatch[4]; |
| 56 | |
| 57 | // pattern for IPv6 address enclosed in [ ], with optional port number |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 58 | static const std::regex v6Exp("^\\[([a-fA-F0-9:]+)\\](?:\\:(\\d+))?$"); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 59 | // pattern for IPv4-mapped IPv6 address, with optional port number |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 60 | static const std::regex v4MappedV6Exp("^\\[::ffff:(\\d+(?:\\.\\d+){3})\\](?:\\:(\\d+))?$"); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 61 | // pattern for IPv4/hostname/fd/ifname, with optional port number |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 62 | static const std::regex v4HostExp("^([^:]+)(?:\\:(\\d+))?$"); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 63 | |
| 64 | if (authority.empty()) { |
| 65 | // UNIX, internal |
| 66 | } |
| 67 | else { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 68 | std::smatch match; |
| 69 | bool isV6 = std::regex_match(authority, match, v6Exp); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 70 | if (isV6 || |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 71 | std::regex_match(authority, match, v4MappedV6Exp) || |
| 72 | std::regex_match(authority, match, v4HostExp)) { |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 73 | m_host = match[1]; |
| 74 | m_port = match[2]; |
| 75 | } |
| 76 | else { |
| 77 | return; |
| 78 | } |
| 79 | } |
| 80 | if (m_port.empty()) { |
| 81 | m_port = "80"; |
| 82 | } |
| 83 | if (m_path.empty()) { |
| 84 | m_path = "/"; |
| 85 | } |
| 86 | m_isValid = true; |
| 87 | } |
| 88 | |
| 89 | bool |
| 90 | isValid() const |
| 91 | { |
| 92 | return m_isValid; |
| 93 | } |
| 94 | |
| 95 | const std::string& |
| 96 | getScheme() const |
| 97 | { |
| 98 | return m_scheme; |
| 99 | } |
| 100 | |
| 101 | const std::string& |
| 102 | getHost() const |
| 103 | { |
| 104 | return m_host; |
| 105 | } |
| 106 | |
| 107 | const std::string& |
| 108 | getPort() const |
| 109 | { |
| 110 | return m_port; |
| 111 | } |
| 112 | |
| 113 | const std::string& |
| 114 | getPath() const |
| 115 | { |
| 116 | return m_path; |
| 117 | } |
| 118 | |
| 119 | private: |
| 120 | bool m_isValid; |
| 121 | std::string m_scheme; |
| 122 | std::string m_host; |
| 123 | std::string m_port; |
| 124 | std::string m_path; |
| 125 | }; |
| 126 | |
| 127 | class HttpException : public std::runtime_error |
| 128 | { |
| 129 | public: |
| 130 | explicit |
| 131 | HttpException(const std::string& what) |
| 132 | : std::runtime_error(what) |
| 133 | { |
| 134 | } |
| 135 | }; |
| 136 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 137 | NdnFchDiscovery::NdnFchDiscovery(const std::string& url) |
| 138 | : m_url(url) |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 139 | { |
| 140 | } |
| 141 | |
| 142 | void |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 143 | NdnFchDiscovery::doStart() |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 144 | { |
| 145 | try { |
Davide Pesavento | 45898d2 | 2018-04-17 21:59:54 -0400 | [diff] [blame] | 146 | boost::asio::ip::tcp::iostream requestStream; |
| 147 | #if BOOST_VERSION >= 106700 |
| 148 | requestStream.expires_after(std::chrono::seconds(3)); |
| 149 | #else |
| 150 | requestStream.expires_from_now(boost::posix_time::seconds(3)); |
| 151 | #endif // BOOST_VERSION >= 106700 |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 152 | |
| 153 | Url url(m_url); |
| 154 | if (!url.isValid()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 155 | NDN_THROW(HttpException("Invalid NDN-FCH URL: " + m_url)); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 156 | } |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 157 | if (!boost::iequals(url.getScheme(), "http")) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 158 | NDN_THROW(HttpException("Only http:// NDN-FCH URLs are supported")); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | requestStream.connect(url.getHost(), url.getPort()); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 162 | if (!requestStream) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 163 | NDN_THROW(HttpException("HTTP connection error to " + m_url)); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | requestStream << "GET " << url.getPath() << " HTTP/1.0\r\n"; |
| 167 | requestStream << "Host: " << url.getHost() << ":" << url.getPort() << "\r\n"; |
| 168 | requestStream << "Accept: */*\r\n"; |
| 169 | requestStream << "Cache-Control: no-cache\r\n"; |
| 170 | requestStream << "Connection: close\r\n\r\n"; |
| 171 | requestStream.flush(); |
| 172 | |
| 173 | std::string statusLine; |
| 174 | std::getline(requestStream, statusLine); |
| 175 | if (!requestStream) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 176 | NDN_THROW(HttpException("HTTP communication error")); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | std::stringstream responseStream(statusLine); |
| 180 | std::string httpVersion; |
| 181 | responseStream >> httpVersion; |
| 182 | unsigned int statusCode; |
| 183 | responseStream >> statusCode; |
| 184 | std::string statusMessage; |
| 185 | |
| 186 | std::getline(responseStream, statusMessage); |
| 187 | if (!static_cast<bool>(requestStream) || httpVersion.substr(0, 5) != "HTTP/") { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 188 | NDN_THROW(HttpException("HTTP communication error")); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 189 | } |
| 190 | if (statusCode != 200) { |
| 191 | boost::trim(statusMessage); |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 192 | NDN_THROW(HttpException("HTTP request failed: " + to_string(statusCode) + " " + statusMessage)); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 193 | } |
| 194 | std::string header; |
| 195 | while (std::getline(requestStream, header) && header != "\r") |
| 196 | ; |
| 197 | |
| 198 | std::string hubHost; |
| 199 | requestStream >> hubHost; |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 200 | if (hubHost.empty()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 201 | NDN_THROW(HttpException("NDN-FCH did not return hub host")); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 204 | this->provideHubFaceUri("udp://" + hubHost); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 205 | } |
| 206 | catch (const std::runtime_error& e) { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 207 | this->fail(e.what()); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 211 | } // namespace ndn::autoconfig |