blob: 1af70b7fb724612dcc81eaa8eb06730b26c9f99a [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -08002/*
Eric Newberrye345baa2018-05-23 18:17:07 -07003 * Copyright (c) 2013-2018 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Afanasyeve289b532014-02-09 22:14:44 -080020 */
21
Junxiao Shi7357ef22016-09-07 02:39:37 +000022#include "controller.hpp"
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080023#include "face.hpp"
24#include "security/v2/key-chain.hpp"
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080025#include "util/segment-fetcher.hpp"
26
27#include <boost/lexical_cast.hpp>
Alexander Afanasyeve289b532014-02-09 22:14:44 -080028
29namespace ndn {
30namespace nfd {
31
Junxiao Shi034c1882016-06-24 18:06:51 +000032using ndn::util::SegmentFetcher;
33
Junxiao Shib1990df2015-11-05 00:14:44 +000034const uint32_t Controller::ERROR_TIMEOUT = 10060; // WinSock ESAETIMEDOUT
35const uint32_t Controller::ERROR_NACK = 10800; // 10000 + TLV-TYPE of Nack header
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000036const uint32_t Controller::ERROR_VALIDATION = 10021; // 10000 + TLS1_ALERT_DECRYPTION_FAILED
Junxiao Shi5de006b2014-10-26 20:20:52 -070037const uint32_t Controller::ERROR_SERVER = 500;
38const uint32_t Controller::ERROR_LBOUND = 400;
39
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080040Controller::Controller(Face& face, KeyChain& keyChain, security::v2::Validator& validator)
Junxiao Shi70911652014-08-12 10:14:24 -070041 : m_face(face)
42 , m_keyChain(keyChain)
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000043 , m_validator(validator)
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080044 , m_signer(keyChain)
Junxiao Shi70911652014-08-12 10:14:24 -070045{
46}
47
48void
49Controller::startCommand(const shared_ptr<ControlCommand>& command,
50 const ControlParameters& parameters,
Davide Pesavento3b101d02018-07-21 22:44:09 -040051 const CommandSucceedCallback& onSuccess,
52 const CommandFailCallback& onFailure,
Junxiao Shi5de006b2014-10-26 20:20:52 -070053 const CommandOptions& options)
54{
55 Name requestName = command->getRequestName(options.getPrefix(), parameters);
Alexander Afanasyev80782e02017-01-04 13:16:54 -080056 Interest interest = m_signer.makeCommandInterest(requestName, options.getSigningInfo());
Junxiao Shi5de006b2014-10-26 20:20:52 -070057 interest.setInterestLifetime(options.getTimeout());
Junxiao Shi5de006b2014-10-26 20:20:52 -070058
59 m_face.expressInterest(interest,
Junxiao Shie7c7f152016-08-20 22:36:22 +000060 [=] (const Interest&, const Data& data) {
Davide Pesavento3b101d02018-07-21 22:44:09 -040061 processCommandResponse(data, command, onSuccess, onFailure);
Junxiao Shie7c7f152016-08-20 22:36:22 +000062 },
63 [=] (const Interest&, const lp::Nack&) {
Davide Pesavento3b101d02018-07-21 22:44:09 -040064 if (onFailure)
65 onFailure(ControlResponse(Controller::ERROR_NACK, "network Nack received"));
Junxiao Shie7c7f152016-08-20 22:36:22 +000066 },
67 [=] (const Interest&) {
Davide Pesavento3b101d02018-07-21 22:44:09 -040068 if (onFailure)
69 onFailure(ControlResponse(Controller::ERROR_TIMEOUT, "request timed out"));
Junxiao Shie7c7f152016-08-20 22:36:22 +000070 });
Junxiao Shi5de006b2014-10-26 20:20:52 -070071}
72
73void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070074Controller::processCommandResponse(const Data& data,
75 const shared_ptr<ControlCommand>& command,
76 const CommandSucceedCallback& onSuccess,
77 const CommandFailCallback& onFailure)
78{
Junxiao Shi54f727d2016-08-08 20:29:11 +000079 m_validator.validate(data,
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080080 [=] (const Data& data) {
Davide Pesavento3b101d02018-07-21 22:44:09 -040081 processValidatedCommandResponse(data, command, onSuccess, onFailure);
Junxiao Shi54f727d2016-08-08 20:29:11 +000082 },
Davide Pesavento3b101d02018-07-21 22:44:09 -040083 [=] (const Data&, const auto& error) {
84 if (onFailure)
85 onFailure(ControlResponse(ERROR_VALIDATION, boost::lexical_cast<std::string>(error)));
Junxiao Shi54f727d2016-08-08 20:29:11 +000086 }
87 );
88}
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070089
Junxiao Shi54f727d2016-08-08 20:29:11 +000090void
91Controller::processValidatedCommandResponse(const Data& data,
92 const shared_ptr<ControlCommand>& command,
93 const CommandSucceedCallback& onSuccess,
94 const CommandFailCallback& onFailure)
95{
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070096 ControlResponse response;
97 try {
98 response.wireDecode(data.getContent().blockFromValue());
99 }
Junxiao Shi600f7112016-07-16 11:57:18 +0000100 catch (const tlv::Error& e) {
Davide Pesavento3b101d02018-07-21 22:44:09 -0400101 if (onFailure)
102 onFailure(ControlResponse(ERROR_SERVER, e.what()));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700103 return;
104 }
105
106 uint32_t code = response.getCode();
Junxiao Shi5de006b2014-10-26 20:20:52 -0700107 if (code >= ERROR_LBOUND) {
Davide Pesavento3b101d02018-07-21 22:44:09 -0400108 if (onFailure)
109 onFailure(response);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700110 return;
111 }
112
113 ControlParameters parameters;
114 try {
115 parameters.wireDecode(response.getBody());
116 }
Junxiao Shi600f7112016-07-16 11:57:18 +0000117 catch (const tlv::Error& e) {
Davide Pesavento3b101d02018-07-21 22:44:09 -0400118 if (onFailure)
119 onFailure(ControlResponse(ERROR_SERVER, e.what()));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700120 return;
121 }
122
123 try {
124 command->validateResponse(parameters);
125 }
Junxiao Shi600f7112016-07-16 11:57:18 +0000126 catch (const ControlCommand::ArgumentError& e) {
Davide Pesavento3b101d02018-07-21 22:44:09 -0400127 if (onFailure)
128 onFailure(ControlResponse(ERROR_SERVER, e.what()));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700129 return;
130 }
131
Davide Pesavento3b101d02018-07-21 22:44:09 -0400132 if (onSuccess)
133 onSuccess(parameters);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700134}
135
Junxiao Shi034c1882016-06-24 18:06:51 +0000136void
137Controller::fetchDataset(const Name& prefix,
Eric Newberrye345baa2018-05-23 18:17:07 -0700138 const std::function<void(ConstBufferPtr)>& processResponse,
Junxiao Shie7c7f152016-08-20 22:36:22 +0000139 const DatasetFailCallback& onFailure,
Junxiao Shi034c1882016-06-24 18:06:51 +0000140 const CommandOptions& options)
141{
Eric Newberrye345baa2018-05-23 18:17:07 -0700142 SegmentFetcher::Options fetcherOptions;
143 fetcherOptions.maxTimeout = options.getTimeout();
Davide Pesavento3b101d02018-07-21 22:44:09 -0400144
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 Newberrye345baa2018-05-23 18:17:07 -0700151 processDatasetFetchError(onFailure, code, msg);
152 });
Davide Pesavento3b101d02018-07-21 22:44:09 -0400153 }
Junxiao Shi034c1882016-06-24 18:06:51 +0000154}
155
156void
Junxiao Shie7c7f152016-08-20 22:36:22 +0000157Controller::processDatasetFetchError(const DatasetFailCallback& onFailure,
Junxiao Shi034c1882016-06-24 18:06:51 +0000158 uint32_t code, std::string msg)
159{
Davide Pesavento3b101d02018-07-21 22:44:09 -0400160 BOOST_ASSERT(onFailure);
161
Junxiao Shi034c1882016-06-24 18:06:51 +0000162 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 Newberrye345baa2018-05-23 18:17:07 -0700170 case SegmentFetcher::ErrorCode::FINALBLOCKID_NOT_SEGMENT:
Junxiao Shi034c1882016-06-24 18:06:51 +0000171 onFailure(ERROR_SERVER, msg);
172 break;
173 case SegmentFetcher::ErrorCode::SEGMENT_VALIDATION_FAIL:
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000174 /// \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 Shi034c1882016-06-24 18:06:51 +0000177 break;
178 case SegmentFetcher::ErrorCode::NACK_ERROR:
179 onFailure(ERROR_NACK, msg);
180 break;
181 }
182}
183
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800184} // namespace nfd
185} // namespace ndn