certificate-fetcher-direct-fetch.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"
25 #include "ndn-cxx/lp/tags.hpp"
28 
29 namespace ndn {
30 namespace security {
31 inline namespace v2 {
32 
35 {
36 }
37 
38 void
40 {
41  m_wantDirectInterestOnly = wantDirectInterestOnly;
42 }
43 
44 void
45 CertificateFetcherDirectFetch::doFetch(const shared_ptr<CertificateRequest>& keyRequest,
46  const shared_ptr<ValidationState>& state,
47  const ValidationContinuation& continueValidation)
48 {
49  uint64_t incomingFaceId = 0;
50  auto interestState = dynamic_pointer_cast<InterestValidationState>(state);
51  if (interestState != nullptr) {
52  auto incomingFaceIdTag = interestState->getOriginalInterest().getTag<lp::IncomingFaceIdTag>();
53  if (incomingFaceIdTag != nullptr) {
54  incomingFaceId = incomingFaceIdTag->get();
55  }
56  }
57  else {
58  auto dataState = dynamic_pointer_cast<DataValidationState>(state);
59  auto incomingFaceIdTag = dataState->getOriginalData().getTag<lp::IncomingFaceIdTag>();
60  if (incomingFaceIdTag != nullptr) {
61  incomingFaceId = incomingFaceIdTag->get();
62  }
63  }
64 
65  if (incomingFaceId != 0) {
66  Interest directInterest(keyRequest->interest);
67  directInterest.refreshNonce();
68  directInterest.setTag(make_shared<lp::NextHopFaceIdTag>(incomingFaceId));
69 
70  if (!m_wantDirectInterestOnly) {
71  // disable callbacks
72  m_face.expressInterest(directInterest, nullptr, nullptr, nullptr);
73  }
74  else {
75  m_face.expressInterest(directInterest,
76  [=] (const Interest&, const Data& data) {
77  dataCallback(data, keyRequest, state, continueValidation);
78  },
79  [=] (const Interest&, const lp::Nack& nack) {
80  nackCallback(nack, keyRequest, state, continueValidation);
81  },
82  [=] (const Interest&) {
83  timeoutCallback(keyRequest, state, continueValidation);
84  });
85  }
86  }
87 
88  if (!m_wantDirectInterestOnly) {
89  // send infrastructure Interest
90  CertificateFetcherFromNetwork::doFetch(keyRequest, state, continueValidation);
91  }
92  else if (incomingFaceId == 0) {
93  state->fail({ValidationError::CANNOT_RETRIEVE_CERT, "IncomingFaceId not set"});
94  }
95 }
96 
97 } // inline namespace v2
98 } // namespace security
99 } // namespace ndn
Represents a Data packet.
Definition: data.hpp:39
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
void refreshNonce()
Change nonce value.
Definition: interest.cpp:424
Provides a tag type for simple types.
Definition: tag.hpp:56
constexpr const T & get() const noexcept
Returns the enclosed value.
Definition: tag.hpp:86
void setTag(shared_ptr< T > tag) const
Set (add or replace) a tag item.
Definition: tag-host.hpp:80
Represents a Network Nack.
Definition: nack.hpp:40
void setSendDirectInterestOnly(bool wantDirectInterestOnly)
If wantDirectInterestOnly, only the direct Interest will be sent out.
void doFetch(const shared_ptr< CertificateRequest > &keyRequest, const shared_ptr< ValidationState > &state, const ValidationContinuation &continueValidation) override
Asynchronous certificate fetching implementation.
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.
std::function< void(const Certificate &cert, const shared_ptr< ValidationState > &state)> ValidationContinuation
@ CANNOT_RETRIEVE_CERT
The certificate cannot be retrieved.
Definition: data.cpp:25