Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [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 "mgmt/management-tool.hpp" |
| 21 | #include "ndns-label.hpp" |
| 22 | #include "logger.hpp" |
| 23 | #include "util/util.hpp" |
| 24 | |
| 25 | #include <boost/program_options.hpp> |
| 26 | #include <boost/filesystem.hpp> |
| 27 | |
| 28 | #include <string> |
| 29 | |
| 30 | int |
| 31 | main(int argc, char* argv[]) |
| 32 | { |
| 33 | using std::string; |
| 34 | using namespace ndn; |
| 35 | using namespace ndns; |
| 36 | |
| 37 | ndn::ndns::log::init(); |
| 38 | int ttlInt = -1; |
| 39 | int versionInt = -1; |
| 40 | string zoneStr; |
| 41 | Name dsk; |
| 42 | string db; |
| 43 | string rrLabelStr; |
| 44 | string rrTypeStr; |
| 45 | string ndnsTypeStr; |
| 46 | std::vector<std::string> content; |
| 47 | try { |
| 48 | namespace po = boost::program_options; |
| 49 | po::variables_map vm; |
| 50 | |
| 51 | po::options_description options("Generic Options"); |
| 52 | options.add_options() |
| 53 | ("help,h", "print help message") |
| 54 | ("db,b", po::value<std::string>(&db), "Set the path of NDNS server database. " |
| 55 | "Default: " DEFAULT_DATABASE_PATH "/ndns.db") |
| 56 | ; |
| 57 | |
| 58 | po::options_description config("Record Options"); |
| 59 | config.add_options() |
| 60 | ("content-type,t", po::value<string>(&ndnsTypeStr), "Set the ndnsType of the resource record." |
| 61 | "Options: resp|nack|auth|raw (will try guess type by default)") |
| 62 | ("dsk,d", po::value<Name>(&dsk), "Set the name of DSK's certificate. " |
| 63 | "Default: use default DSK and its default certificate") |
| 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); |
| 130 | |
| 131 | if (ndnsTypeStr.empty()) { |
| 132 | if (rrTypeStr == "NS" || rrTypeStr == "TXT") |
| 133 | ndnsTypeStr = "resp"; |
| 134 | else if (rrTypeStr == "ID-CERT") { |
| 135 | ndnsTypeStr = "raw"; |
| 136 | } |
| 137 | else { |
| 138 | std::cerr << "Error: content type needs to be explicitly set using --content-type option" |
| 139 | << std::endl; |
| 140 | return 1; |
| 141 | } |
| 142 | } |
| 143 | NdnsType ndnsType = ndns::toNdnsType(ndnsTypeStr); |
| 144 | time::seconds ttl; |
| 145 | if (ttlInt == -1) |
| 146 | ttl = ndns::DEFAULT_CACHE_TTL; |
| 147 | else |
| 148 | ttl = time::seconds(ttlInt); |
| 149 | uint64_t version = static_cast<uint64_t>(versionInt); |
| 150 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 151 | ndn::KeyChain keyChain; |
| 152 | ndn::ndns::ManagementTool tool(db, keyChain); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 153 | tool.addRrSet(zoneName, label, type, ndnsType, version, content, dsk, ttl); |
| 154 | |
| 155 | /// @todo Report success or failure |
| 156 | // May be also show the inserted record in ndns-list-zone format |
| 157 | } |
| 158 | catch (const std::exception& ex) { |
| 159 | std::cerr << "Error: " << ex.what() << std::endl; |
| 160 | return 1; |
| 161 | } |
| 162 | } |