2 * Copyright (c) 2000-2019 Samsung Electronics Co., Ltd. All rights reserved
4 * Contact: Dongsun Lee <ds73.lee@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
19 * @file socket-manager.h
20 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
21 * @author Zofia Abramowska (z.abramowska@samsung.com)
23 * @brief SocketManager implementation.
26 #ifndef _CENT_KEY_MNG_SOCKET_MANAGER_
27 #define _CENT_KEY_MNG_SOCKET_MANAGER_
37 #include <dpl/exception.h>
39 #include <generic-socket-manager.h>
40 #include <service-messages.h>
46 class SocketManager : public GenericSocketManager {
50 DECLARE_EXCEPTION_TYPE(CKM::Exception, Base)
51 DECLARE_EXCEPTION_TYPE(Base, InitFailed)
55 virtual ~SocketManager();
56 virtual void MainLoop();
57 virtual void MainLoopStop();
59 virtual void CynaraSocket(int oldFd, int newFd, bool isRW);
60 void SecurityStatus(int sock, int counter, bool allowed);
62 virtual void RegisterSocketService(GenericSocketService *service);
63 virtual void Close(ConnectionID connectionID);
64 virtual void Write(ConnectionID connectionID, const RawBuffer &rawBuffer);
65 virtual void SecurityCheck(ConnectionID connectionID);
68 void CreateDomainSocket(
69 GenericSocketService *service,
70 const GenericSocketService::ServiceDescription &desc);
71 int CreateDomainSocketHelp(
72 const GenericSocketService::ServiceDescription &desc);
73 int GetSocketFromSystemD(
74 const GenericSocketService::ServiceDescription &desc);
76 void ReadyForRead(int sock);
77 void ReadyForWrite(int sock);
78 void ReadyForAccept(int sock);
79 void ProcessQueue(void);
81 void CloseSocket(int sock);
83 struct SocketDescription {
86 return m_flags & OPEN;
91 return m_flags & LISTEN;
96 return m_flags & CYNARA;
101 return m_flags & TIMEOUT;
104 void setOpen(bool isSet)
106 isSet ? m_flags |= OPEN : m_flags &= ~OPEN;
109 void setListen(bool isSet)
111 isSet ? m_flags |= LISTEN : m_flags &= ~LISTEN;
114 void setCynara(bool isSet)
116 isSet ? m_flags |= CYNARA : m_flags &= ~CYNARA;
119 void setTimeout(bool isSet)
121 isSet ? m_flags |= TIMEOUT : m_flags &= ~TIMEOUT;
124 InterfaceID interfaceID;
125 GenericSocketService *service;
129 std::string cynaraPrivilege;
130 std::string cynaraUser;
131 std::string cynaraClient;
133 SocketDescription() :
136 timeout(::time(nullptr)),
141 static const char LISTEN = 1 << 0;
142 static const char OPEN = 1 << 1;
143 static const char CYNARA = 1 << 2;
144 static const char TIMEOUT = 1 << 3;
148 SocketDescription &CreateDefaultReadSocketDescription(int sock, bool timeout);
150 typedef std::vector<SocketDescription> SocketDescriptionVector;
152 // support for generic event Queue
153 typedef std::function<void(void)> EventFunction;
154 template <typename E>
155 void AddEvent(E event)
157 CreateEvent([this, event]() {
161 void CreateEvent(EventFunction fun);
164 ConnectionID connectionID;
168 struct CloseEvent : public ConnectionID {};
169 struct SecurityEvent : public ConnectionID {};
171 void Handle(const WriteEvent &event);
172 void Handle(const CloseEvent &event);
173 void Handle(const SecurityEvent &event);
174 // support for generic event Queue
179 bool operator<(const Timeout &second) const
181 return time > second.time; // mininum first!
185 SocketDescriptionVector m_socketDescriptionVector;
190 std::mutex m_eventQueueMutex;
191 std::queue<EventFunction> m_eventQueue;
194 std::priority_queue<Timeout> m_timeoutQueue;
196 std::unique_ptr<Cynara> m_cynara;
197 std::vector<GenericSocketService *> m_serviceVector;
202 #endif // _CENT_KEY_MNG_SOCKET_MANAGER_