Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, 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 "manager-base.hpp" |
| 27 | |
| 28 | namespace nfd { |
| 29 | |
| 30 | using ndn::mgmt::ValidateParameters; |
| 31 | using ndn::mgmt::Authorization; |
| 32 | |
| 33 | ManagerBase::ManagerBase(Dispatcher& dispatcher, |
| 34 | CommandValidator& validator, |
| 35 | const std::string& module) |
| 36 | : m_dispatcher(dispatcher) |
| 37 | , m_validator(validator) |
| 38 | , m_mgmtModuleName(module) |
| 39 | { |
| 40 | m_validator.addSupportedPrivilege(module); |
| 41 | } |
| 42 | |
| 43 | void |
| 44 | ManagerBase::registerStatusDatasetHandler(const std::string& verb, |
| 45 | const ndn::mgmt::StatusDatasetHandler& handler) |
| 46 | { |
| 47 | m_dispatcher.addStatusDataset(makeRelPrefix(verb), |
| 48 | ndn::mgmt::makeAcceptAllAuthorization(), |
| 49 | handler); |
| 50 | } |
| 51 | |
| 52 | ndn::mgmt::PostNotification |
| 53 | ManagerBase::registerNotificationStream(const std::string& verb) |
| 54 | { |
| 55 | return m_dispatcher.addNotificationStream(makeRelPrefix(verb)); |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | ManagerBase::authorize(const Name& prefix, const Interest& interest, |
| 60 | const ndn::mgmt::ControlParameters* params, |
| 61 | ndn::mgmt::AcceptContinuation accept, |
| 62 | ndn::mgmt::RejectContinuation reject) |
| 63 | { |
| 64 | BOOST_ASSERT(params != nullptr); |
| 65 | BOOST_ASSERT(typeid(*params) == typeid(ndn::nfd::ControlParameters)); |
| 66 | |
| 67 | m_validator.validate(interest, |
| 68 | bind(&ManagerBase::extractRequester, this, interest, accept), |
| 69 | bind([&] { reject(ndn::mgmt::RejectReply::STATUS403); })); |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | ManagerBase::extractRequester(const Interest& interest, |
| 74 | ndn::mgmt::AcceptContinuation accept) |
| 75 | { |
| 76 | const Name& interestName = interest.getName(); |
| 77 | |
| 78 | try { |
| 79 | ndn::SignatureInfo sigInfo(interestName.at(ndn::signed_interest::POS_SIG_INFO).blockFromValue()); |
| 80 | if (!sigInfo.hasKeyLocator() || |
| 81 | sigInfo.getKeyLocator().getType() != ndn::KeyLocator::KeyLocator_Name) { |
| 82 | return accept(""); |
| 83 | } |
| 84 | |
| 85 | accept(sigInfo.getKeyLocator().getName().toUri()); |
| 86 | } |
| 87 | catch (const tlv::Error&) { |
| 88 | accept(""); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | bool |
| 93 | ManagerBase::validateParameters(const nfd::ControlCommand& command, const ndn::mgmt::ControlParameters& parameters) |
| 94 | { |
| 95 | BOOST_ASSERT(dynamic_cast<const ControlParameters*>(¶meters) != nullptr); |
| 96 | |
| 97 | try { |
| 98 | command.validateRequest(static_cast<const ControlParameters&>(parameters)); |
| 99 | } |
| 100 | catch (const ControlCommand::ArgumentError&) { |
| 101 | return false; |
| 102 | } |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | ManagerBase::handleCommand(shared_ptr<nfd::ControlCommand> command, |
| 108 | const ControlCommandHandler& handler, |
| 109 | const Name& prefix, const Interest& interest, |
| 110 | const ndn::mgmt::ControlParameters& params, |
| 111 | ndn::mgmt::CommandContinuation done) |
| 112 | { |
| 113 | BOOST_ASSERT(dynamic_cast<const ControlParameters*>(¶ms) != nullptr); |
| 114 | ControlParameters parameters = static_cast<const ControlParameters&>(params); |
| 115 | command->applyDefaultsToRequest(parameters); |
| 116 | handler(*command, prefix, interest, parameters, done); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | } // namespace nfd |