Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 41 | namespace nfd { |
| 42 | |
| 43 | static const std::string INTERNAL_CONFIG = "internal://nfd.conf"; |
| 44 | |
| 45 | Nfd::Nfd(const std::string& configFile, ndn::KeyChain& keyChain) |
| 46 | : m_configFile(configFile) |
| 47 | , m_keyChain(keyChain) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | Nfd::Nfd(const ConfigSection& config, ndn::KeyChain& keyChain) |
| 52 | : m_configSection(config) |
| 53 | , m_keyChain(keyChain) |
| 54 | { |
| 55 | } |
| 56 | |
Alexander Afanasyev | 2bda6f8 | 2015-02-10 14:17:19 -0800 | [diff] [blame] | 57 | Nfd::~Nfd() |
| 58 | { |
| 59 | // It is necessary to explicitly define the destructor, because some member variables (e.g., |
| 60 | // unique_ptr<Forwarder>) are forward-declared, but implicitly declared destructor requires |
| 61 | // complete types for all members when instantiated. |
| 62 | } |
| 63 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 64 | void |
| 65 | Nfd::initialize() |
| 66 | { |
| 67 | initializeLogging(); |
| 68 | |
Alexander Afanasyev | 2bda6f8 | 2015-02-10 14:17:19 -0800 | [diff] [blame] | 69 | m_forwarder.reset(new Forwarder()); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 70 | |
| 71 | initializeManagement(); |
| 72 | |
| 73 | m_forwarder->getFaceTable().addReserved(make_shared<NullFace>(), FACEID_NULL); |
| 74 | m_forwarder->getFaceTable().addReserved(make_shared<NullFace>(FaceUri("contentstore://")), |
| 75 | FACEID_CONTENT_STORE); |
| 76 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 77 | PrivilegeHelper::drop(); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void |
| 81 | Nfd::initializeLogging() |
| 82 | { |
| 83 | ConfigFile config(&ConfigFile::ignoreUnknownSection); |
| 84 | LoggerFactory::getInstance().setConfigFile(config); |
| 85 | |
| 86 | if (!m_configFile.empty()) { |
| 87 | config.parse(m_configFile, true); |
| 88 | config.parse(m_configFile, false); |
| 89 | } |
| 90 | else { |
| 91 | config.parse(m_configSection, true, INTERNAL_CONFIG); |
| 92 | config.parse(m_configSection, false, INTERNAL_CONFIG); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | |
| 97 | static inline void |
| 98 | ignoreRibAndLogSections(const std::string& filename, const std::string& sectionName, |
| 99 | const ConfigSection& section, bool isDryRun) |
| 100 | { |
| 101 | // Ignore "log" and "rib" sections, but raise an error if we're missing a |
| 102 | // handler for an NFD section. |
| 103 | if (sectionName == "rib" || sectionName == "log") { |
| 104 | // do nothing |
| 105 | } |
| 106 | else { |
| 107 | // missing NFD section |
| 108 | ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | Nfd::initializeManagement() |
| 114 | { |
| 115 | m_internalFace = make_shared<InternalFace>(); |
| 116 | |
Alexander Afanasyev | 2bda6f8 | 2015-02-10 14:17:19 -0800 | [diff] [blame] | 117 | m_fibManager.reset(new FibManager(m_forwarder->getFib(), |
| 118 | bind(&Forwarder::getFace, m_forwarder.get(), _1), |
| 119 | m_internalFace, m_keyChain)); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 120 | |
Alexander Afanasyev | 2bda6f8 | 2015-02-10 14:17:19 -0800 | [diff] [blame] | 121 | m_faceManager.reset(new FaceManager(m_forwarder->getFaceTable(), m_internalFace, m_keyChain)); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 122 | |
Alexander Afanasyev | 2bda6f8 | 2015-02-10 14:17:19 -0800 | [diff] [blame] | 123 | m_strategyChoiceManager.reset(new StrategyChoiceManager(m_forwarder->getStrategyChoice(), |
| 124 | m_internalFace, m_keyChain)); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 125 | |
Alexander Afanasyev | 2bda6f8 | 2015-02-10 14:17:19 -0800 | [diff] [blame] | 126 | m_statusServer.reset(new StatusServer(m_internalFace, *m_forwarder, m_keyChain)); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 127 | |
| 128 | ConfigFile config(&ignoreRibAndLogSections); |
| 129 | general::setConfigFile(config); |
| 130 | |
| 131 | TablesConfigSection tablesConfig(m_forwarder->getCs(), |
| 132 | m_forwarder->getPit(), |
| 133 | m_forwarder->getFib(), |
| 134 | m_forwarder->getStrategyChoice(), |
| 135 | m_forwarder->getMeasurements()); |
| 136 | tablesConfig.setConfigFile(config); |
| 137 | |
| 138 | m_internalFace->getValidator().setConfigFile(config); |
| 139 | |
| 140 | m_forwarder->getFaceTable().addReserved(m_internalFace, FACEID_INTERNAL_FACE); |
| 141 | |
| 142 | m_faceManager->setConfigFile(config); |
| 143 | |
| 144 | // parse config file |
| 145 | if (!m_configFile.empty()) { |
| 146 | config.parse(m_configFile, true); |
| 147 | config.parse(m_configFile, false); |
| 148 | } |
| 149 | else { |
| 150 | config.parse(m_configSection, true, INTERNAL_CONFIG); |
| 151 | config.parse(m_configSection, false, INTERNAL_CONFIG); |
| 152 | } |
| 153 | |
| 154 | tablesConfig.ensureTablesAreConfigured(); |
| 155 | |
| 156 | // add FIB entry for NFD Management Protocol |
| 157 | shared_ptr<fib::Entry> entry = m_forwarder->getFib().insert("/localhost/nfd").first; |
| 158 | entry->addNextHop(m_internalFace, 0); |
| 159 | } |
| 160 | |
| 161 | void |
| 162 | Nfd::reloadConfigFile() |
| 163 | { |
| 164 | // Logging |
| 165 | initializeLogging(); |
| 166 | /// \todo Reopen log file |
| 167 | |
| 168 | // Other stuff |
| 169 | ConfigFile config(&ignoreRibAndLogSections); |
| 170 | |
| 171 | general::setConfigFile(config); |
| 172 | |
| 173 | TablesConfigSection tablesConfig(m_forwarder->getCs(), |
| 174 | m_forwarder->getPit(), |
| 175 | m_forwarder->getFib(), |
| 176 | m_forwarder->getStrategyChoice(), |
| 177 | m_forwarder->getMeasurements()); |
| 178 | |
| 179 | tablesConfig.setConfigFile(config); |
| 180 | |
| 181 | m_internalFace->getValidator().setConfigFile(config); |
| 182 | m_faceManager->setConfigFile(config); |
| 183 | |
| 184 | if (!m_configFile.empty()) { |
| 185 | config.parse(m_configFile, false); |
| 186 | } |
| 187 | else { |
| 188 | config.parse(m_configSection, false, INTERNAL_CONFIG); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | } // namespace nfd |