alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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/>. |
| 20 | **/ |
| 21 | |
| 22 | #include "prefix-update-processor.hpp" |
| 23 | |
| 24 | #include "lsdb.hpp" |
| 25 | #include "nlsr.hpp" |
| 26 | #include "prefix-update-commands.hpp" |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 27 | |
Junxiao Shi | 3e5120c | 2016-09-10 16:58:34 +0000 | [diff] [blame] | 28 | #include <ndn-cxx/mgmt/nfd/control-response.hpp> |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 29 | |
| 30 | namespace nlsr { |
| 31 | namespace update { |
| 32 | |
| 33 | INIT_LOGGER("PrefixUpdateProcessor"); |
| 34 | |
| 35 | const ndn::Name::Component PrefixUpdateProcessor::MODULE_COMPONENT = ndn::Name::Component("prefix-update"); |
| 36 | const ndn::Name::Component PrefixUpdateProcessor::ADVERTISE_VERB = ndn::Name::Component("advertise"); |
| 37 | const ndn::Name::Component PrefixUpdateProcessor::WITHDRAW_VERB = ndn::Name::Component("withdraw"); |
| 38 | |
| 39 | PrefixUpdateProcessor::PrefixUpdateProcessor(ndn::Face& face, |
| 40 | NamePrefixList& namePrefixList, |
| 41 | Lsdb& lsdb, |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 42 | const ndn::Name broadcastPrefix, |
| 43 | ndn::KeyChain& keyChain, |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 44 | std::shared_ptr<ndn::CertificateCacheTtl> certificateCache, |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 45 | security::CertificateStore& certStore) |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 46 | : m_face(face) |
| 47 | , m_namePrefixList(namePrefixList) |
| 48 | , m_lsdb(lsdb) |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 49 | , m_keyChain(keyChain) |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame] | 50 | , m_validator(m_face, broadcastPrefix, certificateCache, certStore) |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 51 | , m_isEnabled(false) |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 52 | , COMMAND_PREFIX(ndn::Name(Nlsr::LOCALHOST_PREFIX).append(MODULE_COMPONENT)) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | PrefixUpdateProcessor::startListening() |
| 58 | { |
| 59 | _LOG_DEBUG("Setting Interest filter for: " << COMMAND_PREFIX); |
| 60 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 61 | m_face.setInterestFilter(COMMAND_PREFIX, std::bind(&PrefixUpdateProcessor::onInterest, this, _2)); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void |
| 65 | PrefixUpdateProcessor::onInterest(const ndn::Interest& request) |
| 66 | { |
| 67 | _LOG_TRACE("Received Interest: " << request); |
| 68 | |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 69 | if (!m_isEnabled) { |
| 70 | sendNack(request); |
| 71 | return; |
| 72 | } |
| 73 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 74 | m_validator.validate(request, |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 75 | std::bind(&PrefixUpdateProcessor::onCommandValidated, this, _1), |
| 76 | std::bind(&PrefixUpdateProcessor::onCommandValidationFailed, this, _1, _2)); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void |
| 80 | PrefixUpdateProcessor::loadValidator(boost::property_tree::ptree section, |
| 81 | const std::string& filename) |
| 82 | { |
| 83 | m_validator.load(section, filename); |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | PrefixUpdateProcessor::onCommandValidated(const std::shared_ptr<const ndn::Interest>& request) |
| 88 | { |
| 89 | const ndn::Name& command = request->getName(); |
| 90 | const ndn::Name::Component& verb = command[COMMAND_PREFIX.size()]; |
| 91 | const ndn::Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1]; |
| 92 | |
| 93 | if (verb == ADVERTISE_VERB || verb == WITHDRAW_VERB) { |
| 94 | ndn::nfd::ControlParameters parameters; |
| 95 | |
| 96 | if (!extractParameters(parameterComponent, parameters)) { |
| 97 | sendResponse(request, 400, "Malformed command"); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | if (verb == ADVERTISE_VERB) { |
| 102 | advertise(request, parameters); |
| 103 | } |
| 104 | else if (verb == WITHDRAW_VERB) { |
| 105 | withdraw(request, parameters); |
| 106 | } |
| 107 | |
| 108 | sendResponse(request, 200, "Success"); |
| 109 | } |
| 110 | else { |
| 111 | sendResponse(request, 501, "Unsupported command"); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | PrefixUpdateProcessor::onCommandValidationFailed(const std::shared_ptr<const ndn::Interest>& request, |
| 117 | const std::string& failureInfo) |
| 118 | { |
| 119 | sendResponse(request, 403, failureInfo); |
| 120 | } |
| 121 | |
| 122 | bool |
| 123 | PrefixUpdateProcessor::extractParameters(const ndn::Name::Component& parameterComponent, |
| 124 | ndn::nfd::ControlParameters& extractedParameters) |
| 125 | { |
| 126 | try { |
| 127 | ndn::Block rawParameters = parameterComponent.blockFromValue(); |
| 128 | extractedParameters.wireDecode(rawParameters); |
| 129 | } |
| 130 | catch (const ndn::tlv::Error&) { |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | void |
| 138 | PrefixUpdateProcessor::advertise(const std::shared_ptr<const ndn::Interest>& request, |
| 139 | const ndn::nfd::ControlParameters& parameters) |
| 140 | { |
| 141 | AdvertisePrefixCommand command; |
| 142 | |
| 143 | if (!validateParameters(command, parameters)) { |
| 144 | sendResponse(request, 400, "Malformed command"); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | _LOG_INFO("Advertising name: " << parameters.getName()); |
| 149 | |
| 150 | if (m_namePrefixList.insert(parameters.getName())) { |
| 151 | // Only build a Name LSA if the added name is new |
| 152 | m_lsdb.buildAndInstallOwnNameLsa(); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | void |
| 157 | PrefixUpdateProcessor::withdraw(const std::shared_ptr<const ndn::Interest>& request, |
| 158 | const ndn::nfd::ControlParameters& parameters) |
| 159 | { |
| 160 | WithdrawPrefixCommand command; |
| 161 | |
| 162 | if (!validateParameters(command, parameters)) { |
| 163 | sendResponse(request, 400, "Malformed command"); |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | _LOG_INFO("Withdrawing name: " << parameters.getName()); |
| 168 | |
| 169 | if (m_namePrefixList.remove(parameters.getName())) { |
| 170 | // Only build a Name LSA if a name was actually removed |
| 171 | m_lsdb.buildAndInstallOwnNameLsa(); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | bool |
| 176 | PrefixUpdateProcessor::validateParameters(const ndn::nfd::ControlCommand& command, |
| 177 | const ndn::nfd::ControlParameters& parameters) |
| 178 | { |
| 179 | try { |
| 180 | command.validateRequest(parameters); |
| 181 | } |
| 182 | catch (const ndn::nfd::ControlCommand::ArgumentError&) { |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | void |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 190 | PrefixUpdateProcessor::sendNack(const ndn::Interest& request) |
| 191 | { |
| 192 | ndn::MetaInfo metaInfo; |
| 193 | metaInfo.setType(ndn::tlv::ContentType_Nack); |
| 194 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 195 | std::shared_ptr<ndn::Data> responseData = std::make_shared<ndn::Data>(request.getName()); |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 196 | responseData->setMetaInfo(metaInfo); |
| 197 | |
| 198 | m_keyChain.sign(*responseData); |
| 199 | m_face.put(*responseData); |
| 200 | } |
| 201 | |
| 202 | void |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 203 | PrefixUpdateProcessor::sendResponse(const std::shared_ptr<const ndn::Interest>& request, |
| 204 | uint32_t code, |
| 205 | const std::string& text) |
| 206 | { |
| 207 | if (request == nullptr) { |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | ndn::nfd::ControlResponse response(code, text); |
| 212 | const ndn::Block& encodedControl = response.wireEncode(); |
| 213 | |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 214 | std::shared_ptr<ndn::Data> responseData = std::make_shared<ndn::Data>(request->getName()); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 215 | responseData->setContent(encodedControl); |
| 216 | |
| 217 | m_keyChain.sign(*responseData); |
| 218 | m_face.put(*responseData); |
| 219 | } |
| 220 | |
| 221 | } // namespace update |
| 222 | } // namespace nlsr |