blob: 5d74e4dbeaeffbf95478c63b6e3c2bdcc1666c43 [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 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
Davide Pesavento59769b12017-11-12 23:52:06 -050026#include "core/extended-error-message.hpp"
Davide Pesaventoa997d292017-08-24 20:16:59 -040027#include "core/network.hpp"
28#include "core/version.hpp"
29
Alexander Afanasyev4a771362014-04-24 21:29:33 -070030#include <ndn-cxx/face.hpp>
31#include <ndn-cxx/name.hpp>
Junxiao Shi08e96312017-06-29 18:07:27 +000032#include <ndn-cxx/encoding/buffer-stream.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000033#include <ndn-cxx/mgmt/nfd/controller.hpp>
34#include <ndn-cxx/mgmt/nfd/face-monitor.hpp>
35#include <ndn-cxx/mgmt/nfd/face-status.hpp>
Yanbiao Liafb20592017-08-03 16:20:46 -070036#include <ndn-cxx/net/address-converter.hpp>
Junxiao Shi83be1da2017-06-30 13:37:37 +000037#include <ndn-cxx/net/face-uri.hpp>
Junxiao Shi08e96312017-06-29 18:07:27 +000038#include <ndn-cxx/security/key-chain.hpp>
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070039
40#include <boost/program_options/options_description.hpp>
41#include <boost/program_options/variables_map.hpp>
42#include <boost/program_options/parsers.hpp>
43
Davide Pesaventoa997d292017-08-24 20:16:59 -040044#include <iostream>
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070045
Junxiao Shia1937bf2014-11-06 11:43:40 -070046namespace ndn {
47namespace nfd_autoreg {
48
Junxiao Shi08e96312017-06-29 18:07:27 +000049using ::nfd::Network;
50
Junxiao Shidda0b462014-06-30 19:42:29 -070051class AutoregServer : boost::noncopyable
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070052{
53public:
54 AutoregServer()
Junxiao Shi8e273ca2014-11-12 00:42:29 -070055 : m_controller(m_face, m_keyChain)
Junxiao Shi15b12e72014-08-09 19:56:24 -070056 , m_faceMonitor(m_face)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070057 , m_cost(255)
58 {
59 }
60
61 void
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070062 onRegisterCommandSuccess(uint64_t faceId, const Name& prefix)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070063 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070064 std::cerr << "SUCCEED: register " << prefix << " on face " << faceId << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070065 }
66
67 void
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070068 onRegisterCommandFailure(uint64_t faceId, const Name& prefix,
Junxiao Shi29b41282016-08-22 03:47:02 +000069 const nfd::ControlResponse& response)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070070 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070071 std::cerr << "FAILED: register " << prefix << " on face " << faceId
Junxiao Shi29b41282016-08-22 03:47:02 +000072 << " (code: " << response.getCode() << ", reason: " << response.getText() << ")"
73 << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070074 }
75
Junxiao Shidda0b462014-06-30 19:42:29 -070076 /**
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070077 * \return true if uri has schema allowed to do auto-registrations
Junxiao Shidda0b462014-06-30 19:42:29 -070078 */
Davide Pesavento59769b12017-11-12 23:52:06 -050079 static bool
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070080 hasAllowedSchema(const FaceUri& uri)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070081 {
82 const std::string& scheme = uri.getScheme();
Davide Pesavento59769b12017-11-12 23:52:06 -050083 return scheme == "udp4" || scheme == "tcp4" ||
84 scheme == "udp6" || scheme == "tcp6";
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070085 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070086
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070087 /**
Alexander Afanasyevf056c112014-08-14 16:39:25 -070088 * \return true if address is blacklisted
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070089 */
90 bool
Davide Pesavento59769b12017-11-12 23:52:06 -050091 isBlacklisted(const boost::asio::ip::address& address) const
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070092 {
Junxiao Shi08e96312017-06-29 18:07:27 +000093 return std::any_of(m_blackList.begin(), m_blackList.end(),
94 bind(&Network::doesContain, _1, address));
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070095 }
96
97 /**
Alexander Afanasyevf056c112014-08-14 16:39:25 -070098 * \return true if address is whitelisted
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070099 */
100 bool
Davide Pesavento59769b12017-11-12 23:52:06 -0500101 isWhitelisted(const boost::asio::ip::address& address) const
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700102 {
Junxiao Shi08e96312017-06-29 18:07:27 +0000103 return std::any_of(m_whiteList.begin(), m_whiteList.end(),
104 bind(&Network::doesContain, _1, address));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700105 }
106
107 void
Davide Pesavento22db5392017-04-14 00:56:43 -0400108 registerPrefixesForFace(uint64_t faceId, const std::vector<Name>& prefixes)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700109 {
Junxiao Shi08e96312017-06-29 18:07:27 +0000110 for (const Name& prefix : prefixes) {
111 m_controller.start<nfd::RibRegisterCommand>(
112 nfd::ControlParameters()
113 .setName(prefix)
114 .setFaceId(faceId)
115 .setOrigin(nfd::ROUTE_ORIGIN_AUTOREG)
116 .setCost(m_cost)
117 .setExpirationPeriod(time::milliseconds::max()),
118 bind(&AutoregServer::onRegisterCommandSuccess, this, faceId, prefix),
119 bind(&AutoregServer::onRegisterCommandFailure, this, faceId, prefix, _1));
120 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700121 }
122
123 void
Junxiao Shi08e96312017-06-29 18:07:27 +0000124 registerPrefixesIfNeeded(uint64_t faceId, const FaceUri& uri, nfd::FacePersistency facePersistency)
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700125 {
126 if (hasAllowedSchema(uri)) {
127 boost::system::error_code ec;
Yanbiao Liafb20592017-08-03 16:20:46 -0700128 boost::asio::ip::address address = ndn::ip::addressFromString(uri.getHost(), ec);
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700129
130 if (!address.is_multicast()) {
131 // register all-face prefixes
132 registerPrefixesForFace(faceId, m_allFacesPrefixes);
133
134 // register autoreg prefixes if new face is on-demand and not blacklisted and whitelisted
Junxiao Shi08e96312017-06-29 18:07:27 +0000135 if (facePersistency == nfd::FACE_PERSISTENCY_ON_DEMAND &&
Chengyu Fan9942cea2014-10-13 14:47:13 -0600136 !isBlacklisted(address) && isWhitelisted(address)) {
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700137 registerPrefixesForFace(faceId, m_autoregPrefixes);
138 }
139 }
140 }
141 }
142
143 void
Junxiao Shi08e96312017-06-29 18:07:27 +0000144 onNotification(const nfd::FaceEventNotification& notification)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700145 {
Junxiao Shi08e96312017-06-29 18:07:27 +0000146 if (notification.getKind() == nfd::FACE_EVENT_CREATED &&
147 notification.getFaceScope() != nfd::FACE_SCOPE_LOCAL) {
148 std::cerr << "PROCESSING: " << notification << std::endl;
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700149
Junxiao Shi08e96312017-06-29 18:07:27 +0000150 registerPrefixesIfNeeded(notification.getFaceId(), FaceUri(notification.getRemoteUri()),
151 notification.getFacePersistency());
152 }
153 else {
154 std::cerr << "IGNORED: " << notification << std::endl;
155 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700156 }
157
158 void
159 signalHandler()
160 {
161 m_face.shutdown();
162 }
163
Davide Pesavento59769b12017-11-12 23:52:06 -0500164 static void
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700165 usage(std::ostream& os,
Davide Pesavento22db5392017-04-14 00:56:43 -0400166 const boost::program_options::options_description& desc,
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700167 const char* programName)
168 {
Davide Pesavento59769b12017-11-12 23:52:06 -0500169 os << "Usage: " << programName << " [--prefix=</autoreg/prefix>]... [options]\n"
170 << "\n"
171 << desc;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700172 }
173
174 void
175 startProcessing()
176 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700177 std::cerr << "AUTOREG prefixes: " << std::endl;
Junxiao Shi08e96312017-06-29 18:07:27 +0000178 for (const Name& prefix : m_autoregPrefixes) {
179 std::cout << " " << prefix << std::endl;
180 }
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700181 std::cerr << "ALL-FACES-AUTOREG prefixes: " << std::endl;
Junxiao Shi08e96312017-06-29 18:07:27 +0000182 for (const Name& prefix : m_allFacesPrefixes) {
183 std::cout << " " << prefix << std::endl;
184 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700185
Junxiao Shi08e96312017-06-29 18:07:27 +0000186 if (!m_blackList.empty()) {
187 std::cerr << "Blacklisted networks: " << std::endl;
188 for (const Network& network : m_blackList) {
189 std::cout << " " << network << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700190 }
Junxiao Shi08e96312017-06-29 18:07:27 +0000191 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700192
Junxiao Shidda0b462014-06-30 19:42:29 -0700193 std::cerr << "Whitelisted networks: " << std::endl;
Junxiao Shi08e96312017-06-29 18:07:27 +0000194 for (const Network& network : m_whiteList) {
195 std::cout << " " << network << std::endl;
196 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700197
Junxiao Shifbf78342015-01-23 14:46:41 -0700198 m_faceMonitor.onNotification.connect(bind(&AutoregServer::onNotification, this, _1));
Junxiao Shi15b12e72014-08-09 19:56:24 -0700199 m_faceMonitor.start();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700200
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700201 boost::asio::signal_set signalSet(m_face.getIoService(), SIGINT, SIGTERM);
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700202 signalSet.async_wait(bind(&AutoregServer::signalHandler, this));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700203
204 m_face.processEvents();
205 }
206
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700207 void
208 startFetchingFaceStatusDataset()
209 {
Junxiao Shi08e96312017-06-29 18:07:27 +0000210 m_controller.fetch<nfd::FaceDataset>(
211 [this] (const std::vector<nfd::FaceStatus>& faces) {
Davide Pesavento22db5392017-04-14 00:56:43 -0400212 for (const auto& faceStatus : faces) {
Weiwei Liu7c795132016-10-07 14:22:54 -0700213 registerPrefixesIfNeeded(faceStatus.getFaceId(), FaceUri(faceStatus.getRemoteUri()),
214 faceStatus.getFacePersistency());
215 }
216 },
Davide Pesaventoac238f22017-09-12 15:19:40 -0400217 [] (uint32_t code, const std::string& reason) {});
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700218 }
219
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700220 int
221 main(int argc, char* argv[])
222 {
Davide Pesavento22db5392017-04-14 00:56:43 -0400223 namespace po = boost::program_options;
224
Davide Pesavento59769b12017-11-12 23:52:06 -0500225 po::options_description optionsDesc("Options");
226 optionsDesc.add_options()
227 ("help,h", "print this message and exit")
228 ("version,V", "show version information and exit")
Davide Pesavento22db5392017-04-14 00:56:43 -0400229 ("prefix,i", po::value<std::vector<Name>>(&m_autoregPrefixes)->composing(),
Davide Pesavento59769b12017-11-12 23:52:06 -0500230 "prefix that should be automatically registered when a new non-local face is created")
Davide Pesavento22db5392017-04-14 00:56:43 -0400231 ("all-faces-prefix,a", po::value<std::vector<Name>>(&m_allFacesPrefixes)->composing(),
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700232 "prefix that should be automatically registered for all TCP and UDP non-local faces "
233 "(blacklists and whitelists do not apply to this prefix)")
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700234 ("cost,c", po::value<uint64_t>(&m_cost)->default_value(255),
Davide Pesavento59769b12017-11-12 23:52:06 -0500235 "FIB cost that should be assigned to autoreg nexthops")
Davide Pesavento22db5392017-04-14 00:56:43 -0400236 ("whitelist,w", po::value<std::vector<Network>>(&m_whiteList)->composing(),
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700237 "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
Davide Pesavento22db5392017-04-14 00:56:43 -0400238 ("blacklist,b", po::value<std::vector<Network>>(&m_blackList)->composing(),
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700239 "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
240 ;
241
242 po::variables_map options;
Junxiao Shi08e96312017-06-29 18:07:27 +0000243 try {
Davide Pesavento59769b12017-11-12 23:52:06 -0500244 po::store(po::parse_command_line(argc, argv, optionsDesc), options);
Junxiao Shi08e96312017-06-29 18:07:27 +0000245 po::notify(options);
246 }
247 catch (const std::exception& e) {
248 std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
Davide Pesavento59769b12017-11-12 23:52:06 -0500249 usage(std::cerr, optionsDesc, argv[0]);
250 return 2;
Junxiao Shi08e96312017-06-29 18:07:27 +0000251 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700252
Davide Pesavento59769b12017-11-12 23:52:06 -0500253 if (options.count("help") > 0) {
254 usage(std::cout, optionsDesc, argv[0]);
Junxiao Shi08e96312017-06-29 18:07:27 +0000255 return 0;
256 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700257
Davide Pesavento59769b12017-11-12 23:52:06 -0500258 if (options.count("version") > 0) {
Junxiao Shi08e96312017-06-29 18:07:27 +0000259 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
260 return 0;
261 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700262
Junxiao Shi08e96312017-06-29 18:07:27 +0000263 if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty()) {
264 std::cerr << "ERROR: at least one --prefix or --all-faces-prefix must be specified"
265 << std::endl << std::endl;
Davide Pesavento59769b12017-11-12 23:52:06 -0500266 usage(std::cerr, optionsDesc, argv[0]);
Junxiao Shi08e96312017-06-29 18:07:27 +0000267 return 2;
268 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700269
Junxiao Shi08e96312017-06-29 18:07:27 +0000270 if (m_whiteList.empty()) {
271 // Allow everything
272 m_whiteList.push_back(Network::getMaxRangeV4());
273 m_whiteList.push_back(Network::getMaxRangeV6());
274 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700275
Junxiao Shi08e96312017-06-29 18:07:27 +0000276 try {
277 startFetchingFaceStatusDataset();
278 startProcessing();
279 }
280 catch (const std::exception& e) {
Davide Pesavento59769b12017-11-12 23:52:06 -0500281 std::cerr << "ERROR: " << ::nfd::getExtendedErrorMessage(e) << std::endl;
282 return 1;
Junxiao Shi08e96312017-06-29 18:07:27 +0000283 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700284
285 return 0;
286 }
287
288private:
289 Face m_face;
Junxiao Shi8e273ca2014-11-12 00:42:29 -0700290 KeyChain m_keyChain;
Junxiao Shi08e96312017-06-29 18:07:27 +0000291 nfd::Controller m_controller;
292 nfd::FaceMonitor m_faceMonitor;
Davide Pesavento22db5392017-04-14 00:56:43 -0400293 std::vector<Name> m_autoregPrefixes;
294 std::vector<Name> m_allFacesPrefixes;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700295 uint64_t m_cost;
296 std::vector<Network> m_whiteList;
297 std::vector<Network> m_blackList;
298};
299
Junxiao Shia1937bf2014-11-06 11:43:40 -0700300} // namespace nfd_autoreg
301} // namespace ndn
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700302
303int
304main(int argc, char* argv[])
305{
Junxiao Shia1937bf2014-11-06 11:43:40 -0700306 ndn::nfd_autoreg::AutoregServer server;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700307 return server.main(argc, argv);
308}