hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
Davide Pesavento | 6fc750f | 2014-03-18 19:49:39 +0100 | [diff] [blame] | 6 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 7 | #include <ndn-cpp-dev/face.hpp> |
| 8 | #include <ndn-cpp-dev/management/nfd-controller.hpp> |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 9 | #include <ndn-cpp-dev/security/key-chain.hpp> |
| 10 | |
| 11 | #include <sys/types.h> |
| 12 | #include <netinet/in.h> |
| 13 | #include <arpa/nameser.h> |
| 14 | #include <resolv.h> |
| 15 | |
| 16 | #ifdef __APPLE__ |
| 17 | #include <arpa/nameser_compat.h> |
| 18 | #endif |
| 19 | |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 20 | namespace tools { |
| 21 | |
| 22 | class NdnAutoconfig |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 23 | { |
| 24 | public: |
| 25 | union QueryAnswer |
| 26 | { |
| 27 | HEADER header; |
| 28 | uint8_t buf[NS_PACKETSZ]; |
| 29 | }; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 30 | |
| 31 | class Error : public std::runtime_error |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 32 | { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 33 | public: |
| 34 | explicit |
| 35 | Error(const std::string& what) |
| 36 | : std::runtime_error(what) |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 37 | { |
| 38 | } |
| 39 | }; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 40 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 41 | explicit |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 42 | NdnAutoconfig() |
| 43 | : m_controller(m_face) |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 44 | { |
| 45 | } |
Davide Pesavento | 6fc750f | 2014-03-18 19:49:39 +0100 | [diff] [blame] | 46 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 47 | // Start to look for a hub (NDN hub discovery first stage) |
| 48 | void |
| 49 | discoverHubStage1() |
| 50 | { |
| 51 | ndn::Interest interest(ndn::Name("/localhop/ndn-autoconf/hub")); |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 52 | interest.setInterestLifetime(ndn::time::milliseconds(4000)); // 4 seconds |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 53 | interest.setMustBeFresh(true); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 54 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 55 | std::cerr << "Stage 1: Trying muticast discovery..." << std::endl; |
| 56 | m_face.expressInterest(interest, |
| 57 | ndn::bind(&NdnAutoconfig::onDiscoverHubStage1Success, this, _1, _2), |
| 58 | ndn::bind(&NdnAutoconfig::discoverHubStage2, this, _1, "Timeout")); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 59 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 60 | m_face.processEvents(); |
| 61 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 62 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 63 | // First stage OnData Callback |
| 64 | void |
| 65 | onDiscoverHubStage1Success(const ndn::Interest& interest, ndn::Data& data) |
| 66 | { |
| 67 | const ndn::Block& content = data.getContent(); |
| 68 | content.parse(); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 69 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 70 | // Get Uri |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 71 | ndn::Block::element_const_iterator blockValue = content.find(ndn::tlv::nfd::Uri); |
| 72 | if (blockValue == content.elements_end()) |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 73 | { |
| 74 | discoverHubStage2(interest, "Incorrect reply to stage1"); |
| 75 | return; |
| 76 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 77 | std::string faceMgmtUri(reinterpret_cast<const char*>(blockValue->value()), |
| 78 | blockValue->value_size()); |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 79 | connectToHub(faceMgmtUri); |
| 80 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 81 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 82 | // First stage OnTimeout callback - start 2nd stage |
| 83 | void |
| 84 | discoverHubStage2(const ndn::Interest& interest, const std::string& message) |
| 85 | { |
| 86 | std::cerr << message << std::endl; |
| 87 | std::cerr << "Stage 2: Trying DNS query with default suffix..." << std::endl; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 88 | |
| 89 | _res.retry = 2; |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 90 | _res.ndots = 10; |
| 91 | |
| 92 | QueryAnswer queryAnswer; |
| 93 | |
| 94 | int answerSize = res_search("_ndn._udp", |
| 95 | ns_c_in, |
| 96 | ns_t_srv, |
| 97 | queryAnswer.buf, |
| 98 | sizeof(queryAnswer)); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 99 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 100 | // 2nd stage failed - move on to the third stage |
| 101 | if (answerSize < 0) |
| 102 | { |
| 103 | discoverHubStage3("Failed to find NDN router using default suffix DNS query"); |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | bool isParsed = parseHostAndConnectToHub(queryAnswer, answerSize); |
| 108 | if (isParsed == false) |
| 109 | { |
| 110 | // Failed to parse DNS response, try stage 3 |
| 111 | discoverHubStage3("Failed to parse DNS response"); |
| 112 | } |
| 113 | } |
| 114 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 115 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 116 | // Second stage OnTimeout callback |
| 117 | void |
| 118 | discoverHubStage3(const std::string& message) |
| 119 | { |
| 120 | std::cerr << message << std::endl; |
| 121 | std::cerr << "Stage 3: Trying to find home router..." << std::endl; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 122 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 123 | ndn::KeyChain keyChain; |
| 124 | ndn::Name identity = keyChain.getDefaultIdentity(); |
| 125 | std::string serverName = "_ndn._udp."; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 126 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 127 | for (ndn::Name::const_reverse_iterator i = identity.rbegin(); i != identity.rend(); i++) |
| 128 | { |
| 129 | serverName.append(i->toEscapedString()); |
| 130 | serverName.append("."); |
| 131 | } |
| 132 | serverName += "_homehub._autoconf.named-data.net"; |
| 133 | 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^] | 134 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 135 | QueryAnswer queryAnswer; |
| 136 | |
| 137 | int answerSize = res_query(serverName.c_str(), |
| 138 | ns_c_in, |
| 139 | ns_t_srv, |
| 140 | queryAnswer.buf, |
| 141 | sizeof(queryAnswer)); |
| 142 | |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 143 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 144 | // 3rd stage failed - abort |
| 145 | if (answerSize < 0) |
| 146 | { |
| 147 | std::cerr << "Failed to find a home router" << std::endl; |
| 148 | std::cerr << "exit" << std::endl; |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | bool isParsed = parseHostAndConnectToHub(queryAnswer, answerSize); |
| 153 | if (isParsed == false) |
| 154 | { |
| 155 | // Failed to parse DNS response |
| 156 | throw Error("Failed to parse DNS response"); |
| 157 | } |
| 158 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 159 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 160 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 161 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 162 | void |
| 163 | connectToHub(const std::string& uri) |
| 164 | { |
| 165 | std::cerr << "about to connect to: " << uri << std::endl; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 166 | |
| 167 | m_controller.start<ndn::nfd::FaceCreateCommand>( |
| 168 | ndn::nfd::ControlParameters() |
| 169 | .setUri(uri), |
| 170 | bind(&NdnAutoconfig::onHubConnectSuccess, this, |
| 171 | _1, "Succesfully created face: "), |
| 172 | bind(&NdnAutoconfig::onHubConnectError, this, |
| 173 | _1, _2, "Failed to create face: ") |
| 174 | ); |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 175 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 176 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 177 | void |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 178 | onHubConnectSuccess(const ndn::nfd::ControlParameters& resp, const std::string& message) |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 179 | { |
| 180 | std::cerr << message << resp << std::endl; |
| 181 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 182 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 183 | void |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 184 | onHubConnectError(uint32_t code, const std::string& error, const std::string& message) |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 185 | { |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 186 | std::ostringstream os; |
| 187 | os << message << ": " << error << " (code: " << code << ")"; |
| 188 | throw Error(os.str()); |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 191 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 192 | bool parseHostAndConnectToHub(QueryAnswer& queryAnswer, int answerSize) |
| 193 | { |
| 194 | // The references of the next classes are: |
| 195 | // http://www.diablotin.com/librairie/networking/dnsbind/ch14_02.htm |
| 196 | // https://gist.github.com/mologie/6027597 |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 197 | |
| 198 | struct rechdr |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 199 | { |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 200 | uint16_t type; |
| 201 | uint16_t iclass; |
| 202 | uint32_t ttl; |
| 203 | uint16_t length; |
| 204 | }; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 205 | |
| 206 | struct srv_t |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 207 | { |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 208 | uint16_t priority; |
| 209 | uint16_t weight; |
| 210 | uint16_t port; |
| 211 | uint8_t* target; |
| 212 | }; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 213 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 214 | if (ntohs(queryAnswer.header.ancount) == 0) |
| 215 | { |
| 216 | std::cerr << "No records found\n" << std::endl; |
| 217 | return false; |
| 218 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 219 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 220 | uint8_t* blob = queryAnswer.buf + NS_HFIXEDSZ; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 221 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 222 | blob += dn_skipname(blob, queryAnswer.buf + answerSize) + NS_QFIXEDSZ; |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 223 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 224 | for (int i = 0; i < ntohs(queryAnswer.header.ancount); i++) |
| 225 | { |
| 226 | char hostName[NS_MAXDNAME]; |
| 227 | int domainNameSize = dn_expand(queryAnswer.buf, |
| 228 | queryAnswer.buf + answerSize, |
| 229 | blob, |
| 230 | hostName, |
| 231 | NS_MAXDNAME); |
| 232 | if (domainNameSize < 0) |
| 233 | { |
| 234 | return false; |
| 235 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 236 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 237 | blob += domainNameSize; |
Davide Pesavento | 6fc750f | 2014-03-18 19:49:39 +0100 | [diff] [blame] | 238 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 239 | domainNameSize = dn_expand(queryAnswer.buf, |
| 240 | queryAnswer.buf + answerSize, |
| 241 | blob + 16, |
| 242 | hostName, |
| 243 | NS_MAXDNAME); |
| 244 | if (domainNameSize < 0) |
| 245 | { |
| 246 | return false; |
| 247 | } |
Davide Pesavento | 6fc750f | 2014-03-18 19:49:39 +0100 | [diff] [blame] | 248 | |
| 249 | srv_t* server = reinterpret_cast<srv_t*>(&blob[sizeof(rechdr)]); |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 250 | uint16_t convertedPort = be16toh(server->port); |
| 251 | std::string uri = "udp://"; |
| 252 | uri.append(hostName); |
| 253 | uri.append(":"); |
| 254 | uri.append(boost::lexical_cast<std::string>(convertedPort)); |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 255 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 256 | connectToHub(uri); |
| 257 | return true; |
| 258 | } |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 259 | |
| 260 | return false; |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 261 | } |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 262 | |
| 263 | private: |
| 264 | ndn::Face m_face; |
| 265 | ndn::nfd::Controller m_controller; |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 266 | }; |
| 267 | |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 268 | } // namespace tools |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 269 | |
| 270 | int |
| 271 | main() |
| 272 | { |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 273 | try |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 274 | { |
| 275 | tools::NdnAutoconfig autoConfigInstance; |
| 276 | |
| 277 | autoConfigInstance.discoverHubStage1(); |
| 278 | } |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 279 | catch (const std::exception& error) |
Alexander Afanasyev | 352e14e | 2014-03-27 16:02:12 -0700 | [diff] [blame^] | 280 | { |
| 281 | std::cerr << "ERROR: " << error.what() << std::endl; |
| 282 | } |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 283 | return 0; |
| 284 | } |