blob: 41391520ed010f21e714dcceb6b294e86d852606 [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"
16#include "fw/face-table.hpp"
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070017
18#include <ndn-cpp-dev/management/nfd-face-management-options.hpp>
19#include <ndn-cpp-dev/management/nfd-control-response.hpp>
20
21namespace nfd {
22
23const std::string FACE_MANAGER_PRIVILEGE = "faces";
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060024
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070025class ProtocolFactory;
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060026class NetworkInterfaceInfo;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070027
28class FaceManager : public ManagerBase
29{
30public:
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060031 class Error : public ManagerBase::Error
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070032 {
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060033 public:
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070034 Error(const std::string& what) : ManagerBase::Error(what) {}
35 };
36
37 /**
38 * \throws FaceManager::Error if localPort is an invalid port number
39 */
40
41 FaceManager(FaceTable& faceTable,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070042 shared_ptr<InternalFace> face);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070043
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060044 virtual
45 ~FaceManager();
46
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070047 /** \brief Subscribe to a face management section(s) for the config file
48 */
49 void
50 setConfigFile(ConfigFile& configFile);
51
52 void
53 onFaceRequest(const Interest& request);
54
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060055PUBLIC_WITH_TESTS_ELSE_PRIVATE:
56 void
57 listFaces(const Interest& request);
58
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070059PROTECTED_WITH_TESTS_ELSE_PRIVATE:
60
61 void
62 onValidatedFaceRequest(const shared_ptr<const Interest>& request);
63
64 VIRTUAL_WITH_TESTS void
65 createFace(const Name& requestName,
66 ndn::nfd::FaceManagementOptions& options);
67
68 VIRTUAL_WITH_TESTS void
69 destroyFace(const Name& requestName,
70 ndn::nfd::FaceManagementOptions& options);
71
72 bool
73 extractOptions(const Interest& request,
74 ndn::nfd::FaceManagementOptions& extractedOptions);
75
76 void
77 onCreated(const Name& requestName,
78 ndn::nfd::FaceManagementOptions& options,
79 const shared_ptr<Face>& newFace);
80
81 void
82 onConnectFailed(const Name& requestName, const std::string& reason);
83
84private:
85 void
86 onConfig(const ConfigSection& configSection, bool isDryRun);
87
88 void
89 processSectionUnix(const ConfigSection& configSection, bool isDryRun);
90
91 void
92 processSectionTcp(const ConfigSection& configSection, bool isDryRun);
93
94 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060095 processSectionUdp(const ConfigSection& configSection,
96 bool isDryRun,
97 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070098
99 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600100 processSectionEther(const ConfigSection& configSection,
101 bool isDryRun,
102 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700103
104 /** \brief parse a config option that can be either "yes" or "no"
105 * \throw ConfigFile::Error value is neither "yes" nor "no"
106 * \return true if "yes", false if "no"
107 */
108 bool
109 parseYesNo(const ConfigSection::const_iterator& i,
110 const std::string& optionName,
111 const std::string& sectionName);
112
113private:
114 typedef std::map< std::string/*protocol*/, shared_ptr<ProtocolFactory> > FactoryMap;
115 FactoryMap m_factories;
116 FaceTable& m_faceTable;
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600117 FaceStatusPublisher m_statusPublisher;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700118
119 typedef function<void(FaceManager*,
120 const Name&,
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600121 ndn::nfd::FaceManagementOptions&)> SignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700122
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600123 typedef std::map<Name::Component, SignedVerbProcessor> SignedVerbDispatchTable;
124 typedef std::pair<Name::Component, SignedVerbProcessor> SignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700125
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600126 typedef function<void(FaceManager*, const Interest&)> UnsignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700127
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600128 typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable;
129 typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700130
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700131
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600132 const SignedVerbDispatchTable m_signedVerbDispatch;
133 const UnsignedVerbDispatchTable m_unsignedVerbDispatch;
134
135 static const Name COMMAND_PREFIX; // /localhost/nfd/faces
136
137 // number of components in an invalid signed command (i.e. should be signed, but isn't)
138 // (/localhost/nfd/faces + verb + options) = 5
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700139 static const size_t COMMAND_UNSIGNED_NCOMPS;
140
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600141 // number of components in a valid signed command.
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700142 // (see UNSIGNED_NCOMPS), 9 with signed Interest support.
143 static const size_t COMMAND_SIGNED_NCOMPS;
144
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600145 static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[];
146 static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[];
147
148 static const Name LIST_COMMAND_PREFIX;
149 static const size_t LIST_COMMAND_NCOMPS;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700150};
151
152inline bool
153FaceManager::parseYesNo(const ConfigSection::const_iterator& i,
154 const std::string& optionName,
155 const std::string& sectionName)
156{
157 const std::string value = i->second.get_value<std::string>();
158 if (value == "yes")
159 {
160 return true;
161 }
162 else if (value == "no")
163 {
164 return false;
165 }
166
167 throw ConfigFile::Error("Invalid value for option \"" +
168 optionName + "\" in \"" +
169 sectionName + "\" section");
170
171}
172
173} // namespace nfd
174
175#endif // NFD_MGMT_FACE_MANAGER_HPP