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