blob: d7a60d73d545d693304ce4a02fab3d3bf88a4019 [file] [log] [blame]
Alexander Afanasyev31c781e2015-02-09 17:39:59 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "nfd.hpp"
27
28#include "core/logger-factory.hpp"
29#include "core/privilege-helper.hpp"
30#include "fw/forwarder.hpp"
31#include "face/null-face.hpp"
32#include "mgmt/internal-face.hpp"
33#include "mgmt/fib-manager.hpp"
34#include "mgmt/face-manager.hpp"
35#include "mgmt/strategy-choice-manager.hpp"
36#include "mgmt/status-server.hpp"
37#include "core/config-file.hpp"
38#include "mgmt/general-config-section.hpp"
39#include "mgmt/tables-config-section.hpp"
40
41namespace nfd {
42
43static const std::string INTERNAL_CONFIG = "internal://nfd.conf";
44
45Nfd::Nfd(const std::string& configFile, ndn::KeyChain& keyChain)
46 : m_configFile(configFile)
47 , m_keyChain(keyChain)
48{
49}
50
51Nfd::Nfd(const ConfigSection& config, ndn::KeyChain& keyChain)
52 : m_configSection(config)
53 , m_keyChain(keyChain)
54{
55}
56
57void
58Nfd::initialize()
59{
60 initializeLogging();
61
62 m_forwarder = make_shared<Forwarder>();
63
64 initializeManagement();
65
66 m_forwarder->getFaceTable().addReserved(make_shared<NullFace>(), FACEID_NULL);
67 m_forwarder->getFaceTable().addReserved(make_shared<NullFace>(FaceUri("contentstore://")),
68 FACEID_CONTENT_STORE);
69
70#ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
71 PrivilegeHelper::drop();
72#endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
73}
74
75void
76Nfd::initializeLogging()
77{
78 ConfigFile config(&ConfigFile::ignoreUnknownSection);
79 LoggerFactory::getInstance().setConfigFile(config);
80
81 if (!m_configFile.empty()) {
82 config.parse(m_configFile, true);
83 config.parse(m_configFile, false);
84 }
85 else {
86 config.parse(m_configSection, true, INTERNAL_CONFIG);
87 config.parse(m_configSection, false, INTERNAL_CONFIG);
88 }
89}
90
91
92static inline void
93ignoreRibAndLogSections(const std::string& filename, const std::string& sectionName,
94 const ConfigSection& section, bool isDryRun)
95{
96 // Ignore "log" and "rib" sections, but raise an error if we're missing a
97 // handler for an NFD section.
98 if (sectionName == "rib" || sectionName == "log") {
99 // do nothing
100 }
101 else {
102 // missing NFD section
103 ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
104 }
105}
106
107void
108Nfd::initializeManagement()
109{
110 m_internalFace = make_shared<InternalFace>();
111
112 m_fibManager = make_shared<FibManager>(ref(m_forwarder->getFib()),
113 bind(&Forwarder::getFace, m_forwarder.get(), _1),
114 m_internalFace,
115 ndn::ref(m_keyChain));
116
117 m_faceManager = make_shared<FaceManager>(ref(m_forwarder->getFaceTable()),
118 m_internalFace,
119 ndn::ref(m_keyChain));
120
121 m_strategyChoiceManager =
122 make_shared<StrategyChoiceManager>(ref(m_forwarder->getStrategyChoice()),
123 m_internalFace,
124 ndn::ref(m_keyChain));
125
126 m_statusServer = make_shared<StatusServer>(m_internalFace,
127 ref(*m_forwarder),
128 ndn::ref(m_keyChain));
129
130 ConfigFile config(&ignoreRibAndLogSections);
131 general::setConfigFile(config);
132
133 TablesConfigSection tablesConfig(m_forwarder->getCs(),
134 m_forwarder->getPit(),
135 m_forwarder->getFib(),
136 m_forwarder->getStrategyChoice(),
137 m_forwarder->getMeasurements());
138 tablesConfig.setConfigFile(config);
139
140 m_internalFace->getValidator().setConfigFile(config);
141
142 m_forwarder->getFaceTable().addReserved(m_internalFace, FACEID_INTERNAL_FACE);
143
144 m_faceManager->setConfigFile(config);
145
146 // parse config file
147 if (!m_configFile.empty()) {
148 config.parse(m_configFile, true);
149 config.parse(m_configFile, false);
150 }
151 else {
152 config.parse(m_configSection, true, INTERNAL_CONFIG);
153 config.parse(m_configSection, false, INTERNAL_CONFIG);
154 }
155
156 tablesConfig.ensureTablesAreConfigured();
157
158 // add FIB entry for NFD Management Protocol
159 shared_ptr<fib::Entry> entry = m_forwarder->getFib().insert("/localhost/nfd").first;
160 entry->addNextHop(m_internalFace, 0);
161}
162
163void
164Nfd::reloadConfigFile()
165{
166 // Logging
167 initializeLogging();
168 /// \todo Reopen log file
169
170 // Other stuff
171 ConfigFile config(&ignoreRibAndLogSections);
172
173 general::setConfigFile(config);
174
175 TablesConfigSection tablesConfig(m_forwarder->getCs(),
176 m_forwarder->getPit(),
177 m_forwarder->getFib(),
178 m_forwarder->getStrategyChoice(),
179 m_forwarder->getMeasurements());
180
181 tablesConfig.setConfigFile(config);
182
183 m_internalFace->getValidator().setConfigFile(config);
184 m_faceManager->setConfigFile(config);
185
186 if (!m_configFile.empty()) {
187 config.parse(m_configFile, false);
188 }
189 else {
190 config.parse(m_configSection, false, INTERNAL_CONFIG);
191 }
192}
193
194} // namespace nfd