hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, 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 |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 24 | */ |
Davide Pesavento | 6fc750f | 2014-03-18 19:49:39 +0100 | [diff] [blame] | 25 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 26 | #include "version.hpp" |
| 27 | |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 28 | #include <ndn-cxx/face.hpp> |
Junxiao Shi | 8e273ca | 2014-11-12 00:42:29 -0700 | [diff] [blame] | 29 | #include <ndn-cxx/security/key-chain.hpp> |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 30 | #include <ndn-cxx/management/nfd-controller.hpp> |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 31 | #include <ndn-cxx/management/nfd-face-status.hpp> |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 32 | #include <ndn-cxx/security/key-chain.hpp> |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 33 | #include <ndn-cxx/encoding/buffer-stream.hpp> |
Junxiao Shi | a1937bf | 2014-11-06 11:43:40 -0700 | [diff] [blame^] | 34 | #include <ndn-cxx/util/face-uri.hpp> |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 35 | |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 36 | #include <boost/lexical_cast.hpp> |
Junxiao Shi | a1937bf | 2014-11-06 11:43:40 -0700 | [diff] [blame^] | 37 | #include <boost/noncopyable.hpp> |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 38 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 39 | #include <sys/types.h> |
| 40 | #include <netinet/in.h> |
| 41 | #include <arpa/nameser.h> |
| 42 | #include <resolv.h> |
| 43 | |
| 44 | #ifdef __APPLE__ |
| 45 | #include <arpa/nameser_compat.h> |
| 46 | #endif |
| 47 | |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 48 | namespace ndn { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 49 | namespace tools { |
| 50 | |
Alexander Afanasyev | 6fce078 | 2014-06-18 16:59:54 -0700 | [diff] [blame] | 51 | static const Name LOCALHOP_HUB_DISCOVERY_PREFIX = "/localhop/ndn-autoconf/hub"; |
| 52 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 53 | void |
| 54 | usage(const char* programName) |
| 55 | { |
| 56 | std::cout << "Usage:\n" << programName << " [-h] [-V]\n" |
| 57 | << " -h - print usage and exit\n" |
| 58 | << " -V - print version number and exit\n" |
| 59 | << std::endl; |
| 60 | } |
| 61 | |
Junxiao Shi | a1937bf | 2014-11-06 11:43:40 -0700 | [diff] [blame^] | 62 | class NdnAutoconfig : boost::noncopyable |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 63 | { |
| 64 | public: |
| 65 | union QueryAnswer |
| 66 | { |
| 67 | HEADER header; |
| 68 | uint8_t buf[NS_PACKETSZ]; |
| 69 | }; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 70 | |
| 71 | class Error : public std::runtime_error |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 72 | { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 73 | public: |
| 74 | explicit |
| 75 | Error(const std::string& what) |
| 76 | : std::runtime_error(what) |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 77 | { |
| 78 | } |
| 79 | }; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 80 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 81 | explicit |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 82 | NdnAutoconfig() |
Junxiao Shi | 8e273ca | 2014-11-12 00:42:29 -0700 | [diff] [blame] | 83 | : m_controller(m_face, m_keyChain) |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 84 | { |
| 85 | } |
Davide Pesavento | 6fc750f | 2014-03-18 19:49:39 +0100 | [diff] [blame] | 86 | |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 87 | void |
| 88 | run() |
| 89 | { |
| 90 | m_face.processEvents(); |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | fetchSegments(const Data& data, const shared_ptr<OBufferStream>& buffer, |
| 95 | void (NdnAutoconfig::*onDone)(const shared_ptr<OBufferStream>&)) |
| 96 | { |
| 97 | buffer->write(reinterpret_cast<const char*>(data.getContent().value()), |
| 98 | data.getContent().value_size()); |
| 99 | |
| 100 | uint64_t currentSegment = data.getName().get(-1).toSegment(); |
| 101 | |
| 102 | const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId(); |
| 103 | if (finalBlockId.empty() || |
| 104 | finalBlockId.toSegment() > currentSegment) |
| 105 | { |
| 106 | m_face.expressInterest(data.getName().getPrefix(-1).appendSegment(currentSegment+1), |
Davide Pesavento | ab1e8f2 | 2014-10-21 22:45:33 +0200 | [diff] [blame] | 107 | ndn::bind(&NdnAutoconfig::fetchSegments, this, _2, buffer, onDone), |
| 108 | ndn::bind(&NdnAutoconfig::discoverHubStage2, this, "Timeout")); |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 109 | } |
| 110 | else |
| 111 | { |
| 112 | return (this->*onDone)(buffer); |
| 113 | } |
| 114 | } |
| 115 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 116 | void |
| 117 | discoverHubStage1() |
| 118 | { |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 119 | shared_ptr<OBufferStream> buffer = make_shared<OBufferStream>(); |
| 120 | |
| 121 | Interest interest("/localhost/nfd/faces/list"); |
| 122 | interest.setChildSelector(1); |
| 123 | interest.setMustBeFresh(true); |
| 124 | |
| 125 | m_face.expressInterest(interest, |
Davide Pesavento | ab1e8f2 | 2014-10-21 22:45:33 +0200 | [diff] [blame] | 126 | ndn::bind(&NdnAutoconfig::fetchSegments, this, _2, buffer, |
| 127 | &NdnAutoconfig::discoverHubStage1_registerHubDiscoveryPrefix), |
| 128 | ndn::bind(&NdnAutoconfig::discoverHubStage2, this, "Timeout")); |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void |
Alexander Afanasyev | 6fce078 | 2014-06-18 16:59:54 -0700 | [diff] [blame] | 132 | discoverHubStage1_registerHubDiscoveryPrefix(const shared_ptr<OBufferStream>& buffer) |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 133 | { |
| 134 | ConstBufferPtr buf = buffer->buf(); |
| 135 | std::vector<uint64_t> multicastFaces; |
| 136 | |
| 137 | size_t offset = 0; |
| 138 | while (offset < buf->size()) |
| 139 | { |
| 140 | Block block; |
| 141 | bool ok = Block::fromBuffer(buf, offset, block); |
| 142 | if (!ok) |
| 143 | { |
| 144 | std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl; |
| 145 | break; |
| 146 | } |
| 147 | |
| 148 | offset += block.size(); |
| 149 | |
| 150 | nfd::FaceStatus faceStatus(block); |
| 151 | |
Junxiao Shi | a1937bf | 2014-11-06 11:43:40 -0700 | [diff] [blame^] | 152 | ndn::util::FaceUri uri(faceStatus.getRemoteUri()); |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 153 | if (uri.getScheme() == "udp4") { |
| 154 | namespace ip = boost::asio::ip; |
| 155 | boost::system::error_code ec; |
| 156 | ip::address address = ip::address::from_string(uri.getHost(), ec); |
| 157 | |
| 158 | if (!ec && address.is_multicast()) { |
| 159 | multicastFaces.push_back(faceStatus.getFaceId()); |
| 160 | } |
| 161 | else |
| 162 | continue; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if (multicastFaces.empty()) { |
| 167 | discoverHubStage2("No multicast faces available, skipping stage 1"); |
| 168 | } |
| 169 | else { |
| 170 | shared_ptr<nfd::Controller> controller = make_shared<nfd::Controller>(ref(m_face)); |
| 171 | shared_ptr<std::pair<size_t, size_t> > nRegistrations = |
| 172 | make_shared<std::pair<size_t, size_t> >(0, 0); |
| 173 | |
| 174 | nfd::ControlParameters parameters; |
| 175 | parameters |
Alexander Afanasyev | 6fce078 | 2014-06-18 16:59:54 -0700 | [diff] [blame] | 176 | .setName(LOCALHOP_HUB_DISCOVERY_PREFIX) |
Junxiao Shi | a753f1b | 2014-07-01 22:25:14 -0700 | [diff] [blame] | 177 | .setCost(1) |
| 178 | .setExpirationPeriod(time::seconds(30)); |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 179 | |
| 180 | nRegistrations->first = multicastFaces.size(); |
| 181 | |
| 182 | for (std::vector<uint64_t>::iterator i = multicastFaces.begin(); |
| 183 | i != multicastFaces.end(); ++i) { |
| 184 | parameters.setFaceId(*i); |
| 185 | |
| 186 | controller->start<nfd::RibRegisterCommand>(parameters, |
| 187 | bind(&NdnAutoconfig::discoverHubStage1_onRegisterSuccess, |
| 188 | this, controller, nRegistrations), |
| 189 | bind(&NdnAutoconfig::discoverHubStage1_onRegisterFailure, |
| 190 | this, _1, _2, controller, nRegistrations)); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | discoverHubStage1_onRegisterSuccess(const shared_ptr<nfd::Controller>& controller, |
| 197 | const shared_ptr<std::pair<size_t, size_t> >& nRegistrations) |
| 198 | { |
| 199 | nRegistrations->second++; |
| 200 | |
| 201 | if (nRegistrations->first == nRegistrations->second) { |
Alexander Afanasyev | 6fce078 | 2014-06-18 16:59:54 -0700 | [diff] [blame] | 202 | discoverHubStage1_setStrategy(controller); |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| 206 | void |
| 207 | discoverHubStage1_onRegisterFailure(uint32_t code, const std::string& error, |
Alexander Afanasyev | 6fce078 | 2014-06-18 16:59:54 -0700 | [diff] [blame] | 208 | const shared_ptr<nfd::Controller>& controller, |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 209 | const shared_ptr<std::pair<size_t, size_t> >& nRegistrations) |
| 210 | { |
| 211 | std::cerr << "ERROR: " << error << " (code: " << code << ")" << std::endl; |
| 212 | nRegistrations->first--; |
| 213 | |
| 214 | if (nRegistrations->first == nRegistrations->second) { |
| 215 | if (nRegistrations->first > 0) { |
Alexander Afanasyev | 6fce078 | 2014-06-18 16:59:54 -0700 | [diff] [blame] | 216 | discoverHubStage1_setStrategy(controller); |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 217 | } else { |
Alexander Afanasyev | 6fce078 | 2014-06-18 16:59:54 -0700 | [diff] [blame] | 218 | discoverHubStage2("Failed to register " + LOCALHOP_HUB_DISCOVERY_PREFIX.toUri() + |
| 219 | " for all multicast faces"); |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
Alexander Afanasyev | 6fce078 | 2014-06-18 16:59:54 -0700 | [diff] [blame] | 224 | void |
| 225 | discoverHubStage1_setStrategy(const shared_ptr<nfd::Controller>& controller) |
| 226 | { |
| 227 | nfd::ControlParameters parameters; |
| 228 | parameters |
| 229 | .setName(LOCALHOP_HUB_DISCOVERY_PREFIX) |
| 230 | .setStrategy("/localhost/nfd/strategy/broadcast"); |
| 231 | |
| 232 | controller->start<nfd::StrategyChoiceSetCommand>(parameters, |
| 233 | bind(&NdnAutoconfig::discoverHubStage1_onSetStrategySuccess, |
| 234 | this, controller), |
| 235 | bind(&NdnAutoconfig::discoverHubStage1_onSetStrategyFailure, |
| 236 | this, _2, controller)); |
| 237 | } |
| 238 | |
| 239 | void |
| 240 | discoverHubStage1_onSetStrategySuccess(const shared_ptr<nfd::Controller>& controller) |
| 241 | { |
| 242 | discoverHubStage1_requestHubData(); |
| 243 | } |
| 244 | |
| 245 | void |
| 246 | discoverHubStage1_onSetStrategyFailure(const std::string& error, |
| 247 | const shared_ptr<nfd::Controller>& controller) |
| 248 | { |
| 249 | discoverHubStage2("Failed to set broadcast strategy for " + |
| 250 | LOCALHOP_HUB_DISCOVERY_PREFIX.toUri() + " namespace (" + error + ")"); |
| 251 | } |
| 252 | |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 253 | // Start to look for a hub (NDN hub discovery first stage) |
| 254 | void |
| 255 | discoverHubStage1_requestHubData() |
| 256 | { |
Alexander Afanasyev | 6fce078 | 2014-06-18 16:59:54 -0700 | [diff] [blame] | 257 | Interest interest(LOCALHOP_HUB_DISCOVERY_PREFIX); |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 258 | interest.setInterestLifetime(time::milliseconds(4000)); // 4 seconds |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 259 | interest.setMustBeFresh(true); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 260 | |
hilata | 3a4eb5c | 2014-04-29 00:32:05 -0500 | [diff] [blame] | 261 | std::cerr << "Stage 1: Trying multicast discovery..." << std::endl; |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 262 | m_face.expressInterest(interest, |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 263 | bind(&NdnAutoconfig::onDiscoverHubStage1Success, this, _1, _2), |
| 264 | bind(&NdnAutoconfig::discoverHubStage2, this, "Timeout")); |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 265 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 266 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 267 | // First stage OnData Callback |
| 268 | void |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 269 | onDiscoverHubStage1Success(const Interest& interest, Data& data) |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 270 | { |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 271 | const Block& content = data.getContent(); |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 272 | content.parse(); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 273 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 274 | // Get Uri |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 275 | Block::element_const_iterator blockValue = content.find(tlv::nfd::Uri); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 276 | if (blockValue == content.elements_end()) |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 277 | { |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 278 | discoverHubStage2("Incorrect reply to stage1"); |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 279 | return; |
| 280 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 281 | std::string faceMgmtUri(reinterpret_cast<const char*>(blockValue->value()), |
| 282 | blockValue->value_size()); |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 283 | connectToHub(faceMgmtUri); |
| 284 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 285 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 286 | // First stage OnTimeout callback - start 2nd stage |
| 287 | void |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 288 | discoverHubStage2(const std::string& message) |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 289 | { |
| 290 | std::cerr << message << std::endl; |
| 291 | std::cerr << "Stage 2: Trying DNS query with default suffix..." << std::endl; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 292 | |
| 293 | _res.retry = 2; |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 294 | _res.ndots = 10; |
| 295 | |
| 296 | QueryAnswer queryAnswer; |
| 297 | |
| 298 | int answerSize = res_search("_ndn._udp", |
| 299 | ns_c_in, |
| 300 | ns_t_srv, |
| 301 | queryAnswer.buf, |
| 302 | sizeof(queryAnswer)); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 303 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 304 | // 2nd stage failed - move on to the third stage |
| 305 | if (answerSize < 0) |
| 306 | { |
| 307 | discoverHubStage3("Failed to find NDN router using default suffix DNS query"); |
| 308 | } |
| 309 | else |
| 310 | { |
| 311 | bool isParsed = parseHostAndConnectToHub(queryAnswer, answerSize); |
| 312 | if (isParsed == false) |
| 313 | { |
| 314 | // Failed to parse DNS response, try stage 3 |
| 315 | discoverHubStage3("Failed to parse DNS response"); |
| 316 | } |
| 317 | } |
| 318 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 319 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 320 | // Second stage OnTimeout callback |
| 321 | void |
| 322 | discoverHubStage3(const std::string& message) |
| 323 | { |
| 324 | std::cerr << message << std::endl; |
| 325 | std::cerr << "Stage 3: Trying to find home router..." << std::endl; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 326 | |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 327 | KeyChain keyChain; |
| 328 | Name identity = keyChain.getDefaultIdentity(); |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 329 | std::string serverName = "_ndn._udp."; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 330 | |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 331 | for (Name::const_reverse_iterator i = identity.rbegin(); i != identity.rend(); i++) |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 332 | { |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 333 | serverName.append(i->toUri()); |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 334 | serverName.append("."); |
| 335 | } |
| 336 | serverName += "_homehub._autoconf.named-data.net"; |
| 337 | std::cerr << "Stage3: About to query for a home router: " << serverName << std::endl; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 338 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 339 | QueryAnswer queryAnswer; |
| 340 | |
| 341 | int answerSize = res_query(serverName.c_str(), |
| 342 | ns_c_in, |
| 343 | ns_t_srv, |
| 344 | queryAnswer.buf, |
| 345 | sizeof(queryAnswer)); |
| 346 | |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 347 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 348 | // 3rd stage failed - abort |
| 349 | if (answerSize < 0) |
| 350 | { |
| 351 | std::cerr << "Failed to find a home router" << std::endl; |
| 352 | std::cerr << "exit" << std::endl; |
| 353 | } |
| 354 | else |
| 355 | { |
| 356 | bool isParsed = parseHostAndConnectToHub(queryAnswer, answerSize); |
| 357 | if (isParsed == false) |
| 358 | { |
| 359 | // Failed to parse DNS response |
| 360 | throw Error("Failed to parse DNS response"); |
| 361 | } |
| 362 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 363 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 364 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 365 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 366 | void |
| 367 | connectToHub(const std::string& uri) |
| 368 | { |
| 369 | std::cerr << "about to connect to: " << uri << std::endl; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 370 | |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 371 | m_controller.start<nfd::FaceCreateCommand>( |
| 372 | nfd::ControlParameters() |
hilata | 3a4eb5c | 2014-04-29 00:32:05 -0500 | [diff] [blame] | 373 | .setUri(uri), |
| 374 | bind(&NdnAutoconfig::onHubConnectSuccess, this, _1), |
| 375 | bind(&NdnAutoconfig::onHubConnectError, this, _1, _2) |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 376 | ); |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 377 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 378 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 379 | void |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 380 | onHubConnectSuccess(const nfd::ControlParameters& resp) |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 381 | { |
hilata | 3a4eb5c | 2014-04-29 00:32:05 -0500 | [diff] [blame] | 382 | std::cerr << "Successfully created face: " << resp << std::endl; |
hilata | f4f8673 | 2014-05-03 19:06:34 -0500 | [diff] [blame] | 383 | |
| 384 | // Register a prefix in RIB |
Junxiao Shi | a753f1b | 2014-07-01 22:25:14 -0700 | [diff] [blame] | 385 | static const Name TESTBED_PREFIX("/ndn"); |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 386 | m_controller.start<nfd::RibRegisterCommand>( |
Junxiao Shi | a753f1b | 2014-07-01 22:25:14 -0700 | [diff] [blame] | 387 | nfd::ControlParameters() |
| 388 | .setName(TESTBED_PREFIX) |
| 389 | .setFaceId(resp.getFaceId()) |
| 390 | .setOrigin(nfd::ROUTE_ORIGIN_AUTOCONF) |
| 391 | .setCost(100) |
| 392 | .setExpirationPeriod(time::milliseconds::max()), |
hilata | f4f8673 | 2014-05-03 19:06:34 -0500 | [diff] [blame] | 393 | bind(&NdnAutoconfig::onPrefixRegistrationSuccess, this, _1), |
| 394 | bind(&NdnAutoconfig::onPrefixRegistrationError, this, _1, _2)); |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 395 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 396 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 397 | void |
hilata | 3a4eb5c | 2014-04-29 00:32:05 -0500 | [diff] [blame] | 398 | onHubConnectError(uint32_t code, const std::string& error) |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 399 | { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 400 | std::ostringstream os; |
hilata | 3a4eb5c | 2014-04-29 00:32:05 -0500 | [diff] [blame] | 401 | os << "Failed to create face: " << error << " (code: " << code << ")"; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 402 | throw Error(os.str()); |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 403 | } |
| 404 | |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 405 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 406 | bool parseHostAndConnectToHub(QueryAnswer& queryAnswer, int answerSize) |
| 407 | { |
| 408 | // The references of the next classes are: |
| 409 | // http://www.diablotin.com/librairie/networking/dnsbind/ch14_02.htm |
| 410 | // https://gist.github.com/mologie/6027597 |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 411 | |
| 412 | struct rechdr |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 413 | { |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 414 | uint16_t type; |
| 415 | uint16_t iclass; |
| 416 | uint32_t ttl; |
| 417 | uint16_t length; |
| 418 | }; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 419 | |
| 420 | struct srv_t |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 421 | { |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 422 | uint16_t priority; |
| 423 | uint16_t weight; |
| 424 | uint16_t port; |
| 425 | uint8_t* target; |
| 426 | }; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 427 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 428 | if (ntohs(queryAnswer.header.ancount) == 0) |
| 429 | { |
| 430 | std::cerr << "No records found\n" << std::endl; |
| 431 | return false; |
| 432 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 433 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 434 | uint8_t* blob = queryAnswer.buf + NS_HFIXEDSZ; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 435 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 436 | blob += dn_skipname(blob, queryAnswer.buf + answerSize) + NS_QFIXEDSZ; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 437 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 438 | for (int i = 0; i < ntohs(queryAnswer.header.ancount); i++) |
| 439 | { |
hilata | 3a4eb5c | 2014-04-29 00:32:05 -0500 | [diff] [blame] | 440 | char srvName[NS_MAXDNAME]; |
| 441 | int serverNameSize = dn_expand(queryAnswer.buf, // message pointer |
| 442 | queryAnswer.buf + answerSize, // end of message |
| 443 | blob, // compressed server name |
| 444 | srvName, // expanded server name |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 445 | NS_MAXDNAME); |
hilata | 3a4eb5c | 2014-04-29 00:32:05 -0500 | [diff] [blame] | 446 | if (serverNameSize < 0) |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 447 | { |
| 448 | return false; |
| 449 | } |
Davide Pesavento | 6fc750f | 2014-03-18 19:49:39 +0100 | [diff] [blame] | 450 | |
| 451 | srv_t* server = reinterpret_cast<srv_t*>(&blob[sizeof(rechdr)]); |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 452 | uint16_t convertedPort = be16toh(server->port); |
hilata | 3a4eb5c | 2014-04-29 00:32:05 -0500 | [diff] [blame] | 453 | |
| 454 | blob += serverNameSize + NS_HFIXEDSZ + NS_QFIXEDSZ; |
| 455 | |
| 456 | char hostName[NS_MAXDNAME]; |
| 457 | int hostNameSize = dn_expand(queryAnswer.buf, // message pointer |
| 458 | queryAnswer.buf + answerSize, // end of message |
| 459 | blob, // compressed host name |
| 460 | hostName, // expanded host name |
| 461 | NS_MAXDNAME); |
| 462 | if (hostNameSize < 0) |
| 463 | { |
| 464 | return false; |
| 465 | } |
| 466 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 467 | std::string uri = "udp://"; |
| 468 | uri.append(hostName); |
| 469 | uri.append(":"); |
| 470 | uri.append(boost::lexical_cast<std::string>(convertedPort)); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 471 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 472 | connectToHub(uri); |
| 473 | return true; |
| 474 | } |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 475 | |
| 476 | return false; |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 477 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 478 | |
hilata | f4f8673 | 2014-05-03 19:06:34 -0500 | [diff] [blame] | 479 | void |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 480 | onPrefixRegistrationSuccess(const nfd::ControlParameters& commandSuccessResult) |
hilata | f4f8673 | 2014-05-03 19:06:34 -0500 | [diff] [blame] | 481 | { |
| 482 | std::cerr << "Successful in name registration: " << commandSuccessResult << std::endl; |
| 483 | } |
| 484 | |
| 485 | void |
| 486 | onPrefixRegistrationError(uint32_t code, const std::string& error) |
| 487 | { |
| 488 | std::ostringstream os; |
| 489 | os << "Failed in name registration, " << error << " (code: " << code << ")"; |
| 490 | throw Error(os.str()); |
| 491 | } |
| 492 | |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 493 | private: |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 494 | Face m_face; |
Junxiao Shi | 8e273ca | 2014-11-12 00:42:29 -0700 | [diff] [blame] | 495 | KeyChain m_keyChain; |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 496 | nfd::Controller m_controller; |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 497 | }; |
| 498 | |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 499 | } // namespace tools |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 500 | } // namespace ndn |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 501 | |
| 502 | int |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 503 | main(int argc, char** argv) |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 504 | { |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 505 | int opt; |
| 506 | const char* programName = argv[0]; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 507 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 508 | while ((opt = getopt(argc, argv, "hV")) != -1) { |
| 509 | switch (opt) { |
| 510 | case 'h': |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 511 | ndn::tools::usage(programName); |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 512 | return 0; |
| 513 | case 'V': |
| 514 | std::cout << NFD_VERSION_BUILD_STRING << std::endl; |
| 515 | return 0; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame] | 516 | } |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | try { |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 520 | ndn::tools::NdnAutoconfig autoConfigInstance; |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 521 | |
| 522 | autoConfigInstance.discoverHubStage1(); |
Alexander Afanasyev | 554ef7a | 2014-06-15 16:10:43 -0700 | [diff] [blame] | 523 | autoConfigInstance.run(); |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 524 | } |
| 525 | catch (const std::exception& error) { |
| 526 | std::cerr << "ERROR: " << error.what() << std::endl; |
| 527 | return 1; |
| 528 | } |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 529 | return 0; |
| 530 | } |