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/face.hpp>
27 #include <boost/algorithm/string.hpp>
28 #include <algorithm>
29 
30 namespace nlsr {
31 namespace update {
32 
33 INIT_LOGGER(update.PrefixUpdateProcessor);
34 
37 using SignerTag = ndn::SimpleTag<ndn::Name, 20>;
38 
41 static ndn::optional<std::string>
42 getSignerFromTag(const ndn::Interest& interest)
43 {
44  shared_ptr<SignerTag> signerTag = interest.getTag<SignerTag>();
45  if (signerTag == nullptr) {
46  return ndn::nullopt;
47  }
48  else {
49  return signerTag->get().toUri();
50  }
51 }
52 
53 PrefixUpdateProcessor::PrefixUpdateProcessor(ndn::mgmt::Dispatcher& dispatcher,
54  ndn::security::ValidatorConfig& validator,
55  NamePrefixList& namePrefixList,
56  Lsdb& lsdb, const std::string& configFileName)
57  : CommandManagerBase(dispatcher, namePrefixList, lsdb, "prefix-update")
58  , m_validator(validator)
59  , m_confFileNameDynamic(configFileName)
60 {
61  NLSR_LOG_DEBUG("Setting dispatcher to capture Interests for: "
62  << ndn::Name(Nlsr::LOCALHOST_PREFIX).append("prefix-update"));
63 
64  m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(makeRelPrefix("advertise"),
65  makeAuthorization(),
66  std::bind(&PrefixUpdateProcessor::validateParameters<AdvertisePrefixCommand>,
67  this, _1),
68  std::bind(&PrefixUpdateProcessor::advertiseAndInsertPrefix, this, _1, _2, _3, _4));
69 
70  m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(makeRelPrefix("withdraw"),
71  makeAuthorization(),
72  std::bind(&PrefixUpdateProcessor::validateParameters<WithdrawPrefixCommand>,
73  this, _1),
74  std::bind(&PrefixUpdateProcessor::withdrawAndRemovePrefix, this, _1, _2, _3, _4));
75 }
76 
77 ndn::mgmt::Authorization
78 PrefixUpdateProcessor::makeAuthorization()
79 {
80  return [=] (const ndn::Name& prefix, const ndn::Interest& interest,
81  const ndn::mgmt::ControlParameters* params,
82  const ndn::mgmt::AcceptContinuation& accept,
83  const ndn::mgmt::RejectContinuation& reject) {
84  m_validator.validate(interest,
85  [accept] (const ndn::Interest& request) {
86 
87  auto signer1 = getSignerFromTag(request);
88  std::string signer = signer1.value_or("*");
89  NLSR_LOG_DEBUG("accept " << request.getName() << " signer=" << signer);
90  accept(signer);
91  },
92  [reject] (const ndn::Interest& request, const ndn::security::v2::ValidationError& error) {
93  NLSR_LOG_DEBUG("reject " << request.getName() << " signer=" <<
94  getSignerFromTag(request).value_or("?") << ' ' << error);
95  reject(ndn::mgmt::RejectReply::STATUS403);
96  });
97  };
98 }
99 
100 void
101 PrefixUpdateProcessor::loadValidator(boost::property_tree::ptree section,
102  const std::string& filename)
103 {
104  m_validator.load(section, filename);
105 }
106 
107 bool
109 {
110  std::string line;
111  std::fstream fp(m_confFileNameDynamic);
112  if (!fp.good() || !fp.is_open()) {
113  NLSR_LOG_ERROR("Failed to open configuration file for parsing");
114  return true;
115  }
116  while (!fp.eof()) {
117  getline(fp, line);
118  if (line == prefix) {
119  return true;
120  }
121  }
122  fp.close();
123  return false;
124 }
125 
126 bool
127 PrefixUpdateProcessor::addOrDeletePrefix(const ndn::Name& prefix, bool addPrefix)
128 {
129  std::string value = " prefix " + prefix.toUri();;
130  std::string fileString;
131  std::string line;
132  std::string trimedLine;
133  std::fstream input(m_confFileNameDynamic, input.in);
134  if (!input.good() || !input.is_open()) {
135  NLSR_LOG_ERROR("Failed to open configuration file for parsing");
136  return false;
137  }
138 
139  if (addPrefix) {
140  //check if prefix already exist in the nlsr configuration file
141  if (checkForPrefixInFile(value)) {
142  NLSR_LOG_ERROR("Prefix already exists in the configuration file");
143  return false;
144  }
145  while (!input.eof()) {
146  getline(input, line);
147  if (!line.empty()) {
148  fileString.append(line + "\n");
149  if (line == "advertising") {
150  getline(input, line);
151  fileString.append(line + "\n" + value + "\n");
152  }
153  }
154  }
155  }
156  else {
157  if (!checkForPrefixInFile(value)) {
158  NLSR_LOG_ERROR("Prefix doesn't exists in the configuration file");
159  return false;
160  }
161  boost::trim(value);
162  while (!input.eof()) {
163  getline(input, line);
164  if (!line.empty()) {
165  std::string trimLine = line;
166  boost::trim(trimLine);
167  if (trimLine != value) {
168  fileString.append(line + "\n");
169  }
170  }
171  }
172  }
173  input.close();
174  std::ofstream output(m_confFileNameDynamic);
175  output << fileString;
176  output.close();
177  return true;
178 }
179 
180 ndn::optional<bool>
182 {
183  return addOrDeletePrefix(prefix, true);
184 }
185 
186 ndn::optional<bool>
187 PrefixUpdateProcessor::afterWithdraw(const ndn::Name& prefix)
188 {
189  return addOrDeletePrefix(prefix, false);
190 }
191 
192 } // namespace update
193 } // namespace nlsr
ndn::SimpleTag< ndn::Name, 20 > SignerTag
an Interest tag to indicate command signer
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:38
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:260
#define INIT_LOGGER(name)
Definition: logger.hpp:35
ndn::optional< bool > afterAdvertise(const ndn::Name &prefix)
save an advertised prefix to the nlsr configuration file returns bool from the overridden function wh...
PrefixUpdateProcessor(ndn::mgmt::Dispatcher &dispatcher, ndn::security::ValidatorConfig &validator, NamePrefixList &namePrefixList, Lsdb &lsdb, const std::string &configFileName)
ndn::optional< bool > afterWithdraw(const ndn::Name &prefix)
save an advertised prefix to the nlsr configuration file returns bool from the overridden function wh...
bool addOrDeletePrefix(const ndn::Name &prefix, bool addPrefix)
Add or delete an advertise or withdrawn prefix to the nlsr configuration file.
#define NLSR_LOG_ERROR(x)
Definition: logger.hpp:41
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
bool checkForPrefixInFile(const std::string prefix)
Check if a prefix exists in the nlsr configuration file.
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