blob: 3745cb0fc6c175d9235f3192cf8f50f8903d47aa [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 Pesavento412c9822021-07-02 00:21:05 -04003 * Copyright (c) 2014-2021, 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>
30#include <ndn-cxx/name.hpp>
Junxiao Shi08e96312017-06-29 18:07:27 +000031#include <ndn-cxx/encoding/buffer-stream.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000032#include <ndn-cxx/mgmt/nfd/controller.hpp>
33#include <ndn-cxx/mgmt/nfd/face-monitor.hpp>
34#include <ndn-cxx/mgmt/nfd/face-status.hpp>
Junxiao Shi83be1da2017-06-30 13:37:37 +000035#include <ndn-cxx/net/face-uri.hpp>
Junxiao Shi08e96312017-06-29 18:07:27 +000036#include <ndn-cxx/security/key-chain.hpp>
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070037
Davide Pesavento97e33022019-02-14 16:00:50 -050038#include <boost/exception/diagnostic_information.hpp>
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070039#include <boost/program_options/options_description.hpp>
40#include <boost/program_options/variables_map.hpp>
41#include <boost/program_options/parsers.hpp>
42
Davide Pesaventoa997d292017-08-24 20:16:59 -040043#include <iostream>
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070044
Junxiao Shia1937bf2014-11-06 11:43:40 -070045namespace ndn {
46namespace nfd_autoreg {
47
Junxiao Shi08e96312017-06-29 18:07:27 +000048using ::nfd::Network;
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 */
Davide Pesavento59769b12017-11-12 23:52:06 -050078 static 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();
Davide Pesavento59769b12017-11-12 23:52:06 -050082 return scheme == "udp4" || scheme == "tcp4" ||
83 scheme == "udp6" || scheme == "tcp6";
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070084 }
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
Davide Pesavento59769b12017-11-12 23:52:06 -050090 isBlacklisted(const boost::asio::ip::address& address) const
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070091 {
Junxiao Shi08e96312017-06-29 18:07:27 +000092 return std::any_of(m_blackList.begin(), m_blackList.end(),
Davide Pesavento412c9822021-07-02 00:21:05 -040093 std::bind(&Network::doesContain, _1, address));
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070094 }
95
96 /**
Alexander Afanasyevf056c112014-08-14 16:39:25 -070097 * \return true if address is whitelisted
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070098 */
99 bool
Davide Pesavento59769b12017-11-12 23:52:06 -0500100 isWhitelisted(const boost::asio::ip::address& address) const
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700101 {
Junxiao Shi08e96312017-06-29 18:07:27 +0000102 return std::any_of(m_whiteList.begin(), m_whiteList.end(),
Davide Pesavento412c9822021-07-02 00:21:05 -0400103 std::bind(&Network::doesContain, _1, address));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700104 }
105
106 void
Davide Pesavento22db5392017-04-14 00:56:43 -0400107 registerPrefixesForFace(uint64_t faceId, const std::vector<Name>& prefixes)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700108 {
Junxiao Shi08e96312017-06-29 18:07:27 +0000109 for (const Name& prefix : prefixes) {
110 m_controller.start<nfd::RibRegisterCommand>(
111 nfd::ControlParameters()
112 .setName(prefix)
113 .setFaceId(faceId)
114 .setOrigin(nfd::ROUTE_ORIGIN_AUTOREG)
115 .setCost(m_cost)
116 .setExpirationPeriod(time::milliseconds::max()),
Davide Pesavento412c9822021-07-02 00:21:05 -0400117 std::bind(&AutoregServer::onRegisterCommandSuccess, this, faceId, prefix),
118 std::bind(&AutoregServer::onRegisterCommandFailure, this, faceId, prefix, _1));
Junxiao Shi08e96312017-06-29 18:07:27 +0000119 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700120 }
121
122 void
Junxiao Shi08e96312017-06-29 18:07:27 +0000123 registerPrefixesIfNeeded(uint64_t faceId, const FaceUri& uri, nfd::FacePersistency facePersistency)
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700124 {
125 if (hasAllowedSchema(uri)) {
126 boost::system::error_code ec;
Davide Pesavento9c33b902018-05-20 01:30:29 -0400127 auto address = boost::asio::ip::address::from_string(uri.getHost(), ec);
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700128
129 if (!address.is_multicast()) {
130 // register all-face prefixes
131 registerPrefixesForFace(faceId, m_allFacesPrefixes);
132
133 // register autoreg prefixes if new face is on-demand and not blacklisted and whitelisted
Junxiao Shi08e96312017-06-29 18:07:27 +0000134 if (facePersistency == nfd::FACE_PERSISTENCY_ON_DEMAND &&
Chengyu Fan9942cea2014-10-13 14:47:13 -0600135 !isBlacklisted(address) && isWhitelisted(address)) {
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700136 registerPrefixesForFace(faceId, m_autoregPrefixes);
137 }
138 }
139 }
140 }
141
142 void
Junxiao Shi08e96312017-06-29 18:07:27 +0000143 onNotification(const nfd::FaceEventNotification& notification)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700144 {
Junxiao Shi08e96312017-06-29 18:07:27 +0000145 if (notification.getKind() == nfd::FACE_EVENT_CREATED &&
146 notification.getFaceScope() != nfd::FACE_SCOPE_LOCAL) {
147 std::cerr << "PROCESSING: " << notification << std::endl;
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700148
Junxiao Shi08e96312017-06-29 18:07:27 +0000149 registerPrefixesIfNeeded(notification.getFaceId(), FaceUri(notification.getRemoteUri()),
150 notification.getFacePersistency());
151 }
152 else {
153 std::cerr << "IGNORED: " << notification << std::endl;
154 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700155 }
156
Davide Pesavento59769b12017-11-12 23:52:06 -0500157 static void
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700158 usage(std::ostream& os,
Davide Pesavento22db5392017-04-14 00:56:43 -0400159 const boost::program_options::options_description& desc,
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700160 const char* programName)
161 {
Davide Pesavento59769b12017-11-12 23:52:06 -0500162 os << "Usage: " << programName << " [--prefix=</autoreg/prefix>]... [options]\n"
163 << "\n"
164 << desc;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700165 }
166
167 void
168 startProcessing()
169 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700170 std::cerr << "AUTOREG prefixes: " << std::endl;
Junxiao Shi08e96312017-06-29 18:07:27 +0000171 for (const Name& prefix : m_autoregPrefixes) {
172 std::cout << " " << prefix << std::endl;
173 }
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700174 std::cerr << "ALL-FACES-AUTOREG prefixes: " << std::endl;
Junxiao Shi08e96312017-06-29 18:07:27 +0000175 for (const Name& prefix : m_allFacesPrefixes) {
176 std::cout << " " << prefix << std::endl;
177 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700178
Junxiao Shi08e96312017-06-29 18:07:27 +0000179 if (!m_blackList.empty()) {
180 std::cerr << "Blacklisted networks: " << std::endl;
181 for (const Network& network : m_blackList) {
182 std::cout << " " << network << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700183 }
Junxiao Shi08e96312017-06-29 18:07:27 +0000184 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700185
Junxiao Shidda0b462014-06-30 19:42:29 -0700186 std::cerr << "Whitelisted networks: " << std::endl;
Junxiao Shi08e96312017-06-29 18:07:27 +0000187 for (const Network& network : m_whiteList) {
188 std::cout << " " << network << std::endl;
189 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700190
Davide Pesavento412c9822021-07-02 00:21:05 -0400191 m_faceMonitor.onNotification.connect(std::bind(&AutoregServer::onNotification, this, _1));
Junxiao Shi15b12e72014-08-09 19:56:24 -0700192 m_faceMonitor.start();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700193
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700194 boost::asio::signal_set signalSet(m_face.getIoService(), SIGINT, SIGTERM);
Davide Pesavento412c9822021-07-02 00:21:05 -0400195 signalSet.async_wait([this] (auto&&...) { m_face.shutdown(); });
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700196
197 m_face.processEvents();
198 }
199
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700200 void
201 startFetchingFaceStatusDataset()
202 {
Junxiao Shi08e96312017-06-29 18:07:27 +0000203 m_controller.fetch<nfd::FaceDataset>(
Davide Pesavento412c9822021-07-02 00:21:05 -0400204 [this] (const auto& faces) {
Davide Pesavento22db5392017-04-14 00:56:43 -0400205 for (const auto& faceStatus : faces) {
Weiwei Liu7c795132016-10-07 14:22:54 -0700206 registerPrefixesIfNeeded(faceStatus.getFaceId(), FaceUri(faceStatus.getRemoteUri()),
207 faceStatus.getFacePersistency());
208 }
209 },
Davide Pesavento412c9822021-07-02 00:21:05 -0400210 [] (auto&&...) {});
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700211 }
212
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700213 int
214 main(int argc, char* argv[])
215 {
Davide Pesavento22db5392017-04-14 00:56:43 -0400216 namespace po = boost::program_options;
217
Davide Pesavento59769b12017-11-12 23:52:06 -0500218 po::options_description optionsDesc("Options");
219 optionsDesc.add_options()
220 ("help,h", "print this message and exit")
221 ("version,V", "show version information and exit")
Davide Pesavento22db5392017-04-14 00:56:43 -0400222 ("prefix,i", po::value<std::vector<Name>>(&m_autoregPrefixes)->composing(),
Davide Pesavento59769b12017-11-12 23:52:06 -0500223 "prefix that should be automatically registered when a new non-local face is created")
Davide Pesavento22db5392017-04-14 00:56:43 -0400224 ("all-faces-prefix,a", po::value<std::vector<Name>>(&m_allFacesPrefixes)->composing(),
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700225 "prefix that should be automatically registered for all TCP and UDP non-local faces "
226 "(blacklists and whitelists do not apply to this prefix)")
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700227 ("cost,c", po::value<uint64_t>(&m_cost)->default_value(255),
Davide Pesavento59769b12017-11-12 23:52:06 -0500228 "FIB cost that should be assigned to autoreg nexthops")
Davide Pesavento22db5392017-04-14 00:56:43 -0400229 ("whitelist,w", po::value<std::vector<Network>>(&m_whiteList)->composing(),
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700230 "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
Davide Pesavento22db5392017-04-14 00:56:43 -0400231 ("blacklist,b", po::value<std::vector<Network>>(&m_blackList)->composing(),
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700232 "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
233 ;
234
235 po::variables_map options;
Junxiao Shi08e96312017-06-29 18:07:27 +0000236 try {
Davide Pesavento59769b12017-11-12 23:52:06 -0500237 po::store(po::parse_command_line(argc, argv, optionsDesc), options);
Junxiao Shi08e96312017-06-29 18:07:27 +0000238 po::notify(options);
239 }
240 catch (const std::exception& e) {
241 std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
Davide Pesavento59769b12017-11-12 23:52:06 -0500242 usage(std::cerr, optionsDesc, argv[0]);
243 return 2;
Junxiao Shi08e96312017-06-29 18:07:27 +0000244 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700245
Davide Pesavento59769b12017-11-12 23:52:06 -0500246 if (options.count("help") > 0) {
247 usage(std::cout, optionsDesc, argv[0]);
Junxiao Shi08e96312017-06-29 18:07:27 +0000248 return 0;
249 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700250
Davide Pesavento59769b12017-11-12 23:52:06 -0500251 if (options.count("version") > 0) {
Junxiao Shi08e96312017-06-29 18:07:27 +0000252 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
253 return 0;
254 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700255
Junxiao Shi08e96312017-06-29 18:07:27 +0000256 if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty()) {
257 std::cerr << "ERROR: at least one --prefix or --all-faces-prefix must be specified"
258 << std::endl << std::endl;
Davide Pesavento59769b12017-11-12 23:52:06 -0500259 usage(std::cerr, optionsDesc, argv[0]);
Junxiao Shi08e96312017-06-29 18:07:27 +0000260 return 2;
261 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700262
Junxiao Shi08e96312017-06-29 18:07:27 +0000263 if (m_whiteList.empty()) {
264 // Allow everything
265 m_whiteList.push_back(Network::getMaxRangeV4());
266 m_whiteList.push_back(Network::getMaxRangeV6());
267 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700268
Junxiao Shi08e96312017-06-29 18:07:27 +0000269 try {
270 startFetchingFaceStatusDataset();
271 startProcessing();
272 }
273 catch (const std::exception& e) {
Davide Pesavento97e33022019-02-14 16:00:50 -0500274 std::cerr << "ERROR: " << boost::diagnostic_information(e);
Davide Pesavento59769b12017-11-12 23:52:06 -0500275 return 1;
Junxiao Shi08e96312017-06-29 18:07:27 +0000276 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700277
278 return 0;
279 }
280
281private:
282 Face m_face;
Junxiao Shi8e273ca2014-11-12 00:42:29 -0700283 KeyChain m_keyChain;
Junxiao Shi08e96312017-06-29 18:07:27 +0000284 nfd::Controller m_controller;
285 nfd::FaceMonitor m_faceMonitor;
Davide Pesavento22db5392017-04-14 00:56:43 -0400286 std::vector<Name> m_autoregPrefixes;
287 std::vector<Name> m_allFacesPrefixes;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700288 uint64_t m_cost;
289 std::vector<Network> m_whiteList;
290 std::vector<Network> m_blackList;
291};
292
Junxiao Shia1937bf2014-11-06 11:43:40 -0700293} // namespace nfd_autoreg
294} // namespace ndn
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700295
296int
297main(int argc, char* argv[])
298{
Junxiao Shia1937bf2014-11-06 11:43:40 -0700299 ndn::nfd_autoreg::AutoregServer server;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700300 return server.main(argc, argv);
301}