All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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"
25 
26 #include <boost/algorithm/string.hpp>
27 #include <fstream>
28 
29 namespace nlsr::update {
30 
31 INIT_LOGGER(update.PrefixUpdateProcessor);
32 
35 using SignerTag = ndn::SimpleTag<ndn::Name, 20>;
36 
39 static std::optional<std::string>
40 getSignerFromTag(const ndn::Interest& interest)
41 {
42  auto signerTag = interest.getTag<SignerTag>();
43  if (signerTag == nullptr) {
44  return std::nullopt;
45  }
46  else {
47  return signerTag->get().toUri();
48  }
49 }
50 
51 PrefixUpdateProcessor::PrefixUpdateProcessor(ndn::mgmt::Dispatcher& dispatcher,
52  ndn::security::ValidatorConfig& validator,
53  NamePrefixList& namePrefixList,
54  Lsdb& lsdb, const std::string& configFileName)
55  : CommandProcessor(dispatcher, namePrefixList, lsdb)
56  , m_validator(validator)
57  , m_confFileNameDynamic(configFileName)
58 {
59  m_dispatcher.addControlCommand<AdvertisePrefixCommand>(
60  makeAuthorization(),
61  // the first and second arguments are ignored since the handler does not need them
62  std::bind(&PrefixUpdateProcessor::advertiseAndInsertPrefix, this, _3, _4));
63 
64  m_dispatcher.addControlCommand<WithdrawPrefixCommand>(
65  makeAuthorization(),
66  // the first and second arguments are ignored since the handler does not need them
67  std::bind(&PrefixUpdateProcessor::withdrawAndRemovePrefix, this, _3, _4));
68 }
69 
70 ndn::mgmt::Authorization
71 PrefixUpdateProcessor::makeAuthorization()
72 {
73  return [=] (const ndn::Name& prefix, const ndn::Interest& interest,
74  const ndn::mgmt::ControlParametersBase* params,
75  const ndn::mgmt::AcceptContinuation& accept,
76  const ndn::mgmt::RejectContinuation& reject) {
77  m_validator.validate(interest,
78  [accept] (const ndn::Interest& request) {
79  auto signer1 = getSignerFromTag(request);
80  std::string signer = signer1.value_or("*");
81  NLSR_LOG_DEBUG("accept " << request.getName() << " signer=" << signer);
82  accept(signer);
83  },
84  [reject] (const ndn::Interest& request, const ndn::security::ValidationError& error) {
85  NLSR_LOG_DEBUG("reject " << request.getName() << " signer=" <<
86  getSignerFromTag(request).value_or("?") << ' ' << error);
87  reject(ndn::mgmt::RejectReply::STATUS403);
88  });
89  };
90 }
91 
92 void
93 PrefixUpdateProcessor::loadValidator(boost::property_tree::ptree section,
94  const std::string& filename)
95 {
96  m_validator.load(section, filename);
97 }
98 
99 bool
101 {
102  std::string line;
103  std::fstream fp(m_confFileNameDynamic);
104  if (!fp.good() || !fp.is_open()) {
105  NLSR_LOG_ERROR("Failed to open configuration file for parsing");
106  return true;
107  }
108  while (!fp.eof()) {
109  getline(fp, line);
110  if (line == prefix) {
111  return true;
112  }
113  }
114  return false;
115 }
116 
117 bool
118 PrefixUpdateProcessor::addOrDeletePrefix(const ndn::Name& prefix, bool addPrefix)
119 {
120  std::string value = " prefix " + prefix.toUri();
121  std::string fileString;
122  std::string line;
123  std::string trimedLine;
124  std::fstream input(m_confFileNameDynamic, input.in);
125  if (!input.good() || !input.is_open()) {
126  NLSR_LOG_ERROR("Failed to open configuration file for parsing");
127  return false;
128  }
129 
130  if (addPrefix) {
131  //check if prefix already exist in the nlsr configuration file
132  if (checkForPrefixInFile(value)) {
133  NLSR_LOG_ERROR("Prefix already exists in the configuration file");
134  return false;
135  }
136  while (!input.eof()) {
137  getline(input, line);
138  if (!line.empty()) {
139  fileString.append(line + "\n");
140  if (line == "advertising") {
141  getline(input, line);
142  fileString.append(line + "\n" + value + "\n");
143  }
144  }
145  }
146  }
147  else {
148  if (!checkForPrefixInFile(value)) {
149  NLSR_LOG_ERROR("Prefix doesn't exists in the configuration file");
150  return false;
151  }
152  boost::trim(value);
153  while (!input.eof()) {
154  getline(input, line);
155  if (!line.empty()) {
156  std::string trimLine = line;
157  boost::trim(trimLine);
158  if (trimLine != value) {
159  fileString.append(line + "\n");
160  }
161  }
162  }
163  }
164  input.close();
165  std::ofstream output(m_confFileNameDynamic);
166  output << fileString;
167  output.close();
168  return true;
169 }
170 
171 std::optional<bool>
173 {
174  return addOrDeletePrefix(prefix, true);
175 }
176 
177 std::optional<bool>
178 PrefixUpdateProcessor::afterWithdraw(const ndn::Name& prefix)
179 {
180  return addOrDeletePrefix(prefix, false);
181 }
182 
183 } // namespace nlsr::update
ndn::mgmt::Dispatcher & m_dispatcher
void advertiseAndInsertPrefix(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...
void withdrawAndRemovePrefix(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 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.
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.
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