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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
|
/*
* Copyright 2019-2021 OARC, Inc.
* Copyright 2017-2018 Akamai Technologies
* Copyright 2006-2016 Nominum, Inc.
* 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.
*/
#include "config.h"
#include "net.h"
#include "log.h"
#include "strerror.h"
#include "util.h"
#include "os.h"
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <openssl/err.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <ck_pr.h>
static SSL_CTX* ssl_ctx = 0;
#define self ((struct perf__dot_socket*)sock)
struct perf__dot_socket {
struct perf_net_socket base;
pthread_mutex_t lock;
SSL* ssl;
char recvbuf[TCP_RECV_BUF_SIZE], sendbuf[TCP_SEND_BUF_SIZE];
size_t at, sending;
bool is_ready, is_conn_ready, have_more, is_sending, do_reconnect;
perf_sockaddr_t server, local;
size_t bufsize;
uint16_t qid;
uint64_t conn_ts;
perf_socket_event_t conn_event, conning_event;
size_t num_queries_per_conn, nqpc_timeout;
unsigned int nqpc_sent, nqpc_recv;
uint64_t nqpc_ts;
};
static void perf__dot_connect(struct perf_net_socket* sock)
{
int ret;
self->nqpc_sent = 0;
ck_pr_store_uint(&self->nqpc_recv, 0);
self->nqpc_ts = 0;
int fd = socket(self->server.sa.sa.sa_family, SOCK_STREAM, 0);
if (fd == -1) {
char __s[256];
perf_log_fatal("socket: %s", perf_strerror_r(errno, __s, sizeof(__s)));
}
ck_pr_store_int(&sock->fd, fd);
if (self->ssl) {
SSL_free(self->ssl);
}
if (!(self->ssl = SSL_new(ssl_ctx))) {
perf_log_fatal("SSL_new(): %s", ERR_error_string(ERR_get_error(), 0));
}
if (!(ret = SSL_set_fd(self->ssl, sock->fd))) {
perf_log_fatal("SSL_set_fd(): %s", ERR_error_string(SSL_get_error(self->ssl, ret), 0));
}
if (self->server.sa.sa.sa_family == AF_INET6) {
int on = 1;
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) == -1) {
perf_log_warning("setsockopt(IPV6_V6ONLY) failed");
}
}
if (bind(sock->fd, &self->local.sa.sa, self->local.length) == -1) {
char __s[256];
perf_log_fatal("bind: %s", perf_strerror_r(errno, __s, sizeof(__s)));
}
if (self->bufsize > 0) {
ret = setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
&self->bufsize, sizeof(self->bufsize));
if (ret < 0)
perf_log_warning("setsockbuf(SO_RCVBUF) failed");
ret = setsockopt(sock->fd, SOL_SOCKET, SO_SNDBUF,
&self->bufsize, sizeof(self->bufsize));
if (ret < 0)
perf_log_warning("setsockbuf(SO_SNDBUF) failed");
}
int flags = fcntl(sock->fd, F_GETFL, 0);
if (flags < 0)
perf_log_fatal("fcntl(F_GETFL)");
ret = fcntl(sock->fd, F_SETFL, flags | O_NONBLOCK);
if (ret < 0)
perf_log_fatal("fcntl(F_SETFL)");
self->conn_ts = perf_get_time();
if (sock->event) {
sock->event(sock, self->conning_event, self->conn_ts);
self->conning_event = perf_socket_event_reconnecting;
}
if (connect(sock->fd, &self->server.sa.sa, self->server.length)) {
if (errno == EINPROGRESS) {
return;
} else {
char __s[256];
perf_log_fatal("connect() failed: %s", perf_strerror_r(errno, __s, sizeof(__s)));
}
}
self->is_conn_ready = true;
}
static void perf__dot_reconnect(struct perf_net_socket* sock)
{
close(sock->fd);
self->have_more = false;
self->at = 0;
if (self->sending) {
self->sending = 0;
self->is_sending = false;
}
self->is_ready = false;
self->is_conn_ready = false;
perf__dot_connect(sock);
}
static ssize_t perf__dot_recv(struct perf_net_socket* sock, void* buf, size_t len, int flags)
{
ssize_t n;
uint16_t dnslen, dnslen2;
if (!self->have_more) {
PERF_LOCK(&self->lock);
if (!self->is_ready) {
PERF_UNLOCK(&self->lock);
errno = EAGAIN;
return -1;
}
n = SSL_read(self->ssl, self->recvbuf + self->at, TCP_RECV_BUF_SIZE - self->at);
if (!n) {
perf__dot_reconnect(sock);
PERF_UNLOCK(&self->lock);
errno = EAGAIN;
return -1;
}
if (n < 0) {
int err = SSL_get_error(self->ssl, n);
switch (err) {
case SSL_ERROR_WANT_READ:
errno = EAGAIN;
break;
case SSL_ERROR_SYSCALL:
switch (errno) {
case ECONNREFUSED:
case ECONNRESET:
case ENOTCONN:
perf__dot_reconnect(sock);
errno = EAGAIN;
break;
default:
break;
}
break;
default:
errno = EBADF;
break;
}
PERF_UNLOCK(&self->lock);
return -1;
}
PERF_UNLOCK(&self->lock);
self->at += n;
if (self->at < 3) {
errno = EAGAIN;
return -1;
}
}
memcpy(&dnslen, self->recvbuf, 2);
dnslen = ntohs(dnslen);
if (self->at < dnslen + 2) {
errno = EAGAIN;
return -1;
}
memcpy(buf, self->recvbuf + 2, len < dnslen ? len : dnslen);
memmove(self->recvbuf, self->recvbuf + 2 + dnslen, self->at - 2 - dnslen);
self->at -= 2 + dnslen;
if (self->num_queries_per_conn) {
ck_pr_inc_uint(&self->nqpc_recv);
}
if (self->at > 2) {
memcpy(&dnslen2, self->recvbuf, 2);
dnslen2 = ntohs(dnslen2);
if (self->at >= dnslen2 + 2) {
self->have_more = true;
return dnslen;
}
}
self->have_more = false;
return dnslen;
}
static ssize_t perf__dot_sendto(struct perf_net_socket* sock, uint16_t qid, const void* buf, size_t len, int flags, const struct sockaddr* dest_addr, socklen_t addrlen)
{
size_t send = len < TCP_SEND_BUF_SIZE - 2 ? len : (TCP_SEND_BUF_SIZE - 2);
// TODO: We only send what we can send, because we can't continue sending
uint16_t dnslen = htons(send);
ssize_t n;
PERF_LOCK(&self->lock);
memcpy(self->sendbuf, &dnslen, 2);
memcpy(self->sendbuf + 2, buf, send);
self->qid = qid;
if (!self->is_ready) {
self->is_sending = true;
self->sending = 0;
PERF_UNLOCK(&self->lock);
errno = EINPROGRESS;
return -1;
}
n = SSL_write(self->ssl, self->sendbuf, send + 2);
if (n < 1) {
switch (SSL_get_error(self->ssl, n)) {
case SSL_ERROR_SYSCALL:
switch (errno) {
case ECONNREFUSED:
case ECONNRESET:
case ENOTCONN:
case EPIPE:
perf__dot_reconnect(sock);
self->is_sending = true;
self->sending = 0;
PERF_UNLOCK(&self->lock);
errno = EINPROGRESS;
return -1;
default:
break;
}
PERF_UNLOCK(&self->lock);
return -1;
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
self->is_sending = true;
self->sending = 0;
PERF_UNLOCK(&self->lock);
errno = EINPROGRESS;
return -1;
default:
break;
}
perf_log_warning("SSL_write(): %s", ERR_error_string(SSL_get_error(self->ssl, n), 0));
errno = EBADF;
return -1;
}
if (n < send + 2) {
self->sending = n;
self->is_sending = true;
PERF_UNLOCK(&self->lock);
errno = EINPROGRESS;
return -1;
}
PERF_UNLOCK(&self->lock);
self->nqpc_sent++;
return n - 2;
}
static int perf__dot_close(struct perf_net_socket* sock)
{
// TODO
return close(sock->fd);
}
static int perf__dot_sockeq(struct perf_net_socket* sock_a, struct perf_net_socket* sock_b)
{
return sock_a->fd == sock_b->fd;
}
static int perf__dot_sockready(struct perf_net_socket* sock, int pipe_fd, int64_t timeout)
{
PERF_LOCK(&self->lock);
if (self->do_reconnect) {
perf__dot_reconnect(sock);
self->do_reconnect = false;
}
if (self->is_ready) {
if (self->is_sending) {
uint16_t dnslen;
ssize_t n;
memcpy(&dnslen, self->sendbuf, 2);
dnslen = ntohs(dnslen);
n = SSL_write(self->ssl, self->sendbuf + self->sending, dnslen + 2 - self->sending);
if (n < 1) {
switch (SSL_get_error(self->ssl, n)) {
case SSL_ERROR_SYSCALL:
perf__dot_reconnect(sock);
PERF_UNLOCK(&self->lock);
return 0;
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
PERF_UNLOCK(&self->lock);
return 0;
default:
break;
}
perf_log_warning("SSL_write(): %s", ERR_error_string(SSL_get_error(self->ssl, n), 0));
PERF_UNLOCK(&self->lock);
return 0;
}
PERF_UNLOCK(&self->lock);
self->sending += n;
if (self->sending < dnslen + 2) {
return 0;
}
self->sending = 0;
self->is_sending = false;
if (sock->sent) {
sock->sent(sock, self->qid);
}
self->nqpc_sent++;
} else {
PERF_UNLOCK(&self->lock);
}
if (self->num_queries_per_conn && self->nqpc_sent >= self->num_queries_per_conn) {
if (!self->nqpc_ts) {
self->nqpc_ts = perf_get_time() + self->nqpc_timeout;
}
unsigned int r = ck_pr_load_uint(&self->nqpc_recv);
if (r >= self->nqpc_sent || perf_get_time() > self->nqpc_ts) {
PERF_LOCK(&self->lock);
perf__dot_reconnect(sock);
PERF_UNLOCK(&self->lock);
}
return 0;
}
return 1;
}
if (!self->is_conn_ready) {
switch (perf_os_waituntilanywritable(&sock, 1, pipe_fd, timeout)) {
case PERF_R_TIMEDOUT:
PERF_UNLOCK(&self->lock);
return -1;
case PERF_R_SUCCESS: {
int error = 0;
socklen_t len = (socklen_t)sizeof(error);
getsockopt(sock->fd, SOL_SOCKET, SO_ERROR, (void*)&error, &len);
if (error != 0) {
if (error == EINPROGRESS
#if EWOULDBLOCK != EAGAIN
|| error == EWOULDBLOCK
#endif
|| error == EAGAIN) {
PERF_UNLOCK(&self->lock);
return 0;
}
// unrecoverable error, reconnect
self->do_reconnect = true;
PERF_UNLOCK(&self->lock);
return 0;
}
break;
}
default:
PERF_UNLOCK(&self->lock);
return -1;
}
self->is_conn_ready = true;
}
int ret = SSL_connect(self->ssl);
if (!ret) {
// unrecoverable error, reconnect
self->do_reconnect = true;
PERF_UNLOCK(&self->lock);
return 0;
}
if (ret < 0) {
switch (SSL_get_error(self->ssl, ret)) {
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
break;
default:
// unrecoverable error, reconnect
self->do_reconnect = true;
}
PERF_UNLOCK(&self->lock);
return 0;
}
self->is_ready = true;
PERF_UNLOCK(&self->lock);
if (sock->event) {
sock->event(sock, self->conn_event, perf_get_time() - self->conn_ts);
self->conn_event = perf_socket_event_reconnected;
}
if (self->is_sending) {
return 0;
}
return 1;
}
static bool perf__dot_have_more(struct perf_net_socket* sock)
{
return self->have_more;
}
static void perf__dot_num_queries_per_conn(struct perf_net_socket* sock, size_t num_queries_per_conn, size_t timeout)
{
self->num_queries_per_conn = num_queries_per_conn;
self->nqpc_timeout = timeout;
}
struct perf_net_socket* perf_net_dot_opensocket(const perf_sockaddr_t* server, const perf_sockaddr_t* local, size_t bufsize, void* data, perf_net_sent_cb_t sent, perf_net_event_cb_t event)
{
struct perf__dot_socket* tmp = calloc(1, sizeof(struct perf__dot_socket)); // clang scan-build
struct perf_net_socket* sock = (struct perf_net_socket*)tmp;
if (!sock) {
perf_log_fatal("perf_net_dot_opensocket() out of memory");
return 0; // needed for clang scan build
}
sock->recv = perf__dot_recv;
sock->sendto = perf__dot_sendto;
sock->close = perf__dot_close;
sock->sockeq = perf__dot_sockeq;
sock->sockready = perf__dot_sockready;
sock->have_more = perf__dot_have_more;
sock->num_queries_per_conn = perf__dot_num_queries_per_conn;
sock->data = data;
sock->sent = sent;
sock->event = event;
self->server = *server;
self->local = *local;
self->bufsize = bufsize;
if (self->bufsize > 0) {
self->bufsize *= 1024;
}
self->conning_event = perf_socket_event_connecting;
self->conn_event = perf_socket_event_connected;
PERF_MUTEX_INIT(&self->lock);
if (!ssl_ctx) {
#ifdef HAVE_TLS_METHOD
if (!(ssl_ctx = SSL_CTX_new(TLS_method()))) {
perf_log_fatal("SSL_CTX_new(): %s", ERR_error_string(ERR_get_error(), 0));
}
if (!SSL_CTX_set_min_proto_version(ssl_ctx, TLS1_2_VERSION)) {
perf_log_fatal("SSL_CTX_set_min_proto_version(TLS1_2_VERSION): %s", ERR_error_string(ERR_get_error(), 0));
}
#else
if (!(ssl_ctx = SSL_CTX_new(SSLv23_client_method()))) {
perf_log_fatal("SSL_CTX_new(): %s", ERR_error_string(ERR_get_error(), 0));
}
#endif
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
}
perf__dot_connect(sock);
return sock;
}
|