prefix-update-processor.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
23 #include "lsdb.hpp"
24 #include "nlsr.hpp"
25 #include <ndn-cxx/mgmt/nfd/control-response.hpp>
26 #include <ndn-cxx/tag.hpp>
27 #include <ndn-cxx/face.hpp>
28 
29 namespace nlsr {
30 namespace update {
31 
32 INIT_LOGGER("PrefixUpdateProcessor");
33 
36 using SignerTag = ndn::SimpleTag<ndn::Name, 20>;
37 
40 static ndn::optional<std::string>
41 getSignerFromTag(const ndn::Interest& interest)
42 {
43  shared_ptr<SignerTag> signerTag = interest.getTag<SignerTag>();
44  if (signerTag == nullptr) {
45  return ndn::nullopt;
46  }
47  else {
48  return signerTag->get().toUri();
49  }
50 }
51 
52 PrefixUpdateProcessor::PrefixUpdateProcessor(ndn::mgmt::Dispatcher& dispatcher,
53  ndn::Face& face,
54  NamePrefixList& namePrefixList,
55  Lsdb& lsdb)
56  : CommandManagerBase(dispatcher, namePrefixList, lsdb, "prefix-update")
57 
58  , m_validator(ndn::make_unique<ndn::security::v2::CertificateFetcherDirectFetch>(face))
59 {
60  NLSR_LOG_DEBUG("Setting dispatcher to capture Interests for: "
61  << ndn::Name(Nlsr::LOCALHOST_PREFIX).append("prefix-update"));
62 
63  m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(makeRelPrefix("advertise"),
64  makeAuthorization(),
65  std::bind(&PrefixUpdateProcessor::validateParameters<AdvertisePrefixCommand>,
66  this, _1),
67  std::bind(&PrefixUpdateProcessor::advertiseAndInsertPrefix, this, _1, _2, _3, _4));
68 
69  m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(makeRelPrefix("withdraw"),
70  makeAuthorization(),
71  std::bind(&PrefixUpdateProcessor::validateParameters<WithdrawPrefixCommand>,
72  this, _1),
73  std::bind(&PrefixUpdateProcessor::withdrawAndRemovePrefix, this, _1, _2, _3, _4));
74 }
75 
76 ndn::mgmt::Authorization
77 PrefixUpdateProcessor::makeAuthorization()
78 {
79  return [=] (const ndn::Name& prefix, const ndn::Interest& interest,
80  const ndn::mgmt::ControlParameters* params,
81  const ndn::mgmt::AcceptContinuation& accept,
82  const ndn::mgmt::RejectContinuation& reject) {
83  m_validator.validate(interest,
84  [accept] (const ndn::Interest& request) {
85 
86  auto signer1 = getSignerFromTag(request);
87  std::string signer = signer1.value_or("*");
88  NLSR_LOG_DEBUG("accept " << request.getName() << " signer=" << signer);
89  accept(signer);
90  },
91  [reject] (const ndn::Interest& request, const ndn::security::v2::ValidationError& error) {
92  NLSR_LOG_DEBUG("reject " << request.getName() << " signer=" <<
93  getSignerFromTag(request).value_or("?") << ' ' << error);
94  reject(ndn::mgmt::RejectReply::STATUS403);
95  });
96  };
97 }
98 
99 void
100 PrefixUpdateProcessor::loadValidator(boost::property_tree::ptree section,
101  const std::string& filename)
102 {
103  m_validator.load(section, filename);
104 }
105 
106 } // namespace update
107 } // namespace nlsr
ndn::SimpleTag< ndn::Name, 20 > SignerTag
an Interest tag to indicate command signer
Copyright (c) 2014-2017, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
Definition: tlv-nlsr.hpp:27
void loadValidator(ConfigSection section, const std::string &filename)
Load the validator&#39;s configuration from a section of a configuration file.
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:41
void advertiseAndInsertPrefix(const ndn::Name &prefix, const ndn::Interest &interest, const ndn::mgmt::ControlParameters &parameters, const ndn::mgmt::CommandContinuation &done)
add desired name prefix to the advertised name prefix list or insert a prefix into the FIB if paramet...
static const ndn::Name LOCALHOST_PREFIX
Definition: nlsr.hpp:479
#define INIT_LOGGER(name)
Definition: logger.hpp:35
PrefixUpdateProcessor(ndn::mgmt::Dispatcher &dispatcher, ndn::Face &face, NamePrefixList &namePrefixList, Lsdb &lsdb)
Copyright (c) 2014-2017, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
void withdrawAndRemovePrefix(const ndn::Name &prefix, const ndn::Interest &interest, const ndn::mgmt::ControlParameters &parameters, const ndn::mgmt::CommandContinuation &done)
remove desired name prefix from the advertised name prefix list or remove a prefix from the FIB if pa...
static ndn::optional< std::string > getSignerFromTag(const ndn::Interest &interest)
obtain signer from SignerTag attached to Interest, if available
ndn::PartialName makeRelPrefix(const std::string &verb) const
generate the relative prefix for a handler by appending the verb name to the module name ...
ndn::mgmt::Dispatcher & m_dispatcher