NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2020 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::Code::MALFORMED_CERT, "Fetched a malformed certificate "
75  "`" + data.getName().toUri() + "` (" + e.what() + ")"});
76  }
77  continueValidation(cert, state);
78 }
79 
80 void
82  const shared_ptr<CertificateRequest>& certRequest,
83  const shared_ptr<ValidationState>& state,
84  const ValidationContinuation& continueValidation)
85 {
86  NDN_LOG_DEBUG_DEPTH("NACK (" << nack.getReason() << ") while fetching certificate "
87  << certRequest->interest.getName());
88 
89  --certRequest->nRetriesLeft;
90  if (certRequest->nRetriesLeft >= 0) {
91  m_scheduler.schedule(certRequest->waitAfterNack,
92  [=] { fetch(certRequest, state, continueValidation); });
93  certRequest->waitAfterNack *= 2;
94  }
95  else {
96  state->fail({ValidationError::Code::CANNOT_RETRIEVE_CERT, "Cannot fetch certificate after all "
97  "retries `" + certRequest->interest.getName().toUri() + "`"});
98  }
99 }
100 
101 void
102 CertificateFetcherFromNetwork::timeoutCallback(const shared_ptr<CertificateRequest>& certRequest,
103  const shared_ptr<ValidationState>& state,
104  const ValidationContinuation& continueValidation)
105 {
106  NDN_LOG_DEBUG_DEPTH("Timeout while fetching certificate " << certRequest->interest.getName()
107  << ", retrying");
108 
109  --certRequest->nRetriesLeft;
110  if (certRequest->nRetriesLeft >= 0) {
111  fetch(certRequest, state, continueValidation);
112  }
113  else {
114  state->fail({ValidationError::Code::CANNOT_RETRIEVE_CERT, "Cannot fetch certificate after all "
115  "retries `" + certRequest->interest.getName().toUri() + "`"});
116  }
117 }
118 
119 } // inline namespace v2
120 } // namespace security
121 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
Represents an NDN certificate following the version 2.0 format.
Definition: certificate.hpp:60
Interface used by the validator to fetch missing certificates.
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.
Represents an Interest packet.
Definition: interest.hpp:48
std::function< void(const Certificate &cert, const shared_ptr< ValidationState > &state)> ValidationContinuation
EventId schedule(time::nanoseconds after, EventCallback callback)
Schedule a one-time event after the specified delay.
Definition: scheduler.cpp:96
represents a Network Nack
Definition: nack.hpp:38
NackReason getReason() const
Definition: nack.hpp:90
#define NDN_LOG_DEBUG_DEPTH(x)
void fetch(const shared_ptr< CertificateRequest > &certRequest, const shared_ptr< ValidationState > &state, const ValidationContinuation &continueValidation)
Asynchronously fetch certificate.
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:90
const Name & getName() const noexcept
Get name.
Definition: data.hpp:127
void dataCallback(const Data &data, const shared_ptr< CertificateRequest > &certRequest, const shared_ptr< ValidationState > &state, const ValidationContinuation &continueValidation)
Callback invoked when certificate is retrieved.
PendingInterestHandle expressInterest(const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
Express Interest.
Definition: face.cpp:123
void toUri(std::ostream &os, name::UriFormat format=name::UriFormat::DEFAULT) const
Write URI representation of the name to the output stream.
Definition: name.cpp:349
#define NDN_LOG_INIT(name)
declare a log module
Definition: logger.hpp:81
void timeoutCallback(const shared_ptr< CertificateRequest > &certRequest, const shared_ptr< ValidationState > &state, const ValidationContinuation &continueValidation)
Callback invoked when interest for fetching certificate times out.
Represents a Data packet.
Definition: data.hpp:37
represents an error in TLV encoding or decoding
Definition: tlv.hpp:52
void doFetch(const shared_ptr< CertificateRequest > &certRequest, const shared_ptr< ValidationState > &state, const ValidationContinuation &continueValidation) override
Asynchronous certificate fetching implementation.