4adfb0dcfeed6b2166e7db00b1e53fdf25c6c85a
[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 <ckm/ckm-type.h>
34 #include <message-buffer.h>
35 #include <protocols.h>
36
37 #define KEY_MANAGER_API __attribute__((visibility("default")))
38
39 extern "C" {
40     struct msghdr;
41 }
42
43 namespace CKM {
44
45 class AliasSupport
46 {
47     public:
48         AliasSupport(const Alias &alias);
49
50         const Label & getLabel() const;
51         const Name & getName() const;
52         bool isLabelEmpty() const;
53
54         static Alias merge(const Label &label, const Name &alias);
55     private:
56         Name m_name;
57         Label m_label;
58 };
59
60 int connectSocket(int& sock, char const * const interface);
61
62 int sendToServer(char const * const interface, const RawBuffer &send, MessageBuffer &recv);
63
64
65 class SockRAII {
66 public:
67     SockRAII()
68       : m_sock(-1)
69     {}
70
71     NONCOPYABLE(SockRAII);
72
73     virtual ~SockRAII() {
74         if (m_sock > -1)
75             close(m_sock);
76     }
77
78     int Connect(char const * const interface) {
79         return CKM::connectSocket(m_sock, interface);
80     }
81
82     int Get() const {
83         return m_sock;
84     }
85
86 private:
87     int m_sock;
88 };
89
90 /*
91  * Decorator function that performs frequently repeated exception handling in
92  * SS client API functions. Accepts lambda expression as an argument.
93  */
94 int try_catch(const std::function<int()>& func);
95
96 void try_catch_async(const std::function<void()>& func, const std::function<void(int)>& error);
97
98 } // namespace CKM
99
100 #endif // _KEY_MANAGER_CLIENT_