blob: 60f3bc95e1def48a8306af7407f9c362368716d7 [file] [log] [blame]
Steve DiBenedetto042bfe92014-01-30 15:05:08 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * Copyright (c) 2014 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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070024
25#include "manager-base.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060026#include "core/logger.hpp"
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070027
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070028namespace nfd {
29
Steve DiBenedetto3970c892014-01-31 23:31:13 -070030NFD_LOG_INIT("ManagerBase");
31
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070032ManagerBase::ManagerBase(shared_ptr<InternalFace> face, const std::string& privilege)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070033 : m_face(face)
34{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070035 face->getValidator().addSupportedPrivilege(privilege);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070036}
37
38ManagerBase::~ManagerBase()
39{
40
41}
42
Steve DiBenedetto7564d972014-03-24 14:28:46 -060043bool
44ManagerBase::extractParameters(const Name::Component& parameterComponent,
45 ControlParameters& extractedParameters)
46{
47 try
48 {
49 Block rawParameters = parameterComponent.blockFromValue();
50 extractedParameters.wireDecode(rawParameters);
51 }
52 catch (const ndn::Tlv::Error& e)
53 {
54 return false;
55 }
56
57 NFD_LOG_DEBUG("Parameters parsed OK");
58 return true;
59}
60
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070061void
62ManagerBase::sendResponse(const Name& name,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070063 uint32_t code,
64 const std::string& text)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070065{
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080066 ControlResponse response(code, text);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070067 sendResponse(name, response);
68}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070069
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070070void
71ManagerBase::sendResponse(const Name& name,
Steve DiBenedetto7564d972014-03-24 14:28:46 -060072 uint32_t code,
73 const std::string& text,
74 const Block& body)
75{
76 ControlResponse response(code, text);
77 response.setBody(body);
78 sendResponse(name, response);
79}
80
81void
82ManagerBase::sendResponse(const Name& name,
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080083 const ControlResponse& response)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070084{
85 NFD_LOG_DEBUG("responding"
86 << " name: " << name
87 << " code: " << response.getCode()
88 << " text: " << response.getText());
Steve DiBenedetto3970c892014-01-31 23:31:13 -070089
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070090 const Block& encodedControl = response.wireEncode();
Steve DiBenedetto3970c892014-01-31 23:31:13 -070091
Steve DiBenedetto86fce8e2014-03-06 12:16:20 -070092 shared_ptr<Data> responseData(make_shared<Data>(name));
93 responseData->setContent(encodedControl);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070094
Steve DiBenedetto86fce8e2014-03-06 12:16:20 -070095 m_face->sign(*responseData);
96 m_face->put(*responseData);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070097}
98
Steve DiBenedetto51d242a2014-03-31 13:46:43 -060099bool
100ManagerBase::validateParameters(const ControlCommand& command,
101 ControlParameters& parameters)
102{
103 try
104 {
105 command.validateRequest(parameters);
106 }
107 catch (const ControlCommand::ArgumentError& error)
108 {
109 return false;
110 }
111
112 command.applyDefaultsToRequest(parameters);
113
114 return true;
115}
116
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700117void
118ManagerBase::onCommandValidationFailed(const shared_ptr<const Interest>& command,
119 const std::string& error)
120{
Alexander Afanasyevf4e89b42014-05-31 15:54:18 +0300121 NFD_LOG_DEBUG("command result: unauthorized command: " << *command << " (" << error << ")");
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700122 sendResponse(command->getName(), 403, "Unauthorized command");
123}
124
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700125
126} // namespace nfd