Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014, 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 |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 25 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 26 | #include <getopt.h> |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 27 | #include <boost/filesystem.hpp> |
| 28 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 29 | #include "version.hpp" |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 30 | #include "core/logger.hpp" |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 31 | #include "core/global-io.hpp" |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 32 | #include "core/privilege-helper.hpp" |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 33 | #include "fw/forwarder.hpp" |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 34 | #include "face/null-face.hpp" |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 35 | #include "mgmt/internal-face.hpp" |
Steve DiBenedetto | 214563c | 2014-02-03 19:20:36 -0700 | [diff] [blame] | 36 | #include "mgmt/fib-manager.hpp" |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 37 | #include "mgmt/face-manager.hpp" |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 38 | #include "mgmt/strategy-choice-manager.hpp" |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 39 | #include "mgmt/status-server.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 40 | #include "core/config-file.hpp" |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 41 | #include "mgmt/general-config-section.hpp" |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 42 | #include "mgmt/tables-config-section.hpp" |
Alexander Afanasyev | c78b141 | 2014-02-19 14:08:26 -0800 | [diff] [blame] | 43 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 44 | namespace nfd { |
| 45 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 46 | NFD_LOG_INIT("NFD"); |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 47 | |
| 48 | struct ProgramOptions |
Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 49 | { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 50 | bool showUsage; |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 51 | bool showVersion; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 52 | bool showModules; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 53 | std::string config; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 56 | class Nfd : noncopyable |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 57 | { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 58 | public: |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 59 | explicit |
| 60 | Nfd(const std::string& configFile) |
| 61 | : m_configFile(configFile) |
| 62 | , m_originalStreamBuf(0) |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | ~Nfd() |
| 67 | { |
| 68 | if (static_cast<bool>(m_originalStreamBuf)) { |
| 69 | std::clog.rdbuf(m_originalStreamBuf); |
| 70 | } |
| 71 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 72 | |
| 73 | void |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 74 | initialize() |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 75 | { |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 76 | initializeLogging(); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 77 | |
| 78 | m_forwarder = make_shared<Forwarder>(); |
| 79 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 80 | initializeManagement(); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 81 | |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 82 | m_forwarder->getFaceTable().addReserved(make_shared<NullFace>(), FACEID_NULL); |
| 83 | m_forwarder->getFaceTable().addReserved( |
| 84 | make_shared<NullFace>(FaceUri("contentstore://")), FACEID_CONTENT_STORE); |
| 85 | |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 86 | PrivilegeHelper::drop(); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | |
| 90 | void |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 91 | initializeLogging() |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 92 | { |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 93 | ConfigFile config(&ConfigFile::ignoreUnknownSection); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 94 | LoggerFactory::getInstance().setConfigFile(config); |
| 95 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 96 | config.parse(m_configFile, true); |
| 97 | config.parse(m_configFile, false); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 98 | } |
| 99 | |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 100 | class IgnoreRibAndLogSections |
| 101 | { |
| 102 | public: |
| 103 | void |
| 104 | operator()(const std::string& filename, |
| 105 | const std::string& sectionName, |
| 106 | const ConfigSection& section, |
| 107 | bool isDryRun) |
| 108 | |
| 109 | { |
| 110 | // Ignore "log" and sections beginning with "rib_" (intended for rib manager), |
| 111 | // but raise an error if we're missing a handler for an NFD section. |
| 112 | |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 113 | if (sectionName.find("rib") == 0 || sectionName == "log") |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 114 | { |
| 115 | // do nothing |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | // missing NFD section |
| 120 | ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun); |
| 121 | } |
| 122 | } |
| 123 | }; |
| 124 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 125 | void |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 126 | initializeManagement() |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 127 | { |
| 128 | m_internalFace = make_shared<InternalFace>(); |
| 129 | |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 130 | m_fibManager = make_shared<FibManager>(ref(m_forwarder->getFib()), |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 131 | bind(&Forwarder::getFace, m_forwarder.get(), _1), |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 132 | m_internalFace, |
| 133 | ndn::ref(m_keyChain)); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 134 | |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 135 | m_faceManager = make_shared<FaceManager>(ref(m_forwarder->getFaceTable()), |
| 136 | m_internalFace, |
| 137 | ndn::ref(m_keyChain)); |
| 138 | |
| 139 | m_strategyChoiceManager = |
| 140 | make_shared<StrategyChoiceManager>(ref(m_forwarder->getStrategyChoice()), |
| 141 | m_internalFace, |
| 142 | ndn::ref(m_keyChain)); |
| 143 | |
| 144 | m_statusServer = make_shared<StatusServer>(m_internalFace, |
| 145 | ref(*m_forwarder), |
| 146 | ndn::ref(m_keyChain)); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 147 | |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 148 | ConfigFile config((IgnoreRibAndLogSections())); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 149 | general::setConfigFile(config); |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 150 | |
| 151 | TablesConfigSection tablesConfig(m_forwarder->getCs(), |
| 152 | m_forwarder->getPit(), |
| 153 | m_forwarder->getFib(), |
| 154 | m_forwarder->getStrategyChoice(), |
| 155 | m_forwarder->getMeasurements()); |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 156 | tablesConfig.setConfigFile(config); |
| 157 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 158 | m_internalFace->getValidator().setConfigFile(config); |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 159 | |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 160 | m_forwarder->getFaceTable().addReserved(m_internalFace, FACEID_INTERNAL_FACE); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 161 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 162 | m_faceManager->setConfigFile(config); |
| 163 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 164 | // parse config file |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 165 | config.parse(m_configFile, true); |
| 166 | config.parse(m_configFile, false); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 167 | |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 168 | tablesConfig.ensureTablesAreConfigured(); |
| 169 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 170 | // add FIB entry for NFD Management Protocol |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 171 | shared_ptr<fib::Entry> entry = m_forwarder->getFib().insert("/localhost/nfd").first; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 172 | entry->addNextHop(m_internalFace, 0); |
| 173 | } |
| 174 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 175 | static void |
| 176 | printUsage(std::ostream& os, const std::string& programName) |
| 177 | { |
| 178 | os << "Usage: \n" |
| 179 | << " " << programName << " [options]\n" |
| 180 | << "\n" |
| 181 | << "Run NFD forwarding daemon\n" |
| 182 | << "\n" |
| 183 | << "Options:\n" |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 184 | << " [--help] - print this help message\n" |
| 185 | << " [--version] - print version and exit\n" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 186 | << " [--modules] - list available logging modules\n" |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 187 | << " [--config /path/to/nfd.conf] - path to configuration file " |
| 188 | << "(default: " << DEFAULT_CONFIG_FILE << ")\n" |
| 189 | ; |
| 190 | } |
| 191 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 192 | static void |
| 193 | printModules(std::ostream& os) |
| 194 | { |
| 195 | using namespace std; |
| 196 | |
| 197 | os << "Available logging modules: \n"; |
| 198 | |
| 199 | list<string> modules(LoggerFactory::getInstance().getModules()); |
| 200 | for (list<string>::const_iterator i = modules.begin(); i != modules.end(); ++i) |
| 201 | { |
| 202 | os << *i << "\n"; |
| 203 | } |
| 204 | } |
| 205 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 206 | static bool |
| 207 | parseCommandLine(int argc, char** argv, ProgramOptions& options) |
| 208 | { |
| 209 | options.showUsage = false; |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 210 | options.showVersion = false; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 211 | options.showModules = false; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 212 | options.config = DEFAULT_CONFIG_FILE; |
| 213 | |
| 214 | while (true) { |
| 215 | int optionIndex = 0; |
| 216 | static ::option longOptions[] = { |
| 217 | { "help" , no_argument , 0, 0 }, |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 218 | { "modules", no_argument , 0, 0 }, |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 219 | { "config" , required_argument, 0, 0 }, |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 220 | { "version", no_argument , 0, 0 }, |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 221 | { 0 , 0 , 0, 0 } |
| 222 | }; |
| 223 | int c = getopt_long_only(argc, argv, "", longOptions, &optionIndex); |
| 224 | if (c == -1) |
| 225 | break; |
| 226 | |
| 227 | switch (c) { |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 228 | case 0: |
| 229 | switch (optionIndex) { |
| 230 | case 0: // help |
| 231 | options.showUsage = true; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 232 | break; |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 233 | case 1: // modules |
| 234 | options.showModules = true; |
| 235 | break; |
| 236 | case 2: // config |
| 237 | options.config = ::optarg; |
| 238 | break; |
| 239 | case 3: // version |
| 240 | options.showVersion = true; |
| 241 | break; |
| 242 | default: |
| 243 | return false; |
| 244 | } |
| 245 | break; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 246 | } |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 247 | } |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 248 | return true; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 249 | } |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 250 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 251 | void |
| 252 | terminate(const boost::system::error_code& error, |
| 253 | int signalNo, |
| 254 | boost::asio::signal_set& signalSet) |
| 255 | { |
| 256 | if (error) |
| 257 | return; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 258 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 259 | if (signalNo == SIGINT || |
| 260 | signalNo == SIGTERM) |
| 261 | { |
| 262 | getGlobalIoService().stop(); |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 263 | NFD_LOG_INFO("Caught signal '" << strsignal(signalNo) << "', exiting..."); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 264 | } |
| 265 | else |
| 266 | { |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 267 | signalSet.async_wait(bind(&Nfd::terminate, this, _1, _2, ref(signalSet))); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 268 | } |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 269 | } |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 270 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 271 | void |
| 272 | reload(const boost::system::error_code& error, |
| 273 | int signalNo, |
| 274 | boost::asio::signal_set& signalSet) |
| 275 | { |
| 276 | if (error) |
| 277 | return; |
| 278 | |
| 279 | NFD_LOG_INFO("Caught signal '" << strsignal(signalNo)); |
| 280 | |
| 281 | //////////////////////// |
| 282 | // Reload config file // |
| 283 | //////////////////////// |
| 284 | |
| 285 | // Logging |
| 286 | initializeLogging(); |
| 287 | /// \todo Reopen log file |
| 288 | |
| 289 | // Other stuff |
| 290 | ConfigFile config((IgnoreRibAndLogSections())); |
| 291 | |
| 292 | general::setConfigFile(config); |
| 293 | |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 294 | TablesConfigSection tablesConfig(m_forwarder->getCs(), |
| 295 | m_forwarder->getPit(), |
| 296 | m_forwarder->getFib(), |
| 297 | m_forwarder->getStrategyChoice(), |
| 298 | m_forwarder->getMeasurements()); |
| 299 | |
| 300 | tablesConfig.setConfigFile(config); |
| 301 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 302 | m_internalFace->getValidator().setConfigFile(config); |
| 303 | m_faceManager->setConfigFile(config); |
| 304 | |
| 305 | config.parse(m_configFile, false); |
| 306 | |
| 307 | //////////////////////// |
| 308 | |
| 309 | signalSet.async_wait(bind(&Nfd::reload, this, _1, _2, ref(signalSet))); |
| 310 | } |
| 311 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 312 | private: |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 313 | std::string m_configFile; |
| 314 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 315 | shared_ptr<Forwarder> m_forwarder; |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 316 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 317 | shared_ptr<InternalFace> m_internalFace; |
| 318 | shared_ptr<FibManager> m_fibManager; |
| 319 | shared_ptr<FaceManager> m_faceManager; |
| 320 | shared_ptr<StrategyChoiceManager> m_strategyChoiceManager; |
| 321 | shared_ptr<StatusServer> m_statusServer; |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 322 | |
| 323 | shared_ptr<std::ofstream> m_logFile; |
| 324 | std::basic_streambuf<char>* m_originalStreamBuf; |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 325 | ndn::KeyChain m_keyChain; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 326 | }; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 327 | |
| 328 | } // namespace nfd |
| 329 | |
| 330 | int |
| 331 | main(int argc, char** argv) |
| 332 | { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 333 | using namespace nfd; |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 334 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 335 | ProgramOptions options; |
| 336 | bool isCommandLineValid = Nfd::parseCommandLine(argc, argv, options); |
| 337 | if (!isCommandLineValid) { |
| 338 | Nfd::printUsage(std::cerr, argv[0]); |
| 339 | return 1; |
| 340 | } |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 341 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 342 | if (options.showUsage) { |
| 343 | Nfd::printUsage(std::cout, argv[0]); |
| 344 | return 0; |
| 345 | } |
| 346 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 347 | if (options.showVersion) { |
| 348 | std::cout << NFD_VERSION_BUILD_STRING << std::endl; |
| 349 | return 0; |
| 350 | } |
| 351 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 352 | if (options.showModules) { |
| 353 | Nfd::printModules(std::cout); |
| 354 | return 0; |
| 355 | } |
| 356 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 357 | Nfd nfdInstance(options.config); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 358 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 359 | try { |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 360 | nfdInstance.initialize(); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 361 | } |
| 362 | catch (boost::filesystem::filesystem_error& e) { |
| 363 | if (e.code() == boost::system::errc::permission_denied) { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 364 | NFD_LOG_FATAL("Permissions denied for " << e.path1() << ". " << |
| 365 | argv[0] << " should be run as superuser"); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 366 | } |
| 367 | else { |
| 368 | NFD_LOG_FATAL(e.what()); |
| 369 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 370 | return 1; |
| 371 | } |
| 372 | catch (const std::exception& e) { |
| 373 | NFD_LOG_FATAL(e.what()); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 374 | return 2; |
| 375 | } |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 376 | catch (const PrivilegeHelper::Error& e) { |
| 377 | // PrivilegeHelper::Errors do not inherit from std::exception |
| 378 | // and represent seteuid/gid failures |
| 379 | |
| 380 | NFD_LOG_FATAL(e.what()); |
| 381 | return 3; |
| 382 | } |
| 383 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 384 | boost::asio::signal_set terminationSignalSet(getGlobalIoService()); |
| 385 | terminationSignalSet.add(SIGINT); |
| 386 | terminationSignalSet.add(SIGTERM); |
| 387 | terminationSignalSet.async_wait(bind(&Nfd::terminate, &nfdInstance, _1, _2, |
| 388 | ref(terminationSignalSet))); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 389 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 390 | boost::asio::signal_set reloadSignalSet(getGlobalIoService()); |
| 391 | reloadSignalSet.add(SIGHUP); |
| 392 | reloadSignalSet.async_wait(bind(&Nfd::reload, &nfdInstance, _1, _2, |
| 393 | ref(reloadSignalSet))); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 394 | |
| 395 | try { |
| 396 | getGlobalIoService().run(); |
| 397 | } |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 398 | catch (const std::exception& e) { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 399 | NFD_LOG_FATAL(e.what()); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 400 | return 4; |
| 401 | } |
| 402 | catch (const PrivilegeHelper::Error& e) { |
| 403 | NFD_LOG_FATAL(e.what()); |
| 404 | return 5; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | return 0; |
| 408 | } |