ndn-fch-discovery.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2017, Regents of the University of California,
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"
27 
28 #include <boost/algorithm/string.hpp>
29 #include <boost/regex.hpp>
30 
31 #include <sstream>
32 
33 namespace ndn {
34 namespace tools {
35 namespace autoconfig {
36 
43 class Url
44 {
45 public:
46  Url(const std::string& url)
47  : m_isValid(false)
48  {
49  static const boost::regex protocolExp("(\\w+\\d?(\\+\\w+)?)://([^/]*)(\\/[^?]*)?");
50  boost::smatch protocolMatch;
51  if (!boost::regex_match(url, protocolMatch, protocolExp)) {
52  return;
53  }
54  m_scheme = protocolMatch[1];
55  const std::string& authority = protocolMatch[3];
56  m_path = protocolMatch[4];
57 
58  // pattern for IPv6 address enclosed in [ ], with optional port number
59  static const boost::regex v6Exp("^\\[([a-fA-F0-9:]+)\\](?:\\:(\\d+))?$");
60  // pattern for IPv4-mapped IPv6 address, with optional port number
61  static const boost::regex v4MappedV6Exp("^\\[::ffff:(\\d+(?:\\.\\d+){3})\\](?:\\:(\\d+))?$");
62  // pattern for IPv4/hostname/fd/ifname, with optional port number
63  static const boost::regex v4HostExp("^([^:]+)(?:\\:(\\d+))?$");
64 
65  if (authority.empty()) {
66  // UNIX, internal
67  }
68  else {
69  boost::smatch match;
70  bool isV6 = boost::regex_match(authority, match, v6Exp);
71  if (isV6 ||
72  boost::regex_match(authority, match, v4MappedV6Exp) ||
73  boost::regex_match(authority, match, v4HostExp)) {
74  m_host = match[1];
75  m_port = match[2];
76  }
77  else {
78  return;
79  }
80  }
81  if (m_port.empty()) {
82  m_port = "80";
83  }
84  if (m_path.empty()) {
85  m_path = "/";
86  }
87  m_isValid = true;
88  }
89 
90  bool
91  isValid() const
92  {
93  return m_isValid;
94  }
95 
96  const std::string&
97  getScheme() const
98  {
99  return m_scheme;
100  }
101 
102  const std::string&
103  getHost() const
104  {
105  return m_host;
106  }
107 
108  const std::string&
109  getPort() const
110  {
111  return m_port;
112  }
113 
114  const std::string&
115  getPath() const
116  {
117  return m_path;
118  }
119 
120 private:
121  bool m_isValid;
122  std::string m_scheme;
123  std::string m_host;
124  std::string m_port;
125  std::string m_path;
126 };
127 
128 class HttpException : public std::runtime_error
129 {
130 public:
131  explicit
132  HttpException(const std::string& what)
133  : std::runtime_error(what)
134  {
135  }
136 };
137 
138 NdnFchDiscovery::NdnFchDiscovery(const std::string& url)
139  : m_url(url)
140 {
141 }
142 
143 void
144 NdnFchDiscovery::doStart()
145 {
146  try {
147  using namespace boost::asio::ip;
148  tcp::iostream requestStream;
149 
150  requestStream.expires_from_now(boost::posix_time::milliseconds(3000));
151 
152  Url url(m_url);
153  if (!url.isValid()) {
154  BOOST_THROW_EXCEPTION(HttpException("Invalid NDN-FCH URL: " + m_url));
155  }
156 
157  if (!boost::iequals(url.getScheme(), "http")) {
158  BOOST_THROW_EXCEPTION(HttpException("Only http:// NDN-FCH URLs are supported"));
159  }
160 
161  requestStream.connect(url.getHost(), url.getPort());
162 
163  if (!requestStream) {
164  BOOST_THROW_EXCEPTION(HttpException("HTTP connection error to " + m_url));
165  }
166 
167  requestStream << "GET " << url.getPath() << " HTTP/1.0\r\n";
168  requestStream << "Host: " << url.getHost() << ":" << url.getPort() << "\r\n";
169  requestStream << "Accept: */*\r\n";
170  requestStream << "Cache-Control: no-cache\r\n";
171  requestStream << "Connection: close\r\n\r\n";
172  requestStream.flush();
173 
174  std::string statusLine;
175  std::getline(requestStream, statusLine);
176  if (!requestStream) {
177  BOOST_THROW_EXCEPTION(HttpException("HTTP communication error"));
178  }
179 
180  std::stringstream responseStream(statusLine);
181  std::string httpVersion;
182  responseStream >> httpVersion;
183  unsigned int statusCode;
184  responseStream >> statusCode;
185  std::string statusMessage;
186 
187  std::getline(responseStream, statusMessage);
188  if (!static_cast<bool>(requestStream) || httpVersion.substr(0, 5) != "HTTP/") {
189  BOOST_THROW_EXCEPTION(HttpException("HTTP communication error"));
190  }
191  if (statusCode != 200) {
192  boost::trim(statusMessage);
193  BOOST_THROW_EXCEPTION(HttpException("HTTP request failed: " + to_string(statusCode) + " " + statusMessage));
194  }
195  std::string header;
196  while (std::getline(requestStream, header) && header != "\r")
197  ;
198 
199  std::string hubHost;
200  requestStream >> hubHost;
201 
202  if (hubHost.empty()) {
203  BOOST_THROW_EXCEPTION(HttpException("NDN-FCH did not return hub host"));
204  }
205 
206  this->provideHubFaceUri("udp://" + hubHost);
207  }
208  catch (const std::runtime_error& e) {
209  this->fail(e.what());
210  }
211 }
212 
213 } // namespace autoconfig
214 } // namespace tools
215 } // namespace ndn
Copyright (c) 2014-2017, Regents of the University of California, Arizona Board of Regents...
STL namespace.
NdnFchDiscovery(const std::string &url)
Create stage to discover NDN hub using NDN-FCH protocol.
void fail(const std::string &msg)
Definition: stage.cpp:65
void provideHubFaceUri(const std::string &s)
parse HUB FaceUri from string and declare success
Definition: stage.cpp:45