blob: 58ce2c7c1c4f626fd0f553e437e6cb01a625d9ed [file] [log] [blame]
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa997d292017-08-24 20:16:59 -04002/*
Davide Pesavento40641272023-03-16 13:31:12 -04003 * Copyright (c) 2014-2023, 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
Davide Pesaventoa997d292017-08-24 20:16:59 -040026#include "core/network.hpp"
27#include "core/version.hpp"
28
Alexander Afanasyev4a771362014-04-24 21:29:33 -070029#include <ndn-cxx/face.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000030#include <ndn-cxx/mgmt/nfd/controller.hpp>
31#include <ndn-cxx/mgmt/nfd/face-monitor.hpp>
Davide Pesavento40641272023-03-16 13:31:12 -040032#include <ndn-cxx/mgmt/nfd/status-dataset.hpp>
Junxiao Shi83be1da2017-06-30 13:37:37 +000033#include <ndn-cxx/net/face-uri.hpp>
Junxiao Shi08e96312017-06-29 18:07:27 +000034#include <ndn-cxx/security/key-chain.hpp>
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070035
Davide Pesaventoa9b09b62022-06-04 14:07:25 -040036#include <boost/asio/signal_set.hpp>
Davide Pesavento97e33022019-02-14 16:00:50 -050037#include <boost/exception/diagnostic_information.hpp>
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070038#include <boost/program_options/options_description.hpp>
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070039#include <boost/program_options/parsers.hpp>
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040040#include <boost/program_options/variables_map.hpp>
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070041
Davide Pesaventoa997d292017-08-24 20:16:59 -040042#include <iostream>
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070043
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040044namespace nfd::tools::autoreg {
Junxiao Shi08e96312017-06-29 18:07:27 +000045
Davide Pesaventoa9b09b62022-06-04 14:07:25 -040046using ndn::FaceUri;
47using ndn::Name;
48
Junxiao Shidda0b462014-06-30 19:42:29 -070049class AutoregServer : boost::noncopyable
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070050{
51public:
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040052 static void
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070053 onRegisterCommandSuccess(uint64_t faceId, const Name& prefix)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070054 {
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040055 std::cerr << "SUCCESS: register " << prefix << " on face " << faceId << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070056 }
57
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040058 static void
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070059 onRegisterCommandFailure(uint64_t faceId, const Name& prefix,
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040060 const ndn::nfd::ControlResponse& response)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070061 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070062 std::cerr << "FAILED: register " << prefix << " on face " << faceId
Junxiao Shi29b41282016-08-22 03:47:02 +000063 << " (code: " << response.getCode() << ", reason: " << response.getText() << ")"
64 << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070065 }
66
Junxiao Shidda0b462014-06-30 19:42:29 -070067 /**
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070068 * \return true if uri has schema allowed to do auto-registrations
Junxiao Shidda0b462014-06-30 19:42:29 -070069 */
Davide Pesavento59769b12017-11-12 23:52:06 -050070 static bool
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070071 hasAllowedSchema(const FaceUri& uri)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070072 {
73 const std::string& scheme = uri.getScheme();
Davide Pesavento59769b12017-11-12 23:52:06 -050074 return scheme == "udp4" || scheme == "tcp4" ||
75 scheme == "udp6" || scheme == "tcp6";
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070076 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070077
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070078 /**
Alexander Afanasyevf056c112014-08-14 16:39:25 -070079 * \return true if address is blacklisted
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070080 */
81 bool
Davide Pesavento59769b12017-11-12 23:52:06 -050082 isBlacklisted(const boost::asio::ip::address& address) const
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070083 {
Junxiao Shi08e96312017-06-29 18:07:27 +000084 return std::any_of(m_blackList.begin(), m_blackList.end(),
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040085 [&] (const auto& net) { return net.doesContain(address); });
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070086 }
87
88 /**
Alexander Afanasyevf056c112014-08-14 16:39:25 -070089 * \return true if address is whitelisted
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070090 */
91 bool
Davide Pesavento59769b12017-11-12 23:52:06 -050092 isWhitelisted(const boost::asio::ip::address& address) const
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070093 {
Junxiao Shi08e96312017-06-29 18:07:27 +000094 return std::any_of(m_whiteList.begin(), m_whiteList.end(),
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040095 [&] (const auto& net) { return net.doesContain(address); });
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070096 }
97
98 void
Davide Pesavento22db5392017-04-14 00:56:43 -040099 registerPrefixesForFace(uint64_t faceId, const std::vector<Name>& prefixes)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700100 {
Junxiao Shi08e96312017-06-29 18:07:27 +0000101 for (const Name& prefix : prefixes) {
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400102 m_controller.start<ndn::nfd::RibRegisterCommand>(
103 ndn::nfd::ControlParameters()
Junxiao Shi08e96312017-06-29 18:07:27 +0000104 .setName(prefix)
105 .setFaceId(faceId)
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400106 .setOrigin(ndn::nfd::ROUTE_ORIGIN_AUTOREG)
Junxiao Shi08e96312017-06-29 18:07:27 +0000107 .setCost(m_cost)
Davide Pesaventoa9b09b62022-06-04 14:07:25 -0400108 .setExpirationPeriod(ndn::time::milliseconds::max()),
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400109 [=] (auto&&...) { onRegisterCommandSuccess(faceId, prefix); },
110 [=] (const auto& response) { onRegisterCommandFailure(faceId, prefix, response); });
Junxiao Shi08e96312017-06-29 18:07:27 +0000111 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700112 }
113
114 void
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400115 registerPrefixesIfNeeded(uint64_t faceId, const FaceUri& uri, ndn::nfd::FacePersistency facePersistency)
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700116 {
117 if (hasAllowedSchema(uri)) {
118 boost::system::error_code ec;
Davide Pesaventod91fe6d2023-10-04 21:40:02 -0400119 auto address = boost::asio::ip::make_address(uri.getHost(), ec);
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700120
121 if (!address.is_multicast()) {
122 // register all-face prefixes
123 registerPrefixesForFace(faceId, m_allFacesPrefixes);
124
125 // register autoreg prefixes if new face is on-demand and not blacklisted and whitelisted
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400126 if (facePersistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND &&
Chengyu Fan9942cea2014-10-13 14:47:13 -0600127 !isBlacklisted(address) && isWhitelisted(address)) {
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700128 registerPrefixesForFace(faceId, m_autoregPrefixes);
129 }
130 }
131 }
132 }
133
134 void
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400135 onNotification(const ndn::nfd::FaceEventNotification& notification)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700136 {
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400137 if (notification.getKind() == ndn::nfd::FACE_EVENT_CREATED &&
138 notification.getFaceScope() != ndn::nfd::FACE_SCOPE_LOCAL) {
Junxiao Shi08e96312017-06-29 18:07:27 +0000139 std::cerr << "PROCESSING: " << notification << std::endl;
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700140
Junxiao Shi08e96312017-06-29 18:07:27 +0000141 registerPrefixesIfNeeded(notification.getFaceId(), FaceUri(notification.getRemoteUri()),
142 notification.getFacePersistency());
143 }
144 else {
145 std::cerr << "IGNORED: " << notification << std::endl;
146 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700147 }
148
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700149 void
150 startProcessing()
151 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700152 std::cerr << "AUTOREG prefixes: " << std::endl;
Junxiao Shi08e96312017-06-29 18:07:27 +0000153 for (const Name& prefix : m_autoregPrefixes) {
154 std::cout << " " << prefix << std::endl;
155 }
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700156 std::cerr << "ALL-FACES-AUTOREG prefixes: " << std::endl;
Junxiao Shi08e96312017-06-29 18:07:27 +0000157 for (const Name& prefix : m_allFacesPrefixes) {
158 std::cout << " " << prefix << std::endl;
159 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700160
Junxiao Shi08e96312017-06-29 18:07:27 +0000161 if (!m_blackList.empty()) {
162 std::cerr << "Blacklisted networks: " << std::endl;
163 for (const Network& network : m_blackList) {
164 std::cout << " " << network << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700165 }
Junxiao Shi08e96312017-06-29 18:07:27 +0000166 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700167
Junxiao Shidda0b462014-06-30 19:42:29 -0700168 std::cerr << "Whitelisted networks: " << std::endl;
Junxiao Shi08e96312017-06-29 18:07:27 +0000169 for (const Network& network : m_whiteList) {
170 std::cout << " " << network << std::endl;
171 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700172
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400173 m_faceMonitor.onNotification.connect([this] (const auto& notif) { onNotification(notif); });
Junxiao Shi15b12e72014-08-09 19:56:24 -0700174 m_faceMonitor.start();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700175
Davide Pesaventoe277f8b2023-11-11 17:14:02 -0500176 boost::asio::signal_set signalSet(m_face.getIoContext(), SIGINT, SIGTERM);
Davide Pesavento412c9822021-07-02 00:21:05 -0400177 signalSet.async_wait([this] (auto&&...) { m_face.shutdown(); });
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700178
179 m_face.processEvents();
180 }
181
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700182 void
183 startFetchingFaceStatusDataset()
184 {
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400185 m_controller.fetch<ndn::nfd::FaceDataset>(
Davide Pesavento412c9822021-07-02 00:21:05 -0400186 [this] (const auto& faces) {
Davide Pesavento22db5392017-04-14 00:56:43 -0400187 for (const auto& faceStatus : faces) {
Weiwei Liu7c795132016-10-07 14:22:54 -0700188 registerPrefixesIfNeeded(faceStatus.getFaceId(), FaceUri(faceStatus.getRemoteUri()),
189 faceStatus.getFacePersistency());
190 }
191 },
Davide Pesavento412c9822021-07-02 00:21:05 -0400192 [] (auto&&...) {});
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700193 }
194
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700195 int
196 main(int argc, char* argv[])
197 {
Davide Pesavento22db5392017-04-14 00:56:43 -0400198 namespace po = boost::program_options;
199
Davide Pesavento59769b12017-11-12 23:52:06 -0500200 po::options_description optionsDesc("Options");
201 optionsDesc.add_options()
202 ("help,h", "print this message and exit")
203 ("version,V", "show version information and exit")
Davide Pesavento22db5392017-04-14 00:56:43 -0400204 ("prefix,i", po::value<std::vector<Name>>(&m_autoregPrefixes)->composing(),
Davide Pesavento59769b12017-11-12 23:52:06 -0500205 "prefix that should be automatically registered when a new non-local face is created")
Davide Pesavento22db5392017-04-14 00:56:43 -0400206 ("all-faces-prefix,a", po::value<std::vector<Name>>(&m_allFacesPrefixes)->composing(),
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700207 "prefix that should be automatically registered for all TCP and UDP non-local faces "
208 "(blacklists and whitelists do not apply to this prefix)")
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400209 ("cost,c", po::value<uint64_t>(&m_cost)->default_value(m_cost),
Davide Pesavento59769b12017-11-12 23:52:06 -0500210 "FIB cost that should be assigned to autoreg nexthops")
Davide Pesavento22db5392017-04-14 00:56:43 -0400211 ("whitelist,w", po::value<std::vector<Network>>(&m_whiteList)->composing(),
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700212 "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
Davide Pesavento22db5392017-04-14 00:56:43 -0400213 ("blacklist,b", po::value<std::vector<Network>>(&m_blackList)->composing(),
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700214 "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
215 ;
216
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400217 auto usage = [&] (std::ostream& os) {
218 os << "Usage: " << argv[0] << " [--prefix=</autoreg/prefix>]... [options]\n"
219 << "\n"
220 << optionsDesc;
221 };
222
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700223 po::variables_map options;
Junxiao Shi08e96312017-06-29 18:07:27 +0000224 try {
Davide Pesavento59769b12017-11-12 23:52:06 -0500225 po::store(po::parse_command_line(argc, argv, optionsDesc), options);
Junxiao Shi08e96312017-06-29 18:07:27 +0000226 po::notify(options);
227 }
228 catch (const std::exception& e) {
229 std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400230 usage(std::cerr);
Davide Pesavento59769b12017-11-12 23:52:06 -0500231 return 2;
Junxiao Shi08e96312017-06-29 18:07:27 +0000232 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700233
Davide Pesavento59769b12017-11-12 23:52:06 -0500234 if (options.count("help") > 0) {
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400235 usage(std::cout);
Junxiao Shi08e96312017-06-29 18:07:27 +0000236 return 0;
237 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700238
Davide Pesavento59769b12017-11-12 23:52:06 -0500239 if (options.count("version") > 0) {
Junxiao Shi08e96312017-06-29 18:07:27 +0000240 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
241 return 0;
242 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700243
Junxiao Shi08e96312017-06-29 18:07:27 +0000244 if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty()) {
245 std::cerr << "ERROR: at least one --prefix or --all-faces-prefix must be specified"
246 << std::endl << std::endl;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400247 usage(std::cerr);
Junxiao Shi08e96312017-06-29 18:07:27 +0000248 return 2;
249 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700250
Junxiao Shi08e96312017-06-29 18:07:27 +0000251 if (m_whiteList.empty()) {
252 // Allow everything
253 m_whiteList.push_back(Network::getMaxRangeV4());
254 m_whiteList.push_back(Network::getMaxRangeV6());
255 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700256
Junxiao Shi08e96312017-06-29 18:07:27 +0000257 try {
258 startFetchingFaceStatusDataset();
259 startProcessing();
260 }
261 catch (const std::exception& e) {
Davide Pesavento97e33022019-02-14 16:00:50 -0500262 std::cerr << "ERROR: " << boost::diagnostic_information(e);
Davide Pesavento59769b12017-11-12 23:52:06 -0500263 return 1;
Junxiao Shi08e96312017-06-29 18:07:27 +0000264 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700265
266 return 0;
267 }
268
269private:
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400270 ndn::Face m_face;
271 ndn::KeyChain m_keyChain;
272 ndn::nfd::Controller m_controller{m_face, m_keyChain};
273 ndn::nfd::FaceMonitor m_faceMonitor{m_face};
Davide Pesavento22db5392017-04-14 00:56:43 -0400274 std::vector<Name> m_autoregPrefixes;
275 std::vector<Name> m_allFacesPrefixes;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400276 uint64_t m_cost = 255;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700277 std::vector<Network> m_whiteList;
278 std::vector<Network> m_blackList;
279};
280
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400281} // namespace nfd::tools::autoreg
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700282
283int
284main(int argc, char* argv[])
285{
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400286 nfd::tools::autoreg::AutoregServer server;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700287 return server.main(argc, argv);
288}