Comments and defines changed to comply with new project name
[platform/core/security/security-manager.git] / src / server / main / generic-socket-manager.h
1 /*
2  *  Copyright (c) 2000 - 2013 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  * @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           : smackLabel(smackLabel)
64           , interfaceID(interfaceID)
65           , serviceHandlerPath(path)
66           , useSendMsg(useSendMsg)
67         {}
68
69         SmackLabel smackLabel;                 // Smack label for socket
70         InterfaceID interfaceID;               // All data from serviceHandlerPath will be marked with this interfaceHandler
71         ServiceHandlerPath serviceHandlerPath; // Path to file
72         bool useSendMsg;
73     };
74
75     typedef std::vector<ServiceDescription> ServiceDescriptionVector;
76
77     struct AcceptEvent : public GenericEvent {
78         ConnectionID connectionID;
79         InterfaceID interfaceID;
80     };
81
82     struct WriteEvent : public GenericEvent {
83         ConnectionID connectionID;
84         size_t size;
85         size_t left;
86     };
87
88     struct ReadEvent : public GenericEvent {
89         ConnectionID connectionID;
90         RawBuffer rawBuffer;
91     };
92
93     struct CloseEvent : public GenericEvent {
94         ConnectionID connectionID;
95     };
96
97     virtual void SetSocketManager(GenericSocketManager *manager) {
98         m_serviceManager = manager;
99     }
100
101     virtual ServiceDescriptionVector GetServiceDescription() = 0;
102     virtual void Event(const AcceptEvent &event) = 0;
103     virtual void Event(const WriteEvent &event) = 0;
104     virtual void Event(const ReadEvent &event) = 0;
105     virtual void Event(const CloseEvent &event) = 0;
106
107     GenericSocketService() : m_serviceManager(NULL) {}
108     virtual ~GenericSocketService(){}
109 protected:
110     GenericSocketManager *m_serviceManager;
111 };
112
113 class SendMsgData {
114 public:
115     class Internal;
116
117     SendMsgData();
118     SendMsgData(int resultCode, int fileDesc, int flags = 0);
119     SendMsgData(const SendMsgData &second);
120     SendMsgData& operator=(const SendMsgData &second);
121     virtual ~SendMsgData();
122
123     msghdr* getMsghdr();
124     int flags();
125 private:
126     int m_resultCode;
127     int m_fileDesc;
128     int m_flags;
129     Internal *m_pimpl;
130 };
131
132 struct GenericSocketManager {
133     virtual void MainLoop() = 0;
134     virtual void RegisterSocketService(GenericSocketService *ptr) = 0;
135     virtual void Close(ConnectionID connectionID) = 0;
136     virtual void Write(ConnectionID connectionID, const RawBuffer &rawBuffer) = 0;
137     virtual void Write(ConnectionID connectionID, const SendMsgData &sendMsgData) = 0;
138     virtual ~GenericSocketManager(){}
139 };
140
141 } // namespace SecurityManager
142
143 #endif // _SECURITY_MANAGER_GENERIC_SERVICE_MANAGER_