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