blob: ca52e9b86e9f59eb312d1a1fb49642a18597f189 [file] [log] [blame]
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento22db5392017-04-14 00:56:43 -04003 * Copyright (c) 2014-2017, Regents of the University of California,
Junxiao Shifbf78342015-01-23 14:46:41 -07004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Junxiao Shidda0b462014-06-30 19:42:29 -070024 */
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070025
Alexander Afanasyev4a771362014-04-24 21:29:33 -070026#include <ndn-cxx/face.hpp>
27#include <ndn-cxx/name.hpp>
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070028
Junxiao Shi8e273ca2014-11-12 00:42:29 -070029#include <ndn-cxx/security/key-chain.hpp>
Junxiao Shia1937bf2014-11-06 11:43:40 -070030#include <ndn-cxx/util/face-uri.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000031#include <ndn-cxx/mgmt/nfd/controller.hpp>
32#include <ndn-cxx/mgmt/nfd/face-monitor.hpp>
33#include <ndn-cxx/mgmt/nfd/face-status.hpp>
Alexander Afanasyevf056c112014-08-14 16:39:25 -070034#include <ndn-cxx/encoding/buffer-stream.hpp>
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070035
36#include <boost/program_options/options_description.hpp>
37#include <boost/program_options/variables_map.hpp>
38#include <boost/program_options/parsers.hpp>
39
Junxiao Shi9f5b01d2016-08-05 03:54:28 +000040#include "core/version.hpp"
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080041#include "core/network.hpp"
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070042
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070043using namespace ndn::nfd;
Junxiao Shia1937bf2014-11-06 11:43:40 -070044using ndn::util::FaceUri;
45using ::nfd::Network;
46
47namespace ndn {
48namespace nfd_autoreg {
49
Junxiao Shidda0b462014-06-30 19:42:29 -070050class AutoregServer : boost::noncopyable
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070051{
52public:
53 AutoregServer()
Junxiao Shi8e273ca2014-11-12 00:42:29 -070054 : m_controller(m_face, m_keyChain)
Junxiao Shi15b12e72014-08-09 19:56:24 -070055 , m_faceMonitor(m_face)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070056 , m_cost(255)
57 {
58 }
59
60 void
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070061 onRegisterCommandSuccess(uint64_t faceId, const Name& prefix)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070062 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070063 std::cerr << "SUCCEED: register " << prefix << " on face " << faceId << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070064 }
65
66 void
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070067 onRegisterCommandFailure(uint64_t faceId, const Name& prefix,
Junxiao Shi29b41282016-08-22 03:47:02 +000068 const nfd::ControlResponse& response)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070069 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070070 std::cerr << "FAILED: register " << prefix << " on face " << faceId
Junxiao Shi29b41282016-08-22 03:47:02 +000071 << " (code: " << response.getCode() << ", reason: " << response.getText() << ")"
72 << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070073 }
74
Junxiao Shidda0b462014-06-30 19:42:29 -070075 /**
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070076 * \return true if uri has schema allowed to do auto-registrations
Junxiao Shidda0b462014-06-30 19:42:29 -070077 */
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070078 bool
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070079 hasAllowedSchema(const FaceUri& uri)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070080 {
81 const std::string& scheme = uri.getScheme();
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070082 return (scheme == "udp4" || scheme == "tcp4" ||
83 scheme == "udp6" || scheme == "tcp6");
84 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070085
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070086 /**
Alexander Afanasyevf056c112014-08-14 16:39:25 -070087 * \return true if address is blacklisted
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070088 */
89 bool
Alexander Afanasyevf056c112014-08-14 16:39:25 -070090 isBlacklisted(const boost::asio::ip::address& address)
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070091 {
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070092 for (std::vector<Network>::const_iterator network = m_blackList.begin();
93 network != m_blackList.end();
94 ++network)
95 {
96 if (network->doesContain(address))
97 return true;
98 }
99
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700100 return false;
101 }
102
103 /**
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700104 * \return true if address is whitelisted
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700105 */
106 bool
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700107 isWhitelisted(const boost::asio::ip::address& address)
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700108 {
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700109 for (std::vector<Network>::const_iterator network = m_whiteList.begin();
110 network != m_whiteList.end();
111 ++network)
112 {
113 if (network->doesContain(address))
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700114 return true;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700115 }
116
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700117 return false;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700118 }
119
120 void
Davide Pesavento22db5392017-04-14 00:56:43 -0400121 registerPrefixesForFace(uint64_t faceId, const std::vector<Name>& prefixes)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700122 {
Davide Pesavento22db5392017-04-14 00:56:43 -0400123 for (std::vector<Name>::const_iterator prefix = prefixes.begin();
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700124 prefix != prefixes.end();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700125 ++prefix)
126 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700127 m_controller.start<RibRegisterCommand>(
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700128 ControlParameters()
129 .setName(*prefix)
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700130 .setFaceId(faceId)
Junxiao Shidda0b462014-06-30 19:42:29 -0700131 .setOrigin(ROUTE_ORIGIN_AUTOREG)
132 .setCost(m_cost)
133 .setExpirationPeriod(time::milliseconds::max()),
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700134 bind(&AutoregServer::onRegisterCommandSuccess, this, faceId, *prefix),
Junxiao Shi29b41282016-08-22 03:47:02 +0000135 bind(&AutoregServer::onRegisterCommandFailure, this, faceId, *prefix, _1));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700136 }
137 }
138
139 void
Chengyu Fan9942cea2014-10-13 14:47:13 -0600140 registerPrefixesIfNeeded(uint64_t faceId, const FaceUri& uri, FacePersistency facePersistency)
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700141 {
142 if (hasAllowedSchema(uri)) {
143 boost::system::error_code ec;
144 boost::asio::ip::address address = boost::asio::ip::address::from_string(uri.getHost(), ec);
145
146 if (!address.is_multicast()) {
147 // register all-face prefixes
148 registerPrefixesForFace(faceId, m_allFacesPrefixes);
149
150 // register autoreg prefixes if new face is on-demand and not blacklisted and whitelisted
Chengyu Fan9942cea2014-10-13 14:47:13 -0600151 if (facePersistency == FACE_PERSISTENCY_ON_DEMAND &&
152 !isBlacklisted(address) && isWhitelisted(address)) {
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700153 registerPrefixesForFace(faceId, m_autoregPrefixes);
154 }
155 }
156 }
157 }
158
159 void
Junxiao Shi15b12e72014-08-09 19:56:24 -0700160 onNotification(const FaceEventNotification& notification)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700161 {
Junxiao Shi6e694322014-04-03 10:27:13 -0700162 if (notification.getKind() == FACE_EVENT_CREATED &&
Chengyu Fan9942cea2014-10-13 14:47:13 -0600163 notification.getFaceScope() != FACE_SCOPE_LOCAL)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700164 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700165 std::cerr << "PROCESSING: " << notification << std::endl;
166
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700167 registerPrefixesIfNeeded(notification.getFaceId(), FaceUri(notification.getRemoteUri()),
Chengyu Fan9942cea2014-10-13 14:47:13 -0600168 notification.getFacePersistency());
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700169 }
170 else
171 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700172 std::cerr << "IGNORED: " << notification << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700173 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700174 }
175
176 void
177 signalHandler()
178 {
179 m_face.shutdown();
180 }
181
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700182 void
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700183 usage(std::ostream& os,
Davide Pesavento22db5392017-04-14 00:56:43 -0400184 const boost::program_options::options_description& desc,
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700185 const char* programName)
186 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700187 os << "Usage:\n"
188 << " " << programName << " --prefix=</autoreg/prefix> [--prefix=/another/prefix] ...\n"
189 << "\n";
Davide Pesavento22db5392017-04-14 00:56:43 -0400190 os << desc;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700191 }
192
193 void
194 startProcessing()
195 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700196 std::cerr << "AUTOREG prefixes: " << std::endl;
Davide Pesavento22db5392017-04-14 00:56:43 -0400197 for (std::vector<Name>::const_iterator prefix = m_autoregPrefixes.begin();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700198 prefix != m_autoregPrefixes.end();
199 ++prefix)
200 {
201 std::cout << " " << *prefix << std::endl;
202 }
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700203 std::cerr << "ALL-FACES-AUTOREG prefixes: " << std::endl;
Davide Pesavento22db5392017-04-14 00:56:43 -0400204 for (std::vector<Name>::const_iterator prefix = m_allFacesPrefixes.begin();
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700205 prefix != m_allFacesPrefixes.end();
206 ++prefix)
207 {
208 std::cout << " " << *prefix << std::endl;
209 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700210
211 if (!m_blackList.empty())
212 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700213 std::cerr << "Blacklisted networks: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700214 for (std::vector<Network>::const_iterator network = m_blackList.begin();
215 network != m_blackList.end();
216 ++network)
217 {
218 std::cout << " " << *network << std::endl;
219 }
220 }
221
Junxiao Shidda0b462014-06-30 19:42:29 -0700222 std::cerr << "Whitelisted networks: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700223 for (std::vector<Network>::const_iterator network = m_whiteList.begin();
224 network != m_whiteList.end();
225 ++network)
226 {
227 std::cout << " " << *network << std::endl;
228 }
229
Junxiao Shifbf78342015-01-23 14:46:41 -0700230 m_faceMonitor.onNotification.connect(bind(&AutoregServer::onNotification, this, _1));
Junxiao Shi15b12e72014-08-09 19:56:24 -0700231 m_faceMonitor.start();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700232
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700233 boost::asio::signal_set signalSet(m_face.getIoService(), SIGINT, SIGTERM);
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700234 signalSet.async_wait(bind(&AutoregServer::signalHandler, this));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700235
236 m_face.processEvents();
237 }
238
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700239 void
240 startFetchingFaceStatusDataset()
241 {
Weiwei Liu7c795132016-10-07 14:22:54 -0700242 m_controller.fetch<FaceDataset>(
Davide Pesavento22db5392017-04-14 00:56:43 -0400243 [this] (const std::vector<FaceStatus>& faces) {
244 for (const auto& faceStatus : faces) {
Weiwei Liu7c795132016-10-07 14:22:54 -0700245 registerPrefixesIfNeeded(faceStatus.getFaceId(), FaceUri(faceStatus.getRemoteUri()),
246 faceStatus.getFacePersistency());
247 }
248 },
249 [this] (uint32_t code, const std::string& reason) {});
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700250 }
251
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700252 int
253 main(int argc, char* argv[])
254 {
Davide Pesavento22db5392017-04-14 00:56:43 -0400255 namespace po = boost::program_options;
256
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700257 po::options_description optionDesciption;
258 optionDesciption.add_options()
259 ("help,h", "produce help message")
Davide Pesavento22db5392017-04-14 00:56:43 -0400260 ("prefix,i", po::value<std::vector<Name>>(&m_autoregPrefixes)->composing(),
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700261 "prefix that should be automatically registered when new a remote non-local face is "
262 "established")
Davide Pesavento22db5392017-04-14 00:56:43 -0400263 ("all-faces-prefix,a", po::value<std::vector<Name>>(&m_allFacesPrefixes)->composing(),
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700264 "prefix that should be automatically registered for all TCP and UDP non-local faces "
265 "(blacklists and whitelists do not apply to this prefix)")
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700266 ("cost,c", po::value<uint64_t>(&m_cost)->default_value(255),
267 "FIB cost which should be assigned to autoreg nexthops")
Davide Pesavento22db5392017-04-14 00:56:43 -0400268 ("whitelist,w", po::value<std::vector<Network>>(&m_whiteList)->composing(),
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700269 "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
Davide Pesavento22db5392017-04-14 00:56:43 -0400270 ("blacklist,b", po::value<std::vector<Network>>(&m_blackList)->composing(),
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700271 "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700272 ("version,V", "show version and exit")
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700273 ;
274
275 po::variables_map options;
276 try
277 {
278 po::store(po::command_line_parser(argc, argv).options(optionDesciption).run(), options);
279 po::notify(options);
280 }
Davide Pesavento22db5392017-04-14 00:56:43 -0400281 catch (const std::exception& e)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700282 {
283 std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700284 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700285 return 1;
286 }
287
288 if (options.count("help"))
289 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700290 usage(std::cout, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700291 return 0;
292 }
293
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700294 if (options.count("version"))
295 {
296 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
297 return 0;
298 }
299
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700300 if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty())
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700301 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700302 std::cerr << "ERROR: at least one --prefix or --all-faces-prefix must be specified"
303 << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700304 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700305 return 2;
306 }
307
308 if (m_whiteList.empty())
309 {
310 // Allow everything
311 m_whiteList.push_back(Network::getMaxRangeV4());
312 m_whiteList.push_back(Network::getMaxRangeV6());
313 }
314
315 try
316 {
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700317 startFetchingFaceStatusDataset();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700318 startProcessing();
319 }
Davide Pesavento22db5392017-04-14 00:56:43 -0400320 catch (const std::exception& e)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700321 {
322 std::cerr << "ERROR: " << e.what() << std::endl;
323 return 2;
324 }
325
326 return 0;
327 }
328
329private:
330 Face m_face;
Junxiao Shi8e273ca2014-11-12 00:42:29 -0700331 KeyChain m_keyChain;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700332 Controller m_controller;
Junxiao Shi15b12e72014-08-09 19:56:24 -0700333 FaceMonitor m_faceMonitor;
Davide Pesavento22db5392017-04-14 00:56:43 -0400334 std::vector<Name> m_autoregPrefixes;
335 std::vector<Name> m_allFacesPrefixes;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700336 uint64_t m_cost;
337 std::vector<Network> m_whiteList;
338 std::vector<Network> m_blackList;
339};
340
Junxiao Shia1937bf2014-11-06 11:43:40 -0700341} // namespace nfd_autoreg
342} // namespace ndn
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700343
344int
345main(int argc, char* argv[])
346{
Junxiao Shia1937bf2014-11-06 11:43:40 -0700347 ndn::nfd_autoreg::AutoregServer server;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700348 return server.main(argc, argv);
349}