Change contact information to Mr Dongsun Lee
[platform/core/security/key-manager.git] / src / manager / main / socket-manager.h
1 /*
2  *  Copyright (c) 2000-2019 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  *  Contact: Dongsun Lee <ds73.lee@samsung.com>
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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
17  */
18 /*
19  * @file        socket-manager.h
20  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
21  * @author      Zofia Abramowska (z.abramowska@samsung.com)
22  * @version     1.0
23  * @brief       SocketManager implementation.
24  */
25
26 #ifndef _CENT_KEY_MNG_SOCKET_MANAGER_
27 #define _CENT_KEY_MNG_SOCKET_MANAGER_
28
29 #include <vector>
30 #include <queue>
31 #include <string>
32 #include <mutex>
33 #include <thread>
34 #include <functional>
35 #include <ctime>
36
37 #include <dpl/exception.h>
38
39 #include <generic-socket-manager.h>
40 #include <service-messages.h>
41
42 namespace CKM {
43
44 class Cynara;
45
46 class SocketManager : public GenericSocketManager {
47 public:
48         class Exception {
49         public:
50                 DECLARE_EXCEPTION_TYPE(CKM::Exception, Base)
51                 DECLARE_EXCEPTION_TYPE(Base, InitFailed)
52         };
53
54         SocketManager();
55         virtual ~SocketManager();
56         virtual void MainLoop();
57         virtual void MainLoopStop();
58
59         virtual void CynaraSocket(int oldFd, int newFd, bool isRW);
60         void SecurityStatus(int sock, int counter, bool allowed);
61
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);
66
67 protected:
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);
75
76         void ReadyForRead(int sock);
77         void ReadyForWrite(int sock);
78         void ReadyForAccept(int sock);
79         void ProcessQueue(void);
80         void NotifyMe(void);
81         void CloseSocket(int sock);
82
83         struct SocketDescription {
84                 bool isOpen()
85                 {
86                         return m_flags & OPEN;
87                 }
88
89                 bool isListen()
90                 {
91                         return m_flags & LISTEN;
92                 }
93
94                 bool isCynara()
95                 {
96                         return m_flags & CYNARA;
97                 }
98
99                 bool isTimeout()
100                 {
101                         return m_flags & TIMEOUT;
102                 }
103
104                 void setOpen(bool isSet)
105                 {
106                         isSet ? m_flags |= OPEN : m_flags &= ~OPEN;
107                 }
108
109                 void setListen(bool isSet)
110                 {
111                         isSet ? m_flags |= LISTEN : m_flags &= ~LISTEN;
112                 }
113
114                 void setCynara(bool isSet)
115                 {
116                         isSet ? m_flags |= CYNARA : m_flags &= ~CYNARA;
117                 }
118
119                 void setTimeout(bool isSet)
120                 {
121                         isSet ? m_flags |= TIMEOUT : m_flags &= ~TIMEOUT;
122                 }
123
124                 InterfaceID interfaceID;
125                 GenericSocketService *service;
126                 time_t timeout;
127                 RawBuffer rawBuffer;
128                 int counter;
129                 std::string cynaraPrivilege;
130                 std::string cynaraUser;
131                 std::string cynaraClient;
132
133                 SocketDescription() :
134                         interfaceID(-1),
135                         service(nullptr),
136                         timeout(::time(nullptr)),
137                         counter(0),
138                         m_flags(0) {}
139
140         private:
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;
145                 int m_flags;
146         };
147
148         SocketDescription &CreateDefaultReadSocketDescription(int sock, bool timeout);
149
150         typedef std::vector<SocketDescription> SocketDescriptionVector;
151
152         // support for generic event Queue
153         typedef std::function<void(void)> EventFunction;
154         template <typename E>
155         void AddEvent(E event)
156         {
157                 CreateEvent([this, event]() {
158                         this->Handle(event);
159                 });
160         }
161         void CreateEvent(EventFunction fun);
162
163         struct WriteEvent {
164                 ConnectionID connectionID;
165                 RawBuffer rawBuffer;
166         };
167
168         struct CloseEvent : public ConnectionID {};
169         struct SecurityEvent : public ConnectionID {};
170
171         void Handle(const WriteEvent &event);
172         void Handle(const CloseEvent &event);
173         void Handle(const SecurityEvent &event);
174         // support for generic event Queue
175
176         struct Timeout {
177                 time_t time;
178                 int sock;
179                 bool operator<(const Timeout &second) const
180                 {
181                         return time > second.time; // mininum first!
182                 }
183         };
184
185         SocketDescriptionVector m_socketDescriptionVector;
186         fd_set m_readSet;
187         fd_set m_writeSet;
188         int m_maxDesc;
189         bool m_working;
190         std::mutex m_eventQueueMutex;
191         std::queue<EventFunction> m_eventQueue;
192         int m_notifyMe[2];
193         int m_counter;
194         std::priority_queue<Timeout> m_timeoutQueue;
195         CommMgr m_commMgr;
196         std::unique_ptr<Cynara> m_cynara;
197         std::vector<GenericSocketService *> m_serviceVector;
198 };
199
200 } // namespace CKM
201
202 #endif // _CENT_KEY_MNG_SOCKET_MANAGER_