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