Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2017, 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 | #include <ndn-cxx/encoding/block-helpers.hpp> |
| 28 | #include <ndn-cxx/encoding/tlv.hpp> |
| 29 | #include <ndn-cxx/encoding/tlv-nfd.hpp> |
| 30 | |
| 31 | namespace ndn { |
| 32 | namespace tools { |
| 33 | namespace autoconfig_server { |
| 34 | |
| 35 | static const Name HUB_DATA_NAME("/localhop/ndn-autoconf/hub"); |
| 36 | static const Name ROUTABLE_PREFIXES_DATA_PREFIX("/localhop/nfd"); |
| 37 | static const PartialName ROUTABLE_PREFIXES_DATA_SUFFIX("rib/routable-prefixes"); |
| 38 | |
| 39 | Program::Program(const Options& options, Face& face, KeyChain& keyChain) |
| 40 | : m_face(face) |
| 41 | , m_keyChain(keyChain) |
| 42 | , m_dispatcher(face, keyChain) |
| 43 | { |
| 44 | this->enableHubData(options.hubFaceUri); |
| 45 | if (!options.routablePrefixes.empty()) { |
| 46 | this->enableRoutablePrefixesDataset(options.routablePrefixes); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | Program::enableHubData(const FaceUri& hubFaceUri) |
| 52 | { |
| 53 | std::string uri = hubFaceUri.toString(); |
| 54 | |
| 55 | auto data = make_shared<Data>(Name(HUB_DATA_NAME).appendVersion()); |
| 56 | data->setFreshnessPeriod(time::hours(1)); |
| 57 | data->setContent(makeBinaryBlock(tlv::nfd::Uri, |
| 58 | reinterpret_cast<const uint8_t*>(uri.data()), uri.size())); |
| 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 | bind(&Program::handlePrefixRegistrationFailure, this, _1, _2)); |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | Program::enableRoutablePrefixesDataset(const std::vector<Name>& routablePrefixes) |
| 72 | { |
| 73 | m_dispatcher.addStatusDataset(ROUTABLE_PREFIXES_DATA_SUFFIX, |
| 74 | mgmt::makeAcceptAllAuthorization(), |
| 75 | [=] (const Name& prefix, const Interest& interest, mgmt::StatusDatasetContext& context) { |
| 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), |
| 84 | nullptr, |
| 85 | bind(&Program::handlePrefixRegistrationFailure, this, _1, _2)); |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | Program::handlePrefixRegistrationFailure(const Name& prefix, const std::string& reason) |
| 90 | { |
| 91 | std::cerr << "ERROR: cannot register prefix " << prefix |
| 92 | << " (" << reason << ")" << std::endl; |
| 93 | m_face.shutdown(); |
| 94 | } |
| 95 | |
| 96 | } // namespace autoconfig_server |
| 97 | } // namespace tools |
| 98 | } // namespace ndn |