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