blob: ad1f9a6e2936e0972acd75f19b1c6d41873353ba [file] [log] [blame]
alvy297f4162015-03-03 17:15:33 -06001/* -*- 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,
alvy297f4162015-03-03 17:15:33 -06004 * 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"
alvy297f4162015-03-03 17:15:33 -060027
Junxiao Shi3e5120c2016-09-10 16:58:34 +000028#include <ndn-cxx/mgmt/nfd/control-response.hpp>
alvy297f4162015-03-03 17:15:33 -060029
30namespace nlsr {
31namespace update {
32
33INIT_LOGGER("PrefixUpdateProcessor");
34
35const ndn::Name::Component PrefixUpdateProcessor::MODULE_COMPONENT = ndn::Name::Component("prefix-update");
36const ndn::Name::Component PrefixUpdateProcessor::ADVERTISE_VERB = ndn::Name::Component("advertise");
37const ndn::Name::Component PrefixUpdateProcessor::WITHDRAW_VERB = ndn::Name::Component("withdraw");
38
39PrefixUpdateProcessor::PrefixUpdateProcessor(ndn::Face& face,
40 NamePrefixList& namePrefixList,
41 Lsdb& lsdb,
alvy297f4162015-03-03 17:15:33 -060042 const ndn::Name broadcastPrefix,
43 ndn::KeyChain& keyChain,
dmcoomes9f936662017-03-02 10:33:09 -060044 std::shared_ptr<ndn::CertificateCacheTtl> certificateCache,
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050045 security::CertificateStore& certStore)
alvy297f4162015-03-03 17:15:33 -060046 : m_face(face)
47 , m_namePrefixList(namePrefixList)
48 , m_lsdb(lsdb)
alvy297f4162015-03-03 17:15:33 -060049 , m_keyChain(keyChain)
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050050 , m_validator(m_face, broadcastPrefix, certificateCache, certStore)
Vince Lehmand33e5bc2015-06-22 15:27:50 -050051 , m_isEnabled(false)
alvy297f4162015-03-03 17:15:33 -060052 , COMMAND_PREFIX(ndn::Name(Nlsr::LOCALHOST_PREFIX).append(MODULE_COMPONENT))
53{
54}
55
56void
57PrefixUpdateProcessor::startListening()
58{
59 _LOG_DEBUG("Setting Interest filter for: " << COMMAND_PREFIX);
60
dmcoomes9f936662017-03-02 10:33:09 -060061 m_face.setInterestFilter(COMMAND_PREFIX, std::bind(&PrefixUpdateProcessor::onInterest, this, _2));
alvy297f4162015-03-03 17:15:33 -060062}
63
64void
65PrefixUpdateProcessor::onInterest(const ndn::Interest& request)
66{
67 _LOG_TRACE("Received Interest: " << request);
68
Vince Lehmand33e5bc2015-06-22 15:27:50 -050069 if (!m_isEnabled) {
70 sendNack(request);
71 return;
72 }
73
alvy297f4162015-03-03 17:15:33 -060074 m_validator.validate(request,
dmcoomes9f936662017-03-02 10:33:09 -060075 std::bind(&PrefixUpdateProcessor::onCommandValidated, this, _1),
76 std::bind(&PrefixUpdateProcessor::onCommandValidationFailed, this, _1, _2));
alvy297f4162015-03-03 17:15:33 -060077}
78
79void
80PrefixUpdateProcessor::loadValidator(boost::property_tree::ptree section,
81 const std::string& filename)
82{
83 m_validator.load(section, filename);
84}
85
86void
87PrefixUpdateProcessor::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
115void
116PrefixUpdateProcessor::onCommandValidationFailed(const std::shared_ptr<const ndn::Interest>& request,
117 const std::string& failureInfo)
118{
119 sendResponse(request, 403, failureInfo);
120}
121
122bool
123PrefixUpdateProcessor::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
137void
138PrefixUpdateProcessor::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();
alvy297f4162015-03-03 17:15:33 -0600153 }
154}
155
156void
157PrefixUpdateProcessor::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();
alvy297f4162015-03-03 17:15:33 -0600172 }
173}
174
175bool
176PrefixUpdateProcessor::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
189void
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500190PrefixUpdateProcessor::sendNack(const ndn::Interest& request)
191{
192 ndn::MetaInfo metaInfo;
193 metaInfo.setType(ndn::tlv::ContentType_Nack);
194
dmcoomes9f936662017-03-02 10:33:09 -0600195 std::shared_ptr<ndn::Data> responseData = std::make_shared<ndn::Data>(request.getName());
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500196 responseData->setMetaInfo(metaInfo);
197
198 m_keyChain.sign(*responseData);
199 m_face.put(*responseData);
200}
201
202void
alvy297f4162015-03-03 17:15:33 -0600203PrefixUpdateProcessor::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 Lehmand33e5bc2015-06-22 15:27:50 -0500214 std::shared_ptr<ndn::Data> responseData = std::make_shared<ndn::Data>(request->getName());
alvy297f4162015-03-03 17:15:33 -0600215 responseData->setContent(encodedControl);
216
217 m_keyChain.sign(*responseData);
218 m_face.put(*responseData);
219}
220
221} // namespace update
222} // namespace nlsr