blob: 418c3d6bb843760b59d898b8698904e7aab7918d [file] [log] [blame]
Yanbiao Li698f4fe2015-08-19 16:30:16 -07001/* -*- 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
28namespace nfd {
29
30using ndn::mgmt::ValidateParameters;
31using ndn::mgmt::Authorization;
32
33ManagerBase::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
43void
44ManagerBase::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
52ndn::mgmt::PostNotification
53ManagerBase::registerNotificationStream(const std::string& verb)
54{
55 return m_dispatcher.addNotificationStream(makeRelPrefix(verb));
56}
57
58void
59ManagerBase::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
72void
73ManagerBase::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
92bool
93ManagerBase::validateParameters(const nfd::ControlCommand& command, const ndn::mgmt::ControlParameters& parameters)
94{
95 BOOST_ASSERT(dynamic_cast<const ControlParameters*>(&parameters) != 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
106void
107ManagerBase::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*>(&params) != 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