cedb50ed4ad72f3079dd241aaab4f64b512a9704
[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 contains 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 <unistd.h>
30
31 #include <vector>
32 #include <functional>
33
34 #include <noncopyable.h>
35 #include <ckm/ckm-type.h>
36 #include <ckmc/ckmc-error.h>
37 #include <message-buffer.h>
38 #include <protocols.h>
39
40 #define EXCEPTION_GUARD_START_CPPAPI return CKM::try_catch([&]()->int {
41 #define EXCEPTION_GUARD_START_CAPI   return CKM::try_catch_enclosure([&]()->int {
42 #define EXCEPTION_GUARD_END          });
43
44 extern "C" {
45         struct msghdr;
46 }
47
48 namespace CKM {
49
50 class AliasSupport {
51 public:
52         AliasSupport(const Alias &alias);
53
54         const ClientId &getOwner() const;
55         const Name &getName() const;
56         bool isOwnerEmpty() const;
57
58         static Alias merge(const ClientId &owner, const Name &alias);
59
60 private:
61         Name m_name;
62         ClientId m_owner;
63 };
64
65 class SockRAII {
66 public:
67         SockRAII();
68
69         NONCOPYABLE(SockRAII);
70
71         virtual ~SockRAII();
72
73         int connect(const char *interface);
74         void disconnect();
75         bool isConnected() const;
76         int get() const;
77         int waitForSocket(int event, int timeout);
78
79 protected:
80         int connectWrapper(int socket, const char *interface);
81         int m_sock;
82 };
83
84 class ServiceConnection {
85 public:
86         ServiceConnection(const char *service_interface);
87
88         // roundtrip: send and receive
89         int processRequest(const CKM::RawBuffer &send_buf,
90                                            CKM::MessageBuffer &recv_buf);
91
92         // blocking
93         int send(const CKM::RawBuffer &send_buf);
94         int receive(CKM::MessageBuffer &recv_buf);
95
96         virtual ~ServiceConnection();
97
98 protected:
99         int prepareConnection();
100
101         SockRAII m_socket;
102         std::string m_serviceInterface;
103 };
104
105 /*
106  * Decorator function that performs frequently repeated exception handling in
107  * SS client API functions. Accepts lambda expression as an argument.
108  */
109 // for c++ api layer
110 int try_catch(const std::function<int()> &func);
111 // for c api layer
112 int try_catch_enclosure(const std::function<int()> &func);
113
114 // for c++ async api layer
115 void try_catch_async(const std::function<void()> &func,
116                                          const std::function<void(int)> &error);
117
118 } // namespace CKM
119
120 #endif // _KEY_MANAGER_CLIENT_