Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 3 | * 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 24 | */ |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 25 | |
| 26 | #include "manager-base.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 27 | #include "core/logger.hpp" |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 28 | |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 29 | namespace nfd { |
| 30 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 31 | NFD_LOG_INIT("ManagerBase"); |
| 32 | |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 33 | ManagerBase::ManagerBase(shared_ptr<InternalFace> face, const std::string& privilege, |
| 34 | ndn::KeyChain& keyChain) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 35 | : m_face(face) |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 36 | , m_keyChain(keyChain) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 37 | { |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 38 | face->getValidator().addSupportedPrivilege(privilege); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | ManagerBase::~ManagerBase() |
| 42 | { |
| 43 | |
| 44 | } |
| 45 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 46 | bool |
| 47 | ManagerBase::extractParameters(const Name::Component& parameterComponent, |
| 48 | ControlParameters& extractedParameters) |
| 49 | { |
| 50 | try |
| 51 | { |
| 52 | Block rawParameters = parameterComponent.blockFromValue(); |
| 53 | extractedParameters.wireDecode(rawParameters); |
| 54 | } |
Junxiao Shi | 67f11ac | 2014-10-19 09:29:13 -0700 | [diff] [blame^] | 55 | catch (const tlv::Error&) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 56 | { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | NFD_LOG_DEBUG("Parameters parsed OK"); |
| 61 | return true; |
| 62 | } |
| 63 | |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 64 | void |
| 65 | ManagerBase::sendResponse(const Name& name, |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 66 | uint32_t code, |
| 67 | const std::string& text) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 68 | { |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 69 | ControlResponse response(code, text); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 70 | sendResponse(name, response); |
| 71 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 72 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 73 | void |
| 74 | ManagerBase::sendResponse(const Name& name, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 75 | 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 | |
| 84 | void |
| 85 | ManagerBase::sendResponse(const Name& name, |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 86 | const ControlResponse& response) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 87 | { |
| 88 | NFD_LOG_DEBUG("responding" |
| 89 | << " name: " << name |
| 90 | << " code: " << response.getCode() |
| 91 | << " text: " << response.getText()); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 92 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 93 | const Block& encodedControl = response.wireEncode(); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 94 | |
Steve DiBenedetto | 86fce8e | 2014-03-06 12:16:20 -0700 | [diff] [blame] | 95 | shared_ptr<Data> responseData(make_shared<Data>(name)); |
| 96 | responseData->setContent(encodedControl); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 97 | |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 98 | m_keyChain.sign(*responseData); |
Steve DiBenedetto | 86fce8e | 2014-03-06 12:16:20 -0700 | [diff] [blame] | 99 | m_face->put(*responseData); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 102 | bool |
| 103 | ManagerBase::validateParameters(const ControlCommand& command, |
| 104 | ControlParameters& parameters) |
| 105 | { |
| 106 | try |
| 107 | { |
| 108 | command.validateRequest(parameters); |
| 109 | } |
| 110 | catch (const ControlCommand::ArgumentError& error) |
| 111 | { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | command.applyDefaultsToRequest(parameters); |
| 116 | |
| 117 | return true; |
| 118 | } |
| 119 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 120 | void |
| 121 | ManagerBase::onCommandValidationFailed(const shared_ptr<const Interest>& command, |
| 122 | const std::string& error) |
| 123 | { |
Alexander Afanasyev | f4e89b4 | 2014-05-31 15:54:18 +0300 | [diff] [blame] | 124 | NFD_LOG_DEBUG("command result: unauthorized command: " << *command << " (" << error << ")"); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 125 | sendResponse(command->getName(), 403, "Unauthorized command"); |
| 126 | } |
| 127 | |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 128 | |
| 129 | } // namespace nfd |