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 | |
| 57 | void |
| 58 | Nfd::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 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 70 | PrivilegeHelper::drop(); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void |
| 74 | Nfd::initializeLogging() |
| 75 | { |
| 76 | ConfigFile config(&ConfigFile::ignoreUnknownSection); |
| 77 | LoggerFactory::getInstance().setConfigFile(config); |
| 78 | |
| 79 | if (!m_configFile.empty()) { |
| 80 | config.parse(m_configFile, true); |
| 81 | config.parse(m_configFile, false); |
| 82 | } |
| 83 | else { |
| 84 | config.parse(m_configSection, true, INTERNAL_CONFIG); |
| 85 | config.parse(m_configSection, false, INTERNAL_CONFIG); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | |
| 90 | static inline void |
| 91 | ignoreRibAndLogSections(const std::string& filename, const std::string& sectionName, |
| 92 | const ConfigSection& section, bool isDryRun) |
| 93 | { |
| 94 | // Ignore "log" and "rib" sections, but raise an error if we're missing a |
| 95 | // handler for an NFD section. |
| 96 | if (sectionName == "rib" || sectionName == "log") { |
| 97 | // do nothing |
| 98 | } |
| 99 | else { |
| 100 | // missing NFD section |
| 101 | ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | Nfd::initializeManagement() |
| 107 | { |
| 108 | m_internalFace = make_shared<InternalFace>(); |
| 109 | |
| 110 | m_fibManager = make_shared<FibManager>(ref(m_forwarder->getFib()), |
| 111 | bind(&Forwarder::getFace, m_forwarder.get(), _1), |
| 112 | m_internalFace, |
| 113 | ndn::ref(m_keyChain)); |
| 114 | |
| 115 | m_faceManager = make_shared<FaceManager>(ref(m_forwarder->getFaceTable()), |
| 116 | m_internalFace, |
| 117 | ndn::ref(m_keyChain)); |
| 118 | |
| 119 | m_strategyChoiceManager = |
| 120 | make_shared<StrategyChoiceManager>(ref(m_forwarder->getStrategyChoice()), |
| 121 | m_internalFace, |
| 122 | ndn::ref(m_keyChain)); |
| 123 | |
| 124 | m_statusServer = make_shared<StatusServer>(m_internalFace, |
| 125 | ref(*m_forwarder), |
| 126 | ndn::ref(m_keyChain)); |
| 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 |