Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 8d60e64 | 2023-04-17 02:36:03 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 4 | * |
| 5 | * NAC library is free software: you can redistribute it and/or modify it under the |
| 6 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 7 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * NAC library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 11 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 14 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 15 | * <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * See AUTHORS.md for complete list of NAC library authors and contributors. |
| 18 | */ |
| 19 | |
| 20 | #include <ndn-cxx/face.hpp> |
| 21 | #include <ndn-cxx/security/validator-config.hpp> |
| 22 | |
| 23 | // #include <ndn-nac/decryptor.hpp> |
| 24 | #include "decryptor.hpp" |
| 25 | |
| 26 | #include <iostream> |
| 27 | |
| 28 | // Enclosing code in ndn simplifies coding (can also use `using namespace ndn`) |
| 29 | namespace ndn { |
| 30 | namespace nac { |
Davide Pesavento | 8d60e64 | 2023-04-17 02:36:03 -0400 | [diff] [blame] | 31 | // Additional nested namespaces should be used to prevent/limit name conflicts |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 32 | namespace examples { |
| 33 | |
Davide Pesavento | 8d60e64 | 2023-04-17 02:36:03 -0400 | [diff] [blame] | 34 | class Consumer |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 35 | { |
| 36 | public: |
| 37 | Consumer() |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 38 | : m_decryptor(m_keyChain.getPib().getDefaultIdentity().getDefaultKey(), |
| 39 | m_validator, m_keyChain, m_face) |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 40 | { |
| 41 | m_validator.load(R"CONF( |
| 42 | trust-anchor |
| 43 | { |
| 44 | type any |
| 45 | } |
| 46 | )CONF", "fake-config"); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | run() |
| 51 | { |
Davide Pesavento | 8d60e64 | 2023-04-17 02:36:03 -0400 | [diff] [blame] | 52 | using namespace std::placeholders; |
| 53 | |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 54 | Interest interest(Name("/example/testApp/randomData")); |
Junxiao Shi | 2f0a401 | 2022-09-12 22:02:29 +0000 | [diff] [blame] | 55 | interest.setCanBePrefix(true); |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 56 | interest.setMustBeFresh(true); |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 57 | interest.setInterestLifetime(3_s); // 3 seconds |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 58 | |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 59 | std::cout << "Sending Interest " << interest << std::endl; |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 60 | m_face.expressInterest(interest, |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 61 | std::bind(&Consumer::onData, this, _1, _2), |
| 62 | std::bind(&Consumer::onNack, this, _1, _2), |
| 63 | std::bind(&Consumer::onTimeout, this, _1)); |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 64 | |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 65 | // processEvents will block until the requested data is received or a timeout occurs |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 66 | m_face.processEvents(); |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | void |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 71 | onData(const Interest&, const Data& data) |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 72 | { |
| 73 | m_validator.validate(data, |
| 74 | [=] (const Data& data) { |
| 75 | m_decryptor.decrypt(data.getContent().blockFromValue(), |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 76 | [] (const ConstBufferPtr& content) { |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 77 | std::cout << "Decrypted content: " |
| 78 | << std::string(reinterpret_cast<const char*>(content->data()), content->size()) |
| 79 | << std::endl; |
| 80 | }, |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 81 | [] (const ErrorCode&, const std::string& error) { |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 82 | std::cerr << "Cannot decrypt data: " << error << std::endl; |
| 83 | }); |
| 84 | }, |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 85 | [] (const Data&, const ValidationError& error) { |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 86 | std::cerr << "Cannot validate retrieved data: " << error << std::endl; |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | void |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 91 | onNack(const Interest& interest, const lp::Nack& nack) const |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 92 | { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 93 | std::cout << "Received Nack with reason " << nack.getReason() |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 94 | << " for interest " << interest << std::endl; |
| 95 | } |
| 96 | |
| 97 | void |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 98 | onTimeout(const Interest& interest) const |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 99 | { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 100 | std::cout << "Timeout for " << interest << std::endl; |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | private: |
| 104 | KeyChain m_keyChain; |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 105 | Face m_face{nullptr, m_keyChain}; |
| 106 | ValidatorConfig m_validator{m_face}; |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 107 | Decryptor m_decryptor; |
| 108 | }; |
| 109 | |
| 110 | } // namespace examples |
| 111 | } // namespace nac |
| 112 | } // namespace ndn |
| 113 | |
| 114 | int |
| 115 | main(int argc, char** argv) |
| 116 | { |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 117 | try { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 118 | ndn::nac::examples::Consumer consumer; |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 119 | consumer.run(); |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 120 | return 0; |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 121 | } |
| 122 | catch (const std::exception& e) { |
| 123 | std::cerr << "ERROR: " << e.what() << std::endl; |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame] | 124 | return 1; |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 125 | } |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 126 | } |