blob: 57bb5791c001f5f0cc3d40af73cec2d36afa2847 [file] [log] [blame]
alvy297f4162015-03-03 17:15:33 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
dmcoomescf8d0ed2017-02-21 11:39:01 -06003 * Copyright (c) 2014-2018, The University of Memphis,
alvy297f4162015-03-03 17:15:33 -06004 * 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"
alvy297f4162015-03-03 17:15:33 -060023#include "lsdb.hpp"
24#include "nlsr.hpp"
Junxiao Shi3e5120c2016-09-10 16:58:34 +000025#include <ndn-cxx/mgmt/nfd/control-response.hpp>
Laqin Fan54a43f02017-03-08 12:31:30 -060026#include <ndn-cxx/tag.hpp>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050027#include <ndn-cxx/face.hpp>
alvy297f4162015-03-03 17:15:33 -060028
29namespace nlsr {
30namespace update {
31
dmcoomescf8d0ed2017-02-21 11:39:01 -060032INIT_LOGGER(update.PrefixUpdateProcessor);
alvy297f4162015-03-03 17:15:33 -060033
Laqin Fan54a43f02017-03-08 12:31:30 -060034/** \brief an Interest tag to indicate command signer
35 */
36using SignerTag = ndn::SimpleTag<ndn::Name, 20>;
alvy297f4162015-03-03 17:15:33 -060037
Laqin Fan54a43f02017-03-08 12:31:30 -060038/** \brief obtain signer from SignerTag attached to Interest, if available
39 */
40static ndn::optional<std::string>
41getSignerFromTag(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
52PrefixUpdateProcessor::PrefixUpdateProcessor(ndn::mgmt::Dispatcher& dispatcher,
53 ndn::Face& face,
alvy297f4162015-03-03 17:15:33 -060054 NamePrefixList& namePrefixList,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050055 Lsdb& lsdb)
Laqin Fan54a43f02017-03-08 12:31:30 -060056 : CommandManagerBase(dispatcher, namePrefixList, lsdb, "prefix-update")
Davide Pesaventoa08dc3f2018-05-24 00:40:28 -040057 , m_validator(std::make_unique<ndn::security::v2::CertificateFetcherDirectFetch>(face))
alvy297f4162015-03-03 17:15:33 -060058{
dmcoomes5bcb39e2017-10-31 15:07:55 -050059 NLSR_LOG_DEBUG("Setting dispatcher to capture Interests for: "
Laqin Fan54a43f02017-03-08 12:31:30 -060060 << ndn::Name(Nlsr::LOCALHOST_PREFIX).append("prefix-update"));
61
62 m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(makeRelPrefix("advertise"),
63 makeAuthorization(),
64 std::bind(&PrefixUpdateProcessor::validateParameters<AdvertisePrefixCommand>,
65 this, _1),
66 std::bind(&PrefixUpdateProcessor::advertiseAndInsertPrefix, this, _1, _2, _3, _4));
67
68 m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(makeRelPrefix("withdraw"),
69 makeAuthorization(),
70 std::bind(&PrefixUpdateProcessor::validateParameters<WithdrawPrefixCommand>,
71 this, _1),
72 std::bind(&PrefixUpdateProcessor::withdrawAndRemovePrefix, this, _1, _2, _3, _4));
alvy297f4162015-03-03 17:15:33 -060073}
74
Laqin Fan54a43f02017-03-08 12:31:30 -060075ndn::mgmt::Authorization
76PrefixUpdateProcessor::makeAuthorization()
alvy297f4162015-03-03 17:15:33 -060077{
Laqin Fan54a43f02017-03-08 12:31:30 -060078 return [=] (const ndn::Name& prefix, const ndn::Interest& interest,
79 const ndn::mgmt::ControlParameters* params,
80 const ndn::mgmt::AcceptContinuation& accept,
81 const ndn::mgmt::RejectContinuation& reject) {
82 m_validator.validate(interest,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050083 [accept] (const ndn::Interest& request) {
alvy297f4162015-03-03 17:15:33 -060084
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050085 auto signer1 = getSignerFromTag(request);
Laqin Fan54a43f02017-03-08 12:31:30 -060086 std::string signer = signer1.value_or("*");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050087 NLSR_LOG_DEBUG("accept " << request.getName() << " signer=" << signer);
Laqin Fan54a43f02017-03-08 12:31:30 -060088 accept(signer);
89 },
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050090 [reject] (const ndn::Interest& request, const ndn::security::v2::ValidationError& error) {
91 NLSR_LOG_DEBUG("reject " << request.getName() << " signer=" <<
92 getSignerFromTag(request).value_or("?") << ' ' << error);
Laqin Fan54a43f02017-03-08 12:31:30 -060093 reject(ndn::mgmt::RejectReply::STATUS403);
94 });
95 };
alvy297f4162015-03-03 17:15:33 -060096}
97
98void
99PrefixUpdateProcessor::loadValidator(boost::property_tree::ptree section,
100 const std::string& filename)
101{
102 m_validator.load(section, filename);
103}
104
alvy297f4162015-03-03 17:15:33 -0600105} // namespace update
106} // namespace nlsr