blob: cf75d044cad3e8a136c5eeeaa691de7c189f5686 [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 Fanab205c22014-11-18 10:58:41 -0700102void
103ManagerBase::sendNack(const Name& name)
104{
105 NFD_LOG_DEBUG("responding NACK to " << name);
106
107 ndn::MetaInfo meta;
108 meta.setType(tlv::ContentType_Nack);
109
110 shared_ptr<Data> responseData(make_shared<Data>(name));
111 responseData->setMetaInfo(meta);
112
113 m_keyChain.sign(*responseData);
114 m_face->put(*responseData);
115}
Chengyu Fan320d2332014-10-29 16:40:33 -0600116
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600117bool
118ManagerBase::validateParameters(const ControlCommand& command,
119 ControlParameters& parameters)
120{
121 try
122 {
123 command.validateRequest(parameters);
124 }
125 catch (const ControlCommand::ArgumentError& error)
126 {
127 return false;
128 }
129
130 command.applyDefaultsToRequest(parameters);
131
132 return true;
133}
134
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700135void
136ManagerBase::onCommandValidationFailed(const shared_ptr<const Interest>& command,
137 const std::string& error)
138{
Alexander Afanasyevf4e89b42014-05-31 15:54:18 +0300139 NFD_LOG_DEBUG("command result: unauthorized command: " << *command << " (" << error << ")");
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700140 sendResponse(command->getName(), 403, "Unauthorized command");
141}
142
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700143
144} // namespace nfd