blob: 6787084d68d51c6ad9bd96c4aadcb4f702e4fd87 [file] [log] [blame]
Steve DiBenedettoabe9e972014-02-20 15:37:04 -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#ifndef NFD_MGMT_FACE_MANAGER_HPP
8#define NFD_MGMT_FACE_MANAGER_HPP
9
10#include "common.hpp"
11#include "face/face.hpp"
12#include "mgmt/app-face.hpp"
13#include "mgmt/manager-base.hpp"
14#include "mgmt/config-file.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060015#include "mgmt/face-status-publisher.hpp"
Steve DiBenedettofbb40a82014-03-11 19:40:15 -060016#include "mgmt/notification-stream.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060017#include "fw/face-table.hpp"
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070018
19#include <ndn-cpp-dev/management/nfd-face-management-options.hpp>
20#include <ndn-cpp-dev/management/nfd-control-response.hpp>
21
22namespace nfd {
23
24const std::string FACE_MANAGER_PRIVILEGE = "faces";
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060025
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070026class ProtocolFactory;
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060027class NetworkInterfaceInfo;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070028
29class FaceManager : public ManagerBase
30{
31public:
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060032 class Error : public ManagerBase::Error
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070033 {
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060034 public:
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070035 Error(const std::string& what) : ManagerBase::Error(what) {}
36 };
37
38 /**
39 * \throws FaceManager::Error if localPort is an invalid port number
40 */
41
42 FaceManager(FaceTable& faceTable,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070043 shared_ptr<InternalFace> face);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070044
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060045 virtual
46 ~FaceManager();
47
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070048 /** \brief Subscribe to a face management section(s) for the config file
49 */
50 void
51 setConfigFile(ConfigFile& configFile);
52
53 void
54 onFaceRequest(const Interest& request);
55
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060056PUBLIC_WITH_TESTS_ELSE_PRIVATE:
57 void
58 listFaces(const Interest& request);
59
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070060PROTECTED_WITH_TESTS_ELSE_PRIVATE:
61
62 void
63 onValidatedFaceRequest(const shared_ptr<const Interest>& request);
64
65 VIRTUAL_WITH_TESTS void
66 createFace(const Name& requestName,
67 ndn::nfd::FaceManagementOptions& options);
68
69 VIRTUAL_WITH_TESTS void
70 destroyFace(const Name& requestName,
71 ndn::nfd::FaceManagementOptions& options);
72
Steve DiBenedettofbb40a82014-03-11 19:40:15 -060073 void
74 ignoreUnsignedVerb(const Interest& request);
75
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070076 bool
77 extractOptions(const Interest& request,
78 ndn::nfd::FaceManagementOptions& extractedOptions);
79
80 void
81 onCreated(const Name& requestName,
82 ndn::nfd::FaceManagementOptions& options,
83 const shared_ptr<Face>& newFace);
84
85 void
86 onConnectFailed(const Name& requestName, const std::string& reason);
87
Steve DiBenedettofbb40a82014-03-11 19:40:15 -060088 void
89 onAddFace(shared_ptr<Face> face);
90
91 void
92 onRemoveFace(shared_ptr<Face> face);
93
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070094private:
95 void
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060096 onConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070097
98 void
99 processSectionUnix(const ConfigSection& configSection, bool isDryRun);
100
101 void
102 processSectionTcp(const ConfigSection& configSection, bool isDryRun);
103
104 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600105 processSectionUdp(const ConfigSection& configSection,
106 bool isDryRun,
107 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700108
109 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600110 processSectionEther(const ConfigSection& configSection,
111 bool isDryRun,
112 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700113
114 /** \brief parse a config option that can be either "yes" or "no"
115 * \throw ConfigFile::Error value is neither "yes" nor "no"
116 * \return true if "yes", false if "no"
117 */
118 bool
119 parseYesNo(const ConfigSection::const_iterator& i,
120 const std::string& optionName,
121 const std::string& sectionName);
122
123private:
124 typedef std::map< std::string/*protocol*/, shared_ptr<ProtocolFactory> > FactoryMap;
125 FactoryMap m_factories;
126 FaceTable& m_faceTable;
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600127 FaceStatusPublisher m_statusPublisher;
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600128 NotificationStream m_notificationStream;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700129
130 typedef function<void(FaceManager*,
131 const Name&,
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600132 ndn::nfd::FaceManagementOptions&)> SignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700133
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600134 typedef std::map<Name::Component, SignedVerbProcessor> SignedVerbDispatchTable;
135 typedef std::pair<Name::Component, SignedVerbProcessor> SignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700136
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600137 typedef function<void(FaceManager*, const Interest&)> UnsignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700138
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600139 typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable;
140 typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700141
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700142
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600143 const SignedVerbDispatchTable m_signedVerbDispatch;
144 const UnsignedVerbDispatchTable m_unsignedVerbDispatch;
145
146 static const Name COMMAND_PREFIX; // /localhost/nfd/faces
147
148 // number of components in an invalid signed command (i.e. should be signed, but isn't)
149 // (/localhost/nfd/faces + verb + options) = 5
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700150 static const size_t COMMAND_UNSIGNED_NCOMPS;
151
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600152 // number of components in a valid signed command.
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700153 // (see UNSIGNED_NCOMPS), 9 with signed Interest support.
154 static const size_t COMMAND_SIGNED_NCOMPS;
155
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600156 static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[];
157 static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[];
158
159 static const Name LIST_COMMAND_PREFIX;
160 static const size_t LIST_COMMAND_NCOMPS;
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600161
162 static const Name EVENTS_COMMAND_PREFIX;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700163};
164
165inline bool
166FaceManager::parseYesNo(const ConfigSection::const_iterator& i,
167 const std::string& optionName,
168 const std::string& sectionName)
169{
170 const std::string value = i->second.get_value<std::string>();
171 if (value == "yes")
172 {
173 return true;
174 }
175 else if (value == "no")
176 {
177 return false;
178 }
179
180 throw ConfigFile::Error("Invalid value for option \"" +
181 optionName + "\" in \"" +
182 sectionName + "\" section");
183
184}
185
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600186inline void
187FaceManager::ignoreUnsignedVerb(const Interest& request)
188{
189 // do nothing
190}
191
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700192} // namespace nfd
193
194#endif // NFD_MGMT_FACE_MANAGER_HPP