blob: d5f26f2d51acd787108b024d320e696faec05033 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2014 Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
* Washington University in St. Louis,
* Beijing Institute of Technology,
* The University of Memphis
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
*
* NFD is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
**/
#include "rib-manager.hpp"
#include "core/logger.hpp"
namespace nfd {
namespace rib {
NFD_LOG_INIT("RibManager");
const Name RibManager::COMMAND_PREFIX = "/localhost/nrd";
const Name RibManager::REMOTE_COMMAND_PREFIX = "/localhop/nrd";
const size_t RibManager::COMMAND_UNSIGNED_NCOMPS =
RibManager::COMMAND_PREFIX.size() +
1 + // verb
1; // verb options
const size_t RibManager::COMMAND_SIGNED_NCOMPS =
RibManager::COMMAND_UNSIGNED_NCOMPS +
4; // (timestamp, nonce, signed info tlv, signature tlv)
const RibManager::VerbAndProcessor RibManager::COMMAND_VERBS[] =
{
VerbAndProcessor(
Name::Component("register"),
&RibManager::insertEntry
),
VerbAndProcessor(
Name::Component("unregister"),
&RibManager::deleteEntry
),
};
RibManager::RibManager()
: m_face(new ndn::Face())
, m_nfdController(new ndn::nfd::Controller(*m_face))
, m_validator(m_face)
, m_faceMonitor(*m_face)
, m_verbDispatch(COMMAND_VERBS,
COMMAND_VERBS + (sizeof(COMMAND_VERBS) / sizeof(VerbAndProcessor)))
{
}
void
RibManager::registerWithNfd()
{
//check whether the components of localhop and localhost prefixes are same
BOOST_ASSERT(COMMAND_PREFIX.size() == REMOTE_COMMAND_PREFIX.size());
NFD_LOG_INFO("Setting interest filter on: " << COMMAND_PREFIX.toUri());
m_face->setController(m_nfdController);
m_face->setInterestFilter(COMMAND_PREFIX.toUri(),
bind(&RibManager::onRibRequest, this, _2),
bind(&RibManager::setInterestFilterFailed, this, _1, _2));
NFD_LOG_INFO("Setting interest filter on: " << REMOTE_COMMAND_PREFIX.toUri());
m_face->setInterestFilter(REMOTE_COMMAND_PREFIX.toUri(),
bind(&RibManager::onRibRequest, this, _2),
bind(&RibManager::setInterestFilterFailed, this, _1, _2));
NFD_LOG_INFO("Start monitoring face create/destroy events");
m_faceMonitor.addSubscriber(boost::bind(&RibManager::onNotification, this, _1));
m_faceMonitor.startNotifications();
}
void
RibManager::setConfigFile(ConfigFile& configFile)
{
configFile.addSectionHandler("security",
bind(&RibManager::onConfig, this, _1, _2, _3));
}
void
RibManager::onConfig(const ConfigSection& configSection,
bool isDryRun,
const std::string& filename)
{
/// \todo remove check after validator-conf replaces settings on each load
if (!isDryRun)
m_validator.load(configSection, filename);
}
void
RibManager::setInterestFilterFailed(const Name& name, const std::string& msg)
{
NFD_LOG_ERROR("Error in setting interest filter (" << name << "): " << msg);
m_face->shutdown();
}
void
RibManager::sendResponse(const Name& name,
const ControlResponse& response)
{
const Block& encodedControl = response.wireEncode();
Data responseData(name);
responseData.setContent(encodedControl);
m_keyChain.sign(responseData);
m_face->put(responseData);
}
void
RibManager::sendResponse(const Name& name,
uint32_t code,
const std::string& text)
{
ControlResponse response(code, text);
sendResponse(name, response);
}
void
RibManager::onRibRequest(const Interest& request)
{
m_validator.validate(request,
bind(&RibManager::onRibRequestValidated, this, _1),
bind(&RibManager::onRibRequestValidationFailed, this, _1, _2));
}
void
RibManager::onRibRequestValidated(const shared_ptr<const Interest>& request)
{
const Name& command = request->getName();
//REMOTE_COMMAND_PREFIX number of componenets are same as
// NRD_COMMAND_PREFIX's so no extra checks are required.
const Name::Component& verb = command.get(COMMAND_PREFIX.size());
VerbDispatchTable::const_iterator verbProcessor = m_verbDispatch.find(verb);
if (verbProcessor != m_verbDispatch.end())
{
NFD_LOG_TRACE("Processing '" << verb << "' verb");
PrefixRegOptions options;
if (!extractOptions(*request, options))
{
NFD_LOG_DEBUG("Error while extracting options, returning malformed command");
sendResponse(command, 400, "Malformed command");
return;
}
NFD_LOG_DEBUG("Received options (name, faceid, cost): " << options.getName()
<< ", " << options.getFaceId() << ", " << options.getCost());
ControlResponse response;
(verbProcessor->second)(this, *request, options);
}
else
{
NFD_LOG_DEBUG("Unsupported command: " << verb);
sendResponse(request->getName(), 501, "Unsupported command");
}
}
void
RibManager::onRibRequestValidationFailed(const shared_ptr<const Interest>& request,
const std::string& failureInfo)
{
NFD_LOG_DEBUG("RibRequestValidationFailed: " << failureInfo);
sendResponse(request->getName(), 403, failureInfo);
}
bool
RibManager::extractOptions(const Interest& request,
PrefixRegOptions& extractedOptions)
{
// const Name& command = request.getName();
//REMOTE_COMMAND_PREFIX is same in size of NRD_COMMAND_PREFIX
//so no extra processing is required.
const size_t optionCompIndex = COMMAND_PREFIX.size() + 1;
try
{
Block rawOptions = request.getName()[optionCompIndex].blockFromValue();
extractedOptions.wireDecode(rawOptions);
}
catch (const ndn::Tlv::Error& e)
{
return false;
}
if (extractedOptions.getFaceId() == 0)
{
NFD_LOG_TRACE("IncomingFaceId: " << request.getIncomingFaceId());
extractedOptions.setFaceId(request.getIncomingFaceId());
}
return true;
}
void
RibManager::onCommandError(uint32_t code, const std::string& error,
const Interest& request,
const PrefixRegOptions& options)
{
NFD_LOG_ERROR("NFD returned an error: " << error << " (code: " << code << ")");
ControlResponse response;
if (code == 404)
{
response.setCode(code);
response.setText(error);
}
else
{
response.setCode(533);
std::ostringstream os;
os << "Failure to update NFD " << "(NFD Error: " << code << " " << error << ")";
response.setText(os.str());
}
sendResponse(request.getName(), response);
m_managedRib.erase(options);
}
void
RibManager::onUnRegSuccess(const Interest& request, const PrefixRegOptions& options)
{
ControlResponse response;
response.setCode(200);
response.setText("Success");
response.setBody(options.wireEncode());
NFD_LOG_DEBUG("onUnRegSuccess: Name unregistered (" << options.getName()
<< ", " << options.getFaceId() << ")");
sendResponse(request.getName(), response);
m_managedRib.erase(options);
}
void
RibManager::onRegSuccess(const Interest& request, const PrefixRegOptions& options)
{
ControlResponse response;
response.setCode(200);
response.setText("Success");
response.setBody(options.wireEncode());
NFD_LOG_DEBUG("onRegSuccess: Name registered (" << options.getName() << ", "
<< options.getFaceId() << ")");
sendResponse(request.getName(), response);
}
void
RibManager::insertEntry(const Interest& request, const PrefixRegOptions& options)
{
// For right now, just pass the options to fib as it is,
// without processing flags. Later options will be first added to
// Rib tree, then nrd will generate fib updates based on flags and then
// will add next hops one by one..
m_managedRib.insert(options);
m_nfdController->start<ndn::nfd::FibAddNextHopCommand>(
ControlParameters()
.setName(options.getName())
.setFaceId(options.getFaceId())
.setCost(options.getCost()),
bind(&RibManager::onRegSuccess, this, request, options),
bind(&RibManager::onCommandError, this, _1, _2, request, options));
}
void
RibManager::deleteEntry(const Interest& request, const PrefixRegOptions& options)
{
m_nfdController->start<ndn::nfd::FibRemoveNextHopCommand>(
ControlParameters()
.setName(options.getName())
.setFaceId(options.getFaceId()),
bind(&RibManager::onUnRegSuccess, this, request, options),
bind(&RibManager::onCommandError, this, _1, _2, request, options));
}
boost::asio::io_service&
RibManager::getIoService()
{
/// \todo Switch face to use global io service (needs library update)
return *m_face->ioService();
}
void
RibManager::onControlHeaderSuccess()
{
NFD_LOG_DEBUG("Local control header enabled");
}
void
RibManager::onControlHeaderError(uint32_t code, const std::string& reason)
{
NFD_LOG_ERROR("Error: couldn't enable local control header "
<< "(code: " << code << ", info: " << reason << ")");
m_face->shutdown();
}
void
RibManager::enableLocalControlHeader()
{
m_nfdController->start<ndn::nfd::FaceEnableLocalControlCommand>(
ControlParameters()
.setLocalControlFeature(ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID),
bind(&RibManager::onControlHeaderSuccess, this),
bind(&RibManager::onControlHeaderError, this, _1, _2));
}
void
RibManager::onNotification(const FaceEventNotification& notification)
{
/// \todo A notification can be missed, in this case check Facelist
NFD_LOG_TRACE("onNotification: " << notification);
if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) { //face destroyed
m_managedRib.erase(notification.getFaceId());
}
}
} // namespace rib
} // namespace nfd