blob: 7b3fe897bce334cbb1029e0991108d41e873bce2 [file] [log] [blame]
alvy297f4162015-03-03 17:15:33 -06001/* -*- 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
31namespace nlsr {
32namespace update {
33
34INIT_LOGGER("PrefixUpdateProcessor");
35
36const ndn::Name::Component PrefixUpdateProcessor::MODULE_COMPONENT = ndn::Name::Component("prefix-update");
37const ndn::Name::Component PrefixUpdateProcessor::ADVERTISE_VERB = ndn::Name::Component("advertise");
38const ndn::Name::Component PrefixUpdateProcessor::WITHDRAW_VERB = ndn::Name::Component("withdraw");
39
40PrefixUpdateProcessor::PrefixUpdateProcessor(ndn::Face& face,
41 NamePrefixList& namePrefixList,
42 Lsdb& lsdb,
43 SyncLogicHandler& sync,
44 const ndn::Name broadcastPrefix,
45 ndn::KeyChain& keyChain,
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050046 ndn::shared_ptr<ndn::CertificateCacheTtl> certificateCache,
47 security::CertificateStore& certStore)
alvy297f4162015-03-03 17:15:33 -060048 : m_face(face)
49 , m_namePrefixList(namePrefixList)
50 , m_lsdb(lsdb)
51 , m_sync(sync)
52 , m_keyChain(keyChain)
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050053 , m_validator(m_face, broadcastPrefix, certificateCache, certStore)
Vince Lehmand33e5bc2015-06-22 15:27:50 -050054 , m_isEnabled(false)
alvy297f4162015-03-03 17:15:33 -060055 , COMMAND_PREFIX(ndn::Name(Nlsr::LOCALHOST_PREFIX).append(MODULE_COMPONENT))
56{
57}
58
59void
60PrefixUpdateProcessor::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
67void
68PrefixUpdateProcessor::onInterest(const ndn::Interest& request)
69{
70 _LOG_TRACE("Received Interest: " << request);
71
Vince Lehmand33e5bc2015-06-22 15:27:50 -050072 if (!m_isEnabled) {
73 sendNack(request);
74 return;
75 }
76
alvy297f4162015-03-03 17:15:33 -060077 m_validator.validate(request,
78 bind(&PrefixUpdateProcessor::onCommandValidated, this, _1),
79 bind(&PrefixUpdateProcessor::onCommandValidationFailed, this, _1, _2));
80}
81
82void
83PrefixUpdateProcessor::loadValidator(boost::property_tree::ptree section,
84 const std::string& filename)
85{
86 m_validator.load(section, filename);
87}
88
89void
90PrefixUpdateProcessor::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
118void
119PrefixUpdateProcessor::onCommandValidationFailed(const std::shared_ptr<const ndn::Interest>& request,
120 const std::string& failureInfo)
121{
122 sendResponse(request, 403, failureInfo);
123}
124
125bool
126PrefixUpdateProcessor::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
140void
141PrefixUpdateProcessor::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
160void
161PrefixUpdateProcessor::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
180bool
181PrefixUpdateProcessor::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
194void
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500195PrefixUpdateProcessor::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
207void
alvy297f4162015-03-03 17:15:33 -0600208PrefixUpdateProcessor::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 Lehmand33e5bc2015-06-22 15:27:50 -0500219 std::shared_ptr<ndn::Data> responseData = std::make_shared<ndn::Data>(request->getName());
alvy297f4162015-03-03 17:15:33 -0600220 responseData->setContent(encodedControl);
221
222 m_keyChain.sign(*responseData);
223 m_face.put(*responseData);
224}
225
226} // namespace update
227} // namespace nlsr