blob: 2bec0aed7976240c0341680e601601fcb2976637 [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"
awlane879afac2025-04-28 17:59:36 -050025#include "utility/boost-info-editor.hpp"
Davide Pesaventod90338d2021-01-07 17:50:05 -050026
Saurab Dulal7526cee2018-01-31 18:14:10 +000027#include <boost/algorithm/string.hpp>
Davide Pesavento7bc3d432021-10-25 21:08:04 -040028#include <fstream>
alvy297f4162015-03-03 17:15:33 -060029
Davide Pesavento384327d2025-01-02 01:40:23 -050030namespace nlsr::update {
alvy297f4162015-03-03 17:15:33 -060031
dmcoomescf8d0ed2017-02-21 11:39:01 -060032INIT_LOGGER(update.PrefixUpdateProcessor);
alvy297f4162015-03-03 17:15:33 -060033
Laqin Fan54a43f02017-03-08 12:31:30 -060034/** \brief an Interest tag to indicate command signer
35 */
36using SignerTag = ndn::SimpleTag<ndn::Name, 20>;
alvy297f4162015-03-03 17:15:33 -060037
Laqin Fan54a43f02017-03-08 12:31:30 -060038/** \brief obtain signer from SignerTag attached to Interest, if available
39 */
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040040static std::optional<std::string>
Laqin Fan54a43f02017-03-08 12:31:30 -060041getSignerFromTag(const ndn::Interest& interest)
42{
Davide Pesaventod90338d2021-01-07 17:50:05 -050043 auto signerTag = interest.getTag<SignerTag>();
Laqin Fan54a43f02017-03-08 12:31:30 -060044 if (signerTag == nullptr) {
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040045 return std::nullopt;
Laqin Fan54a43f02017-03-08 12:31:30 -060046 }
47 else {
48 return signerTag->get().toUri();
49 }
50}
51
52PrefixUpdateProcessor::PrefixUpdateProcessor(ndn::mgmt::Dispatcher& dispatcher,
Ashlesh Gawande85998a12017-12-07 22:22:13 -060053 ndn::security::ValidatorConfig& validator,
alvy297f4162015-03-03 17:15:33 -060054 NamePrefixList& namePrefixList,
Saurab Dulal7526cee2018-01-31 18:14:10 +000055 Lsdb& lsdb, const std::string& configFileName)
Davide Pesavento20e60a22025-01-07 01:50:10 -050056 : CommandProcessor(dispatcher, namePrefixList, lsdb)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060057 , m_validator(validator)
dulalsaurab82a34c22019-02-04 17:31:21 +000058 , m_confFileNameDynamic(configFileName)
alvy297f4162015-03-03 17:15:33 -060059{
Davide Pesavento968eb1a2025-01-07 00:46:05 -050060 m_dispatcher.addControlCommand<AdvertisePrefixCommand>(
Laqin Fan54a43f02017-03-08 12:31:30 -060061 makeAuthorization(),
Davide Pesavento20e60a22025-01-07 01:50:10 -050062 // the first and second arguments are ignored since the handler does not need them
Davide Pesavento968eb1a2025-01-07 00:46:05 -050063 std::bind(&PrefixUpdateProcessor::advertiseAndInsertPrefix, this, _3, _4));
Laqin Fan54a43f02017-03-08 12:31:30 -060064
Davide Pesavento968eb1a2025-01-07 00:46:05 -050065 m_dispatcher.addControlCommand<WithdrawPrefixCommand>(
Laqin Fan54a43f02017-03-08 12:31:30 -060066 makeAuthorization(),
Davide Pesavento20e60a22025-01-07 01:50:10 -050067 // the first and second arguments are ignored since the handler does not need them
Davide Pesavento968eb1a2025-01-07 00:46:05 -050068 std::bind(&PrefixUpdateProcessor::withdrawAndRemovePrefix, this, _3, _4));
alvy297f4162015-03-03 17:15:33 -060069}
70
Laqin Fan54a43f02017-03-08 12:31:30 -060071ndn::mgmt::Authorization
72PrefixUpdateProcessor::makeAuthorization()
alvy297f4162015-03-03 17:15:33 -060073{
Laqin Fan54a43f02017-03-08 12:31:30 -060074 return [=] (const ndn::Name& prefix, const ndn::Interest& interest,
Davide Pesaventod2610dc2025-01-03 13:20:06 -050075 const ndn::mgmt::ControlParametersBase* params,
Laqin Fan54a43f02017-03-08 12:31:30 -060076 const ndn::mgmt::AcceptContinuation& accept,
77 const ndn::mgmt::RejectContinuation& reject) {
78 m_validator.validate(interest,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050079 [accept] (const ndn::Interest& request) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050080 auto signer1 = getSignerFromTag(request);
Laqin Fan54a43f02017-03-08 12:31:30 -060081 std::string signer = signer1.value_or("*");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050082 NLSR_LOG_DEBUG("accept " << request.getName() << " signer=" << signer);
Laqin Fan54a43f02017-03-08 12:31:30 -060083 accept(signer);
84 },
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040085 [reject] (const ndn::Interest& request, const ndn::security::ValidationError& error) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050086 NLSR_LOG_DEBUG("reject " << request.getName() << " signer=" <<
Davide Pesavento968eb1a2025-01-07 00:46:05 -050087 getSignerFromTag(request).value_or("?") << ' ' << error);
Laqin Fan54a43f02017-03-08 12:31:30 -060088 reject(ndn::mgmt::RejectReply::STATUS403);
89 });
90 };
alvy297f4162015-03-03 17:15:33 -060091}
92
93void
94PrefixUpdateProcessor::loadValidator(boost::property_tree::ptree section,
95 const std::string& filename)
96{
97 m_validator.load(section, filename);
98}
99
Saurab Dulal7526cee2018-01-31 18:14:10 +0000100bool
101PrefixUpdateProcessor::checkForPrefixInFile(const std::string prefix)
102{
103 std::string line;
dulalsaurab82a34c22019-02-04 17:31:21 +0000104 std::fstream fp(m_confFileNameDynamic);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000105 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 }
Saurab Dulal7526cee2018-01-31 18:14:10 +0000115 return false;
116}
117
awlane697a0ad2025-04-21 13:09:17 -0500118std::tuple<bool, std::string>
awlane879afac2025-04-28 17:59:36 -0500119PrefixUpdateProcessor::addOrDeletePrefix(const ndn::Name& prefix, uint64_t cost, bool addPrefix)
Saurab Dulal7526cee2018-01-31 18:14:10 +0000120{
awlane879afac2025-04-28 17:59:36 -0500121 std::string section = "advertising." + prefix.toUri();
122 std::string value = " " + prefix.toUri() + " " + std::to_string(cost);
dulalsaurab82a34c22019-02-04 17:31:21 +0000123 std::fstream input(m_confFileNameDynamic, input.in);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000124 if (!input.good() || !input.is_open()) {
125 NLSR_LOG_ERROR("Failed to open configuration file for parsing");
awlane697a0ad2025-04-21 13:09:17 -0500126 return {false, "Failed to open configuration file for parsing"};
Saurab Dulal7526cee2018-01-31 18:14:10 +0000127 }
awlane879afac2025-04-28 17:59:36 -0500128 input.close();
Saurab Dulal7526cee2018-01-31 18:14:10 +0000129 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");
awlane697a0ad2025-04-21 13:09:17 -0500133 return {false, "Prefix already exists in the configuration file"};
Saurab Dulal7526cee2018-01-31 18:14:10 +0000134 }
awlane879afac2025-04-28 17:59:36 -0500135 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"};
Saurab Dulal7526cee2018-01-31 18:14:10 +0000138 }
139 }
140 else {
141 if (!checkForPrefixInFile(value)) {
142 NLSR_LOG_ERROR("Prefix doesn't exists in the configuration file");
awlane697a0ad2025-04-21 13:09:17 -0500143 return {false, "Prefix doesn't exists in the configuration file"};
Saurab Dulal7526cee2018-01-31 18:14:10 +0000144 }
awlane879afac2025-04-28 17:59:36 -0500145 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"};
Saurab Dulal7526cee2018-01-31 18:14:10 +0000148 }
149 }
awlane879afac2025-04-28 17:59:36 -0500150
awlane697a0ad2025-04-21 13:09:17 -0500151 return {true, "OK"};
Saurab Dulal7526cee2018-01-31 18:14:10 +0000152}
153
awlane697a0ad2025-04-21 13:09:17 -0500154std::tuple<bool, std::string>
awlane879afac2025-04-28 17:59:36 -0500155PrefixUpdateProcessor::afterAdvertise(const ndn::Name& prefix, uint64_t cost)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600156{
awlane879afac2025-04-28 17:59:36 -0500157 return addOrDeletePrefix(prefix, cost, true);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000158}
159
awlane697a0ad2025-04-21 13:09:17 -0500160std::tuple<bool, std::string>
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600161PrefixUpdateProcessor::afterWithdraw(const ndn::Name& prefix)
162{
awlane879afac2025-04-28 17:59:36 -0500163 return addOrDeletePrefix(prefix, 0, false);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000164}
165
Davide Pesavento384327d2025-01-02 01:40:23 -0500166} // namespace nlsr::update