prefix-update-processor.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2025, The University of Memphis,
4  * Regents of the University of California,
5  * Arizona Board of Regents.
6  *
7  * This file is part of NLSR (Named-data Link State Routing).
8  * See AUTHORS.md for complete list of NLSR authors and contributors.
9  *
10  * NLSR is free software: you can redistribute it and/or modify it under the terms
11  * of the GNU General Public License as published by the Free Software Foundation,
12  * either version 3 of the License, or (at your option) any later version.
13  *
14  * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16  * PURPOSE. See the GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
23 #include "logger.hpp"
24 #include "lsdb.hpp"
25 #include "nlsr.hpp"
27 
28 #include <ndn-cxx/face.hpp>
29 #include <ndn-cxx/mgmt/nfd/control-response.hpp>
30 
31 #include <boost/algorithm/string.hpp>
32 #include <fstream>
33 
34 namespace nlsr::update {
35 
36 INIT_LOGGER(update.PrefixUpdateProcessor);
37 
40 using SignerTag = ndn::SimpleTag<ndn::Name, 20>;
41 
44 static std::optional<std::string>
45 getSignerFromTag(const ndn::Interest& interest)
46 {
47  auto signerTag = interest.getTag<SignerTag>();
48  if (signerTag == nullptr) {
49  return std::nullopt;
50  }
51  else {
52  return signerTag->get().toUri();
53  }
54 }
55 
56 PrefixUpdateProcessor::PrefixUpdateProcessor(ndn::mgmt::Dispatcher& dispatcher,
57  ndn::security::ValidatorConfig& validator,
58  NamePrefixList& namePrefixList,
59  Lsdb& lsdb, const std::string& configFileName)
60  : CommandManagerBase(dispatcher, namePrefixList, lsdb, "prefix-update")
61  , m_validator(validator)
62  , m_confFileNameDynamic(configFileName)
63 {
64  NLSR_LOG_DEBUG("Setting dispatcher to capture Interests for: "
65  << ndn::Name(Nlsr::LOCALHOST_PREFIX).append("prefix-update"));
66 
67  m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(makeRelPrefix("advertise"),
68  makeAuthorization(),
69  [] (const auto& p) { return validateParameters<AdvertisePrefixCommand>(p); },
70  std::bind(&PrefixUpdateProcessor::advertiseAndInsertPrefix, this, _1, _2, _3, _4));
71 
72  m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(makeRelPrefix("withdraw"),
73  makeAuthorization(),
74  [] (const auto& p) { return validateParameters<WithdrawPrefixCommand>(p); },
75  std::bind(&PrefixUpdateProcessor::withdrawAndRemovePrefix, this, _1, _2, _3, _4));
76 }
77 
78 ndn::mgmt::Authorization
79 PrefixUpdateProcessor::makeAuthorization()
80 {
81  return [=] (const ndn::Name& prefix, const ndn::Interest& interest,
82  const ndn::mgmt::ControlParametersBase* params,
83  const ndn::mgmt::AcceptContinuation& accept,
84  const ndn::mgmt::RejectContinuation& reject) {
85  m_validator.validate(interest,
86  [accept] (const ndn::Interest& request) {
87 
88  auto signer1 = getSignerFromTag(request);
89  std::string signer = signer1.value_or("*");
90  NLSR_LOG_DEBUG("accept " << request.getName() << " signer=" << signer);
91  accept(signer);
92  },
93  [reject] (const ndn::Interest& request, const ndn::security::ValidationError& error) {
94  NLSR_LOG_DEBUG("reject " << request.getName() << " signer=" <<
95  getSignerFromTag(request).value_or("?") << ' ' << error);
96  reject(ndn::mgmt::RejectReply::STATUS403);
97  });
98  };
99 }
100 
101 void
102 PrefixUpdateProcessor::loadValidator(boost::property_tree::ptree section,
103  const std::string& filename)
104 {
105  m_validator.load(section, filename);
106 }
107 
108 bool
110 {
111  std::string line;
112  std::fstream fp(m_confFileNameDynamic);
113  if (!fp.good() || !fp.is_open()) {
114  NLSR_LOG_ERROR("Failed to open configuration file for parsing");
115  return true;
116  }
117  while (!fp.eof()) {
118  getline(fp, line);
119  if (line == prefix) {
120  return true;
121  }
122  }
123  fp.close();
124  return false;
125 }
126 
127 bool
128 PrefixUpdateProcessor::addOrDeletePrefix(const ndn::Name& prefix, bool addPrefix)
129 {
130  std::string value = " prefix " + prefix.toUri();
131  std::string fileString;
132  std::string line;
133  std::string trimedLine;
134  std::fstream input(m_confFileNameDynamic, input.in);
135  if (!input.good() || !input.is_open()) {
136  NLSR_LOG_ERROR("Failed to open configuration file for parsing");
137  return false;
138  }
139 
140  if (addPrefix) {
141  //check if prefix already exist in the nlsr configuration file
142  if (checkForPrefixInFile(value)) {
143  NLSR_LOG_ERROR("Prefix already exists in the configuration file");
144  return false;
145  }
146  while (!input.eof()) {
147  getline(input, line);
148  if (!line.empty()) {
149  fileString.append(line + "\n");
150  if (line == "advertising") {
151  getline(input, line);
152  fileString.append(line + "\n" + value + "\n");
153  }
154  }
155  }
156  }
157  else {
158  if (!checkForPrefixInFile(value)) {
159  NLSR_LOG_ERROR("Prefix doesn't exists in the configuration file");
160  return false;
161  }
162  boost::trim(value);
163  while (!input.eof()) {
164  getline(input, line);
165  if (!line.empty()) {
166  std::string trimLine = line;
167  boost::trim(trimLine);
168  if (trimLine != value) {
169  fileString.append(line + "\n");
170  }
171  }
172  }
173  }
174  input.close();
175  std::ofstream output(m_confFileNameDynamic);
176  output << fileString;
177  output.close();
178  return true;
179 }
180 
181 std::optional<bool>
183 {
184  return addOrDeletePrefix(prefix, true);
185 }
186 
187 std::optional<bool>
188 PrefixUpdateProcessor::afterWithdraw(const ndn::Name& prefix)
189 {
190  return addOrDeletePrefix(prefix, false);
191 }
192 
193 } // namespace nlsr::update
static const ndn::Name LOCALHOST_PREFIX
Definition: nlsr.hpp:161
void withdrawAndRemovePrefix(const ndn::Name &prefix, const ndn::Interest &interest, const ndn::mgmt::ControlParametersBase &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...
void advertiseAndInsertPrefix(const ndn::Name &prefix, const ndn::Interest &interest, const ndn::mgmt::ControlParametersBase &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...
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
void loadValidator(ConfigSection section, const std::string &filename)
Load the validator's configuration from a section of a configuration file.
std::optional< bool > afterAdvertise(const ndn::Name &prefix) override
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)
bool addOrDeletePrefix(const ndn::Name &prefix, bool addPrefix)
Add or delete an advertise or withdrawn prefix to the nlsr configuration file.
std::optional< bool > afterWithdraw(const ndn::Name &prefix) override
save an advertised prefix to the nlsr configuration file returns bool from the overridden function wh...
bool checkForPrefixInFile(const std::string prefix)
Check if a prefix exists in the nlsr configuration file.
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:38
#define INIT_LOGGER(name)
Definition: logger.hpp:35
#define NLSR_LOG_ERROR(x)
Definition: logger.hpp:41
ndn::SimpleTag< ndn::Name, 20 > SignerTag
an Interest tag to indicate command signer
static std::optional< std::string > getSignerFromTag(const ndn::Interest &interest)
obtain signer from SignerTag attached to Interest, if available