Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 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" |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 27 | |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 28 | #include <ndn-cxx/encoding/block-helpers.hpp> |
| 29 | #include <ndn-cxx/encoding/tlv.hpp> |
| 30 | #include <ndn-cxx/encoding/tlv-nfd.hpp> |
| 31 | |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 32 | #include <iostream> |
| 33 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 34 | namespace ndn::autoconfig_server { |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 35 | |
Davide Pesavento | 6cc9541 | 2022-09-20 19:10:55 -0400 | [diff] [blame] | 36 | const Name HUB_DATA_NAME{"/localhop/ndn-autoconf/hub"}; |
| 37 | const Name ROUTABLE_PREFIXES_DATA_PREFIX{"/localhop/nfd"}; |
| 38 | const PartialName ROUTABLE_PREFIXES_DATA_SUFFIX{"rib/routable-prefixes"}; |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 39 | |
| 40 | Program::Program(const Options& options, Face& face, KeyChain& keyChain) |
| 41 | : m_face(face) |
| 42 | , m_keyChain(keyChain) |
| 43 | , m_dispatcher(face, keyChain) |
| 44 | { |
| 45 | this->enableHubData(options.hubFaceUri); |
| 46 | if (!options.routablePrefixes.empty()) { |
| 47 | this->enableRoutablePrefixesDataset(options.routablePrefixes); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | Program::enableHubData(const FaceUri& hubFaceUri) |
| 53 | { |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 54 | auto data = make_shared<Data>(Name(HUB_DATA_NAME).appendVersion()); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 55 | data->setFreshnessPeriod(1_h); |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 56 | data->setContent(makeStringBlock(tlv::nfd::Uri, hubFaceUri.toString())); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 57 | m_keyChain.sign(*data); |
| 58 | |
| 59 | m_face.setInterestFilter(HUB_DATA_NAME, |
| 60 | [this, data] (const Name&, const Interest& interest) { |
| 61 | if (interest.matchesData(*data)) { |
| 62 | m_face.put(*data); |
| 63 | } |
| 64 | }, |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 65 | [this] (auto&&... args) { |
| 66 | handlePrefixRegistrationFailure(std::forward<decltype(args)>(args)...); |
| 67 | }); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void |
| 71 | Program::enableRoutablePrefixesDataset(const std::vector<Name>& routablePrefixes) |
| 72 | { |
| 73 | m_dispatcher.addStatusDataset(ROUTABLE_PREFIXES_DATA_SUFFIX, |
| 74 | mgmt::makeAcceptAllAuthorization(), |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 75 | [=] (const Name&, const Interest&, mgmt::StatusDatasetContext& context) { |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 76 | for (const Name& routablePrefix : routablePrefixes) { |
| 77 | context.append(routablePrefix.wireEncode()); |
| 78 | } |
| 79 | context.end(); |
| 80 | }); |
| 81 | m_dispatcher.addTopPrefix(ROUTABLE_PREFIXES_DATA_PREFIX, false); |
| 82 | |
| 83 | m_face.registerPrefix(Name(ROUTABLE_PREFIXES_DATA_PREFIX).append(ROUTABLE_PREFIXES_DATA_SUFFIX), |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 84 | nullptr, |
| 85 | [this] (auto&&... args) { |
| 86 | handlePrefixRegistrationFailure(std::forward<decltype(args)>(args)...); |
| 87 | }); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void |
| 91 | Program::handlePrefixRegistrationFailure(const Name& prefix, const std::string& reason) |
| 92 | { |
| 93 | std::cerr << "ERROR: cannot register prefix " << prefix |
| 94 | << " (" << reason << ")" << std::endl; |
| 95 | m_face.shutdown(); |
| 96 | } |
| 97 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 98 | } // namespace ndn::autoconfig_server |