Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 22 | #include "controller.hpp" |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 23 | #include "face.hpp" |
| 24 | #include "security/v2/key-chain.hpp" |
| 25 | #include "security/validator-null.hpp" |
| 26 | #include "util/segment-fetcher.hpp" |
| 27 | |
| 28 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | namespace nfd { |
| 32 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 33 | using ndn::util::SegmentFetcher; |
| 34 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 35 | const uint32_t Controller::ERROR_TIMEOUT = 10060; // WinSock ESAETIMEDOUT |
| 36 | const uint32_t Controller::ERROR_NACK = 10800; // 10000 + TLV-TYPE of Nack header |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 37 | const uint32_t Controller::ERROR_VALIDATION = 10021; // 10000 + TLS1_ALERT_DECRYPTION_FAILED |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 38 | const uint32_t Controller::ERROR_SERVER = 500; |
| 39 | const uint32_t Controller::ERROR_LBOUND = 400; |
| 40 | |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 41 | Controller::Controller(Face& face, KeyChain& keyChain, security::v2::Validator& validator) |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 42 | : m_face(face) |
| 43 | , m_keyChain(keyChain) |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 44 | , m_validator(validator) |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 45 | , m_signer(keyChain) |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | Controller::startCommand(const shared_ptr<ControlCommand>& command, |
| 51 | const ControlParameters& parameters, |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame] | 52 | const CommandSucceedCallback& onSuccess1, |
| 53 | const CommandFailCallback& onFailure1, |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 54 | const CommandOptions& options) |
| 55 | { |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame] | 56 | const CommandSucceedCallback& onSuccess = onSuccess1 ? |
| 57 | onSuccess1 : [] (const ControlParameters&) {}; |
| 58 | const CommandFailCallback& onFailure = onFailure1 ? |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame] | 59 | onFailure1 : [] (const ControlResponse&) {}; |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame] | 60 | |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 61 | Name requestName = command->getRequestName(options.getPrefix(), parameters); |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 62 | Interest interest = m_signer.makeCommandInterest(requestName, options.getSigningInfo()); |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 63 | interest.setInterestLifetime(options.getTimeout()); |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 64 | |
| 65 | m_face.expressInterest(interest, |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame] | 66 | [=] (const Interest&, const Data& data) { |
| 67 | this->processCommandResponse(data, command, onSuccess, onFailure); |
| 68 | }, |
| 69 | [=] (const Interest&, const lp::Nack&) { |
| 70 | onFailure(ControlResponse(Controller::ERROR_NACK, "network Nack received")); |
| 71 | }, |
| 72 | [=] (const Interest&) { |
| 73 | onFailure(ControlResponse(Controller::ERROR_TIMEOUT, "request timed out")); |
| 74 | }); |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 78 | Controller::processCommandResponse(const Data& data, |
| 79 | const shared_ptr<ControlCommand>& command, |
| 80 | const CommandSucceedCallback& onSuccess, |
| 81 | const CommandFailCallback& onFailure) |
| 82 | { |
Junxiao Shi | 54f727d | 2016-08-08 20:29:11 +0000 | [diff] [blame] | 83 | m_validator.validate(data, |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 84 | [=] (const Data& data) { |
| 85 | this->processValidatedCommandResponse(data, command, onSuccess, onFailure); |
Junxiao Shi | 54f727d | 2016-08-08 20:29:11 +0000 | [diff] [blame] | 86 | }, |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 87 | [=] (const Data& data, const security::v2::ValidationError& error) { |
| 88 | onFailure(ControlResponse(ERROR_VALIDATION, boost::lexical_cast<std::string>(error))); |
Junxiao Shi | 54f727d | 2016-08-08 20:29:11 +0000 | [diff] [blame] | 89 | } |
| 90 | ); |
| 91 | } |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 92 | |
Junxiao Shi | 54f727d | 2016-08-08 20:29:11 +0000 | [diff] [blame] | 93 | void |
| 94 | Controller::processValidatedCommandResponse(const Data& data, |
| 95 | const shared_ptr<ControlCommand>& command, |
| 96 | const CommandSucceedCallback& onSuccess, |
| 97 | const CommandFailCallback& onFailure) |
| 98 | { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 99 | ControlResponse response; |
| 100 | try { |
| 101 | response.wireDecode(data.getContent().blockFromValue()); |
| 102 | } |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame] | 103 | catch (const tlv::Error& e) { |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame] | 104 | onFailure(ControlResponse(ERROR_SERVER, e.what())); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 105 | return; |
| 106 | } |
| 107 | |
| 108 | uint32_t code = response.getCode(); |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 109 | if (code >= ERROR_LBOUND) { |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame] | 110 | onFailure(response); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 111 | return; |
| 112 | } |
| 113 | |
| 114 | ControlParameters parameters; |
| 115 | try { |
| 116 | parameters.wireDecode(response.getBody()); |
| 117 | } |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame] | 118 | catch (const tlv::Error& e) { |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame] | 119 | onFailure(ControlResponse(ERROR_SERVER, e.what())); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 120 | return; |
| 121 | } |
| 122 | |
| 123 | try { |
| 124 | command->validateResponse(parameters); |
| 125 | } |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame] | 126 | catch (const ControlCommand::ArgumentError& e) { |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame] | 127 | onFailure(ControlResponse(ERROR_SERVER, e.what())); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 128 | return; |
| 129 | } |
| 130 | |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame] | 131 | onSuccess(parameters); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 134 | void |
| 135 | Controller::fetchDataset(const Name& prefix, |
| 136 | const std::function<void(const ConstBufferPtr&)>& processResponse, |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame] | 137 | const DatasetFailCallback& onFailure, |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 138 | const CommandOptions& options) |
| 139 | { |
| 140 | Interest baseInterest(prefix); |
| 141 | baseInterest.setInterestLifetime(options.getTimeout()); |
| 142 | |
| 143 | SegmentFetcher::fetch(m_face, baseInterest, m_validator, processResponse, |
| 144 | bind(&Controller::processDatasetFetchError, this, onFailure, _1, _2)); |
| 145 | } |
| 146 | |
| 147 | void |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame] | 148 | Controller::processDatasetFetchError(const DatasetFailCallback& onFailure, |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 149 | uint32_t code, std::string msg) |
| 150 | { |
| 151 | switch (static_cast<SegmentFetcher::ErrorCode>(code)) { |
| 152 | // It's intentional to cast as SegmentFetcher::ErrorCode, and to not have a 'default' clause. |
| 153 | // This forces the switch statement to handle every defined SegmentFetcher::ErrorCode, |
| 154 | // and breaks compilation if it does not. |
| 155 | case SegmentFetcher::ErrorCode::INTEREST_TIMEOUT: |
| 156 | onFailure(ERROR_TIMEOUT, msg); |
| 157 | break; |
| 158 | case SegmentFetcher::ErrorCode::DATA_HAS_NO_SEGMENT: |
| 159 | onFailure(ERROR_SERVER, msg); |
| 160 | break; |
| 161 | case SegmentFetcher::ErrorCode::SEGMENT_VALIDATION_FAIL: |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 162 | /// \todo When SegmentFetcher exposes validator error code, Controller::ERROR_VALIDATION |
| 163 | /// should be replaced with a range that corresponds to validator error codes. |
| 164 | onFailure(ERROR_VALIDATION, msg); |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 165 | break; |
| 166 | case SegmentFetcher::ErrorCode::NACK_ERROR: |
| 167 | onFailure(ERROR_NACK, msg); |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 172 | } // namespace nfd |
| 173 | } // namespace ndn |