Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 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. |
| 20 | */ |
| 21 | |
| 22 | #include "certificate-fetcher-from-network.hpp" |
| 23 | #include "face.hpp" |
| 24 | #include "util/logger.hpp" |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace security { |
| 28 | namespace v2 { |
| 29 | |
| 30 | NDN_LOG_INIT(ndn.security.v2.CertificateFetcher); |
| 31 | |
| 32 | #define NDN_LOG_DEBUG_DEPTH(x) NDN_LOG_DEBUG(std::string(state->getDepth() + 1, '>') << " " << x) |
| 33 | #define NDN_LOG_TRACE_DEPTH(x) NDN_LOG_TRACE(std::string(state->getDepth() + 1, '>') << " " << x) |
| 34 | |
| 35 | CertificateFetcherFromNetwork::CertificateFetcherFromNetwork(Face& face) |
| 36 | : m_face(face) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | void |
| 41 | CertificateFetcherFromNetwork::doFetch(const shared_ptr<CertificateRequest>& certRequest, |
| 42 | const shared_ptr<ValidationState>& state, |
| 43 | const ValidationContinuation& continueValidation) |
| 44 | { |
| 45 | m_face.expressInterest(certRequest->m_interest, |
| 46 | [=] (const Interest& interest, const Data& data) { |
| 47 | dataCallback(data, certRequest, state, continueValidation); |
| 48 | }, |
| 49 | [=] (const Interest& interest, const lp::Nack& nack) { |
| 50 | nackCallback(nack, certRequest, state, continueValidation); |
| 51 | }, |
| 52 | [=] (const Interest& interest) { |
| 53 | timeoutCallback(certRequest, state, continueValidation); |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | CertificateFetcherFromNetwork::dataCallback(const Data& data, |
| 59 | const shared_ptr<CertificateRequest>& certRequest, |
| 60 | const shared_ptr<ValidationState>& state, |
| 61 | const ValidationContinuation& continueValidation) |
| 62 | { |
| 63 | NDN_LOG_DEBUG_DEPTH("Fetched certificate from network " << data.getName()); |
| 64 | |
| 65 | Certificate cert; |
| 66 | try { |
| 67 | cert = Certificate(data); |
| 68 | } |
| 69 | catch (const tlv::Error& e) { |
| 70 | return state->fail({ValidationError::Code::MALFORMED_CERT, "Fetched a malformed certificate " |
| 71 | "`" + data.getName().toUri() + "` (" + e.what() + ")"}); |
| 72 | } |
| 73 | continueValidation(cert, state); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | CertificateFetcherFromNetwork::nackCallback(const lp::Nack& nack, |
| 78 | const shared_ptr<CertificateRequest>& certRequest, |
| 79 | const shared_ptr<ValidationState>& state, |
| 80 | const ValidationContinuation& continueValidation) |
| 81 | { |
| 82 | NDN_LOG_DEBUG_DEPTH("NACK (" << nack.getReason() << ") while fetching certificate " |
| 83 | << certRequest->m_interest.getName()); |
| 84 | |
| 85 | --certRequest->m_nRetriesLeft; |
| 86 | if (certRequest->m_nRetriesLeft >= 0) { |
| 87 | // TODO implement delay for the the next fetch |
| 88 | fetch(certRequest, state, continueValidation); |
| 89 | } |
| 90 | else { |
| 91 | state->fail({ValidationError::Code::CANNOT_RETRIEVE_CERT, "Cannot fetch certificate after all " |
| 92 | "retries `" + certRequest->m_interest.getName().toUri() + "`"}); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | CertificateFetcherFromNetwork::timeoutCallback(const shared_ptr<CertificateRequest>& certRequest, |
| 98 | const shared_ptr<ValidationState>& state, |
| 99 | const ValidationContinuation& continueValidation) |
| 100 | { |
| 101 | NDN_LOG_DEBUG_DEPTH("Timeout while fetching certificate " << certRequest->m_interest.getName() |
| 102 | << ", retrying"); |
| 103 | |
| 104 | --certRequest->m_nRetriesLeft; |
| 105 | if (certRequest->m_nRetriesLeft >= 0) { |
| 106 | fetch(certRequest, state, continueValidation); |
| 107 | } |
| 108 | else { |
| 109 | state->fail({ValidationError::Code::CANNOT_RETRIEVE_CERT, "Cannot fetch certificate after all " |
| 110 | "retries `" + certRequest->m_interest.getName().toUri() + "`"}); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | } // namespace v2 |
| 115 | } // namespace security |
| 116 | } // namespace ndn |