Enable -Wshadow and fix warnings
[platform/core/security/key-manager.git] / src / manager / main / generic-socket-manager.h
1 /*
2  *  Copyright (c) 2014-2021 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        generic-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       Implementation of GenericSocketService and GenericSocketManager.
24  */
25
26 #ifndef _CENT_KEY_MNG_GENERIC_SERVICE_MANAGER_
27 #define _CENT_KEY_MNG_GENERIC_SERVICE_MANAGER_
28
29 #include <vector>
30 #include <string>
31
32 #include <sys/types.h>
33
34 #include <dpl/exception.h>
35 #include <generic-event.h>
36 #include <dpl/raw-buffer.h>
37 #include <ckm/ckm-type.h>
38 #include <credentials.h>
39 #include <service-messages.h>
40
41 extern "C" {
42         struct msghdr;
43 } // extern "C"
44
45 namespace CKM {
46
47 typedef int InterfaceID;
48
49 struct ConnectionID {
50         int sock;                                 // This is decriptor used for connection
51         int counter;                              // Unique handler per socket
52 };
53
54 struct GenericSocketManager;
55
56 struct GenericSocketService {
57         typedef std::string ServiceHandlerPath;
58         struct ServiceDescription {
59                 ServiceDescription(const char *path,
60                                                    const char *_privilege,
61                                                    InterfaceID _interfaceID = 0,
62                                                    bool _useSendMsg = false) :
63                         privilege(_privilege),
64                         interfaceID(_interfaceID),
65                         serviceHandlerPath(path),
66                         useSendMsg(_useSendMsg) {}
67
68                 std::string privilege;
69
70                 // All data from serviceHandlerPath will be marked with this interfaceHandler
71                 InterfaceID interfaceID;
72
73                 // Path to file
74                 ServiceHandlerPath serviceHandlerPath;
75                 bool useSendMsg;
76         };
77
78         typedef std::vector<ServiceDescription> ServiceDescriptionVector;
79
80         struct AcceptEvent : public GenericEvent {
81                 ConnectionID connectionID;
82                 InterfaceID interfaceID;
83                 Credentials credentials;
84         };
85
86         struct WriteEvent : public GenericEvent {
87                 ConnectionID connectionID;
88                 size_t size;
89                 size_t left;
90         };
91
92         struct ReadEvent : public GenericEvent {
93                 ConnectionID connectionID;
94                 RawBuffer rawBuffer;
95         };
96
97         struct CloseEvent : public GenericEvent {
98                 ConnectionID connectionID;
99         };
100
101         struct SecurityEvent : public GenericEvent {
102                 ConnectionID connectionID;
103                 bool allowed;
104         };
105
106         virtual void SetSocketManager(GenericSocketManager *manager)
107         {
108                 m_serviceManager = manager;
109         }
110
111         virtual void SetCommManager(CommMgr *manager)
112         {
113                 m_commMgr = manager;
114         }
115
116         virtual ServiceDescriptionVector GetServiceDescription() = 0;
117         virtual void Event(const AcceptEvent &event) = 0;
118         virtual void Event(const WriteEvent &event) = 0;
119         virtual void Event(const ReadEvent &event) = 0;
120         virtual void Event(const CloseEvent &event) = 0;
121         virtual void Event(const SecurityEvent &event) = 0;
122
123         virtual void Start() = 0;
124         virtual void Stop() = 0;
125
126         GenericSocketService() : m_serviceManager(NULL), m_commMgr(NULL) {}
127         virtual ~GenericSocketService() {}
128
129 protected:
130         GenericSocketManager *m_serviceManager;
131         CommMgr *m_commMgr;
132 };
133
134 struct GenericSocketManager {
135         virtual void MainLoop() = 0;
136         virtual void MainLoopStop() = 0;
137         virtual void RegisterSocketService(GenericSocketService *ptr) = 0;
138         virtual void CynaraSocket(int oldFd, int newFd, bool isRW) = 0;
139         virtual void Close(ConnectionID connectionID) = 0;
140         virtual void Write(ConnectionID connectionID, const RawBuffer &rawBuffer) = 0;
141         virtual void SecurityCheck(ConnectionID connectionID) = 0;
142         virtual ~GenericSocketManager() {}
143 };
144
145 } // namespace CKM
146
147 #endif // _CENT_KEY_MNG_GENERIC_SERVICE_MANAGER_