certificate-fetcher-from-network.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2022 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 
23 
24 #include "ndn-cxx/face.hpp"
27 #include "ndn-cxx/util/logger.hpp"
28 
29 namespace ndn {
30 namespace security {
31 inline namespace v2 {
32 
34 
35 #define NDN_LOG_DEBUG_DEPTH(x) NDN_LOG_DEBUG(std::string(state->getDepth() + 1, '>') << " " << x)
36 #define NDN_LOG_TRACE_DEPTH(x) NDN_LOG_TRACE(std::string(state->getDepth() + 1, '>') << " " << x)
37 
39  : m_face(face)
40  , m_scheduler(face.getIoService())
41 {
42 }
43 
44 void
45 CertificateFetcherFromNetwork::doFetch(const shared_ptr<CertificateRequest>& certRequest,
46  const shared_ptr<ValidationState>& state,
47  const ValidationContinuation& continueValidation)
48 {
49  m_face.expressInterest(certRequest->interest,
50  [=] (const Interest&, const Data& data) {
51  dataCallback(data, certRequest, state, continueValidation);
52  },
53  [=] (const Interest&, const lp::Nack& nack) {
54  nackCallback(nack, certRequest, state, continueValidation);
55  },
56  [=] (const Interest&) {
57  timeoutCallback(certRequest, state, continueValidation);
58  });
59 }
60 
61 void
63  const shared_ptr<CertificateRequest>&,
64  const shared_ptr<ValidationState>& state,
65  const ValidationContinuation& continueValidation)
66 {
67  NDN_LOG_DEBUG_DEPTH("Fetched certificate from network " << data.getName());
68 
69  Certificate cert;
70  try {
71  cert = Certificate(data);
72  }
73  catch (const tlv::Error& e) {
74  return state->fail({ValidationError::MALFORMED_CERT, "`" + data.getName().toUri() + "`: " + e.what()});
75  }
76  continueValidation(cert, state);
77 }
78 
79 void
81  const shared_ptr<CertificateRequest>& certRequest,
82  const shared_ptr<ValidationState>& state,
83  const ValidationContinuation& continueValidation)
84 {
85  NDN_LOG_DEBUG_DEPTH("Nack (" << nack.getReason() << ") while fetching certificate "
86  << certRequest->interest.getName());
87 
88  --certRequest->nRetriesLeft;
89  if (certRequest->nRetriesLeft >= 0) {
90  m_scheduler.schedule(certRequest->waitAfterNack,
91  [=] { fetch(certRequest, state, continueValidation); });
92  certRequest->waitAfterNack *= 2;
93  }
94  else {
95  state->fail({ValidationError::CANNOT_RETRIEVE_CERT, "Nack after exhausting all retries for "
96  "`" + certRequest->interest.getName().toUri() + "`"});
97  }
98 }
99 
100 void
101 CertificateFetcherFromNetwork::timeoutCallback(const shared_ptr<CertificateRequest>& certRequest,
102  const shared_ptr<ValidationState>& state,
103  const ValidationContinuation& continueValidation)
104 {
105  NDN_LOG_DEBUG_DEPTH("Timeout while fetching certificate " << certRequest->interest.getName());
106 
107  --certRequest->nRetriesLeft;
108  if (certRequest->nRetriesLeft >= 0) {
109  fetch(certRequest, state, continueValidation);
110  }
111  else {
112  state->fail({ValidationError::CANNOT_RETRIEVE_CERT, "Timeout after exhausting all retries for "
113  "`" + certRequest->interest.getName().toUri() + "`"});
114  }
115 }
116 
117 } // inline namespace v2
118 } // namespace security
119 } // namespace ndn
#define NDN_LOG_DEBUG_DEPTH(x)
Represents a Data packet.
Definition: data.hpp:39
const Name & getName() const noexcept
Get the data name.
Definition: data.hpp:129
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:91
PendingInterestHandle expressInterest(const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
Express an Interest.
Definition: face.cpp:164
Represents an Interest packet.
Definition: interest.hpp:50
Represents a Network Nack.
Definition: nack.hpp:40
NackReason getReason() const
Definition: nack.hpp:91
EventId schedule(time::nanoseconds after, EventCallback callback)
Schedule a one-time event after the specified delay.
Definition: scheduler.cpp:92
void doFetch(const shared_ptr< CertificateRequest > &certRequest, const shared_ptr< ValidationState > &state, const ValidationContinuation &continueValidation) override
Asynchronous certificate fetching implementation.
void dataCallback(const Data &data, const shared_ptr< CertificateRequest > &certRequest, const shared_ptr< ValidationState > &state, const ValidationContinuation &continueValidation)
Callback invoked when certificate is retrieved.
void nackCallback(const lp::Nack &nack, const shared_ptr< CertificateRequest > &certRequest, const shared_ptr< ValidationState > &state, const ValidationContinuation &continueValidation)
Callback invoked when interest for fetching certificate gets NACKed.
void timeoutCallback(const shared_ptr< CertificateRequest > &certRequest, const shared_ptr< ValidationState > &state, const ValidationContinuation &continueValidation)
Callback invoked when interest for fetching certificate times out.
Interface used by the validator to fetch missing certificates.
void fetch(const shared_ptr< CertificateRequest > &certRequest, const shared_ptr< ValidationState > &state, const ValidationContinuation &continueValidation)
Asynchronously fetch certificate.
std::function< void(const Certificate &cert, const shared_ptr< ValidationState > &state)> ValidationContinuation
Represents an NDN certificate.
Definition: certificate.hpp:60
@ MALFORMED_CERT
The certificate is malformed.
@ CANNOT_RETRIEVE_CERT
The certificate cannot be retrieved.
Represents an error in TLV encoding or decoding.
Definition: tlv.hpp:54
#define NDN_LOG_INIT(name)
Define a non-member log module.
Definition: logger.hpp:163
Definition: data.cpp:25