Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 1 | /* -*- 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 "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 | |
| 43 | namespace ndn { |
| 44 | namespace ndns { |
| 45 | NDNS_LOG_INIT("NdnsUpdate"); |
| 46 | |
| 47 | class NdnsUpdate : noncopyable |
| 48 | { |
| 49 | public: |
Shock Jiang | 43e59b4 | 2014-12-02 15:05:02 -0800 | [diff] [blame^] | 50 | NdnsUpdate(const Name& hint, const Name& zone, const shared_ptr<Data>& update, Face& face) |
| 51 | : m_hint(hint) |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 52 | , m_zone(zone) |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 53 | , m_interestLifetime(DEFAULT_INTEREST_LIFETIME) |
| 54 | , m_face(face) |
| 55 | , m_validator(face) |
Shock Jiang | 43e59b4 | 2014-12-02 15:05:02 -0800 | [diff] [blame^] | 56 | , m_update(update) |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 57 | , m_hasError(false) |
| 58 | { |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void |
Shock Jiang | 43e59b4 | 2014-12-02 15:05:02 -0800 | [diff] [blame^] | 62 | start() |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 63 | { |
Shock Jiang | 43e59b4 | 2014-12-02 15:05:02 -0800 | [diff] [blame^] | 64 | NDNS_LOG_INFO(" ================ " |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 65 | << "start to update RR at Zone = " << this->m_zone |
Shock Jiang | 43e59b4 | 2014-12-02 15:05:02 -0800 | [diff] [blame^] | 66 | << " new RR is: " << m_update->getName() |
| 67 | <<" =================== "); |
| 68 | |
| 69 | NDNS_LOG_INFO("new RR is signed by: " |
| 70 | << m_update->getSignature().getKeyLocator().getName()); |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 71 | |
| 72 | Interest interest = this->makeUpdateInterest(); |
Shock Jiang | 43e59b4 | 2014-12-02 15:05:02 -0800 | [diff] [blame^] | 73 | NDNS_LOG_TRACE("[* <- *] send Update: " << m_update->getName().toUri()); |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 74 | m_face.expressInterest(interest, |
| 75 | bind(&NdnsUpdate::onData, this, _1, _2), |
| 76 | bind(&NdnsUpdate::onTimeout, this, _1) //dynamic binding |
| 77 | ); |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void |
| 81 | stop() |
| 82 | { |
| 83 | m_face.getIoService().stop(); |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | void |
| 88 | onData(const Interest& interest, const Data& data) |
| 89 | { |
| 90 | NDNS_LOG_INFO("get response of Update"); |
| 91 | int ret = -1; |
| 92 | std::string msg; |
| 93 | std::tie(ret, msg) = this->parseResponse(data); |
| 94 | NDNS_LOG_INFO("Return Code: " << ret << ", and Update " |
| 95 | << (ret == UPDATE_OK ? "succeeds" : "fails")); |
| 96 | if (ret != UPDATE_OK) |
| 97 | m_hasError = true; |
| 98 | |
| 99 | if (!msg.empty()) { |
| 100 | NDNS_LOG_INFO("Return Msg: " << msg); |
| 101 | } |
| 102 | |
| 103 | NDNS_LOG_INFO("to verify the response"); |
| 104 | m_validator.validate(data, |
| 105 | bind(&NdnsUpdate::onDataValidated, this, _1), |
| 106 | bind(&NdnsUpdate::onDataValidationFailed, this, _1, _2) |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | std::tuple<int, std::string> |
| 111 | parseResponse(const Data& data) |
| 112 | { |
| 113 | int ret = -1; |
| 114 | std::string msg; |
| 115 | Block blk = data.getContent(); |
| 116 | blk.parse(); |
| 117 | Block block = blk.blockFromValue(); |
| 118 | block.parse(); |
| 119 | Block::element_const_iterator val = block.elements_begin(); |
| 120 | for (; val != block.elements_end(); ++val) { |
| 121 | if (val->type() == ndns::tlv::UpdateReturnCode) { // the first must be return code |
| 122 | ret = readNonNegativeInteger(*val); |
| 123 | } |
| 124 | else if (val->type() == ndns::tlv::UpdateReturnMsg) { |
| 125 | msg = std::string(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return std::make_tuple(ret, msg); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @brief construct a query (interest) which contains the update information |
| 134 | */ |
| 135 | Interest |
| 136 | makeUpdateInterest() |
| 137 | { |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 138 | Query q(m_hint, m_zone, label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | 43e59b4 | 2014-12-02 15:05:02 -0800 | [diff] [blame^] | 139 | q.setRrLabel(Name().append(m_update->wireEncode())); |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 140 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 141 | q.setInterestLifetime(m_interestLifetime); |
| 142 | |
| 143 | return q.toInterest(); |
| 144 | } |
| 145 | |
| 146 | private: |
| 147 | void |
| 148 | onTimeout(const ndn::Interest& interest) |
| 149 | { |
| 150 | NDNS_LOG_TRACE("Update timeouts"); |
| 151 | m_hasError = true; |
| 152 | this->stop(); |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | onDataValidated(const shared_ptr<const Data>& data) |
| 157 | { |
| 158 | NDNS_LOG_INFO("data pass verification"); |
| 159 | this->stop(); |
| 160 | } |
| 161 | |
| 162 | void |
| 163 | onDataValidationFailed(const shared_ptr<const Data>& data, const std::string& str) |
| 164 | { |
| 165 | NDNS_LOG_INFO("data does not pass verification"); |
| 166 | m_hasError = true; |
| 167 | this->stop(); |
| 168 | } |
| 169 | |
| 170 | public: |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 171 | |
| 172 | void |
| 173 | setInterestLifetime(const time::milliseconds& interestLifetime) |
| 174 | { |
| 175 | m_interestLifetime = interestLifetime; |
| 176 | } |
| 177 | |
| 178 | const bool |
| 179 | hasError() const |
| 180 | { |
| 181 | return m_hasError; |
| 182 | } |
| 183 | |
| 184 | private: |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 185 | Name m_hint; |
| 186 | Name m_zone; |
Shock Jiang | 43e59b4 | 2014-12-02 15:05:02 -0800 | [diff] [blame^] | 187 | |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 188 | time::milliseconds m_interestLifetime; |
| 189 | |
| 190 | Face& m_face; |
| 191 | Validator m_validator; |
| 192 | KeyChain m_keyChain; |
| 193 | |
Shock Jiang | 43e59b4 | 2014-12-02 15:05:02 -0800 | [diff] [blame^] | 194 | shared_ptr<Data> m_update; |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 195 | bool m_hasError; |
| 196 | }; |
| 197 | |
| 198 | } // namespace ndns |
| 199 | } // namespace ndn |
| 200 | |
| 201 | int |
| 202 | main(int argc, char* argv[]) |
| 203 | { |
| 204 | ndn::ndns::log::init(); |
| 205 | using std::string; |
| 206 | using namespace ndn; |
| 207 | using namespace ndn::ndns; |
| 208 | |
| 209 | Name hint; |
| 210 | Name zone; |
| 211 | int ttl = 4; |
| 212 | Name rrLabel; |
| 213 | string rrType = "TXT"; |
| 214 | string ndnsTypeStr = "resp"; |
| 215 | Name certName; |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 216 | std::vector<string> contents; |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 217 | string contentFile; |
Shock Jiang | 43e59b4 | 2014-12-02 15:05:02 -0800 | [diff] [blame^] | 218 | shared_ptr<Data> update; |
| 219 | |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 220 | try { |
| 221 | namespace po = boost::program_options; |
| 222 | po::variables_map vm; |
| 223 | |
| 224 | po::options_description generic("Generic Options"); |
| 225 | generic.add_options()("help,h", "print help message"); |
| 226 | |
| 227 | po::options_description config("Configuration"); |
| 228 | config.add_options() |
| 229 | ("hint,H", po::value<Name>(&hint), "forwarding hint") |
| 230 | ("ttl,T", po::value<int>(&ttl), "TTL of query. default: 4 sec") |
| 231 | ("rrtype,t", po::value<string>(&rrType), "set request RR Type. default: TXT") |
| 232 | ("ndnsType,n", po::value<string>(&ndnsTypeStr), "Set the ndnsType of the resource record. " |
| 233 | "Potential values are [resp|nack|auth|raw]. Default: resp") |
| 234 | ("cert,c", po::value<Name>(&certName), "set the name of certificate to sign the update") |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 235 | ("content,o", po::value<std::vector<string>>(&contents)->multitoken(), |
| 236 | "set the content of the RR") |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 237 | ("contentFile,f", po::value<string>(&contentFile), "set the path of file which contain" |
Shock Jiang | 43e59b4 | 2014-12-02 15:05:02 -0800 | [diff] [blame^] | 238 | " Response packet in base64 format") |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 239 | ; |
| 240 | |
| 241 | po::options_description hidden("Hidden Options"); |
| 242 | hidden.add_options() |
| 243 | ("zone,z", po::value<Name>(&zone), "zone the record is delegated") |
| 244 | ("rrlabel,l", po::value<Name>(&rrLabel), "set request RR Label") |
| 245 | ; |
| 246 | po::positional_options_description postion; |
| 247 | postion.add("zone", 1); |
| 248 | postion.add("rrlabel", 1); |
| 249 | |
| 250 | po::options_description cmdline_options; |
| 251 | cmdline_options.add(generic).add(config).add(hidden); |
| 252 | |
| 253 | po::options_description config_file_options; |
| 254 | config_file_options.add(config).add(hidden); |
| 255 | |
Shock Jiang | 43e59b4 | 2014-12-02 15:05:02 -0800 | [diff] [blame^] | 256 | po::options_description visible("Usage: ndns-update zone rrLabel [-t rrType] [-T TTL] " |
| 257 | "[-H hint] [-n NdnsType] [-c cert] " |
| 258 | "[-f contentFile]|[-o content]\n" |
| 259 | "Allowed options"); |
| 260 | |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 261 | visible.add(generic).add(config); |
| 262 | |
| 263 | po::parsed_options parsed = |
| 264 | po::command_line_parser(argc, argv).options(cmdline_options).positional(postion).run(); |
| 265 | |
| 266 | po::store(parsed, vm); |
| 267 | po::notify(vm); |
| 268 | |
| 269 | if (vm.count("help")) { |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 270 | std::cout << visible << std::endl; |
| 271 | return 0; |
| 272 | } |
| 273 | |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 274 | |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 275 | if (vm.count("content") && vm.count("contentFile")) { |
Shock Jiang | 43e59b4 | 2014-12-02 15:05:02 -0800 | [diff] [blame^] | 276 | std::cerr << "both -o content and -f contentFile are set. Only one is allowed" << std::endl; |
| 277 | return 1; |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Shock Jiang | 43e59b4 | 2014-12-02 15:05:02 -0800 | [diff] [blame^] | 280 | if (!vm.count("contentFile")) { |
| 281 | NDNS_LOG_TRACE("content option is set. try to figure out the certificate"); |
| 282 | if (!vm.count("zone") || !vm.count("rrlabel")) { |
| 283 | std::cerr << "-o option must be set together with -z zone and -r rrLabel" << std::endl; |
| 284 | return 1; |
| 285 | } |
| 286 | |
| 287 | KeyChain keyChain; |
| 288 | if (certName.empty()) { |
| 289 | Name name = Name().append(zone).append(rrLabel); |
| 290 | // choosing the longest match of the identity who also have default certificate |
| 291 | for (size_t i = name.size() + 1; i > 0; --i) { // i >=0 will present warnning |
| 292 | Name tmp = name.getPrefix(i - 1); |
| 293 | if (keyChain.doesIdentityExist(tmp)) { |
| 294 | try { |
| 295 | certName = keyChain.getDefaultCertificateNameForIdentity(tmp); |
| 296 | break; |
| 297 | } |
| 298 | catch (std::exception&) { |
| 299 | // If it cannot get a default certificate from one identity, |
| 300 | // just ignore this one try next identity. |
| 301 | ; |
| 302 | } |
| 303 | } |
| 304 | } // for |
| 305 | |
| 306 | if (certName.empty()) { |
| 307 | std::cerr << "cannot figure out the certificate automatically. " |
| 308 | << "please set it with -c CERT_NAEME" << std::endl; |
| 309 | return 1; |
| 310 | } |
| 311 | } |
| 312 | else { |
| 313 | if (!keyChain.doesCertificateExist(certName)) { |
| 314 | std::cerr << "certificate: " << certName << " does not exist" << std::endl; |
| 315 | return 1; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | NdnsType ndnsType = toNdnsType(ndnsTypeStr); |
| 320 | |
| 321 | if (ndnsType == ndns::NDNS_UNKNOWN) { |
| 322 | std::cerr << "unknown NdnsType: " << ndnsTypeStr << std::endl; |
| 323 | return 1; |
| 324 | } |
| 325 | |
| 326 | Response re; |
| 327 | re.setZone(zone); |
| 328 | re.setRrLabel(rrLabel); |
| 329 | name::Component qType = (rrType == "ID-CERT" ? |
| 330 | ndns::label::NDNS_CERT_QUERY : ndns::label::NDNS_ITERATIVE_QUERY); |
| 331 | |
| 332 | re.setQueryType(qType); |
| 333 | re.setRrType(name::Component(rrType)); |
| 334 | re.setNdnsType(ndnsType); |
| 335 | |
| 336 | for (const auto& content : contents) { |
| 337 | re.addRr(ndn::dataBlock(ndn::ndns::tlv::RrData, content.c_str(), content.size())); |
| 338 | |
| 339 | // re.addRr(content); |
| 340 | } |
| 341 | |
| 342 | update = re.toData(); |
| 343 | keyChain.sign(*update, certName); |
| 344 | } |
| 345 | else { |
| 346 | try { |
| 347 | update = ndn::io::load<ndn::Data>(contentFile); |
| 348 | NDNS_LOG_TRACE("load data " << update->getName() << " from content file: " << contentFile); |
| 349 | } |
| 350 | catch (const std::exception& e) { |
| 351 | std::cerr << "Error: load Data packet from file: " << contentFile |
| 352 | << ". Due to: " << e.what() << std::endl; |
| 353 | return 1; |
| 354 | } |
| 355 | |
| 356 | try { |
| 357 | // must check the Data is a legal Response with right name |
| 358 | shared_ptr<Regex> regex = make_shared<Regex>("(<>*)<KEY>(<>+)<ID-CERT><>*"); |
| 359 | shared_ptr<Regex> regex2 = make_shared<Regex>("(<>*)<NDNS>(<>+)"); |
| 360 | |
| 361 | Name zone2; |
| 362 | if (regex->match(update->getName())) { |
| 363 | zone2 = regex->expand("\\1"); |
| 364 | } |
| 365 | else if (regex2->match(update->getName())) { |
| 366 | zone2 = regex2->expand("\\1"); |
| 367 | } |
| 368 | else { |
| 369 | std::cerr << "The loaded Data packet cannot be stored in NDNS " |
| 370 | "since its does not have a proper name" << std::endl; |
| 371 | return 1; |
| 372 | } |
| 373 | |
| 374 | if (vm.count("zone") && zone != zone2) { |
| 375 | std::cerr << "The loaded Data packet is supposed to be stored at zone: " << zone2 |
| 376 | << " instead of zone: " << zone << std::endl; |
| 377 | return 1; |
| 378 | } |
| 379 | else { |
| 380 | zone = zone2; |
| 381 | } |
| 382 | |
| 383 | Response re; |
| 384 | re.fromData(hint, zone, *update); |
| 385 | |
| 386 | if (vm.count("rrlabel") && rrLabel != re.getRrLabel()) { |
| 387 | std::cerr << "The loaded Data packet is supposed to have rrLabel: " << re.getRrLabel() |
| 388 | << " instead of label: " << rrLabel << std::endl; |
| 389 | return 1; |
| 390 | } |
| 391 | |
| 392 | if (vm.count("rrtype") && name::Component(rrType) != re.getRrType()) { |
| 393 | std::cerr << "The loaded Data packet is supposed to have rrType: " << re.getRrType() |
| 394 | << " instead of label: " << rrType << std::endl; |
| 395 | return 1; |
| 396 | } |
| 397 | } |
| 398 | catch (const std::exception& e) { |
| 399 | std::cerr << "Error: the loaded Data packet cannot parse to a Response stored at zone: " |
| 400 | << zone << std::endl; |
| 401 | return 1; |
| 402 | } |
| 403 | |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 404 | } |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 405 | } |
| 406 | catch (const std::exception& ex) { |
| 407 | std::cerr << "Parameter Error: " << ex.what() << std::endl; |
Shock Jiang | 43e59b4 | 2014-12-02 15:05:02 -0800 | [diff] [blame^] | 408 | return 1; |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | Face face; |
Shock Jiang | 43e59b4 | 2014-12-02 15:05:02 -0800 | [diff] [blame^] | 412 | try { |
| 413 | NdnsUpdate updater(hint, zone, update, face); |
| 414 | updater.setInterestLifetime(ndn::time::seconds(ttl)); |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 415 | |
Shock Jiang | 43e59b4 | 2014-12-02 15:05:02 -0800 | [diff] [blame^] | 416 | updater.start(); |
| 417 | face.processEvents(); |
| 418 | if (updater.hasError()) |
| 419 | return 1; |
| 420 | else |
Shock Jiang | 06cd214 | 2014-11-23 17:36:02 -0800 | [diff] [blame] | 421 | return 0; |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 422 | } |
Shock Jiang | 43e59b4 | 2014-12-02 15:05:02 -0800 | [diff] [blame^] | 423 | catch (const ndn::ValidatorConfig::Error& e) { |
| 424 | std::cerr << "Fail to create the validator: " << e.what() << std::endl; |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 425 | return 1; |
Shock Jiang | 43e59b4 | 2014-12-02 15:05:02 -0800 | [diff] [blame^] | 426 | } |
| 427 | catch (const std::exception& e) { |
| 428 | std::cerr << "Error: " << e.what() << std::endl; |
| 429 | return 1; |
| 430 | } |
Shock Jiang | ae42912 | 2014-10-24 09:04:58 -0700 | [diff] [blame] | 431 | } |