blob: 21e78578a730bec8767cad1452b6586fc115464e [file] [log] [blame]
Junxiao Shi3b081542017-07-04 18:37:34 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa997d292017-08-24 20:16:59 -04002/*
Davide Pesavento412c9822021-07-02 00:21:05 -04003 * Copyright (c) 2014-2021, Regents of the University of California,
Junxiao Shi3b081542017-07-04 18:37:34 +00004 * 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"
Davide Pesaventoa997d292017-08-24 20:16:59 -040027
Junxiao Shi3b081542017-07-04 18:37:34 +000028#include <ndn-cxx/encoding/block-helpers.hpp>
29#include <ndn-cxx/encoding/tlv.hpp>
30#include <ndn-cxx/encoding/tlv-nfd.hpp>
31
Davide Pesaventoa997d292017-08-24 20:16:59 -040032#include <iostream>
33
Junxiao Shi3b081542017-07-04 18:37:34 +000034namespace ndn {
35namespace tools {
36namespace autoconfig_server {
37
Davide Pesavento14e71f02019-03-28 17:35:25 -040038const Name HUB_DATA_NAME("/localhop/ndn-autoconf/hub");
39const Name ROUTABLE_PREFIXES_DATA_PREFIX("/localhop/nfd");
40const PartialName ROUTABLE_PREFIXES_DATA_SUFFIX("rib/routable-prefixes");
Junxiao Shi3b081542017-07-04 18:37:34 +000041
42Program::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
53void
54Program::enableHubData(const FaceUri& hubFaceUri)
55{
56 std::string uri = hubFaceUri.toString();
57
58 auto data = make_shared<Data>(Name(HUB_DATA_NAME).appendVersion());
Davide Pesavento14e71f02019-03-28 17:35:25 -040059 data->setFreshnessPeriod(1_h);
Junxiao Shi3b081542017-07-04 18:37:34 +000060 data->setContent(makeBinaryBlock(tlv::nfd::Uri,
61 reinterpret_cast<const uint8_t*>(uri.data()), uri.size()));
62 m_keyChain.sign(*data);
63
64 m_face.setInterestFilter(HUB_DATA_NAME,
65 [this, data] (const Name&, const Interest& interest) {
66 if (interest.matchesData(*data)) {
67 m_face.put(*data);
68 }
69 },
Davide Pesavento412c9822021-07-02 00:21:05 -040070 [this] (auto&&... args) {
71 handlePrefixRegistrationFailure(std::forward<decltype(args)>(args)...);
72 });
Junxiao Shi3b081542017-07-04 18:37:34 +000073}
74
75void
76Program::enableRoutablePrefixesDataset(const std::vector<Name>& routablePrefixes)
77{
78 m_dispatcher.addStatusDataset(ROUTABLE_PREFIXES_DATA_SUFFIX,
79 mgmt::makeAcceptAllAuthorization(),
Davide Pesavento14e71f02019-03-28 17:35:25 -040080 [=] (const Name&, const Interest&, mgmt::StatusDatasetContext& context) {
Junxiao Shi3b081542017-07-04 18:37:34 +000081 for (const Name& routablePrefix : routablePrefixes) {
82 context.append(routablePrefix.wireEncode());
83 }
84 context.end();
85 });
86 m_dispatcher.addTopPrefix(ROUTABLE_PREFIXES_DATA_PREFIX, false);
87
88 m_face.registerPrefix(Name(ROUTABLE_PREFIXES_DATA_PREFIX).append(ROUTABLE_PREFIXES_DATA_SUFFIX),
Davide Pesavento412c9822021-07-02 00:21:05 -040089 nullptr,
90 [this] (auto&&... args) {
91 handlePrefixRegistrationFailure(std::forward<decltype(args)>(args)...);
92 });
Junxiao Shi3b081542017-07-04 18:37:34 +000093}
94
95void
96Program::handlePrefixRegistrationFailure(const Name& prefix, const std::string& reason)
97{
98 std::cerr << "ERROR: cannot register prefix " << prefix
99 << " (" << reason << ")" << std::endl;
100 m_face.shutdown();
101}
102
103} // namespace autoconfig_server
104} // namespace tools
105} // namespace ndn