23 #include "../lp/tags.hpp" 24 #include "../util/logger.hpp" 36 return [] (
const Name& prefix,
49 , m_keyChain(keyChain)
50 , m_signingInfo(signingInfo)
51 , m_storage(m_face.getIoService(), imsCapacity)
57 std::vector<Name> topPrefixNames;
58 std::transform(m_topLevelPrefixes.begin(), m_topLevelPrefixes.end(),
59 std::back_inserter(topPrefixNames),
60 [] (
const std::unordered_map<Name, TopPrefixEntry>::value_type& entry) {
61 return entry.second.topPrefix;
64 for (
const auto&
name : topPrefixNames) {
73 bool hasOverlap = std::any_of(m_topLevelPrefixes.begin(), m_topLevelPrefixes.end(),
74 [&] (
const std::unordered_map<Name, TopPrefixEntry>::value_type& x) {
75 return x.first.isPrefixOf(prefix) || prefix.
isPrefixOf(x.first);
78 BOOST_THROW_EXCEPTION(std::out_of_range(
"top-level prefix overlaps"));
81 TopPrefixEntry& topPrefixEntry = m_topLevelPrefixes[prefix];
82 topPrefixEntry.topPrefix = prefix;
87 [] (
const Name&,
const std::string& reason) {
88 BOOST_THROW_EXCEPTION(std::runtime_error(
"prefix registration failed: " + reason));
93 for (
const auto& entry : m_handlers) {
94 Name fullPrefix =
Name(prefix).append(entry.first);
95 const InterestFilterId* filter = m_face.
setInterestFilter(fullPrefix, bind(entry.second, prefix, _2));
96 topPrefixEntry.interestFilters.push_back(filter);
103 auto it = m_topLevelPrefixes.find(prefix);
104 if (it == m_topLevelPrefixes.end()) {
108 const TopPrefixEntry& topPrefixEntry = it->second;
109 if (topPrefixEntry.registeredPrefixId) {
110 m_face.
unregisterPrefix(*topPrefixEntry.registeredPrefixId,
nullptr,
nullptr);
112 for (
const auto& filter : topPrefixEntry.interestFilters) {
116 m_topLevelPrefixes.erase(it);
120 Dispatcher::isOverlappedWithOthers(
const PartialName& relPrefix)
const 122 bool hasOverlapWithHandlers =
123 std::any_of(m_handlers.begin(), m_handlers.end(),
124 [&] (
const std::unordered_map<PartialName, InterestHandler>::value_type& entry) {
125 return entry.first.isPrefixOf(relPrefix) || relPrefix.
isPrefixOf(entry.first);
127 bool hasOverlapWithStreams =
128 std::any_of(m_streams.begin(), m_streams.end(),
129 [&] (
const std::unordered_map<PartialName, uint64_t>::value_type& entry) {
130 return entry.first.isPrefixOf(relPrefix) || relPrefix.
isPrefixOf(entry.first);
133 return hasOverlapWithHandlers || hasOverlapWithStreams;
140 sendControlResponse(
ControlResponse(403,
"authorization rejected"), interest);
145 Dispatcher::queryStorage(
const Name& prefix,
const Interest& interest,
146 const InterestHandler& missContinuation)
148 auto data = m_storage.find(interest);
149 if (data ==
nullptr) {
151 if (missContinuation)
152 missContinuation(prefix, interest);
161 Dispatcher::sendData(
const Name& dataName,
const Block& content,
const MetaInfo& metaInfo,
162 SendDestination option, time::milliseconds imsFresh)
164 auto data = make_shared<Data>(dataName);
167 m_keyChain.
sign(*data, m_signingInfo);
169 if (option == SendDestination::IMS || option == SendDestination::FACE_AND_IMS) {
172 data->setTag(make_shared<lp::CachePolicyTag>(policy));
173 m_storage.insert(*data, imsFresh);
176 if (option == SendDestination::FACE || option == SendDestination::FACE_AND_IMS) {
182 Dispatcher::sendOnFace(
const Data& data)
187 catch (
const Face::Error& e) {
193 Dispatcher::processControlCommandInterest(
const Name& prefix,
194 const Name& relPrefix,
196 const ControlParametersParser& parser,
198 const AuthorizationAcceptedCallback& accepted,
199 const AuthorizationRejectedCallback& rejected)
202 size_t parametersLoc = prefix.size() + relPrefix.size();
205 shared_ptr<ControlParameters> parameters;
207 parameters = parser(pc);
209 catch (
const tlv::Error&) {
215 authorization(prefix, interest, parameters.get(), accept, reject);
219 Dispatcher::processAuthorizedControlCommandInterest(
const std::string& requester,
222 const shared_ptr<ControlParameters>& parameters,
226 if (validateParams(*parameters)) {
227 handler(prefix, interest, *parameters,
228 bind(&Dispatcher::sendControlResponse,
this, _1, interest,
false));
231 sendControlResponse(
ControlResponse(400,
"failed in validating parameters"), interest);
244 sendData(interest.getName(), resp.wireEncode(), metaInfo,
253 if (!m_topLevelPrefixes.empty()) {
254 BOOST_THROW_EXCEPTION(std::domain_error(
"one or more top-level prefix has been added"));
257 if (isOverlappedWithOthers(relPrefix)) {
258 BOOST_THROW_EXCEPTION(std::out_of_range(
"status dataset name overlaps"));
261 AuthorizationAcceptedCallback accepted =
262 bind(&Dispatcher::processAuthorizedStatusDatasetInterest,
this, _1, _2, _3, handler);
263 AuthorizationRejectedCallback rejected =
264 bind(&Dispatcher::afterAuthorizationRejected,
this, _1, _2);
267 InterestHandler missContinuation = bind(&Dispatcher::processStatusDatasetInterest,
this,
268 _1, _2, authorization, accepted, rejected);
269 m_handlers[relPrefix] = bind(&Dispatcher::queryStorage,
this, _1, _2, missContinuation);
273 Dispatcher::processStatusDatasetInterest(
const Name& prefix,
276 const AuthorizationAcceptedCallback& accepted,
277 const AuthorizationRejectedCallback& rejected)
280 bool endsWithVersionOrSegment = interestName.
size() >= 1 &&
281 (interestName[-1].isVersion() || interestName[-1].isSegment());
282 if (endsWithVersionOrSegment) {
288 authorization(prefix, interest,
nullptr, accept, reject);
292 Dispatcher::processAuthorizedStatusDatasetInterest(
const std::string& requester,
297 StatusDatasetContext context(interest,
298 bind(&Dispatcher::sendStatusDatasetSegment,
this, _1, _2, _3, _4),
299 bind(&Dispatcher::sendControlResponse,
this, _1, interest,
true));
300 handler(prefix, interest, context);
304 Dispatcher::sendStatusDatasetSegment(
const Name& dataName,
const Block& content,
305 time::milliseconds imsFresh,
bool isFinalBlock)
309 auto destination = SendDestination::IMS;
310 if (dataName[-1].toSegment() == 0) {
311 destination = SendDestination::FACE_AND_IMS;
316 metaInfo.setFinalBlock(dataName[-1]);
319 sendData(dataName, content, metaInfo, destination, imsFresh);
325 if (!m_topLevelPrefixes.empty()) {
326 BOOST_THROW_EXCEPTION(std::domain_error(
"one or more top-level prefix has been added"));
329 if (isOverlappedWithOthers(relPrefix)) {
330 BOOST_THROW_EXCEPTION(std::out_of_range(
"notification stream name overlaps"));
335 m_handlers[relPrefix] = bind(&Dispatcher::queryStorage,
this, _1, _2,
nullptr);
336 m_streams[relPrefix] = 0;
338 return bind(&Dispatcher::postNotification,
this, _1, relPrefix);
342 Dispatcher::postNotification(
const Block& notification,
const PartialName& relPrefix)
344 if (m_topLevelPrefixes.empty() || m_topLevelPrefixes.size() > 1) {
345 NDN_LOG_WARN(
"postNotification: no top-level prefix or too many top-level prefixes");
349 Name streamName(m_topLevelPrefixes.begin()->second.topPrefix);
350 streamName.
append(relPrefix);
represents a CachePolicy header field
Copyright (c) 2011-2015 Regents of the University of California.
The interface of signing key management.
indicates a producer generated NACK
std::function< void(const Block ¬ification)> PostNotification
a function to post a notification
represents a dispatcher on server side of NFD Management protocol
RejectReply
indicate how to reply in case authorization is rejected
const RegisteredPrefixId * setInterestFilter(const InterestFilter &interestFilter, const InterestCallback &onInterest, const RegisterPrefixFailureCallback &onFailure, const security::SigningInfo &signingInfo=security::SigningInfo(), uint64_t flags=nfd::ROUTE_FLAG_CHILD_INHERIT)
Set InterestFilter to dispatch incoming matching interest to onInterest callback and register the fil...
reply with a ControlResponse where StatusCode is 403
bool isPrefixOf(const Name &other) const
Check if this name is a prefix of another name.
CachePolicy & setPolicy(CachePolicyType policy)
set policy type code
Represents a TLV element of NDN packet format.
Represents an Interest packet.
std::function< void(const std::string &requester)> AcceptContinuation
a function to be called if authorization is successful
Dispatcher(Face &face, KeyChain &keyChain, const security::SigningInfo &signingInfo=security::SigningInfo(), size_t imsCapacity=256)
constructor
Authorization makeAcceptAllAuthorization()
return an Authorization that accepts all Interests, with empty string as requester ...
Name & append(const Component &component)
Append a component.
Signing parameters passed to KeyChain.
void unregisterPrefix(const RegisteredPrefixId *registeredPrefixId, const UnregisterPrefixSuccessCallback &onSuccess, const UnregisterPrefixFailureCallback &onFailure)
Unregister prefix from RIB.
#define NDN_LOG_INIT(name)
declare a log module
const Block & get(uint32_t type) const
Get the first sub element of specified TLV-TYPE.
mgmt::ControlResponse ControlResponse
std::function< void(RejectReply reply)> RejectContinuation
a function to be called if authorization is rejected
Provide a communication channel with local or remote NDN forwarder.
void addTopPrefix(const Name &prefix, bool wantRegister=true, const security::SigningInfo &signingInfo=security::SigningInfo())
add a top-level prefix
Name & appendSequenceNumber(uint64_t seqNo)
Append a sequence number component.
Represents an absolute name.
const time::milliseconds DEFAULT_FRESHNESS_PERIOD
void unsetInterestFilter(const RegisteredPrefixId *registeredPrefixId)
Remove the registered prefix entry with the registeredPrefixId.
base class for a struct that contains ControlCommand parameters
size_t size() const
Get number of components.
void sign(Data &data, const SigningInfo ¶ms=getDefaultSigningInfo())
Sign data according to the supplied signing information.
Represents a name component.
std::function< void(const Name &prefix, const Interest &interest, const ControlParameters ¶ms, const CommandContinuation &done)> ControlCommandHandler
a function to handle an authorized ControlCommand
void addStatusDataset(const PartialName &relPrefix, const Authorization &authorization, const StatusDatasetHandler &handler)
register a StatusDataset or a prefix under which StatusDatasets can be requested
std::function< bool(const ControlParameters ¶ms)> ValidateParameters
a function to validate input ControlParameters
void put(Data data)
Publish data packet.
void removeTopPrefix(const Name &prefix)
remove a top-level prefix
#define NDN_LOG_ERROR(expression)
#define NDN_LOG_WARN(expression)
std::function< void(const Name &prefix, const Interest &interest, const ControlParameters *params, const AcceptContinuation &accept, const RejectContinuation &reject)> Authorization
a function that performs authorization
const RegisteredPrefixId * registerPrefix(const Name &prefix, const RegisterPrefixSuccessCallback &onSuccess, const RegisterPrefixFailureCallback &onFailure, const security::SigningInfo &signingInfo=security::SigningInfo(), uint64_t flags=nfd::ROUTE_FLAG_CHILD_INHERIT)
Register prefix with the connected NDN forwarder.
PostNotification addNotificationStream(const PartialName &relPrefix)
register a NotificationStream
const Name & getName() const
std::function< void(const Name &prefix, const Interest &interest, StatusDatasetContext &context)> StatusDatasetHandler
a function to handle a StatusDataset request