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), |
| 132 | m_internalFace); |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 133 | m_faceManager = make_shared<FaceManager>(ref(m_forwarder->getFaceTable()), |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 134 | m_internalFace); |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame^] | 135 | m_strategyChoiceManager = make_shared<StrategyChoiceManager>( |
| 136 | ref(m_forwarder->getStrategyChoice()), m_internalFace); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 137 | |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame^] | 138 | m_statusServer = make_shared<StatusServer>(m_internalFace, ref(*m_forwarder)); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 139 | |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 140 | ConfigFile config((IgnoreRibAndLogSections())); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 141 | general::setConfigFile(config); |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 142 | |
| 143 | TablesConfigSection tablesConfig(m_forwarder->getCs(), |
| 144 | m_forwarder->getPit(), |
| 145 | m_forwarder->getFib(), |
| 146 | m_forwarder->getStrategyChoice(), |
| 147 | m_forwarder->getMeasurements()); |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 148 | tablesConfig.setConfigFile(config); |
| 149 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 150 | m_internalFace->getValidator().setConfigFile(config); |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 151 | |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame^] | 152 | m_forwarder->getFaceTable().addReserved(m_internalFace, FACEID_INTERNAL_FACE); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 153 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 154 | m_faceManager->setConfigFile(config); |
| 155 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 156 | // parse config file |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 157 | config.parse(m_configFile, true); |
| 158 | config.parse(m_configFile, false); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 159 | |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 160 | tablesConfig.ensureTablesAreConfigured(); |
| 161 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 162 | // add FIB entry for NFD Management Protocol |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 163 | shared_ptr<fib::Entry> entry = m_forwarder->getFib().insert("/localhost/nfd").first; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 164 | entry->addNextHop(m_internalFace, 0); |
| 165 | } |
| 166 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 167 | static void |
| 168 | printUsage(std::ostream& os, const std::string& programName) |
| 169 | { |
| 170 | os << "Usage: \n" |
| 171 | << " " << programName << " [options]\n" |
| 172 | << "\n" |
| 173 | << "Run NFD forwarding daemon\n" |
| 174 | << "\n" |
| 175 | << "Options:\n" |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 176 | << " [--help] - print this help message\n" |
| 177 | << " [--version] - print version and exit\n" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 178 | << " [--modules] - list available logging modules\n" |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 179 | << " [--config /path/to/nfd.conf] - path to configuration file " |
| 180 | << "(default: " << DEFAULT_CONFIG_FILE << ")\n" |
| 181 | ; |
| 182 | } |
| 183 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 184 | static void |
| 185 | printModules(std::ostream& os) |
| 186 | { |
| 187 | using namespace std; |
| 188 | |
| 189 | os << "Available logging modules: \n"; |
| 190 | |
| 191 | list<string> modules(LoggerFactory::getInstance().getModules()); |
| 192 | for (list<string>::const_iterator i = modules.begin(); i != modules.end(); ++i) |
| 193 | { |
| 194 | os << *i << "\n"; |
| 195 | } |
| 196 | } |
| 197 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 198 | static bool |
| 199 | parseCommandLine(int argc, char** argv, ProgramOptions& options) |
| 200 | { |
| 201 | options.showUsage = false; |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 202 | options.showVersion = false; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 203 | options.showModules = false; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 204 | options.config = DEFAULT_CONFIG_FILE; |
| 205 | |
| 206 | while (true) { |
| 207 | int optionIndex = 0; |
| 208 | static ::option longOptions[] = { |
| 209 | { "help" , no_argument , 0, 0 }, |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 210 | { "modules", no_argument , 0, 0 }, |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 211 | { "config" , required_argument, 0, 0 }, |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 212 | { "version", no_argument , 0, 0 }, |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 213 | { 0 , 0 , 0, 0 } |
| 214 | }; |
| 215 | int c = getopt_long_only(argc, argv, "", longOptions, &optionIndex); |
| 216 | if (c == -1) |
| 217 | break; |
| 218 | |
| 219 | switch (c) { |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 220 | case 0: |
| 221 | switch (optionIndex) { |
| 222 | case 0: // help |
| 223 | options.showUsage = true; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 224 | break; |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 225 | case 1: // modules |
| 226 | options.showModules = true; |
| 227 | break; |
| 228 | case 2: // config |
| 229 | options.config = ::optarg; |
| 230 | break; |
| 231 | case 3: // version |
| 232 | options.showVersion = true; |
| 233 | break; |
| 234 | default: |
| 235 | return false; |
| 236 | } |
| 237 | break; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 238 | } |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 239 | } |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 240 | return true; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 241 | } |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 242 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 243 | void |
| 244 | terminate(const boost::system::error_code& error, |
| 245 | int signalNo, |
| 246 | boost::asio::signal_set& signalSet) |
| 247 | { |
| 248 | if (error) |
| 249 | return; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 250 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 251 | if (signalNo == SIGINT || |
| 252 | signalNo == SIGTERM) |
| 253 | { |
| 254 | getGlobalIoService().stop(); |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 255 | NFD_LOG_INFO("Caught signal '" << strsignal(signalNo) << "', exiting..."); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 256 | } |
| 257 | else |
| 258 | { |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 259 | signalSet.async_wait(bind(&Nfd::terminate, this, _1, _2, ref(signalSet))); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 260 | } |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 261 | } |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 262 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 263 | void |
| 264 | reload(const boost::system::error_code& error, |
| 265 | int signalNo, |
| 266 | boost::asio::signal_set& signalSet) |
| 267 | { |
| 268 | if (error) |
| 269 | return; |
| 270 | |
| 271 | NFD_LOG_INFO("Caught signal '" << strsignal(signalNo)); |
| 272 | |
| 273 | //////////////////////// |
| 274 | // Reload config file // |
| 275 | //////////////////////// |
| 276 | |
| 277 | // Logging |
| 278 | initializeLogging(); |
| 279 | /// \todo Reopen log file |
| 280 | |
| 281 | // Other stuff |
| 282 | ConfigFile config((IgnoreRibAndLogSections())); |
| 283 | |
| 284 | general::setConfigFile(config); |
| 285 | |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 286 | TablesConfigSection tablesConfig(m_forwarder->getCs(), |
| 287 | m_forwarder->getPit(), |
| 288 | m_forwarder->getFib(), |
| 289 | m_forwarder->getStrategyChoice(), |
| 290 | m_forwarder->getMeasurements()); |
| 291 | |
| 292 | tablesConfig.setConfigFile(config); |
| 293 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 294 | m_internalFace->getValidator().setConfigFile(config); |
| 295 | m_faceManager->setConfigFile(config); |
| 296 | |
| 297 | config.parse(m_configFile, false); |
| 298 | |
| 299 | //////////////////////// |
| 300 | |
| 301 | signalSet.async_wait(bind(&Nfd::reload, this, _1, _2, ref(signalSet))); |
| 302 | } |
| 303 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 304 | private: |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 305 | std::string m_configFile; |
| 306 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 307 | shared_ptr<Forwarder> m_forwarder; |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 308 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 309 | shared_ptr<InternalFace> m_internalFace; |
| 310 | shared_ptr<FibManager> m_fibManager; |
| 311 | shared_ptr<FaceManager> m_faceManager; |
| 312 | shared_ptr<StrategyChoiceManager> m_strategyChoiceManager; |
| 313 | shared_ptr<StatusServer> m_statusServer; |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 314 | |
| 315 | shared_ptr<std::ofstream> m_logFile; |
| 316 | std::basic_streambuf<char>* m_originalStreamBuf; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 317 | }; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 318 | |
| 319 | } // namespace nfd |
| 320 | |
| 321 | int |
| 322 | main(int argc, char** argv) |
| 323 | { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 324 | using namespace nfd; |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 325 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 326 | ProgramOptions options; |
| 327 | bool isCommandLineValid = Nfd::parseCommandLine(argc, argv, options); |
| 328 | if (!isCommandLineValid) { |
| 329 | Nfd::printUsage(std::cerr, argv[0]); |
| 330 | return 1; |
| 331 | } |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 332 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 333 | if (options.showUsage) { |
| 334 | Nfd::printUsage(std::cout, argv[0]); |
| 335 | return 0; |
| 336 | } |
| 337 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 338 | if (options.showVersion) { |
| 339 | std::cout << NFD_VERSION_BUILD_STRING << std::endl; |
| 340 | return 0; |
| 341 | } |
| 342 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 343 | if (options.showModules) { |
| 344 | Nfd::printModules(std::cout); |
| 345 | return 0; |
| 346 | } |
| 347 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 348 | Nfd nfdInstance(options.config); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 349 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 350 | try { |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 351 | nfdInstance.initialize(); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 352 | } |
| 353 | catch (boost::filesystem::filesystem_error& e) { |
| 354 | if (e.code() == boost::system::errc::permission_denied) { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 355 | NFD_LOG_FATAL("Permissions denied for " << e.path1() << ". " << |
| 356 | argv[0] << " should be run as superuser"); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 357 | } |
| 358 | else { |
| 359 | NFD_LOG_FATAL(e.what()); |
| 360 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 361 | return 1; |
| 362 | } |
| 363 | catch (const std::exception& e) { |
| 364 | NFD_LOG_FATAL(e.what()); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 365 | return 2; |
| 366 | } |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 367 | catch (const PrivilegeHelper::Error& e) { |
| 368 | // PrivilegeHelper::Errors do not inherit from std::exception |
| 369 | // and represent seteuid/gid failures |
| 370 | |
| 371 | NFD_LOG_FATAL(e.what()); |
| 372 | return 3; |
| 373 | } |
| 374 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 375 | boost::asio::signal_set terminationSignalSet(getGlobalIoService()); |
| 376 | terminationSignalSet.add(SIGINT); |
| 377 | terminationSignalSet.add(SIGTERM); |
| 378 | terminationSignalSet.async_wait(bind(&Nfd::terminate, &nfdInstance, _1, _2, |
| 379 | ref(terminationSignalSet))); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 380 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 381 | boost::asio::signal_set reloadSignalSet(getGlobalIoService()); |
| 382 | reloadSignalSet.add(SIGHUP); |
| 383 | reloadSignalSet.async_wait(bind(&Nfd::reload, &nfdInstance, _1, _2, |
| 384 | ref(reloadSignalSet))); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 385 | |
| 386 | try { |
| 387 | getGlobalIoService().run(); |
| 388 | } |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 389 | catch (const std::exception& e) { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 390 | NFD_LOG_FATAL(e.what()); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 391 | return 4; |
| 392 | } |
| 393 | catch (const PrivilegeHelper::Error& e) { |
| 394 | NFD_LOG_FATAL(e.what()); |
| 395 | return 5; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | return 0; |
| 399 | } |