blob: 99cd76b31c22275d118fcec56c792cc8fb54d1ec [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>
alvy297f4162015-03-03 17:15:33 -060031
32namespace nlsr {
33namespace update {
34
dmcoomescf8d0ed2017-02-21 11:39:01 -060035INIT_LOGGER(update.PrefixUpdateProcessor);
alvy297f4162015-03-03 17:15:33 -060036
Laqin Fan54a43f02017-03-08 12:31:30 -060037/** \brief an Interest tag to indicate command signer
38 */
39using SignerTag = ndn::SimpleTag<ndn::Name, 20>;
alvy297f4162015-03-03 17:15:33 -060040
Laqin Fan54a43f02017-03-08 12:31:30 -060041/** \brief obtain signer from SignerTag attached to Interest, if available
42 */
43static ndn::optional<std::string>
44getSignerFromTag(const ndn::Interest& interest)
45{
Davide Pesaventod90338d2021-01-07 17:50:05 -050046 auto signerTag = interest.getTag<SignerTag>();
Laqin Fan54a43f02017-03-08 12:31:30 -060047 if (signerTag == nullptr) {
48 return ndn::nullopt;
49 }
50 else {
51 return signerTag->get().toUri();
52 }
53}
54
55PrefixUpdateProcessor::PrefixUpdateProcessor(ndn::mgmt::Dispatcher& dispatcher,
Ashlesh Gawande85998a12017-12-07 22:22:13 -060056 ndn::security::ValidatorConfig& validator,
alvy297f4162015-03-03 17:15:33 -060057 NamePrefixList& namePrefixList,
Saurab Dulal7526cee2018-01-31 18:14:10 +000058 Lsdb& lsdb, const std::string& configFileName)
Laqin Fan54a43f02017-03-08 12:31:30 -060059 : CommandManagerBase(dispatcher, namePrefixList, lsdb, "prefix-update")
Ashlesh Gawande85998a12017-12-07 22:22:13 -060060 , m_validator(validator)
dulalsaurab82a34c22019-02-04 17:31:21 +000061 , m_confFileNameDynamic(configFileName)
alvy297f4162015-03-03 17:15:33 -060062{
dmcoomes5bcb39e2017-10-31 15:07:55 -050063 NLSR_LOG_DEBUG("Setting dispatcher to capture Interests for: "
Laqin Fan54a43f02017-03-08 12:31:30 -060064 << ndn::Name(Nlsr::LOCALHOST_PREFIX).append("prefix-update"));
65
66 m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(makeRelPrefix("advertise"),
67 makeAuthorization(),
68 std::bind(&PrefixUpdateProcessor::validateParameters<AdvertisePrefixCommand>,
69 this, _1),
70 std::bind(&PrefixUpdateProcessor::advertiseAndInsertPrefix, this, _1, _2, _3, _4));
71
72 m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(makeRelPrefix("withdraw"),
73 makeAuthorization(),
74 std::bind(&PrefixUpdateProcessor::validateParameters<WithdrawPrefixCommand>,
75 this, _1),
76 std::bind(&PrefixUpdateProcessor::withdrawAndRemovePrefix, this, _1, _2, _3, _4));
alvy297f4162015-03-03 17:15:33 -060077}
78
Laqin Fan54a43f02017-03-08 12:31:30 -060079ndn::mgmt::Authorization
80PrefixUpdateProcessor::makeAuthorization()
alvy297f4162015-03-03 17:15:33 -060081{
Laqin Fan54a43f02017-03-08 12:31:30 -060082 return [=] (const ndn::Name& prefix, const ndn::Interest& interest,
83 const ndn::mgmt::ControlParameters* params,
84 const ndn::mgmt::AcceptContinuation& accept,
85 const ndn::mgmt::RejectContinuation& reject) {
86 m_validator.validate(interest,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050087 [accept] (const ndn::Interest& request) {
alvy297f4162015-03-03 17:15:33 -060088
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050089 auto signer1 = getSignerFromTag(request);
Laqin Fan54a43f02017-03-08 12:31:30 -060090 std::string signer = signer1.value_or("*");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050091 NLSR_LOG_DEBUG("accept " << request.getName() << " signer=" << signer);
Laqin Fan54a43f02017-03-08 12:31:30 -060092 accept(signer);
93 },
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040094 [reject] (const ndn::Interest& request, const ndn::security::ValidationError& error) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050095 NLSR_LOG_DEBUG("reject " << request.getName() << " signer=" <<
96 getSignerFromTag(request).value_or("?") << ' ' << error);
Laqin Fan54a43f02017-03-08 12:31:30 -060097 reject(ndn::mgmt::RejectReply::STATUS403);
98 });
99 };
alvy297f4162015-03-03 17:15:33 -0600100}
101
102void
103PrefixUpdateProcessor::loadValidator(boost::property_tree::ptree section,
104 const std::string& filename)
105{
106 m_validator.load(section, filename);
107}
108
Saurab Dulal7526cee2018-01-31 18:14:10 +0000109bool
110PrefixUpdateProcessor::checkForPrefixInFile(const std::string prefix)
111{
112 std::string line;
dulalsaurab82a34c22019-02-04 17:31:21 +0000113 std::fstream fp(m_confFileNameDynamic);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000114 if (!fp.good() || !fp.is_open()) {
115 NLSR_LOG_ERROR("Failed to open configuration file for parsing");
116 return true;
117 }
118 while (!fp.eof()) {
119 getline(fp, line);
120 if (line == prefix) {
121 return true;
122 }
123 }
124 fp.close();
125 return false;
126}
127
128bool
129PrefixUpdateProcessor::addOrDeletePrefix(const ndn::Name& prefix, bool addPrefix)
130{
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500131 std::string value = " prefix " + prefix.toUri();
Saurab Dulal7526cee2018-01-31 18:14:10 +0000132 std::string fileString;
133 std::string line;
134 std::string trimedLine;
dulalsaurab82a34c22019-02-04 17:31:21 +0000135 std::fstream input(m_confFileNameDynamic, input.in);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000136 if (!input.good() || !input.is_open()) {
137 NLSR_LOG_ERROR("Failed to open configuration file for parsing");
138 return false;
139 }
140
141 if (addPrefix) {
142 //check if prefix already exist in the nlsr configuration file
143 if (checkForPrefixInFile(value)) {
144 NLSR_LOG_ERROR("Prefix already exists in the configuration file");
145 return false;
146 }
147 while (!input.eof()) {
148 getline(input, line);
149 if (!line.empty()) {
150 fileString.append(line + "\n");
151 if (line == "advertising") {
152 getline(input, line);
153 fileString.append(line + "\n" + value + "\n");
154 }
155 }
156 }
157 }
158 else {
159 if (!checkForPrefixInFile(value)) {
160 NLSR_LOG_ERROR("Prefix doesn't exists in the configuration file");
161 return false;
162 }
163 boost::trim(value);
164 while (!input.eof()) {
165 getline(input, line);
166 if (!line.empty()) {
167 std::string trimLine = line;
168 boost::trim(trimLine);
169 if (trimLine != value) {
170 fileString.append(line + "\n");
171 }
172 }
173 }
174 }
175 input.close();
dulalsaurab82a34c22019-02-04 17:31:21 +0000176 std::ofstream output(m_confFileNameDynamic);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000177 output << fileString;
178 output.close();
179 return true;
180}
181
182ndn::optional<bool>
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600183PrefixUpdateProcessor::afterAdvertise(const ndn::Name& prefix)
184{
Saurab Dulal7526cee2018-01-31 18:14:10 +0000185 return addOrDeletePrefix(prefix, true);
186}
187
188ndn::optional<bool>
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600189PrefixUpdateProcessor::afterWithdraw(const ndn::Name& prefix)
190{
Saurab Dulal7526cee2018-01-31 18:14:10 +0000191 return addOrDeletePrefix(prefix, false);
192}
193
alvy297f4162015-03-03 17:15:33 -0600194} // namespace update
195} // namespace nlsr