-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCDNConnection.cpp
266 lines (224 loc) · 7.92 KB
/
CDNConnection.cpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "CDNConnection.h"
#include "log.h"
#include "app_exceptions.h"
#include <ws2tcpip.h>
#include "mbedtls/error.h"
#if defined(WIN32)
# pragma comment(lib,"mbedTLS.lib")
#endif
CDNConnection::CDNConnection() :
_RECVBUF_SIZE(1048576),
_SENDBUF_SIZE(1048576),
_CHUNK_SIZE_(131072)
{
_socket = INVALID_SOCKET;
}
CDNConnection::~CDNConnection() {
this->close();
closesocket(_socket);
}
void CDNConnection::connect(std::string host, uint32_t port) {
_hostname = host;
// DNS resolution step
struct addrinfo hints;
struct addrinfo * resolved = NULL;
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
char port_string[20];
_itoa_s(port, port_string, 20, 10);
DWORD status = getaddrinfo(host.c_str(), port_string, &hints, &resolved);
if(status != 0) {
LOGE << "CDNConnection::connect DNS error. host=" << host << " port=" << port;
throw ErrorCritical("CDNConnection::connect DNS error");
}
// connecting
struct addrinfo * candidate = NULL;
for(candidate = resolved; candidate != NULL; candidate = candidate->ai_next) {
if(candidate->ai_family == AF_INET && candidate->ai_socktype == SOCK_STREAM) {
_socket = socket(AF_INET, SOCK_STREAM, candidate->ai_protocol);
if(_socket == INVALID_SOCKET) {
throw ErrorCritical("CDNConnection::connect() socket create error");
}
status = ::connect(_socket, candidate->ai_addr, (int) candidate->ai_addrlen);
if(status == SOCKET_ERROR) {
closesocket(_socket);
_socket = INVALID_SOCKET;
continue;
}
break;
}
}
freeaddrinfo(resolved);
if(_socket == INVALID_SOCKET) {
LOGE << "Unable to connect to CDN " << host << ":" << port;
throw ErrorCritical("Unable to connect to CDN");
}
int sockopt_status = setsockopt(_socket, SOL_SOCKET, SO_RCVBUF, (char *) &_RECVBUF_SIZE, sizeof(int));
if(sockopt_status == SOCKET_ERROR) {
throw ErrorCritical("CDNConnection setsockopt(SO_RCVBUF) error");
}
sockopt_status = setsockopt(_socket, SOL_SOCKET, SO_SNDBUF, (char *) &_SENDBUF_SIZE, sizeof(int));
if(sockopt_status == SOCKET_ERROR) {
throw ErrorCritical("CDNConnection setsockopt(SO_RCVBUF) error");
}
}
void CDNConnection::send(char * data, size_t data_size) {
while(data_size > 0) {
DWORD bytes_sent = ::send(_socket, data, data_size, 0);
if(bytes_sent == SOCKET_ERROR) {
int code = WSAGetLastError();
LOGE << "CDNConnection::send error=" << code;
throw EventConnectionBroken();
}
data = data + bytes_sent;
data_size -= bytes_sent;
}
}
size_t CDNConnection::read_next_chunk(DataBuffer * buffer) {
buffer->reserve_capacity_from_end(_CHUNK_SIZE_);
int count = ::recv(_socket, buffer->end_of_data(), _CHUNK_SIZE_, 0);
buffer->_bytes_written += count;
return count;
}
void CDNConnection::close() {
shutdown(_socket, SD_BOTH);
}
CDNConnectionSSL::CDNConnectionSSL() {
_ssl_context_valid = false;
}
CDNConnectionSSL::~CDNConnectionSSL() {
mbedtls_x509_crt_free(&_cacert);
mbedtls_ssl_free(&_ssl);
mbedtls_ssl_config_free(&_conf);
mbedtls_ctr_drbg_free(&_ctr_drbg);
mbedtls_entropy_free(&_entropy);
}
void log_mbedtls_debug(void * context, int level, const char * file, int line, const char * txt) {
LOG << "[TLS] " << level << " " << txt << " file=" << file << " line=" << line;
}
#define SOMETHING "Mean server machine"
void CDNConnectionSSL::connect(std::string host, uint32_t port) {
mbedtls_ssl_init ( &_ssl );
mbedtls_ssl_config_init( &_conf );
mbedtls_x509_crt_init ( &_cacert );
mbedtls_ctr_drbg_init ( &_ctr_drbg );
mbedtls_entropy_init ( &_entropy );
int status = mbedtls_ctr_drbg_seed(
&_ctr_drbg,
mbedtls_entropy_func,
&_entropy,
(const unsigned char *) SOMETHING,
strlen(SOMETHING)
);
if(status != 0) {
LOGE << "CDNConnectionSSL::connect mbedtls_ctr_drbg_seed error=" << status;
throw ErrorCritical("CDNConnectionSSL::connect mbedtls_ctr_drbg_seed error");
}
CDNConnection::connect(host, port);
status = mbedtls_ssl_config_defaults(
&_conf,
MBEDTLS_SSL_IS_CLIENT,
MBEDTLS_SSL_TRANSPORT_STREAM,
MBEDTLS_SSL_PRESET_DEFAULT
);
if(status != 0) {
LOGE << "CDNConnectionSSL::connect mbedtls_ssl_config_defaults error=" << status;
throw ErrorCritical("CDNConnectionSSL::connect mbedtls_ssl_config_defaults error");
}
// TODO: proper setup
mbedtls_ssl_conf_authmode(&_conf, MBEDTLS_SSL_VERIFY_OPTIONAL); // MBEDTLS_SSL_VERIFY_REQUIRED
mbedtls_ssl_conf_ca_chain(&_conf, &_cacert, NULL);
mbedtls_ssl_conf_rng(&_conf, mbedtls_ctr_drbg_random, &_ctr_drbg);
void * debug_log_context = NULL; // context pointer not used in our simple logging logic
mbedtls_ssl_conf_dbg(&_conf, log_mbedtls_debug, debug_log_context);
status = mbedtls_ssl_setup(&_ssl, &_conf);
if(status != 0) {
LOGE << "CDNConnectionSSL::connect mbedtls_ssl_setup error=" << status;
throw ErrorCritical("CDNConnectionSSL::connect mbedtls_ssl_setup error");
}
status = mbedtls_ssl_set_hostname(&_ssl, _hostname.c_str());
if(status != 0) {
LOGE << "CDNConnectionSSL::connect mbedtls_ssl_set_hostname error=" << status;
throw ErrorCritical("CDNConnectionSSL::connect mbedtls_ssl_set_hostname error");
}
mbedtls_ssl_set_bio(&_ssl, this, CDNConnectionSSL::_ssl_send_, CDNConnectionSSL::_ssl_recv_, NULL);
_ssl_context_valid = true;
// handshake
status = mbedtls_ssl_handshake(&_ssl);
if(status != 0) {
_ssl_context_valid = false;
LOGE << "CDNConnectionSSL::connect mbedtls_ssl_handshake error=" << (-status);
throw ErrorCritical("CDNConnectionSSL::connect mbedtls_ssl_handshake error");
}
}
int CDNConnectionSSL::_ssl_send_(void *ctx, const unsigned char *buf, size_t len) {
CDNConnectionSSL * self = (CDNConnectionSSL *) ctx;
if(self->_ssl_context_valid == false) {
LOGE << "_ssl_send_ on broken context";
return 0;
}
DWORD bytes_sent = ::send(self->_socket, (const char *) buf, len, 0);
return bytes_sent;
}
int CDNConnectionSSL::_ssl_recv_(void *ctx, unsigned char *buf, size_t len) {
CDNConnectionSSL * self = (CDNConnectionSSL *) ctx;
if(self->_ssl_context_valid == false) {
LOGE << "_ssl_recv_ on broken context";
return 0;
}
int count = ::recv(self->_socket, (char *) buf, len, 0);
return count;
}
void CDNConnectionSSL::send(char * data, size_t data_size) {
if(_ssl_context_valid == false) {
LOGE << "send on broken context";
throw EventConnectionBroken();
}
while(data_size > 0) {
int count = mbedtls_ssl_write(&_ssl, (const unsigned char *) data, data_size);
if(count < 1) {
_ssl_context_valid = false;
LOGE << "CDNConnectionSSL::send mbedtls_ssl_write error=" << count;
throw ErrorCritical("CDNConnectionSSL::send mbedtls_ssl_write error");
}
data_size -= count;
data += count;
}
}
size_t CDNConnectionSSL::read_next_chunk(DataBuffer * buffer) {
if(_ssl_context_valid == false) {
LOGE << "read_next_chunk on broken context";
throw EventConnectionBroken();
}
int count = 0;
buffer->reserve_capacity_from_end(_CHUNK_SIZE_);
while(true) {
count = mbedtls_ssl_read(
&_ssl,
(unsigned char *) (buffer->end_of_data()),
_CHUNK_SIZE_
);
if(count == MBEDTLS_ERR_SSL_WANT_READ || count == MBEDTLS_ERR_SSL_WANT_WRITE) continue;
if(count == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY || count == 0) {
// // LOG << "CDN ssl closed connection";
_ssl_context_valid = false;
throw EventConnectionBroken();
}
if(count < 0) {
char error_str[200];
mbedtls_strerror(count, error_str, 199);
LOG << "CDN ssl ret=" << count << " msg=" << error_str;
_ssl_context_valid = false;
throw ErrorCritical("CDN ssl error=");
}
break;
}
buffer->_bytes_written += count;
return count;
}
void CDNConnectionSSL::close() {
// // mbedtls_ssl_close_notify(&_ssl);
CDNConnection::close();
}