blob: 463022acb98d7bdda13ac01324f07816abd1083e [file] [log] [blame]
Shock Jiangcde28712014-10-19 21:17:20 -07001/* -*- 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 "daemon/name-server.hpp"
21#include "logger.hpp"
22#include "config.hpp"
23#include "daemon/config-file.hpp"
24#include "ndn-cxx/security/key-chain.hpp"
25#include <boost/program_options.hpp>
26
27namespace ndn {
28namespace ndns {
29
30NDNS_LOG_INIT("NdnsDaemon");
31
32/**
33 * @brief Name Server Daemon
34 * @note NdnsDaemon allows multiple name servers hosted by the same daemon, and they
35 * share same KeyChain, DbMgr, Validator and Face
36 */
37class NdnsDaemon : noncopyable
38{
39 DEFINE_ERROR(Error, std::runtime_error);
40
41public:
42 explicit
43 NdnsDaemon(const std::string& configFile, Face& face)
44 : m_configFile(configFile)
45 , m_face(face)
46 , m_validator(face)
47 {
48 try {
49 ConfigFile config;
50 NDNS_LOG_TRACE("configFile: " << configFile);
51
52 config.addSectionHandler("zones",
53 bind(&NdnsDaemon::processZonesSection, this, _1, _3));
54 config.addSectionHandler("hints",
55 bind(&NdnsDaemon::processHintsSection, this, _1, _3));
56
57 config.parse(configFile, false);
58
59 }
60 catch (boost::filesystem::filesystem_error& e) {
61 if (e.code() == boost::system::errc::permission_denied) {
62 NDNS_LOG_FATAL("Permissions denied for " << e.path1());
63 }
64 else {
65 NDNS_LOG_FATAL(e.what());
66 }
67 }
68 catch (const std::exception& e) {
69 NDNS_LOG_FATAL(e.what());
70 }
71 }
72
73 void
74 processHintsSection(const ndn::ndns::ConfigSection& section, const std::string& filename)
75 {
76 // hint is not supported yet
77 ;
78 }
79
80 void
81 processZonesSection(const ndn::ndns::ConfigSection& section, const std::string& filename)
82 {
83 using namespace boost::filesystem;
84 using namespace ndn::ndns;
85 using ndn::ndns::ConfigSection;
86
87 if (section.begin() == section.end()) {
88 throw Error("zones section is empty");
89 }
90
91 ConfigSection::const_assoc_iterator item = section.find("dbFile");
92 if (item != section.not_found()) {
93 m_dbFile = item->second.get_value<std::string>();
94 }
95 else {
96 m_dbFile = "/usr/local/var/ndns/ndns.db";
97 }
98 NDNS_LOG_TRACE("dbFile " << m_dbFile);
99
100 m_dbMgr = make_shared<DbMgr>(m_dbFile);
101
102 for (const auto& option : section) {
103 Name name;
104 Name cert;
105 if (option.first == "zone") {
106 try {
107 name = option.second.get<Name>("name"); // exception leads to exit
108 }
109 catch (const std::exception& e) {
110 NDNS_LOG_ERROR("Required `name' attribute missing in `zone' section");
111 throw Error("Required `name' attribute missing in `zone' section");
112 }
113 try {
114 cert = option.second.get<Name>("cert");
115 }
116 catch (std::exception&) {
117 ;
118 }
119
120 if (cert.empty()) {
121 cert = m_keyChain.getDefaultCertificateNameForIdentity(name);
122 }
123 else {
124 if (!m_keyChain.doesCertificateExist(cert)) {
125 throw Error("Certificate `" + cert.toUri() + "` does not exist in the KeyChain");
126 }
127 }
128 NDNS_LOG_TRACE("name = " << name << " cert = " << cert);
129 m_servers.push_back(make_shared<NameServer>(name, cert, m_face, *m_dbMgr,
130 m_keyChain, m_validator));
131 }
132 } // for
133 }
134
135private:
136 std::string m_configFile;
137 Face& m_face;
138 Validator m_validator;
139 std::string m_dbFile;
140 shared_ptr<DbMgr> m_dbMgr;
141 std::vector<shared_ptr<NameServer> > m_servers;
142 KeyChain m_keyChain;
143};
144
145} // namespace ndns
146} // namespace ndn
147
148int
149main(int argc, char* argv[])
150{
151 using std::string;
152 using ndn::ndns::ConfigFile;
153 using namespace ndn::ndns;
154
155 ndn::ndns::log::init();
156 string configFile = DEFAULT_CONFIG_PATH "/" "ndns.conf";
157
158 try {
159 namespace po = boost::program_options;
160 po::variables_map vm;
161
162 po::options_description generic("Generic Options");
163 generic.add_options()("help,h", "print help message");
164
165 po::options_description config("Configuration");
166 config.add_options()
167 ("config,c", po::value<string>(&configFile), "set the path of configuration file")
168 ;
169
170 po::options_description cmdline_options;
171 cmdline_options.add(generic).add(config);
172
173 po::parsed_options parsed =
174 po::command_line_parser(argc, argv).options(cmdline_options).run();
175
176 po::store(parsed, vm);
177 po::notify(vm);
178
179 if (vm.count("help")) {
180 std::cout << "Usage:\n"
181 << " ndns-daemon [-c configFile]\n"
182 << std::endl;
183 std::cout << generic << config << std::endl;
184 return 0;
185 }
186 }
187 catch (const std::exception& ex) {
188 std::cerr << "Parameter Error: " << ex.what() << std::endl;
189
190 return 1;
191 }
192 catch (...) {
193 std::cerr << "Parameter Unknown error" << std::endl;
194 return 1;
195 }
196
197 try {
198 ndn::Face face;
199 NdnsDaemon nsd(configFile, face);
200
201 boost::asio::signal_set signalSet(face.getIoService(), SIGINT, SIGTERM);
202
203 signalSet.async_wait([&face] (const boost::system::error_code&, const int) {
204 face.getIoService().stop();
205 });
206
207 face.processEvents();
208 }
209 catch (std::exception& e) {
210 NDNS_LOG_FATAL("ERROR: " << e.what());
211 return 2;
212 }
213
214 return 0;
215}