Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 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 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 26 | #include "nfd.hpp" |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 27 | #include "rib/service.hpp" |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 29 | #include "common/global.hpp" |
| 30 | #include "common/logger.hpp" |
| 31 | #include "common/privilege-helper.hpp" |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 32 | #include "core/version.hpp" |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 33 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 34 | #include <string.h> // for strsignal() |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 35 | |
Davide Pesavento | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame] | 36 | #include <boost/asio/signal_set.hpp> |
Davide Pesavento | 47456e7 | 2018-02-25 16:21:53 -0500 | [diff] [blame] | 37 | #include <boost/config.hpp> |
Davide Pesavento | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 38 | #include <boost/exception/diagnostic_information.hpp> |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 39 | #include <boost/program_options/options_description.hpp> |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 40 | #include <boost/program_options/parsers.hpp> |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 41 | #include <boost/program_options/variables_map.hpp> |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 42 | #include <boost/system/system_error.hpp> |
Davide Pesavento | 47456e7 | 2018-02-25 16:21:53 -0500 | [diff] [blame] | 43 | #include <boost/version.hpp> |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 44 | |
| 45 | #include <atomic> |
| 46 | #include <condition_variable> |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 47 | #include <iostream> |
Davide Pesavento | 2bdf60c | 2019-02-19 18:23:45 -0500 | [diff] [blame] | 48 | #include <thread> |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 49 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 50 | #include <ndn-cxx/util/logging.hpp> |
Davide Pesavento | 759db61 | 2019-08-31 22:56:28 -0400 | [diff] [blame] | 51 | #include <ndn-cxx/util/ostream-joiner.hpp> |
Davide Pesavento | 47456e7 | 2018-02-25 16:21:53 -0500 | [diff] [blame] | 52 | #include <ndn-cxx/version.hpp> |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 53 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 54 | #ifdef NFD_HAVE_LIBPCAP |
Davide Pesavento | 47456e7 | 2018-02-25 16:21:53 -0500 | [diff] [blame] | 55 | #include <pcap/pcap.h> |
| 56 | #endif |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 57 | #ifdef NFD_HAVE_SYSTEMD |
Davide Pesavento | 774071c | 2018-11-15 21:33:23 -0500 | [diff] [blame] | 58 | #include <systemd/sd-daemon.h> |
| 59 | #endif |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 60 | #ifdef NFD_HAVE_WEBSOCKET |
Davide Pesavento | 47456e7 | 2018-02-25 16:21:53 -0500 | [diff] [blame] | 61 | #include <websocketpp/version.hpp> |
| 62 | #endif |
| 63 | |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 64 | namespace po = boost::program_options; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 65 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 66 | NFD_LOG_INIT(Main); |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 67 | |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 68 | namespace nfd { |
| 69 | |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 70 | /** \brief Executes NFD with RIB manager |
| 71 | * |
| 72 | * NFD (main forwarding procedure) and RIB manager execute in two different threads. |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame] | 73 | * Each thread has its own instances of global io_context and global scheduler. |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 74 | * |
| 75 | * When either of the daemons fails, execution of non-failed daemon will be terminated as |
| 76 | * well. In other words, when NFD fails, RIB manager will be terminated; when RIB manager |
| 77 | * fails, NFD will be terminated. |
| 78 | */ |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame] | 79 | class NfdRunner : noncopyable |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 80 | { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 81 | public: |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 82 | explicit |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 83 | NfdRunner(const std::string& configFile) |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 84 | : m_nfd(configFile, m_nfdKeyChain) |
| 85 | , m_configFile(configFile) |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 86 | , m_terminateSignals(getGlobalIoService(), SIGINT, SIGTERM) |
| 87 | , m_reloadSignals(getGlobalIoService(), SIGHUP) |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 88 | { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 89 | m_terminateSignals.async_wait([this] (auto&&... args) { |
| 90 | terminate(std::forward<decltype(args)>(args)...); |
| 91 | }); |
| 92 | m_reloadSignals.async_wait([this] (auto&&... args) { |
| 93 | reload(std::forward<decltype(args)>(args)...); |
| 94 | }); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 97 | void |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 98 | initialize() |
| 99 | { |
| 100 | m_nfd.initialize(); |
| 101 | } |
| 102 | |
| 103 | int |
| 104 | run() |
| 105 | { |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 106 | // Return value: a non-zero value is assigned when either NFD or RIB manager (running in |
| 107 | // a separate thread) fails. |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 108 | std::atomic_int retval(0); |
| 109 | |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame] | 110 | boost::asio::io_context* const mainIo = &getGlobalIoService(); |
Teng Liang | f59e58f | 2018-09-07 16:41:54 -0700 | [diff] [blame] | 111 | setMainIoService(mainIo); |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame] | 112 | boost::asio::io_context* ribIo = nullptr; |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 113 | |
| 114 | // Mutex and conditional variable to implement synchronization between main and RIB manager |
| 115 | // threads: |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 116 | // - to block main thread until RIB manager thread starts and initializes ribIo (to allow |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 117 | // stopping it later) |
| 118 | std::mutex m; |
| 119 | std::condition_variable cv; |
| 120 | |
Davide Pesavento | 2bdf60c | 2019-02-19 18:23:45 -0500 | [diff] [blame] | 121 | std::thread ribThread([configFile = m_configFile, &retval, &ribIo, mainIo, &cv, &m] { |
| 122 | { |
| 123 | std::lock_guard<std::mutex> lock(m); |
| 124 | ribIo = &getGlobalIoService(); |
| 125 | BOOST_ASSERT(ribIo != mainIo); |
| 126 | setRibIoService(ribIo); |
| 127 | } |
| 128 | cv.notify_all(); // notify that ribIo has been assigned |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 129 | |
Davide Pesavento | 2bdf60c | 2019-02-19 18:23:45 -0500 | [diff] [blame] | 130 | try { |
| 131 | ndn::KeyChain ribKeyChain; |
| 132 | // must be created inside a separate thread |
| 133 | rib::Service ribService(configFile, ribKeyChain); |
| 134 | getGlobalIoService().run(); // ribIo is not thread-safe to use here |
| 135 | } |
| 136 | catch (const std::exception& e) { |
| 137 | NFD_LOG_FATAL(boost::diagnostic_information(e)); |
| 138 | retval = 1; |
| 139 | mainIo->stop(); |
| 140 | } |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 141 | |
Davide Pesavento | 2bdf60c | 2019-02-19 18:23:45 -0500 | [diff] [blame] | 142 | { |
| 143 | std::lock_guard<std::mutex> lock(m); |
| 144 | ribIo = nullptr; |
| 145 | } |
| 146 | }); |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 147 | |
| 148 | { |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 149 | // Wait to guarantee that ribIo is properly initialized, so it can be used to terminate |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 150 | // RIB manager thread. |
| 151 | std::unique_lock<std::mutex> lock(m); |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 152 | cv.wait(lock, [&ribIo] { return ribIo != nullptr; }); |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | try { |
Davide Pesavento | 774071c | 2018-11-15 21:33:23 -0500 | [diff] [blame] | 156 | systemdNotify("READY=1"); |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 157 | mainIo->run(); |
| 158 | } |
| 159 | catch (const std::exception& e) { |
Davide Pesavento | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 160 | NFD_LOG_FATAL(boost::diagnostic_information(e)); |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 161 | retval = 1; |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 162 | } |
| 163 | catch (const PrivilegeHelper::Error& e) { |
| 164 | NFD_LOG_FATAL(e.what()); |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 165 | retval = 4; |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | { |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 169 | // ribIo is guaranteed to be alive at this point |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 170 | std::lock_guard<std::mutex> lock(m); |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 171 | if (ribIo != nullptr) { |
| 172 | ribIo->stop(); |
| 173 | ribIo = nullptr; |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 176 | ribThread.join(); |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 177 | |
| 178 | return retval; |
| 179 | } |
| 180 | |
Davide Pesavento | 774071c | 2018-11-15 21:33:23 -0500 | [diff] [blame] | 181 | static void |
| 182 | systemdNotify(const char* state) |
| 183 | { |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 184 | #ifdef NFD_HAVE_SYSTEMD |
Davide Pesavento | 774071c | 2018-11-15 21:33:23 -0500 | [diff] [blame] | 185 | sd_notify(0, state); |
| 186 | #endif |
| 187 | } |
| 188 | |
| 189 | private: |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 190 | void |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 191 | terminate(const boost::system::error_code& error, int signalNo) |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 192 | { |
| 193 | if (error) |
| 194 | return; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 195 | |
Davide Pesavento | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 196 | NFD_LOG_INFO("Caught signal " << signalNo << " (" << ::strsignal(signalNo) << "), exiting..."); |
Davide Pesavento | 774071c | 2018-11-15 21:33:23 -0500 | [diff] [blame] | 197 | |
| 198 | systemdNotify("STOPPING=1"); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 199 | getGlobalIoService().stop(); |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 200 | } |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 201 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 202 | void |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 203 | reload(const boost::system::error_code& error, int signalNo) |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 204 | { |
| 205 | if (error) |
| 206 | return; |
| 207 | |
Davide Pesavento | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 208 | NFD_LOG_INFO("Caught signal " << signalNo << " (" << ::strsignal(signalNo) << "), reloading..."); |
Davide Pesavento | 774071c | 2018-11-15 21:33:23 -0500 | [diff] [blame] | 209 | |
| 210 | systemdNotify("RELOADING=1"); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 211 | m_nfd.reloadConfigFile(); |
Davide Pesavento | 774071c | 2018-11-15 21:33:23 -0500 | [diff] [blame] | 212 | systemdNotify("READY=1"); |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 213 | |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 214 | m_reloadSignals.async_wait([this] (auto&&... args) { |
| 215 | reload(std::forward<decltype(args)>(args)...); |
| 216 | }); |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 217 | } |
| 218 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 219 | private: |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 220 | ndn::KeyChain m_nfdKeyChain; |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 221 | Nfd m_nfd; |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 222 | std::string m_configFile; |
| 223 | |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 224 | boost::asio::signal_set m_terminateSignals; |
| 225 | boost::asio::signal_set m_reloadSignals; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 226 | }; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 227 | |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 228 | static void |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 229 | printUsage(std::ostream& os, const char* programName, const po::options_description& opts) |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 230 | { |
| 231 | os << "Usage: " << programName << " [options]\n" |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 232 | << "\n" |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 233 | << "Run the NDN Forwarding Daemon (NFD)\n" |
| 234 | << "\n" |
| 235 | << opts; |
| 236 | } |
| 237 | |
| 238 | static void |
| 239 | printLogModules(std::ostream& os) |
| 240 | { |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 241 | const auto& modules = ndn::util::Logging::getLoggerNames(); |
| 242 | std::copy(modules.begin(), modules.end(), ndn::make_ostream_joiner(os, "\n")); |
| 243 | os << std::endl; |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 244 | } |
| 245 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 246 | } // namespace nfd |
| 247 | |
| 248 | int |
| 249 | main(int argc, char** argv) |
| 250 | { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 251 | using namespace nfd; |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 252 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 253 | std::string configFile = NFD_DEFAULT_CONFIG_FILE; |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 254 | |
| 255 | po::options_description description("Options"); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 256 | description.add_options() |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 257 | ("help,h", "print this message and exit") |
| 258 | ("version,V", "show version information and exit") |
| 259 | ("config,c", po::value<std::string>(&configFile), |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 260 | "path to configuration file (default: " NFD_DEFAULT_CONFIG_FILE ")") |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 261 | ("modules,m", "list available logging modules") |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 262 | ; |
| 263 | |
| 264 | po::variables_map vm; |
| 265 | try { |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 266 | po::store(po::parse_command_line(argc, argv, description), vm); |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 267 | po::notify(vm); |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 268 | } |
| 269 | catch (const std::exception& e) { |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 270 | // Cannot use NFD_LOG_* macros here, because the logging subsystem is not initialized yet |
| 271 | // at this point. Moreover, we don't want to clutter error messages related to command-line |
| 272 | // parsing with timestamps and other useless text added by the macros. |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 273 | std::cerr << "ERROR: " << e.what() << "\n\n"; |
| 274 | printUsage(std::cerr, argv[0], description); |
| 275 | return 2; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 276 | } |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 277 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 278 | if (vm.count("help") > 0) { |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 279 | printUsage(std::cout, argv[0], description); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 280 | return 0; |
| 281 | } |
| 282 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 283 | if (vm.count("version") > 0) { |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 284 | std::cout << NFD_VERSION_BUILD_STRING << std::endl; |
| 285 | return 0; |
| 286 | } |
| 287 | |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 288 | if (vm.count("modules") > 0) { |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 289 | printLogModules(std::cout); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 290 | return 0; |
| 291 | } |
| 292 | |
Davide Pesavento | 47456e7 | 2018-02-25 16:21:53 -0500 | [diff] [blame] | 293 | const std::string boostBuildInfo = |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame^] | 294 | "with Boost version " + std::to_string(BOOST_VERSION / 100000) + |
| 295 | "." + std::to_string(BOOST_VERSION / 100 % 1000) + |
| 296 | "." + std::to_string(BOOST_VERSION % 100); |
Davide Pesavento | 47456e7 | 2018-02-25 16:21:53 -0500 | [diff] [blame] | 297 | const std::string pcapBuildInfo = |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 298 | #ifdef NFD_HAVE_LIBPCAP |
Davide Pesavento | 47456e7 | 2018-02-25 16:21:53 -0500 | [diff] [blame] | 299 | "with " + std::string(pcap_lib_version()); |
| 300 | #else |
| 301 | "without libpcap"; |
| 302 | #endif |
| 303 | const std::string wsBuildInfo = |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 304 | #ifdef NFD_HAVE_WEBSOCKET |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame^] | 305 | "with WebSocket++ version " + std::to_string(websocketpp::major_version) + |
| 306 | "." + std::to_string(websocketpp::minor_version) + |
| 307 | "." + std::to_string(websocketpp::patch_version); |
Davide Pesavento | 47456e7 | 2018-02-25 16:21:53 -0500 | [diff] [blame] | 308 | #else |
| 309 | "without WebSocket++"; |
| 310 | #endif |
| 311 | |
Davide Pesavento | 03f45d2 | 2019-04-04 12:34:26 -0400 | [diff] [blame] | 312 | std::clog << "NFD version " << NFD_VERSION_BUILD_STRING << " starting\n" |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 313 | << "Built with " BOOST_COMPILER ", with " BOOST_STDLIB |
Davide Pesavento | 47456e7 | 2018-02-25 16:21:53 -0500 | [diff] [blame] | 314 | ", " << boostBuildInfo << |
| 315 | ", " << pcapBuildInfo << |
| 316 | ", " << wsBuildInfo << |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 317 | ", with ndn-cxx version " NDN_CXX_VERSION_BUILD_STRING |
| 318 | << std::endl; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 319 | |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 320 | auto flush = ndn::make_scope_exit([] { ndn::util::Logging::flush(); }); |
| 321 | |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 322 | NfdRunner runner(configFile); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 323 | try { |
Alexander Afanasyev | 31c781e | 2015-02-09 17:39:59 -0800 | [diff] [blame] | 324 | runner.initialize(); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 325 | } |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 326 | catch (const boost::system::system_error& e) { |
Davide Pesavento | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 327 | NFD_LOG_FATAL(boost::diagnostic_information(e)); |
Davide Pesavento | 4b1921f | 2024-01-12 20:25:45 -0500 | [diff] [blame] | 328 | if (e.code() == boost::system::errc::operation_not_permitted || |
| 329 | e.code() == boost::system::errc::permission_denied) { |
| 330 | return 4; |
| 331 | } |
| 332 | return 1; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 333 | } |
| 334 | catch (const std::exception& e) { |
Davide Pesavento | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 335 | NFD_LOG_FATAL(boost::diagnostic_information(e)); |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 336 | return 1; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 337 | } |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 338 | catch (const PrivilegeHelper::Error& e) { |
| 339 | // PrivilegeHelper::Errors do not inherit from std::exception |
| 340 | // and represent seteuid/gid failures |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 341 | NFD_LOG_FATAL(e.what()); |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 342 | return 4; |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 343 | } |
| 344 | |
Alexander Afanasyev | f08a737 | 2015-02-09 21:28:19 -0800 | [diff] [blame] | 345 | return runner.run(); |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 346 | } |