program.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, 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 
26 #include "program.hpp"
27 
28 #include <ndn-cxx/encoding/block-helpers.hpp>
29 #include <ndn-cxx/encoding/tlv.hpp>
30 #include <ndn-cxx/encoding/tlv-nfd.hpp>
31 
32 #include <iostream>
33 
34 namespace ndn {
35 namespace tools {
36 namespace autoconfig_server {
37 
38 const Name HUB_DATA_NAME("/localhop/ndn-autoconf/hub");
39 const Name ROUTABLE_PREFIXES_DATA_PREFIX("/localhop/nfd");
40 const PartialName ROUTABLE_PREFIXES_DATA_SUFFIX("rib/routable-prefixes");
41 
42 Program::Program(const Options& options, Face& face, KeyChain& keyChain)
43  : m_face(face)
44  , m_keyChain(keyChain)
45  , m_dispatcher(face, keyChain)
46 {
47  this->enableHubData(options.hubFaceUri);
48  if (!options.routablePrefixes.empty()) {
49  this->enableRoutablePrefixesDataset(options.routablePrefixes);
50  }
51 }
52 
53 void
54 Program::enableHubData(const FaceUri& hubFaceUri)
55 {
56  auto data = make_shared<Data>(Name(HUB_DATA_NAME).appendVersion());
57  data->setFreshnessPeriod(1_h);
58  data->setContent(makeStringBlock(tlv::nfd::Uri, hubFaceUri.toString()));
59  m_keyChain.sign(*data);
60 
61  m_face.setInterestFilter(HUB_DATA_NAME,
62  [this, data] (const Name&, const Interest& interest) {
63  if (interest.matchesData(*data)) {
64  m_face.put(*data);
65  }
66  },
67  [this] (auto&&... args) {
68  handlePrefixRegistrationFailure(std::forward<decltype(args)>(args)...);
69  });
70 }
71 
72 void
73 Program::enableRoutablePrefixesDataset(const std::vector<Name>& routablePrefixes)
74 {
75  m_dispatcher.addStatusDataset(ROUTABLE_PREFIXES_DATA_SUFFIX,
76  mgmt::makeAcceptAllAuthorization(),
77  [=] (const Name&, const Interest&, mgmt::StatusDatasetContext& context) {
78  for (const Name& routablePrefix : routablePrefixes) {
79  context.append(routablePrefix.wireEncode());
80  }
81  context.end();
82  });
83  m_dispatcher.addTopPrefix(ROUTABLE_PREFIXES_DATA_PREFIX, false);
84 
85  m_face.registerPrefix(Name(ROUTABLE_PREFIXES_DATA_PREFIX).append(ROUTABLE_PREFIXES_DATA_SUFFIX),
86  nullptr,
87  [this] (auto&&... args) {
88  handlePrefixRegistrationFailure(std::forward<decltype(args)>(args)...);
89  });
90 }
91 
92 void
93 Program::handlePrefixRegistrationFailure(const Name& prefix, const std::string& reason)
94 {
95  std::cerr << "ERROR: cannot register prefix " << prefix
96  << " (" << reason << ")" << std::endl;
97  m_face.shutdown();
98 }
99 
100 } // namespace autoconfig_server
101 } // namespace tools
102 } // namespace ndn
Program(const Options &options, Face &face, KeyChain &keyChain)
Definition: program.cpp:42
const Name HUB_DATA_NAME("/localhop/ndn-autoconf/hub")
const Name ROUTABLE_PREFIXES_DATA_PREFIX("/localhop/nfd")
const PartialName ROUTABLE_PREFIXES_DATA_SUFFIX("rib/routable-prefixes")
Copyright (c) 2014-2017, Regents of the University of California, Arizona Board of Regents,...
Definition: dns-srv.cpp:41
std::vector< Name > routablePrefixes
Definition: program.hpp:41