This repository has been archived by the owner on Sep 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
std.c
640 lines (517 loc) · 11.6 KB
/
std.c
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/*
* Copyright (c) 1998-2012 Caucho Technology -- all rights reserved
*
* @author Scott Ferguson
*/
#define _GNU_SOURCE
#include <sys/types.h>
#ifdef WIN32
#ifndef _WINSOCKAPI_
#define _WINSOCKAPI_
#endif
#include <windows.h>
#include <winsock2.h>
#else
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <unistd.h>
#ifdef POLL
#include <sys/poll.h>
#else
#include <sys/select.h>
#endif
#endif
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
/* probably system-dependent */
#include <jni.h>
#include <errno.h>
#include <signal.h>
#ifdef linux
#include <sys/uio.h>
#endif
#include "resin_os.h"
int std_init(connection_t *conn);
static int std_read(connection_t *conn, char *buf, int len, int timeout);
static int std_read_nonblock(connection_t *conn, char *buf, int len);
static int std_write(connection_t *conn, char *buf, int len);
static int std_write_nonblock(connection_t *conn, char *buf, int len);
int conn_close(connection_t *conn);
static void std_free(connection_t *conn);
static int std_read_client_certificate(connection_t *conn, char *buf, int len);
struct connection_ops_t std_ops = {
std_init,
std_read,
std_read_nonblock,
std_write,
std_write_nonblock,
conn_close,
std_read_client_certificate,
std_free,
};
static int
write_exception_status(connection_t *conn, int error)
{
if (error == EAGAIN || error == EWOULDBLOCK || error == EINTR) {
if (conn->jni_env) {
resin_printf_exception(conn->jni_env, "com/caucho/vfs/ClientDisconnectException",
"timeout fd=%d errno=%d\n", conn->fd, error);
}
return TIMEOUT_EXN;
}
else if (error == EPIPE || error == ECONNRESET) {
if (conn->jni_env) {
resin_printf_exception(conn->jni_env, "com/caucho/vfs/ClientDisconnectException",
"Client disconnect fd=%d errno=%d\n",
conn->fd, error);
}
return DISCONNECT_EXN;
}
else {
return -1;
}
}
static int
read_exception_status(connection_t *conn, int error)
{
if (error == EAGAIN || error == EWOULDBLOCK || error == EINTR) {
return TIMEOUT_EXN;
}
else if (error == EPIPE || error == ECONNRESET) {
return -1;
}
else {
return -1;
}
}
static int
resin_tcp_set_recv_timeout(connection_t *conn, int timeout_ms)
{
int fd = conn->fd;
int result = 0;
struct timeval timeout;
#ifdef HAS_SOCK_TIMEOUT
timeout.tv_sec = timeout_ms / 1000;
timeout.tv_usec = timeout_ms % 1000 * 1000;
result = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,
(char *) &timeout, sizeof(timeout));
#endif
return result;
}
static int
std_read_nonblock(connection_t *conn, char *buf, int len)
{
int fd;
int result;
int retry = 16;
if (! conn)
return -1;
fd = conn->fd;
if (fd < 0)
return -1;
do {
result = recv(fd, buf, len, 0);
} while (result < 0 && errno == EINTR && retry-- > 0);
return result;
}
#ifdef POLL
int
poll_read(int fd, int ms)
{
struct pollfd pollfd[1];
int result;
int rd_hup = 0;
int retry = 16;
if (ms < 0) {
errno = ECONNRESET;
return -1;
}
#ifdef POLLRDHUP
/* the other end has hung up */
rd_hup = POLLRDHUP;
#endif
pollfd[0].fd = fd;
pollfd[0].events = POLLIN|POLLPRI|rd_hup;
pollfd[0].revents = 0;
do {
result = poll(pollfd, 1, ms);
} while (result < 0 && errno == EINTR && retry-- > 0);
if (result <= 0) {
return result;
}
else if ((pollfd[0].revents & rd_hup) != 0) {
errno = ECONNRESET;
return -1;
}
else if ((pollfd[0].revents & (POLLIN|POLLPRI)) == 0) {
return 1;
}
else {
return result;
}
}
int
poll_write(int fd, int ms)
{
struct pollfd pollfd[1];
pollfd[0].fd = fd;
pollfd[0].events = POLLOUT;
pollfd[0].revents = 0;
return poll(pollfd, 1, ms);
}
#else /* select */
int
poll_read(int fd, int ms)
{
fd_set read_set;
struct timeval timeout;
int result;
FD_ZERO(&read_set);
FD_SET(fd, &read_set);
timeout.tv_sec = ms / 1000;
timeout.tv_usec = (ms % 1000) * 1000;
result = select(fd + 1, &read_set, 0, 0, &timeout);
return result;
}
int
poll_write(int fd, int ms)
{
fd_set write_set;
struct timeval timeout;
FD_ZERO(&write_set);
FD_SET(fd, &write_set);
timeout.tv_sec = ms / 1000;
timeout.tv_usec = (ms % 1000) * 1000;
return select(fd + 1, 0, &write_set, 0, &timeout);
}
#endif
static int
calculate_poll_result(connection_t *conn, int poll_result)
{
if (poll_result == 0) {
return TIMEOUT_EXN;
}
else if (poll_result < 0 && errno != EINTR) {
return read_exception_status(conn, errno);
}
else {
return poll_result;
}
}
static int
std_read(connection_t *conn, char *buf, int len, int timeout)
{
int fd;
int result;
int retry = 3;
int poll_result;
int poll_timeout;
if (! conn) {
return -1;
}
fd = conn->fd;
if (fd <= 0 || conn->is_read_shutdown) {
return -1;
}
if (timeout >= 0) {
poll_timeout = timeout;
}
else {
poll_timeout = conn->socket_timeout;
}
if (timeout > 0 && conn->is_recv_timeout) {
if (conn->recv_timeout != poll_timeout) {
conn->recv_timeout = poll_timeout;
resin_tcp_set_recv_timeout(conn, poll_timeout);
}
}
else {
poll_result = poll_read(fd, poll_timeout);
if (poll_result <= 0) {
return calculate_poll_result(conn, poll_result);
}
}
do {
/* recv returns 0 on end of file */
result = recv(fd, buf, len, 0);
// fprintf(stderr, "rcv %d\n", result);
if (result > 0) {
return result;
}
else if (result == 0) {
/* recv returns 0 on end of file */
return -1;
}
if (errno == EINTR) {
/* EAGAIN is returned by a timeout */
poll_result = poll_read(fd, conn->socket_timeout);
if (poll_result <= 0) {
return calculate_poll_result(conn, poll_result);
}
}
else {
return read_exception_status(conn, errno);
}
} while (retry-- >= 0);
if (result > 0) {
return result;
}
else if (result == 0) {
return -1;
}
else {
return read_exception_status(conn, errno);
}
}
static int
std_write(connection_t *conn, char *buf, int len)
{
int fd;
int result;
int retry = 5;
int poll_result;
int error;
if (! conn)
return -1;
fd = conn->fd;
if (fd <= 0) {
return -1;
}
conn->sent_data = 1;
if (! conn->is_recv_timeout && poll_write(fd, conn->socket_timeout) == 0) {
return TIMEOUT_EXN;
}
do {
result = send(fd, buf, len, 0);
if (result >= 0)
return result;
error = errno;
if (errno == EINTR || errno == EAGAIN) {
poll_result = poll_write(fd, conn->socket_timeout);
if (poll_result == 0) {
return TIMEOUT_EXN;
}
else if (poll_result < 0 && errno != EINTR) {
return write_exception_status(conn, errno);
}
}
else {
return write_exception_status(conn, errno);
}
} while (retry-- >= 0);
return write_exception_status(conn, error);
}
static int
std_write_nonblock(connection_t *conn, char *buf, int len)
{
int fd;
int result;
int retry = 16;
if (! conn)
return -1;
fd = conn->fd;
if (fd <= 0) {
return -1;
}
#ifndef O_NONBLOCK
if (poll_write(fd, 0) <= 0)
return 0;
#endif
do {
result = send(fd, buf, len, 0);
} while (result < 0 && errno == EINTR && retry-- > 0);
return result;
}
void
std_free(connection_t *conn)
{
}
int
conn_close(connection_t *conn)
{
int fd;
int pipe0;
int pipe1;
if (! conn) {
return -1;
}
fd = conn->fd;
conn->fd = -1;
pipe0 = conn->pipe[0];
pipe1 = conn->pipe[1];
conn->pipe[0] = 0;
conn->pipe[1] = 0;
if (fd > 0) {
closesocket(fd);
}
if (pipe0 > 0) {
close(pipe0);
}
if (pipe1 > 0) {
close(pipe1);
}
return 1;
}
int
std_accept(server_socket_t *ss, connection_t *conn)
{
int fd;
int sock = -1;
char sin_data[256];
struct sockaddr *sin = (struct sockaddr *) &sin_data;
unsigned int sin_len;
int poll_result;
int result;
if (! ss || ! conn) {
return 0;
}
fd = ss->fd;
if (fd <= 0) {
return 0;
}
if (conn->fd > 0) {
return 0;
}
/*
memset(sin_data, 0, sizeof(sin_data));
sin = (struct sockaddr *) &sin_data;
sin_len = sizeof(sin_data);
*/
#ifdef WIN32
WaitForSingleObject(ss->accept_lock, INFINITE);
fd = ss->fd;
if (fd < 0) {
ReleaseMutex(ss->accept_lock);
return 0;
}
#endif
sin_len = sizeof(conn->client_data);
memset(conn->client_data, 0, sin_len);
conn->client_sin = (struct sockaddr *) &conn->client_data;
sock = accept(fd, conn->client_sin, &sin_len);
#ifdef WIN32
ReleaseMutex(ss->accept_lock);
#endif
if (sock < 0) {
return 0;
}
else if (sock == 0) {
fprintf(stderr, "unexpected file descriptor %d\n", sock);
return 0;
}
conn->ss = ss;
conn->fd = sock;
return 1;
}
int
std_init(connection_t *conn)
{
server_socket_t *ss = conn->ss;
int sock = conn->fd;
struct timeval timeout;
int sin_len;
conn->socket_timeout = ss->conn_socket_timeout;
if (ss->tcp_no_delay) {/* && ! ss->tcp_cork) { */
int flag = 1;
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
}
conn->tcp_cork = 0;
#ifdef TCP_CORK
if (ss->tcp_cork) {
int flag = 1;
int result;
/*
result = setsockopt(sock, IPPROTO_TCP, TCP_CORK, (char *) &flag, sizeof(int));*/
conn->tcp_cork = 1;
}
#endif
#ifdef SO_KEEPALIVE
if (ss->tcp_keepalive) {
int flag = 1;
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
(char *) &flag, sizeof(int));
}
#endif
conn->is_recv_timeout = 0;
#ifdef HAS_SOCK_TIMEOUT
timeout.tv_sec = conn->socket_timeout / 1000;
timeout.tv_usec = conn->socket_timeout % 1000 * 1000;
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
(char *) &timeout, sizeof(timeout)) == 0) {
conn->is_recv_timeout = 1;
conn->recv_timeout = conn->socket_timeout;
timeout.tv_sec = conn->socket_timeout / 1000;
timeout.tv_usec = conn->socket_timeout % 1000 * 1000;
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
(char *) &timeout, sizeof(timeout));
}
#endif
conn->ssl_lock = &ss->ssl_lock;
/*
conn->ssl_sock = 0;
conn->ops = &std_ops;
*/
/*
conn->client_sin = (struct sockaddr *) &conn->client_data;
memcpy(conn->client_sin, sin, sizeof(conn->client_data));
*/
conn->is_init = 0;
conn->server_sin = (struct sockaddr *) &conn->server_data;
sin_len = sizeof(conn->server_data);
getsockname(sock, conn->server_sin, &sin_len);
/*
conn->ssl_cipher = 0;
conn->ssl_bits = 0;
*/
return 1;
}
void
std_close_ss(server_socket_t *ss)
{
int fd;
char server_data[128];
struct sockaddr *server_sin = (struct sockaddr *) server_data;
unsigned int sin_len;
int result;
if (! ss)
return;
fd = ss->fd;
ss->fd = -1;
if (fd < 0)
return;
sin_len = sizeof(server_data);
if (! getsockname(fd, server_sin, &sin_len)) {
int retry;
/* probably should check for 0 socket name for local host*/
/* connect enough times to clear the threads waiting for a connection */
for (retry = 20; retry >= 0; retry--) {
int sock = socket(AF_INET, SOCK_STREAM, 0);
int flags;
int result;
if (sock < 0)
break;
#ifdef O_NONBLOCK
flags = fcntl(sock, F_GETFL);
fcntl(sock, F_SETFL, O_NONBLOCK|flags);
#endif
result = connect(sock, server_sin, sin_len);
closesocket(sock);
if (result < 0)
break;
}
}
result = closesocket(fd);
/* fprintf(stderr, "closesocket: %d %p\n", fd, ss->context); */
}
static int
std_read_client_certificate(connection_t *conn, char *buffer, int length)
{
return -1;
}