blob: 1ecd6e54c81de7deb222af7b6c7522405dd4effc [file] [log] [blame]
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi9f5b01d2016-08-05 03:54:28 +00003 * Copyright (c) 2014-2016, 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;
44using ndn::Face;
Junxiao Shi8e273ca2014-11-12 00:42:29 -070045using ndn::KeyChain;
Alexander Afanasyev585e5a62014-08-12 11:49:31 -070046using ndn::nfd::FaceEventNotification;
Junxiao Shia1937bf2014-11-06 11:43:40 -070047using ndn::util::FaceUri;
48using ::nfd::Network;
49
50namespace ndn {
51namespace nfd_autoreg {
52
53namespace po = boost::program_options;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070054
Junxiao Shidda0b462014-06-30 19:42:29 -070055class AutoregServer : boost::noncopyable
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070056{
57public:
58 AutoregServer()
Junxiao Shi8e273ca2014-11-12 00:42:29 -070059 : m_controller(m_face, m_keyChain)
Junxiao Shi15b12e72014-08-09 19:56:24 -070060 , m_faceMonitor(m_face)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070061 , m_cost(255)
62 {
63 }
64
65 void
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070066 onRegisterCommandSuccess(uint64_t faceId, const Name& prefix)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070067 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070068 std::cerr << "SUCCEED: register " << prefix << " on face " << faceId << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070069 }
70
71 void
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070072 onRegisterCommandFailure(uint64_t faceId, const Name& prefix,
Junxiao Shi29b41282016-08-22 03:47:02 +000073 const nfd::ControlResponse& response)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070074 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070075 std::cerr << "FAILED: register " << prefix << " on face " << faceId
Junxiao Shi29b41282016-08-22 03:47:02 +000076 << " (code: " << response.getCode() << ", reason: " << response.getText() << ")"
77 << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070078 }
79
Junxiao Shidda0b462014-06-30 19:42:29 -070080 /**
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070081 * \return true if uri has schema allowed to do auto-registrations
Junxiao Shidda0b462014-06-30 19:42:29 -070082 */
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070083 bool
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070084 hasAllowedSchema(const FaceUri& uri)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070085 {
86 const std::string& scheme = uri.getScheme();
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070087 return (scheme == "udp4" || scheme == "tcp4" ||
88 scheme == "udp6" || scheme == "tcp6");
89 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070090
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070091 /**
Alexander Afanasyevf056c112014-08-14 16:39:25 -070092 * \return true if address is blacklisted
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070093 */
94 bool
Alexander Afanasyevf056c112014-08-14 16:39:25 -070095 isBlacklisted(const boost::asio::ip::address& address)
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070096 {
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070097 for (std::vector<Network>::const_iterator network = m_blackList.begin();
98 network != m_blackList.end();
99 ++network)
100 {
101 if (network->doesContain(address))
102 return true;
103 }
104
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700105 return false;
106 }
107
108 /**
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700109 * \return true if address is whitelisted
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700110 */
111 bool
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700112 isWhitelisted(const boost::asio::ip::address& address)
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700113 {
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700114 for (std::vector<Network>::const_iterator network = m_whiteList.begin();
115 network != m_whiteList.end();
116 ++network)
117 {
118 if (network->doesContain(address))
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700119 return true;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700120 }
121
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700122 return false;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700123 }
124
125 void
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700126 registerPrefixesForFace(uint64_t faceId,
127 const std::vector<ndn::Name>& prefixes)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700128 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700129 for (std::vector<ndn::Name>::const_iterator prefix = prefixes.begin();
130 prefix != prefixes.end();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700131 ++prefix)
132 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700133 m_controller.start<RibRegisterCommand>(
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700134 ControlParameters()
135 .setName(*prefix)
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700136 .setFaceId(faceId)
Junxiao Shidda0b462014-06-30 19:42:29 -0700137 .setOrigin(ROUTE_ORIGIN_AUTOREG)
138 .setCost(m_cost)
139 .setExpirationPeriod(time::milliseconds::max()),
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700140 bind(&AutoregServer::onRegisterCommandSuccess, this, faceId, *prefix),
Junxiao Shi29b41282016-08-22 03:47:02 +0000141 bind(&AutoregServer::onRegisterCommandFailure, this, faceId, *prefix, _1));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700142 }
143 }
144
145 void
Chengyu Fan9942cea2014-10-13 14:47:13 -0600146 registerPrefixesIfNeeded(uint64_t faceId, const FaceUri& uri, FacePersistency facePersistency)
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700147 {
148 if (hasAllowedSchema(uri)) {
149 boost::system::error_code ec;
150 boost::asio::ip::address address = boost::asio::ip::address::from_string(uri.getHost(), ec);
151
152 if (!address.is_multicast()) {
153 // register all-face prefixes
154 registerPrefixesForFace(faceId, m_allFacesPrefixes);
155
156 // register autoreg prefixes if new face is on-demand and not blacklisted and whitelisted
Chengyu Fan9942cea2014-10-13 14:47:13 -0600157 if (facePersistency == FACE_PERSISTENCY_ON_DEMAND &&
158 !isBlacklisted(address) && isWhitelisted(address)) {
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700159 registerPrefixesForFace(faceId, m_autoregPrefixes);
160 }
161 }
162 }
163 }
164
165 void
Junxiao Shi15b12e72014-08-09 19:56:24 -0700166 onNotification(const FaceEventNotification& notification)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700167 {
Junxiao Shi6e694322014-04-03 10:27:13 -0700168 if (notification.getKind() == FACE_EVENT_CREATED &&
Chengyu Fan9942cea2014-10-13 14:47:13 -0600169 notification.getFaceScope() != FACE_SCOPE_LOCAL)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700170 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700171 std::cerr << "PROCESSING: " << notification << std::endl;
172
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700173 registerPrefixesIfNeeded(notification.getFaceId(), FaceUri(notification.getRemoteUri()),
Chengyu Fan9942cea2014-10-13 14:47:13 -0600174 notification.getFacePersistency());
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700175 }
176 else
177 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700178 std::cerr << "IGNORED: " << notification << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700179 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700180 }
181
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700182
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700183 void
184 signalHandler()
185 {
186 m_face.shutdown();
187 }
188
189
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700190 void
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700191 usage(std::ostream& os,
192 const po::options_description& optionDesciption,
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700193 const char* programName)
194 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700195 os << "Usage:\n"
196 << " " << programName << " --prefix=</autoreg/prefix> [--prefix=/another/prefix] ...\n"
197 << "\n";
198 os << optionDesciption;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700199 }
200
201 void
202 startProcessing()
203 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700204 std::cerr << "AUTOREG prefixes: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700205 for (std::vector<ndn::Name>::const_iterator prefix = m_autoregPrefixes.begin();
206 prefix != m_autoregPrefixes.end();
207 ++prefix)
208 {
209 std::cout << " " << *prefix << std::endl;
210 }
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700211 std::cerr << "ALL-FACES-AUTOREG prefixes: " << std::endl;
212 for (std::vector<ndn::Name>::const_iterator prefix = m_allFacesPrefixes.begin();
213 prefix != m_allFacesPrefixes.end();
214 ++prefix)
215 {
216 std::cout << " " << *prefix << std::endl;
217 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700218
219 if (!m_blackList.empty())
220 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700221 std::cerr << "Blacklisted networks: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700222 for (std::vector<Network>::const_iterator network = m_blackList.begin();
223 network != m_blackList.end();
224 ++network)
225 {
226 std::cout << " " << *network << std::endl;
227 }
228 }
229
Junxiao Shidda0b462014-06-30 19:42:29 -0700230 std::cerr << "Whitelisted networks: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700231 for (std::vector<Network>::const_iterator network = m_whiteList.begin();
232 network != m_whiteList.end();
233 ++network)
234 {
235 std::cout << " " << *network << std::endl;
236 }
237
Junxiao Shifbf78342015-01-23 14:46:41 -0700238 m_faceMonitor.onNotification.connect(bind(&AutoregServer::onNotification, this, _1));
Junxiao Shi15b12e72014-08-09 19:56:24 -0700239 m_faceMonitor.start();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700240
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700241 boost::asio::signal_set signalSet(m_face.getIoService(), SIGINT, SIGTERM);
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700242 signalSet.async_wait(bind(&AutoregServer::signalHandler, this));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700243
244 m_face.processEvents();
245 }
246
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700247 void
248 startFetchingFaceStatusDataset()
249 {
Weiwei Liu7c795132016-10-07 14:22:54 -0700250 m_controller.fetch<FaceDataset>(
251 [this] (const std::vector<ndn::nfd::FaceStatus>& faces) {
252 for (const ndn::nfd::FaceStatus& faceStatus : faces) {
253 registerPrefixesIfNeeded(faceStatus.getFaceId(), FaceUri(faceStatus.getRemoteUri()),
254 faceStatus.getFacePersistency());
255 }
256 },
257 [this] (uint32_t code, const std::string& reason) {});
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700258 }
259
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700260 int
261 main(int argc, char* argv[])
262 {
263 po::options_description optionDesciption;
264 optionDesciption.add_options()
265 ("help,h", "produce help message")
266 ("prefix,i", po::value<std::vector<ndn::Name> >(&m_autoregPrefixes)->composing(),
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700267 "prefix that should be automatically registered when new a remote non-local face is "
268 "established")
269 ("all-faces-prefix,a", po::value<std::vector<ndn::Name> >(&m_allFacesPrefixes)->composing(),
270 "prefix that should be automatically registered for all TCP and UDP non-local faces "
271 "(blacklists and whitelists do not apply to this prefix)")
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700272 ("cost,c", po::value<uint64_t>(&m_cost)->default_value(255),
273 "FIB cost which should be assigned to autoreg nexthops")
274 ("whitelist,w", po::value<std::vector<Network> >(&m_whiteList)->composing(),
275 "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
276 ("blacklist,b", po::value<std::vector<Network> >(&m_blackList)->composing(),
277 "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700278 ("version,V", "show version and exit")
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700279 ;
280
281 po::variables_map options;
282 try
283 {
284 po::store(po::command_line_parser(argc, argv).options(optionDesciption).run(), options);
285 po::notify(options);
286 }
287 catch (std::exception& e)
288 {
289 std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700290 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700291 return 1;
292 }
293
294 if (options.count("help"))
295 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700296 usage(std::cout, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700297 return 0;
298 }
299
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700300 if (options.count("version"))
301 {
302 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
303 return 0;
304 }
305
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700306 if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty())
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700307 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700308 std::cerr << "ERROR: at least one --prefix or --all-faces-prefix must be specified"
309 << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700310 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700311 return 2;
312 }
313
314 if (m_whiteList.empty())
315 {
316 // Allow everything
317 m_whiteList.push_back(Network::getMaxRangeV4());
318 m_whiteList.push_back(Network::getMaxRangeV6());
319 }
320
321 try
322 {
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700323 startFetchingFaceStatusDataset();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700324 startProcessing();
325 }
326 catch (std::exception& e)
327 {
328 std::cerr << "ERROR: " << e.what() << std::endl;
329 return 2;
330 }
331
332 return 0;
333 }
334
335private:
336 Face m_face;
Junxiao Shi8e273ca2014-11-12 00:42:29 -0700337 KeyChain m_keyChain;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700338 Controller m_controller;
Junxiao Shi15b12e72014-08-09 19:56:24 -0700339 FaceMonitor m_faceMonitor;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700340 std::vector<ndn::Name> m_autoregPrefixes;
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700341 std::vector<ndn::Name> m_allFacesPrefixes;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700342 uint64_t m_cost;
343 std::vector<Network> m_whiteList;
344 std::vector<Network> m_blackList;
345};
346
Junxiao Shia1937bf2014-11-06 11:43:40 -0700347} // namespace nfd_autoreg
348} // namespace ndn
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700349
350int
351main(int argc, char* argv[])
352{
Junxiao Shia1937bf2014-11-06 11:43:40 -0700353 ndn::nfd_autoreg::AutoregServer server;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700354 return server.main(argc, argv);
355}