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 | 45898d2 | 2018-04-17 21:59:54 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, 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 | |
| 34 | namespace ndn { |
| 35 | namespace tools { |
| 36 | namespace autoconfig { |
| 37 | |
| 38 | /** |
| 39 | * A partial and specialized copy of ndn::FaceUri implementation |
| 40 | * |
| 41 | * Consider removing in favor of a library-provided URL parsing, if project |
| 42 | * includes such a library. |
| 43 | */ |
| 44 | class Url |
| 45 | { |
| 46 | public: |
| 47 | Url(const std::string& url) |
| 48 | : m_isValid(false) |
| 49 | { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame^] | 50 | static const std::regex protocolExp("(\\w+\\d?(\\+\\w+)?)://([^/]*)(\\/[^?]*)?"); |
| 51 | std::smatch protocolMatch; |
| 52 | if (!std::regex_match(url, protocolMatch, protocolExp)) { |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 53 | return; |
| 54 | } |
| 55 | m_scheme = protocolMatch[1]; |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame^] | 56 | std::string authority = protocolMatch[3]; |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 57 | m_path = protocolMatch[4]; |
| 58 | |
| 59 | // pattern for IPv6 address enclosed in [ ], with optional port number |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame^] | 60 | static const std::regex v6Exp("^\\[([a-fA-F0-9:]+)\\](?:\\:(\\d+))?$"); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 61 | // pattern for IPv4-mapped IPv6 address, with optional port number |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame^] | 62 | static const std::regex v4MappedV6Exp("^\\[::ffff:(\\d+(?:\\.\\d+){3})\\](?:\\:(\\d+))?$"); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 63 | // pattern for IPv4/hostname/fd/ifname, with optional port number |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame^] | 64 | static const std::regex v4HostExp("^([^:]+)(?:\\:(\\d+))?$"); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 65 | |
| 66 | if (authority.empty()) { |
| 67 | // UNIX, internal |
| 68 | } |
| 69 | else { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame^] | 70 | std::smatch match; |
| 71 | bool isV6 = std::regex_match(authority, match, v6Exp); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 72 | if (isV6 || |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame^] | 73 | std::regex_match(authority, match, v4MappedV6Exp) || |
| 74 | std::regex_match(authority, match, v4HostExp)) { |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 75 | m_host = match[1]; |
| 76 | m_port = match[2]; |
| 77 | } |
| 78 | else { |
| 79 | return; |
| 80 | } |
| 81 | } |
| 82 | if (m_port.empty()) { |
| 83 | m_port = "80"; |
| 84 | } |
| 85 | if (m_path.empty()) { |
| 86 | m_path = "/"; |
| 87 | } |
| 88 | m_isValid = true; |
| 89 | } |
| 90 | |
| 91 | bool |
| 92 | isValid() const |
| 93 | { |
| 94 | return m_isValid; |
| 95 | } |
| 96 | |
| 97 | const std::string& |
| 98 | getScheme() const |
| 99 | { |
| 100 | return m_scheme; |
| 101 | } |
| 102 | |
| 103 | const std::string& |
| 104 | getHost() const |
| 105 | { |
| 106 | return m_host; |
| 107 | } |
| 108 | |
| 109 | const std::string& |
| 110 | getPort() const |
| 111 | { |
| 112 | return m_port; |
| 113 | } |
| 114 | |
| 115 | const std::string& |
| 116 | getPath() const |
| 117 | { |
| 118 | return m_path; |
| 119 | } |
| 120 | |
| 121 | private: |
| 122 | bool m_isValid; |
| 123 | std::string m_scheme; |
| 124 | std::string m_host; |
| 125 | std::string m_port; |
| 126 | std::string m_path; |
| 127 | }; |
| 128 | |
| 129 | class HttpException : public std::runtime_error |
| 130 | { |
| 131 | public: |
| 132 | explicit |
| 133 | HttpException(const std::string& what) |
| 134 | : std::runtime_error(what) |
| 135 | { |
| 136 | } |
| 137 | }; |
| 138 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 139 | NdnFchDiscovery::NdnFchDiscovery(const std::string& url) |
| 140 | : m_url(url) |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 141 | { |
| 142 | } |
| 143 | |
| 144 | void |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 145 | NdnFchDiscovery::doStart() |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 146 | { |
| 147 | try { |
Davide Pesavento | 45898d2 | 2018-04-17 21:59:54 -0400 | [diff] [blame] | 148 | boost::asio::ip::tcp::iostream requestStream; |
| 149 | #if BOOST_VERSION >= 106700 |
| 150 | requestStream.expires_after(std::chrono::seconds(3)); |
| 151 | #else |
| 152 | requestStream.expires_from_now(boost::posix_time::seconds(3)); |
| 153 | #endif // BOOST_VERSION >= 106700 |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 154 | |
| 155 | Url url(m_url); |
| 156 | if (!url.isValid()) { |
| 157 | BOOST_THROW_EXCEPTION(HttpException("Invalid NDN-FCH URL: " + m_url)); |
| 158 | } |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 159 | if (!boost::iequals(url.getScheme(), "http")) { |
| 160 | BOOST_THROW_EXCEPTION(HttpException("Only http:// NDN-FCH URLs are supported")); |
| 161 | } |
| 162 | |
| 163 | requestStream.connect(url.getHost(), url.getPort()); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 164 | if (!requestStream) { |
| 165 | BOOST_THROW_EXCEPTION(HttpException("HTTP connection error to " + m_url)); |
| 166 | } |
| 167 | |
| 168 | requestStream << "GET " << url.getPath() << " HTTP/1.0\r\n"; |
| 169 | requestStream << "Host: " << url.getHost() << ":" << url.getPort() << "\r\n"; |
| 170 | requestStream << "Accept: */*\r\n"; |
| 171 | requestStream << "Cache-Control: no-cache\r\n"; |
| 172 | requestStream << "Connection: close\r\n\r\n"; |
| 173 | requestStream.flush(); |
| 174 | |
| 175 | std::string statusLine; |
| 176 | std::getline(requestStream, statusLine); |
| 177 | if (!requestStream) { |
| 178 | BOOST_THROW_EXCEPTION(HttpException("HTTP communication error")); |
| 179 | } |
| 180 | |
| 181 | std::stringstream responseStream(statusLine); |
| 182 | std::string httpVersion; |
| 183 | responseStream >> httpVersion; |
| 184 | unsigned int statusCode; |
| 185 | responseStream >> statusCode; |
| 186 | std::string statusMessage; |
| 187 | |
| 188 | std::getline(responseStream, statusMessage); |
| 189 | if (!static_cast<bool>(requestStream) || httpVersion.substr(0, 5) != "HTTP/") { |
Qi Zhao | 9ae8857 | 2017-05-23 10:54:01 -0700 | [diff] [blame] | 190 | BOOST_THROW_EXCEPTION(HttpException("HTTP communication error")); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 191 | } |
| 192 | if (statusCode != 200) { |
| 193 | boost::trim(statusMessage); |
Alexander Afanasyev | 0c63c63 | 2017-12-05 11:17:09 -0500 | [diff] [blame] | 194 | BOOST_THROW_EXCEPTION(HttpException("HTTP request failed: " + to_string(statusCode) + " " + statusMessage)); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 195 | } |
| 196 | std::string header; |
| 197 | while (std::getline(requestStream, header) && header != "\r") |
| 198 | ; |
| 199 | |
| 200 | std::string hubHost; |
| 201 | requestStream >> hubHost; |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 202 | if (hubHost.empty()) { |
Qi Zhao | 9ae8857 | 2017-05-23 10:54:01 -0700 | [diff] [blame] | 203 | BOOST_THROW_EXCEPTION(HttpException("NDN-FCH did not return hub host")); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 206 | this->provideHubFaceUri("udp://" + hubHost); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 207 | } |
| 208 | catch (const std::runtime_error& e) { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 209 | this->fail(e.what()); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | } // namespace autoconfig |
| 214 | } // namespace tools |
| 215 | } // namespace ndn |