summaryrefslogtreecommitdiff
path: root/src/manager/client/client-common.h
blob: 032f2f9267b0b19cc5c9c9cc8a9e54ec9a0d455c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 *  Copyright (c) 2000 - 2019 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 */
/*
 * @file        client-common.h
 * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
 * @version     1.0
 * @brief       This file contains implementation of common types
 *              used in Central Key Manager.
 */

#ifndef _KEY_MANAGER_CLIENT_
#define _KEY_MANAGER_CLIENT_

#include <unistd.h>

#include <vector>
#include <functional>

#include <noncopyable.h>
#include <ckm/ckm-type.h>
#include <ckmc/ckmc-error.h>
#include <message-buffer.h>
#include <protocols.h>

#define EXCEPTION_GUARD_START_CPPAPI return CKM::try_catch([&]()->int {
#define EXCEPTION_GUARD_START_CAPI   return CKM::try_catch_enclosure([&]()->int {
#define EXCEPTION_GUARD_END          });

extern "C" {
	struct msghdr;

	struct ckmc_alias_info_s {
		char* alias;
		bool is_password_protected;
	};

}

namespace CKM {

class AliasSupport {
public:
	AliasSupport(const Alias &alias);

	const ClientId &getOwner() const;
	const Name &getName() const;
	bool isOwnerEmpty() const;

	static Alias merge(const ClientId &owner, const Name &alias);

private:
	Name m_name;
	ClientId m_owner;
};

class SockRAII {
public:
	SockRAII();

	NONCOPYABLE(SockRAII);

	virtual ~SockRAII();

	int connect(const char *interface);
	void disconnect();
	bool isConnected() const;
	int get() const;
	int waitForSocket(int event, int timeout);

protected:
	int connectWrapper(int socket, const char *interface);
	int m_sock;
};

class ServiceConnection {
public:
	ServiceConnection(const char *service_interface);

	// roundtrip: send and receive
	int processRequest(const CKM::RawBuffer &send_buf,
					   CKM::MessageBuffer &recv_buf);

	// blocking
	int send(const CKM::RawBuffer &send_buf);
	int receive(CKM::MessageBuffer &recv_buf);

	virtual ~ServiceConnection();

protected:
	int prepareConnection();

	SockRAII m_socket;
	std::string m_serviceInterface;
};

/*
 * Decorator function that performs frequently repeated exception handling in
 * SS client API functions. Accepts lambda expression as an argument.
 */
// for c++ api layer
int try_catch(const std::function<int()> &func);
// for c api layer
int try_catch_enclosure(const std::function<int()> &func);

// for c++ async api layer
void try_catch_async(const std::function<void()> &func,
					 const std::function<void(int)> &error);

} // namespace CKM

#endif // _KEY_MANAGER_CLIENT_