blob: 996c03c17b590649fee3d75e25268c91fbcc4f0d [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>
Alexander Afanasyev4a771362014-04-24 21:29:33 -070031#include <ndn-cxx/management/nfd-controller.hpp>
Alexander Afanasyev585e5a62014-08-12 11:49:31 -070032#include <ndn-cxx/management/nfd-face-monitor.hpp>
Alexander Afanasyevf056c112014-08-14 16:39:25 -070033#include <ndn-cxx/management/nfd-face-status.hpp>
34#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
248 void
249 fetchFaceStatusSegments(const Data& data, const shared_ptr<ndn::OBufferStream>& buffer)
250 {
251 buffer->write(reinterpret_cast<const char*>(data.getContent().value()),
252 data.getContent().value_size());
253
254 uint64_t currentSegment = data.getName().get(-1).toSegment();
255
256 const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
Junxiao Shia1937bf2014-11-06 11:43:40 -0700257 if (finalBlockId.empty() || finalBlockId.toSegment() > currentSegment) {
258 m_face.expressInterest(data.getName().getPrefix(-1).appendSegment(currentSegment + 1),
259 bind(&AutoregServer::fetchFaceStatusSegments, this, _2, buffer),
260 ndn::OnTimeout());
261 }
262 else {
263 return processFaceStatusDataset(buffer);
264 }
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700265 }
266
267 void
268 startFetchingFaceStatusDataset()
269 {
270 shared_ptr<ndn::OBufferStream> buffer = make_shared<ndn::OBufferStream>();
271
272 Interest interest("/localhost/nfd/faces/list");
273 interest.setChildSelector(1);
274 interest.setMustBeFresh(true);
275
276 m_face.expressInterest(interest,
277 bind(&AutoregServer::fetchFaceStatusSegments, this, _2, buffer),
278 ndn::OnTimeout());
279 }
280
281 void
282 processFaceStatusDataset(const shared_ptr<ndn::OBufferStream>& buffer)
283 {
284 ndn::ConstBufferPtr buf = buffer->buf();
285 std::vector<uint64_t> multicastFaces;
286
287 size_t offset = 0;
Junxiao Shi78926c92015-02-28 22:56:06 -0700288 while (offset < buf->size()) {
289 bool isOk = false;
290 Block block;
291 std::tie(isOk, block) = Block::fromBuffer(buf, offset);
292 if (!isOk) {
293 std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl;
294 break;
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700295 }
Junxiao Shi78926c92015-02-28 22:56:06 -0700296
297 offset += block.size();
298
299 nfd::FaceStatus faceStatus(block);
300 registerPrefixesIfNeeded(faceStatus.getFaceId(), FaceUri(faceStatus.getRemoteUri()),
301 faceStatus.getFacePersistency());
302 }
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700303 }
304
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700305 int
306 main(int argc, char* argv[])
307 {
308 po::options_description optionDesciption;
309 optionDesciption.add_options()
310 ("help,h", "produce help message")
311 ("prefix,i", po::value<std::vector<ndn::Name> >(&m_autoregPrefixes)->composing(),
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700312 "prefix that should be automatically registered when new a remote non-local face is "
313 "established")
314 ("all-faces-prefix,a", po::value<std::vector<ndn::Name> >(&m_allFacesPrefixes)->composing(),
315 "prefix that should be automatically registered for all TCP and UDP non-local faces "
316 "(blacklists and whitelists do not apply to this prefix)")
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700317 ("cost,c", po::value<uint64_t>(&m_cost)->default_value(255),
318 "FIB cost which should be assigned to autoreg nexthops")
319 ("whitelist,w", po::value<std::vector<Network> >(&m_whiteList)->composing(),
320 "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
321 ("blacklist,b", po::value<std::vector<Network> >(&m_blackList)->composing(),
322 "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700323 ("version,V", "show version and exit")
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700324 ;
325
326 po::variables_map options;
327 try
328 {
329 po::store(po::command_line_parser(argc, argv).options(optionDesciption).run(), options);
330 po::notify(options);
331 }
332 catch (std::exception& e)
333 {
334 std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700335 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700336 return 1;
337 }
338
339 if (options.count("help"))
340 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700341 usage(std::cout, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700342 return 0;
343 }
344
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700345 if (options.count("version"))
346 {
347 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
348 return 0;
349 }
350
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700351 if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty())
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700352 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700353 std::cerr << "ERROR: at least one --prefix or --all-faces-prefix must be specified"
354 << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700355 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700356 return 2;
357 }
358
359 if (m_whiteList.empty())
360 {
361 // Allow everything
362 m_whiteList.push_back(Network::getMaxRangeV4());
363 m_whiteList.push_back(Network::getMaxRangeV6());
364 }
365
366 try
367 {
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700368 startFetchingFaceStatusDataset();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700369 startProcessing();
370 }
371 catch (std::exception& e)
372 {
373 std::cerr << "ERROR: " << e.what() << std::endl;
374 return 2;
375 }
376
377 return 0;
378 }
379
380private:
381 Face m_face;
Junxiao Shi8e273ca2014-11-12 00:42:29 -0700382 KeyChain m_keyChain;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700383 Controller m_controller;
Junxiao Shi15b12e72014-08-09 19:56:24 -0700384 FaceMonitor m_faceMonitor;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700385 std::vector<ndn::Name> m_autoregPrefixes;
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700386 std::vector<ndn::Name> m_allFacesPrefixes;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700387 uint64_t m_cost;
388 std::vector<Network> m_whiteList;
389 std::vector<Network> m_blackList;
390};
391
Junxiao Shia1937bf2014-11-06 11:43:40 -0700392} // namespace nfd_autoreg
393} // namespace ndn
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700394
395int
396main(int argc, char* argv[])
397{
Junxiao Shia1937bf2014-11-06 11:43:40 -0700398 ndn::nfd_autoreg::AutoregServer server;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700399 return server.main(argc, argv);
400}