ae25a62b759fd0f3dd52597a0ba85e5e02d069a7
[platform/core/security/key-manager.git] / src / manager / client / client-common.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        client-common.h
20  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
21  * @version     1.0
22  * @brief       This file constains implementation of common types
23  *              used in Central Key Manager.
24  */
25
26 #ifndef _KEY_MANAGER_CLIENT_
27 #define _KEY_MANAGER_CLIENT_
28
29 #include <vector>
30 #include <functional>
31
32 #include <noncopyable.h>
33 #include <message-buffer.h>
34
35 #define KEY_MANAGER_API __attribute__((visibility("default")))
36
37 extern "C" {
38     struct msghdr;
39 }
40
41 namespace CKM {
42
43 int connectSocket(int& sock, char const * const interface);
44
45 int sendToServer(char const * const interface, const RawBuffer &send, MessageBuffer &recv);
46
47
48 class SockRAII {
49 public:
50     SockRAII()
51       : m_sock(-1)
52     {}
53
54     NONCOPYABLE(SockRAII);
55
56     virtual ~SockRAII() {
57         if (m_sock > -1)
58             close(m_sock);
59     }
60
61     int Connect(char const * const interface) {
62         return CKM::connectSocket(m_sock, interface);
63     }
64
65     int Get() const {
66         return m_sock;
67     }
68
69 private:
70     int m_sock;
71 };
72
73 /*
74  * Decorator function that performs frequently repeated exception handling in
75  * SS client API functions. Accepts lambda expression as an argument.
76  */
77 int try_catch(const std::function<int()>& func);
78
79 } // namespace CKM
80
81 #endif // _KEY_MANAGER_CLIENT_