nfd-autoreg.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2018, 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.
10  *
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/>.
24  */
25 
27 #include "core/network.hpp"
28 #include "core/version.hpp"
29 
30 #include <ndn-cxx/face.hpp>
31 #include <ndn-cxx/name.hpp>
32 #include <ndn-cxx/encoding/buffer-stream.hpp>
33 #include <ndn-cxx/mgmt/nfd/controller.hpp>
34 #include <ndn-cxx/mgmt/nfd/face-monitor.hpp>
35 #include <ndn-cxx/mgmt/nfd/face-status.hpp>
36 #include <ndn-cxx/net/face-uri.hpp>
37 #include <ndn-cxx/security/key-chain.hpp>
38 
39 #include <boost/program_options/options_description.hpp>
40 #include <boost/program_options/variables_map.hpp>
41 #include <boost/program_options/parsers.hpp>
42 
43 #include <iostream>
44 
45 namespace ndn {
46 namespace nfd_autoreg {
47 
48 using ::nfd::Network;
49 
50 class AutoregServer : boost::noncopyable
51 {
52 public:
53  AutoregServer()
54  : m_controller(m_face, m_keyChain)
55  , m_faceMonitor(m_face)
56  , m_cost(255)
57  {
58  }
59 
60  void
61  onRegisterCommandSuccess(uint64_t faceId, const Name& prefix)
62  {
63  std::cerr << "SUCCEED: register " << prefix << " on face " << faceId << std::endl;
64  }
65 
66  void
67  onRegisterCommandFailure(uint64_t faceId, const Name& prefix,
68  const nfd::ControlResponse& response)
69  {
70  std::cerr << "FAILED: register " << prefix << " on face " << faceId
71  << " (code: " << response.getCode() << ", reason: " << response.getText() << ")"
72  << std::endl;
73  }
74 
78  static bool
79  hasAllowedSchema(const FaceUri& uri)
80  {
81  const std::string& scheme = uri.getScheme();
82  return scheme == "udp4" || scheme == "tcp4" ||
83  scheme == "udp6" || scheme == "tcp6";
84  }
85 
89  bool
90  isBlacklisted(const boost::asio::ip::address& address) const
91  {
92  return std::any_of(m_blackList.begin(), m_blackList.end(),
93  bind(&Network::doesContain, _1, address));
94  }
95 
99  bool
100  isWhitelisted(const boost::asio::ip::address& address) const
101  {
102  return std::any_of(m_whiteList.begin(), m_whiteList.end(),
103  bind(&Network::doesContain, _1, address));
104  }
105 
106  void
107  registerPrefixesForFace(uint64_t faceId, const std::vector<Name>& prefixes)
108  {
109  for (const Name& prefix : prefixes) {
110  m_controller.start<nfd::RibRegisterCommand>(
111  nfd::ControlParameters()
112  .setName(prefix)
113  .setFaceId(faceId)
114  .setOrigin(nfd::ROUTE_ORIGIN_AUTOREG)
115  .setCost(m_cost)
116  .setExpirationPeriod(time::milliseconds::max()),
117  bind(&AutoregServer::onRegisterCommandSuccess, this, faceId, prefix),
118  bind(&AutoregServer::onRegisterCommandFailure, this, faceId, prefix, _1));
119  }
120  }
121 
122  void
123  registerPrefixesIfNeeded(uint64_t faceId, const FaceUri& uri, nfd::FacePersistency facePersistency)
124  {
125  if (hasAllowedSchema(uri)) {
126  boost::system::error_code ec;
127  auto address = boost::asio::ip::address::from_string(uri.getHost(), ec);
128 
129  if (!address.is_multicast()) {
130  // register all-face prefixes
131  registerPrefixesForFace(faceId, m_allFacesPrefixes);
132 
133  // register autoreg prefixes if new face is on-demand and not blacklisted and whitelisted
134  if (facePersistency == nfd::FACE_PERSISTENCY_ON_DEMAND &&
135  !isBlacklisted(address) && isWhitelisted(address)) {
136  registerPrefixesForFace(faceId, m_autoregPrefixes);
137  }
138  }
139  }
140  }
141 
142  void
143  onNotification(const nfd::FaceEventNotification& notification)
144  {
145  if (notification.getKind() == nfd::FACE_EVENT_CREATED &&
146  notification.getFaceScope() != nfd::FACE_SCOPE_LOCAL) {
147  std::cerr << "PROCESSING: " << notification << std::endl;
148 
149  registerPrefixesIfNeeded(notification.getFaceId(), FaceUri(notification.getRemoteUri()),
150  notification.getFacePersistency());
151  }
152  else {
153  std::cerr << "IGNORED: " << notification << std::endl;
154  }
155  }
156 
157  void
158  signalHandler()
159  {
160  m_face.shutdown();
161  }
162 
163  static void
164  usage(std::ostream& os,
165  const boost::program_options::options_description& desc,
166  const char* programName)
167  {
168  os << "Usage: " << programName << " [--prefix=</autoreg/prefix>]... [options]\n"
169  << "\n"
170  << desc;
171  }
172 
173  void
174  startProcessing()
175  {
176  std::cerr << "AUTOREG prefixes: " << std::endl;
177  for (const Name& prefix : m_autoregPrefixes) {
178  std::cout << " " << prefix << std::endl;
179  }
180  std::cerr << "ALL-FACES-AUTOREG prefixes: " << std::endl;
181  for (const Name& prefix : m_allFacesPrefixes) {
182  std::cout << " " << prefix << std::endl;
183  }
184 
185  if (!m_blackList.empty()) {
186  std::cerr << "Blacklisted networks: " << std::endl;
187  for (const Network& network : m_blackList) {
188  std::cout << " " << network << std::endl;
189  }
190  }
191 
192  std::cerr << "Whitelisted networks: " << std::endl;
193  for (const Network& network : m_whiteList) {
194  std::cout << " " << network << std::endl;
195  }
196 
197  m_faceMonitor.onNotification.connect(bind(&AutoregServer::onNotification, this, _1));
198  m_faceMonitor.start();
199 
200  boost::asio::signal_set signalSet(m_face.getIoService(), SIGINT, SIGTERM);
201  signalSet.async_wait(bind(&AutoregServer::signalHandler, this));
202 
203  m_face.processEvents();
204  }
205 
206  void
207  startFetchingFaceStatusDataset()
208  {
209  m_controller.fetch<nfd::FaceDataset>(
210  [this] (const std::vector<nfd::FaceStatus>& faces) {
211  for (const auto& faceStatus : faces) {
212  registerPrefixesIfNeeded(faceStatus.getFaceId(), FaceUri(faceStatus.getRemoteUri()),
213  faceStatus.getFacePersistency());
214  }
215  },
216  [] (uint32_t code, const std::string& reason) {});
217  }
218 
219  int
220  main(int argc, char* argv[])
221  {
222  namespace po = boost::program_options;
223 
224  po::options_description optionsDesc("Options");
225  optionsDesc.add_options()
226  ("help,h", "print this message and exit")
227  ("version,V", "show version information and exit")
228  ("prefix,i", po::value<std::vector<Name>>(&m_autoregPrefixes)->composing(),
229  "prefix that should be automatically registered when a new non-local face is created")
230  ("all-faces-prefix,a", po::value<std::vector<Name>>(&m_allFacesPrefixes)->composing(),
231  "prefix that should be automatically registered for all TCP and UDP non-local faces "
232  "(blacklists and whitelists do not apply to this prefix)")
233  ("cost,c", po::value<uint64_t>(&m_cost)->default_value(255),
234  "FIB cost that should be assigned to autoreg nexthops")
235  ("whitelist,w", po::value<std::vector<Network>>(&m_whiteList)->composing(),
236  "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
237  ("blacklist,b", po::value<std::vector<Network>>(&m_blackList)->composing(),
238  "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
239  ;
240 
241  po::variables_map options;
242  try {
243  po::store(po::parse_command_line(argc, argv, optionsDesc), options);
244  po::notify(options);
245  }
246  catch (const std::exception& e) {
247  std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
248  usage(std::cerr, optionsDesc, argv[0]);
249  return 2;
250  }
251 
252  if (options.count("help") > 0) {
253  usage(std::cout, optionsDesc, argv[0]);
254  return 0;
255  }
256 
257  if (options.count("version") > 0) {
258  std::cout << NFD_VERSION_BUILD_STRING << std::endl;
259  return 0;
260  }
261 
262  if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty()) {
263  std::cerr << "ERROR: at least one --prefix or --all-faces-prefix must be specified"
264  << std::endl << std::endl;
265  usage(std::cerr, optionsDesc, argv[0]);
266  return 2;
267  }
268 
269  if (m_whiteList.empty()) {
270  // Allow everything
271  m_whiteList.push_back(Network::getMaxRangeV4());
272  m_whiteList.push_back(Network::getMaxRangeV6());
273  }
274 
275  try {
276  startFetchingFaceStatusDataset();
277  startProcessing();
278  }
279  catch (const std::exception& e) {
280  std::cerr << "ERROR: " << ::nfd::getExtendedErrorMessage(e) << std::endl;
281  return 1;
282  }
283 
284  return 0;
285  }
286 
287 private:
288  Face m_face;
289  KeyChain m_keyChain;
290  nfd::Controller m_controller;
291  nfd::FaceMonitor m_faceMonitor;
292  std::vector<Name> m_autoregPrefixes;
293  std::vector<Name> m_allFacesPrefixes;
294  uint64_t m_cost;
295  std::vector<Network> m_whiteList;
296  std::vector<Network> m_blackList;
297 };
298 
299 } // namespace nfd_autoreg
300 } // namespace ndn
301 
302 int
303 main(int argc, char* argv[])
304 {
305  ndn::nfd_autoreg::AutoregServer server;
306  return server.main(argc, argv);
307 }
static void usage(std::ostream &os, const po::options_description &opts, const char *programName)
Definition: main.cpp:58
Copyright (c) 2014-2017, Regents of the University of California, Arizona Board of Regents...
std::string getExtendedErrorMessage(const E &exception)
int main(int argc, char *argv[])