blob: 30afe88420096259e761cfdc26c415f5a53a710d [file] [log] [blame]
Shock Jiang698e6ed2014-11-09 11:22:24 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
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 Jiang06cd2142014-11-23 17:36:02 -080026#include "util/util.hpp"
Shock Jiang698e6ed2014-11-09 11:22:24 -080027
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 Afanasyevc7c99002015-10-09 17:27:30 -070038NDNS_LOG_INIT("NdnsDig")
Shock Jiang5d5928c2014-12-03 13:41:22 -080039
Shock Jiang698e6ed2014-11-09 11:22:24 -080040namespace ndn {
41namespace ndns {
Shock Jiang698e6ed2014-11-09 11:22:24 -080042
43class NdnsDig
44{
45public:
46 NdnsDig(const Name& hint, const Name& dstLabel,
Shock Jiang5d5928c2014-12-03 13:41:22 -080047 const name::Component& rrType, bool shouldValidateIntermediate)
Shock Jiang698e6ed2014-11-09 11:22:24 -080048 : m_dstLabel(dstLabel)
49 , m_rrType(rrType)
50 , m_hint(hint)
51 , m_interestLifetime(DEFAULT_INTEREST_LIFETIME)
52 , m_validator(m_face)
Shock Jiang5d5928c2014-12-03 13:41:22 -080053 , m_shouldValidateIntermediate(shouldValidateIntermediate)
Shock Jiang698e6ed2014-11-09 11:22:24 -080054 , m_hasError(false)
55 {
Shock Jiang5d5928c2014-12-03 13:41:22 -080056 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 Jiang698e6ed2014-11-09 11:22:24 -080068 }
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 Jiang5d5928c2014-12-03 13:41:22 -080083 std::cerr << "Error: " << e.what();
Shock Jiang698e6ed2014-11-09 11:22:24 -080084 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 Jiang5d5928c2014-12-03 13:41:22 -080095 void
96 setStartZone(const Name& start)
97 {
98 m_ctr->setStartComponentIndex(start.size());
99 }
100
Shock Jiang698e6ed2014-11-09 11:22:24 -0800101private:
102 void
103 onSucceed(const Data& data, const Response& response)
104 {
Shock Jiang06cd2142014-11-23 17:36:02 -0800105 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 Jiang698e6ed2014-11-09 11:22:24 -0800146 NDNS_LOG_INFO(response);
Shock Jiang06cd2142014-11-23 17:36:02 -0800147
Shock Jiang698e6ed2014-11-09 11:22:24 -0800148 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 Jiang06cd2142014-11-23 17:36:02 -0800155
Shock Jiang698e6ed2014-11-09 11:22:24 -0800156 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
179public:
180 void
181 setInterestLifetime(const time::milliseconds& lifetime)
182 {
183 m_interestLifetime = lifetime;
184 }
185
186 const bool
187 hasError() const
188 {
189 return m_hasError;
190 }
191
Shock Jiang06cd2142014-11-23 17:36:02 -0800192 void
193 setDstFile(const std::string& dstFile)
194 {
195 m_dstFile = dstFile;
196 }
197
Shock Jiang698e6ed2014-11-09 11:22:24 -0800198private:
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 Jiang5d5928c2014-12-03 13:41:22 -0800209 bool m_shouldValidateIntermediate;
Shock Jiang698e6ed2014-11-09 11:22:24 -0800210 std::unique_ptr<QueryController> m_ctr;
211
212 bool m_hasError;
Shock Jiang06cd2142014-11-23 17:36:02 -0800213 std::string m_dstFile;
Shock Jiang698e6ed2014-11-09 11:22:24 -0800214};
215
216} // namespace ndns
217} // namespace ndn
218
219
220int
221main(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 Jiang06cd2142014-11-23 17:36:02 -0800230 string dstFile;
Shock Jiang5d5928c2014-12-03 13:41:22 -0800231 bool shouldValidateIntermediate = true;
232 Name start("/ndn");
233
Shock Jiang698e6ed2014-11-09 11:22:24 -0800234 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 Jiangbb4e15b2014-12-05 09:48:02 -0800243 ("timeout,T", po::value<int>(&ttl), "query timeout. default: 4 sec")
Shock Jiang698e6ed2014-11-09 11:22:24 -0800244 ("rrtype,t", po::value<std::string>(&rrType), "set request RR Type. default: TXT")
Shock Jiang06cd2142014-11-23 17:36:02 -0800245 ("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 Jiang5d5928c2014-12-03 13:41:22 -0800247 ("start,s", po::value<Name>(&start)->default_value("/ndn"), "set first zone to query")
248 ("not-validate,n", "trigger not validate intermediate results")
Shock Jiang698e6ed2014-11-09 11:22:24 -0800249 ;
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 Jiang06cd2142014-11-23 17:36:02 -0800264 po::options_description visible("Usage: ndns-dig /name/to/be/resolved [-t rrType] [-T ttl]"
Shock Jiangbb4e15b2014-12-05 09:48:02 -0800265 "[-d dstFile] [-s startZone] [-n]\n"
Shock Jiang06cd2142014-11-23 17:36:02 -0800266 "Allowed options");
267
Shock Jiang698e6ed2014-11-09 11:22:24 -0800268 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 Jiang698e6ed2014-11-09 11:22:24 -0800277 std::cout << visible << std::endl;
278 return 0;
279 }
Shock Jiang5d5928c2014-12-03 13:41:22 -0800280
Shock Jiangbb4e15b2014-12-05 09:48:02 -0800281 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 Jiang5d5928c2014-12-03 13:41:22 -0800287 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 Jiang698e6ed2014-11-09 11:22:24 -0800301 }
302 catch (const std::exception& ex) {
303 std::cerr << "Parameter Error: " << ex.what() << std::endl;
Shock Jiang5d5928c2014-12-03 13:41:22 -0800304 return 1;
Shock Jiang698e6ed2014-11-09 11:22:24 -0800305 }
306
Shock Jiangbb4e15b2014-12-05 09:48:02 -0800307 try {
308 ndn::ndns::NdnsDig dig("", dstLabel, ndn::name::Component(rrType), shouldValidateIntermediate);
309 dig.setInterestLifetime(ndn::time::seconds(ttl));
310 dig.setDstFile(dstFile);
Shock Jiang5d5928c2014-12-03 13:41:22 -0800311
Shock Jiangbb4e15b2014-12-05 09:48:02 -0800312 // 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 Jiang698e6ed2014-11-09 11:22:24 -0800316
Shock Jiangbb4e15b2014-12-05 09:48:02 -0800317 dig.run();
Shock Jiang5d5928c2014-12-03 13:41:22 -0800318
Shock Jiangbb4e15b2014-12-05 09:48:02 -0800319 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 Jiang698e6ed2014-11-09 11:22:24 -0800326 return 1;
Shock Jiangbb4e15b2014-12-05 09:48:02 -0800327 }
328 catch (const std::exception& e) {
329 std::cerr << "Error: " << e.what() << std::endl;
330 return 1;
331 }
332
Shock Jiang698e6ed2014-11-09 11:22:24 -0800333}