Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [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, |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [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 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 22 | /*! \file lsdb-dataset-interest-handler.cpp |
| 23 | |
| 24 | This file details a class that is used by NLSRC and other command-line |
| 25 | tools to examine the state of NLSR. This system is not designed to |
| 26 | be used by routers to publish data to each other. |
| 27 | */ |
| 28 | |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 29 | #include "lsdb-dataset-interest-handler.hpp" |
| 30 | |
| 31 | #include "logger.hpp" |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 32 | #include "nlsr.hpp" |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 33 | |
| 34 | #include <ndn-cxx/face.hpp> |
Junxiao Shi | 3e5120c | 2016-09-10 16:58:34 +0000 | [diff] [blame] | 35 | #include <ndn-cxx/mgmt/nfd/control-response.hpp> |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 36 | #include <ndn-cxx/util/regex.hpp> |
| 37 | |
| 38 | namespace nlsr { |
| 39 | |
| 40 | INIT_LOGGER("LsdbDatasetInterestHandler"); |
| 41 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 42 | const uint32_t LsdbDatasetInterestHandler::ERROR_CODE_MALFORMED_COMMAND = 400; |
| 43 | const uint32_t LsdbDatasetInterestHandler::ERROR_CODE_UNSUPPORTED_COMMAND = 501; |
| 44 | |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 45 | LsdbDatasetInterestHandler::LsdbDatasetInterestHandler(Lsdb& lsdb, |
| 46 | ndn::Face& face, |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 47 | ndn::KeyChain& keyChain) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 48 | : LOCALHOST_COMMAND_PREFIX(ndn::Name(Nlsr::LOCALHOST_PREFIX).append(Lsdb::NAME_COMPONENT)) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 49 | , m_face(face) |
| 50 | , m_keyChain(keyChain) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 51 | , m_adjacencyLsaPublisher(lsdb, face, keyChain) |
| 52 | , m_coordinateLsaPublisher(lsdb, face, keyChain) |
| 53 | , m_nameLsaPublisher(lsdb, face, keyChain) |
| 54 | , m_lsdbStatusPublisher(lsdb, face, keyChain, |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 55 | m_adjacencyLsaPublisher, |
| 56 | m_coordinateLsaPublisher, |
| 57 | m_nameLsaPublisher) |
| 58 | |
| 59 | { |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 63 | LsdbDatasetInterestHandler::startListeningOnLocalhost() |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 64 | { |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 65 | _LOG_DEBUG("Setting interest filter for: " << LOCALHOST_COMMAND_PREFIX); |
| 66 | m_face.setInterestFilter(LOCALHOST_COMMAND_PREFIX, |
| 67 | std::bind(&LsdbDatasetInterestHandler::onInterest, this, _2, |
| 68 | std::cref(LOCALHOST_COMMAND_PREFIX))); |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | LsdbDatasetInterestHandler::startListeningOnRouterPrefix() |
| 73 | { |
Muktadir R Chowdhury | 3ac0728 | 2016-06-17 16:30:29 -0500 | [diff] [blame] | 74 | _LOG_DEBUG("Setting interest filter for: " << m_routerNameCommandPrefix); |
| 75 | m_face.setInterestFilter(m_routerNameCommandPrefix, |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 76 | std::bind(&LsdbDatasetInterestHandler::onInterest, this, _2, |
Muktadir R Chowdhury | 3ac0728 | 2016-06-17 16:30:29 -0500 | [diff] [blame] | 77 | std::cref(m_routerNameCommandPrefix))); |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void |
| 81 | LsdbDatasetInterestHandler::onInterest(const ndn::Interest& interest, |
| 82 | const ndn::Name& commandPrefix) |
| 83 | { |
| 84 | if (!isValidCommandPrefix(interest, commandPrefix)) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 85 | { |
| 86 | _LOG_DEBUG("Received malformed interest: " << interest.getName()); |
| 87 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 88 | sendErrorResponse(interest.getName(), ERROR_CODE_MALFORMED_COMMAND, "Malformed command"); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 89 | return; |
| 90 | } |
| 91 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 92 | ndn::Name::Component command = interest.getName().get(commandPrefix.size()); |
| 93 | processCommand(interest, command); |
| 94 | } |
| 95 | |
| 96 | bool |
| 97 | LsdbDatasetInterestHandler::isValidCommandPrefix(const ndn::Interest& interest, |
| 98 | const ndn::Name& commandPrefix) |
| 99 | { |
| 100 | size_t commandSize = interest.getName().size(); |
| 101 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 102 | return (commandSize == commandPrefix.size() + 1 && commandPrefix.isPrefixOf(interest.getName())); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | LsdbDatasetInterestHandler::processCommand(const ndn::Interest& interest, |
| 107 | const ndn::Name::Component& command) |
| 108 | { |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 109 | _LOG_TRACE("Received interest with command: " << command); |
| 110 | |
| 111 | if (command.equals(AdjacencyLsaPublisher::DATASET_COMPONENT)) { |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 112 | m_adjacencyLsaPublisher.publish(interest.getName()); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 113 | } |
| 114 | else if (command.equals(CoordinateLsaPublisher::DATASET_COMPONENT)) { |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 115 | m_coordinateLsaPublisher.publish(interest.getName()); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 116 | } |
| 117 | else if (command.equals(NameLsaPublisher::DATASET_COMPONENT)) { |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 118 | m_nameLsaPublisher.publish(interest.getName()); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 119 | } |
| 120 | else if (command.equals(LsdbStatusPublisher::DATASET_COMPONENT)) { |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 121 | m_lsdbStatusPublisher.publish(interest.getName()); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 122 | } |
| 123 | else { |
| 124 | _LOG_DEBUG("Unsupported command: " << command); |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 125 | sendErrorResponse(interest.getName(), ERROR_CODE_UNSUPPORTED_COMMAND, "Unsupported command"); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | LsdbDatasetInterestHandler::sendErrorResponse(const ndn::Name& name, |
| 131 | uint32_t code, |
| 132 | const std::string& error) |
| 133 | { |
| 134 | ndn::nfd::ControlResponse response(code, error); |
| 135 | |
| 136 | std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(name); |
| 137 | data->setContent(response.wireEncode()); |
| 138 | |
| 139 | m_keyChain.sign(*data); |
| 140 | m_face.put(*data); |
| 141 | } |
| 142 | |
| 143 | } // namespace nlsr |