alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 384327d | 2025-01-02 01:40:23 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2025, The University of Memphis, |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 4 | * 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 Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 20 | */ |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 21 | |
| 22 | #include "prefix-update-processor.hpp" |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 23 | #include "logger.hpp" |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 24 | #include "lsdb.hpp" |
| 25 | #include "nlsr.hpp" |
Davide Pesavento | 384327d | 2025-01-02 01:40:23 -0500 | [diff] [blame] | 26 | #include "prefix-update-commands.hpp" |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 27 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 28 | #include <ndn-cxx/face.hpp> |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 29 | #include <ndn-cxx/mgmt/nfd/control-response.hpp> |
| 30 | |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 31 | #include <boost/algorithm/string.hpp> |
Davide Pesavento | 7bc3d43 | 2021-10-25 21:08:04 -0400 | [diff] [blame] | 32 | #include <fstream> |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 33 | |
Davide Pesavento | 384327d | 2025-01-02 01:40:23 -0500 | [diff] [blame] | 34 | namespace nlsr::update { |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 35 | |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 36 | INIT_LOGGER(update.PrefixUpdateProcessor); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 37 | |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 38 | /** \brief an Interest tag to indicate command signer |
| 39 | */ |
| 40 | using SignerTag = ndn::SimpleTag<ndn::Name, 20>; |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 41 | |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 42 | /** \brief obtain signer from SignerTag attached to Interest, if available |
| 43 | */ |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 44 | static std::optional<std::string> |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 45 | getSignerFromTag(const ndn::Interest& interest) |
| 46 | { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 47 | auto signerTag = interest.getTag<SignerTag>(); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 48 | if (signerTag == nullptr) { |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 49 | return std::nullopt; |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 50 | } |
| 51 | else { |
| 52 | return signerTag->get().toUri(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | PrefixUpdateProcessor::PrefixUpdateProcessor(ndn::mgmt::Dispatcher& dispatcher, |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 57 | ndn::security::ValidatorConfig& validator, |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 58 | NamePrefixList& namePrefixList, |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 59 | Lsdb& lsdb, const std::string& configFileName) |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 60 | : CommandManagerBase(dispatcher, namePrefixList, lsdb, "prefix-update") |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 61 | , m_validator(validator) |
dulalsaurab | 82a34c2 | 2019-02-04 17:31:21 +0000 | [diff] [blame] | 62 | , m_confFileNameDynamic(configFileName) |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 63 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 64 | NLSR_LOG_DEBUG("Setting dispatcher to capture Interests for: " |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 65 | << ndn::Name(Nlsr::LOCALHOST_PREFIX).append("prefix-update")); |
| 66 | |
| 67 | m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(makeRelPrefix("advertise"), |
| 68 | makeAuthorization(), |
Davide Pesavento | 384327d | 2025-01-02 01:40:23 -0500 | [diff] [blame] | 69 | [] (const auto& p) { return validateParameters<AdvertisePrefixCommand>(p); }, |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 70 | std::bind(&PrefixUpdateProcessor::advertiseAndInsertPrefix, this, _1, _2, _3, _4)); |
| 71 | |
| 72 | m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(makeRelPrefix("withdraw"), |
| 73 | makeAuthorization(), |
Davide Pesavento | 384327d | 2025-01-02 01:40:23 -0500 | [diff] [blame] | 74 | [] (const auto& p) { return validateParameters<WithdrawPrefixCommand>(p); }, |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 75 | std::bind(&PrefixUpdateProcessor::withdrawAndRemovePrefix, this, _1, _2, _3, _4)); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 76 | } |
| 77 | |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 78 | ndn::mgmt::Authorization |
| 79 | PrefixUpdateProcessor::makeAuthorization() |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 80 | { |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 81 | return [=] (const ndn::Name& prefix, const ndn::Interest& interest, |
Davide Pesavento | d2610dc | 2025-01-03 13:20:06 -0500 | [diff] [blame] | 82 | const ndn::mgmt::ControlParametersBase* params, |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 83 | const ndn::mgmt::AcceptContinuation& accept, |
| 84 | const ndn::mgmt::RejectContinuation& reject) { |
| 85 | m_validator.validate(interest, |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 86 | [accept] (const ndn::Interest& request) { |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 87 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 88 | auto signer1 = getSignerFromTag(request); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 89 | std::string signer = signer1.value_or("*"); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 90 | NLSR_LOG_DEBUG("accept " << request.getName() << " signer=" << signer); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 91 | accept(signer); |
| 92 | }, |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 93 | [reject] (const ndn::Interest& request, const ndn::security::ValidationError& error) { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 94 | NLSR_LOG_DEBUG("reject " << request.getName() << " signer=" << |
| 95 | getSignerFromTag(request).value_or("?") << ' ' << error); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 96 | reject(ndn::mgmt::RejectReply::STATUS403); |
| 97 | }); |
| 98 | }; |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void |
| 102 | PrefixUpdateProcessor::loadValidator(boost::property_tree::ptree section, |
| 103 | const std::string& filename) |
| 104 | { |
| 105 | m_validator.load(section, filename); |
| 106 | } |
| 107 | |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 108 | bool |
| 109 | PrefixUpdateProcessor::checkForPrefixInFile(const std::string prefix) |
| 110 | { |
| 111 | std::string line; |
dulalsaurab | 82a34c2 | 2019-02-04 17:31:21 +0000 | [diff] [blame] | 112 | std::fstream fp(m_confFileNameDynamic); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 113 | if (!fp.good() || !fp.is_open()) { |
| 114 | NLSR_LOG_ERROR("Failed to open configuration file for parsing"); |
| 115 | return true; |
| 116 | } |
| 117 | while (!fp.eof()) { |
| 118 | getline(fp, line); |
| 119 | if (line == prefix) { |
| 120 | return true; |
| 121 | } |
| 122 | } |
| 123 | fp.close(); |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | bool |
| 128 | PrefixUpdateProcessor::addOrDeletePrefix(const ndn::Name& prefix, bool addPrefix) |
| 129 | { |
Ashlesh Gawande | 08bce9c | 2019-04-05 11:08:07 -0500 | [diff] [blame] | 130 | std::string value = " prefix " + prefix.toUri(); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 131 | std::string fileString; |
| 132 | std::string line; |
| 133 | std::string trimedLine; |
dulalsaurab | 82a34c2 | 2019-02-04 17:31:21 +0000 | [diff] [blame] | 134 | std::fstream input(m_confFileNameDynamic, input.in); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 135 | if (!input.good() || !input.is_open()) { |
| 136 | NLSR_LOG_ERROR("Failed to open configuration file for parsing"); |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | if (addPrefix) { |
| 141 | //check if prefix already exist in the nlsr configuration file |
| 142 | if (checkForPrefixInFile(value)) { |
| 143 | NLSR_LOG_ERROR("Prefix already exists in the configuration file"); |
| 144 | return false; |
| 145 | } |
| 146 | while (!input.eof()) { |
| 147 | getline(input, line); |
| 148 | if (!line.empty()) { |
| 149 | fileString.append(line + "\n"); |
| 150 | if (line == "advertising") { |
| 151 | getline(input, line); |
| 152 | fileString.append(line + "\n" + value + "\n"); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | else { |
| 158 | if (!checkForPrefixInFile(value)) { |
| 159 | NLSR_LOG_ERROR("Prefix doesn't exists in the configuration file"); |
| 160 | return false; |
| 161 | } |
| 162 | boost::trim(value); |
| 163 | while (!input.eof()) { |
| 164 | getline(input, line); |
| 165 | if (!line.empty()) { |
| 166 | std::string trimLine = line; |
| 167 | boost::trim(trimLine); |
| 168 | if (trimLine != value) { |
| 169 | fileString.append(line + "\n"); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | input.close(); |
dulalsaurab | 82a34c2 | 2019-02-04 17:31:21 +0000 | [diff] [blame] | 175 | std::ofstream output(m_confFileNameDynamic); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 176 | output << fileString; |
| 177 | output.close(); |
| 178 | return true; |
| 179 | } |
| 180 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 181 | std::optional<bool> |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 182 | PrefixUpdateProcessor::afterAdvertise(const ndn::Name& prefix) |
| 183 | { |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 184 | return addOrDeletePrefix(prefix, true); |
| 185 | } |
| 186 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 187 | std::optional<bool> |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 188 | PrefixUpdateProcessor::afterWithdraw(const ndn::Name& prefix) |
| 189 | { |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 190 | return addOrDeletePrefix(prefix, false); |
| 191 | } |
| 192 | |
Davide Pesavento | 384327d | 2025-01-02 01:40:23 -0500 | [diff] [blame] | 193 | } // namespace nlsr::update |