blob: f07f0d0b42985a684052a0290689f23cf8125b83 [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"
Davide Pesavento384327d2025-01-02 01:40:23 -050024#include "prefix-update-commands.hpp"
Davide Pesaventod90338d2021-01-07 17:50:05 -050025
Saurab Dulal7526cee2018-01-31 18:14:10 +000026#include <boost/algorithm/string.hpp>
Davide Pesavento7bc3d432021-10-25 21:08:04 -040027#include <fstream>
alvy297f4162015-03-03 17:15:33 -060028
Davide Pesavento384327d2025-01-02 01:40:23 -050029namespace nlsr::update {
alvy297f4162015-03-03 17:15:33 -060030
dmcoomescf8d0ed2017-02-21 11:39:01 -060031INIT_LOGGER(update.PrefixUpdateProcessor);
alvy297f4162015-03-03 17:15:33 -060032
Laqin Fan54a43f02017-03-08 12:31:30 -060033/** \brief an Interest tag to indicate command signer
34 */
35using SignerTag = ndn::SimpleTag<ndn::Name, 20>;
alvy297f4162015-03-03 17:15:33 -060036
Laqin Fan54a43f02017-03-08 12:31:30 -060037/** \brief obtain signer from SignerTag attached to Interest, if available
38 */
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040039static std::optional<std::string>
Laqin Fan54a43f02017-03-08 12:31:30 -060040getSignerFromTag(const ndn::Interest& interest)
41{
Davide Pesaventod90338d2021-01-07 17:50:05 -050042 auto signerTag = interest.getTag<SignerTag>();
Laqin Fan54a43f02017-03-08 12:31:30 -060043 if (signerTag == nullptr) {
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040044 return std::nullopt;
Laqin Fan54a43f02017-03-08 12:31:30 -060045 }
46 else {
47 return signerTag->get().toUri();
48 }
49}
50
51PrefixUpdateProcessor::PrefixUpdateProcessor(ndn::mgmt::Dispatcher& dispatcher,
Ashlesh Gawande85998a12017-12-07 22:22:13 -060052 ndn::security::ValidatorConfig& validator,
alvy297f4162015-03-03 17:15:33 -060053 NamePrefixList& namePrefixList,
Saurab Dulal7526cee2018-01-31 18:14:10 +000054 Lsdb& lsdb, const std::string& configFileName)
Davide Pesavento20e60a22025-01-07 01:50:10 -050055 : CommandProcessor(dispatcher, namePrefixList, lsdb)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060056 , m_validator(validator)
dulalsaurab82a34c22019-02-04 17:31:21 +000057 , m_confFileNameDynamic(configFileName)
alvy297f4162015-03-03 17:15:33 -060058{
Davide Pesavento968eb1a2025-01-07 00:46:05 -050059 m_dispatcher.addControlCommand<AdvertisePrefixCommand>(
Laqin Fan54a43f02017-03-08 12:31:30 -060060 makeAuthorization(),
Davide Pesavento20e60a22025-01-07 01:50:10 -050061 // the first and second arguments are ignored since the handler does not need them
Davide Pesavento968eb1a2025-01-07 00:46:05 -050062 std::bind(&PrefixUpdateProcessor::advertiseAndInsertPrefix, this, _3, _4));
Laqin Fan54a43f02017-03-08 12:31:30 -060063
Davide Pesavento968eb1a2025-01-07 00:46:05 -050064 m_dispatcher.addControlCommand<WithdrawPrefixCommand>(
Laqin Fan54a43f02017-03-08 12:31:30 -060065 makeAuthorization(),
Davide Pesavento20e60a22025-01-07 01:50:10 -050066 // the first and second arguments are ignored since the handler does not need them
Davide Pesavento968eb1a2025-01-07 00:46:05 -050067 std::bind(&PrefixUpdateProcessor::withdrawAndRemovePrefix, this, _3, _4));
alvy297f4162015-03-03 17:15:33 -060068}
69
Laqin Fan54a43f02017-03-08 12:31:30 -060070ndn::mgmt::Authorization
71PrefixUpdateProcessor::makeAuthorization()
alvy297f4162015-03-03 17:15:33 -060072{
Laqin Fan54a43f02017-03-08 12:31:30 -060073 return [=] (const ndn::Name& prefix, const ndn::Interest& interest,
Davide Pesaventod2610dc2025-01-03 13:20:06 -050074 const ndn::mgmt::ControlParametersBase* params,
Laqin Fan54a43f02017-03-08 12:31:30 -060075 const ndn::mgmt::AcceptContinuation& accept,
76 const ndn::mgmt::RejectContinuation& reject) {
77 m_validator.validate(interest,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050078 [accept] (const ndn::Interest& request) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050079 auto signer1 = getSignerFromTag(request);
Laqin Fan54a43f02017-03-08 12:31:30 -060080 std::string signer = signer1.value_or("*");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050081 NLSR_LOG_DEBUG("accept " << request.getName() << " signer=" << signer);
Laqin Fan54a43f02017-03-08 12:31:30 -060082 accept(signer);
83 },
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040084 [reject] (const ndn::Interest& request, const ndn::security::ValidationError& error) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050085 NLSR_LOG_DEBUG("reject " << request.getName() << " signer=" <<
Davide Pesavento968eb1a2025-01-07 00:46:05 -050086 getSignerFromTag(request).value_or("?") << ' ' << error);
Laqin Fan54a43f02017-03-08 12:31:30 -060087 reject(ndn::mgmt::RejectReply::STATUS403);
88 });
89 };
alvy297f4162015-03-03 17:15:33 -060090}
91
92void
93PrefixUpdateProcessor::loadValidator(boost::property_tree::ptree section,
94 const std::string& filename)
95{
96 m_validator.load(section, filename);
97}
98
Saurab Dulal7526cee2018-01-31 18:14:10 +000099bool
100PrefixUpdateProcessor::checkForPrefixInFile(const std::string prefix)
101{
102 std::string line;
dulalsaurab82a34c22019-02-04 17:31:21 +0000103 std::fstream fp(m_confFileNameDynamic);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000104 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 }
Saurab Dulal7526cee2018-01-31 18:14:10 +0000114 return false;
115}
116
117bool
118PrefixUpdateProcessor::addOrDeletePrefix(const ndn::Name& prefix, bool addPrefix)
119{
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500120 std::string value = " prefix " + prefix.toUri();
Saurab Dulal7526cee2018-01-31 18:14:10 +0000121 std::string fileString;
122 std::string line;
123 std::string trimedLine;
dulalsaurab82a34c22019-02-04 17:31:21 +0000124 std::fstream input(m_confFileNameDynamic, input.in);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000125 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();
dulalsaurab82a34c22019-02-04 17:31:21 +0000165 std::ofstream output(m_confFileNameDynamic);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000166 output << fileString;
167 output.close();
168 return true;
169}
170
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400171std::optional<bool>
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600172PrefixUpdateProcessor::afterAdvertise(const ndn::Name& prefix)
173{
Saurab Dulal7526cee2018-01-31 18:14:10 +0000174 return addOrDeletePrefix(prefix, true);
175}
176
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400177std::optional<bool>
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600178PrefixUpdateProcessor::afterWithdraw(const ndn::Name& prefix)
179{
Saurab Dulal7526cee2018-01-31 18:14:10 +0000180 return addOrDeletePrefix(prefix, false);
181}
182
Davide Pesavento384327d2025-01-02 01:40:23 -0500183} // namespace nlsr::update