blob: 5885e48fea02656de78c308db59b52ea144906ee [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
36const Name::Component FibManager::FIB_MANAGER_COMMAND_VERB_INSERT = "insert";
37const Name::Component FibManager::FIB_MANAGER_COMMAND_VERB_DELETE = "delete";
38const Name::Component FibManager::FIB_MANAGER_COMMAND_VERB_ADD_NEXTHOP = "add-nexthop";
39const Name::Component FibManager::FIB_MANAGER_COMMAND_VERB_REMOVE_NEXTHOP = "remove-nexthop";
40const Name::Component FibManager::FIB_MANAGER_COMMAND_VERB_STRATEGY = "strategy";
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070041
42
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070043const FibManager::VerbAndProcessor FibManager::FIB_MANAGER_COMMAND_VERBS[] =
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070044 {
45 // Unsupported
46
47 // VerbAndProcessor(
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070048 // FibManager::FIB_MANAGER_COMMAND_VERB_INSERT,
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070049 // &FibManager::fibInsert
50 // ),
51
52 // VerbAndProcessor(
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070053 // FibManager::FIB_MANAGER_COMMAND_VERB_DELETE,
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070054 // &FibManager::fibDelete
55 // ),
56
57 VerbAndProcessor(
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070058 FibManager::FIB_MANAGER_COMMAND_VERB_ADD_NEXTHOP,
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070059 &FibManager::fibAddNextHop
60 ),
61
62 // Unsupported
63
64 // VerbAndProcessor(
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070065 // FibManager::FIB_MANAGER_COMMAND_VERB_REMOVE_NEXTHOP,
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070066 // &FibManager::fibRemoveNextHop
67 // ),
68
69 // VerbAndProcessor(
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070070 // FibManager::FIB_MANAGER_COMMAND_VERB_STRATEGY,
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070071 // &FibManager::fibStrategy
72 // )
73
74 };
75
76FibManager::FibManager(Fib& fib,
Steve DiBenedetto3970c892014-01-31 23:31:13 -070077 function<shared_ptr<Face>(FaceId)> getFace,
78 shared_ptr<AppFace> face)
79 : ManagerBase(face),
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070080 m_managedFib(fib),
81 m_getFace(getFace),
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070082 m_verbDispatch(FIB_MANAGER_COMMAND_VERBS,
83 FIB_MANAGER_COMMAND_VERBS +
84 (sizeof(FIB_MANAGER_COMMAND_VERBS) / sizeof(VerbAndProcessor)))
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070085{
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070086 face->setInterestFilter("/localhost/nfd/fib",
87 bind(&FibManager::onFibRequest, this, _2));
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070088}
89
90void
91FibManager::onFibRequest(const Interest& request)
92{
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070093 const Name& command = request.getName();
94 const size_t commandNComps = command.size();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070095
96 /// \todo Separate out response status codes 400 and 401
97
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070098 if (FIB_MANAGER_COMMAND_UNSIGNED_NCOMPS <= commandNComps &&
99 commandNComps < FIB_MANAGER_COMMAND_SIGNED_NCOMPS)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700100 {
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700101 NFD_LOG_INFO("Unsigned command: " << command);
102 sendResponse(command, 401, "SIGREQ");
103
104 return;
105 }
106 else if (commandNComps < FIB_MANAGER_COMMAND_SIGNED_NCOMPS ||
107 !FIB_MANAGER_COMMAND_PREFIX.isPrefixOf(command))
108 {
109 NFD_LOG_INFO("Malformed command: " << command);
110 sendResponse(command, 400, "MALFORMED");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700111 return;
112 }
113
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700114
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700115 const Name::Component& verb = command.get(FIB_MANAGER_COMMAND_PREFIX.size());
116
117 VerbDispatchTable::const_iterator verbProcessor = m_verbDispatch.find (verb);
118 if (verbProcessor != m_verbDispatch.end())
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700119 {
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700120 NFD_LOG_INFO("Processing command verb: " << verb);
121 (verbProcessor->second)(this, request);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700122 }
123 else
124 {
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700125 NFD_LOG_INFO("Unsupported command verb: " << verb);
126 sendResponse(request.getName(), 404, "UNSUPPORTED");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700127 }
128
129}
130
131void
132FibManager::fibInsert(const Interest& request)
133{
134
135}
136
137void
138FibManager::fibDelete(const Interest& request)
139{
140
141}
142
143void
144FibManager::fibAddNextHop(const Interest& request)
145{
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700146 const Name& command = request.getName();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700147 ndn::FibManagementOptions options;
148 const size_t optionCompIndex =
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700149 FIB_MANAGER_COMMAND_PREFIX.size() + 1;
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700150
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700151 const ndn::Buffer& optionBuffer =
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700152 request.getName()[optionCompIndex].getValue();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700153 shared_ptr<const ndn::Buffer> tmpOptionBuffer(make_shared<ndn::Buffer>(optionBuffer));
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700154
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700155 try
156 {
157 Block rawOptions(tmpOptionBuffer);
158 options.wireDecode(rawOptions);
159 }
160 catch (const ndn::Tlv::Error& e)
161 {
162 NFD_LOG_INFO("Bad command option parse: " << command);
163 sendResponse(request.getName(), 400, "MALFORMED");
164 return;
165 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700166
167 /// \todo authorize command
168 if (false)
169 {
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700170 NFD_LOG_INFO("Unauthorized command attempt: " << command);
171 sendResponse(request.getName(), 403, "UNAUTHORIZED");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700172 return;
173 }
174
175 NFD_LOG_INFO("add-nexthop Name: " << options.getName()
176 << " FaceId: " << options.getFaceId()
177 << " Cost: " << options.getCost());
178
179 shared_ptr<Face> nextHopFace = m_getFace(options.getFaceId());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700180 if (nextHopFace != 0)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700181 {
182 std::pair<shared_ptr<fib::Entry>, bool> insertResult = m_managedFib.insert(options.getName());
183 insertResult.first->addNextHop(nextHopFace, options.getCost());
184 sendResponse(request.getName(), 200, "OK");
185 }
186 else
187 {
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700188 NFD_LOG_INFO("Unknown FaceId: " << command);
189 sendResponse(request.getName(), 400, "MALFORMED");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700190 }
191}
192
193void
194FibManager::fibRemoveNextHop(const Interest& request)
195{
196
197}
198
199void
200FibManager::fibStrategy(const Interest& request)
201{
202
203}
204
205// void
206// FibManager::onConfig(ConfigFile::Node section, bool isDryRun)
207// {
208
209// }
210
211} // namespace nfd