blob: 1a1b5daf64dcd83a56b388c0f039a2709b313a5b [file] [log] [blame]
alvy297f4162015-03-03 17:15:33 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -04002/*
Davide Pesavento384327d2025-01-02 01:40:23 -05003 * Copyright (c) 2014-2025, The University of Memphis,
alvy297f4162015-03-03 17:15:33 -06004 * 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/>.
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040020 */
alvy297f4162015-03-03 17:15:33 -060021
22#include "prefix-update-processor.hpp"
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040023#include "logger.hpp"
alvy297f4162015-03-03 17:15:33 -060024#include "lsdb.hpp"
25#include "nlsr.hpp"
Davide Pesavento384327d2025-01-02 01:40:23 -050026#include "prefix-update-commands.hpp"
Davide Pesaventod90338d2021-01-07 17:50:05 -050027
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050028#include <ndn-cxx/face.hpp>
Davide Pesaventod90338d2021-01-07 17:50:05 -050029#include <ndn-cxx/mgmt/nfd/control-response.hpp>
30
Saurab Dulal7526cee2018-01-31 18:14:10 +000031#include <boost/algorithm/string.hpp>
Davide Pesavento7bc3d432021-10-25 21:08:04 -040032#include <fstream>
alvy297f4162015-03-03 17:15:33 -060033
Davide Pesavento384327d2025-01-02 01:40:23 -050034namespace nlsr::update {
alvy297f4162015-03-03 17:15:33 -060035
dmcoomescf8d0ed2017-02-21 11:39:01 -060036INIT_LOGGER(update.PrefixUpdateProcessor);
alvy297f4162015-03-03 17:15:33 -060037
Laqin Fan54a43f02017-03-08 12:31:30 -060038/** \brief an Interest tag to indicate command signer
39 */
40using SignerTag = ndn::SimpleTag<ndn::Name, 20>;
alvy297f4162015-03-03 17:15:33 -060041
Laqin Fan54a43f02017-03-08 12:31:30 -060042/** \brief obtain signer from SignerTag attached to Interest, if available
43 */
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040044static std::optional<std::string>
Laqin Fan54a43f02017-03-08 12:31:30 -060045getSignerFromTag(const ndn::Interest& interest)
46{
Davide Pesaventod90338d2021-01-07 17:50:05 -050047 auto signerTag = interest.getTag<SignerTag>();
Laqin Fan54a43f02017-03-08 12:31:30 -060048 if (signerTag == nullptr) {
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040049 return std::nullopt;
Laqin Fan54a43f02017-03-08 12:31:30 -060050 }
51 else {
52 return signerTag->get().toUri();
53 }
54}
55
56PrefixUpdateProcessor::PrefixUpdateProcessor(ndn::mgmt::Dispatcher& dispatcher,
Ashlesh Gawande85998a12017-12-07 22:22:13 -060057 ndn::security::ValidatorConfig& validator,
alvy297f4162015-03-03 17:15:33 -060058 NamePrefixList& namePrefixList,
Saurab Dulal7526cee2018-01-31 18:14:10 +000059 Lsdb& lsdb, const std::string& configFileName)
Laqin Fan54a43f02017-03-08 12:31:30 -060060 : CommandManagerBase(dispatcher, namePrefixList, lsdb, "prefix-update")
Ashlesh Gawande85998a12017-12-07 22:22:13 -060061 , m_validator(validator)
dulalsaurab82a34c22019-02-04 17:31:21 +000062 , m_confFileNameDynamic(configFileName)
alvy297f4162015-03-03 17:15:33 -060063{
dmcoomes5bcb39e2017-10-31 15:07:55 -050064 NLSR_LOG_DEBUG("Setting dispatcher to capture Interests for: "
Laqin Fan54a43f02017-03-08 12:31:30 -060065 << ndn::Name(Nlsr::LOCALHOST_PREFIX).append("prefix-update"));
66
67 m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(makeRelPrefix("advertise"),
68 makeAuthorization(),
Davide Pesavento384327d2025-01-02 01:40:23 -050069 [] (const auto& p) { return validateParameters<AdvertisePrefixCommand>(p); },
Laqin Fan54a43f02017-03-08 12:31:30 -060070 std::bind(&PrefixUpdateProcessor::advertiseAndInsertPrefix, this, _1, _2, _3, _4));
71
72 m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(makeRelPrefix("withdraw"),
73 makeAuthorization(),
Davide Pesavento384327d2025-01-02 01:40:23 -050074 [] (const auto& p) { return validateParameters<WithdrawPrefixCommand>(p); },
Laqin Fan54a43f02017-03-08 12:31:30 -060075 std::bind(&PrefixUpdateProcessor::withdrawAndRemovePrefix, this, _1, _2, _3, _4));
alvy297f4162015-03-03 17:15:33 -060076}
77
Laqin Fan54a43f02017-03-08 12:31:30 -060078ndn::mgmt::Authorization
79PrefixUpdateProcessor::makeAuthorization()
alvy297f4162015-03-03 17:15:33 -060080{
Laqin Fan54a43f02017-03-08 12:31:30 -060081 return [=] (const ndn::Name& prefix, const ndn::Interest& interest,
Davide Pesaventod2610dc2025-01-03 13:20:06 -050082 const ndn::mgmt::ControlParametersBase* params,
Laqin Fan54a43f02017-03-08 12:31:30 -060083 const ndn::mgmt::AcceptContinuation& accept,
84 const ndn::mgmt::RejectContinuation& reject) {
85 m_validator.validate(interest,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050086 [accept] (const ndn::Interest& request) {
alvy297f4162015-03-03 17:15:33 -060087
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050088 auto signer1 = getSignerFromTag(request);
Laqin Fan54a43f02017-03-08 12:31:30 -060089 std::string signer = signer1.value_or("*");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050090 NLSR_LOG_DEBUG("accept " << request.getName() << " signer=" << signer);
Laqin Fan54a43f02017-03-08 12:31:30 -060091 accept(signer);
92 },
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040093 [reject] (const ndn::Interest& request, const ndn::security::ValidationError& error) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050094 NLSR_LOG_DEBUG("reject " << request.getName() << " signer=" <<
95 getSignerFromTag(request).value_or("?") << ' ' << error);
Laqin Fan54a43f02017-03-08 12:31:30 -060096 reject(ndn::mgmt::RejectReply::STATUS403);
97 });
98 };
alvy297f4162015-03-03 17:15:33 -060099}
100
101void
102PrefixUpdateProcessor::loadValidator(boost::property_tree::ptree section,
103 const std::string& filename)
104{
105 m_validator.load(section, filename);
106}
107
Saurab Dulal7526cee2018-01-31 18:14:10 +0000108bool
109PrefixUpdateProcessor::checkForPrefixInFile(const std::string prefix)
110{
111 std::string line;
dulalsaurab82a34c22019-02-04 17:31:21 +0000112 std::fstream fp(m_confFileNameDynamic);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000113 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
127bool
128PrefixUpdateProcessor::addOrDeletePrefix(const ndn::Name& prefix, bool addPrefix)
129{
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500130 std::string value = " prefix " + prefix.toUri();
Saurab Dulal7526cee2018-01-31 18:14:10 +0000131 std::string fileString;
132 std::string line;
133 std::string trimedLine;
dulalsaurab82a34c22019-02-04 17:31:21 +0000134 std::fstream input(m_confFileNameDynamic, input.in);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000135 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();
dulalsaurab82a34c22019-02-04 17:31:21 +0000175 std::ofstream output(m_confFileNameDynamic);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000176 output << fileString;
177 output.close();
178 return true;
179}
180
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400181std::optional<bool>
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600182PrefixUpdateProcessor::afterAdvertise(const ndn::Name& prefix)
183{
Saurab Dulal7526cee2018-01-31 18:14:10 +0000184 return addOrDeletePrefix(prefix, true);
185}
186
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400187std::optional<bool>
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600188PrefixUpdateProcessor::afterWithdraw(const ndn::Name& prefix)
189{
Saurab Dulal7526cee2018-01-31 18:14:10 +0000190 return addOrDeletePrefix(prefix, false);
191}
192
Davide Pesavento384327d2025-01-02 01:40:23 -0500193} // namespace nlsr::update