Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 45c1f6a | 2025-01-01 19:30:30 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2025, Regents of the University of California, |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [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 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_MGMT_MANAGER_BASE_HPP |
| 27 | #define NFD_DAEMON_MGMT_MANAGER_BASE_HPP |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 29 | #include "command-authenticator.hpp" |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 30 | |
| 31 | #include <ndn-cxx/mgmt/dispatcher.hpp> |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/mgmt/nfd/control-command.hpp> |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 33 | #include <ndn-cxx/mgmt/nfd/control-parameters.hpp> |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 34 | #include <ndn-cxx/mgmt/nfd/control-response.hpp> |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 36 | #include <functional> |
| 37 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 38 | namespace nfd { |
| 39 | |
| 40 | using ndn::mgmt::Dispatcher; |
Davide Pesavento | 45c1f6a | 2025-01-01 19:30:30 -0500 | [diff] [blame^] | 41 | using ndn::mgmt::CommandContinuation; |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 42 | using ndn::nfd::ControlParameters; |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 43 | using ndn::nfd::ControlResponse; |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 46 | * @brief A collection of common functions shared by all NFD managers, |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 47 | * such as communicating with the dispatcher and command validator. |
| 48 | */ |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 49 | class ManagerBase : noncopyable |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 50 | { |
| 51 | public: |
| 52 | class Error : public std::runtime_error |
| 53 | { |
| 54 | public: |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 55 | using std::runtime_error::runtime_error; |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 58 | const std::string& |
| 59 | getModule() const |
| 60 | { |
| 61 | return m_module; |
| 62 | } |
| 63 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 64 | protected: |
| 65 | /** |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 66 | * @warning If you use this constructor, you MUST override makeAuthorization(). |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 67 | */ |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 68 | ManagerBase(std::string_view module, Dispatcher& dispatcher); |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 69 | |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 70 | ManagerBase(std::string_view module, Dispatcher& dispatcher, |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 71 | CommandAuthenticator& authenticator); |
| 72 | |
Davide Pesavento | 0a05f7a | 2023-10-16 20:28:06 -0400 | [diff] [blame] | 73 | // ManagerBase is not supposed to be used polymorphically, so we make the destructor |
| 74 | // protected to prevent deletion of derived objects through a pointer to the base class, |
| 75 | // which would be UB when the destructor is non-virtual. |
| 76 | ~ManagerBase(); |
| 77 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 78 | NFD_PUBLIC_WITH_TESTS_ELSE_PROTECTED: // registrations to the dispatcher |
Davide Pesavento | 45c1f6a | 2025-01-01 19:30:30 -0500 | [diff] [blame^] | 79 | // difference from ndn::mgmt::ControlCommandHandler: accepts nfd::ControlParameters |
| 80 | using ControlCommandHandler = std::function<void(const Name& prefix, const Interest& interest, |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 81 | const ControlParameters& parameters, |
Davide Pesavento | 45c1f6a | 2025-01-01 19:30:30 -0500 | [diff] [blame^] | 82 | const CommandContinuation& done)>; |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 83 | |
| 84 | template<typename Command> |
| 85 | void |
| 86 | registerCommandHandler(const std::string& verb, |
Davide Pesavento | 45c1f6a | 2025-01-01 19:30:30 -0500 | [diff] [blame^] | 87 | ControlCommandHandler handler); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 88 | |
| 89 | void |
| 90 | registerStatusDatasetHandler(const std::string& verb, |
| 91 | const ndn::mgmt::StatusDatasetHandler& handler); |
| 92 | |
| 93 | ndn::mgmt::PostNotification |
| 94 | registerNotificationStream(const std::string& verb); |
| 95 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 96 | NFD_PUBLIC_WITH_TESTS_ELSE_PROTECTED: // helpers |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 97 | /** |
Davide Pesavento | 6d6f207 | 2022-09-12 23:08:34 -0400 | [diff] [blame] | 98 | * @brief Extracts the name from the %KeyLocator of a ControlCommand request. |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 99 | * |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 100 | * This is called after the signature has been validated. |
Davide Pesavento | 6d6f207 | 2022-09-12 23:08:34 -0400 | [diff] [blame] | 101 | * Returns an empty string if %SignatureInfo or %KeyLocator are missing or malformed. |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 102 | */ |
Davide Pesavento | 6d6f207 | 2022-09-12 23:08:34 -0400 | [diff] [blame] | 103 | static std::string |
| 104 | extractSigner(const Interest& interest); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 105 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 106 | NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 107 | /** |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 108 | * @brief Returns an authorization function for a specific management module and verb. |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 109 | */ |
| 110 | virtual ndn::mgmt::Authorization |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 111 | makeAuthorization(const std::string& verb); |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 112 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 113 | /** |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 114 | * @brief Generates the relative prefix for a handler by appending the verb name to the module name. |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 115 | * |
| 116 | * @param verb the verb name |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 117 | * @return the generated relative prefix |
| 118 | */ |
| 119 | PartialName |
Davide Pesavento | 45c1f6a | 2025-01-01 19:30:30 -0500 | [diff] [blame^] | 120 | makeRelPrefix(const std::string& verb) const |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 121 | { |
| 122 | return PartialName(m_module).append(verb); |
| 123 | } |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 124 | |
| 125 | private: |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 126 | std::string m_module; |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 127 | Dispatcher& m_dispatcher; |
| 128 | CommandAuthenticator* m_authenticator = nullptr; |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 131 | template<typename Command> |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 132 | void |
Davide Pesavento | 45c1f6a | 2025-01-01 19:30:30 -0500 | [diff] [blame^] | 133 | ManagerBase::registerCommandHandler(const std::string& verb, ControlCommandHandler handler) |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 134 | { |
Davide Pesavento | 45c1f6a | 2025-01-01 19:30:30 -0500 | [diff] [blame^] | 135 | auto validate = [] (const ndn::mgmt::ControlParametersBase& params) { |
| 136 | BOOST_ASSERT(dynamic_cast<const ControlParameters*>(¶ms) != nullptr); |
| 137 | try { |
| 138 | Command::validateRequest(static_cast<const ControlParameters&>(params)); |
| 139 | return true; |
| 140 | } |
| 141 | catch (const std::invalid_argument&) { |
| 142 | return false; |
| 143 | } |
| 144 | }; |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 145 | |
Davide Pesavento | 45c1f6a | 2025-01-01 19:30:30 -0500 | [diff] [blame^] | 146 | auto handle = [handler = std::move(handler)] (const Name& prefix, const Interest& interest, |
| 147 | const ndn::mgmt::ControlParametersBase& params, |
| 148 | const CommandContinuation& done) { |
| 149 | BOOST_ASSERT(dynamic_cast<const ControlParameters*>(¶ms) != nullptr); |
| 150 | ControlParameters parameters = static_cast<const ControlParameters&>(params); |
| 151 | Command::applyDefaultsToRequest(parameters); |
| 152 | handler(prefix, interest, parameters, done); |
| 153 | }; |
| 154 | |
| 155 | m_dispatcher.addControlCommand<ControlParameters>(makeRelPrefix(verb), makeAuthorization(verb), |
| 156 | std::move(validate), std::move(handle)); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | } // namespace nfd |
| 160 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 161 | #endif // NFD_DAEMON_MGMT_MANAGER_BASE_HPP |