blob: 881e0e781589d7a07468586a7dafd4f7dcfcf0bb [file] [log] [blame]
Shock Jiangae429122014-10-24 09:04:58 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi767f35c2016-07-23 01:54:42 +00003 * Copyright (c) 2014-2016, Regents of the University of California.
Shock Jiangae429122014-10-24 09:04:58 -07004 *
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 "clients/response.hpp"
21#include "clients/query.hpp"
22#include "ndns-label.hpp"
23#include "validator.hpp"
24#include "ndns-enum.hpp"
25#include "ndns-tlv.hpp"
26#include "logger.hpp"
27#include "daemon/db-mgr.hpp"
28#include "util/util.hpp"
29
30#include <ndn-cxx/security/key-chain.hpp>
31#include <ndn-cxx/data.hpp>
32#include <ndn-cxx/util/io.hpp>
33#include <ndn-cxx/encoding/block.hpp>
34#include <ndn-cxx/encoding/block-helpers.hpp>
35#include <boost/noncopyable.hpp>
36#include <boost/program_options.hpp>
37#include <boost/asio.hpp>
38#include <boost/filesystem.hpp>
39
40#include <string>
41#include <tuple>
42
43namespace ndn {
44namespace ndns {
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070045
46NDNS_LOG_INIT("NdnsUpdate")
Shock Jiangae429122014-10-24 09:04:58 -070047
48class NdnsUpdate : noncopyable
49{
50public:
Yumin Xia6343c5b2016-10-20 15:45:50 -070051 NdnsUpdate(const Name& zone, const shared_ptr<Data>& update, Face& face)
52 : m_zone(zone)
Shock Jiangae429122014-10-24 09:04:58 -070053 , m_interestLifetime(DEFAULT_INTEREST_LIFETIME)
54 , m_face(face)
55 , m_validator(face)
Shock Jiang43e59b42014-12-02 15:05:02 -080056 , m_update(update)
Shock Jiangae429122014-10-24 09:04:58 -070057 , m_hasError(false)
58 {
Shock Jiangae429122014-10-24 09:04:58 -070059 }
60
61 void
Shock Jiang43e59b42014-12-02 15:05:02 -080062 start()
Shock Jiangae429122014-10-24 09:04:58 -070063 {
Shock Jiang43e59b42014-12-02 15:05:02 -080064 NDNS_LOG_INFO(" ================ "
Shock Jiangae429122014-10-24 09:04:58 -070065 << "start to update RR at Zone = " << this->m_zone
Shock Jiang43e59b42014-12-02 15:05:02 -080066 << " new RR is: " << m_update->getName()
67 <<" =================== ");
68
69 NDNS_LOG_INFO("new RR is signed by: "
70 << m_update->getSignature().getKeyLocator().getName());
Shock Jiangae429122014-10-24 09:04:58 -070071
72 Interest interest = this->makeUpdateInterest();
Shock Jiang43e59b42014-12-02 15:05:02 -080073 NDNS_LOG_TRACE("[* <- *] send Update: " << m_update->getName().toUri());
Shock Jiangae429122014-10-24 09:04:58 -070074 m_face.expressInterest(interest,
75 bind(&NdnsUpdate::onData, this, _1, _2),
Alexander Afanasyevf4193ea2017-06-12 15:35:13 -070076 bind(&NdnsUpdate::onTimeout, this, _1), // nack
Shock Jiangae429122014-10-24 09:04:58 -070077 bind(&NdnsUpdate::onTimeout, this, _1) //dynamic binding
78 );
Shock Jiangae429122014-10-24 09:04:58 -070079 }
80
81 void
82 stop()
83 {
84 m_face.getIoService().stop();
85 }
86
87private:
88 void
89 onData(const Interest& interest, const Data& data)
90 {
91 NDNS_LOG_INFO("get response of Update");
92 int ret = -1;
93 std::string msg;
94 std::tie(ret, msg) = this->parseResponse(data);
95 NDNS_LOG_INFO("Return Code: " << ret << ", and Update "
96 << (ret == UPDATE_OK ? "succeeds" : "fails"));
97 if (ret != UPDATE_OK)
98 m_hasError = true;
99
100 if (!msg.empty()) {
101 NDNS_LOG_INFO("Return Msg: " << msg);
102 }
103
104 NDNS_LOG_INFO("to verify the response");
105 m_validator.validate(data,
106 bind(&NdnsUpdate::onDataValidated, this, _1),
107 bind(&NdnsUpdate::onDataValidationFailed, this, _1, _2)
108 );
109 }
110
111 std::tuple<int, std::string>
112 parseResponse(const Data& data)
113 {
114 int ret = -1;
115 std::string msg;
116 Block blk = data.getContent();
117 blk.parse();
118 Block block = blk.blockFromValue();
119 block.parse();
120 Block::element_const_iterator val = block.elements_begin();
121 for (; val != block.elements_end(); ++val) {
122 if (val->type() == ndns::tlv::UpdateReturnCode) { // the first must be return code
123 ret = readNonNegativeInteger(*val);
124 }
125 else if (val->type() == ndns::tlv::UpdateReturnMsg) {
126 msg = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
127 }
128 }
129
130 return std::make_tuple(ret, msg);
131 }
132
133 /**
134 * @brief construct a query (interest) which contains the update information
135 */
136 Interest
137 makeUpdateInterest()
138 {
Yumin Xia6343c5b2016-10-20 15:45:50 -0700139 Query q(m_zone, label::NDNS_ITERATIVE_QUERY);
Shock Jiang43e59b42014-12-02 15:05:02 -0800140 q.setRrLabel(Name().append(m_update->wireEncode()));
Shock Jiangae429122014-10-24 09:04:58 -0700141 q.setRrType(label::NDNS_UPDATE_LABEL);
142 q.setInterestLifetime(m_interestLifetime);
143
144 return q.toInterest();
145 }
146
147private:
148 void
149 onTimeout(const ndn::Interest& interest)
150 {
151 NDNS_LOG_TRACE("Update timeouts");
152 m_hasError = true;
153 this->stop();
154 }
155
156 void
157 onDataValidated(const shared_ptr<const Data>& data)
158 {
159 NDNS_LOG_INFO("data pass verification");
160 this->stop();
161 }
162
163 void
164 onDataValidationFailed(const shared_ptr<const Data>& data, const std::string& str)
165 {
166 NDNS_LOG_INFO("data does not pass verification");
167 m_hasError = true;
168 this->stop();
169 }
170
171public:
Shock Jiangae429122014-10-24 09:04:58 -0700172
173 void
174 setInterestLifetime(const time::milliseconds& interestLifetime)
175 {
176 m_interestLifetime = interestLifetime;
177 }
178
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800179 bool
Shock Jiangae429122014-10-24 09:04:58 -0700180 hasError() const
181 {
182 return m_hasError;
183 }
184
185private:
Shock Jiangae429122014-10-24 09:04:58 -0700186 Name m_zone;
Shock Jiang43e59b42014-12-02 15:05:02 -0800187
Shock Jiangae429122014-10-24 09:04:58 -0700188 time::milliseconds m_interestLifetime;
189
190 Face& m_face;
191 Validator m_validator;
192 KeyChain m_keyChain;
193
Shock Jiang43e59b42014-12-02 15:05:02 -0800194 shared_ptr<Data> m_update;
Shock Jiangae429122014-10-24 09:04:58 -0700195 bool m_hasError;
196};
197
198} // namespace ndns
199} // namespace ndn
200
201int
202main(int argc, char* argv[])
203{
204 ndn::ndns::log::init();
205 using std::string;
206 using namespace ndn;
207 using namespace ndn::ndns;
208
Shock Jiangae429122014-10-24 09:04:58 -0700209 Name zone;
210 int ttl = 4;
211 Name rrLabel;
212 string rrType = "TXT";
Yumin Xiaa484ba72016-11-10 20:40:12 -0800213 string contentTypeStr = "resp";
Shock Jiangae429122014-10-24 09:04:58 -0700214 Name certName;
Shock Jiang06cd2142014-11-23 17:36:02 -0800215 std::vector<string> contents;
Shock Jiangae429122014-10-24 09:04:58 -0700216 string contentFile;
Shock Jiang43e59b42014-12-02 15:05:02 -0800217 shared_ptr<Data> update;
218
Shock Jiangae429122014-10-24 09:04:58 -0700219 try {
220 namespace po = boost::program_options;
221 po::variables_map vm;
222
223 po::options_description generic("Generic Options");
224 generic.add_options()("help,h", "print help message");
225
226 po::options_description config("Configuration");
227 config.add_options()
Shock Jiangae429122014-10-24 09:04:58 -0700228 ("ttl,T", po::value<int>(&ttl), "TTL of query. default: 4 sec")
229 ("rrtype,t", po::value<string>(&rrType), "set request RR Type. default: TXT")
Yumin Xiaa484ba72016-11-10 20:40:12 -0800230 ("contentType,n", po::value<string>(&contentTypeStr), "Set the contentType of the resource record. "
231 "Potential values are [blob|link|nack|auth|resp]. Default: resp")
Shock Jiangae429122014-10-24 09:04:58 -0700232 ("cert,c", po::value<Name>(&certName), "set the name of certificate to sign the update")
Shock Jiang06cd2142014-11-23 17:36:02 -0800233 ("content,o", po::value<std::vector<string>>(&contents)->multitoken(),
234 "set the content of the RR")
Shock Jiangae429122014-10-24 09:04:58 -0700235 ("contentFile,f", po::value<string>(&contentFile), "set the path of file which contain"
Shock Jiang43e59b42014-12-02 15:05:02 -0800236 " Response packet in base64 format")
Shock Jiangae429122014-10-24 09:04:58 -0700237 ;
238
239 po::options_description hidden("Hidden Options");
240 hidden.add_options()
241 ("zone,z", po::value<Name>(&zone), "zone the record is delegated")
242 ("rrlabel,l", po::value<Name>(&rrLabel), "set request RR Label")
243 ;
244 po::positional_options_description postion;
245 postion.add("zone", 1);
246 postion.add("rrlabel", 1);
247
248 po::options_description cmdline_options;
249 cmdline_options.add(generic).add(config).add(hidden);
250
251 po::options_description config_file_options;
252 config_file_options.add(config).add(hidden);
253
Shock Jiang43e59b42014-12-02 15:05:02 -0800254 po::options_description visible("Usage: ndns-update zone rrLabel [-t rrType] [-T TTL] "
Yumin Xiaa484ba72016-11-10 20:40:12 -0800255 "[-n NdnsContentType] [-c cert] "
Shock Jiang43e59b42014-12-02 15:05:02 -0800256 "[-f contentFile]|[-o content]\n"
257 "Allowed options");
258
Shock Jiangae429122014-10-24 09:04:58 -0700259 visible.add(generic).add(config);
260
261 po::parsed_options parsed =
262 po::command_line_parser(argc, argv).options(cmdline_options).positional(postion).run();
263
264 po::store(parsed, vm);
265 po::notify(vm);
266
267 if (vm.count("help")) {
Shock Jiangae429122014-10-24 09:04:58 -0700268 std::cout << visible << std::endl;
269 return 0;
270 }
271
Shock Jiang06cd2142014-11-23 17:36:02 -0800272
Shock Jiangae429122014-10-24 09:04:58 -0700273 if (vm.count("content") && vm.count("contentFile")) {
Shock Jiang43e59b42014-12-02 15:05:02 -0800274 std::cerr << "both -o content and -f contentFile are set. Only one is allowed" << std::endl;
275 return 1;
Shock Jiangae429122014-10-24 09:04:58 -0700276 }
277
Shock Jiang43e59b42014-12-02 15:05:02 -0800278 if (!vm.count("contentFile")) {
279 NDNS_LOG_TRACE("content option is set. try to figure out the certificate");
280 if (!vm.count("zone") || !vm.count("rrlabel")) {
281 std::cerr << "-o option must be set together with -z zone and -r rrLabel" << std::endl;
282 return 1;
283 }
284
285 KeyChain keyChain;
286 if (certName.empty()) {
287 Name name = Name().append(zone).append(rrLabel);
288 // choosing the longest match of the identity who also have default certificate
289 for (size_t i = name.size() + 1; i > 0; --i) { // i >=0 will present warnning
290 Name tmp = name.getPrefix(i - 1);
291 if (keyChain.doesIdentityExist(tmp)) {
292 try {
293 certName = keyChain.getDefaultCertificateNameForIdentity(tmp);
294 break;
295 }
296 catch (std::exception&) {
297 // If it cannot get a default certificate from one identity,
298 // just ignore this one try next identity.
299 ;
300 }
301 }
302 } // for
303
304 if (certName.empty()) {
305 std::cerr << "cannot figure out the certificate automatically. "
306 << "please set it with -c CERT_NAEME" << std::endl;
307 return 1;
308 }
309 }
310 else {
311 if (!keyChain.doesCertificateExist(certName)) {
312 std::cerr << "certificate: " << certName << " does not exist" << std::endl;
313 return 1;
314 }
315 }
316
Yumin Xiaa484ba72016-11-10 20:40:12 -0800317 NdnsContentType contentType = toNdnsContentType(contentTypeStr);
Shock Jiang43e59b42014-12-02 15:05:02 -0800318
Yumin Xiaa484ba72016-11-10 20:40:12 -0800319 if (contentType == ndns::NDNS_UNKNOWN) {
320 std::cerr << "unknown NdnsContentType: " << contentTypeStr << std::endl;
Shock Jiang43e59b42014-12-02 15:05:02 -0800321 return 1;
322 }
323
324 Response re;
325 re.setZone(zone);
326 re.setRrLabel(rrLabel);
327 name::Component qType = (rrType == "ID-CERT" ?
328 ndns::label::NDNS_CERT_QUERY : ndns::label::NDNS_ITERATIVE_QUERY);
329
330 re.setQueryType(qType);
331 re.setRrType(name::Component(rrType));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800332 re.setContentType(contentType);
Shock Jiang43e59b42014-12-02 15:05:02 -0800333
334 for (const auto& content : contents) {
Junxiao Shi767f35c2016-07-23 01:54:42 +0000335 re.addRr(makeBinaryBlock(ndns::tlv::RrData, content.c_str(), content.size()));
Shock Jiang43e59b42014-12-02 15:05:02 -0800336
337 // re.addRr(content);
338 }
339
340 update = re.toData();
341 keyChain.sign(*update, certName);
342 }
343 else {
344 try {
345 update = ndn::io::load<ndn::Data>(contentFile);
346 NDNS_LOG_TRACE("load data " << update->getName() << " from content file: " << contentFile);
347 }
348 catch (const std::exception& e) {
349 std::cerr << "Error: load Data packet from file: " << contentFile
350 << ". Due to: " << e.what() << std::endl;
351 return 1;
352 }
353
354 try {
355 // must check the Data is a legal Response with right name
356 shared_ptr<Regex> regex = make_shared<Regex>("(<>*)<KEY>(<>+)<ID-CERT><>*");
357 shared_ptr<Regex> regex2 = make_shared<Regex>("(<>*)<NDNS>(<>+)");
358
359 Name zone2;
360 if (regex->match(update->getName())) {
361 zone2 = regex->expand("\\1");
362 }
363 else if (regex2->match(update->getName())) {
364 zone2 = regex2->expand("\\1");
365 }
366 else {
367 std::cerr << "The loaded Data packet cannot be stored in NDNS "
368 "since its does not have a proper name" << std::endl;
369 return 1;
370 }
371
372 if (vm.count("zone") && zone != zone2) {
373 std::cerr << "The loaded Data packet is supposed to be stored at zone: " << zone2
374 << " instead of zone: " << zone << std::endl;
375 return 1;
376 }
377 else {
378 zone = zone2;
379 }
380
381 Response re;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700382 re.fromData(zone, *update);
Shock Jiang43e59b42014-12-02 15:05:02 -0800383
384 if (vm.count("rrlabel") && rrLabel != re.getRrLabel()) {
385 std::cerr << "The loaded Data packet is supposed to have rrLabel: " << re.getRrLabel()
386 << " instead of label: " << rrLabel << std::endl;
387 return 1;
388 }
389
390 if (vm.count("rrtype") && name::Component(rrType) != re.getRrType()) {
391 std::cerr << "The loaded Data packet is supposed to have rrType: " << re.getRrType()
392 << " instead of label: " << rrType << std::endl;
393 return 1;
394 }
395 }
396 catch (const std::exception& e) {
397 std::cerr << "Error: the loaded Data packet cannot parse to a Response stored at zone: "
398 << zone << std::endl;
399 return 1;
400 }
401
Shock Jiangae429122014-10-24 09:04:58 -0700402 }
Shock Jiangae429122014-10-24 09:04:58 -0700403 }
404 catch (const std::exception& ex) {
405 std::cerr << "Parameter Error: " << ex.what() << std::endl;
Shock Jiang43e59b42014-12-02 15:05:02 -0800406 return 1;
Shock Jiangae429122014-10-24 09:04:58 -0700407 }
408
409 Face face;
Shock Jiang43e59b42014-12-02 15:05:02 -0800410 try {
Yumin Xia6343c5b2016-10-20 15:45:50 -0700411 NdnsUpdate updater(zone, update, face);
Shock Jiang43e59b42014-12-02 15:05:02 -0800412 updater.setInterestLifetime(ndn::time::seconds(ttl));
Shock Jiangae429122014-10-24 09:04:58 -0700413
Shock Jiang43e59b42014-12-02 15:05:02 -0800414 updater.start();
415 face.processEvents();
416 if (updater.hasError())
417 return 1;
418 else
Shock Jiang06cd2142014-11-23 17:36:02 -0800419 return 0;
Shock Jiangae429122014-10-24 09:04:58 -0700420 }
Shock Jiang43e59b42014-12-02 15:05:02 -0800421 catch (const ndn::ValidatorConfig::Error& e) {
422 std::cerr << "Fail to create the validator: " << e.what() << std::endl;
Shock Jiangae429122014-10-24 09:04:58 -0700423 return 1;
Shock Jiang43e59b42014-12-02 15:05:02 -0800424 }
425 catch (const std::exception& e) {
426 std::cerr << "Error: " << e.what() << std::endl;
427 return 1;
428 }
Shock Jiangae429122014-10-24 09:04:58 -0700429}