blob: b40b918e47538dea8b8caaeec4ae2009554edf39 [file] [log] [blame]
Alexander Afanasyevc3d29902018-06-29 18:20:55 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento714dba02022-03-17 20:46:28 -04002/*
Davide Pesavento8d60e642023-04-17 02:36:03 -04003 * Copyright (c) 2014-2023, Regents of the University of California
Alexander Afanasyevc3d29902018-06-29 18:20:55 -04004 *
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`)
29namespace ndn {
30namespace nac {
Davide Pesavento8d60e642023-04-17 02:36:03 -040031// Additional nested namespaces should be used to prevent/limit name conflicts
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040032namespace examples {
33
Davide Pesavento8d60e642023-04-17 02:36:03 -040034class Consumer
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040035{
36public:
37 Consumer()
Davide Pesavento714dba02022-03-17 20:46:28 -040038 : m_decryptor(m_keyChain.getPib().getDefaultIdentity().getDefaultKey(),
39 m_validator, m_keyChain, m_face)
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040040 {
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 Pesavento8d60e642023-04-17 02:36:03 -040052 using namespace std::placeholders;
53
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040054 Interest interest(Name("/example/testApp/randomData"));
Junxiao Shi2f0a4012022-09-12 22:02:29 +000055 interest.setCanBePrefix(true);
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040056 interest.setMustBeFresh(true);
Davide Pesavento714dba02022-03-17 20:46:28 -040057 interest.setInterestLifetime(3_s); // 3 seconds
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040058
Davide Pesavento714dba02022-03-17 20:46:28 -040059 std::cout << "Sending Interest " << interest << std::endl;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040060 m_face.expressInterest(interest,
Davide Pesavento714dba02022-03-17 20:46:28 -040061 std::bind(&Consumer::onData, this, _1, _2),
62 std::bind(&Consumer::onNack, this, _1, _2),
63 std::bind(&Consumer::onTimeout, this, _1));
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040064
Davide Pesavento714dba02022-03-17 20:46:28 -040065 // processEvents will block until the requested data is received or a timeout occurs
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040066 m_face.processEvents();
67 }
68
69private:
70 void
Davide Pesavento714dba02022-03-17 20:46:28 -040071 onData(const Interest&, const Data& data)
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040072 {
73 m_validator.validate(data,
74 [=] (const Data& data) {
75 m_decryptor.decrypt(data.getContent().blockFromValue(),
Davide Pesavento714dba02022-03-17 20:46:28 -040076 [] (const ConstBufferPtr& content) {
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040077 std::cout << "Decrypted content: "
78 << std::string(reinterpret_cast<const char*>(content->data()), content->size())
79 << std::endl;
80 },
Davide Pesavento714dba02022-03-17 20:46:28 -040081 [] (const ErrorCode&, const std::string& error) {
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040082 std::cerr << "Cannot decrypt data: " << error << std::endl;
83 });
84 },
Davide Pesavento714dba02022-03-17 20:46:28 -040085 [] (const Data&, const ValidationError& error) {
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040086 std::cerr << "Cannot validate retrieved data: " << error << std::endl;
87 });
88 }
89
90 void
Davide Pesavento714dba02022-03-17 20:46:28 -040091 onNack(const Interest& interest, const lp::Nack& nack) const
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040092 {
Davide Pesavento714dba02022-03-17 20:46:28 -040093 std::cout << "Received Nack with reason " << nack.getReason()
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040094 << " for interest " << interest << std::endl;
95 }
96
97 void
Davide Pesavento714dba02022-03-17 20:46:28 -040098 onTimeout(const Interest& interest) const
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040099 {
Davide Pesavento714dba02022-03-17 20:46:28 -0400100 std::cout << "Timeout for " << interest << std::endl;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400101 }
102
103private:
104 KeyChain m_keyChain;
Davide Pesavento714dba02022-03-17 20:46:28 -0400105 Face m_face{nullptr, m_keyChain};
106 ValidatorConfig m_validator{m_face};
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400107 Decryptor m_decryptor;
108};
109
110} // namespace examples
111} // namespace nac
112} // namespace ndn
113
114int
115main(int argc, char** argv)
116{
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400117 try {
Davide Pesavento714dba02022-03-17 20:46:28 -0400118 ndn::nac::examples::Consumer consumer;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400119 consumer.run();
Davide Pesavento714dba02022-03-17 20:46:28 -0400120 return 0;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400121 }
122 catch (const std::exception& e) {
123 std::cerr << "ERROR: " << e.what() << std::endl;
Davide Pesavento714dba02022-03-17 20:46:28 -0400124 return 1;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400125 }
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400126}