blob: 61ae22a8572314daab97f607bf246e0408d8d931 [file] [log] [blame]
Alexander Afanasyev2a655f72015-01-26 18:38:33 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi9f5b01d2016-08-05 03:54:28 +00003 * Copyright (c) 2014-2016, Regents of the University of California,
Alexander Afanasyev2a655f72015-01-26 18:38:33 -08004 * 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 Shi9f5b01d2016-08-05 03:54:28 +000026#include "core/version.hpp"
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080027
28#include "multicast-discovery.hpp"
29#include "guess-from-search-domains.hpp"
30#include "guess-from-identity-name.hpp"
31
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -080032#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 Afanasyev2a655f72015-01-26 18:38:33 -080036#include <boost/noncopyable.hpp>
Alexander Afanasyev5c475972015-12-20 16:16:56 -080037#include <boost/program_options/options_description.hpp>
Alexander Afanasyev5c475972015-12-20 16:16:56 -080038#include <boost/program_options/parsers.hpp>
Junxiao Shi52fa45c2016-11-29 21:18:13 +000039#include <boost/program_options/variables_map.hpp>
Alexander Afanasyev5c475972015-12-20 16:16:56 -080040
41namespace po = boost::program_options;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080042
43namespace ndn {
44namespace tools {
Junxiao Shi52fa45c2016-11-29 21:18:13 +000045namespace 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 Afanasyev2a655f72015-01-26 18:38:33 -080048
49class NdnAutoconfig : boost::noncopyable
50{
51public:
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 Afanasyeve46279dc2015-01-29 15:39:17 -080062 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 Afanasyev2a655f72015-01-26 18:38:33 -080070 [&] (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 Afanasyeve46279dc2015-01-29 15:39:17 -080082 if (!m_isDaemonMode)
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -070083 BOOST_THROW_EXCEPTION(Error("No more stages, automatic discovery failed"));
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -080084 else
85 std::cerr << "No more stages, automatic discovery failed" << std::endl;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080086 })
87 {
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -080088 if (m_isDaemonMode) {
Junxiao Shi52fa45c2016-11-29 21:18:13 +000089 m_networkMonitor.reset(new ndn::util::NetworkMonitor(m_io));
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -080090 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 Afanasyev2a655f72015-01-26 18:38:33 -0800101 }
102
103 void
104 run()
105 {
Alexander Afanasyevba3f79e2015-02-02 13:56:13 -0800106 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 Afanasyeve46279dc2015-01-29 15:39:17 -0800111
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 Afanasyev2a655f72015-01-26 18:38:33 -0800122 }
123
124 static void
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800125 usage(std::ostream& os,
126 const po::options_description& optionDescription,
127 const char* programName)
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800128 {
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800129 os << "Usage:\n"
130 << " " << programName << " [options]\n"
131 << "\n";
132 os << optionDescription;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800133 }
134
135private:
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -0800136 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
146private:
147 boost::asio::io_service m_io;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800148 Face m_face;
149 KeyChain m_keyChain;
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -0800150 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 Afanasyev2a655f72015-01-26 18:38:33 -0800155
Junxiao Shi52fa45c2016-11-29 21:18:13 +0000156 MulticastDiscovery m_stage1;
157 GuessFromSearchDomains m_stage2;
158 GuessFromIdentityName m_stage3;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800159};
160
Junxiao Shi52fa45c2016-11-29 21:18:13 +0000161static int
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800162main(int argc, char** argv)
163{
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -0800164 bool isDaemonMode = false;
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800165 std::string configFile;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800166
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800167 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 Shi52fa45c2016-11-29 21:18:13 +0000187 NdnAutoconfig::usage(std::cerr, optionDescription, argv[0]);
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800188 return 1;
189 }
190
191 if (options.count("help")) {
Junxiao Shi52fa45c2016-11-29 21:18:13 +0000192 NdnAutoconfig::usage(std::cout, optionDescription, argv[0]);
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800193 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 Afanasyev2a655f72015-01-26 18:38:33 -0800214 }
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800215 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 Afanasyev2a655f72015-01-26 18:38:33 -0800223 }
224
225 try {
Junxiao Shi52fa45c2016-11-29 21:18:13 +0000226 NdnAutoconfig autoConfigInstance(isDaemonMode);
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800227 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 Shi52fa45c2016-11-29 21:18:13 +0000235
236} // namespace autoconfig
237} // namespace tools
238} // namespace ndn
239
240int
241main(int argc, char** argv)
242{
243 return ndn::tools::autoconfig::main(argc, argv);
244}