Require socket to be passed by systemd, don't create it on our own
[platform/core/security/security-manager.git] / src / server / main / include / generic-socket-manager.h
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Rafal Krypa <r.krypa@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  * @version     1.0
22  * @brief       Implementation of GenericSocketService and GenericSocketManager.
23  */
24
25 #ifndef _SECURITY_MANAGER_GENERIC_SERVICE_MANAGER_
26 #define _SECURITY_MANAGER_GENERIC_SERVICE_MANAGER_
27
28 #include <vector>
29 #include <string>
30
31 #include <dpl/exception.h>
32
33 #include <generic-event.h>
34
35 extern "C" {
36 struct msghdr;
37 } // extern "C"
38
39 namespace SecurityManager {
40
41 typedef int InterfaceID;
42
43 struct ConnectionID {
44     int sock;                                 // This is decriptor used for connection
45     int counter;                              // Unique handler per socket
46     inline bool operator<(const ConnectionID &second) const {
47         return counter < second.counter;
48     }
49 };
50
51 typedef std::vector<unsigned char> RawBuffer;
52
53 struct GenericSocketManager;
54
55 struct GenericSocketService {
56     typedef std::string SmackLabel;
57     typedef std::string ServiceHandlerPath;
58     struct ServiceDescription {
59         ServiceDescription(const char *path,
60             const char *smackLabel,
61             InterfaceID interfaceID = 0,
62             bool useSendMsg = false,
63             bool systemdOnly = false)
64           : smackLabel(smackLabel)
65           , interfaceID(interfaceID)
66           , serviceHandlerPath(path)
67           , useSendMsg(useSendMsg)
68           , systemdOnly(systemdOnly)
69         {}
70
71         SmackLabel 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         bool systemdOnly;
76     };
77
78     typedef std::vector<ServiceDescription> ServiceDescriptionVector;
79
80     struct AcceptEvent : public GenericEvent {
81         ConnectionID connectionID;
82         InterfaceID interfaceID;
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     GenericSocketService() : m_serviceManager(NULL) {}
111     virtual ~GenericSocketService(){}
112 protected:
113     GenericSocketManager *m_serviceManager;
114 };
115
116 class SendMsgData {
117 public:
118     class Internal;
119
120     SendMsgData();
121     SendMsgData(int resultCode, int fileDesc, int flags = 0);
122     SendMsgData(const SendMsgData &second);
123     SendMsgData& operator=(const SendMsgData &second);
124     virtual ~SendMsgData();
125
126     msghdr* getMsghdr();
127     int flags();
128 private:
129     int m_resultCode;
130     int m_fileDesc;
131     int m_flags;
132     Internal *m_pimpl;
133 };
134
135 struct GenericSocketManager {
136     virtual void MainLoop() = 0;
137     virtual void RegisterSocketService(GenericSocketService *ptr) = 0;
138     virtual void Close(ConnectionID connectionID) = 0;
139     virtual void Write(ConnectionID connectionID, const RawBuffer &rawBuffer) = 0;
140     virtual void Write(ConnectionID connectionID, const SendMsgData &sendMsgData) = 0;
141     virtual ~GenericSocketManager(){}
142 };
143
144 } // namespace SecurityManager
145
146 #endif // _SECURITY_MANAGER_GENERIC_SERVICE_MANAGER_