Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 7 | #include <getopt.h> |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 8 | #include <boost/filesystem.hpp> |
| 9 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 10 | #include "core/logger.hpp" |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 11 | #include "core/global-io.hpp" |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 12 | #include "fw/forwarder.hpp" |
| 13 | #include "mgmt/internal-face.hpp" |
Steve DiBenedetto | 214563c | 2014-02-03 19:20:36 -0700 | [diff] [blame] | 14 | #include "mgmt/fib-manager.hpp" |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 15 | #include "mgmt/face-manager.hpp" |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 16 | #include "mgmt/strategy-choice-manager.hpp" |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 17 | #include "mgmt/status-server.hpp" |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 18 | #include "mgmt/config-file.hpp" |
Alexander Afanasyev | c78b141 | 2014-02-19 14:08:26 -0800 | [diff] [blame] | 19 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 20 | namespace nfd { |
| 21 | |
| 22 | NFD_LOG_INIT("Main"); |
| 23 | |
| 24 | struct ProgramOptions |
Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 25 | { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 26 | bool showUsage; |
| 27 | std::string config; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 30 | class Nfd : noncopyable |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 31 | { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 32 | public: |
| 33 | explicit |
| 34 | Nfd(const std::string& configFile) |
| 35 | : m_internalFace(new InternalFace()) |
| 36 | , m_fibManager(m_forwarder.getFib(), |
| 37 | bind(&Forwarder::getFace, &m_forwarder, _1), |
| 38 | m_internalFace) |
| 39 | , m_faceManager(m_forwarder.getFaceTable(), m_internalFace) |
| 40 | , m_strategyChoiceManager(m_forwarder.getStrategyChoice(), m_internalFace) |
| 41 | , m_statusServer(m_internalFace, m_forwarder) |
| 42 | { |
| 43 | ConfigFile config; |
| 44 | m_internalFace->getValidator().setConfigFile(config); |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 45 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 46 | m_forwarder.addFace(m_internalFace); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 47 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 48 | m_faceManager.setConfigFile(config); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 49 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 50 | // parse config file |
| 51 | config.parse(configFile, true); |
| 52 | config.parse(configFile, false); |
| 53 | |
| 54 | // add FIB entry for NFD Management Protocol |
| 55 | shared_ptr<fib::Entry> entry = m_forwarder.getFib().insert("/localhost/nfd").first; |
| 56 | entry->addNextHop(m_internalFace, 0); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | static void |
| 61 | printUsage(std::ostream& os, const std::string& programName) |
| 62 | { |
| 63 | os << "Usage: \n" |
| 64 | << " " << programName << " [options]\n" |
| 65 | << "\n" |
| 66 | << "Run NFD forwarding daemon\n" |
| 67 | << "\n" |
| 68 | << "Options:\n" |
| 69 | << " [--help] - print this help message\n" |
| 70 | << " [--config /path/to/nfd.conf] - path to configuration file " |
| 71 | << "(default: " << DEFAULT_CONFIG_FILE << ")\n" |
| 72 | ; |
| 73 | } |
| 74 | |
| 75 | static bool |
| 76 | parseCommandLine(int argc, char** argv, ProgramOptions& options) |
| 77 | { |
| 78 | options.showUsage = false; |
| 79 | options.config = DEFAULT_CONFIG_FILE; |
| 80 | |
| 81 | while (true) { |
| 82 | int optionIndex = 0; |
| 83 | static ::option longOptions[] = { |
| 84 | { "help" , no_argument , 0, 0 }, |
| 85 | { "config" , required_argument, 0, 0 }, |
| 86 | { 0 , 0 , 0, 0 } |
| 87 | }; |
| 88 | int c = getopt_long_only(argc, argv, "", longOptions, &optionIndex); |
| 89 | if (c == -1) |
| 90 | break; |
| 91 | |
| 92 | switch (c) { |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 93 | case 0: |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 94 | switch (optionIndex) { |
| 95 | case 0: // help |
| 96 | options.showUsage = true; |
| 97 | break; |
| 98 | case 1: // config |
| 99 | options.config = ::optarg; |
| 100 | break; |
| 101 | default: |
| 102 | return false; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 103 | } |
| 104 | break; |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 105 | } |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 106 | } |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 107 | |
| 108 | return true; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 109 | } |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 110 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 111 | void |
| 112 | terminate(const boost::system::error_code& error, |
| 113 | int signalNo, |
| 114 | boost::asio::signal_set& signalSet) |
| 115 | { |
| 116 | if (error) |
| 117 | return; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 118 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 119 | if (signalNo == SIGINT || |
| 120 | signalNo == SIGTERM) |
| 121 | { |
| 122 | getGlobalIoService().stop(); |
| 123 | std::cout << "Caught signal '" << strsignal(signalNo) << "', exiting..." << std::endl; |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | /// \todo May be try to reload config file (at least security section) |
| 128 | signalSet.async_wait(bind(&Nfd::terminate, this, _1, _2, |
| 129 | boost::ref(signalSet))); |
| 130 | } |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 131 | } |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 132 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 133 | private: |
| 134 | Forwarder m_forwarder; |
| 135 | shared_ptr<InternalFace> m_internalFace; |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 136 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 137 | FibManager m_fibManager; |
| 138 | FaceManager m_faceManager; |
| 139 | StrategyChoiceManager m_strategyChoiceManager; |
| 140 | StatusServer m_statusServer; |
| 141 | }; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 142 | |
| 143 | } // namespace nfd |
| 144 | |
| 145 | int |
| 146 | main(int argc, char** argv) |
| 147 | { |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 148 | using namespace nfd; |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 149 | |
Alexander Afanasyev | 5a4388a | 2014-03-27 18:02:55 -0700 | [diff] [blame] | 150 | ProgramOptions options; |
| 151 | bool isCommandLineValid = Nfd::parseCommandLine(argc, argv, options); |
| 152 | if (!isCommandLineValid) { |
| 153 | Nfd::printUsage(std::cerr, argv[0]); |
| 154 | return 1; |
| 155 | } |
| 156 | if (options.showUsage) { |
| 157 | Nfd::printUsage(std::cout, argv[0]); |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | try { |
| 162 | Nfd nfdInstance(options.config); |
| 163 | |
| 164 | boost::asio::signal_set signalSet(getGlobalIoService()); |
| 165 | signalSet.add(SIGINT); |
| 166 | signalSet.add(SIGTERM); |
| 167 | signalSet.add(SIGHUP); |
| 168 | signalSet.add(SIGUSR1); |
| 169 | signalSet.add(SIGUSR2); |
| 170 | signalSet.async_wait(bind(&Nfd::terminate, &nfdInstance, _1, _2, |
| 171 | boost::ref(signalSet))); |
| 172 | |
| 173 | getGlobalIoService().run(); |
| 174 | } |
| 175 | catch (boost::filesystem::filesystem_error& e) { |
| 176 | if (e.code() == boost::system::errc::permission_denied) { |
| 177 | NFD_LOG_FATAL("Permissions denied for " << e.path1() << ". " |
| 178 | << argv[0] << " should be run as superuser"); |
| 179 | } |
| 180 | else { |
| 181 | NFD_LOG_FATAL(e.what()); |
| 182 | } |
| 183 | return 2; |
| 184 | } |
| 185 | catch (std::exception& e) { |
| 186 | NFD_LOG_FATAL(e.what()); |
| 187 | return 1; |
| 188 | } |
| 189 | |
| 190 | return 0; |
| 191 | } |