blob: d8302d485f6f04d03cc4ad9a5a117948710bc4bc [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 Pesaventod90338d2021-01-07 17:50:05 -05003 * Copyright (c) 2014-2021, 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"
alvy297f4162015-03-03 17:15:33 -060023#include "lsdb.hpp"
24#include "nlsr.hpp"
Davide Pesaventod90338d2021-01-07 17:50:05 -050025
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050026#include <ndn-cxx/face.hpp>
Davide Pesaventod90338d2021-01-07 17:50:05 -050027#include <ndn-cxx/mgmt/nfd/control-response.hpp>
28
Saurab Dulal7526cee2018-01-31 18:14:10 +000029#include <boost/algorithm/string.hpp>
30#include <algorithm>
Davide Pesavento7bc3d432021-10-25 21:08:04 -040031#include <fstream>
alvy297f4162015-03-03 17:15:33 -060032
33namespace nlsr {
34namespace update {
35
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 */
44static ndn::optional<std::string>
45getSignerFromTag(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) {
49 return ndn::nullopt;
50 }
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(),
69 std::bind(&PrefixUpdateProcessor::validateParameters<AdvertisePrefixCommand>,
70 this, _1),
71 std::bind(&PrefixUpdateProcessor::advertiseAndInsertPrefix, this, _1, _2, _3, _4));
72
73 m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(makeRelPrefix("withdraw"),
74 makeAuthorization(),
75 std::bind(&PrefixUpdateProcessor::validateParameters<WithdrawPrefixCommand>,
76 this, _1),
77 std::bind(&PrefixUpdateProcessor::withdrawAndRemovePrefix, this, _1, _2, _3, _4));
alvy297f4162015-03-03 17:15:33 -060078}
79
Laqin Fan54a43f02017-03-08 12:31:30 -060080ndn::mgmt::Authorization
81PrefixUpdateProcessor::makeAuthorization()
alvy297f4162015-03-03 17:15:33 -060082{
Laqin Fan54a43f02017-03-08 12:31:30 -060083 return [=] (const ndn::Name& prefix, const ndn::Interest& interest,
84 const ndn::mgmt::ControlParameters* params,
85 const ndn::mgmt::AcceptContinuation& accept,
86 const ndn::mgmt::RejectContinuation& reject) {
87 m_validator.validate(interest,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050088 [accept] (const ndn::Interest& request) {
alvy297f4162015-03-03 17:15:33 -060089
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050090 auto signer1 = getSignerFromTag(request);
Laqin Fan54a43f02017-03-08 12:31:30 -060091 std::string signer = signer1.value_or("*");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050092 NLSR_LOG_DEBUG("accept " << request.getName() << " signer=" << signer);
Laqin Fan54a43f02017-03-08 12:31:30 -060093 accept(signer);
94 },
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040095 [reject] (const ndn::Interest& request, const ndn::security::ValidationError& error) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050096 NLSR_LOG_DEBUG("reject " << request.getName() << " signer=" <<
97 getSignerFromTag(request).value_or("?") << ' ' << error);
Laqin Fan54a43f02017-03-08 12:31:30 -060098 reject(ndn::mgmt::RejectReply::STATUS403);
99 });
100 };
alvy297f4162015-03-03 17:15:33 -0600101}
102
103void
104PrefixUpdateProcessor::loadValidator(boost::property_tree::ptree section,
105 const std::string& filename)
106{
107 m_validator.load(section, filename);
108}
109
Saurab Dulal7526cee2018-01-31 18:14:10 +0000110bool
111PrefixUpdateProcessor::checkForPrefixInFile(const std::string prefix)
112{
113 std::string line;
dulalsaurab82a34c22019-02-04 17:31:21 +0000114 std::fstream fp(m_confFileNameDynamic);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000115 if (!fp.good() || !fp.is_open()) {
116 NLSR_LOG_ERROR("Failed to open configuration file for parsing");
117 return true;
118 }
119 while (!fp.eof()) {
120 getline(fp, line);
121 if (line == prefix) {
122 return true;
123 }
124 }
125 fp.close();
126 return false;
127}
128
129bool
130PrefixUpdateProcessor::addOrDeletePrefix(const ndn::Name& prefix, bool addPrefix)
131{
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500132 std::string value = " prefix " + prefix.toUri();
Saurab Dulal7526cee2018-01-31 18:14:10 +0000133 std::string fileString;
134 std::string line;
135 std::string trimedLine;
dulalsaurab82a34c22019-02-04 17:31:21 +0000136 std::fstream input(m_confFileNameDynamic, input.in);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000137 if (!input.good() || !input.is_open()) {
138 NLSR_LOG_ERROR("Failed to open configuration file for parsing");
139 return false;
140 }
141
142 if (addPrefix) {
143 //check if prefix already exist in the nlsr configuration file
144 if (checkForPrefixInFile(value)) {
145 NLSR_LOG_ERROR("Prefix already exists in the configuration file");
146 return false;
147 }
148 while (!input.eof()) {
149 getline(input, line);
150 if (!line.empty()) {
151 fileString.append(line + "\n");
152 if (line == "advertising") {
153 getline(input, line);
154 fileString.append(line + "\n" + value + "\n");
155 }
156 }
157 }
158 }
159 else {
160 if (!checkForPrefixInFile(value)) {
161 NLSR_LOG_ERROR("Prefix doesn't exists in the configuration file");
162 return false;
163 }
164 boost::trim(value);
165 while (!input.eof()) {
166 getline(input, line);
167 if (!line.empty()) {
168 std::string trimLine = line;
169 boost::trim(trimLine);
170 if (trimLine != value) {
171 fileString.append(line + "\n");
172 }
173 }
174 }
175 }
176 input.close();
dulalsaurab82a34c22019-02-04 17:31:21 +0000177 std::ofstream output(m_confFileNameDynamic);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000178 output << fileString;
179 output.close();
180 return true;
181}
182
183ndn::optional<bool>
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600184PrefixUpdateProcessor::afterAdvertise(const ndn::Name& prefix)
185{
Saurab Dulal7526cee2018-01-31 18:14:10 +0000186 return addOrDeletePrefix(prefix, true);
187}
188
189ndn::optional<bool>
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600190PrefixUpdateProcessor::afterWithdraw(const ndn::Name& prefix)
191{
Saurab Dulal7526cee2018-01-31 18:14:10 +0000192 return addOrDeletePrefix(prefix, false);
193}
194
alvy297f4162015-03-03 17:15:33 -0600195} // namespace update
196} // namespace nlsr