Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 984ca9d | 2016-12-19 13:09:14 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2014-2016, Regents of the University of California. |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDNS (Named Data Networking Domain Name Service). |
| 6 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
| 7 | * |
| 8 | * NDNS is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "ndns-label.hpp" |
| 21 | #include "logger.hpp" |
| 22 | #include "clients/response.hpp" |
| 23 | #include "clients/query.hpp" |
| 24 | #include "clients/iterative-query-controller.hpp" |
| 25 | #include "validator.hpp" |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 26 | #include "util/util.hpp" |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 27 | |
| 28 | #include <ndn-cxx/security/key-chain.hpp> |
| 29 | #include <ndn-cxx/face.hpp> |
| 30 | #include <boost/program_options.hpp> |
| 31 | #include <boost/asio.hpp> |
| 32 | #include <boost/filesystem.hpp> |
| 33 | #include <boost/noncopyable.hpp> |
| 34 | |
| 35 | #include <memory> |
| 36 | #include <string> |
| 37 | |
Alexander Afanasyev | c7c9900 | 2015-10-09 17:27:30 -0700 | [diff] [blame] | 38 | NDNS_LOG_INIT("NdnsDig") |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 39 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 40 | namespace ndn { |
| 41 | namespace ndns { |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 42 | |
| 43 | class NdnsDig |
| 44 | { |
| 45 | public: |
| 46 | NdnsDig(const Name& hint, const Name& dstLabel, |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 47 | const name::Component& rrType, bool shouldValidateIntermediate) |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 48 | : m_dstLabel(dstLabel) |
| 49 | , m_rrType(rrType) |
| 50 | , m_hint(hint) |
| 51 | , m_interestLifetime(DEFAULT_INTEREST_LIFETIME) |
| 52 | , m_validator(m_face) |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 53 | , m_shouldValidateIntermediate(shouldValidateIntermediate) |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 54 | , m_hasError(false) |
| 55 | { |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 56 | if (m_shouldValidateIntermediate) |
| 57 | m_ctr = std::unique_ptr<IterativeQueryController> |
| 58 | (new IterativeQueryController(m_dstLabel, m_rrType, m_interestLifetime, |
| 59 | bind(&NdnsDig::onSucceed, this, _1, _2), |
| 60 | bind(&NdnsDig::onFail, this, _1, _2), |
| 61 | m_face, &m_validator)); |
| 62 | else |
| 63 | m_ctr = std::unique_ptr<IterativeQueryController> |
| 64 | (new IterativeQueryController(m_dstLabel, m_rrType, m_interestLifetime, |
| 65 | bind(&NdnsDig::onSucceed, this, _1, _2), |
| 66 | bind(&NdnsDig::onFail, this, _1, _2), |
| 67 | m_face, nullptr)); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void |
| 71 | run() |
| 72 | { |
| 73 | NDNS_LOG_INFO(" =================================== " |
| 74 | << "start to dig label = " << this->m_dstLabel |
| 75 | << " for type = " << this->m_rrType |
| 76 | << " =================================== "); |
| 77 | |
| 78 | try { |
| 79 | m_ctr->start(); // non-block, may throw exception |
| 80 | m_face.processEvents(); |
| 81 | } |
| 82 | catch (std::exception& e) { |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 83 | std::cerr << "Error: " << e.what(); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 84 | m_hasError = true; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | stop() |
| 90 | { |
| 91 | m_face.getIoService().stop(); |
| 92 | NDNS_LOG_TRACE("application stops."); |
| 93 | } |
| 94 | |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 95 | void |
| 96 | setStartZone(const Name& start) |
| 97 | { |
| 98 | m_ctr->setStartComponentIndex(start.size()); |
| 99 | } |
| 100 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 101 | private: |
| 102 | void |
| 103 | onSucceed(const Data& data, const Response& response) |
| 104 | { |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 105 | NDNS_LOG_INFO("Dig get following Response (need verification):"); |
| 106 | Name name = Name().append(response.getZone()).append(response.getRrLabel()); |
| 107 | if (name == m_dstLabel && m_rrType == response.getRrType()) { |
| 108 | NDNS_LOG_INFO("This is the final response returned by zone=" << response.getZone() |
| 109 | << " and NdnsType=" << response.getNdnsType() |
| 110 | << ". It contains " << response.getRrs().size() << " RR(s)"); |
| 111 | |
| 112 | std::string msg; |
| 113 | size_t i = 0; |
| 114 | for (const auto& rr : response.getRrs()) { |
| 115 | try { |
| 116 | msg = std::string(reinterpret_cast<const char*>(rr.value()), rr.value_size()); |
| 117 | NDNS_LOG_INFO("succeed to get the info from RR[" << i << "]" |
| 118 | "type=" << rr.type() << " content=" << msg); |
| 119 | } |
| 120 | catch (std::exception& e) { |
| 121 | NDNS_LOG_INFO("error to get the info from RR[" << i << "]" |
| 122 | "type=" << rr.type()); |
| 123 | } |
| 124 | ++i; |
| 125 | } |
| 126 | } |
| 127 | else { |
| 128 | NDNS_LOG_INFO("[* !! *] This is not final response.The target Label: " |
| 129 | << m_dstLabel << " may not exist"); |
| 130 | } |
| 131 | |
| 132 | if (m_dstFile.empty()) { |
| 133 | ; |
| 134 | } |
| 135 | else if (m_dstFile == "-") { |
| 136 | output(data, std::cout, true); |
| 137 | } |
| 138 | else { |
| 139 | NDNS_LOG_INFO("output Data packet to " << m_dstFile << " with BASE64 encoding format"); |
| 140 | std::filebuf fb; |
| 141 | fb.open(m_dstFile, std::ios::out); |
| 142 | std::ostream os(&fb); |
| 143 | output(data, os, false); |
| 144 | } |
| 145 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 146 | NDNS_LOG_INFO(response); |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 147 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 148 | NDNS_LOG_TRACE("to verify the response"); |
| 149 | m_validator.validate(data, |
| 150 | bind(&NdnsDig::onDataValidated, this, _1), |
| 151 | bind(&NdnsDig::onDataValidationFailed, this, _1, _2) |
| 152 | ); |
| 153 | } |
| 154 | |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 155 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 156 | void |
| 157 | onFail(uint32_t errCode, const std::string& errMsg) |
| 158 | { |
| 159 | NDNS_LOG_INFO("fail to get response: errCode=" << errCode << " msg=" << errMsg); |
| 160 | m_hasError = true; |
| 161 | this->stop(); |
| 162 | } |
| 163 | |
| 164 | void |
| 165 | onDataValidated(const shared_ptr<const Data>& data) |
| 166 | { |
| 167 | NDNS_LOG_INFO("final data pass verification"); |
| 168 | this->stop(); |
| 169 | } |
| 170 | |
| 171 | void |
| 172 | onDataValidationFailed(const shared_ptr<const Data>& data, const std::string& str) |
| 173 | { |
| 174 | NDNS_LOG_INFO("final data does not pass verification"); |
| 175 | m_hasError = true; |
| 176 | this->stop(); |
| 177 | } |
| 178 | |
| 179 | public: |
| 180 | void |
| 181 | setInterestLifetime(const time::milliseconds& lifetime) |
| 182 | { |
| 183 | m_interestLifetime = lifetime; |
| 184 | } |
| 185 | |
Alexander Afanasyev | 984ca9d | 2016-12-19 13:09:14 -0800 | [diff] [blame^] | 186 | bool |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 187 | hasError() const |
| 188 | { |
| 189 | return m_hasError; |
| 190 | } |
| 191 | |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 192 | void |
| 193 | setDstFile(const std::string& dstFile) |
| 194 | { |
| 195 | m_dstFile = dstFile; |
| 196 | } |
| 197 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 198 | private: |
| 199 | Name m_dstLabel; |
| 200 | name::Component m_rrType; |
| 201 | |
| 202 | Name m_hint; |
| 203 | Name m_certName; |
| 204 | time::milliseconds m_interestLifetime; |
| 205 | |
| 206 | Face m_face; |
| 207 | |
| 208 | Validator m_validator; |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 209 | bool m_shouldValidateIntermediate; |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 210 | std::unique_ptr<QueryController> m_ctr; |
| 211 | |
| 212 | bool m_hasError; |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 213 | std::string m_dstFile; |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | } // namespace ndns |
| 217 | } // namespace ndn |
| 218 | |
| 219 | |
| 220 | int |
| 221 | main(int argc, char* argv[]) |
| 222 | { |
| 223 | ndn::ndns::log::init(); |
| 224 | using std::string; |
| 225 | using namespace ndn; |
| 226 | |
| 227 | Name dstLabel; |
| 228 | int ttl = 4; |
| 229 | string rrType = "TXT"; |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 230 | string dstFile; |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 231 | bool shouldValidateIntermediate = true; |
| 232 | Name start("/ndn"); |
| 233 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 234 | try { |
| 235 | namespace po = boost::program_options; |
| 236 | po::variables_map vm; |
| 237 | |
| 238 | po::options_description generic("Generic Options"); |
| 239 | generic.add_options()("help,h", "print help message"); |
| 240 | |
| 241 | po::options_description config("Configuration"); |
| 242 | config.add_options() |
Shock Jiang | bb4e15b | 2014-12-05 09:48:02 -0800 | [diff] [blame] | 243 | ("timeout,T", po::value<int>(&ttl), "query timeout. default: 4 sec") |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 244 | ("rrtype,t", po::value<std::string>(&rrType), "set request RR Type. default: TXT") |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 245 | ("dstFile,d", po::value<std::string>(&dstFile), "set output file of the received Data. " |
| 246 | "if omitted, not print; if set to be -, print to stdout; else print to file") |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 247 | ("start,s", po::value<Name>(&start)->default_value("/ndn"), "set first zone to query") |
| 248 | ("not-validate,n", "trigger not validate intermediate results") |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 249 | ; |
| 250 | |
| 251 | po::options_description hidden("Hidden Options"); |
| 252 | hidden.add_options() |
| 253 | ("name", po::value<Name>(&dstLabel), "name to be resolved") |
| 254 | ; |
| 255 | po::positional_options_description postion; |
| 256 | postion.add("name", 1); |
| 257 | |
| 258 | po::options_description cmdline_options; |
| 259 | cmdline_options.add(generic).add(config).add(hidden); |
| 260 | |
| 261 | po::options_description config_file_options; |
| 262 | config_file_options.add(config).add(hidden); |
| 263 | |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 264 | po::options_description visible("Usage: ndns-dig /name/to/be/resolved [-t rrType] [-T ttl]" |
Shock Jiang | bb4e15b | 2014-12-05 09:48:02 -0800 | [diff] [blame] | 265 | "[-d dstFile] [-s startZone] [-n]\n" |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 266 | "Allowed options"); |
| 267 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 268 | visible.add(generic).add(config); |
| 269 | |
| 270 | po::parsed_options parsed = |
| 271 | po::command_line_parser(argc, argv).options(cmdline_options).positional(postion).run(); |
| 272 | |
| 273 | po::store(parsed, vm); |
| 274 | po::notify(vm); |
| 275 | |
| 276 | if (vm.count("help")) { |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 277 | std::cout << visible << std::endl; |
| 278 | return 0; |
| 279 | } |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 280 | |
Shock Jiang | bb4e15b | 2014-12-05 09:48:02 -0800 | [diff] [blame] | 281 | if (!vm.count("name")) { |
| 282 | std::cerr << "must contain a target label parameter." << std::endl; |
| 283 | std::cerr << visible << std::endl; |
| 284 | return 1; |
| 285 | } |
| 286 | |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 287 | if (!start.isPrefixOf(dstLabel)) { |
| 288 | std::cerr << "Error: start zone " << start << " is not prefix of the target label " |
| 289 | << dstLabel << std::endl; |
| 290 | return 1; |
| 291 | } |
| 292 | |
| 293 | if (vm.count("not-validate")) { |
| 294 | shouldValidateIntermediate = false; |
| 295 | } |
| 296 | |
| 297 | if (ttl < 0) { |
| 298 | std::cerr << "Error: ttl parameter cannot be negative" << std::endl; |
| 299 | return 1; |
| 300 | } |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 301 | } |
| 302 | catch (const std::exception& ex) { |
| 303 | std::cerr << "Parameter Error: " << ex.what() << std::endl; |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 304 | return 1; |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Shock Jiang | bb4e15b | 2014-12-05 09:48:02 -0800 | [diff] [blame] | 307 | try { |
| 308 | ndn::ndns::NdnsDig dig("", dstLabel, ndn::name::Component(rrType), shouldValidateIntermediate); |
| 309 | dig.setInterestLifetime(ndn::time::seconds(ttl)); |
| 310 | dig.setDstFile(dstFile); |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 311 | |
Shock Jiang | bb4e15b | 2014-12-05 09:48:02 -0800 | [diff] [blame] | 312 | // Due to ndn testbed does not contain the root zone |
| 313 | // dig here starts from the TLD (Top-level Domain) |
| 314 | // precondition is that TLD : 1) only contains one component in its name; 2) its name is routable |
| 315 | dig.setStartZone(start); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 316 | |
Shock Jiang | bb4e15b | 2014-12-05 09:48:02 -0800 | [diff] [blame] | 317 | dig.run(); |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 318 | |
Shock Jiang | bb4e15b | 2014-12-05 09:48:02 -0800 | [diff] [blame] | 319 | if (dig.hasError()) |
| 320 | return 1; |
| 321 | else |
| 322 | return 0; |
| 323 | } |
| 324 | catch (const ndn::ValidatorConfig::Error& e) { |
| 325 | std::cerr << "Fail to create the validator: " << e.what() << std::endl; |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 326 | return 1; |
Shock Jiang | bb4e15b | 2014-12-05 09:48:02 -0800 | [diff] [blame] | 327 | } |
| 328 | catch (const std::exception& e) { |
| 329 | std::cerr << "Error: " << e.what() << std::endl; |
| 330 | return 1; |
| 331 | } |
| 332 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 333 | } |