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