Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | cdcde90 | 2017-08-23 15:40:22 -0400 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 5b06597 | 2022-02-16 10:29:01 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2013-2022 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Davide Pesavento | 1944281 | 2018-11-23 14:00:04 -0500 | [diff] [blame] | 22 | #include <ndn-cxx/face.hpp> |
| 23 | #include <ndn-cxx/security/key-chain.hpp> |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 24 | #include <ndn-cxx/security/signing-helpers.hpp> |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 25 | |
Davide Pesavento | cdcde90 | 2017-08-23 15:40:22 -0400 | [diff] [blame] | 26 | #include <iostream> |
| 27 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 28 | // Enclosing code in ndn simplifies coding (can also use `using namespace ndn`) |
| 29 | namespace ndn { |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 30 | // Additional nested namespaces should be used to prevent/limit name conflicts |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 31 | namespace examples { |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 32 | |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 33 | class Producer |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 34 | { |
| 35 | public: |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 36 | void |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 37 | run() |
| 38 | { |
Alexander Afanasyev | d1b6f95 | 2021-07-13 15:05:43 -0400 | [diff] [blame] | 39 | m_face.setInterestFilter("/example/testApp/randomData", |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 40 | std::bind(&Producer::onInterest, this, _1, _2), |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 41 | nullptr, // RegisterPrefixSuccessCallback is optional |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 42 | std::bind(&Producer::onRegisterFailed, this, _1, _2)); |
Alexander Afanasyev | d1b6f95 | 2021-07-13 15:05:43 -0400 | [diff] [blame] | 43 | |
| 44 | auto cert = m_keyChain.getPib().getDefaultIdentity().getDefaultKey().getDefaultCertificate(); |
| 45 | m_certServeHandle = m_face.setInterestFilter(security::extractIdentityFromCertName(cert.getName()), |
| 46 | [this, cert] (auto&&...) { |
| 47 | m_face.put(cert); |
| 48 | }, |
| 49 | std::bind(&Producer::onRegisterFailed, this, _1, _2)); |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 50 | m_face.processEvents(); |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | void |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 55 | onInterest(const InterestFilter&, const Interest& interest) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 56 | { |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 57 | std::cout << ">> I: " << interest << std::endl; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 58 | |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 59 | static const std::string content("Hello, world!"); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 60 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 61 | // Create Data packet |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 62 | auto data = make_shared<Data>(interest.getName()); |
| 63 | data->setFreshnessPeriod(10_s); |
Alexander Afanasyev | 5b06597 | 2022-02-16 10:29:01 -0500 | [diff] [blame^] | 64 | data->setContent(make_span(reinterpret_cast<const uint8_t*>(content.data()), content.size())); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 65 | |
Alexander Afanasyev | d1b6f95 | 2021-07-13 15:05:43 -0400 | [diff] [blame] | 66 | // in order for the consumer application to be able to validate the packet, you need to setup |
| 67 | // the following keys: |
| 68 | // 1. Generate example trust anchor |
| 69 | // |
| 70 | // ndnsec key-gen /example |
| 71 | // ndnsec cert-dump -i /example > example-trust-anchor.cert |
| 72 | // |
| 73 | // 2. Create a key for the producer and sign it with the example trust anchor |
| 74 | // |
| 75 | // ndnsec key-gen /example/testApp |
| 76 | // ndnsec sign-req /example/testApp | ndnsec cert-gen -s /example -i example | ndnsec cert-install - |
| 77 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 78 | // Sign Data packet with default identity |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 79 | m_keyChain.sign(*data); |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 80 | // m_keyChain.sign(*data, signingByIdentity(<identityName>)); |
| 81 | // m_keyChain.sign(*data, signingByKey(<keyName>)); |
| 82 | // m_keyChain.sign(*data, signingByCertificate(<certName>)); |
| 83 | // m_keyChain.sign(*data, signingWithSha256()); |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 84 | |
| 85 | // Return Data packet to the requester |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 86 | std::cout << "<< D: " << *data << std::endl; |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 87 | m_face.put(*data); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 91 | onRegisterFailed(const Name& prefix, const std::string& reason) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 92 | { |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 93 | std::cerr << "ERROR: Failed to register prefix '" << prefix |
| 94 | << "' with the local forwarder (" << reason << ")" << std::endl; |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 95 | m_face.shutdown(); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 96 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 97 | |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 98 | private: |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 99 | Face m_face; |
| 100 | KeyChain m_keyChain; |
Alexander Afanasyev | d1b6f95 | 2021-07-13 15:05:43 -0400 | [diff] [blame] | 101 | ScopedRegisteredPrefixHandle m_certServeHandle; |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 102 | }; |
| 103 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 104 | } // namespace examples |
| 105 | } // namespace ndn |
| 106 | |
| 107 | int |
| 108 | main(int argc, char** argv) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 109 | { |
| 110 | try { |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 111 | ndn::examples::Producer producer; |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 112 | producer.run(); |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 113 | return 0; |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 114 | } |
Steve DiBenedetto | 9fcc24f | 2015-01-05 12:16:16 -0700 | [diff] [blame] | 115 | catch (const std::exception& e) { |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 116 | std::cerr << "ERROR: " << e.what() << std::endl; |
Zhiyi Zhang | 8b0344d | 2019-06-22 16:53:58 -0700 | [diff] [blame] | 117 | return 1; |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 118 | } |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 119 | } |