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