nfd-autoreg.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include <ndn-cxx/face.hpp>
27 #include <ndn-cxx/name.hpp>
28 
29 #include <ndn-cxx/security/key-chain.hpp>
30 #include <ndn-cxx/util/face-uri.hpp>
31 #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>
34 #include <ndn-cxx/encoding/buffer-stream.hpp>
35 
36 #include <boost/program_options/options_description.hpp>
37 #include <boost/program_options/variables_map.hpp>
38 #include <boost/program_options/parsers.hpp>
39 
40 #include "core/version.hpp"
41 #include "core/network.hpp"
42 
43 using namespace ndn::nfd;
44 using ndn::Face;
45 using ndn::KeyChain;
46 using ndn::nfd::FaceEventNotification;
47 using ndn::util::FaceUri;
48 using ::nfd::Network;
49 
50 namespace ndn {
51 namespace nfd_autoreg {
52 
53 namespace po = boost::program_options;
54 
55 class AutoregServer : boost::noncopyable
56 {
57 public:
58  AutoregServer()
59  : m_controller(m_face, m_keyChain)
60  , m_faceMonitor(m_face)
61  , m_cost(255)
62  {
63  }
64 
65  void
66  onRegisterCommandSuccess(uint64_t faceId, const Name& prefix)
67  {
68  std::cerr << "SUCCEED: register " << prefix << " on face " << faceId << std::endl;
69  }
70 
71  void
72  onRegisterCommandFailure(uint64_t faceId, const Name& prefix,
73  const nfd::ControlResponse& response)
74  {
75  std::cerr << "FAILED: register " << prefix << " on face " << faceId
76  << " (code: " << response.getCode() << ", reason: " << response.getText() << ")"
77  << std::endl;
78  }
79 
83  bool
84  hasAllowedSchema(const FaceUri& uri)
85  {
86  const std::string& scheme = uri.getScheme();
87  return (scheme == "udp4" || scheme == "tcp4" ||
88  scheme == "udp6" || scheme == "tcp6");
89  }
90 
94  bool
95  isBlacklisted(const boost::asio::ip::address& address)
96  {
97  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 
105  return false;
106  }
107 
111  bool
112  isWhitelisted(const boost::asio::ip::address& address)
113  {
114  for (std::vector<Network>::const_iterator network = m_whiteList.begin();
115  network != m_whiteList.end();
116  ++network)
117  {
118  if (network->doesContain(address))
119  return true;
120  }
121 
122  return false;
123  }
124 
125  void
126  registerPrefixesForFace(uint64_t faceId,
127  const std::vector<ndn::Name>& prefixes)
128  {
129  for (std::vector<ndn::Name>::const_iterator prefix = prefixes.begin();
130  prefix != prefixes.end();
131  ++prefix)
132  {
133  m_controller.start<RibRegisterCommand>(
134  ControlParameters()
135  .setName(*prefix)
136  .setFaceId(faceId)
137  .setOrigin(ROUTE_ORIGIN_AUTOREG)
138  .setCost(m_cost)
139  .setExpirationPeriod(time::milliseconds::max()),
140  bind(&AutoregServer::onRegisterCommandSuccess, this, faceId, *prefix),
141  bind(&AutoregServer::onRegisterCommandFailure, this, faceId, *prefix, _1));
142  }
143  }
144 
145  void
146  registerPrefixesIfNeeded(uint64_t faceId, const FaceUri& uri, FacePersistency facePersistency)
147  {
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
157  if (facePersistency == FACE_PERSISTENCY_ON_DEMAND &&
158  !isBlacklisted(address) && isWhitelisted(address)) {
159  registerPrefixesForFace(faceId, m_autoregPrefixes);
160  }
161  }
162  }
163  }
164 
165  void
166  onNotification(const FaceEventNotification& notification)
167  {
168  if (notification.getKind() == FACE_EVENT_CREATED &&
169  notification.getFaceScope() != FACE_SCOPE_LOCAL)
170  {
171  std::cerr << "PROCESSING: " << notification << std::endl;
172 
173  registerPrefixesIfNeeded(notification.getFaceId(), FaceUri(notification.getRemoteUri()),
174  notification.getFacePersistency());
175  }
176  else
177  {
178  std::cerr << "IGNORED: " << notification << std::endl;
179  }
180  }
181 
182 
183  void
184  signalHandler()
185  {
186  m_face.shutdown();
187  }
188 
189 
190  void
191  usage(std::ostream& os,
192  const po::options_description& optionDesciption,
193  const char* programName)
194  {
195  os << "Usage:\n"
196  << " " << programName << " --prefix=</autoreg/prefix> [--prefix=/another/prefix] ...\n"
197  << "\n";
198  os << optionDesciption;
199  }
200 
201  void
202  startProcessing()
203  {
204  std::cerr << "AUTOREG prefixes: " << std::endl;
205  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  }
211  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  }
218 
219  if (!m_blackList.empty())
220  {
221  std::cerr << "Blacklisted networks: " << std::endl;
222  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 
230  std::cerr << "Whitelisted networks: " << std::endl;
231  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 
238  m_faceMonitor.onNotification.connect(bind(&AutoregServer::onNotification, this, _1));
239  m_faceMonitor.start();
240 
241  boost::asio::signal_set signalSet(m_face.getIoService(), SIGINT, SIGTERM);
242  signalSet.async_wait(bind(&AutoregServer::signalHandler, this));
243 
244  m_face.processEvents();
245  }
246 
247 
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();
257  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  }
265  }
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;
288  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;
295  }
296 
297  offset += block.size();
298 
299  nfd::FaceStatus faceStatus(block);
300  registerPrefixesIfNeeded(faceStatus.getFaceId(), FaceUri(faceStatus.getRemoteUri()),
301  faceStatus.getFacePersistency());
302  }
303  }
304 
305  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(),
312  "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)")
317  ("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")
323  ("version,V", "show version and exit")
324  ;
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;
335  usage(std::cerr, optionDesciption, argv[0]);
336  return 1;
337  }
338 
339  if (options.count("help"))
340  {
341  usage(std::cout, optionDesciption, argv[0]);
342  return 0;
343  }
344 
345  if (options.count("version"))
346  {
347  std::cout << NFD_VERSION_BUILD_STRING << std::endl;
348  return 0;
349  }
350 
351  if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty())
352  {
353  std::cerr << "ERROR: at least one --prefix or --all-faces-prefix must be specified"
354  << std::endl << std::endl;
355  usage(std::cerr, optionDesciption, argv[0]);
356  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  {
368  startFetchingFaceStatusDataset();
369  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 
380 private:
381  Face m_face;
382  KeyChain m_keyChain;
383  Controller m_controller;
384  FaceMonitor m_faceMonitor;
385  std::vector<ndn::Name> m_autoregPrefixes;
386  std::vector<ndn::Name> m_allFacesPrefixes;
387  uint64_t m_cost;
388  std::vector<Network> m_whiteList;
389  std::vector<Network> m_blackList;
390 };
391 
392 } // namespace nfd_autoreg
393 } // namespace ndn
394 
395 int
396 main(int argc, char* argv[])
397 {
398  ndn::nfd_autoreg::AutoregServer server;
399  return server.main(argc, argv);
400 }
Copyright (c) 2014-2016, Regents of the University of California, Arizona Board of Regents...
Definition: nfd.hpp:35
static void usage(const char *programName)
int main(int argc, char **argv)
Copyright (c) 2014-2016, Regents of the University of California, Arizona Board of Regents...