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 | /* |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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" |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 25 | #include "util/segment-fetcher.hpp" |
| 26 | |
| 27 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace nfd { |
| 31 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 32 | using ndn::util::SegmentFetcher; |
| 33 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 34 | const uint32_t Controller::ERROR_TIMEOUT = 10060; // WinSock ESAETIMEDOUT |
| 35 | 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] | 36 | const uint32_t Controller::ERROR_VALIDATION = 10021; // 10000 + TLS1_ALERT_DECRYPTION_FAILED |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 37 | const uint32_t Controller::ERROR_SERVER = 500; |
| 38 | const uint32_t Controller::ERROR_LBOUND = 400; |
| 39 | |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 40 | Controller::Controller(Face& face, KeyChain& keyChain, security::v2::Validator& validator) |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 41 | : m_face(face) |
| 42 | , m_keyChain(keyChain) |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 43 | , m_validator(validator) |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 44 | , m_signer(keyChain) |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 45 | { |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | Controller::startCommand(const shared_ptr<ControlCommand>& command, |
| 50 | const ControlParameters& parameters, |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 51 | const CommandSucceedCallback& onSuccess, |
| 52 | const CommandFailCallback& onFailure, |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 53 | const CommandOptions& options) |
| 54 | { |
| 55 | Name requestName = command->getRequestName(options.getPrefix(), parameters); |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 56 | Interest interest = m_signer.makeCommandInterest(requestName, options.getSigningInfo()); |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 57 | interest.setInterestLifetime(options.getTimeout()); |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 58 | |
| 59 | m_face.expressInterest(interest, |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame] | 60 | [=] (const Interest&, const Data& data) { |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 61 | processCommandResponse(data, command, onSuccess, onFailure); |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame] | 62 | }, |
| 63 | [=] (const Interest&, const lp::Nack&) { |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 64 | if (onFailure) |
| 65 | onFailure(ControlResponse(Controller::ERROR_NACK, "network Nack received")); |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame] | 66 | }, |
| 67 | [=] (const Interest&) { |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 68 | if (onFailure) |
| 69 | onFailure(ControlResponse(Controller::ERROR_TIMEOUT, "request timed out")); |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame] | 70 | }); |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 74 | Controller::processCommandResponse(const Data& data, |
| 75 | const shared_ptr<ControlCommand>& command, |
| 76 | const CommandSucceedCallback& onSuccess, |
| 77 | const CommandFailCallback& onFailure) |
| 78 | { |
Junxiao Shi | 54f727d | 2016-08-08 20:29:11 +0000 | [diff] [blame] | 79 | m_validator.validate(data, |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 80 | [=] (const Data& data) { |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 81 | processValidatedCommandResponse(data, command, onSuccess, onFailure); |
Junxiao Shi | 54f727d | 2016-08-08 20:29:11 +0000 | [diff] [blame] | 82 | }, |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 83 | [=] (const Data&, const auto& error) { |
| 84 | if (onFailure) |
| 85 | onFailure(ControlResponse(ERROR_VALIDATION, boost::lexical_cast<std::string>(error))); |
Junxiao Shi | 54f727d | 2016-08-08 20:29:11 +0000 | [diff] [blame] | 86 | } |
| 87 | ); |
| 88 | } |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 89 | |
Junxiao Shi | 54f727d | 2016-08-08 20:29:11 +0000 | [diff] [blame] | 90 | void |
| 91 | Controller::processValidatedCommandResponse(const Data& data, |
| 92 | const shared_ptr<ControlCommand>& command, |
| 93 | const CommandSucceedCallback& onSuccess, |
| 94 | const CommandFailCallback& onFailure) |
| 95 | { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 96 | ControlResponse response; |
| 97 | try { |
| 98 | response.wireDecode(data.getContent().blockFromValue()); |
| 99 | } |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame] | 100 | catch (const tlv::Error& e) { |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 101 | if (onFailure) |
| 102 | onFailure(ControlResponse(ERROR_SERVER, e.what())); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | |
| 106 | uint32_t code = response.getCode(); |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 107 | if (code >= ERROR_LBOUND) { |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 108 | if (onFailure) |
| 109 | onFailure(response); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | |
| 113 | ControlParameters parameters; |
| 114 | try { |
| 115 | parameters.wireDecode(response.getBody()); |
| 116 | } |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame] | 117 | catch (const tlv::Error& e) { |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 118 | if (onFailure) |
| 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) { |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 127 | if (onFailure) |
| 128 | onFailure(ControlResponse(ERROR_SERVER, e.what())); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 132 | if (onSuccess) |
| 133 | onSuccess(parameters); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 136 | void |
| 137 | Controller::fetchDataset(const Name& prefix, |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 138 | const std::function<void(ConstBufferPtr)>& processResponse, |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame] | 139 | const DatasetFailCallback& onFailure, |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 140 | const CommandOptions& options) |
| 141 | { |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 142 | SegmentFetcher::Options fetcherOptions; |
| 143 | fetcherOptions.maxTimeout = options.getTimeout(); |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 144 | |
| 145 | auto fetcher = SegmentFetcher::start(m_face, Interest(prefix), m_validator, fetcherOptions); |
| 146 | if (processResponse) { |
| 147 | fetcher->onComplete.connect(processResponse); |
| 148 | } |
| 149 | if (onFailure) { |
| 150 | fetcher->onError.connect([=] (uint32_t code, const std::string& msg) { |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 151 | processDatasetFetchError(onFailure, code, msg); |
| 152 | }); |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 153 | } |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame] | 157 | Controller::processDatasetFetchError(const DatasetFailCallback& onFailure, |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 158 | uint32_t code, std::string msg) |
| 159 | { |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 160 | BOOST_ASSERT(onFailure); |
| 161 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 162 | switch (static_cast<SegmentFetcher::ErrorCode>(code)) { |
| 163 | // It's intentional to cast as SegmentFetcher::ErrorCode, and to not have a 'default' clause. |
| 164 | // This forces the switch statement to handle every defined SegmentFetcher::ErrorCode, |
| 165 | // and breaks compilation if it does not. |
| 166 | case SegmentFetcher::ErrorCode::INTEREST_TIMEOUT: |
| 167 | onFailure(ERROR_TIMEOUT, msg); |
| 168 | break; |
| 169 | case SegmentFetcher::ErrorCode::DATA_HAS_NO_SEGMENT: |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 170 | case SegmentFetcher::ErrorCode::FINALBLOCKID_NOT_SEGMENT: |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 171 | onFailure(ERROR_SERVER, msg); |
| 172 | break; |
| 173 | case SegmentFetcher::ErrorCode::SEGMENT_VALIDATION_FAIL: |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 174 | /// \todo When SegmentFetcher exposes validator error code, Controller::ERROR_VALIDATION |
| 175 | /// should be replaced with a range that corresponds to validator error codes. |
| 176 | onFailure(ERROR_VALIDATION, msg); |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 177 | break; |
| 178 | case SegmentFetcher::ErrorCode::NACK_ERROR: |
| 179 | onFailure(ERROR_NACK, msg); |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 184 | } // namespace nfd |
| 185 | } // namespace ndn |