34 #include <ndn-cxx/encoding/tlv.hpp>
40 const Name FibManager::COMMAND_PREFIX =
"/localhost/nfd/fib";
42 const size_t FibManager::COMMAND_UNSIGNED_NCOMPS =
43 FibManager::COMMAND_PREFIX.size() +
47 const size_t FibManager::COMMAND_SIGNED_NCOMPS =
48 FibManager::COMMAND_UNSIGNED_NCOMPS +
51 const FibManager::SignedVerbAndProcessor FibManager::SIGNED_COMMAND_VERBS[] =
54 SignedVerbAndProcessor(
55 Name::Component(
"add-nexthop"),
56 &FibManager::addNextHop
59 SignedVerbAndProcessor(
60 Name::Component(
"remove-nexthop"),
61 &FibManager::removeNextHop
66 const FibManager::UnsignedVerbAndProcessor FibManager::UNSIGNED_COMMAND_VERBS[] =
68 UnsignedVerbAndProcessor(
69 Name::Component(
"list"),
70 &FibManager::listEntries
74 const Name FibManager::LIST_COMMAND_PREFIX(
"/localhost/nfd/fib/list");
75 const size_t FibManager::LIST_COMMAND_NCOMPS = LIST_COMMAND_PREFIX.size();
79 function<shared_ptr<Face>(
FaceId)> getFace,
80 shared_ptr<InternalFace> face,
81 ndn::KeyChain& keyChain)
85 , m_fibEnumerationPublisher(fib, *face, LIST_COMMAND_PREFIX, keyChain)
86 , m_signedVerbDispatch(SIGNED_COMMAND_VERBS,
87 SIGNED_COMMAND_VERBS +
88 (sizeof(SIGNED_COMMAND_VERBS) / sizeof(SignedVerbAndProcessor)))
89 , m_unsignedVerbDispatch(UNSIGNED_COMMAND_VERBS,
90 UNSIGNED_COMMAND_VERBS +
91 (sizeof(UNSIGNED_COMMAND_VERBS) / sizeof(UnsignedVerbAndProcessor)))
93 face->setInterestFilter(
"/localhost/nfd/fib",
105 const Name& command = request.getName();
106 const size_t commandNComps = command.size();
108 if (commandNComps <= COMMAND_PREFIX.size())
116 const Name::Component& verb = command.at(COMMAND_PREFIX.size());
118 const auto unsignedVerbProcessor = m_unsignedVerbDispatch.find(verb);
119 if (unsignedVerbProcessor != m_unsignedVerbDispatch.end())
122 (unsignedVerbProcessor->second)(
this, request);
124 else if (COMMAND_UNSIGNED_NCOMPS <= commandNComps &&
125 commandNComps < COMMAND_SIGNED_NCOMPS)
130 else if (commandNComps < COMMAND_SIGNED_NCOMPS ||
131 !COMMAND_PREFIX.isPrefixOf(command))
139 bind(&FibManager::onValidatedFibRequest,
this, _1),
145 FibManager::onValidatedFibRequest(
const shared_ptr<const Interest>& request)
147 const Name& command = request->getName();
148 const Name::Component& verb = command[COMMAND_PREFIX.size()];
149 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
151 SignedVerbDispatchTable::const_iterator verbProcessor = m_signedVerbDispatch.find(verb);
152 if (verbProcessor != m_signedVerbDispatch.end())
154 ControlParameters parameters;
162 bool isSelfRegistration = (!parameters.hasFaceId() || parameters.getFaceId() == 0);
163 if (isSelfRegistration)
165 parameters.setFaceId(request->getIncomingFaceId());
169 ControlResponse response;
170 (verbProcessor->second)(
this, parameters, response);
181 FibManager::addNextHop(ControlParameters& parameters,
182 ControlResponse& response)
184 ndn::nfd::FibAddNextHopCommand command;
193 const Name& prefix = parameters.getName();
194 FaceId faceId = parameters.getFaceId();
195 uint64_t cost = parameters.getCost();
198 <<
" faceid: " << faceId
199 <<
" cost: " << cost);
201 shared_ptr<Face> nextHopFace = m_getFace(faceId);
202 if (static_cast<bool>(nextHopFace))
204 shared_ptr<fib::Entry> entry = m_managedFib.
insert(prefix).first;
206 entry->addNextHop(nextHopFace, cost);
209 <<
" prefix:" << prefix
210 <<
" faceid: " << faceId
211 <<
" cost: " << cost);
213 setResponse(response, 200,
"Success", parameters.wireEncode());
217 NFD_LOG_DEBUG(
"add-nexthop result: FAIL reason: unknown-faceid: " << faceId);
223 FibManager::removeNextHop(ControlParameters& parameters,
224 ControlResponse& response)
226 ndn::nfd::FibRemoveNextHopCommand command;
229 NFD_LOG_DEBUG(
"remove-nexthop result: FAIL reason: malformed");
234 NFD_LOG_TRACE(
"remove-nexthop prefix: " << parameters.getName()
235 <<
" faceid: " << parameters.getFaceId());
237 shared_ptr<Face> faceToRemove = m_getFace(parameters.getFaceId());
238 if (static_cast<bool>(faceToRemove))
240 shared_ptr<fib::Entry> entry = m_managedFib.
findExactMatch(parameters.getName());
241 if (static_cast<bool>(entry))
243 entry->removeNextHop(faceToRemove);
244 NFD_LOG_DEBUG(
"remove-nexthop result: OK prefix: " << parameters.getName()
245 <<
" faceid: " << parameters.getFaceId());
247 if (!entry->hasNextHops())
249 m_managedFib.
erase(*entry);
254 NFD_LOG_DEBUG(
"remove-nexthop result: OK, but entry for face id "
255 << parameters.getFaceId() <<
" not found");
261 << parameters.getFaceId() <<
" not found");
264 setResponse(response, 200,
"Success", parameters.wireEncode());
268 FibManager::listEntries(
const Interest& request)
270 const Name& command = request.getName();
271 const size_t commandNComps = command.size();
273 if (commandNComps < LIST_COMMAND_NCOMPS ||
274 !LIST_COMMAND_PREFIX.isPrefixOf(command))
281 m_fibEnumerationPublisher.
publish();
#define NFD_LOG_DEBUG(expression)
std::pair< shared_ptr< fib::Entry >, bool > insert(const Name &prefix)
inserts a FIB entry for prefix If an entry for exact same prefix exists, that entry is returned...
void sendResponse(const Name &name, const ControlResponse &response)
FibManager(Fib &fib, function< shared_ptr< Face >(FaceId)> getFace, shared_ptr< InternalFace > face, ndn::KeyChain &keyChain)
static bool extractParameters(const Name::Component ¶meterComponent, ControlParameters &extractedParameters)
const std::string FIB_PRIVILEGE
void erase(const Name &prefix)
shared_ptr< fib::Entry > findExactMatch(const Name &prefix) const
void onCommandValidationFailed(const shared_ptr< const Interest > &command, const std::string &error)
void onFibRequest(const Interest &request)
#define NFD_LOG_INIT(name)
#define NFD_LOG_TRACE(expression)
void setResponse(ControlResponse &response, uint32_t code, const std::string &text)
virtual bool validateParameters(const ControlCommand &command, ControlParameters ¶meters)
void validate(const Interest &interest, const ndn::OnInterestValidated &onValidated, const ndn::OnInterestValidationFailed &onValidationFailed)