Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 9f5b01d | 2016-08-05 03:54:28 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -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. |
| 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 | |
Junxiao Shi | 9f5b01d | 2016-08-05 03:54:28 +0000 | [diff] [blame] | 26 | #include "core/version.hpp" |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 27 | |
| 28 | #include "multicast-discovery.hpp" |
| 29 | #include "guess-from-search-domains.hpp" |
| 30 | #include "guess-from-identity-name.hpp" |
| 31 | |
Alexander Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 32 | #include <ndn-cxx/util/network-monitor.hpp> |
| 33 | #include <ndn-cxx/util/scheduler.hpp> |
| 34 | #include <ndn-cxx/util/scheduler-scoped-event-id.hpp> |
| 35 | |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 36 | #include <boost/noncopyable.hpp> |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 37 | #include <boost/program_options/options_description.hpp> |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 38 | #include <boost/program_options/parsers.hpp> |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 39 | #include <boost/program_options/variables_map.hpp> |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 40 | |
| 41 | namespace po = boost::program_options; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 42 | |
| 43 | namespace ndn { |
| 44 | namespace tools { |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 45 | namespace autoconfig { |
| 46 | // ndn-autoconfig is an NDN tool not an NFD tool, so it uses ndn::tools::autoconfig namespace. |
| 47 | // It lives in NFD repository because nfd-start can automatically start ndn-autoconfig in daemon mode. |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 48 | |
| 49 | class NdnAutoconfig : boost::noncopyable |
| 50 | { |
| 51 | public: |
| 52 | class Error : public std::runtime_error |
| 53 | { |
| 54 | public: |
| 55 | explicit |
| 56 | Error(const std::string& what) |
| 57 | : std::runtime_error(what) |
| 58 | { |
| 59 | } |
| 60 | }; |
| 61 | |
Alexander Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 62 | explicit |
| 63 | NdnAutoconfig(bool isDaemonMode) |
| 64 | : m_face(m_io) |
| 65 | , m_scheduler(m_io) |
| 66 | , m_startStagesEvent(m_scheduler) |
| 67 | , m_isDaemonMode(isDaemonMode) |
| 68 | , m_terminationSignalSet(m_io) |
| 69 | , m_stage1(m_face, m_keyChain, |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 70 | [&] (const std::string& errorMessage) { |
| 71 | std::cerr << "Stage 1 failed: " << errorMessage << std::endl; |
| 72 | m_stage2.start(); |
| 73 | }) |
| 74 | , m_stage2(m_face, m_keyChain, |
| 75 | [&] (const std::string& errorMessage) { |
| 76 | std::cerr << "Stage 2 failed: " << errorMessage << std::endl; |
| 77 | m_stage3.start(); |
| 78 | }) |
| 79 | , m_stage3(m_face, m_keyChain, |
| 80 | [&] (const std::string& errorMessage) { |
| 81 | std::cerr << "Stage 3 failed: " << errorMessage << std::endl; |
Alexander Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 82 | if (!m_isDaemonMode) |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 83 | BOOST_THROW_EXCEPTION(Error("No more stages, automatic discovery failed")); |
Alexander Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 84 | else |
| 85 | std::cerr << "No more stages, automatic discovery failed" << std::endl; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 86 | }) |
| 87 | { |
Alexander Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 88 | if (m_isDaemonMode) { |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 89 | m_networkMonitor.reset(new ndn::util::NetworkMonitor(m_io)); |
Alexander Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 90 | m_networkMonitor->onNetworkStateChanged.connect([this] { |
| 91 | // delay stages, so if multiple events are triggered in short sequence, |
| 92 | // only one auto-detection procedure is triggered |
| 93 | m_startStagesEvent = m_scheduler.scheduleEvent(time::seconds(5), |
| 94 | bind(&NdnAutoconfig::startStages, this)); |
| 95 | }); |
| 96 | } |
| 97 | |
| 98 | // Delay a little bit |
| 99 | m_startStagesEvent = m_scheduler.scheduleEvent(time::milliseconds(100), |
| 100 | bind(&NdnAutoconfig::startStages, this)); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void |
| 104 | run() |
| 105 | { |
Alexander Afanasyev | ba3f79e | 2015-02-02 13:56:13 -0800 | [diff] [blame] | 106 | if (m_isDaemonMode) { |
| 107 | m_terminationSignalSet.add(SIGINT); |
| 108 | m_terminationSignalSet.add(SIGTERM); |
| 109 | m_terminationSignalSet.async_wait(bind(&NdnAutoconfig::terminate, this, _1, _2)); |
| 110 | } |
Alexander Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 111 | |
| 112 | m_io.run(); |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | terminate(const boost::system::error_code& error, int signalNo) |
| 117 | { |
| 118 | if (error) |
| 119 | return; |
| 120 | |
| 121 | m_io.stop(); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static void |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 125 | usage(std::ostream& os, |
| 126 | const po::options_description& optionDescription, |
| 127 | const char* programName) |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 128 | { |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 129 | os << "Usage:\n" |
| 130 | << " " << programName << " [options]\n" |
| 131 | << "\n"; |
| 132 | os << optionDescription; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | private: |
Alexander Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 136 | void |
| 137 | startStages() |
| 138 | { |
| 139 | m_stage1.start(); |
| 140 | if (m_isDaemonMode) { |
| 141 | m_startStagesEvent = m_scheduler.scheduleEvent(time::hours(1), |
| 142 | bind(&NdnAutoconfig::startStages, this)); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | private: |
| 147 | boost::asio::io_service m_io; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 148 | Face m_face; |
| 149 | KeyChain m_keyChain; |
Alexander Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 150 | unique_ptr<util::NetworkMonitor> m_networkMonitor; |
| 151 | util::Scheduler m_scheduler; |
| 152 | util::scheduler::ScopedEventId m_startStagesEvent; |
| 153 | bool m_isDaemonMode; |
| 154 | boost::asio::signal_set m_terminationSignalSet; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 155 | |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 156 | MulticastDiscovery m_stage1; |
| 157 | GuessFromSearchDomains m_stage2; |
| 158 | GuessFromIdentityName m_stage3; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 159 | }; |
| 160 | |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 161 | static int |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 162 | main(int argc, char** argv) |
| 163 | { |
Alexander Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 164 | bool isDaemonMode = false; |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 165 | std::string configFile; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 166 | |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 167 | po::options_description optionDescription("Options"); |
| 168 | optionDescription.add_options() |
| 169 | ("help,h", "produce help message") |
| 170 | ("daemon,d", po::bool_switch(&isDaemonMode)->default_value(isDaemonMode), |
| 171 | "run in daemon mode, detecting network change events and re-running " |
| 172 | "auto-discovery procedure. In addition, the auto-discovery procedure " |
| 173 | "is unconditionally re-run every hour.\n" |
| 174 | "NOTE: if connection to NFD fails, the daemon will be terminated.") |
| 175 | ("config,c", po::value<std::string>(&configFile), "configuration file. If `enabled = true` " |
| 176 | "is not specified, no actions will be performed.") |
| 177 | ("version,V", "show version and exit") |
| 178 | ; |
| 179 | |
| 180 | po::variables_map options; |
| 181 | try { |
| 182 | po::store(po::parse_command_line(argc, argv, optionDescription), options); |
| 183 | po::notify(options); |
| 184 | } |
| 185 | catch (const std::exception& e) { |
| 186 | std::cerr << "ERROR: " << e.what() << "\n" << std::endl; |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 187 | NdnAutoconfig::usage(std::cerr, optionDescription, argv[0]); |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 188 | return 1; |
| 189 | } |
| 190 | |
| 191 | if (options.count("help")) { |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 192 | NdnAutoconfig::usage(std::cout, optionDescription, argv[0]); |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | if (options.count("version")) { |
| 197 | std::cout << NFD_VERSION_BUILD_STRING << std::endl; |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | // Enable (one-shot or daemon mode whenever config file is not specified) |
| 202 | bool isEnabled = true; |
| 203 | |
| 204 | po::options_description configFileOptions; |
| 205 | configFileOptions.add_options() |
| 206 | ("enabled", po::value<bool>(&isEnabled)) |
| 207 | ; |
| 208 | |
| 209 | if (!configFile.empty()) { |
| 210 | isEnabled = false; // Disable by default if config file is specified |
| 211 | try { |
| 212 | po::store(po::parse_config_file<char>(configFile.c_str(), configFileOptions), options); |
| 213 | po::notify(options); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 214 | } |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 215 | catch (const std::exception& e) { |
| 216 | std::cerr << "ERROR: " << e.what() << std::endl << std::endl; |
| 217 | return 1; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | if (!isEnabled) { |
| 222 | return 0; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | try { |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 226 | NdnAutoconfig autoConfigInstance(isDaemonMode); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 227 | autoConfigInstance.run(); |
| 228 | } |
| 229 | catch (const std::exception& error) { |
| 230 | std::cerr << "ERROR: " << error.what() << std::endl; |
| 231 | return 1; |
| 232 | } |
| 233 | return 0; |
| 234 | } |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 235 | |
| 236 | } // namespace autoconfig |
| 237 | } // namespace tools |
| 238 | } // namespace ndn |
| 239 | |
| 240 | int |
| 241 | main(int argc, char** argv) |
| 242 | { |
| 243 | return ndn::tools::autoconfig::main(argc, argv); |
| 244 | } |