alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame^] | 3 | * Copyright (c) 2014-2018, The University of Memphis, |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
| 6 | * |
| 7 | * This file is part of NLSR (Named-data Link State Routing). |
| 8 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 9 | * |
| 10 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 20 | **/ |
| 21 | |
| 22 | #include "prefix-update-processor.hpp" |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 23 | #include "lsdb.hpp" |
| 24 | #include "nlsr.hpp" |
Junxiao Shi | 3e5120c | 2016-09-10 16:58:34 +0000 | [diff] [blame] | 25 | #include <ndn-cxx/mgmt/nfd/control-response.hpp> |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 26 | #include <ndn-cxx/tag.hpp> |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 27 | #include <ndn-cxx/face.hpp> |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 28 | |
| 29 | namespace nlsr { |
| 30 | namespace update { |
| 31 | |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame^] | 32 | INIT_LOGGER(update.PrefixUpdateProcessor); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 33 | |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 34 | /** \brief an Interest tag to indicate command signer |
| 35 | */ |
| 36 | using SignerTag = ndn::SimpleTag<ndn::Name, 20>; |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 37 | |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 38 | /** \brief obtain signer from SignerTag attached to Interest, if available |
| 39 | */ |
| 40 | static ndn::optional<std::string> |
| 41 | getSignerFromTag(const ndn::Interest& interest) |
| 42 | { |
| 43 | shared_ptr<SignerTag> signerTag = interest.getTag<SignerTag>(); |
| 44 | if (signerTag == nullptr) { |
| 45 | return ndn::nullopt; |
| 46 | } |
| 47 | else { |
| 48 | return signerTag->get().toUri(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | PrefixUpdateProcessor::PrefixUpdateProcessor(ndn::mgmt::Dispatcher& dispatcher, |
| 53 | ndn::Face& face, |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 54 | NamePrefixList& namePrefixList, |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 55 | Lsdb& lsdb) |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 56 | : CommandManagerBase(dispatcher, namePrefixList, lsdb, "prefix-update") |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 57 | |
| 58 | , m_validator(ndn::make_unique<ndn::security::v2::CertificateFetcherDirectFetch>(face)) |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 59 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 60 | NLSR_LOG_DEBUG("Setting dispatcher to capture Interests for: " |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 61 | << ndn::Name(Nlsr::LOCALHOST_PREFIX).append("prefix-update")); |
| 62 | |
| 63 | m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(makeRelPrefix("advertise"), |
| 64 | makeAuthorization(), |
| 65 | std::bind(&PrefixUpdateProcessor::validateParameters<AdvertisePrefixCommand>, |
| 66 | this, _1), |
| 67 | std::bind(&PrefixUpdateProcessor::advertiseAndInsertPrefix, this, _1, _2, _3, _4)); |
| 68 | |
| 69 | m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(makeRelPrefix("withdraw"), |
| 70 | makeAuthorization(), |
| 71 | std::bind(&PrefixUpdateProcessor::validateParameters<WithdrawPrefixCommand>, |
| 72 | this, _1), |
| 73 | std::bind(&PrefixUpdateProcessor::withdrawAndRemovePrefix, this, _1, _2, _3, _4)); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 74 | } |
| 75 | |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 76 | ndn::mgmt::Authorization |
| 77 | PrefixUpdateProcessor::makeAuthorization() |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 78 | { |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 79 | return [=] (const ndn::Name& prefix, const ndn::Interest& interest, |
| 80 | const ndn::mgmt::ControlParameters* params, |
| 81 | const ndn::mgmt::AcceptContinuation& accept, |
| 82 | const ndn::mgmt::RejectContinuation& reject) { |
| 83 | m_validator.validate(interest, |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 84 | [accept] (const ndn::Interest& request) { |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 85 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 86 | auto signer1 = getSignerFromTag(request); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 87 | std::string signer = signer1.value_or("*"); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 88 | NLSR_LOG_DEBUG("accept " << request.getName() << " signer=" << signer); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 89 | accept(signer); |
| 90 | }, |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 91 | [reject] (const ndn::Interest& request, const ndn::security::v2::ValidationError& error) { |
| 92 | NLSR_LOG_DEBUG("reject " << request.getName() << " signer=" << |
| 93 | getSignerFromTag(request).value_or("?") << ' ' << error); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 94 | reject(ndn::mgmt::RejectReply::STATUS403); |
| 95 | }); |
| 96 | }; |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void |
| 100 | PrefixUpdateProcessor::loadValidator(boost::property_tree::ptree section, |
| 101 | const std::string& filename) |
| 102 | { |
| 103 | m_validator.load(section, filename); |
| 104 | } |
| 105 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 106 | } // namespace update |
| 107 | } // namespace nlsr |