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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
|
/* $OpenBSD: io.c,v 1.1.1.1 2018/04/27 16:14:36 eric Exp $ */
/*
* Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include "io.h"
#include "iobuf.h"
#include "log.h"
#ifdef IO_SSL
#include <openssl/err.h>
#include <openssl/ssl.h>
#endif
enum {
IO_STATE_DOWN,
IO_STATE_UP,
IO_STATE_CONNECT,
IO_STATE_CONNECT_TLS,
IO_STATE_ACCEPT_TLS
};
#define IO_PAUSE_IN IO_IN
#define IO_PAUSE_OUT IO_OUT
#define IO_READ 0x0100
#define IO_WRITE 0x0200
#define IO_RW (IO_READ | IO_WRITE)
#define IO_RESET 0x1000
#define IO_HELD 0x2000
struct io {
int sock;
void *arg;
void (*cb)(struct io*, int, void *);
struct iobuf iobuf;
size_t lowat;
int timeout;
int flags;
int state;
struct event ev;
void *tls;
const char *error; /* only valid immediately on callback */
struct sockaddr *bind;
struct addrinfo *ai; /* for connecting */
};
static const char* io_strflags(int);
static const char* io_strevents(short);
static void io_reload(struct io *);
static void io_reset(struct io *, short, void (*)(int, short, void*));
static void io_frame_enter(const char *, struct io *, int);
static void io_frame_leave(struct io *);
static void io_hold(struct io *);
static void io_release(struct io *);
static void io_callback(struct io*, int);
static void io_dispatch(int, short, void *);
static void io_dispatch_connect(int, short, void *);
static int io_connect_next(struct io *);
#ifdef IO_SSL
void ssl_error(const char *); /* XXX external */
static const char* io_ssl_error(void);
static void io_dispatch_accept_tls(int, short, void *);
static void io_dispatch_connect_tls(int, short, void *);
static void io_dispatch_read_tls(int, short, void *);
static void io_dispatch_write_tls(int, short, void *);
static void io_reload_tls(struct io *io);
#endif
static struct io *current = NULL;
static long long unsigned frame = 0;
static int _io_trace = 0;
static const char *states[] = {
"DOWN",
"UP",
"CONNECT",
"CONNECT_TLS",
"ACCEPT_TLS"
};
#define io_debug(args...) do { if (_io_trace) log_debug(args); } while(0)
#define IO_READING(io) (((io)->flags & IO_RW) != IO_WRITE)
#define IO_WRITING(io) (((io)->flags & IO_RW) != IO_READ)
void
io_trace(int on)
{
_io_trace = on;
}
const char*
io_strio(struct io *io)
{
static char buf[128];
char ssl[128];
ssl[0] = '\0';
#ifdef IO_SSL
if (io->tls) {
(void)snprintf(ssl, sizeof ssl, " ssl=%s:%s:%d",
SSL_get_version(io->tls),
SSL_get_cipher_name(io->tls),
SSL_get_cipher_bits(io->tls, NULL));
}
#endif
(void)snprintf(buf, sizeof buf,
"<io:%p st=%s, fd=%d to=%d fl=%s%s ib=%zu ob=%zu>",
io, states[io->state], io->sock, io->timeout,
io_strflags(io->flags), ssl, io_datalen(io), io_queued(io));
return buf;
}
const char*
io_strevent(int evt)
{
static char buf[32];
switch (evt) {
case IO_CONNECTED:
return "IO_CONNECTED";
case IO_TLSREADY:
return "IO_TLSREADY";
case IO_DATAIN:
return "IO_DATAIN";
case IO_LOWAT:
return "IO_LOWAT";
case IO_CLOSED:
return "IO_CLOSED";
case IO_DISCONNECTED:
return "IO_DISCONNECTED";
case IO_TIMEOUT:
return "IO_TIMEOUT";
case IO_ERROR:
return "IO_ERROR";
case IO_TLSERROR:
return "IO_TLSERROR";
default:
(void)snprintf(buf, sizeof(buf), "IO_? %d", evt);
return buf;
}
}
struct io *
io_new(void)
{
struct io *io;
io = calloc(1, sizeof(*io));
if (io == NULL)
return NULL;
iobuf_init(&io->iobuf, 0, 0);
io->sock = -1;
io->timeout = -1;
return io;
}
void
io_free(struct io *io)
{
io_debug("%s(%p)", __func__, io);
/* the current io is virtually dead */
if (io == current)
current = NULL;
#ifdef IO_SSL
if (io->tls) {
SSL_free(io->tls);
io->tls = NULL;
}
#endif
if (io->ai)
freeaddrinfo(io->ai);
if (event_initialized(&io->ev))
event_del(&io->ev);
if (io->sock != -1) {
(void)close(io->sock);
io->sock = -1;
}
iobuf_clear(&io->iobuf);
free(io->bind);
free(io);
}
int
io_set_callback(struct io *io, void(*cb)(struct io *, int, void *), void *arg)
{
io->cb = cb;
io->arg = arg;
return 0;
}
int
io_set_bindaddr(struct io *io, const struct sockaddr *sa)
{
struct sockaddr *t;
if (io->state != IO_STATE_DOWN) {
errno = EISCONN;
return -1;
}
t = malloc(sa->sa_len);
if (t == NULL)
return -1;
memmove(t, sa, sa->sa_len);
free(io->bind);
io->bind = t;
return 0;
}
int
io_set_bufsize(struct io *io, size_t sz)
{
errno = ENOSYS;
return -1;
}
void
io_set_timeout(struct io *io, int msec)
{
io_debug("%s(%p, %d)", __func__, io, msec);
io->timeout = msec;
}
void
io_set_lowat(struct io *io, size_t lowat)
{
io_debug("%s(%p, %zu)", __func__, io, lowat);
io->lowat = lowat;
}
const char *
io_error(struct io *io)
{
const char *e;
e = io->error;
io->error = NULL;
return e;
}
int
io_fileno(struct io *io)
{
return io->sock;
}
int
io_attach(struct io *io, int sock)
{
if (io->state != IO_STATE_DOWN) {
errno = EISCONN;
return -1;
}
io->state = IO_STATE_UP;
io->sock = sock;
io_reload(io);
return 0;
}
int
io_detach(struct io *io)
{
errno = ENOSYS;
return -1;
}
int
io_close(struct io *io)
{
errno = ENOSYS;
return -1;
}
int
io_connect(struct io *io, struct addrinfo *ai)
{
if (ai == NULL) {
errno = EINVAL;
fatal("%s", __func__);
return -1;
}
if (io->state != IO_STATE_DOWN) {
freeaddrinfo(ai);
errno = EISCONN;
fatal("%s", __func__);
return -1;
}
io->ai = ai;
return io_connect_next(io);
}
int
io_disconnect(struct io *io)
{
errno = ENOSYS;
fatal("%s", __func__);
return -1;
}
int
io_starttls(struct io *io, void *ssl)
{
#ifdef IO_SSL
int mode;
mode = io->flags & IO_RW;
if (mode == 0 || mode == IO_RW)
fatalx("%s: full-duplex or unset", __func__);
if (io->tls)
fatalx("%s: SSL already started", __func__);
io->tls = ssl;
if (SSL_set_fd(io->tls, io->sock) == 0) {
ssl_error("io_start_tls:SSL_set_fd");
return -1;
}
if (mode == IO_WRITE) {
io->state = IO_STATE_CONNECT_TLS;
SSL_set_connect_state(io->tls);
io_reset(io, EV_WRITE, io_dispatch_connect_tls);
} else {
io->state = IO_STATE_ACCEPT_TLS;
SSL_set_accept_state(io->tls);
io_reset(io, EV_READ, io_dispatch_accept_tls);
}
return 0;
#else
errno = ENOSYS;
return -1;
#endif
}
void
io_pause(struct io *io, int dir)
{
io_debug("%s(%p, %x)", __func__, io, dir);
io->flags |= dir & (IO_IN | IO_OUT);
io_reload(io);
}
void
io_resume(struct io *io, int dir)
{
io_debug("%s(%p, %x)", __func__, io, dir);
io->flags &= ~(dir & (IO_IN | IO_OUT));
io_reload(io);
}
void
io_set_read(struct io *io)
{
int mode;
io_debug("%s(%p)", __func__, io);
mode = io->flags & IO_RW;
if (!(mode == 0 || mode == IO_WRITE))
fatalx("%s: full-duplex or reading", __func__);
io->flags &= ~IO_RW;
io->flags |= IO_READ;
io_reload(io);
}
void
io_set_write(struct io *io)
{
int mode;
io_debug("%s(%p)", __func__, io);
mode = io->flags & IO_RW;
if (!(mode == 0 || mode == IO_READ))
fatalx("%s: full-duplex or writing", __func__);
io->flags &= ~IO_RW;
io->flags |= IO_WRITE;
io_reload(io);
}
int
io_write(struct io *io, const void *buf, size_t len)
{
int r;
r = iobuf_queue(&io->iobuf, buf, len);
io_reload(io);
return r;
}
int
io_writev(struct io *io, const struct iovec *iov, int iovcount)
{
int r;
r = iobuf_queuev(&io->iobuf, iov, iovcount);
io_reload(io);
return r;
}
int
io_print(struct io *io, const char *s)
{
return io_write(io, s, strlen(s));
}
int
io_printf(struct io *io, const char *fmt, ...)
{
va_list ap;
int r;
va_start(ap, fmt);
r = io_vprintf(io, fmt, ap);
va_end(ap);
return r;
}
int
io_vprintf(struct io *io, const char *fmt, va_list ap)
{
char *buf;
int len;
len = vasprintf(&buf, fmt, ap);
if (len == -1)
return -1;
len = io_write(io, buf, len);
free(buf);
return len;
}
size_t
io_queued(struct io *io)
{
return iobuf_queued(&io->iobuf);
}
void *
io_data(struct io *io)
{
return iobuf_data(&io->iobuf);
}
size_t
io_datalen(struct io *io)
{
return iobuf_len(&io->iobuf);
}
char *
io_getline(struct io *io, size_t *sz)
{
return iobuf_getline(&io->iobuf, sz);
}
void
io_drop(struct io *io, size_t sz)
{
return iobuf_drop(&io->iobuf, sz);
}
const char*
io_strflags(int flags)
{
static char buf[64];
buf[0] = '\0';
switch (flags & IO_RW) {
case 0:
(void)strlcat(buf, "rw", sizeof buf);
break;
case IO_READ:
(void)strlcat(buf, "R", sizeof buf);
break;
case IO_WRITE:
(void)strlcat(buf, "W", sizeof buf);
break;
case IO_RW:
(void)strlcat(buf, "RW", sizeof buf);
break;
}
if (flags & IO_PAUSE_IN)
(void)strlcat(buf, ",F_PI", sizeof buf);
if (flags & IO_PAUSE_OUT)
(void)strlcat(buf, ",F_PO", sizeof buf);
return buf;
}
const char*
io_strevents(short ev)
{
static char buf[64];
char buf2[16];
int n;
n = 0;
buf[0] = '\0';
if (ev == 0) {
(void)strlcat(buf, "<NONE>", sizeof(buf));
return buf;
}
if (ev & EV_TIMEOUT) {
(void)strlcat(buf, "EV_TIMEOUT", sizeof(buf));
ev &= ~EV_TIMEOUT;
n++;
}
if (ev & EV_READ) {
if (n)
(void)strlcat(buf, "|", sizeof(buf));
(void)strlcat(buf, "EV_READ", sizeof(buf));
ev &= ~EV_READ;
n++;
}
if (ev & EV_WRITE) {
if (n)
(void)strlcat(buf, "|", sizeof(buf));
(void)strlcat(buf, "EV_WRITE", sizeof(buf));
ev &= ~EV_WRITE;
n++;
}
if (ev & EV_SIGNAL) {
if (n)
(void)strlcat(buf, "|", sizeof(buf));
(void)strlcat(buf, "EV_SIGNAL", sizeof(buf));
ev &= ~EV_SIGNAL;
n++;
}
if (ev) {
if (n)
(void)strlcat(buf, "|", sizeof(buf));
(void)strlcat(buf, "EV_?=0x", sizeof(buf));
(void)snprintf(buf2, sizeof(buf2), "%hx", ev);
(void)strlcat(buf, buf2, sizeof(buf));
}
return buf;
}
/*
* Setup the necessary events as required by the current io state,
* honouring duplex mode and i/o pause.
*/
static void
io_reload(struct io *io)
{
short events;
/* The io will be reloaded at release time. */
if (io->flags & IO_HELD)
return;
/* Do nothing if no socket. */
if (io->sock == -1)
return;
#ifdef IO_SSL
if (io->tls) {
io_reload_tls(io);
return;
}
#endif
io_debug("%s(%p)", __func__, io);
events = 0;
if (IO_READING(io) && !(io->flags & IO_PAUSE_IN))
events = EV_READ;
if (IO_WRITING(io) && !(io->flags & IO_PAUSE_OUT) && io_queued(io))
events |= EV_WRITE;
io_reset(io, events, io_dispatch);
}
static void
io_reset(struct io *io, short events, void (*dispatch)(int, short, void*))
{
struct timeval tv, *ptv;
io_debug("%s(%p, %s, %p) -> %s", __func__, io,
io_strevents(events), dispatch, io_strio(io));
/*
* Indicate that the event has already been reset so that reload
* is not called on frame_leave.
*/
io->flags |= IO_RESET;
if (event_initialized(&io->ev))
event_del(&io->ev);
/*
* The io is paused by the user, so we don't want the timeout to be
* effective.
*/
if (events == 0)
return;
event_set(&io->ev, io->sock, events, dispatch, io);
if (io->timeout >= 0) {
tv.tv_sec = io->timeout / 1000;
tv.tv_usec = (io->timeout % 1000) * 1000;
ptv = &tv;
} else
ptv = NULL;
event_add(&io->ev, ptv);
}
static void
io_frame_enter(const char *where, struct io *io, int ev)
{
io_debug("io: BEGIN %llu", frame);
io_debug("%s(%s, %s, %s)", __func__, where, io_strevents(ev),
io_strio(io));
if (current)
fatalx("%s: interleaved frames", __func__);
current = io;
io_hold(io);
}
static void
io_frame_leave(struct io *io)
{
io_debug("%s(%llu)", __func__, frame);
if (current && current != io)
fatalx("%s: io mismatch", __func__);
/* The io has been cleared. */
if (current == NULL)
goto done;
/*
* TODO: There is a possible optimization there:
* In a typical half-duplex request/response scenario,
* the io is waiting to read a request, and when done, it queues
* the response in the output buffer and goes to write mode.
* There, the write event is set and will be triggered in the next
* event frame. In most case, the write call could be done
* immediately as part of the last read frame, thus avoiding to go
* through the event loop machinery. So, as an optimisation, we
* could detect that case here and force an event dispatching.
*/
/* Reload the io if it has not been reset already. */
io_release(io);
current = NULL;
done:
io_debug("io: END %llu", frame);
frame += 1;
}
static void
io_hold(struct io *io)
{
io_debug("%s(%p)", __func__, io);
if (io->flags & IO_HELD)
fatalx("%s: already held", __func__);
io->flags &= ~IO_RESET;
io->flags |= IO_HELD;
}
static void
io_release(struct io *io)
{
io_debug("%s(%p)", __func__, io);
if (!(io->flags & IO_HELD))
fatalx("%s: not held", __func__);
io->flags &= ~IO_HELD;
if (!(io->flags & IO_RESET))
io_reload(io);
}
static void
io_callback(struct io *io, int evt)
{
io_debug("%s(%s, %s)", __func__, io_strio(io), io_strevent(evt));
io->cb(io, evt, io->arg);
}
static void
io_dispatch(int fd, short ev, void *arg)
{
struct io *io = arg;
size_t w;
ssize_t n;
int saved_errno;
io_frame_enter(__func__, io, ev);
if (ev == EV_TIMEOUT) {
io_callback(io, IO_TIMEOUT);
goto leave;
}
if (ev & EV_WRITE && (w = io_queued(io))) {
if ((n = iobuf_write(&io->iobuf, io->sock)) < 0) {
if (n == IOBUF_WANT_WRITE) /* kqueue bug? */
goto read;
if (n == IOBUF_CLOSED)
io_callback(io, IO_DISCONNECTED);
else {
log_warn("%s: iobuf_write", __func__);
saved_errno = errno;
io->error = strerror(errno);
errno = saved_errno;
io_callback(io, IO_ERROR);
}
goto leave;
}
if (w > io->lowat && w - n <= io->lowat)
io_callback(io, IO_LOWAT);
}
read:
if (ev & EV_READ) {
iobuf_normalize(&io->iobuf);
if ((n = iobuf_read(&io->iobuf, io->sock)) < 0) {
if (n == IOBUF_CLOSED)
io_callback(io, IO_DISCONNECTED);
else {
log_warn("%s: iobuf_read", __func__);
saved_errno = errno;
io->error = strerror(errno);
errno = saved_errno;
io_callback(io, IO_ERROR);
}
goto leave;
}
if (n)
io_callback(io, IO_DATAIN);
}
leave:
io_frame_leave(io);
}
static void
io_dispatch_connect(int fd, short ev, void *arg)
{
struct io *io = arg;
socklen_t sl;
int r, e;
io_frame_enter(__func__, io, ev);
if (ev == EV_TIMEOUT)
e = ETIMEDOUT;
else {
sl = sizeof(e);
r = getsockopt(fd, SOL_SOCKET, SO_ERROR, &e, &sl);
if (r == -1) {
log_warn("%s: getsockopt", __func__);
e = errno;
}
else if (e) {
errno = e;
log_warn("%s: (connect)", __func__);
}
}
if (e == 0) {
io->state = IO_STATE_UP;
io_callback(io, IO_CONNECTED);
goto done;
}
while (io->ai) {
r = io_connect_next(io);
if (r == 0)
goto done;
e = errno;
}
(void)close(fd);
io->sock = -1;
io->error = strerror(e);
io->state = IO_STATE_DOWN;
io_callback(io, e == ETIMEDOUT ? IO_TIMEOUT : IO_ERROR);
done:
io_frame_leave(io);
}
static int
io_connect_next(struct io *io)
{
struct addrinfo *ai;
struct linger l;
int saved_errno;
while ((ai = io->ai)) {
io->ai = ai->ai_next;
ai->ai_next = NULL;
if (ai->ai_socktype == SOCK_STREAM)
break;
freeaddrinfo(ai);
}
if (ai == NULL) {
errno = ESOCKTNOSUPPORT;
log_warn("%s", __func__);
return -1;
}
if ((io->sock = socket(ai->ai_family, ai->ai_socktype | SOCK_NONBLOCK,
0)) == -1) {
log_warn("%s: socket", __func__);
goto fail;
}
memset(&l, 0, sizeof(l));
if (setsockopt(io->sock, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) == -1) {
log_warn("%s: setsockopt", __func__);
goto fail;
}
if (io->bind && bind(io->sock, io->bind, io->bind->sa_len) == -1) {
log_warn("%s: bind", __func__);
goto fail;
}
if (connect(io->sock, ai->ai_addr, ai->ai_addr->sa_len) == -1)
if (errno != EINPROGRESS) {
log_warn("%s: connect", __func__);
goto fail;
}
freeaddrinfo(ai);
io->state = IO_STATE_CONNECT;
io_reset(io, EV_WRITE, io_dispatch_connect);
return 0;
fail:
if (io->sock != -1) {
saved_errno = errno;
close(io->sock);
errno = saved_errno;
io->error = strerror(errno);
io->sock = -1;
}
freeaddrinfo(ai);
if (io->ai) {
freeaddrinfo(io->ai);
io->ai = NULL;
}
return -1;
}
#ifdef IO_SSL
static const char*
io_ssl_error(void)
{
static char buf[128];
unsigned long e;
e = ERR_peek_last_error();
if (e) {
ERR_error_string(e, buf);
return buf;
}
return "No SSL error";
}
static void
io_dispatch_accept_tls(int fd, short event, void *arg)
{
struct io *io = arg;
int e, ret;
io_frame_enter(__func__, io, event);
if (event == EV_TIMEOUT) {
io_callback(io, IO_TIMEOUT);
goto leave;
}
if ((ret = SSL_accept(io->tls)) > 0) {
io->state = IO_STATE_UP;
io_callback(io, IO_TLSREADY);
goto leave;
}
switch ((e = SSL_get_error(io->tls, ret))) {
case SSL_ERROR_WANT_READ:
io_reset(io, EV_READ, io_dispatch_accept_tls);
break;
case SSL_ERROR_WANT_WRITE:
io_reset(io, EV_WRITE, io_dispatch_accept_tls);
break;
default:
io->error = io_ssl_error();
ssl_error("io_dispatch_accept_tls:SSL_accept");
io_callback(io, IO_TLSERROR);
break;
}
leave:
io_frame_leave(io);
}
static void
io_dispatch_connect_tls(int fd, short event, void *arg)
{
struct io *io = arg;
int e, ret;
io_frame_enter(__func__, io, event);
if (event == EV_TIMEOUT) {
io_callback(io, IO_TIMEOUT);
goto leave;
}
if ((ret = SSL_connect(io->tls)) > 0) {
io->state = IO_STATE_UP;
io_callback(io, IO_TLSREADY);
goto leave;
}
switch ((e = SSL_get_error(io->tls, ret))) {
case SSL_ERROR_WANT_READ:
io_reset(io, EV_READ, io_dispatch_connect_tls);
break;
case SSL_ERROR_WANT_WRITE:
io_reset(io, EV_WRITE, io_dispatch_connect_tls);
break;
default:
io->error = io_ssl_error();
ssl_error("io_dispatch_connect_tls:SSL_connect");
io_callback(io, IO_TLSERROR);
break;
}
leave:
io_frame_leave(io);
}
static void
io_dispatch_read_tls(int fd, short event, void *arg)
{
struct io *io = arg;
int n, saved_errno;
io_frame_enter(__func__, io, event);
if (event == EV_TIMEOUT) {
io_callback(io, IO_TIMEOUT);
goto leave;
}
again:
iobuf_normalize(&io->iobuf);
switch ((n = iobuf_read_ssl(&io->iobuf, (SSL*)io->tls))) {
case IOBUF_WANT_READ:
io_reset(io, EV_READ, io_dispatch_read_tls);
break;
case IOBUF_WANT_WRITE:
io_reset(io, EV_WRITE, io_dispatch_read_tls);
break;
case IOBUF_CLOSED:
io_callback(io, IO_DISCONNECTED);
break;
case IOBUF_ERROR:
saved_errno = errno;
io->error = strerror(errno);
errno = saved_errno;
log_warn("%s: iobuf_read_ssl", __func__);
io_callback(io, IO_ERROR);
break;
case IOBUF_SSLERROR:
io->error = io_ssl_error();
ssl_error("io_dispatch_read_tls:SSL_read");
io_callback(io, IO_TLSERROR);
break;
default:
io_debug("%s(...) -> r=%d", __func__, n);
io_callback(io, IO_DATAIN);
if (current == io && IO_READING(io) && SSL_pending(io->tls))
goto again;
}
leave:
io_frame_leave(io);
}
static void
io_dispatch_write_tls(int fd, short event, void *arg)
{
struct io *io = arg;
size_t w2, w;
int n, saved_errno;
io_frame_enter(__func__, io, event);
if (event == EV_TIMEOUT) {
io_callback(io, IO_TIMEOUT);
goto leave;
}
w = io_queued(io);
switch ((n = iobuf_write_ssl(&io->iobuf, (SSL*)io->tls))) {
case IOBUF_WANT_READ:
io_reset(io, EV_READ, io_dispatch_write_tls);
break;
case IOBUF_WANT_WRITE:
io_reset(io, EV_WRITE, io_dispatch_write_tls);
break;
case IOBUF_CLOSED:
io_callback(io, IO_DISCONNECTED);
break;
case IOBUF_ERROR:
saved_errno = errno;
io->error = strerror(errno);
errno = saved_errno;
log_warn("%s: iobuf_write_ssl", __func__);
io_callback(io, IO_ERROR);
break;
case IOBUF_SSLERROR:
io->error = io_ssl_error();
ssl_error("io_dispatch_write_tls:SSL_write");
io_callback(io, IO_TLSERROR);
break;
default:
io_debug("%s(...) -> w=%d", __func__, n);
w2 = io_queued(io);
if (w > io->lowat && w2 <= io->lowat)
io_callback(io, IO_LOWAT);
break;
}
leave:
io_frame_leave(io);
}
static void
io_reload_tls(struct io *io)
{
short ev = 0;
void (*dispatch)(int, short, void*) = NULL;
switch (io->state) {
case IO_STATE_CONNECT_TLS:
ev = EV_WRITE;
dispatch = io_dispatch_connect_tls;
break;
case IO_STATE_ACCEPT_TLS:
ev = EV_READ;
dispatch = io_dispatch_accept_tls;
break;
case IO_STATE_UP:
ev = 0;
if (IO_READING(io) && !(io->flags & IO_PAUSE_IN)) {
ev = EV_READ;
dispatch = io_dispatch_read_tls;
}
else if (IO_WRITING(io) && !(io->flags & IO_PAUSE_OUT) &&
io_queued(io)) {
ev = EV_WRITE;
dispatch = io_dispatch_write_tls;
}
if (!ev)
return; /* paused */
break;
default:
fatalx("%s: unexpected state %d", __func__, io->state);
}
io_reset(io, ev, dispatch);
}
#endif /* IO_SSL */
|