Change RawBuffer into SafeBuffer.
[platform/core/security/key-manager.git] / src / manager / main / generic-socket-manager.h
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Bumjin Im <bj.im@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 <safe-buffer.h>
35
36 #include <dpl/exception.h>
37
38 #include <generic-event.h>
39
40 extern "C" {
41 struct msghdr;
42 } // extern "C"
43
44 namespace CKM {
45
46 typedef int InterfaceID;
47
48 struct Credentials {
49     uid_t uid;
50     std::string smackLabel;
51 };
52
53 struct ConnectionID {
54     int sock;                                 // This is decriptor used for connection
55     int counter;                              // Unique handler per socket
56     inline bool operator<(const ConnectionID &second) const {
57         return counter < second.counter;
58     }
59 };
60
61 struct GenericSocketManager;
62
63 struct GenericSocketService {
64     typedef std::string SmackLabel;
65     typedef std::string ServiceHandlerPath;
66     struct ServiceDescription {
67         ServiceDescription(const char *path,
68             const char *smackLabel,
69             InterfaceID interfaceID = 0,
70             bool useSendMsg = false)
71           : smackLabel(smackLabel)
72           , interfaceID(interfaceID)
73           , serviceHandlerPath(path)
74           , useSendMsg(useSendMsg)
75         {}
76
77         SmackLabel smackLabel;                 // Smack label for socket
78         InterfaceID interfaceID;               // All data from serviceHandlerPath will be marked with this interfaceHandler
79         ServiceHandlerPath serviceHandlerPath; // Path to file
80         bool useSendMsg;
81     };
82
83     typedef std::vector<ServiceDescription> ServiceDescriptionVector;
84
85     struct AcceptEvent : public GenericEvent {
86         ConnectionID connectionID;
87         InterfaceID interfaceID;
88         Credentials credentials;
89     };
90
91     struct WriteEvent : public GenericEvent {
92         ConnectionID connectionID;
93         size_t size;
94         size_t left;
95     };
96
97     struct ReadEvent : public GenericEvent {
98         ConnectionID connectionID;
99         SafeBuffer rawBuffer;
100     };
101
102     struct CloseEvent : public GenericEvent {
103         ConnectionID connectionID;
104     };
105
106     virtual void SetSocketManager(GenericSocketManager *manager) {
107         m_serviceManager = manager;
108     }
109
110     virtual ServiceDescriptionVector GetServiceDescription() = 0;
111     virtual void Event(const AcceptEvent &event) = 0;
112     virtual void Event(const WriteEvent &event) = 0;
113     virtual void Event(const ReadEvent &event) = 0;
114     virtual void Event(const CloseEvent &event) = 0;
115
116     GenericSocketService() : m_serviceManager(NULL) {}
117     virtual ~GenericSocketService(){}
118 protected:
119     GenericSocketManager *m_serviceManager;
120 };
121
122 struct GenericSocketManager {
123     virtual void MainLoop() = 0;
124     virtual void RegisterSocketService(GenericSocketService *ptr) = 0;
125     virtual void Close(ConnectionID connectionID) = 0;
126     virtual void Write(ConnectionID connectionID, const SafeBuffer &rawBuffer) = 0;
127     virtual ~GenericSocketManager(){}
128 };
129
130 } // namespace CKM
131
132 #endif // _CENT_KEY_MNG_GENERIC_SERVICE_MANAGER_