blob: 83bfcc4e339a0f1482eec88f2ac1b7e779c5f915 [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"
Davide Pesavento384327d2025-01-02 01:40:23 -050025#include "prefix-update-commands.hpp"
Davide Pesaventod90338d2021-01-07 17:50:05 -050026
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050027#include <ndn-cxx/face.hpp>
Davide Pesaventod90338d2021-01-07 17:50:05 -050028
Saurab Dulal7526cee2018-01-31 18:14:10 +000029#include <boost/algorithm/string.hpp>
Davide Pesavento7bc3d432021-10-25 21:08:04 -040030#include <fstream>
alvy297f4162015-03-03 17:15:33 -060031
Davide Pesavento384327d2025-01-02 01:40:23 -050032namespace nlsr::update {
alvy297f4162015-03-03 17:15:33 -060033
dmcoomescf8d0ed2017-02-21 11:39:01 -060034INIT_LOGGER(update.PrefixUpdateProcessor);
alvy297f4162015-03-03 17:15:33 -060035
Laqin Fan54a43f02017-03-08 12:31:30 -060036/** \brief an Interest tag to indicate command signer
37 */
38using SignerTag = ndn::SimpleTag<ndn::Name, 20>;
alvy297f4162015-03-03 17:15:33 -060039
Laqin Fan54a43f02017-03-08 12:31:30 -060040/** \brief obtain signer from SignerTag attached to Interest, if available
41 */
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040042static std::optional<std::string>
Laqin Fan54a43f02017-03-08 12:31:30 -060043getSignerFromTag(const ndn::Interest& interest)
44{
Davide Pesaventod90338d2021-01-07 17:50:05 -050045 auto signerTag = interest.getTag<SignerTag>();
Laqin Fan54a43f02017-03-08 12:31:30 -060046 if (signerTag == nullptr) {
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040047 return std::nullopt;
Laqin Fan54a43f02017-03-08 12:31:30 -060048 }
49 else {
50 return signerTag->get().toUri();
51 }
52}
53
54PrefixUpdateProcessor::PrefixUpdateProcessor(ndn::mgmt::Dispatcher& dispatcher,
Ashlesh Gawande85998a12017-12-07 22:22:13 -060055 ndn::security::ValidatorConfig& validator,
alvy297f4162015-03-03 17:15:33 -060056 NamePrefixList& namePrefixList,
Saurab Dulal7526cee2018-01-31 18:14:10 +000057 Lsdb& lsdb, const std::string& configFileName)
Laqin Fan54a43f02017-03-08 12:31:30 -060058 : CommandManagerBase(dispatcher, namePrefixList, lsdb, "prefix-update")
Ashlesh Gawande85998a12017-12-07 22:22:13 -060059 , m_validator(validator)
dulalsaurab82a34c22019-02-04 17:31:21 +000060 , m_confFileNameDynamic(configFileName)
alvy297f4162015-03-03 17:15:33 -060061{
Davide Pesavento968eb1a2025-01-07 00:46:05 -050062 m_dispatcher.addControlCommand<AdvertisePrefixCommand>(
Laqin Fan54a43f02017-03-08 12:31:30 -060063 makeAuthorization(),
Davide Pesavento968eb1a2025-01-07 00:46:05 -050064 std::bind(&PrefixUpdateProcessor::advertiseAndInsertPrefix, this, _3, _4));
Laqin Fan54a43f02017-03-08 12:31:30 -060065
Davide Pesavento968eb1a2025-01-07 00:46:05 -050066 m_dispatcher.addControlCommand<WithdrawPrefixCommand>(
Laqin Fan54a43f02017-03-08 12:31:30 -060067 makeAuthorization(),
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
118bool
119PrefixUpdateProcessor::addOrDeletePrefix(const ndn::Name& prefix, bool addPrefix)
120{
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500121 std::string value = " prefix " + prefix.toUri();
Saurab Dulal7526cee2018-01-31 18:14:10 +0000122 std::string fileString;
123 std::string line;
124 std::string trimedLine;
dulalsaurab82a34c22019-02-04 17:31:21 +0000125 std::fstream input(m_confFileNameDynamic, input.in);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000126 if (!input.good() || !input.is_open()) {
127 NLSR_LOG_ERROR("Failed to open configuration file for parsing");
128 return false;
129 }
130
131 if (addPrefix) {
132 //check if prefix already exist in the nlsr configuration file
133 if (checkForPrefixInFile(value)) {
134 NLSR_LOG_ERROR("Prefix already exists in the configuration file");
135 return false;
136 }
137 while (!input.eof()) {
138 getline(input, line);
139 if (!line.empty()) {
140 fileString.append(line + "\n");
141 if (line == "advertising") {
142 getline(input, line);
143 fileString.append(line + "\n" + value + "\n");
144 }
145 }
146 }
147 }
148 else {
149 if (!checkForPrefixInFile(value)) {
150 NLSR_LOG_ERROR("Prefix doesn't exists in the configuration file");
151 return false;
152 }
153 boost::trim(value);
154 while (!input.eof()) {
155 getline(input, line);
156 if (!line.empty()) {
157 std::string trimLine = line;
158 boost::trim(trimLine);
159 if (trimLine != value) {
160 fileString.append(line + "\n");
161 }
162 }
163 }
164 }
165 input.close();
dulalsaurab82a34c22019-02-04 17:31:21 +0000166 std::ofstream output(m_confFileNameDynamic);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000167 output << fileString;
168 output.close();
169 return true;
170}
171
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400172std::optional<bool>
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600173PrefixUpdateProcessor::afterAdvertise(const ndn::Name& prefix)
174{
Saurab Dulal7526cee2018-01-31 18:14:10 +0000175 return addOrDeletePrefix(prefix, true);
176}
177
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400178std::optional<bool>
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600179PrefixUpdateProcessor::afterWithdraw(const ndn::Name& prefix)
180{
Saurab Dulal7526cee2018-01-31 18:14:10 +0000181 return addOrDeletePrefix(prefix, false);
182}
183
Davide Pesavento384327d2025-01-02 01:40:23 -0500184} // namespace nlsr::update