blob: da553a56983c25fbe3380fe91dc6695c3fdd031b [file] [log] [blame]
Steve DiBenedetto042bfe92014-01-30 15:05:08 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince Lehman5144f822014-07-23 15:12:56 -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 * The University of Memphis
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Vince Lehman5144f822014-07-23 15:12:56 -070024 */
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070025
26#include "manager-base.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060027#include "core/logger.hpp"
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070028
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070029namespace nfd {
30
Steve DiBenedetto3970c892014-01-31 23:31:13 -070031NFD_LOG_INIT("ManagerBase");
32
Vince Lehman5144f822014-07-23 15:12:56 -070033ManagerBase::ManagerBase(shared_ptr<InternalFace> face, const std::string& privilege,
34 ndn::KeyChain& keyChain)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070035 : m_face(face)
Vince Lehman5144f822014-07-23 15:12:56 -070036 , m_keyChain(keyChain)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070037{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070038 face->getValidator().addSupportedPrivilege(privilege);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070039}
40
41ManagerBase::~ManagerBase()
42{
43
44}
45
Steve DiBenedetto7564d972014-03-24 14:28:46 -060046bool
47ManagerBase::extractParameters(const Name::Component& parameterComponent,
48 ControlParameters& extractedParameters)
49{
50 try
51 {
52 Block rawParameters = parameterComponent.blockFromValue();
53 extractedParameters.wireDecode(rawParameters);
54 }
Junxiao Shi67f11ac2014-10-19 09:29:13 -070055 catch (const tlv::Error&)
Steve DiBenedetto7564d972014-03-24 14:28:46 -060056 {
57 return false;
58 }
59
60 NFD_LOG_DEBUG("Parameters parsed OK");
61 return true;
62}
63
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070064void
65ManagerBase::sendResponse(const Name& name,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070066 uint32_t code,
67 const std::string& text)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070068{
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080069 ControlResponse response(code, text);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070070 sendResponse(name, response);
71}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070072
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070073void
74ManagerBase::sendResponse(const Name& name,
Steve DiBenedetto7564d972014-03-24 14:28:46 -060075 uint32_t code,
76 const std::string& text,
77 const Block& body)
78{
79 ControlResponse response(code, text);
80 response.setBody(body);
81 sendResponse(name, response);
82}
83
84void
85ManagerBase::sendResponse(const Name& name,
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080086 const ControlResponse& response)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070087{
88 NFD_LOG_DEBUG("responding"
89 << " name: " << name
90 << " code: " << response.getCode()
91 << " text: " << response.getText());
Steve DiBenedetto3970c892014-01-31 23:31:13 -070092
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070093 const Block& encodedControl = response.wireEncode();
Steve DiBenedetto3970c892014-01-31 23:31:13 -070094
Steve DiBenedetto86fce8e2014-03-06 12:16:20 -070095 shared_ptr<Data> responseData(make_shared<Data>(name));
96 responseData->setContent(encodedControl);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070097
Vince Lehman5144f822014-07-23 15:12:56 -070098 m_keyChain.sign(*responseData);
Steve DiBenedetto86fce8e2014-03-06 12:16:20 -070099 m_face->put(*responseData);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700100}
101
Chengyu Fan320d2332014-10-29 16:40:33 -0600102// for future commit
103//void
104//ManagerBase::sendNack(const Name& name)
105//{
106// NFD_LOG_DEBUG("responding NACK to " << name);
107//
108// ndn::MetaInfo meta;
109// meta.setType(ndn::tlv::ContentType_Nack);
110//
111// shared_ptr<Data> responseData(make_shared<Data>(name));
112// responseData->setMetaInfo(meta);
113//
114// m_keyChain.sign(*responseData);
115// m_face->put(*responseData);
116//}
117
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600118bool
119ManagerBase::validateParameters(const ControlCommand& command,
120 ControlParameters& parameters)
121{
122 try
123 {
124 command.validateRequest(parameters);
125 }
126 catch (const ControlCommand::ArgumentError& error)
127 {
128 return false;
129 }
130
131 command.applyDefaultsToRequest(parameters);
132
133 return true;
134}
135
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700136void
137ManagerBase::onCommandValidationFailed(const shared_ptr<const Interest>& command,
138 const std::string& error)
139{
Alexander Afanasyevf4e89b42014-05-31 15:54:18 +0300140 NFD_LOG_DEBUG("command result: unauthorized command: " << *command << " (" << error << ")");
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700141 sendResponse(command->getName(), 403, "Unauthorized command");
142}
143
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700144
145} // namespace nfd