blob: 69be19f9e88ba5e2f6f42395107990f5aae4142d [file] [log] [blame]
Steve DiBenedetto042bfe92014-01-30 15:05:08 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "fib-manager.hpp"
8
9#include "table/fib.hpp"
10#include "fw/forwarder.hpp"
11#include "mgmt/internal-face.hpp"
12#include "mgmt/app-face.hpp"
13
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070014
15
16#include <ndn-cpp-dev/management/fib-management-options.hpp>
17#include <ndn-cpp-dev/encoding/tlv.hpp>
18
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070019#include <ndn-cpp-dev/management/fib-management-options.hpp>
20
21namespace nfd {
22
23NFD_LOG_INIT("FibManager");
24
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070025const Name FibManager::FIB_MANAGER_COMMAND_PREFIX = "/localhost/nfd/fib";
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070026
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070027const size_t FibManager::FIB_MANAGER_COMMAND_UNSIGNED_NCOMPS =
28 FibManager::FIB_MANAGER_COMMAND_PREFIX.size() +
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070029 1 + // verb
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070030 1; // verb options
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070031
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070032const size_t FibManager::FIB_MANAGER_COMMAND_SIGNED_NCOMPS =
33 FibManager::FIB_MANAGER_COMMAND_UNSIGNED_NCOMPS +
34 0; // No signed Interest support in mock, otherwise 3 (timestamp, signed info tlv, signature tlv)
35
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070036const FibManager::VerbAndProcessor FibManager::FIB_MANAGER_COMMAND_VERBS[] =
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070037 {
38 // Unsupported
39
40 // VerbAndProcessor(
Steve DiBenedettobdedce92014-02-02 22:49:39 -070041 // "insert",
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070042 // &FibManager::fibInsert
43 // ),
44
45 // VerbAndProcessor(
Steve DiBenedettobdedce92014-02-02 22:49:39 -070046 // "delete",
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070047 // &FibManager::fibDelete
48 // ),
49
50 VerbAndProcessor(
Steve DiBenedettobdedce92014-02-02 22:49:39 -070051 "add-nexthop",
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070052 &FibManager::fibAddNextHop
53 ),
54
55 // Unsupported
56
57 // VerbAndProcessor(
Steve DiBenedettobdedce92014-02-02 22:49:39 -070058 // "remove-nexthop",
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070059 // &FibManager::fibRemoveNextHop
60 // ),
61
62 // VerbAndProcessor(
Steve DiBenedettobdedce92014-02-02 22:49:39 -070063 // "strategy",
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070064 // &FibManager::fibStrategy
65 // )
66
67 };
68
69FibManager::FibManager(Fib& fib,
Steve DiBenedetto3970c892014-01-31 23:31:13 -070070 function<shared_ptr<Face>(FaceId)> getFace,
71 shared_ptr<AppFace> face)
72 : ManagerBase(face),
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070073 m_managedFib(fib),
74 m_getFace(getFace),
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070075 m_verbDispatch(FIB_MANAGER_COMMAND_VERBS,
76 FIB_MANAGER_COMMAND_VERBS +
77 (sizeof(FIB_MANAGER_COMMAND_VERBS) / sizeof(VerbAndProcessor)))
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070078{
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070079 face->setInterestFilter("/localhost/nfd/fib",
80 bind(&FibManager::onFibRequest, this, _2));
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070081}
82
83void
84FibManager::onFibRequest(const Interest& request)
85{
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070086 const Name& command = request.getName();
87 const size_t commandNComps = command.size();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070088
89 /// \todo Separate out response status codes 400 and 401
90
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070091 if (FIB_MANAGER_COMMAND_UNSIGNED_NCOMPS <= commandNComps &&
92 commandNComps < FIB_MANAGER_COMMAND_SIGNED_NCOMPS)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070093 {
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070094 NFD_LOG_INFO("Unsigned command: " << command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -070095 sendResponse(command, 401, "Signature required");
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070096
97 return;
98 }
99 else if (commandNComps < FIB_MANAGER_COMMAND_SIGNED_NCOMPS ||
100 !FIB_MANAGER_COMMAND_PREFIX.isPrefixOf(command))
101 {
102 NFD_LOG_INFO("Malformed command: " << command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700103 sendResponse(command, 400, "Malformed command");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700104 return;
105 }
106
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700107
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700108 const Name::Component& verb = command.get(FIB_MANAGER_COMMAND_PREFIX.size());
109
110 VerbDispatchTable::const_iterator verbProcessor = m_verbDispatch.find (verb);
111 if (verbProcessor != m_verbDispatch.end())
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700112 {
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700113 NFD_LOG_INFO("Processing command verb: " << verb);
114 (verbProcessor->second)(this, request);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700115 }
116 else
117 {
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700118 NFD_LOG_INFO("Unsupported command verb: " << verb);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700119 sendResponse(request.getName(), 501, "Unsupported command");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700120 }
121
122}
123
124void
125FibManager::fibInsert(const Interest& request)
126{
127
128}
129
130void
131FibManager::fibDelete(const Interest& request)
132{
133
134}
135
136void
137FibManager::fibAddNextHop(const Interest& request)
138{
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700139 const Name& command = request.getName();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700140 ndn::FibManagementOptions options;
141 const size_t optionCompIndex =
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700142 FIB_MANAGER_COMMAND_PREFIX.size() + 1;
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700143
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700144 const ndn::Buffer& optionBuffer =
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700145 request.getName()[optionCompIndex].getValue();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700146 shared_ptr<const ndn::Buffer> tmpOptionBuffer(make_shared<ndn::Buffer>(optionBuffer));
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700147
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700148 try
149 {
150 Block rawOptions(tmpOptionBuffer);
151 options.wireDecode(rawOptions);
152 }
153 catch (const ndn::Tlv::Error& e)
154 {
155 NFD_LOG_INFO("Bad command option parse: " << command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700156 sendResponse(request.getName(), 400, "Malformed command");
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700157 return;
158 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700159
160 /// \todo authorize command
161 if (false)
162 {
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700163 NFD_LOG_INFO("Unauthorized command attempt: " << command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700164 sendResponse(request.getName(), 403, "Unauthorized command");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700165 return;
166 }
167
168 NFD_LOG_INFO("add-nexthop Name: " << options.getName()
169 << " FaceId: " << options.getFaceId()
170 << " Cost: " << options.getCost());
171
172 shared_ptr<Face> nextHopFace = m_getFace(options.getFaceId());
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700173 if (static_cast<bool>(nextHopFace))
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700174 {
175 std::pair<shared_ptr<fib::Entry>, bool> insertResult = m_managedFib.insert(options.getName());
176 insertResult.first->addNextHop(nextHopFace, options.getCost());
177 sendResponse(request.getName(), 200, "OK");
178 }
179 else
180 {
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700181 NFD_LOG_INFO("Unknown FaceId: " << command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700182 sendResponse(request.getName(), 400, "Malformed command");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700183 }
184}
185
186void
187FibManager::fibRemoveNextHop(const Interest& request)
188{
189
190}
191
192void
193FibManager::fibStrategy(const Interest& request)
194{
195
196}
197
198// void
199// FibManager::onConfig(ConfigFile::Node section, bool isDryRun)
200// {
201
202// }
203
204} // namespace nfd