alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, The University of Memphis, |
| 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 | |
| 29 | #include <ndn-cxx/management/nfd-control-response.hpp> |
| 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) |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 54 | , COMMAND_PREFIX(ndn::Name(Nlsr::LOCALHOST_PREFIX).append(MODULE_COMPONENT)) |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | PrefixUpdateProcessor::startListening() |
| 60 | { |
| 61 | _LOG_DEBUG("Setting Interest filter for: " << COMMAND_PREFIX); |
| 62 | |
| 63 | m_face.setInterestFilter(COMMAND_PREFIX, bind(&PrefixUpdateProcessor::onInterest, this, _2)); |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | PrefixUpdateProcessor::onInterest(const ndn::Interest& request) |
| 68 | { |
| 69 | _LOG_TRACE("Received Interest: " << request); |
| 70 | |
| 71 | m_validator.validate(request, |
| 72 | bind(&PrefixUpdateProcessor::onCommandValidated, this, _1), |
| 73 | bind(&PrefixUpdateProcessor::onCommandValidationFailed, this, _1, _2)); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | PrefixUpdateProcessor::loadValidator(boost::property_tree::ptree section, |
| 78 | const std::string& filename) |
| 79 | { |
| 80 | m_validator.load(section, filename); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | PrefixUpdateProcessor::onCommandValidated(const std::shared_ptr<const ndn::Interest>& request) |
| 85 | { |
| 86 | const ndn::Name& command = request->getName(); |
| 87 | const ndn::Name::Component& verb = command[COMMAND_PREFIX.size()]; |
| 88 | const ndn::Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1]; |
| 89 | |
| 90 | if (verb == ADVERTISE_VERB || verb == WITHDRAW_VERB) { |
| 91 | ndn::nfd::ControlParameters parameters; |
| 92 | |
| 93 | if (!extractParameters(parameterComponent, parameters)) { |
| 94 | sendResponse(request, 400, "Malformed command"); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | if (verb == ADVERTISE_VERB) { |
| 99 | advertise(request, parameters); |
| 100 | } |
| 101 | else if (verb == WITHDRAW_VERB) { |
| 102 | withdraw(request, parameters); |
| 103 | } |
| 104 | |
| 105 | sendResponse(request, 200, "Success"); |
| 106 | } |
| 107 | else { |
| 108 | sendResponse(request, 501, "Unsupported command"); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | PrefixUpdateProcessor::onCommandValidationFailed(const std::shared_ptr<const ndn::Interest>& request, |
| 114 | const std::string& failureInfo) |
| 115 | { |
| 116 | sendResponse(request, 403, failureInfo); |
| 117 | } |
| 118 | |
| 119 | bool |
| 120 | PrefixUpdateProcessor::extractParameters(const ndn::Name::Component& parameterComponent, |
| 121 | ndn::nfd::ControlParameters& extractedParameters) |
| 122 | { |
| 123 | try { |
| 124 | ndn::Block rawParameters = parameterComponent.blockFromValue(); |
| 125 | extractedParameters.wireDecode(rawParameters); |
| 126 | } |
| 127 | catch (const ndn::tlv::Error&) { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | PrefixUpdateProcessor::advertise(const std::shared_ptr<const ndn::Interest>& request, |
| 136 | const ndn::nfd::ControlParameters& parameters) |
| 137 | { |
| 138 | AdvertisePrefixCommand command; |
| 139 | |
| 140 | if (!validateParameters(command, parameters)) { |
| 141 | sendResponse(request, 400, "Malformed command"); |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | _LOG_INFO("Advertising name: " << parameters.getName()); |
| 146 | |
| 147 | if (m_namePrefixList.insert(parameters.getName())) { |
| 148 | // Only build a Name LSA if the added name is new |
| 149 | m_lsdb.buildAndInstallOwnNameLsa(); |
| 150 | m_sync.publishRoutingUpdate(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | PrefixUpdateProcessor::withdraw(const std::shared_ptr<const ndn::Interest>& request, |
| 156 | const ndn::nfd::ControlParameters& parameters) |
| 157 | { |
| 158 | WithdrawPrefixCommand command; |
| 159 | |
| 160 | if (!validateParameters(command, parameters)) { |
| 161 | sendResponse(request, 400, "Malformed command"); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | _LOG_INFO("Withdrawing name: " << parameters.getName()); |
| 166 | |
| 167 | if (m_namePrefixList.remove(parameters.getName())) { |
| 168 | // Only build a Name LSA if a name was actually removed |
| 169 | m_lsdb.buildAndInstallOwnNameLsa(); |
| 170 | m_sync.publishRoutingUpdate(); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | bool |
| 175 | PrefixUpdateProcessor::validateParameters(const ndn::nfd::ControlCommand& command, |
| 176 | const ndn::nfd::ControlParameters& parameters) |
| 177 | { |
| 178 | try { |
| 179 | command.validateRequest(parameters); |
| 180 | } |
| 181 | catch (const ndn::nfd::ControlCommand::ArgumentError&) { |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | void |
| 189 | PrefixUpdateProcessor::sendResponse(const std::shared_ptr<const ndn::Interest>& request, |
| 190 | uint32_t code, |
| 191 | const std::string& text) |
| 192 | { |
| 193 | if (request == nullptr) { |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | ndn::nfd::ControlResponse response(code, text); |
| 198 | const ndn::Block& encodedControl = response.wireEncode(); |
| 199 | |
| 200 | std::shared_ptr<ndn::Data> responseData = ndn::make_shared<ndn::Data>(request->getName()); |
| 201 | responseData->setContent(encodedControl); |
| 202 | |
| 203 | m_keyChain.sign(*responseData); |
| 204 | m_face.put(*responseData); |
| 205 | } |
| 206 | |
| 207 | } // namespace update |
| 208 | } // namespace nlsr |