blob: 359f17b0280795f116e6cc16fe25de8b8c5d252f [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"));
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040053 interest.setMustBeFresh(true);
Davide Pesavento714dba02022-03-17 20:46:28 -040054 interest.setInterestLifetime(3_s); // 3 seconds
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040055
Davide Pesavento714dba02022-03-17 20:46:28 -040056 std::cout << "Sending Interest " << interest << std::endl;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040057 m_face.expressInterest(interest,
Davide Pesavento714dba02022-03-17 20:46:28 -040058 std::bind(&Consumer::onData, this, _1, _2),
59 std::bind(&Consumer::onNack, this, _1, _2),
60 std::bind(&Consumer::onTimeout, this, _1));
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040061
Davide Pesavento714dba02022-03-17 20:46:28 -040062 // processEvents will block until the requested data is received or a timeout occurs
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040063 m_face.processEvents();
64 }
65
66private:
67 void
Davide Pesavento714dba02022-03-17 20:46:28 -040068 onData(const Interest&, const Data& data)
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040069 {
70 m_validator.validate(data,
71 [=] (const Data& data) {
72 m_decryptor.decrypt(data.getContent().blockFromValue(),
Davide Pesavento714dba02022-03-17 20:46:28 -040073 [] (const ConstBufferPtr& content) {
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040074 std::cout << "Decrypted content: "
75 << std::string(reinterpret_cast<const char*>(content->data()), content->size())
76 << std::endl;
77 },
Davide Pesavento714dba02022-03-17 20:46:28 -040078 [] (const ErrorCode&, const std::string& error) {
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040079 std::cerr << "Cannot decrypt data: " << error << std::endl;
80 });
81 },
Davide Pesavento714dba02022-03-17 20:46:28 -040082 [] (const Data&, const ValidationError& error) {
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040083 std::cerr << "Cannot validate retrieved data: " << error << std::endl;
84 });
85 }
86
87 void
Davide Pesavento714dba02022-03-17 20:46:28 -040088 onNack(const Interest& interest, const lp::Nack& nack) const
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040089 {
Davide Pesavento714dba02022-03-17 20:46:28 -040090 std::cout << "Received Nack with reason " << nack.getReason()
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040091 << " for interest " << interest << std::endl;
92 }
93
94 void
Davide Pesavento714dba02022-03-17 20:46:28 -040095 onTimeout(const Interest& interest) const
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040096 {
Davide Pesavento714dba02022-03-17 20:46:28 -040097 std::cout << "Timeout for " << interest << std::endl;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -040098 }
99
100private:
101 KeyChain m_keyChain;
Davide Pesavento714dba02022-03-17 20:46:28 -0400102 Face m_face{nullptr, m_keyChain};
103 ValidatorConfig m_validator{m_face};
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400104 Decryptor m_decryptor;
105};
106
107} // namespace examples
108} // namespace nac
109} // namespace ndn
110
111int
112main(int argc, char** argv)
113{
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400114 try {
Davide Pesavento714dba02022-03-17 20:46:28 -0400115 ndn::nac::examples::Consumer consumer;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400116 consumer.run();
Davide Pesavento714dba02022-03-17 20:46:28 -0400117 return 0;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400118 }
119 catch (const std::exception& e) {
120 std::cerr << "ERROR: " << e.what() << std::endl;
Davide Pesavento714dba02022-03-17 20:46:28 -0400121 return 1;
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400122 }
Alexander Afanasyevc3d29902018-06-29 18:20:55 -0400123}