blob: 5164f5de1ee8dc12621d7b015ee2de42a84a3d1d [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
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700140 registerPrefixesIfNeeded(uint64_t faceId, const FaceUri& uri, bool isOnDemand)
141 {
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
151 if (isOnDemand && !isBlacklisted(address) && isWhitelisted(address)) {
152 registerPrefixesForFace(faceId, m_autoregPrefixes);
153 }
154 }
155 }
156 }
157
158 void
Junxiao Shi15b12e72014-08-09 19:56:24 -0700159 onNotification(const FaceEventNotification& notification)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700160 {
Junxiao Shi6e694322014-04-03 10:27:13 -0700161 if (notification.getKind() == FACE_EVENT_CREATED &&
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700162 !notification.isLocal())
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700163 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700164 std::cerr << "PROCESSING: " << notification << std::endl;
165
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700166 registerPrefixesIfNeeded(notification.getFaceId(), FaceUri(notification.getRemoteUri()),
167 notification.isOnDemand());
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700168 }
169 else
170 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700171 std::cerr << "IGNORED: " << notification << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700172 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700173 }
174
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700175
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700176 void
177 signalHandler()
178 {
179 m_face.shutdown();
180 }
181
182
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700183 void
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700184 usage(std::ostream& os,
185 const po::options_description& optionDesciption,
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700186 const char* programName)
187 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700188 os << "Usage:\n"
189 << " " << programName << " --prefix=</autoreg/prefix> [--prefix=/another/prefix] ...\n"
190 << "\n";
191 os << optionDesciption;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700192 }
193
194 void
195 startProcessing()
196 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700197 std::cerr << "AUTOREG prefixes: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700198 for (std::vector<ndn::Name>::const_iterator prefix = m_autoregPrefixes.begin();
199 prefix != m_autoregPrefixes.end();
200 ++prefix)
201 {
202 std::cout << " " << *prefix << std::endl;
203 }
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700204 std::cerr << "ALL-FACES-AUTOREG prefixes: " << std::endl;
205 for (std::vector<ndn::Name>::const_iterator prefix = m_allFacesPrefixes.begin();
206 prefix != m_allFacesPrefixes.end();
207 ++prefix)
208 {
209 std::cout << " " << *prefix << std::endl;
210 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700211
212 if (!m_blackList.empty())
213 {
Junxiao Shidda0b462014-06-30 19:42:29 -0700214 std::cerr << "Blacklisted networks: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700215 for (std::vector<Network>::const_iterator network = m_blackList.begin();
216 network != m_blackList.end();
217 ++network)
218 {
219 std::cout << " " << *network << std::endl;
220 }
221 }
222
Junxiao Shidda0b462014-06-30 19:42:29 -0700223 std::cerr << "Whitelisted networks: " << std::endl;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700224 for (std::vector<Network>::const_iterator network = m_whiteList.begin();
225 network != m_whiteList.end();
226 ++network)
227 {
228 std::cout << " " << *network << std::endl;
229 }
230
Junxiao Shi15b12e72014-08-09 19:56:24 -0700231 m_faceMonitor.onNotification += bind(&AutoregServer::onNotification, this, _1);
232 m_faceMonitor.start();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700233
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700234 boost::asio::signal_set signalSet(m_face.getIoService(), SIGINT, SIGTERM);
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700235 signalSet.async_wait(bind(&AutoregServer::signalHandler, this));
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700236
237 m_face.processEvents();
238 }
239
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700240
241 void
242 fetchFaceStatusSegments(const Data& data, const shared_ptr<ndn::OBufferStream>& buffer)
243 {
244 buffer->write(reinterpret_cast<const char*>(data.getContent().value()),
245 data.getContent().value_size());
246
247 uint64_t currentSegment = data.getName().get(-1).toSegment();
248
249 const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
250 if (finalBlockId.empty() ||
251 finalBlockId.toSegment() > currentSegment)
252 {
253 m_face.expressInterest(data.getName().getPrefix(-1).appendSegment(currentSegment+1),
254 bind(&AutoregServer::fetchFaceStatusSegments, this, _2, buffer),
255 ndn::OnTimeout());
256 }
257 else
258 {
259 return processFaceStatusDataset(buffer);
260 }
261 }
262
263 void
264 startFetchingFaceStatusDataset()
265 {
266 shared_ptr<ndn::OBufferStream> buffer = make_shared<ndn::OBufferStream>();
267
268 Interest interest("/localhost/nfd/faces/list");
269 interest.setChildSelector(1);
270 interest.setMustBeFresh(true);
271
272 m_face.expressInterest(interest,
273 bind(&AutoregServer::fetchFaceStatusSegments, this, _2, buffer),
274 ndn::OnTimeout());
275 }
276
277 void
278 processFaceStatusDataset(const shared_ptr<ndn::OBufferStream>& buffer)
279 {
280 ndn::ConstBufferPtr buf = buffer->buf();
281 std::vector<uint64_t> multicastFaces;
282
283 size_t offset = 0;
284 while (offset < buf->size())
285 {
286 Block block;
287 bool ok = Block::fromBuffer(buf, offset, block);
288 if (!ok)
289 {
290 std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl;
291 break;
292 }
293
294 offset += block.size();
295
296 nfd::FaceStatus faceStatus(block);
297 registerPrefixesIfNeeded(faceStatus.getFaceId(), FaceUri(faceStatus.getRemoteUri()),
298 faceStatus.isOnDemand());
299 }
300 }
301
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700302 int
303 main(int argc, char* argv[])
304 {
305 po::options_description optionDesciption;
306 optionDesciption.add_options()
307 ("help,h", "produce help message")
308 ("prefix,i", po::value<std::vector<ndn::Name> >(&m_autoregPrefixes)->composing(),
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700309 "prefix that should be automatically registered when new a remote non-local face is "
310 "established")
311 ("all-faces-prefix,a", po::value<std::vector<ndn::Name> >(&m_allFacesPrefixes)->composing(),
312 "prefix that should be automatically registered for all TCP and UDP non-local faces "
313 "(blacklists and whitelists do not apply to this prefix)")
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700314 ("cost,c", po::value<uint64_t>(&m_cost)->default_value(255),
315 "FIB cost which should be assigned to autoreg nexthops")
316 ("whitelist,w", po::value<std::vector<Network> >(&m_whiteList)->composing(),
317 "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
318 ("blacklist,b", po::value<std::vector<Network> >(&m_blackList)->composing(),
319 "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700320 ("version,V", "show version and exit")
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700321 ;
322
323 po::variables_map options;
324 try
325 {
326 po::store(po::command_line_parser(argc, argv).options(optionDesciption).run(), options);
327 po::notify(options);
328 }
329 catch (std::exception& e)
330 {
331 std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700332 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700333 return 1;
334 }
335
336 if (options.count("help"))
337 {
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700338 usage(std::cout, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700339 return 0;
340 }
341
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700342 if (options.count("version"))
343 {
344 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
345 return 0;
346 }
347
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700348 if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty())
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700349 {
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700350 std::cerr << "ERROR: at least one --prefix or --all-faces-prefix must be specified"
351 << std::endl << std::endl;
Alexander Afanasyev60a7ba52014-03-23 11:23:06 -0700352 usage(std::cerr, optionDesciption, argv[0]);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700353 return 2;
354 }
355
356 if (m_whiteList.empty())
357 {
358 // Allow everything
359 m_whiteList.push_back(Network::getMaxRangeV4());
360 m_whiteList.push_back(Network::getMaxRangeV6());
361 }
362
363 try
364 {
Alexander Afanasyevf056c112014-08-14 16:39:25 -0700365 startFetchingFaceStatusDataset();
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700366 startProcessing();
367 }
368 catch (std::exception& e)
369 {
370 std::cerr << "ERROR: " << e.what() << std::endl;
371 return 2;
372 }
373
374 return 0;
375 }
376
377private:
378 Face m_face;
379 Controller m_controller;
Junxiao Shi15b12e72014-08-09 19:56:24 -0700380 FaceMonitor m_faceMonitor;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700381 std::vector<ndn::Name> m_autoregPrefixes;
Alexander Afanasyev81c1a2a2014-08-14 16:07:47 -0700382 std::vector<ndn::Name> m_allFacesPrefixes;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700383 uint64_t m_cost;
384 std::vector<Network> m_whiteList;
385 std::vector<Network> m_blackList;
386};
387
388} // namespace nfd
389
390int
391main(int argc, char* argv[])
392{
393 nfd::AutoregServer server;
394 return server.main(argc, argv);
395}