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 | /* |
| 3 | * Copyright (c) 2014-2022, 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 { |
| 31 | // Additional nested namespaces can be used to prevent/limit name conflicts |
| 32 | namespace examples { |
| 33 | |
| 34 | class Consumer : noncopyable |
| 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 | { |
| 52 | Interest interest(Name("/example/testApp/randomData")); |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 53 | interest.setMustBeFresh(true); |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 54 | interest.setInterestLifetime(3_s); // 3 seconds |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 55 | |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 56 | std::cout << "Sending Interest " << interest << std::endl; |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 57 | m_face.expressInterest(interest, |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 58 | std::bind(&Consumer::onData, this, _1, _2), |
| 59 | std::bind(&Consumer::onNack, this, _1, _2), |
| 60 | std::bind(&Consumer::onTimeout, this, _1)); |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 61 | |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 62 | // 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] | 63 | m_face.processEvents(); |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | void |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 68 | onData(const Interest&, const Data& data) |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 69 | { |
| 70 | m_validator.validate(data, |
| 71 | [=] (const Data& data) { |
| 72 | m_decryptor.decrypt(data.getContent().blockFromValue(), |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 73 | [] (const ConstBufferPtr& content) { |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 74 | std::cout << "Decrypted content: " |
| 75 | << std::string(reinterpret_cast<const char*>(content->data()), content->size()) |
| 76 | << std::endl; |
| 77 | }, |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 78 | [] (const ErrorCode&, const std::string& error) { |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 79 | std::cerr << "Cannot decrypt data: " << error << std::endl; |
| 80 | }); |
| 81 | }, |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 82 | [] (const Data&, const ValidationError& error) { |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 83 | std::cerr << "Cannot validate retrieved data: " << error << std::endl; |
| 84 | }); |
| 85 | } |
| 86 | |
| 87 | void |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 88 | onNack(const Interest& interest, const lp::Nack& nack) const |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 89 | { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 90 | std::cout << "Received Nack with reason " << nack.getReason() |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 91 | << " for interest " << interest << std::endl; |
| 92 | } |
| 93 | |
| 94 | void |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 95 | onTimeout(const Interest& interest) const |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 96 | { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 97 | std::cout << "Timeout for " << interest << std::endl; |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | private: |
| 101 | KeyChain m_keyChain; |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 102 | Face m_face{nullptr, m_keyChain}; |
| 103 | ValidatorConfig m_validator{m_face}; |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 104 | Decryptor m_decryptor; |
| 105 | }; |
| 106 | |
| 107 | } // namespace examples |
| 108 | } // namespace nac |
| 109 | } // namespace ndn |
| 110 | |
| 111 | int |
| 112 | main(int argc, char** argv) |
| 113 | { |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 114 | try { |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 115 | ndn::nac::examples::Consumer consumer; |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 116 | consumer.run(); |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 117 | return 0; |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 118 | } |
| 119 | catch (const std::exception& e) { |
| 120 | std::cerr << "ERROR: " << e.what() << std::endl; |
Davide Pesavento | 714dba0 | 2022-03-17 20:46:28 -0400 | [diff] [blame^] | 121 | return 1; |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 122 | } |
Alexander Afanasyev | c3d2990 | 2018-06-29 18:20:55 -0400 | [diff] [blame] | 123 | } |