blob: 7ba0e92c908056d35ac05709ca54fd327ebd24ef [file] [log] [blame]
Jiewen Tana0497d82015-02-02 21:59:18 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, The University of Memphis,
Jiewen Tana0497d82015-02-02 21:59:18 -08004 * 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 G97e34942016-07-11 14:46:27 -050022/*! \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 Tana0497d82015-02-02 21:59:18 -080029#include "lsdb-dataset-interest-handler.hpp"
30
31#include "logger.hpp"
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050032#include "nlsr.hpp"
Jiewen Tana0497d82015-02-02 21:59:18 -080033
34#include <ndn-cxx/face.hpp>
Junxiao Shi3e5120c2016-09-10 16:58:34 +000035#include <ndn-cxx/mgmt/nfd/control-response.hpp>
Jiewen Tana0497d82015-02-02 21:59:18 -080036#include <ndn-cxx/util/regex.hpp>
37
38namespace nlsr {
39
40INIT_LOGGER("LsdbDatasetInterestHandler");
41
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050042const uint32_t LsdbDatasetInterestHandler::ERROR_CODE_MALFORMED_COMMAND = 400;
43const uint32_t LsdbDatasetInterestHandler::ERROR_CODE_UNSUPPORTED_COMMAND = 501;
44
Jiewen Tana0497d82015-02-02 21:59:18 -080045LsdbDatasetInterestHandler::LsdbDatasetInterestHandler(Lsdb& lsdb,
46 ndn::Face& face,
Jiewen Tana0497d82015-02-02 21:59:18 -080047 ndn::KeyChain& keyChain)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050048 : LOCALHOST_COMMAND_PREFIX(ndn::Name(Nlsr::LOCALHOST_PREFIX).append(Lsdb::NAME_COMPONENT))
Jiewen Tana0497d82015-02-02 21:59:18 -080049 , m_face(face)
50 , m_keyChain(keyChain)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050051 , m_adjacencyLsaPublisher(lsdb, face, keyChain)
52 , m_coordinateLsaPublisher(lsdb, face, keyChain)
53 , m_nameLsaPublisher(lsdb, face, keyChain)
54 , m_lsdbStatusPublisher(lsdb, face, keyChain,
Jiewen Tana0497d82015-02-02 21:59:18 -080055 m_adjacencyLsaPublisher,
56 m_coordinateLsaPublisher,
57 m_nameLsaPublisher)
58
59{
Jiewen Tana0497d82015-02-02 21:59:18 -080060}
61
62void
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050063LsdbDatasetInterestHandler::startListeningOnLocalhost()
Jiewen Tana0497d82015-02-02 21:59:18 -080064{
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050065 _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
71void
72LsdbDatasetInterestHandler::startListeningOnRouterPrefix()
73{
Muktadir R Chowdhury3ac07282016-06-17 16:30:29 -050074 _LOG_DEBUG("Setting interest filter for: " << m_routerNameCommandPrefix);
75 m_face.setInterestFilter(m_routerNameCommandPrefix,
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050076 std::bind(&LsdbDatasetInterestHandler::onInterest, this, _2,
Muktadir R Chowdhury3ac07282016-06-17 16:30:29 -050077 std::cref(m_routerNameCommandPrefix)));
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050078}
79
80void
81LsdbDatasetInterestHandler::onInterest(const ndn::Interest& interest,
82 const ndn::Name& commandPrefix)
83{
84 if (!isValidCommandPrefix(interest, commandPrefix))
Jiewen Tana0497d82015-02-02 21:59:18 -080085 {
86 _LOG_DEBUG("Received malformed interest: " << interest.getName());
87
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050088 sendErrorResponse(interest.getName(), ERROR_CODE_MALFORMED_COMMAND, "Malformed command");
Jiewen Tana0497d82015-02-02 21:59:18 -080089 return;
90 }
91
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050092 ndn::Name::Component command = interest.getName().get(commandPrefix.size());
93 processCommand(interest, command);
94}
95
96bool
97LsdbDatasetInterestHandler::isValidCommandPrefix(const ndn::Interest& interest,
98 const ndn::Name& commandPrefix)
99{
100 size_t commandSize = interest.getName().size();
101
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500102 return (commandSize == commandPrefix.size() + 1 && commandPrefix.isPrefixOf(interest.getName()));
103}
104
105void
106LsdbDatasetInterestHandler::processCommand(const ndn::Interest& interest,
107 const ndn::Name::Component& command)
108{
Jiewen Tana0497d82015-02-02 21:59:18 -0800109 _LOG_TRACE("Received interest with command: " << command);
110
111 if (command.equals(AdjacencyLsaPublisher::DATASET_COMPONENT)) {
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500112 m_adjacencyLsaPublisher.publish(interest.getName());
Jiewen Tana0497d82015-02-02 21:59:18 -0800113 }
114 else if (command.equals(CoordinateLsaPublisher::DATASET_COMPONENT)) {
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500115 m_coordinateLsaPublisher.publish(interest.getName());
Jiewen Tana0497d82015-02-02 21:59:18 -0800116 }
117 else if (command.equals(NameLsaPublisher::DATASET_COMPONENT)) {
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500118 m_nameLsaPublisher.publish(interest.getName());
Jiewen Tana0497d82015-02-02 21:59:18 -0800119 }
120 else if (command.equals(LsdbStatusPublisher::DATASET_COMPONENT)) {
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500121 m_lsdbStatusPublisher.publish(interest.getName());
Jiewen Tana0497d82015-02-02 21:59:18 -0800122 }
123 else {
124 _LOG_DEBUG("Unsupported command: " << command);
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500125 sendErrorResponse(interest.getName(), ERROR_CODE_UNSUPPORTED_COMMAND, "Unsupported command");
Jiewen Tana0497d82015-02-02 21:59:18 -0800126 }
127}
128
129void
130LsdbDatasetInterestHandler::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