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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
/*
Copyright (C) 2009 Red Hat, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _H_REDPEER
#define _H_REDPEER
#include <openssl/ssl.h>
#include <openssl/err.h>
#include "common.h"
#include <spice/protocol.h>
#include "process_loop.h"
#include "threads.h"
#include "platform_utils.h"
#include "marshaller.h"
#include "debug.h"
#include "ssl_verify.h"
class RedPeer: protected EventSources::Socket {
public:
RedPeer();
virtual ~RedPeer();
class InMessage;
class CompoundInMessage;
class OutMessage;
class DisconnectedException {};
class HostAuthOptions {
public:
typedef std::vector<uint8_t> PublicKey;
typedef std::pair<std::string, std::string> CertFieldValuePair;
typedef std::list<CertFieldValuePair> CertFieldValueList;
HostAuthOptions() : type_flags(SPICE_SSL_VERIFY_OP_NONE) {}
public:
SPICE_SSL_VERIFY_OP type_flags;
PublicKey host_pubkey;
std::string host_subject;
std::string CA_file;
};
class ConnectionOptions {
public:
enum Type {
CON_OP_INVALID,
CON_OP_SECURE,
CON_OP_UNSECURE,
CON_OP_BOTH,
};
ConnectionOptions(Type in_type, int in_port, int in_sport,
int in_protocol,
const HostAuthOptions& in_host_auth,
const std::string& in_ciphers)
: type (in_type)
, unsecure_port (in_port)
, secure_port (in_sport)
, protocol (in_protocol)
, host_auth (in_host_auth)
, ciphers (in_ciphers)
{
}
virtual ~ConnectionOptions() {}
bool allow_secure() const
{
return (type == CON_OP_BOTH || type == CON_OP_SECURE) && secure_port != -1;
}
bool allow_unsecure() const
{
return (type == CON_OP_BOTH || type == CON_OP_UNSECURE) && unsecure_port != -1;
}
public:
Type type;
int unsecure_port;
int secure_port;
int protocol; // 0 == auto
HostAuthOptions host_auth; // for secure connection
std::string ciphers;
};
void connect_unsecure(const char* host, int port);
void connect_secure(const ConnectionOptions& options, const char* host);
void disconnect();
void swap(RedPeer* other);
void close();
void enable() { _shut = false;}
virtual CompoundInMessage* receive();
uint32_t do_send(OutMessage& message, uint32_t skip_bytes);
uint32_t send(OutMessage& message);
uint32_t receive(uint8_t* buf, uint32_t size);
uint32_t send(uint8_t* buf, uint32_t size);
protected:
virtual void on_event() {}
virtual int get_socket() { return _peer;}
void cleanup();
private:
void connect_to_peer(const char* host, int port);
void shutdown();
private:
SOCKET _peer;
Mutex _lock;
bool _shut;
uint64_t _serial;
SSL_CTX *_ctx;
SSL *_ssl;
};
class RedPeer::InMessage {
public:
InMessage(uint16_t type, uint32_t size, uint8_t * data)
: _type (type)
, _size (size)
, _data (data)
{
}
virtual ~InMessage() {}
uint16_t type() { return _type;}
uint8_t* data() { return _data;}
virtual uint32_t size() { return _size;}
protected:
uint16_t _type;
uint32_t _size;
uint8_t* _data;
};
class RedPeer::CompoundInMessage: public RedPeer::InMessage {
public:
CompoundInMessage(uint64_t _serial, uint16_t type, uint32_t size, uint32_t sub_list)
: InMessage(type, size, new uint8_t[size])
, _refs (1)
, _serial (_serial)
, _sub_list (sub_list)
{
}
RedPeer::InMessage* ref() { _refs++; return this;}
void unref() {if (!--_refs) delete this;}
uint64_t serial() { return _serial;}
uint32_t sub_list() { return _sub_list;}
virtual uint32_t size() { return _sub_list ? _sub_list : _size;}
uint32_t compound_size() {return _size;}
protected:
virtual ~CompoundInMessage() { delete[] _data;}
private:
int _refs;
uint64_t _serial;
uint32_t _sub_list;
};
class RedPeer::OutMessage {
public:
OutMessage(uint32_t type);
virtual ~OutMessage();
SpiceMarshaller *marshaller() { return _marshaller;}
void reset(uint32_t type);
private:
uint32_t message_size() { return spice_marshaller_get_total_size(_marshaller);}
uint8_t* base() { return spice_marshaller_get_ptr(_marshaller);}
SpiceDataHeader& header() { return *(SpiceDataHeader *)base();}
protected:
SpiceMarshaller *_marshaller;
friend class RedPeer;
friend class RedChannel;
};
#endif
|