blob: e212b6f28f5151c9ecbb7eefe7bc38dccc1dad95 [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/*
3 * Copyright (c) 2014-2022, 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 {
31// Additional nested namespaces can be used to prevent/limit name conflicts
32namespace examples {
33
34class Consumer : noncopyable
35{
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 {
52 Interest interest(Name("/example/testApp/randomData"));
Junxiao Shi2f0a4012022-09-12 22:02:29 +000053 interest.setCanBePrefix(true);
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040054 interest.setMustBeFresh(true);
Davide Pesavento714dba02022-03-17 20:46:28 -040055 interest.setInterestLifetime(3_s); // 3 seconds
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040056
Davide Pesavento714dba02022-03-17 20:46:28 -040057 std::cout << "Sending Interest " << interest << std::endl;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040058 m_face.expressInterest(interest,
Davide Pesavento714dba02022-03-17 20:46:28 -040059 std::bind(&Consumer::onData, this, _1, _2),
60 std::bind(&Consumer::onNack, this, _1, _2),
61 std::bind(&Consumer::onTimeout, this, _1));
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040062
Davide Pesavento714dba02022-03-17 20:46:28 -040063 // processEvents will block until the requested data is received or a timeout occurs
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040064 m_face.processEvents();
65 }
66
67private:
68 void
Davide Pesavento714dba02022-03-17 20:46:28 -040069 onData(const Interest&, const Data& data)
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040070 {
71 m_validator.validate(data,
72 [=] (const Data& data) {
73 m_decryptor.decrypt(data.getContent().blockFromValue(),
Davide Pesavento714dba02022-03-17 20:46:28 -040074 [] (const ConstBufferPtr& content) {
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040075 std::cout << "Decrypted content: "
76 << std::string(reinterpret_cast<const char*>(content->data()), content->size())
77 << std::endl;
78 },
Davide Pesavento714dba02022-03-17 20:46:28 -040079 [] (const ErrorCode&, const std::string& error) {
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040080 std::cerr << "Cannot decrypt data: " << error << std::endl;
81 });
82 },
Davide Pesavento714dba02022-03-17 20:46:28 -040083 [] (const Data&, const ValidationError& error) {
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040084 std::cerr << "Cannot validate retrieved data: " << error << std::endl;
85 });
86 }
87
88 void
Davide Pesavento714dba02022-03-17 20:46:28 -040089 onNack(const Interest& interest, const lp::Nack& nack) const
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040090 {
Davide Pesavento714dba02022-03-17 20:46:28 -040091 std::cout << "Received Nack with reason " << nack.getReason()
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040092 << " for interest " << interest << std::endl;
93 }
94
95 void
Davide Pesavento714dba02022-03-17 20:46:28 -040096 onTimeout(const Interest& interest) const
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040097 {
Davide Pesavento714dba02022-03-17 20:46:28 -040098 std::cout << "Timeout for " << interest << std::endl;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040099 }
100
101private:
102 KeyChain m_keyChain;
Davide Pesavento714dba02022-03-17 20:46:28 -0400103 Face m_face{nullptr, m_keyChain};
104 ValidatorConfig m_validator{m_face};
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400105 Decryptor m_decryptor;
106};
107
108} // namespace examples
109} // namespace nac
110} // namespace ndn
111
112int
113main(int argc, char** argv)
114{
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400115 try {
Davide Pesavento714dba02022-03-17 20:46:28 -0400116 ndn::nac::examples::Consumer consumer;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400117 consumer.run();
Davide Pesavento714dba02022-03-17 20:46:28 -0400118 return 0;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400119 }
120 catch (const std::exception& e) {
121 std::cerr << "ERROR: " << e.what() << std::endl;
Davide Pesavento714dba02022-03-17 20:46:28 -0400122 return 1;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400123 }
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400124}