Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California. |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -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 "mgmt/management-tool.hpp" |
| 21 | #include "ndns-label.hpp" |
| 22 | #include "logger.hpp" |
| 23 | #include "util/util.hpp" |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 24 | #include "daemon/rrset-factory.hpp" |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 25 | |
| 26 | #include <boost/program_options.hpp> |
| 27 | #include <boost/filesystem.hpp> |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 28 | #include <boost/algorithm/string.hpp> |
| 29 | #include <boost/lexical_cast.hpp> |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 30 | |
| 31 | #include <string> |
| 32 | |
| 33 | int |
| 34 | main(int argc, char* argv[]) |
| 35 | { |
| 36 | using std::string; |
| 37 | using namespace ndn; |
| 38 | using namespace ndns; |
| 39 | |
| 40 | ndn::ndns::log::init(); |
| 41 | int ttlInt = -1; |
| 42 | int versionInt = -1; |
| 43 | string zoneStr; |
| 44 | Name dsk; |
| 45 | string db; |
| 46 | string rrLabelStr; |
| 47 | string rrTypeStr; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 48 | std::vector<std::string> content; |
| 49 | try { |
| 50 | namespace po = boost::program_options; |
| 51 | po::variables_map vm; |
| 52 | |
| 53 | po::options_description options("Generic Options"); |
| 54 | options.add_options() |
| 55 | ("help,h", "print help message") |
| 56 | ("db,b", po::value<std::string>(&db), "Set the path of NDNS server database. " |
| 57 | "Default: " DEFAULT_DATABASE_PATH "/ndns.db") |
| 58 | ; |
| 59 | |
| 60 | po::options_description config("Record Options"); |
| 61 | config.add_options() |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 62 | ("dsk,d", po::value<Name>(&dsk), "Set the name of DSK's certificate. " |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 63 | "Default: use default DSK and its default certificate") |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 64 | ("content,c", po::value<std::vector<std::string>>(&content), |
| 65 | "Set the content of resource record. Default: empty string") |
| 66 | ("ttl,a", po::value<int>(&ttlInt), "Set ttl of the rrset. Default: 3600 seconds") |
| 67 | ("version,v", po::value<int>(&ttlInt), "Set version of the rrset. Default: Unix Timestamp") |
| 68 | ; |
| 69 | |
| 70 | // add "Record Options" as a separate section |
| 71 | options.add(config); |
| 72 | |
| 73 | po::options_description hidden("Hidden Options"); |
| 74 | hidden.add_options() |
| 75 | ("zone", po::value<string>(&zoneStr), "host zone name") |
| 76 | ("label", po::value<string>(&rrLabelStr), "label of resource record.") |
| 77 | ("type", po::value<string>(&rrTypeStr), "Set the type of resource record.") |
| 78 | ; |
| 79 | |
| 80 | po::positional_options_description positional; |
| 81 | positional.add("zone", 1); |
| 82 | positional.add("label", 1); |
| 83 | positional.add("type", 1); |
| 84 | positional.add("content", -1); |
| 85 | |
| 86 | po::options_description cmdlineOptions; |
| 87 | cmdlineOptions.add(options).add(hidden); |
| 88 | |
| 89 | // po::options_description configFileOptions; |
| 90 | // configFileOptions.add(config).add(hidden); |
| 91 | |
| 92 | po::parsed_options parsed = |
| 93 | po::command_line_parser(argc, argv).options(cmdlineOptions).positional(positional).run(); |
| 94 | |
| 95 | po::store(parsed, vm); |
| 96 | po::notify(vm); |
| 97 | |
| 98 | |
| 99 | if (vm.count("help")) { |
| 100 | std::cout << "Usage: ndns-add-rr [options] zone label type [content ...]" << std::endl |
| 101 | << std::endl; |
| 102 | std::cout << options << std::endl; |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | if (vm.count("zone") == 0) { |
| 107 | std::cerr << "Error: zone, label, and type must be specified" << std::endl; |
| 108 | return 1; |
| 109 | } |
| 110 | |
| 111 | if (vm.count("label") == 0) { |
| 112 | std::cerr << "Error: label and type must be specified" << std::endl; |
| 113 | return 1; |
| 114 | } |
| 115 | |
| 116 | if (vm.count("type") == 0) { |
| 117 | std::cerr << "Error: type must be specified" << std::endl; |
| 118 | return 1; |
| 119 | } |
| 120 | } |
| 121 | catch (const std::exception& ex) { |
| 122 | std::cerr << "Parameter Error: " << ex.what() << std::endl; |
| 123 | return 1; |
| 124 | } |
| 125 | |
| 126 | try { |
| 127 | Name zoneName(zoneStr); |
| 128 | Name label(rrLabelStr); |
| 129 | name::Component type(rrTypeStr); |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 130 | ndn::KeyChain keyChain; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 131 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 132 | time::seconds ttl; |
| 133 | if (ttlInt == -1) |
| 134 | ttl = ndns::DEFAULT_CACHE_TTL; |
| 135 | else |
| 136 | ttl = time::seconds(ttlInt); |
| 137 | uint64_t version = static_cast<uint64_t>(versionInt); |
| 138 | |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 139 | // todo: reduce copy |
| 140 | RrsetFactory rrsetFactory(db, zoneName, keyChain, dsk); |
| 141 | rrsetFactory.checkZoneKey(); |
| 142 | Rrset rrset; |
| 143 | |
| 144 | if (type == label::NS_RR_TYPE) { |
| 145 | ndn::Link::DelegationSet delegations; |
| 146 | for (const auto& i : content) { |
| 147 | std::vector<string> data; |
| 148 | boost::split(data, i, boost::is_any_of(",")); |
| 149 | uint32_t priority = boost::lexical_cast<uint32_t>(data[0]); |
| 150 | // assert that data has two number. |
| 151 | delegations.insert(std::make_pair(priority, data[1])); |
| 152 | } |
| 153 | |
| 154 | rrset = rrsetFactory.generateNsRrset(label, type, |
| 155 | version, ttl, delegations); |
| 156 | } else if (type == label::TXT_RR_TYPE) { |
| 157 | rrset = rrsetFactory.generateTxtRrset(label, type, |
| 158 | version, ttl, content); |
| 159 | } |
| 160 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 161 | ndn::ndns::ManagementTool tool(db, keyChain); |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 162 | tool.addRrset(rrset); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 163 | |
| 164 | /// @todo Report success or failure |
| 165 | // May be also show the inserted record in ndns-list-zone format |
| 166 | } |
| 167 | catch (const std::exception& ex) { |
| 168 | std::cerr << "Error: " << ex.what() << std::endl; |
| 169 | return 1; |
| 170 | } |
| 171 | } |