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 <boost/program_options.hpp> |
| 24 | #include <boost/filesystem.hpp> |
| 25 | #include <string> |
| 26 | |
Jiewen Tan | 74d745c | 2015-03-20 01:40:41 -0700 | [diff] [blame] | 27 | #include <ndn-cxx/util/io.hpp> |
| 28 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 29 | |
| 30 | // @todo combine this command with ndns-add-rr |
| 31 | int |
| 32 | main(int argc, char* argv[]) |
| 33 | { |
| 34 | using std::string; |
| 35 | using namespace ndn; |
| 36 | using namespace ndns; |
| 37 | |
| 38 | ndn::ndns::log::init(); |
| 39 | int ttlInt = -1; |
| 40 | string zoneStr; |
| 41 | string dskStr; |
| 42 | string db; |
| 43 | string file = "-"; |
Jiewen Tan | 74d745c | 2015-03-20 01:40:41 -0700 | [diff] [blame] | 44 | string encoding = "base64"; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 45 | try { |
| 46 | namespace po = boost::program_options; |
| 47 | po::variables_map vm; |
| 48 | |
| 49 | po::options_description options("Generic Options"); |
| 50 | options.add_options() |
| 51 | ("help,h", "print help message") |
| 52 | ("db,b", po::value<std::string>(&db), "Set the path of NDNS server database. " |
| 53 | "Default: " DEFAULT_DATABASE_PATH "/ndns.db") |
| 54 | ; |
| 55 | |
| 56 | po::options_description config("Record Options"); |
| 57 | config.add_options() |
| 58 | ("file,f", po::value<string>(&file), "Path to the data. Default is stdin(-)") |
| 59 | ("dsk,d", po::value<std::string>(&dskStr), "Set the name of DSK's certificate. " |
| 60 | "Default: use default DSK and its default certificate") |
| 61 | ("ttl,a", po::value<int>(&ttlInt), "Set ttl of the rrset. Default: 3600 seconds") |
Jiewen Tan | 74d745c | 2015-03-20 01:40:41 -0700 | [diff] [blame] | 62 | ("encoding,e", po::value<string>(&encoding), |
| 63 | "Set encoding format of input. Default: base64") |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 64 | ; |
| 65 | |
| 66 | options.add(config); |
| 67 | |
| 68 | po::options_description hidden("Hidden Options"); |
| 69 | hidden.add_options() |
| 70 | ("zone", po::value<string>(&zoneStr), "host zone name") |
| 71 | ; |
| 72 | |
| 73 | po::positional_options_description postion; |
| 74 | postion.add("zone", 1); |
| 75 | postion.add("file", 1); |
| 76 | |
| 77 | po::options_description cmdlineOptions; |
| 78 | cmdlineOptions.add(options).add(hidden); |
| 79 | |
| 80 | // po::options_description config_file_options; |
| 81 | // config_file_options.add(config).add(hidden); |
| 82 | |
| 83 | po::parsed_options parsed = |
| 84 | po::command_line_parser(argc, argv).options(cmdlineOptions).positional(postion).run(); |
| 85 | |
| 86 | po::store(parsed, vm); |
| 87 | po::notify(vm); |
| 88 | |
| 89 | if (vm.count("help")) { |
Jiewen Tan | 74d745c | 2015-03-20 01:40:41 -0700 | [diff] [blame] | 90 | std::cout << "Usage: ndns-add-rr-from-file [-b db] zone [-f file] [-d dskCert] [-a ttl] " |
| 91 | "[-e raw|base64|hex] [file]" << std::endl |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 92 | << std::endl; |
| 93 | std::cout << options << std::endl; |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | if (vm.count("zone") == 0) { |
| 98 | std::cerr << "Error: zone must be specified" << std::endl; |
| 99 | return 1; |
| 100 | } |
| 101 | } |
| 102 | catch (const std::exception& ex) { |
| 103 | std::cerr << "Parameter Error: " << ex.what() << std::endl; |
| 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | try { |
| 108 | Name zoneName(zoneStr); |
| 109 | Name dskName(dskStr); |
| 110 | time::seconds ttl; |
| 111 | if (ttlInt == -1) |
| 112 | ttl = ndns::DEFAULT_CACHE_TTL; |
| 113 | else |
| 114 | ttl = time::seconds(ttlInt); |
| 115 | |
Jiewen Tan | 74d745c | 2015-03-20 01:40:41 -0700 | [diff] [blame] | 116 | ndn::io::IoEncoding ioEncoding; |
| 117 | if (encoding == "raw") { |
| 118 | ioEncoding = ndn::io::NO_ENCODING; |
| 119 | } |
| 120 | else if (encoding == "hex") { |
| 121 | ioEncoding = ndn::io::HEX; |
| 122 | } |
| 123 | else if (encoding == "base64") { |
Junxiao Shi | dc2df7c | 2016-08-31 12:53:14 +0000 | [diff] [blame] | 124 | ioEncoding = ndn::io::BASE64; |
Jiewen Tan | 74d745c | 2015-03-20 01:40:41 -0700 | [diff] [blame] | 125 | } |
| 126 | else { |
| 127 | std::cerr << "Error: not supported encoding format '" << encoding |
| 128 | << "' (valid options are: raw, hex, and base64)" << std::endl; |
| 129 | return 1; |
| 130 | } |
| 131 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 132 | ndn::KeyChain keyChain; |
| 133 | ndn::ndns::ManagementTool tool(db, keyChain); |
Jiewen Tan | 74d745c | 2015-03-20 01:40:41 -0700 | [diff] [blame] | 134 | tool.addRrSet(zoneName, file, ttl, dskName, ioEncoding); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 135 | } |
| 136 | catch (const std::exception& ex) { |
| 137 | std::cerr << "Error: " << ex.what() << std::endl; |
| 138 | return 1; |
| 139 | } |
| 140 | } |