blob: abb934ffba25b19112b540eb18f8efdf76b54a31 [file] [log] [blame]
Alexander Afanasyev2a001942016-12-14 18:18:41 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shif748a4e2017-07-05 23:41:48 +00002/*
Davide Pesavento45898d22018-04-17 21:59:54 -04003 * Copyright (c) 2014-2018, Regents of the University of California,
Alexander Afanasyev2a001942016-12-14 18:18:41 -08004 * 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 Pesaventoa997d292017-08-24 20:16:59 -040027
Alexander Afanasyev2a001942016-12-14 18:18:41 -080028#include <boost/algorithm/string.hpp>
Davide Pesavento45898d22018-04-17 21:59:54 -040029#include <boost/asio/ip/tcp.hpp>
Davide Pesaventoa997d292017-08-24 20:16:59 -040030
Davide Pesaventoe4b22382018-06-10 14:37:24 -040031#include <regex>
Davide Pesaventoa997d292017-08-24 20:16:59 -040032#include <sstream>
Alexander Afanasyev2a001942016-12-14 18:18:41 -080033
34namespace ndn {
35namespace tools {
36namespace 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 */
44class Url
45{
46public:
47 Url(const std::string& url)
48 : m_isValid(false)
49 {
Davide Pesaventoe4b22382018-06-10 14:37:24 -040050 static const std::regex protocolExp("(\\w+\\d?(\\+\\w+)?)://([^/]*)(\\/[^?]*)?");
51 std::smatch protocolMatch;
52 if (!std::regex_match(url, protocolMatch, protocolExp)) {
Alexander Afanasyev2a001942016-12-14 18:18:41 -080053 return;
54 }
55 m_scheme = protocolMatch[1];
Davide Pesaventoe4b22382018-06-10 14:37:24 -040056 std::string authority = protocolMatch[3];
Alexander Afanasyev2a001942016-12-14 18:18:41 -080057 m_path = protocolMatch[4];
58
59 // pattern for IPv6 address enclosed in [ ], with optional port number
Davide Pesaventoe4b22382018-06-10 14:37:24 -040060 static const std::regex v6Exp("^\\[([a-fA-F0-9:]+)\\](?:\\:(\\d+))?$");
Alexander Afanasyev2a001942016-12-14 18:18:41 -080061 // pattern for IPv4-mapped IPv6 address, with optional port number
Davide Pesaventoe4b22382018-06-10 14:37:24 -040062 static const std::regex v4MappedV6Exp("^\\[::ffff:(\\d+(?:\\.\\d+){3})\\](?:\\:(\\d+))?$");
Alexander Afanasyev2a001942016-12-14 18:18:41 -080063 // pattern for IPv4/hostname/fd/ifname, with optional port number
Davide Pesaventoe4b22382018-06-10 14:37:24 -040064 static const std::regex v4HostExp("^([^:]+)(?:\\:(\\d+))?$");
Alexander Afanasyev2a001942016-12-14 18:18:41 -080065
66 if (authority.empty()) {
67 // UNIX, internal
68 }
69 else {
Davide Pesaventoe4b22382018-06-10 14:37:24 -040070 std::smatch match;
71 bool isV6 = std::regex_match(authority, match, v6Exp);
Alexander Afanasyev2a001942016-12-14 18:18:41 -080072 if (isV6 ||
Davide Pesaventoe4b22382018-06-10 14:37:24 -040073 std::regex_match(authority, match, v4MappedV6Exp) ||
74 std::regex_match(authority, match, v4HostExp)) {
Alexander Afanasyev2a001942016-12-14 18:18:41 -080075 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
121private:
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
129class HttpException : public std::runtime_error
130{
131public:
132 explicit
133 HttpException(const std::string& what)
134 : std::runtime_error(what)
135 {
136 }
137};
138
Junxiao Shicb766862017-07-07 22:21:04 +0000139NdnFchDiscovery::NdnFchDiscovery(const std::string& url)
140 : m_url(url)
Alexander Afanasyev2a001942016-12-14 18:18:41 -0800141{
142}
143
144void
Junxiao Shicb766862017-07-07 22:21:04 +0000145NdnFchDiscovery::doStart()
Alexander Afanasyev2a001942016-12-14 18:18:41 -0800146{
147 try {
Davide Pesavento45898d22018-04-17 21:59:54 -0400148 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 Afanasyev2a001942016-12-14 18:18:41 -0800154
155 Url url(m_url);
156 if (!url.isValid()) {
157 BOOST_THROW_EXCEPTION(HttpException("Invalid NDN-FCH URL: " + m_url));
158 }
Alexander Afanasyev2a001942016-12-14 18:18:41 -0800159 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 Afanasyev2a001942016-12-14 18:18:41 -0800164 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 Zhao9ae88572017-05-23 10:54:01 -0700190 BOOST_THROW_EXCEPTION(HttpException("HTTP communication error"));
Alexander Afanasyev2a001942016-12-14 18:18:41 -0800191 }
192 if (statusCode != 200) {
193 boost::trim(statusMessage);
Alexander Afanasyev0c63c632017-12-05 11:17:09 -0500194 BOOST_THROW_EXCEPTION(HttpException("HTTP request failed: " + to_string(statusCode) + " " + statusMessage));
Alexander Afanasyev2a001942016-12-14 18:18:41 -0800195 }
196 std::string header;
197 while (std::getline(requestStream, header) && header != "\r")
198 ;
199
200 std::string hubHost;
201 requestStream >> hubHost;
Alexander Afanasyev2a001942016-12-14 18:18:41 -0800202 if (hubHost.empty()) {
Qi Zhao9ae88572017-05-23 10:54:01 -0700203 BOOST_THROW_EXCEPTION(HttpException("NDN-FCH did not return hub host"));
Alexander Afanasyev2a001942016-12-14 18:18:41 -0800204 }
205
Junxiao Shicb766862017-07-07 22:21:04 +0000206 this->provideHubFaceUri("udp://" + hubHost);
Alexander Afanasyev2a001942016-12-14 18:18:41 -0800207 }
208 catch (const std::runtime_error& e) {
Junxiao Shicb766862017-07-07 22:21:04 +0000209 this->fail(e.what());
Alexander Afanasyev2a001942016-12-14 18:18:41 -0800210 }
211}
212
213} // namespace autoconfig
214} // namespace tools
215} // namespace ndn