blob: 0b6f7a5b884bdbff22e70fa90631275297f4eeb5 [file] [log] [blame]
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shidda0b462014-06-30 19:42:29 -07003 * Copyright (c) 2014, Regents of the University of California,
4 * 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
Alexander Afanasyev4a771362014-04-24 21:29:33 -070029#include <ndn-cxx/management/nfd-controller.hpp>
Alexander Afanasyev585e5a62014-08-12 11:49:31 -070030#include <ndn-cxx/management/nfd-face-monitor.hpp>
Alexander Afanasyevf056c112014-08-14 16:39:25 -070031#include <ndn-cxx/management/nfd-face-status.hpp>
32#include <ndn-cxx/encoding/buffer-stream.hpp>
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070033
34#include <boost/program_options/options_description.hpp>
35#include <boost/program_options/variables_map.hpp>
36#include <boost/program_options/parsers.hpp>
37
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070038#include "version.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070039#include "core/face-uri.hpp"
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070040#include "network.hpp"
41
42namespace po = boost::program_options;
43
44namespace nfd {
45
46using namespace ndn::nfd;
47using ndn::Face;
Alexander Afanasyev585e5a62014-08-12 11:49:31 -070048using ndn::nfd::FaceEventNotification;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070049
Junxiao Shidda0b462014-06-30 19:42:29 -070050class AutoregServer : boost::noncopyable
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070051{
52public:
53 AutoregServer()
54 : m_controller(m_face)
Junxiao Shi15b12e72014-08-09 19:56:24 -070055 , m_faceMonitor(m_face)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070056 , m_cost(255)
57 {
58 }
59
60 void
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070061 onRegisterCommandSuccess(uint64_t faceId, const Name& prefix)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070062 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070063 std::cerr << "SUCCEED: register " << prefix << " on face " << faceId << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070064 }
65
66 void
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070067 onRegisterCommandFailure(uint64_t faceId, const Name& prefix,
Junxiao Shidda0b462014-06-30 19:42:29 -070068 uint32_t code, const std::string& reason)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070069 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070070 std::cerr << "FAILED: register " << prefix << " on face " << faceId
Alexander Afanasyevf4e89b42014-05-31 15:54:18 +030071 << " (code: " << code << ", reason: " << reason << ")" << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070072 }
73
Junxiao Shidda0b462014-06-30 19:42:29 -070074 /**
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070075 * \return true if uri has schema allowed to do auto-registrations
Junxiao Shidda0b462014-06-30 19:42:29 -070076 */
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070077 bool
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070078 hasAllowedSchema(const FaceUri& uri)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070079 {
80 const std::string& scheme = uri.getScheme();
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070081 return (scheme == "udp4" || scheme == "tcp4" ||
82 scheme == "udp6" || scheme == "tcp6");
83 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070084
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070085 /**
Alexander Afanasyevf056c112014-08-14 16:39:25 -070086 * \return true if address is blacklisted
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070087 */
88 bool
Alexander Afanasyevf056c112014-08-14 16:39:25 -070089 isBlacklisted(const boost::asio::ip::address& address)
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070090 {
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070091 for (std::vector<Network>::const_iterator network = m_blackList.begin();
92 network != m_blackList.end();
93 ++network)
94 {
95 if (network->doesContain(address))
96 return true;
97 }
98
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -070099 return false;
100 }
101
102 /**
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700103 * \return true if address is whitelisted
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700104 */
105 bool
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700106 isWhitelisted(const boost::asio::ip::address& address)
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700107 {
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700108 for (std::vector<Network>::const_iterator network = m_whiteList.begin();
109 network != m_whiteList.end();
110 ++network)
111 {
112 if (network->doesContain(address))
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700113 return true;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700114 }
115
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700116 return false;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700117 }
118
119 void
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700120 registerPrefixesForFace(uint64_t faceId,
121 const std::vector<ndn::Name>& prefixes)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700122 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700123 for (std::vector<ndn::Name>::const_iterator prefix = prefixes.begin();
124 prefix != prefixes.end();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700125 ++prefix)
126 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700127 m_controller.start<RibRegisterCommand>(
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700128 ControlParameters()
129 .setName(*prefix)
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700130 .setFaceId(faceId)
Junxiao Shidda0b462014-06-30 19:42:29 -0700131 .setOrigin(ROUTE_ORIGIN_AUTOREG)
132 .setCost(m_cost)
133 .setExpirationPeriod(time::milliseconds::max()),
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700134 bind(&AutoregServer::onRegisterCommandSuccess, this, faceId, *prefix),
135 bind(&AutoregServer::onRegisterCommandFailure, this, faceId, *prefix, _1, _2));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700136 }
137 }
138
139 void
Chengyu Fan9942cea2014-10-13 14:47:13 -0600140 registerPrefixesIfNeeded(uint64_t faceId, const FaceUri& uri, FacePersistency facePersistency)
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700141 {
142 if (hasAllowedSchema(uri)) {
143 boost::system::error_code ec;
144 boost::asio::ip::address address = boost::asio::ip::address::from_string(uri.getHost(), ec);
145
146 if (!address.is_multicast()) {
147 // register all-face prefixes
148 registerPrefixesForFace(faceId, m_allFacesPrefixes);
149
150 // register autoreg prefixes if new face is on-demand and not blacklisted and whitelisted
Chengyu Fan9942cea2014-10-13 14:47:13 -0600151 if (facePersistency == FACE_PERSISTENCY_ON_DEMAND &&
152 !isBlacklisted(address) && isWhitelisted(address)) {
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700153 registerPrefixesForFace(faceId, m_autoregPrefixes);
154 }
155 }
156 }
157 }
158
159 void
Junxiao Shi15b12e72014-08-09 19:56:24 -0700160 onNotification(const FaceEventNotification& notification)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700161 {
Junxiao Shi6e694322014-04-03 10:27:13 -0700162 if (notification.getKind() == FACE_EVENT_CREATED &&
Chengyu Fan9942cea2014-10-13 14:47:13 -0600163 notification.getFaceScope() != FACE_SCOPE_LOCAL)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700164 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700165 std::cerr << "PROCESSING: " << notification << std::endl;
166
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700167 registerPrefixesIfNeeded(notification.getFaceId(), FaceUri(notification.getRemoteUri()),
Chengyu Fan9942cea2014-10-13 14:47:13 -0600168 notification.getFacePersistency());
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700169 }
170 else
171 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700172 std::cerr << "IGNORED: " << notification << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700173 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700174 }
175
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700176
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700177 void
178 signalHandler()
179 {
180 m_face.shutdown();
181 }
182
183
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700184 void
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700185 usage(std::ostream& os,
186 const po::options_description& optionDesciption,
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700187 const char* programName)
188 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700189 os << "Usage:\n"
190 << " " << programName << " --prefix=</autoreg/prefix> [--prefix=/another/prefix] ...\n"
191 << "\n";
192 os << optionDesciption;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700193 }
194
195 void
196 startProcessing()
197 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700198 std::cerr << "AUTOREG prefixes: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700199 for (std::vector<ndn::Name>::const_iterator prefix = m_autoregPrefixes.begin();
200 prefix != m_autoregPrefixes.end();
201 ++prefix)
202 {
203 std::cout << " " << *prefix << std::endl;
204 }
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700205 std::cerr << "ALL-FACES-AUTOREG prefixes: " << std::endl;
206 for (std::vector<ndn::Name>::const_iterator prefix = m_allFacesPrefixes.begin();
207 prefix != m_allFacesPrefixes.end();
208 ++prefix)
209 {
210 std::cout << " " << *prefix << std::endl;
211 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700212
213 if (!m_blackList.empty())
214 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700215 std::cerr << "Blacklisted networks: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700216 for (std::vector<Network>::const_iterator network = m_blackList.begin();
217 network != m_blackList.end();
218 ++network)
219 {
220 std::cout << " " << *network << std::endl;
221 }
222 }
223
Junxiao Shidda0b462014-06-30 19:42:29 -0700224 std::cerr << "Whitelisted networks: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700225 for (std::vector<Network>::const_iterator network = m_whiteList.begin();
226 network != m_whiteList.end();
227 ++network)
228 {
229 std::cout << " " << *network << std::endl;
230 }
231
Junxiao Shi15b12e72014-08-09 19:56:24 -0700232 m_faceMonitor.onNotification += bind(&AutoregServer::onNotification, this, _1);
233 m_faceMonitor.start();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700234
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700235 boost::asio::signal_set signalSet(m_face.getIoService(), SIGINT, SIGTERM);
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700236 signalSet.async_wait(bind(&AutoregServer::signalHandler, this));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700237
238 m_face.processEvents();
239 }
240
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700241
242 void
243 fetchFaceStatusSegments(const Data& data, const shared_ptr<ndn::OBufferStream>& buffer)
244 {
245 buffer->write(reinterpret_cast<const char*>(data.getContent().value()),
246 data.getContent().value_size());
247
248 uint64_t currentSegment = data.getName().get(-1).toSegment();
249
250 const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
251 if (finalBlockId.empty() ||
252 finalBlockId.toSegment() > currentSegment)
253 {
254 m_face.expressInterest(data.getName().getPrefix(-1).appendSegment(currentSegment+1),
255 bind(&AutoregServer::fetchFaceStatusSegments, this, _2, buffer),
256 ndn::OnTimeout());
257 }
258 else
259 {
260 return processFaceStatusDataset(buffer);
261 }
262 }
263
264 void
265 startFetchingFaceStatusDataset()
266 {
267 shared_ptr<ndn::OBufferStream> buffer = make_shared<ndn::OBufferStream>();
268
269 Interest interest("/localhost/nfd/faces/list");
270 interest.setChildSelector(1);
271 interest.setMustBeFresh(true);
272
273 m_face.expressInterest(interest,
274 bind(&AutoregServer::fetchFaceStatusSegments, this, _2, buffer),
275 ndn::OnTimeout());
276 }
277
278 void
279 processFaceStatusDataset(const shared_ptr<ndn::OBufferStream>& buffer)
280 {
281 ndn::ConstBufferPtr buf = buffer->buf();
282 std::vector<uint64_t> multicastFaces;
283
284 size_t offset = 0;
285 while (offset < buf->size())
286 {
287 Block block;
288 bool ok = Block::fromBuffer(buf, offset, block);
289 if (!ok)
290 {
291 std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl;
292 break;
293 }
294
295 offset += block.size();
296
297 nfd::FaceStatus faceStatus(block);
298 registerPrefixesIfNeeded(faceStatus.getFaceId(), FaceUri(faceStatus.getRemoteUri()),
Chengyu Fan9942cea2014-10-13 14:47:13 -0600299 faceStatus.getFacePersistency());
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700300 }
301 }
302
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700303 int
304 main(int argc, char* argv[])
305 {
306 po::options_description optionDesciption;
307 optionDesciption.add_options()
308 ("help,h", "produce help message")
309 ("prefix,i", po::value<std::vector<ndn::Name> >(&m_autoregPrefixes)->composing(),
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700310 "prefix that should be automatically registered when new a remote non-local face is "
311 "established")
312 ("all-faces-prefix,a", po::value<std::vector<ndn::Name> >(&m_allFacesPrefixes)->composing(),
313 "prefix that should be automatically registered for all TCP and UDP non-local faces "
314 "(blacklists and whitelists do not apply to this prefix)")
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700315 ("cost,c", po::value<uint64_t>(&m_cost)->default_value(255),
316 "FIB cost which should be assigned to autoreg nexthops")
317 ("whitelist,w", po::value<std::vector<Network> >(&m_whiteList)->composing(),
318 "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
319 ("blacklist,b", po::value<std::vector<Network> >(&m_blackList)->composing(),
320 "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700321 ("version,V", "show version and exit")
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700322 ;
323
324 po::variables_map options;
325 try
326 {
327 po::store(po::command_line_parser(argc, argv).options(optionDesciption).run(), options);
328 po::notify(options);
329 }
330 catch (std::exception& e)
331 {
332 std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700333 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700334 return 1;
335 }
336
337 if (options.count("help"))
338 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700339 usage(std::cout, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700340 return 0;
341 }
342
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700343 if (options.count("version"))
344 {
345 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
346 return 0;
347 }
348
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700349 if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty())
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700350 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700351 std::cerr << "ERROR: at least one --prefix or --all-faces-prefix must be specified"
352 << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700353 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700354 return 2;
355 }
356
357 if (m_whiteList.empty())
358 {
359 // Allow everything
360 m_whiteList.push_back(Network::getMaxRangeV4());
361 m_whiteList.push_back(Network::getMaxRangeV6());
362 }
363
364 try
365 {
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700366 startFetchingFaceStatusDataset();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700367 startProcessing();
368 }
369 catch (std::exception& e)
370 {
371 std::cerr << "ERROR: " << e.what() << std::endl;
372 return 2;
373 }
374
375 return 0;
376 }
377
378private:
379 Face m_face;
380 Controller m_controller;
Junxiao Shi15b12e72014-08-09 19:56:24 -0700381 FaceMonitor m_faceMonitor;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700382 std::vector<ndn::Name> m_autoregPrefixes;
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700383 std::vector<ndn::Name> m_allFacesPrefixes;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700384 uint64_t m_cost;
385 std::vector<Network> m_whiteList;
386 std::vector<Network> m_blackList;
387};
388
389} // namespace nfd
390
391int
392main(int argc, char* argv[])
393{
394 nfd::AutoregServer server;
395 return server.main(argc, argv);
396}