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