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