blob: 80f1986a32951b7f6b0ce54c9a609a77bfe705b0 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventocdcde902017-08-23 15:40:22 -04002/*
Alexander Afanasyev5b065972022-02-16 10:29:01 -05003 * Copyright (c) 2013-2022 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Afanasyevc4b75982014-01-09 14:51:45 -080020 */
21
Davide Pesavento19442812018-11-23 14:00:04 -050022#include <ndn-cxx/face.hpp>
23#include <ndn-cxx/security/key-chain.hpp>
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070024#include <ndn-cxx/security/signing-helpers.hpp>
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080025
Davide Pesaventocdcde902017-08-23 15:40:22 -040026#include <iostream>
27
Alexander Afanasyev151a8552014-04-11 00:54:43 -070028// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
29namespace ndn {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070030// Additional nested namespaces should be used to prevent/limit name conflicts
Alexander Afanasyev151a8552014-04-11 00:54:43 -070031namespace examples {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080032
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070033class Producer
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080034{
35public:
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080036 void
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070037 run()
38 {
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -040039 m_face.setInterestFilter("/example/testApp/randomData",
Davide Pesavento2e481fc2021-07-02 18:20:03 -040040 std::bind(&Producer::onInterest, this, _1, _2),
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070041 nullptr, // RegisterPrefixSuccessCallback is optional
Davide Pesavento2e481fc2021-07-02 18:20:03 -040042 std::bind(&Producer::onRegisterFailed, this, _1, _2));
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -040043
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 DiBenedetto9fcc24f2015-01-05 12:16:16 -070050 m_face.processEvents();
51 }
52
53private:
54 void
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070055 onInterest(const InterestFilter&, const Interest& interest)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080056 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070057 std::cout << ">> I: " << interest << std::endl;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070058
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070059 static const std::string content("Hello, world!");
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080060
Alexander Afanasyev151a8552014-04-11 00:54:43 -070061 // Create Data packet
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070062 auto data = make_shared<Data>(interest.getName());
63 data->setFreshnessPeriod(10_s);
Alexander Afanasyev5b065972022-02-16 10:29:01 -050064 data->setContent(make_span(reinterpret_cast<const uint8_t*>(content.data()), content.size()));
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080065
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -040066 // 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 Afanasyev151a8552014-04-11 00:54:43 -070078 // Sign Data packet with default identity
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070079 m_keyChain.sign(*data);
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070080 // 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 Afanasyev151a8552014-04-11 00:54:43 -070084
85 // Return Data packet to the requester
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070086 std::cout << "<< D: " << *data << std::endl;
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070087 m_face.put(*data);
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080088 }
89
90 void
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -070091 onRegisterFailed(const Name& prefix, const std::string& reason)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080092 {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -070093 std::cerr << "ERROR: Failed to register prefix '" << prefix
94 << "' with the local forwarder (" << reason << ")" << std::endl;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070095 m_face.shutdown();
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080096 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070097
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080098private:
Alexander Afanasyev151a8552014-04-11 00:54:43 -070099 Face m_face;
100 KeyChain m_keyChain;
Alexander Afanasyevd1b6f952021-07-13 15:05:43 -0400101 ScopedRegisteredPrefixHandle m_certServeHandle;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -0800102};
103
Alexander Afanasyev151a8552014-04-11 00:54:43 -0700104} // namespace examples
105} // namespace ndn
106
107int
108main(int argc, char** argv)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -0800109{
110 try {
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -0700111 ndn::examples::Producer producer;
Alexander Afanasyev151a8552014-04-11 00:54:43 -0700112 producer.run();
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -0700113 return 0;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -0800114 }
Steve DiBenedetto9fcc24f2015-01-05 12:16:16 -0700115 catch (const std::exception& e) {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -0800116 std::cerr << "ERROR: " << e.what() << std::endl;
Zhiyi Zhang8b0344d2019-06-22 16:53:58 -0700117 return 1;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -0800118 }
Alexander Afanasyevc4b75982014-01-09 14:51:45 -0800119}