Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, 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" |
Yumin Xia | 99c821a | 2017-04-07 11:01:08 -0700 | [diff] [blame] | 25 | #include "validator/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 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 35 | #include <iostream> |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 36 | #include <memory> |
| 37 | #include <string> |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 38 | #include <fstream> |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 39 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 40 | NDNS_LOG_INIT(Dig); |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 41 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 42 | namespace ndn { |
| 43 | namespace ndns { |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 44 | |
| 45 | class NdnsDig |
| 46 | { |
| 47 | public: |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 48 | NdnsDig(const Name& dstLabel, |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 49 | const name::Component& rrType, bool shouldValidateIntermediate) |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 50 | : m_dstLabel(dstLabel) |
| 51 | , m_rrType(rrType) |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 52 | , m_interestLifetime(DEFAULT_INTEREST_LIFETIME) |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 53 | , m_validator(NdnsValidatorBuilder::create(m_face)) |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 54 | , m_shouldValidateIntermediate(shouldValidateIntermediate) |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 55 | , m_hasError(false) |
| 56 | { |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 57 | if (m_shouldValidateIntermediate) |
| 58 | m_ctr = std::unique_ptr<IterativeQueryController> |
| 59 | (new IterativeQueryController(m_dstLabel, m_rrType, m_interestLifetime, |
| 60 | bind(&NdnsDig::onSucceed, this, _1, _2), |
| 61 | bind(&NdnsDig::onFail, this, _1, _2), |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 62 | m_face, m_validator.get())); |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 63 | else |
| 64 | m_ctr = std::unique_ptr<IterativeQueryController> |
| 65 | (new IterativeQueryController(m_dstLabel, m_rrType, m_interestLifetime, |
| 66 | bind(&NdnsDig::onSucceed, this, _1, _2), |
| 67 | bind(&NdnsDig::onFail, this, _1, _2), |
| 68 | m_face, nullptr)); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void |
| 72 | run() |
| 73 | { |
| 74 | NDNS_LOG_INFO(" =================================== " |
| 75 | << "start to dig label = " << this->m_dstLabel |
| 76 | << " for type = " << this->m_rrType |
| 77 | << " =================================== "); |
| 78 | |
| 79 | try { |
| 80 | m_ctr->start(); // non-block, may throw exception |
| 81 | m_face.processEvents(); |
| 82 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 83 | catch (const std::exception& e) { |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 84 | std::cerr << "Error: " << e.what(); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 85 | m_hasError = true; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | stop() |
| 91 | { |
| 92 | m_face.getIoService().stop(); |
| 93 | NDNS_LOG_TRACE("application stops."); |
| 94 | } |
| 95 | |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 96 | void |
| 97 | setStartZone(const Name& start) |
| 98 | { |
| 99 | m_ctr->setStartComponentIndex(start.size()); |
| 100 | } |
| 101 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 102 | private: |
| 103 | void |
| 104 | onSucceed(const Data& data, const Response& response) |
| 105 | { |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 106 | NDNS_LOG_INFO("Dig get following Response (need verification):"); |
| 107 | Name name = Name().append(response.getZone()).append(response.getRrLabel()); |
| 108 | if (name == m_dstLabel && m_rrType == response.getRrType()) { |
| 109 | NDNS_LOG_INFO("This is the final response returned by zone=" << response.getZone() |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 110 | << " and NdnsType=" << response.getContentType() |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 111 | << ". It contains " << response.getRrs().size() << " RR(s)"); |
| 112 | |
| 113 | std::string msg; |
| 114 | size_t i = 0; |
| 115 | for (const auto& rr : response.getRrs()) { |
| 116 | try { |
| 117 | msg = std::string(reinterpret_cast<const char*>(rr.value()), rr.value_size()); |
| 118 | NDNS_LOG_INFO("succeed to get the info from RR[" << i << "]" |
| 119 | "type=" << rr.type() << " content=" << msg); |
| 120 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 121 | catch (const std::exception& e) { |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 122 | NDNS_LOG_INFO("error to get the info from RR[" << i << "]" |
| 123 | "type=" << rr.type()); |
| 124 | } |
| 125 | ++i; |
| 126 | } |
| 127 | } |
| 128 | else { |
| 129 | NDNS_LOG_INFO("[* !! *] This is not final response.The target Label: " |
| 130 | << m_dstLabel << " may not exist"); |
| 131 | } |
| 132 | |
| 133 | if (m_dstFile.empty()) { |
| 134 | ; |
| 135 | } |
| 136 | else if (m_dstFile == "-") { |
| 137 | output(data, std::cout, true); |
| 138 | } |
| 139 | else { |
| 140 | NDNS_LOG_INFO("output Data packet to " << m_dstFile << " with BASE64 encoding format"); |
| 141 | std::filebuf fb; |
| 142 | fb.open(m_dstFile, std::ios::out); |
| 143 | std::ostream os(&fb); |
| 144 | output(data, os, false); |
| 145 | } |
| 146 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 147 | NDNS_LOG_INFO(response); |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 148 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 149 | NDNS_LOG_TRACE("to verify the response"); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 150 | m_validator->validate(data, |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 151 | bind(&NdnsDig::onDataValidated, this, _1), |
| 152 | bind(&NdnsDig::onDataValidationFailed, this, _1, _2) |
| 153 | ); |
| 154 | } |
| 155 | |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 156 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 157 | void |
| 158 | onFail(uint32_t errCode, const std::string& errMsg) |
| 159 | { |
| 160 | NDNS_LOG_INFO("fail to get response: errCode=" << errCode << " msg=" << errMsg); |
| 161 | m_hasError = true; |
| 162 | this->stop(); |
| 163 | } |
| 164 | |
| 165 | void |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 166 | onDataValidated(const Data& data) |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 167 | { |
| 168 | NDNS_LOG_INFO("final data pass verification"); |
| 169 | this->stop(); |
| 170 | } |
| 171 | |
| 172 | void |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 173 | onDataValidationFailed(const Data& data, const security::v2::ValidationError& err) |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 174 | { |
| 175 | NDNS_LOG_INFO("final data does not pass verification"); |
| 176 | m_hasError = true; |
| 177 | this->stop(); |
| 178 | } |
| 179 | |
| 180 | public: |
| 181 | void |
| 182 | setInterestLifetime(const time::milliseconds& lifetime) |
| 183 | { |
| 184 | m_interestLifetime = lifetime; |
| 185 | } |
| 186 | |
Alexander Afanasyev | 984ca9d | 2016-12-19 13:09:14 -0800 | [diff] [blame] | 187 | bool |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 188 | hasError() const |
| 189 | { |
| 190 | return m_hasError; |
| 191 | } |
| 192 | |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 193 | void |
| 194 | setDstFile(const std::string& dstFile) |
| 195 | { |
| 196 | m_dstFile = dstFile; |
| 197 | } |
| 198 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 199 | private: |
| 200 | Name m_dstLabel; |
| 201 | name::Component m_rrType; |
| 202 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 203 | Name m_certName; |
| 204 | time::milliseconds m_interestLifetime; |
| 205 | |
| 206 | Face m_face; |
| 207 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 208 | unique_ptr<security::v2::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 | { |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 223 | using std::string; |
| 224 | using namespace ndn; |
| 225 | |
| 226 | Name dstLabel; |
| 227 | int ttl = 4; |
| 228 | string rrType = "TXT"; |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 229 | string dstFile; |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 230 | bool shouldValidateIntermediate = true; |
| 231 | Name start("/ndn"); |
| 232 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 233 | try { |
| 234 | namespace po = boost::program_options; |
| 235 | po::variables_map vm; |
| 236 | |
| 237 | po::options_description generic("Generic Options"); |
| 238 | generic.add_options()("help,h", "print help message"); |
| 239 | |
| 240 | po::options_description config("Configuration"); |
| 241 | config.add_options() |
Shock Jiang | bb4e15b | 2014-12-05 09:48:02 -0800 | [diff] [blame] | 242 | ("timeout,T", po::value<int>(&ttl), "query timeout. default: 4 sec") |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 243 | ("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] | 244 | ("dstFile,d", po::value<std::string>(&dstFile), "set output file of the received Data. " |
| 245 | "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] | 246 | ("start,s", po::value<Name>(&start)->default_value("/ndn"), "set first zone to query") |
| 247 | ("not-validate,n", "trigger not validate intermediate results") |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 248 | ; |
| 249 | |
| 250 | po::options_description hidden("Hidden Options"); |
| 251 | hidden.add_options() |
| 252 | ("name", po::value<Name>(&dstLabel), "name to be resolved") |
| 253 | ; |
| 254 | po::positional_options_description postion; |
| 255 | postion.add("name", 1); |
| 256 | |
| 257 | po::options_description cmdline_options; |
| 258 | cmdline_options.add(generic).add(config).add(hidden); |
| 259 | |
| 260 | po::options_description config_file_options; |
| 261 | config_file_options.add(config).add(hidden); |
| 262 | |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 263 | 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] | 264 | "[-d dstFile] [-s startZone] [-n]\n" |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 265 | "Allowed options"); |
| 266 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 267 | visible.add(generic).add(config); |
| 268 | |
| 269 | po::parsed_options parsed = |
| 270 | po::command_line_parser(argc, argv).options(cmdline_options).positional(postion).run(); |
| 271 | |
| 272 | po::store(parsed, vm); |
| 273 | po::notify(vm); |
| 274 | |
| 275 | if (vm.count("help")) { |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 276 | std::cout << visible << std::endl; |
| 277 | return 0; |
| 278 | } |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 279 | |
Shock Jiang | bb4e15b | 2014-12-05 09:48:02 -0800 | [diff] [blame] | 280 | if (!vm.count("name")) { |
| 281 | std::cerr << "must contain a target label parameter." << std::endl; |
| 282 | std::cerr << visible << std::endl; |
| 283 | return 1; |
| 284 | } |
| 285 | |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 286 | if (!start.isPrefixOf(dstLabel)) { |
| 287 | std::cerr << "Error: start zone " << start << " is not prefix of the target label " |
| 288 | << dstLabel << std::endl; |
| 289 | return 1; |
| 290 | } |
| 291 | |
| 292 | if (vm.count("not-validate")) { |
| 293 | shouldValidateIntermediate = false; |
| 294 | } |
| 295 | |
| 296 | if (ttl < 0) { |
| 297 | std::cerr << "Error: ttl parameter cannot be negative" << std::endl; |
| 298 | return 1; |
| 299 | } |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 300 | } |
| 301 | catch (const std::exception& ex) { |
| 302 | std::cerr << "Parameter Error: " << ex.what() << std::endl; |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 303 | return 1; |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Shock Jiang | bb4e15b | 2014-12-05 09:48:02 -0800 | [diff] [blame] | 306 | try { |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 307 | ndn::ndns::NdnsDig dig(dstLabel, ndn::name::Component(rrType), shouldValidateIntermediate); |
Shock Jiang | bb4e15b | 2014-12-05 09:48:02 -0800 | [diff] [blame] | 308 | dig.setInterestLifetime(ndn::time::seconds(ttl)); |
| 309 | dig.setDstFile(dstFile); |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 310 | |
Shock Jiang | bb4e15b | 2014-12-05 09:48:02 -0800 | [diff] [blame] | 311 | // Due to ndn testbed does not contain the root zone |
| 312 | // dig here starts from the TLD (Top-level Domain) |
| 313 | // precondition is that TLD : 1) only contains one component in its name; 2) its name is routable |
| 314 | dig.setStartZone(start); |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 315 | |
Shock Jiang | bb4e15b | 2014-12-05 09:48:02 -0800 | [diff] [blame] | 316 | dig.run(); |
Shock Jiang | 5d5928c | 2014-12-03 13:41:22 -0800 | [diff] [blame] | 317 | |
Shock Jiang | bb4e15b | 2014-12-05 09:48:02 -0800 | [diff] [blame] | 318 | if (dig.hasError()) |
| 319 | return 1; |
| 320 | else |
| 321 | return 0; |
| 322 | } |
Shock Jiang | bb4e15b | 2014-12-05 09:48:02 -0800 | [diff] [blame] | 323 | catch (const std::exception& e) { |
| 324 | std::cerr << "Error: " << e.what() << std::endl; |
| 325 | return 1; |
| 326 | } |
| 327 | |
Shock Jiang | 698e6ed | 2014-11-09 11:22:24 -0800 | [diff] [blame] | 328 | } |