blob: b21fe6d2466da2daba18a1a27cc111f460239e82 [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 Shidda0b462014-06-30 19:42:29 -070073 uint32_t code, const std::string& reason)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070074 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070075 std::cerr << "FAILED: register " << prefix << " on face " << faceId
Alexander Afanasyevf4e89b42014-05-31 15:54:18 +030076 << " (code: " << code << ", reason: " << reason << ")" << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070077 }
78
Junxiao Shidda0b462014-06-30 19:42:29 -070079 /**
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070080 * \return true if uri has schema allowed to do auto-registrations
Junxiao Shidda0b462014-06-30 19:42:29 -070081 */
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070082 bool
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070083 hasAllowedSchema(const FaceUri& uri)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070084 {
85 const std::string& scheme = uri.getScheme();
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070086 return (scheme == "udp4" || scheme == "tcp4" ||
87 scheme == "udp6" || scheme == "tcp6");
88 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070089
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070090 /**
Alexander Afanasyevf056c112014-08-14 16:39:25 -070091 * \return true if address is blacklisted
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070092 */
93 bool
Alexander Afanasyevf056c112014-08-14 16:39:25 -070094 isBlacklisted(const boost::asio::ip::address& address)
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070095 {
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070096 for (std::vector<Network>::const_iterator network = m_blackList.begin();
97 network != m_blackList.end();
98 ++network)
99 {
100 if (network->doesContain(address))
101 return true;
102 }
103
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700104 return false;
105 }
106
107 /**
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700108 * \return true if address is whitelisted
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700109 */
110 bool
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700111 isWhitelisted(const boost::asio::ip::address& address)
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700112 {
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700113 for (std::vector<Network>::const_iterator network = m_whiteList.begin();
114 network != m_whiteList.end();
115 ++network)
116 {
117 if (network->doesContain(address))
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700118 return true;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700119 }
120
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700121 return false;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700122 }
123
124 void
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700125 registerPrefixesForFace(uint64_t faceId,
126 const std::vector<ndn::Name>& prefixes)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700127 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700128 for (std::vector<ndn::Name>::const_iterator prefix = prefixes.begin();
129 prefix != prefixes.end();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700130 ++prefix)
131 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700132 m_controller.start<RibRegisterCommand>(
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700133 ControlParameters()
134 .setName(*prefix)
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700135 .setFaceId(faceId)
Junxiao Shidda0b462014-06-30 19:42:29 -0700136 .setOrigin(ROUTE_ORIGIN_AUTOREG)
137 .setCost(m_cost)
138 .setExpirationPeriod(time::milliseconds::max()),
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700139 bind(&AutoregServer::onRegisterCommandSuccess, this, faceId, *prefix),
140 bind(&AutoregServer::onRegisterCommandFailure, this, faceId, *prefix, _1, _2));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700141 }
142 }
143
144 void
Chengyu Fan9942cea2014-10-13 14:47:13 -0600145 registerPrefixesIfNeeded(uint64_t faceId, const FaceUri& uri, FacePersistency facePersistency)
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700146 {
147 if (hasAllowedSchema(uri)) {
148 boost::system::error_code ec;
149 boost::asio::ip::address address = boost::asio::ip::address::from_string(uri.getHost(), ec);
150
151 if (!address.is_multicast()) {
152 // register all-face prefixes
153 registerPrefixesForFace(faceId, m_allFacesPrefixes);
154
155 // register autoreg prefixes if new face is on-demand and not blacklisted and whitelisted
Chengyu Fan9942cea2014-10-13 14:47:13 -0600156 if (facePersistency == FACE_PERSISTENCY_ON_DEMAND &&
157 !isBlacklisted(address) && isWhitelisted(address)) {
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700158 registerPrefixesForFace(faceId, m_autoregPrefixes);
159 }
160 }
161 }
162 }
163
164 void
Junxiao Shi15b12e72014-08-09 19:56:24 -0700165 onNotification(const FaceEventNotification& notification)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700166 {
Junxiao Shi6e694322014-04-03 10:27:13 -0700167 if (notification.getKind() == FACE_EVENT_CREATED &&
Chengyu Fan9942cea2014-10-13 14:47:13 -0600168 notification.getFaceScope() != FACE_SCOPE_LOCAL)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700169 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700170 std::cerr << "PROCESSING: " << notification << std::endl;
171
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700172 registerPrefixesIfNeeded(notification.getFaceId(), FaceUri(notification.getRemoteUri()),
Chengyu Fan9942cea2014-10-13 14:47:13 -0600173 notification.getFacePersistency());
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700174 }
175 else
176 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700177 std::cerr << "IGNORED: " << notification << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700178 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700179 }
180
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700181
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700182 void
183 signalHandler()
184 {
185 m_face.shutdown();
186 }
187
188
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700189 void
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700190 usage(std::ostream& os,
191 const po::options_description& optionDesciption,
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700192 const char* programName)
193 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700194 os << "Usage:\n"
195 << " " << programName << " --prefix=</autoreg/prefix> [--prefix=/another/prefix] ...\n"
196 << "\n";
197 os << optionDesciption;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700198 }
199
200 void
201 startProcessing()
202 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700203 std::cerr << "AUTOREG prefixes: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700204 for (std::vector<ndn::Name>::const_iterator prefix = m_autoregPrefixes.begin();
205 prefix != m_autoregPrefixes.end();
206 ++prefix)
207 {
208 std::cout << " " << *prefix << std::endl;
209 }
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700210 std::cerr << "ALL-FACES-AUTOREG prefixes: " << std::endl;
211 for (std::vector<ndn::Name>::const_iterator prefix = m_allFacesPrefixes.begin();
212 prefix != m_allFacesPrefixes.end();
213 ++prefix)
214 {
215 std::cout << " " << *prefix << std::endl;
216 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700217
218 if (!m_blackList.empty())
219 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700220 std::cerr << "Blacklisted networks: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700221 for (std::vector<Network>::const_iterator network = m_blackList.begin();
222 network != m_blackList.end();
223 ++network)
224 {
225 std::cout << " " << *network << std::endl;
226 }
227 }
228
Junxiao Shidda0b462014-06-30 19:42:29 -0700229 std::cerr << "Whitelisted networks: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700230 for (std::vector<Network>::const_iterator network = m_whiteList.begin();
231 network != m_whiteList.end();
232 ++network)
233 {
234 std::cout << " " << *network << std::endl;
235 }
236
Junxiao Shifbf78342015-01-23 14:46:41 -0700237 m_faceMonitor.onNotification.connect(bind(&AutoregServer::onNotification, this, _1));
Junxiao Shi15b12e72014-08-09 19:56:24 -0700238 m_faceMonitor.start();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700239
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700240 boost::asio::signal_set signalSet(m_face.getIoService(), SIGINT, SIGTERM);
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700241 signalSet.async_wait(bind(&AutoregServer::signalHandler, this));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700242
243 m_face.processEvents();
244 }
245
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700246
247 void
248 fetchFaceStatusSegments(const Data& data, const shared_ptr<ndn::OBufferStream>& buffer)
249 {
250 buffer->write(reinterpret_cast<const char*>(data.getContent().value()),
251 data.getContent().value_size());
252
253 uint64_t currentSegment = data.getName().get(-1).toSegment();
254
255 const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
Junxiao Shia1937bf2014-11-06 11:43:40 -0700256 if (finalBlockId.empty() || finalBlockId.toSegment() > currentSegment) {
257 m_face.expressInterest(data.getName().getPrefix(-1).appendSegment(currentSegment + 1),
258 bind(&AutoregServer::fetchFaceStatusSegments, this, _2, buffer),
259 ndn::OnTimeout());
260 }
261 else {
262 return processFaceStatusDataset(buffer);
263 }
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700264 }
265
266 void
267 startFetchingFaceStatusDataset()
268 {
269 shared_ptr<ndn::OBufferStream> buffer = make_shared<ndn::OBufferStream>();
270
271 Interest interest("/localhost/nfd/faces/list");
272 interest.setChildSelector(1);
273 interest.setMustBeFresh(true);
274
275 m_face.expressInterest(interest,
276 bind(&AutoregServer::fetchFaceStatusSegments, this, _2, buffer),
277 ndn::OnTimeout());
278 }
279
280 void
281 processFaceStatusDataset(const shared_ptr<ndn::OBufferStream>& buffer)
282 {
283 ndn::ConstBufferPtr buf = buffer->buf();
284 std::vector<uint64_t> multicastFaces;
285
286 size_t offset = 0;
Junxiao Shi78926c92015-02-28 22:56:06 -0700287 while (offset < buf->size()) {
288 bool isOk = false;
289 Block block;
290 std::tie(isOk, block) = Block::fromBuffer(buf, offset);
291 if (!isOk) {
292 std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl;
293 break;
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700294 }
Junxiao Shi78926c92015-02-28 22:56:06 -0700295
296 offset += block.size();
297
298 nfd::FaceStatus faceStatus(block);
299 registerPrefixesIfNeeded(faceStatus.getFaceId(), FaceUri(faceStatus.getRemoteUri()),
300 faceStatus.getFacePersistency());
301 }
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700302 }
303
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700304 int
305 main(int argc, char* argv[])
306 {
307 po::options_description optionDesciption;
308 optionDesciption.add_options()
309 ("help,h", "produce help message")
310 ("prefix,i", po::value<std::vector<ndn::Name> >(&m_autoregPrefixes)->composing(),
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700311 "prefix that should be automatically registered when new a remote non-local face is "
312 "established")
313 ("all-faces-prefix,a", po::value<std::vector<ndn::Name> >(&m_allFacesPrefixes)->composing(),
314 "prefix that should be automatically registered for all TCP and UDP non-local faces "
315 "(blacklists and whitelists do not apply to this prefix)")
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700316 ("cost,c", po::value<uint64_t>(&m_cost)->default_value(255),
317 "FIB cost which should be assigned to autoreg nexthops")
318 ("whitelist,w", po::value<std::vector<Network> >(&m_whiteList)->composing(),
319 "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
320 ("blacklist,b", po::value<std::vector<Network> >(&m_blackList)->composing(),
321 "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700322 ("version,V", "show version and exit")
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700323 ;
324
325 po::variables_map options;
326 try
327 {
328 po::store(po::command_line_parser(argc, argv).options(optionDesciption).run(), options);
329 po::notify(options);
330 }
331 catch (std::exception& e)
332 {
333 std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700334 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700335 return 1;
336 }
337
338 if (options.count("help"))
339 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700340 usage(std::cout, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700341 return 0;
342 }
343
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700344 if (options.count("version"))
345 {
346 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
347 return 0;
348 }
349
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700350 if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty())
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700351 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700352 std::cerr << "ERROR: at least one --prefix or --all-faces-prefix must be specified"
353 << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700354 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700355 return 2;
356 }
357
358 if (m_whiteList.empty())
359 {
360 // Allow everything
361 m_whiteList.push_back(Network::getMaxRangeV4());
362 m_whiteList.push_back(Network::getMaxRangeV6());
363 }
364
365 try
366 {
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700367 startFetchingFaceStatusDataset();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700368 startProcessing();
369 }
370 catch (std::exception& e)
371 {
372 std::cerr << "ERROR: " << e.what() << std::endl;
373 return 2;
374 }
375
376 return 0;
377 }
378
379private:
380 Face m_face;
Junxiao Shi8e273ca2014-11-12 00:42:29 -0700381 KeyChain m_keyChain;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700382 Controller m_controller;
Junxiao Shi15b12e72014-08-09 19:56:24 -0700383 FaceMonitor m_faceMonitor;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700384 std::vector<ndn::Name> m_autoregPrefixes;
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700385 std::vector<ndn::Name> m_allFacesPrefixes;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700386 uint64_t m_cost;
387 std::vector<Network> m_whiteList;
388 std::vector<Network> m_blackList;
389};
390
Junxiao Shia1937bf2014-11-06 11:43:40 -0700391} // namespace nfd_autoreg
392} // namespace ndn
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700393
394int
395main(int argc, char* argv[])
396{
Junxiao Shia1937bf2014-11-06 11:43:40 -0700397 ndn::nfd_autoreg::AutoregServer server;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700398 return server.main(argc, argv);
399}