blob: 956fc7bec1204120e731b0bf9deea2d7f24e7512 [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/*
3 * Copyright (c) 2014-2020, 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"
Junxiao Shi3e5120c2016-09-10 16:58:34 +000025#include <ndn-cxx/mgmt/nfd/control-response.hpp>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050026#include <ndn-cxx/face.hpp>
Saurab Dulal7526cee2018-01-31 18:14:10 +000027#include <boost/algorithm/string.hpp>
28#include <algorithm>
alvy297f4162015-03-03 17:15:33 -060029
30namespace nlsr {
31namespace update {
32
dmcoomescf8d0ed2017-02-21 11:39:01 -060033INIT_LOGGER(update.PrefixUpdateProcessor);
alvy297f4162015-03-03 17:15:33 -060034
Laqin Fan54a43f02017-03-08 12:31:30 -060035/** \brief an Interest tag to indicate command signer
36 */
37using SignerTag = ndn::SimpleTag<ndn::Name, 20>;
alvy297f4162015-03-03 17:15:33 -060038
Laqin Fan54a43f02017-03-08 12:31:30 -060039/** \brief obtain signer from SignerTag attached to Interest, if available
40 */
41static ndn::optional<std::string>
42getSignerFromTag(const ndn::Interest& interest)
43{
44 shared_ptr<SignerTag> signerTag = interest.getTag<SignerTag>();
45 if (signerTag == nullptr) {
46 return ndn::nullopt;
47 }
48 else {
49 return signerTag->get().toUri();
50 }
51}
52
53PrefixUpdateProcessor::PrefixUpdateProcessor(ndn::mgmt::Dispatcher& dispatcher,
Ashlesh Gawande85998a12017-12-07 22:22:13 -060054 ndn::security::ValidatorConfig& validator,
alvy297f4162015-03-03 17:15:33 -060055 NamePrefixList& namePrefixList,
Saurab Dulal7526cee2018-01-31 18:14:10 +000056 Lsdb& lsdb, const std::string& configFileName)
Laqin Fan54a43f02017-03-08 12:31:30 -060057 : CommandManagerBase(dispatcher, namePrefixList, lsdb, "prefix-update")
Ashlesh Gawande85998a12017-12-07 22:22:13 -060058 , m_validator(validator)
dulalsaurab82a34c22019-02-04 17:31:21 +000059 , m_confFileNameDynamic(configFileName)
alvy297f4162015-03-03 17:15:33 -060060{
dmcoomes5bcb39e2017-10-31 15:07:55 -050061 NLSR_LOG_DEBUG("Setting dispatcher to capture Interests for: "
Laqin Fan54a43f02017-03-08 12:31:30 -060062 << ndn::Name(Nlsr::LOCALHOST_PREFIX).append("prefix-update"));
63
64 m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(makeRelPrefix("advertise"),
65 makeAuthorization(),
66 std::bind(&PrefixUpdateProcessor::validateParameters<AdvertisePrefixCommand>,
67 this, _1),
68 std::bind(&PrefixUpdateProcessor::advertiseAndInsertPrefix, this, _1, _2, _3, _4));
69
70 m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(makeRelPrefix("withdraw"),
71 makeAuthorization(),
72 std::bind(&PrefixUpdateProcessor::validateParameters<WithdrawPrefixCommand>,
73 this, _1),
74 std::bind(&PrefixUpdateProcessor::withdrawAndRemovePrefix, this, _1, _2, _3, _4));
alvy297f4162015-03-03 17:15:33 -060075}
76
Laqin Fan54a43f02017-03-08 12:31:30 -060077ndn::mgmt::Authorization
78PrefixUpdateProcessor::makeAuthorization()
alvy297f4162015-03-03 17:15:33 -060079{
Laqin Fan54a43f02017-03-08 12:31:30 -060080 return [=] (const ndn::Name& prefix, const ndn::Interest& interest,
81 const ndn::mgmt::ControlParameters* params,
82 const ndn::mgmt::AcceptContinuation& accept,
83 const ndn::mgmt::RejectContinuation& reject) {
84 m_validator.validate(interest,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050085 [accept] (const ndn::Interest& request) {
alvy297f4162015-03-03 17:15:33 -060086
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050087 auto signer1 = getSignerFromTag(request);
Laqin Fan54a43f02017-03-08 12:31:30 -060088 std::string signer = signer1.value_or("*");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050089 NLSR_LOG_DEBUG("accept " << request.getName() << " signer=" << signer);
Laqin Fan54a43f02017-03-08 12:31:30 -060090 accept(signer);
91 },
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040092 [reject] (const ndn::Interest& request, const ndn::security::ValidationError& error) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050093 NLSR_LOG_DEBUG("reject " << request.getName() << " signer=" <<
94 getSignerFromTag(request).value_or("?") << ' ' << error);
Laqin Fan54a43f02017-03-08 12:31:30 -060095 reject(ndn::mgmt::RejectReply::STATUS403);
96 });
97 };
alvy297f4162015-03-03 17:15:33 -060098}
99
100void
101PrefixUpdateProcessor::loadValidator(boost::property_tree::ptree section,
102 const std::string& filename)
103{
104 m_validator.load(section, filename);
105}
106
Saurab Dulal7526cee2018-01-31 18:14:10 +0000107bool
108PrefixUpdateProcessor::checkForPrefixInFile(const std::string prefix)
109{
110 std::string line;
dulalsaurab82a34c22019-02-04 17:31:21 +0000111 std::fstream fp(m_confFileNameDynamic);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000112 if (!fp.good() || !fp.is_open()) {
113 NLSR_LOG_ERROR("Failed to open configuration file for parsing");
114 return true;
115 }
116 while (!fp.eof()) {
117 getline(fp, line);
118 if (line == prefix) {
119 return true;
120 }
121 }
122 fp.close();
123 return false;
124}
125
126bool
127PrefixUpdateProcessor::addOrDeletePrefix(const ndn::Name& prefix, bool addPrefix)
128{
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500129 std::string value = " prefix " + prefix.toUri();
Saurab Dulal7526cee2018-01-31 18:14:10 +0000130 std::string fileString;
131 std::string line;
132 std::string trimedLine;
dulalsaurab82a34c22019-02-04 17:31:21 +0000133 std::fstream input(m_confFileNameDynamic, input.in);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000134 if (!input.good() || !input.is_open()) {
135 NLSR_LOG_ERROR("Failed to open configuration file for parsing");
136 return false;
137 }
138
139 if (addPrefix) {
140 //check if prefix already exist in the nlsr configuration file
141 if (checkForPrefixInFile(value)) {
142 NLSR_LOG_ERROR("Prefix already exists in the configuration file");
143 return false;
144 }
145 while (!input.eof()) {
146 getline(input, line);
147 if (!line.empty()) {
148 fileString.append(line + "\n");
149 if (line == "advertising") {
150 getline(input, line);
151 fileString.append(line + "\n" + value + "\n");
152 }
153 }
154 }
155 }
156 else {
157 if (!checkForPrefixInFile(value)) {
158 NLSR_LOG_ERROR("Prefix doesn't exists in the configuration file");
159 return false;
160 }
161 boost::trim(value);
162 while (!input.eof()) {
163 getline(input, line);
164 if (!line.empty()) {
165 std::string trimLine = line;
166 boost::trim(trimLine);
167 if (trimLine != value) {
168 fileString.append(line + "\n");
169 }
170 }
171 }
172 }
173 input.close();
dulalsaurab82a34c22019-02-04 17:31:21 +0000174 std::ofstream output(m_confFileNameDynamic);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000175 output << fileString;
176 output.close();
177 return true;
178}
179
180ndn::optional<bool>
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600181PrefixUpdateProcessor::afterAdvertise(const ndn::Name& prefix)
182{
Saurab Dulal7526cee2018-01-31 18:14:10 +0000183 return addOrDeletePrefix(prefix, true);
184}
185
186ndn::optional<bool>
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600187PrefixUpdateProcessor::afterWithdraw(const ndn::Name& prefix)
188{
Saurab Dulal7526cee2018-01-31 18:14:10 +0000189 return addOrDeletePrefix(prefix, false);
190}
191
alvy297f4162015-03-03 17:15:33 -0600192} // namespace update
193} // namespace nlsr