blob: 042248aad2c7f5df5f65024e02aa7d7d38593f3e [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 Shi08e96312017-06-29 18:07:27 +000029#include <ndn-cxx/encoding/buffer-stream.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>
32#include <ndn-cxx/mgmt/nfd/face-status.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
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
Junxiao Shia1937bf2014-11-06 11:43:40 -070043namespace ndn {
44namespace nfd_autoreg {
45
Junxiao Shi08e96312017-06-29 18:07:27 +000046using ::nfd::Network;
47
Junxiao Shidda0b462014-06-30 19:42:29 -070048class AutoregServer : boost::noncopyable
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070049{
50public:
51 AutoregServer()
Junxiao Shi8e273ca2014-11-12 00:42:29 -070052 : m_controller(m_face, m_keyChain)
Junxiao Shi15b12e72014-08-09 19:56:24 -070053 , m_faceMonitor(m_face)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070054 , m_cost(255)
55 {
56 }
57
58 void
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070059 onRegisterCommandSuccess(uint64_t faceId, const Name& prefix)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070060 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070061 std::cerr << "SUCCEED: register " << prefix << " on face " << faceId << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070062 }
63
64 void
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070065 onRegisterCommandFailure(uint64_t faceId, const Name& prefix,
Junxiao Shi29b41282016-08-22 03:47:02 +000066 const nfd::ControlResponse& response)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070067 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070068 std::cerr << "FAILED: register " << prefix << " on face " << faceId
Junxiao Shi29b41282016-08-22 03:47:02 +000069 << " (code: " << response.getCode() << ", reason: " << response.getText() << ")"
70 << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070071 }
72
Junxiao Shidda0b462014-06-30 19:42:29 -070073 /**
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070074 * \return true if uri has schema allowed to do auto-registrations
Junxiao Shidda0b462014-06-30 19:42:29 -070075 */
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070076 bool
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070077 hasAllowedSchema(const FaceUri& uri)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070078 {
79 const std::string& scheme = uri.getScheme();
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070080 return (scheme == "udp4" || scheme == "tcp4" ||
81 scheme == "udp6" || scheme == "tcp6");
82 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070083
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070084 /**
Alexander Afanasyevf056c112014-08-14 16:39:25 -070085 * \return true if address is blacklisted
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070086 */
87 bool
Alexander Afanasyevf056c112014-08-14 16:39:25 -070088 isBlacklisted(const boost::asio::ip::address& address)
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070089 {
Junxiao Shi08e96312017-06-29 18:07:27 +000090 return std::any_of(m_blackList.begin(), m_blackList.end(),
91 bind(&Network::doesContain, _1, address));
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070092 }
93
94 /**
Alexander Afanasyevf056c112014-08-14 16:39:25 -070095 * \return true if address is whitelisted
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070096 */
97 bool
Alexander Afanasyevf056c112014-08-14 16:39:25 -070098 isWhitelisted(const boost::asio::ip::address& address)
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070099 {
Junxiao Shi08e96312017-06-29 18:07:27 +0000100 return std::any_of(m_whiteList.begin(), m_whiteList.end(),
101 bind(&Network::doesContain, _1, address));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700102 }
103
104 void
Davide Pesavento22db5392017-04-14 00:56:43 -0400105 registerPrefixesForFace(uint64_t faceId, const std::vector<Name>& prefixes)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700106 {
Junxiao Shi08e96312017-06-29 18:07:27 +0000107 for (const Name& prefix : prefixes) {
108 m_controller.start<nfd::RibRegisterCommand>(
109 nfd::ControlParameters()
110 .setName(prefix)
111 .setFaceId(faceId)
112 .setOrigin(nfd::ROUTE_ORIGIN_AUTOREG)
113 .setCost(m_cost)
114 .setExpirationPeriod(time::milliseconds::max()),
115 bind(&AutoregServer::onRegisterCommandSuccess, this, faceId, prefix),
116 bind(&AutoregServer::onRegisterCommandFailure, this, faceId, prefix, _1));
117 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700118 }
119
120 void
Junxiao Shi08e96312017-06-29 18:07:27 +0000121 registerPrefixesIfNeeded(uint64_t faceId, const FaceUri& uri, nfd::FacePersistency facePersistency)
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700122 {
123 if (hasAllowedSchema(uri)) {
124 boost::system::error_code ec;
125 boost::asio::ip::address address = boost::asio::ip::address::from_string(uri.getHost(), ec);
126
127 if (!address.is_multicast()) {
128 // register all-face prefixes
129 registerPrefixesForFace(faceId, m_allFacesPrefixes);
130
131 // register autoreg prefixes if new face is on-demand and not blacklisted and whitelisted
Junxiao Shi08e96312017-06-29 18:07:27 +0000132 if (facePersistency == nfd::FACE_PERSISTENCY_ON_DEMAND &&
Chengyu Fan9942cea2014-10-13 14:47:13 -0600133 !isBlacklisted(address) && isWhitelisted(address)) {
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700134 registerPrefixesForFace(faceId, m_autoregPrefixes);
135 }
136 }
137 }
138 }
139
140 void
Junxiao Shi08e96312017-06-29 18:07:27 +0000141 onNotification(const nfd::FaceEventNotification& notification)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700142 {
Junxiao Shi08e96312017-06-29 18:07:27 +0000143 if (notification.getKind() == nfd::FACE_EVENT_CREATED &&
144 notification.getFaceScope() != nfd::FACE_SCOPE_LOCAL) {
145 std::cerr << "PROCESSING: " << notification << std::endl;
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700146
Junxiao Shi08e96312017-06-29 18:07:27 +0000147 registerPrefixesIfNeeded(notification.getFaceId(), FaceUri(notification.getRemoteUri()),
148 notification.getFacePersistency());
149 }
150 else {
151 std::cerr << "IGNORED: " << notification << std::endl;
152 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700153 }
154
155 void
156 signalHandler()
157 {
158 m_face.shutdown();
159 }
160
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700161 void
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700162 usage(std::ostream& os,
Davide Pesavento22db5392017-04-14 00:56:43 -0400163 const boost::program_options::options_description& desc,
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700164 const char* programName)
165 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700166 os << "Usage:\n"
167 << " " << programName << " --prefix=</autoreg/prefix> [--prefix=/another/prefix] ...\n"
168 << "\n";
Davide Pesavento22db5392017-04-14 00:56:43 -0400169 os << desc;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700170 }
171
172 void
173 startProcessing()
174 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700175 std::cerr << "AUTOREG prefixes: " << std::endl;
Junxiao Shi08e96312017-06-29 18:07:27 +0000176 for (const Name& prefix : m_autoregPrefixes) {
177 std::cout << " " << prefix << std::endl;
178 }
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700179 std::cerr << "ALL-FACES-AUTOREG prefixes: " << std::endl;
Junxiao Shi08e96312017-06-29 18:07:27 +0000180 for (const Name& prefix : m_allFacesPrefixes) {
181 std::cout << " " << prefix << std::endl;
182 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700183
Junxiao Shi08e96312017-06-29 18:07:27 +0000184 if (!m_blackList.empty()) {
185 std::cerr << "Blacklisted networks: " << std::endl;
186 for (const Network& network : m_blackList) {
187 std::cout << " " << network << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700188 }
Junxiao Shi08e96312017-06-29 18:07:27 +0000189 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700190
Junxiao Shidda0b462014-06-30 19:42:29 -0700191 std::cerr << "Whitelisted networks: " << std::endl;
Junxiao Shi08e96312017-06-29 18:07:27 +0000192 for (const Network& network : m_whiteList) {
193 std::cout << " " << network << std::endl;
194 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700195
Junxiao Shifbf78342015-01-23 14:46:41 -0700196 m_faceMonitor.onNotification.connect(bind(&AutoregServer::onNotification, this, _1));
Junxiao Shi15b12e72014-08-09 19:56:24 -0700197 m_faceMonitor.start();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700198
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700199 boost::asio::signal_set signalSet(m_face.getIoService(), SIGINT, SIGTERM);
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700200 signalSet.async_wait(bind(&AutoregServer::signalHandler, this));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700201
202 m_face.processEvents();
203 }
204
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700205 void
206 startFetchingFaceStatusDataset()
207 {
Junxiao Shi08e96312017-06-29 18:07:27 +0000208 m_controller.fetch<nfd::FaceDataset>(
209 [this] (const std::vector<nfd::FaceStatus>& faces) {
Davide Pesavento22db5392017-04-14 00:56:43 -0400210 for (const auto& faceStatus : faces) {
Weiwei Liu7c795132016-10-07 14:22:54 -0700211 registerPrefixesIfNeeded(faceStatus.getFaceId(), FaceUri(faceStatus.getRemoteUri()),
212 faceStatus.getFacePersistency());
213 }
214 },
215 [this] (uint32_t code, const std::string& reason) {});
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700216 }
217
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700218 int
219 main(int argc, char* argv[])
220 {
Davide Pesavento22db5392017-04-14 00:56:43 -0400221 namespace po = boost::program_options;
222
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700223 po::options_description optionDesciption;
224 optionDesciption.add_options()
225 ("help,h", "produce help message")
Davide Pesavento22db5392017-04-14 00:56:43 -0400226 ("prefix,i", po::value<std::vector<Name>>(&m_autoregPrefixes)->composing(),
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700227 "prefix that should be automatically registered when new a remote non-local face is "
228 "established")
Davide Pesavento22db5392017-04-14 00:56:43 -0400229 ("all-faces-prefix,a", po::value<std::vector<Name>>(&m_allFacesPrefixes)->composing(),
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700230 "prefix that should be automatically registered for all TCP and UDP non-local faces "
231 "(blacklists and whitelists do not apply to this prefix)")
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700232 ("cost,c", po::value<uint64_t>(&m_cost)->default_value(255),
233 "FIB cost which should be assigned to autoreg nexthops")
Davide Pesavento22db5392017-04-14 00:56:43 -0400234 ("whitelist,w", po::value<std::vector<Network>>(&m_whiteList)->composing(),
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700235 "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
Davide Pesavento22db5392017-04-14 00:56:43 -0400236 ("blacklist,b", po::value<std::vector<Network>>(&m_blackList)->composing(),
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700237 "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700238 ("version,V", "show version and exit")
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700239 ;
240
241 po::variables_map options;
Junxiao Shi08e96312017-06-29 18:07:27 +0000242 try {
243 po::store(po::command_line_parser(argc, argv).options(optionDesciption).run(), options);
244 po::notify(options);
245 }
246 catch (const std::exception& e) {
247 std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
248 usage(std::cerr, optionDesciption, argv[0]);
249 return 1;
250 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700251
Junxiao Shi08e96312017-06-29 18:07:27 +0000252 if (options.count("help")) {
253 usage(std::cout, optionDesciption, argv[0]);
254 return 0;
255 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700256
Junxiao Shi08e96312017-06-29 18:07:27 +0000257 if (options.count("version")) {
258 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
259 return 0;
260 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700261
Junxiao Shi08e96312017-06-29 18:07:27 +0000262 if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty()) {
263 std::cerr << "ERROR: at least one --prefix or --all-faces-prefix must be specified"
264 << std::endl << std::endl;
265 usage(std::cerr, optionDesciption, argv[0]);
266 return 2;
267 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700268
Junxiao Shi08e96312017-06-29 18:07:27 +0000269 if (m_whiteList.empty()) {
270 // Allow everything
271 m_whiteList.push_back(Network::getMaxRangeV4());
272 m_whiteList.push_back(Network::getMaxRangeV6());
273 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700274
Junxiao Shi08e96312017-06-29 18:07:27 +0000275 try {
276 startFetchingFaceStatusDataset();
277 startProcessing();
278 }
279 catch (const std::exception& e) {
280 std::cerr << "ERROR: " << e.what() << std::endl;
281 return 2;
282 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700283
284 return 0;
285 }
286
287private:
288 Face m_face;
Junxiao Shi8e273ca2014-11-12 00:42:29 -0700289 KeyChain m_keyChain;
Junxiao Shi08e96312017-06-29 18:07:27 +0000290 nfd::Controller m_controller;
291 nfd::FaceMonitor m_faceMonitor;
Davide Pesavento22db5392017-04-14 00:56:43 -0400292 std::vector<Name> m_autoregPrefixes;
293 std::vector<Name> m_allFacesPrefixes;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700294 uint64_t m_cost;
295 std::vector<Network> m_whiteList;
296 std::vector<Network> m_blackList;
297};
298
Junxiao Shia1937bf2014-11-06 11:43:40 -0700299} // namespace nfd_autoreg
300} // namespace ndn
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700301
302int
303main(int argc, char* argv[])
304{
Junxiao Shia1937bf2014-11-06 11:43:40 -0700305 ndn::nfd_autoreg::AutoregServer server;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700306 return server.main(argc, argv);
307}