Loading...
Searching...
No Matches
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"
26
27#include <boost/algorithm/string.hpp>
28#include <fstream>
29
30namespace nlsr::update {
31
32INIT_LOGGER(update.PrefixUpdateProcessor);
33
36using SignerTag = ndn::SimpleTag<ndn::Name, 20>;
37
40static std::optional<std::string>
41getSignerFromTag(const ndn::Interest& interest)
42{
43 auto signerTag = interest.getTag<SignerTag>();
44 if (signerTag == nullptr) {
45 return std::nullopt;
46 }
47 else {
48 return signerTag->get().toUri();
49 }
50}
51
52PrefixUpdateProcessor::PrefixUpdateProcessor(ndn::mgmt::Dispatcher& dispatcher,
53 ndn::security::ValidatorConfig& validator,
54 NamePrefixList& namePrefixList,
55 Lsdb& lsdb, const std::string& configFileName)
56 : CommandProcessor(dispatcher, namePrefixList, lsdb)
57 , m_validator(validator)
58 , m_confFileNameDynamic(configFileName)
59{
60 m_dispatcher.addControlCommand<AdvertisePrefixCommand>(
61 makeAuthorization(),
62 // the first and second arguments are ignored since the handler does not need them
63 std::bind(&PrefixUpdateProcessor::advertiseAndInsertPrefix, this, _3, _4));
64
65 m_dispatcher.addControlCommand<WithdrawPrefixCommand>(
66 makeAuthorization(),
67 // the first and second arguments are ignored since the handler does not need them
68 std::bind(&PrefixUpdateProcessor::withdrawAndRemovePrefix, this, _3, _4));
69}
70
71ndn::mgmt::Authorization
72PrefixUpdateProcessor::makeAuthorization()
73{
74 return [=] (const ndn::Name& prefix, const ndn::Interest& interest,
75 const ndn::mgmt::ControlParametersBase* params,
76 const ndn::mgmt::AcceptContinuation& accept,
77 const ndn::mgmt::RejectContinuation& reject) {
78 m_validator.validate(interest,
79 [accept] (const ndn::Interest& request) {
80 auto signer1 = getSignerFromTag(request);
81 std::string signer = signer1.value_or("*");
82 NLSR_LOG_DEBUG("accept " << request.getName() << " signer=" << signer);
83 accept(signer);
84 },
85 [reject] (const ndn::Interest& request, const ndn::security::ValidationError& error) {
86 NLSR_LOG_DEBUG("reject " << request.getName() << " signer=" <<
87 getSignerFromTag(request).value_or("?") << ' ' << error);
88 reject(ndn::mgmt::RejectReply::STATUS403);
89 });
90 };
91}
92
93void
94PrefixUpdateProcessor::loadValidator(boost::property_tree::ptree section,
95 const std::string& filename)
96{
97 m_validator.load(section, filename);
98}
99
100bool
102{
103 std::string line;
104 std::fstream fp(m_confFileNameDynamic);
105 if (!fp.good() || !fp.is_open()) {
106 NLSR_LOG_ERROR("Failed to open configuration file for parsing");
107 return true;
108 }
109 while (!fp.eof()) {
110 getline(fp, line);
111 if (line == prefix) {
112 return true;
113 }
114 }
115 return false;
116}
117
118std::tuple<bool, std::string>
119PrefixUpdateProcessor::addOrDeletePrefix(const ndn::Name& prefix, uint64_t cost, bool addPrefix)
120{
121 std::string section = "advertising." + prefix.toUri();
122 std::string value = " " + prefix.toUri() + " " + std::to_string(cost);
123 std::fstream input(m_confFileNameDynamic, input.in);
124 if (!input.good() || !input.is_open()) {
125 NLSR_LOG_ERROR("Failed to open configuration file for parsing");
126 return {false, "Failed to open configuration file for parsing"};
127 }
128 input.close();
129 if (addPrefix) {
130 //check if prefix already exist in the nlsr configuration file
131 if (checkForPrefixInFile(value)) {
132 NLSR_LOG_ERROR("Prefix already exists in the configuration file");
133 return {false, "Prefix already exists in the configuration file"};
134 }
135 if (!util::boost_info_editor::put(m_confFileNameDynamic, section, std::to_string(cost))) {
136 NLSR_LOG_ERROR("Unable to save changes to configuration file");
137 return {false, "Unable to save changes to configuration file"};
138 }
139 }
140 else {
141 if (!checkForPrefixInFile(value)) {
142 NLSR_LOG_ERROR("Prefix doesn't exists in the configuration file");
143 return {false, "Prefix doesn't exists in the configuration file"};
144 }
145 if (!util::boost_info_editor::remove(m_confFileNameDynamic, section)) {
146 NLSR_LOG_ERROR("Unable to save changes to configuration file");
147 return {false, "Unable to save changes to configuration file"};
148 }
149 }
150
151 return {true, "OK"};
152}
153
154std::tuple<bool, std::string>
155PrefixUpdateProcessor::afterAdvertise(const ndn::Name& prefix, uint64_t cost)
156{
157 return addOrDeletePrefix(prefix, cost, true);
158}
159
160std::tuple<bool, std::string>
162{
163 return addOrDeletePrefix(prefix, 0, false);
164}
165
166} // 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...
std::tuple< bool, std::string > addOrDeletePrefix(const ndn::Name &prefix, uint64_t cost, bool addPrefix)
Add or delete an advertise or withdrawn prefix to the nlsr configuration file.
void loadValidator(ConfigSection section, const std::string &filename)
Load the validator's configuration from a section of a configuration file.
PrefixUpdateProcessor(ndn::mgmt::Dispatcher &dispatcher, ndn::security::ValidatorConfig &validator, NamePrefixList &namePrefixList, Lsdb &lsdb, const std::string &configFileName)
std::tuple< bool, std::string > afterAdvertise(const ndn::Name &prefix, uint64_t cost) override
Save an advertised prefix to the nlsr configuration file.
std::tuple< bool, std::string > afterWithdraw(const ndn::Name &prefix) override
Remove an advertised prefix from 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
static std::optional< std::string > getSignerFromTag(const ndn::Interest &interest)
obtain signer from SignerTag attached to Interest, if available
ndn::SimpleTag< ndn::Name, 20 > SignerTag
an Interest tag to indicate command signer
bool remove(const std::string &fileName, const std::string &section)
bool put(const std::string &fileName, const std::string &section, const std::string &value)