882d0e52ba188e98d4c22470f6134c70c117515c
[framework/security/security-server.git] / src / daemon / sockets / security_socket_service.h
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file        security_socket_service.h
18  * @author      Zofia Abramowska (z.abramowska@samsung.com)
19  * @version     1.0
20  * @brief       Header of socket server class
21  */
22
23 #ifndef SECURITY_SOCKET_SERVICE_H_
24 #define SECURITY_SOCKET_SERVICE_H_
25
26 #include <map>
27 #include <list>
28 #include <memory>
29 #include <mutex>
30 #include <pthread.h>
31 #include <security_daemon.h>
32 #include <SocketConnection.h>
33 #include <callback_api.h>
34
35 class SecuritySocketService : public SecurityDaemon::DaemonService {
36
37 private:
38
39   virtual void initialize();
40   virtual void start();
41   virtual void stop();
42   virtual void deinitialize();
43
44
45 private:
46
47     //Function for registering callback with given interface and method name and possibly security check callback
48     void registerServiceCallback(const std::string& interfaceName,
49                                  const std::string& methodName,
50                                  socketServerCallback serviceCallback,
51                                  securityCheck securityCallback = NULL);
52     //Thread function for server
53     static void * serverThread(void *);
54     //Main function for server
55     void mainLoop();
56     //Thread function for connection serving
57     static void * connectionThread(void *);
58     //Main function for connection serving
59     void connectionService(int fd);
60     //closing all connections
61     void closeConnections();
62     //logs an error and throws an exception with message containing errno message
63     void throwWithErrnoMessage(const std::string &specificInfo);
64
65     //concurrency safe methods for client socket list - add, remove and pop (with returned value)
66     void addClientSocket(int clientThread);
67     void removeClientSocket(int clientThread);
68     bool popClientSocket(int* clientThread);
69
70     //Address of socket server
71     std::string m_serverAddress;
72     //Signal used for informing threads to stop
73     int m_signalToClose;
74     //Socket for listening
75     int m_listenFd;
76     //Number of main thread
77     pthread_t m_mainThread;
78     //Numbers of all created threads for connections
79     std::list<int> m_clientSocketList;
80
81     //Thread list mutex
82     std::mutex m_clientSocketListMutex;
83
84     //Structure for callback maps
85     class ServiceCallback
86     {
87     public:
88         ServiceCallback(socketServerCallback ser, securityCheck sec) : serviceCallback(ser), securityCallback(sec){}
89         socketServerCallback serviceCallback;
90         securityCheck securityCallback;
91     };
92
93     typedef std::shared_ptr<ServiceCallback> ServiceCallbackPtr;
94     //Map for callback methods, key is a method name and value is a callback to method
95     typedef std::map<std::string, ServiceCallbackPtr> ServiceMethodCallbackMap;
96     //Map for interface methods, key is an interface name and value is a map of available methods with callbacks
97     std::map<std::string, ServiceMethodCallbackMap> m_callbackMap;
98
99     //Structure passed to connection thread
100     struct Connection_Info{
101         Connection_Info(int fd, void * data) : connfd(fd), data(data)
102         {}
103         int connfd;
104         void * data;
105     };
106
107 };
108
109 #endif /* SECURITY_SOCKET_SERVICE_H_ */