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-2019 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 #include "ndn-cxx/face.hpp"
24 #include "ndn-cxx/util/logger.hpp"
25 
26 namespace ndn {
27 namespace security {
28 namespace v2 {
29 
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 
36  : m_face(face)
37  , m_scheduler(face.getIoService())
38 {
39 }
40 
41 void
42 CertificateFetcherFromNetwork::doFetch(const shared_ptr<CertificateRequest>& certRequest,
43  const shared_ptr<ValidationState>& state,
44  const ValidationContinuation& continueValidation)
45 {
46  m_face.expressInterest(certRequest->interest,
47  [=] (const Interest& interest, const Data& data) {
48  dataCallback(data, certRequest, state, continueValidation);
49  },
50  [=] (const Interest& interest, const lp::Nack& nack) {
51  nackCallback(nack, certRequest, state, continueValidation);
52  },
53  [=] (const Interest& interest) {
54  timeoutCallback(certRequest, state, continueValidation);
55  });
56 }
57 
58 void
60  const shared_ptr<CertificateRequest>& certRequest,
61  const shared_ptr<ValidationState>& state,
62  const ValidationContinuation& continueValidation)
63 {
64  NDN_LOG_DEBUG_DEPTH("Fetched certificate from network " << data.getName());
65 
66  Certificate cert;
67  try {
68  cert = Certificate(data);
69  }
70  catch (const tlv::Error& e) {
71  return state->fail({ValidationError::Code::MALFORMED_CERT, "Fetched a malformed certificate "
72  "`" + data.getName().toUri() + "` (" + e.what() + ")"});
73  }
74  continueValidation(cert, state);
75 }
76 
77 void
79  const shared_ptr<CertificateRequest>& certRequest,
80  const shared_ptr<ValidationState>& state,
81  const ValidationContinuation& continueValidation)
82 {
83  NDN_LOG_DEBUG_DEPTH("NACK (" << nack.getReason() << ") while fetching certificate "
84  << certRequest->interest.getName());
85 
86  --certRequest->nRetriesLeft;
87  if (certRequest->nRetriesLeft >= 0) {
88  m_scheduler.schedule(certRequest->waitAfterNack,
89  [=] { fetch(certRequest, state, continueValidation); });
90  certRequest->waitAfterNack *= 2;
91  }
92  else {
93  state->fail({ValidationError::Code::CANNOT_RETRIEVE_CERT, "Cannot fetch certificate after all "
94  "retries `" + certRequest->interest.getName().toUri() + "`"});
95  }
96 }
97 
98 void
99 CertificateFetcherFromNetwork::timeoutCallback(const shared_ptr<CertificateRequest>& certRequest,
100  const shared_ptr<ValidationState>& state,
101  const ValidationContinuation& continueValidation)
102 {
103  NDN_LOG_DEBUG_DEPTH("Timeout while fetching certificate " << certRequest->interest.getName()
104  << ", retrying");
105 
106  --certRequest->nRetriesLeft;
107  if (certRequest->nRetriesLeft >= 0) {
108  fetch(certRequest, state, continueValidation);
109  }
110  else {
111  state->fail({ValidationError::Code::CANNOT_RETRIEVE_CERT, "Cannot fetch certificate after all "
112  "retries `" + certRequest->interest.getName().toUri() + "`"});
113  }
114 }
115 
116 } // namespace v2
117 } // namespace security
118 } // namespace ndn
NDN_LOG_INIT
#define NDN_LOG_INIT(name)
declare a log module
Definition: logger.hpp:81
ndn::security::v2::CertificateFetcherFromNetwork::CertificateFetcherFromNetwork
CertificateFetcherFromNetwork(Face &face)
Definition: certificate-fetcher-from-network.cpp:35
ndn::security::v2::CertificateFetcherFromNetwork::m_face
Face & m_face
Definition: certificate-fetcher-from-network.hpp:79
ndn::Face::expressInterest
PendingInterestHandle expressInterest(const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
Express Interest.
Definition: face.cpp:121
ndn::scheduler::Scheduler::schedule
EventId schedule(time::nanoseconds after, EventCallback callback)
Schedule a one-time event after the specified delay.
Definition: scheduler.cpp:96
ndn::Data::getName
const Name & getName() const
Get name.
Definition: data.hpp:124
ndn::Face
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:90
ndn::security::v2::CertificateFetcher
Interface used by the validator to fetch missing certificates.
Definition: certificate-fetcher.hpp:40
ndn::security::v2::Certificate
The certificate following the certificate format naming convention.
Definition: certificate.hpp:82
ndn::lp::Nack::getReason
NackReason getReason() const
Definition: nack.hpp:90
ndn::security::v2::CertificateFetcherFromNetwork::nackCallback
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.
Definition: certificate-fetcher-from-network.cpp:78
ndn::Interest
Represents an Interest packet.
Definition: interest.hpp:44
logger.hpp
ndn::Data
Represents a Data packet.
Definition: data.hpp:36
ndn::security::v2::CertificateFetcher::ValidationContinuation
std::function< void(const Certificate &cert, const shared_ptr< ValidationState > &state)> ValidationContinuation
Definition: certificate-fetcher.hpp:43
ndn::security::v2::CertificateFetcherFromNetwork::doFetch
void doFetch(const shared_ptr< CertificateRequest > &certRequest, const shared_ptr< ValidationState > &state, const ValidationContinuation &continueValidation) override
Asynchronous certificate fetching implementation.
Definition: certificate-fetcher-from-network.cpp:42
ndn::security
Definition: dummy-keychain.cpp:28
NDN_LOG_DEBUG_DEPTH
#define NDN_LOG_DEBUG_DEPTH(x)
Definition: certificate-fetcher-from-network.cpp:32
face.hpp
ndn::lp::Nack
represents a Network Nack
Definition: nack.hpp:39
ndn::security::v2::CertificateFetcherFromNetwork::timeoutCallback
void timeoutCallback(const shared_ptr< CertificateRequest > &certRequest, const shared_ptr< ValidationState > &state, const ValidationContinuation &continueValidation)
Callback invoked when interest for fetching certificate times out.
Definition: certificate-fetcher-from-network.cpp:99
ndn::security::v2::CertificateFetcherFromNetwork::m_scheduler
Scheduler m_scheduler
Definition: certificate-fetcher-from-network.hpp:80
ndn::security::v2
Definition: command-authenticator.hpp:35
ndn::tlv::Error
represents an error in TLV encoding or decoding
Definition: tlv.hpp:53
ndn::security::v2::CertificateFetcherFromNetwork::dataCallback
void dataCallback(const Data &data, const shared_ptr< CertificateRequest > &certRequest, const shared_ptr< ValidationState > &state, const ValidationContinuation &continueValidation)
Callback invoked when certificate is retrieved.
Definition: certificate-fetcher-from-network.cpp:59
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
certificate-fetcher-from-network.hpp
ndn::security::v2::CertificateFetcher::fetch
void fetch(const shared_ptr< CertificateRequest > &certRequest, const shared_ptr< ValidationState > &state, const ValidationContinuation &continueValidation)
Asynchronously fetch certificate.
Definition: certificate-fetcher.cpp:47