certificate-fetcher-from-network.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
23 #include "face.hpp"
24 #include "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 {
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
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
The certificate following the certificate format naming convention.
Definition: certificate.hpp:81
Interface used by the validator to fetch missing certificates.
represents an Interest packet
Definition: interest.hpp:42
std::function< void(const Certificate &cert, const shared_ptr< ValidationState > &state)> ValidationContinuation
#define NDN_LOG_INIT(name)
declare a log module
Definition: logger.hpp:101
represents a Network Nack
Definition: nack.hpp:40
NackReason getReason() const
Definition: nack.hpp:92
std::string toUri() const
Get URI representation of the name.
Definition: name.cpp:122
#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:95
const Name & getName() const
Get name.
Definition: data.hpp:121
const PendingInterestId * expressInterest(const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
Express Interest.
Definition: face.cpp:178
Represents a Data packet.
Definition: data.hpp:35
represents an error in TLV encoding or decoding
void doFetch(const shared_ptr< CertificateRequest > &certRequest, const shared_ptr< ValidationState > &state, const ValidationContinuation &continueValidation) override
Asynchronous certificate fetching implementation.