-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathcoroutine_socket.h
564 lines (506 loc) Β· 15.9 KB
/
coroutine_socket.h
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#pragma once
#include "coroutine.h"
#include "connection.h"
#include "socks5.h"
#include <vector>
#include <string>
#define SW_DEFAULT_SOCKET_CONNECT_TIMEOUT 1
#define SW_DEFAULT_SOCKET_READ_TIMEOUT -1
#define SW_DEFAULT_SOCKET_WRITE_TIMEOUT -1
namespace swoole
{
enum swTimeout_type
{
SW_TIMEOUT_CONNECT = 1u << 1,
SW_TIMEOUT_READ = 1u << 2,
SW_TIMEOUT_WRITE = 1u << 3,
SW_TIMEOUT_RDWR = SW_TIMEOUT_READ | SW_TIMEOUT_WRITE,
SW_TIMEOUT_ALL = 0xff,
};
static constexpr enum swTimeout_type swTimeout_type_list[3] =
{
SW_TIMEOUT_CONNECT, SW_TIMEOUT_READ, SW_TIMEOUT_WRITE
};
}
namespace swoole { namespace coroutine {
//-------------------------------------------------------------------------------
class Socket
{
public:
static double default_connect_timeout;
static double default_read_timeout;
static double default_write_timeout;
swSocket *socket = nullptr;
int errCode = 0;
const char *errMsg = "";
std::string errString;
bool open_length_check = false;
bool open_eof_check = false;
bool http2 = false;
swProtocol protocol = {};
struct _swSocks5 *socks5_proxy = nullptr;
struct _http_proxy* http_proxy = nullptr;
#ifdef SW_USE_OPENSSL
bool open_ssl = false;
swSSL_option ssl_option = {};
#endif
Socket(int domain, int type, int protocol);
Socket(int _fd, int _domain, int _type, int _protocol);
Socket(enum swSocket_type type = SW_SOCK_TCP);
Socket(int _fd, enum swSocket_type _type);
~Socket();
bool connect(std::string host, int port, int flags = 0);
bool connect(const struct sockaddr *addr, socklen_t addrlen);
bool shutdown(int how = SHUT_RDWR);
bool cancel(const enum swEvent_type event);
bool close();
inline bool is_connect()
{
return activated && !closed;
}
bool check_liveness();
ssize_t peek(void *__buf, size_t __n);
ssize_t recv(void *__buf, size_t __n);
ssize_t send(const void *__buf, size_t __n);
ssize_t read(void *__buf, size_t __n);
ssize_t write(const void *__buf, size_t __n);
ssize_t recvmsg(struct msghdr *msg, int flags);
ssize_t sendmsg(const struct msghdr *msg, int flags);
ssize_t recv_all(void *__buf, size_t __n);
ssize_t send_all(const void *__buf, size_t __n);
ssize_t recv_packet(double timeout = 0);
bool poll(enum swEvent_type type);
Socket* accept(double timeout = 0);
bool bind(std::string address, int port = 0);
bool listen(int backlog = 0);
bool sendfile(const char *filename, off_t offset, size_t length);
ssize_t sendto(const char *address, int port, const void *__buf, size_t __n);
ssize_t recvfrom(void *__buf, size_t __n);
ssize_t recvfrom(void *__buf, size_t __n, struct sockaddr *_addr, socklen_t *_socklen);
#ifdef SW_USE_OPENSSL
bool ssl_handshake();
int ssl_verify(bool allow_self_signed);
bool ssl_accept();
bool ssl_check_context();
#endif
static inline enum swSocket_type convert_to_type(int domain, int type, int protocol = 0)
{
switch (domain)
{
case AF_INET:
return type == SOCK_STREAM ? SW_SOCK_TCP : SW_SOCK_UDP;
case AF_INET6:
return type == SOCK_STREAM ? SW_SOCK_TCP6 : SW_SOCK_UDP6;
case AF_UNIX:
return type == SOCK_STREAM ? SW_SOCK_UNIX_STREAM : SW_SOCK_UNIX_DGRAM;
default:
return SW_SOCK_TCP;
}
}
static inline enum swSocket_type convert_to_type(std::string &host)
{
if (host.compare(0, 6, "unix:/", 0, 6) == 0)
{
host = host.substr(sizeof("unix:") - 1);
host.erase(0, host.find_first_not_of('/') - 1);
return SW_SOCK_UNIX_STREAM;
}
else if (host.find(':') != std::string::npos)
{
return SW_SOCK_TCP6;
}
else
{
return SW_SOCK_TCP;
}
}
static inline void init_reactor(swReactor *reactor)
{
swReactor_set_handler(reactor, SW_FD_CORO_SOCKET | SW_EVENT_READ, readable_event_callback);
swReactor_set_handler(reactor, SW_FD_CORO_SOCKET | SW_EVENT_WRITE, writable_event_callback);
swReactor_set_handler(reactor, SW_FD_CORO_SOCKET | SW_EVENT_ERROR, error_event_callback);
}
inline enum swSocket_type get_type()
{
return type;
}
inline int get_sock_domain()
{
return sock_domain;
}
inline int get_sock_type()
{
return sock_type;
}
inline int get_sock_protocol()
{
return sock_protocol;
}
inline int get_fd()
{
return sock_fd;
}
inline int get_bind_port()
{
return bind_port;
}
bool getsockname(swSocketAddress *sa);
bool getpeername(swSocketAddress *sa);
const char* get_ip();
int get_port();
inline bool has_bound(const enum swEvent_type event = SW_EVENT_RDWR)
{
return get_bound_co(event) != nullptr;
}
inline Coroutine* get_bound_co(const enum swEvent_type event)
{
if (event & SW_EVENT_READ)
{
if (read_co)
{
return read_co;
}
}
if (event & SW_EVENT_WRITE)
{
if (write_co)
{
return write_co;
}
}
return nullptr;
}
inline long get_bound_cid(const enum swEvent_type event = SW_EVENT_RDWR)
{
Coroutine *co = get_bound_co(event);
return co ? co->get_cid() : 0;
}
inline void check_bound_co(const enum swEvent_type event)
{
long cid = get_bound_cid(event);
if (sw_unlikely(cid))
{
swFatalError(
SW_ERROR_CO_HAS_BEEN_BOUND,
"Socket#%d has already been bound to another coroutine#%ld, "
"%s of the same socket in coroutine#%ld at the same time is not allowed",
sock_fd, cid,
(event == SW_EVENT_READ ? "reading" : (event == SW_EVENT_WRITE ? "writing" :
(read_co && write_co ? "reading or writing" : (read_co ? "reading" : "writing")))),
Coroutine::get_current_cid()
);
}
}
inline void set_err(int e)
{
errCode = errno = e;
errMsg = e ? swoole_strerror(e) : "";
}
inline void set_err(int e, const char *s)
{
errCode = errno = e;
errMsg = s;
}
inline void set_err(int e, std::string s)
{
errCode = errno = e;
errString = s;
errMsg = errString.c_str();
}
/* set connect read write timeout */
inline void set_timeout(double timeout, int type = SW_TIMEOUT_ALL)
{
if (timeout == 0)
{
return;
}
if (type & SW_TIMEOUT_CONNECT)
{
connect_timeout = timeout;
}
if (type & SW_TIMEOUT_READ)
{
read_timeout = timeout;
}
if (type & SW_TIMEOUT_WRITE)
{
write_timeout = timeout;
}
}
inline void set_timeout(struct timeval *timeout, int type = SW_TIMEOUT_ALL)
{
set_timeout((double) timeout->tv_sec + ((double) timeout->tv_usec / 1000 / 1000), type);
}
inline double get_timeout(enum swTimeout_type type = SW_TIMEOUT_ALL)
{
SW_ASSERT_1BYTE(type);
if (type == SW_TIMEOUT_CONNECT)
{
return connect_timeout;
}
else if (type == SW_TIMEOUT_READ)
{
return read_timeout;
}
else // if (type == SW_TIMEOUT_WRITE)
{
return write_timeout;
}
}
inline bool set_option(int level, int optname, int optval)
{
if (setsockopt(sock_fd, level, optname, &optval, sizeof(optval)) != 0)
{
swSysWarn("setsockopt(%d, %d, %d, %d) failed", sock_fd, level, optname, optval);
return false;
}
return true;
}
inline swString* get_read_buffer()
{
if (sw_unlikely(!read_buffer))
{
read_buffer = swString_new(SW_BUFFER_SIZE_BIG);
}
return read_buffer;
}
inline swString* get_write_buffer()
{
if (sw_unlikely(!write_buffer))
{
write_buffer = swString_new(SW_BUFFER_SIZE_BIG);
}
return write_buffer;
}
inline swString *pop_read_buffer() {
if (sw_unlikely(!read_buffer)) {
return nullptr;
}
auto tmp = read_buffer;
read_buffer = nullptr;
return tmp;
}
inline swString *pop_write_buffer() {
if (sw_unlikely(!write_buffer)) {
return nullptr;
}
auto tmp = write_buffer;
write_buffer = nullptr;
return tmp;
}
#ifdef SW_USE_OPENSSL
inline bool is_ssl_enable()
{
return socket && socket->ssl != NULL;
}
bool ssl_shutdown();
#endif
private:
enum swSocket_type type;
int sock_domain = 0;
int sock_type = 0;
int sock_protocol = 0;
int sock_fd = -1;
Coroutine *read_co = nullptr;
Coroutine *write_co = nullptr;
#ifdef SW_USE_OPENSSL
enum swEvent_type want_event = SW_EVENT_NULL;
#endif
std::string connect_host;
int connect_port = 0;
std::string bind_address;
int bind_port = 0;
int backlog = 0;
double connect_timeout = default_connect_timeout;
double read_timeout = default_read_timeout;
double write_timeout = default_write_timeout;
swTimer_node *read_timer = nullptr;
swTimer_node *write_timer = nullptr;
swString *read_buffer = nullptr;
swString *write_buffer = nullptr;
swSocketAddress bind_address_info = {};
#ifdef SW_USE_OPENSSL
std::string ssl_host_name;
SSL_CTX *ssl_context = nullptr;
#endif
bool activated = true;
bool shutdown_read = false;
bool shutdown_write = false;
bool closed = false;
static void timer_callback(swTimer *timer, swTimer_node *tnode);
static int readable_event_callback(swReactor *reactor, swEvent *event);
static int writable_event_callback(swReactor *reactor, swEvent *event);
static int error_event_callback(swReactor *reactor, swEvent *event);
Socket(int _fd, swSocketAddress *addr, Socket *socket);
inline void init_sock_type(enum swSocket_type _type);
inline bool init_sock();
void init_reactor_socket(int fd);
inline void init_options()
{
if (type == SW_SOCK_TCP || type == SW_SOCK_TCP6)
{
set_option(IPPROTO_TCP, TCP_NODELAY, 1);
}
protocol.package_length_type = 'N';
protocol.package_length_size = 4;
protocol.package_length_offset = 0;
protocol.package_body_offset = 0;
protocol.package_max_length = SW_BUFFER_INPUT_SIZE;
}
bool add_event(const enum swEvent_type event);
bool wait_event(const enum swEvent_type event, const void **__buf = nullptr, size_t __n = 0);
inline bool is_available(const enum swEvent_type event)
{
if (event != SW_EVENT_NULL)
{
check_bound_co(event);
}
if (sw_unlikely(closed))
{
set_err(ECONNRESET);
return false;
}
return true;
}
// TODO: move to client.cc
bool socks5_handshake();
bool http_proxy_handshake();
class timer_controller
{
public:
timer_controller(swTimer_node **timer_pp, double timeout, Socket *sock, swTimerCallback callback) :
timer_pp(timer_pp), timeout(timeout), socket_(sock), callback(callback)
{
}
bool start()
{
if (timeout != 0 && !*timer_pp)
{
enabled = true;
if (timeout > 0)
{
*timer_pp = swoole_timer_add((long) (timeout * 1000), SW_FALSE, callback, socket_);
return *timer_pp != nullptr;
}
else // if (timeout < 0)
{
*timer_pp = (swTimer_node *) -1;
}
}
return true;
}
~timer_controller()
{
if (enabled && *timer_pp)
{
if (*timer_pp != (swTimer_node *) -1)
{
swoole_timer_del(*timer_pp);
}
*timer_pp = nullptr;
}
}
private:
bool enabled = false;
swTimer_node** timer_pp;
double timeout;
Socket *socket_;
swTimerCallback callback;
};
public:
class timeout_setter
{
public:
timeout_setter(Socket *socket, double timeout, const enum swTimeout_type type) :
socket_(socket), timeout(timeout), type(type)
{
if (timeout == 0)
{
return;
}
for (uint8_t i = 0; i < SW_ARRAY_SIZE(swTimeout_type_list); i++)
{
if (type & swTimeout_type_list[i])
{
original_timeout[i] = socket->get_timeout(swTimeout_type_list[i]);
if (timeout != original_timeout[i])
{
socket->set_timeout(timeout, swTimeout_type_list[i]);
}
}
}
}
~timeout_setter()
{
if (timeout == 0)
{
return;
}
for (uint8_t i = 0; i < SW_ARRAY_SIZE(swTimeout_type_list); i++)
{
if (type & swTimeout_type_list[i])
{
if (timeout != original_timeout[i])
{
socket_->set_timeout(original_timeout[i], swTimeout_type_list[i]);
}
}
}
}
protected:
Socket *socket_;
double timeout;
enum swTimeout_type type;
double original_timeout[sizeof(swTimeout_type_list)] = {};
};
class timeout_controller: public timeout_setter
{
public:
timeout_controller(Socket *socket, double timeout, const enum swTimeout_type type) :
timeout_setter(socket, timeout, type)
{
}
inline bool has_timedout(const enum swTimeout_type type)
{
SW_ASSERT_1BYTE(type);
if (timeout > 0)
{
if (sw_unlikely(startup_time == 0))
{
startup_time = swoole_microtime();
}
else
{
double used_time = swoole_microtime() - startup_time;
if (sw_unlikely(timeout - used_time < SW_TIMER_MIN_SEC))
{
socket_->set_err(ETIMEDOUT);
return true;
}
socket_->set_timeout(timeout - used_time, type);
}
}
return false;
}
protected:
double startup_time = 0;
};
};
std::vector<std::string> dns_lookup(const char *domain, double timeout = 2.0);
//-------------------------------------------------------------------------------
}}