Blob


1 /* $OpenBSD: ioev.c,v 1.42 2019/06/12 17:42:53 eric Exp $ */
2 /*
3 * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17 #define _GNU_SOURCE 1
18 #define _BSD_SOURCE 1
20 #include <sys/types.h>
21 #include <sys/queue.h>
22 #include <sys/socket.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <event.h>
27 #include <fcntl.h>
28 #include <inttypes.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <unistd.h>
34 #include "openbsd-compat.h"
35 #include "ioev.h"
36 #include "iobuf.h"
38 #ifdef IO_TLS
39 #include <openssl/err.h>
40 #include <openssl/ssl.h>
41 #endif
43 enum {
44 IO_STATE_NONE,
45 IO_STATE_CONNECT,
46 IO_STATE_CONNECT_TLS,
47 IO_STATE_ACCEPT_TLS,
48 IO_STATE_UP,
50 IO_STATE_MAX,
51 };
53 #define IO_PAUSE_IN IO_IN
54 #define IO_PAUSE_OUT IO_OUT
55 #define IO_READ 0x04
56 #define IO_WRITE 0x08
57 #define IO_RW (IO_READ | IO_WRITE)
58 #define IO_RESET 0x10 /* internal */
59 #define IO_HELD 0x20 /* internal */
61 struct io {
62 int sock;
63 void *arg;
64 void (*cb)(struct io*, int, void *);
65 struct iobuf iobuf;
66 size_t lowat;
67 int timeout;
68 int flags;
69 int state;
70 struct event ev;
71 void *tls;
72 const char *error; /* only valid immediately on callback */
73 };
75 const char* io_strflags(int);
76 const char* io_evstr(short);
78 void _io_init(void);
79 void io_hold(struct io *);
80 void io_release(struct io *);
81 void io_callback(struct io*, int);
82 void io_dispatch(int, short, void *);
83 void io_dispatch_connect(int, short, void *);
84 size_t io_pending(struct io *);
85 size_t io_queued(struct io*);
86 void io_reset(struct io *, short, void (*)(int, short, void*));
87 void io_frame_enter(const char *, struct io *, int);
88 void io_frame_leave(struct io *);
90 #ifdef IO_TLS
91 void ssl_error(const char *); /* XXX external */
93 static const char* io_tls_error(void);
94 void io_dispatch_accept_tls(int, short, void *);
95 void io_dispatch_connect_tls(int, short, void *);
96 void io_dispatch_read_tls(int, short, void *);
97 void io_dispatch_write_tls(int, short, void *);
98 void io_reload_tls(struct io *io);
99 #endif
101 static struct io *current = NULL;
102 static uint64_t frame = 0;
103 static int _io_debug = 0;
105 //#define io_debug(args...) do { if (_io_debug) printf(args); } while(0)
107 #define io_debug(args...) do { fprintf(stderr, args); } while(0)
109 const char*
110 io_strio(struct io *io)
112 static char buf[128];
113 char ssl[128];
115 ssl[0] = '\0';
116 #ifdef IO_TLS
117 if (io->tls) {
118 (void)snprintf(ssl, sizeof ssl, " tls=%s:%s:%d",
119 SSL_get_version(io->tls),
120 SSL_get_cipher_name(io->tls),
121 SSL_get_cipher_bits(io->tls, NULL));
123 #endif
125 (void)snprintf(buf, sizeof buf,
126 "<io:%p fd=%d to=%d fl=%s%s ib=%zu ob=%zu>",
127 io, io->sock, io->timeout, io_strflags(io->flags), ssl,
128 io_pending(io), io_queued(io));
130 return (buf);
133 #define CASE(x) case x : return #x
135 const char*
136 io_strevent(int evt)
138 static char buf[32];
140 switch (evt) {
141 CASE(IO_CONNECTED);
142 CASE(IO_TLSREADY);
143 CASE(IO_DATAIN);
144 CASE(IO_LOWAT);
145 CASE(IO_DISCONNECTED);
146 CASE(IO_TIMEOUT);
147 CASE(IO_ERROR);
148 default:
149 (void)snprintf(buf, sizeof(buf), "IO_? %d", evt);
150 return buf;
154 void
155 io_set_nonblocking(int fd)
157 int flags;
159 if ((flags = fcntl(fd, F_GETFL)) == -1)
160 err(1, "io_set_blocking:fcntl(F_GETFL)");
162 flags |= O_NONBLOCK;
164 if (fcntl(fd, F_SETFL, flags) == -1)
165 err(1, "io_set_blocking:fcntl(F_SETFL)");
168 void
169 io_set_nolinger(int fd)
171 struct linger l;
173 memset(&l, 0, sizeof(l));
174 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) == -1)
175 err(1, "io_set_linger:setsockopt()");
178 /*
179 * Event framing must not rely on an io pointer to refer to the "same" io
180 * throughout the frame, because this is not always the case:
182 * 1) enter(addr0) -> free(addr0) -> leave(addr0) = SEGV
183 * 2) enter(addr0) -> free(addr0) -> malloc == addr0 -> leave(addr0) = BAD!
185 * In both case, the problem is that the io is freed in the callback, so
186 * the pointer becomes invalid. If that happens, the user is required to
187 * call io_clear, so we can adapt the frame state there.
188 */
189 void
190 io_frame_enter(const char *where, struct io *io, int ev)
192 io_debug("\n=== %" PRIu64 " ===\n"
193 "io_frame_enter(%s, %s, %s)\n",
194 frame, where, io_evstr(ev), io_strio(io));
196 if (current)
197 errx(1, "io_frame_enter: interleaved frames");
199 current = io;
201 io_hold(io);
204 void
205 io_frame_leave(struct io *io)
207 io_debug("io_frame_leave(%" PRIu64 ")\n", frame);
209 if (current && current != io)
210 errx(1, "io_frame_leave: io mismatch");
212 /* io has been cleared */
213 if (current == NULL)
214 goto done;
216 /* TODO: There is a possible optimization there:
217 * In a typical half-duplex request/response scenario,
218 * the io is waiting to read a request, and when done, it queues
219 * the response in the output buffer and goes to write mode.
220 * There, the write event is set and will be triggered in the next
221 * event frame. In most case, the write call could be done
222 * immediately as part of the last read frame, thus avoiding to go
223 * through the event loop machinery. So, as an optimisation, we
224 * could detect that case here and force an event dispatching.
225 */
227 /* Reload the io if it has not been reset already. */
228 io_release(io);
229 current = NULL;
230 done:
231 io_debug("=== /%" PRIu64 "\n", frame);
233 frame += 1;
236 void
237 _io_init(void)
239 static int init = 0;
241 if (init)
242 return;
244 init = 1;
245 _io_debug = getenv("IO_DEBUG") != NULL;
248 struct io *
249 io_new(void)
251 struct io *io;
253 _io_init();
255 if ((io = calloc(1, sizeof(*io))) == NULL)
256 return NULL;
258 io->sock = -1;
259 io->timeout = -1;
261 if (iobuf_init(&io->iobuf, 0, 0) == -1) {
262 free(io);
263 return NULL;
266 return io;
269 void
270 io_free(struct io *io)
272 io_debug("io_clear(%p)\n", io);
274 /* the current io is virtually dead */
275 if (io == current)
276 current = NULL;
278 #ifdef IO_TLS
279 SSL_free(io->tls);
280 io->tls = NULL;
281 #endif
283 if (event_initialized(&io->ev))
284 event_del(&io->ev);
285 if (io->sock != -1) {
286 close(io->sock);
287 io->sock = -1;
290 iobuf_clear(&io->iobuf);
291 free(io);
294 void
295 io_hold(struct io *io)
297 io_debug("io_enter(%p)\n", io);
299 if (io->flags & IO_HELD)
300 errx(1, "io_hold: io is already held");
302 io->flags &= ~IO_RESET;
303 io->flags |= IO_HELD;
306 void
307 io_release(struct io *io)
309 if (!(io->flags & IO_HELD))
310 errx(1, "io_release: io is not held");
312 io->flags &= ~IO_HELD;
313 if (!(io->flags & IO_RESET))
314 io_reload(io);
317 void
318 io_set_fd(struct io *io, int fd)
320 io->sock = fd;
321 if (fd != -1)
322 io_reload(io);
325 void
326 io_set_callback(struct io *io, void(*cb)(struct io *, int, void *), void *arg)
328 io->cb = cb;
329 io->arg = arg;
332 void
333 io_set_timeout(struct io *io, int msec)
335 io_debug("io_set_timeout(%p, %d)\n", io, msec);
337 io->timeout = msec;
340 void
341 io_set_lowat(struct io *io, size_t lowat)
343 io_debug("io_set_lowat(%p, %zu)\n", io, lowat);
345 io->lowat = lowat;
348 void
349 io_pause(struct io *io, int dir)
351 io_debug("io_pause(%p, %x)\n", io, dir);
353 io->flags |= dir & (IO_PAUSE_IN | IO_PAUSE_OUT);
354 io_reload(io);
357 void
358 io_resume(struct io *io, int dir)
360 io_debug("io_resume(%p, %x)\n", io, dir);
362 io->flags &= ~(dir & (IO_PAUSE_IN | IO_PAUSE_OUT));
363 io_reload(io);
366 void
367 io_set_read(struct io *io)
369 int mode;
371 io_debug("io_set_read(%p)\n", io);
373 mode = io->flags & IO_RW;
374 if (!(mode == 0 || mode == IO_WRITE))
375 errx(1, "io_set_read(): full-duplex or reading");
377 io->flags &= ~IO_RW;
378 io->flags |= IO_READ;
379 io_reload(io);
382 void
383 io_set_write(struct io *io)
385 int mode;
387 io_debug("io_set_write(%p)\n", io);
389 mode = io->flags & IO_RW;
390 if (!(mode == 0 || mode == IO_READ))
391 errx(1, "io_set_write(): full-duplex or writing");
393 io->flags &= ~IO_RW;
394 io->flags |= IO_WRITE;
395 io_reload(io);
398 const char *
399 io_error(struct io *io)
401 return io->error;
404 void *
405 io_tls(struct io *io)
407 return io->tls;
410 int
411 io_fileno(struct io *io)
413 return io->sock;
416 int
417 io_paused(struct io *io, int what)
419 return (io->flags & (IO_PAUSE_IN | IO_PAUSE_OUT)) == what;
422 /*
423 * Buffered output functions
424 */
426 int
427 io_write(struct io *io, const void *buf, size_t len)
429 int r;
431 r = iobuf_queue(&io->iobuf, buf, len);
433 io_reload(io);
435 return r;
438 int
439 io_writev(struct io *io, const struct iovec *iov, int iovcount)
441 int r;
443 r = iobuf_queuev(&io->iobuf, iov, iovcount);
445 io_reload(io);
447 return r;
450 int
451 io_print(struct io *io, const char *s)
453 return io_write(io, s, strlen(s));
456 int
457 io_printf(struct io *io, const char *fmt, ...)
459 va_list ap;
460 int r;
462 va_start(ap, fmt);
463 r = io_vprintf(io, fmt, ap);
464 va_end(ap);
466 return r;
469 int
470 io_vprintf(struct io *io, const char *fmt, va_list ap)
473 char *buf;
474 int len;
476 len = vasprintf(&buf, fmt, ap);
477 if (len == -1)
478 return -1;
479 len = io_write(io, buf, len);
480 free(buf);
482 return len;
485 size_t
486 io_queued(struct io *io)
488 return iobuf_queued(&io->iobuf);
491 /*
492 * Buffered input functions
493 */
495 void *
496 io_data(struct io *io)
498 return iobuf_data(&io->iobuf);
501 size_t
502 io_datalen(struct io *io)
504 return iobuf_len(&io->iobuf);
507 char *
508 io_getline(struct io *io, size_t *sz)
510 return iobuf_getline(&io->iobuf, sz);
513 void
514 io_drop(struct io *io, size_t sz)
516 return iobuf_drop(&io->iobuf, sz);
520 #define IO_READING(io) (((io)->flags & IO_RW) != IO_WRITE)
521 #define IO_WRITING(io) (((io)->flags & IO_RW) != IO_READ)
523 /*
524 * Setup the necessary events as required by the current io state,
525 * honouring duplex mode and i/o pauses.
526 */
527 void
528 io_reload(struct io *io)
530 short events;
532 /* io will be reloaded at release time */
533 if (io->flags & IO_HELD)
534 return;
536 iobuf_normalize(&io->iobuf);
538 #ifdef IO_TLS
539 if (io->tls) {
540 io_reload_tls(io);
541 return;
543 #endif
545 io_debug("io_reload(%p)\n", io);
547 events = 0;
548 if (IO_READING(io) && !(io->flags & IO_PAUSE_IN))
549 events = EV_READ;
550 if (IO_WRITING(io) && !(io->flags & IO_PAUSE_OUT) && io_queued(io))
551 events |= EV_WRITE;
553 io_reset(io, events, io_dispatch);
556 /* Set the requested event. */
557 void
558 io_reset(struct io *io, short events, void (*dispatch)(int, short, void*))
560 struct timeval tv, *ptv;
562 io_debug("io_reset(%p, %s, %p) -> %s\n",
563 io, io_evstr(events), dispatch, io_strio(io));
565 /*
566 * Indicate that the event has already been reset so that reload
567 * is not called on frame_leave.
568 */
569 io->flags |= IO_RESET;
571 if (event_initialized(&io->ev) &&
572 event_pending(&io->ev, EV_READ|EV_WRITE, NULL))
573 event_del(&io->ev);
575 /*
576 * The io is paused by the user, so we don't want the timeout to be
577 * effective.
578 */
579 if (events == 0)
580 return;
582 event_set(&io->ev, io->sock, events, dispatch, io);
583 if (io->timeout >= 0) {
584 tv.tv_sec = io->timeout / 1000;
585 tv.tv_usec = (io->timeout % 1000) * 1000;
586 ptv = &tv;
587 } else
588 ptv = NULL;
590 event_add(&io->ev, ptv);
593 size_t
594 io_pending(struct io *io)
596 return iobuf_len(&io->iobuf);
599 const char*
600 io_strflags(int flags)
602 static char buf[64];
604 buf[0] = '\0';
606 switch (flags & IO_RW) {
607 case 0:
608 (void)strlcat(buf, "rw", sizeof buf);
609 break;
610 case IO_READ:
611 (void)strlcat(buf, "R", sizeof buf);
612 break;
613 case IO_WRITE:
614 (void)strlcat(buf, "W", sizeof buf);
615 break;
616 case IO_RW:
617 (void)strlcat(buf, "RW", sizeof buf);
618 break;
621 if (flags & IO_PAUSE_IN)
622 (void)strlcat(buf, ",F_PI", sizeof buf);
623 if (flags & IO_PAUSE_OUT)
624 (void)strlcat(buf, ",F_PO", sizeof buf);
626 return buf;
629 const char*
630 io_evstr(short ev)
632 static char buf[64];
633 char buf2[16];
634 int n;
636 n = 0;
637 buf[0] = '\0';
639 if (ev == 0) {
640 (void)strlcat(buf, "<NONE>", sizeof(buf));
641 return buf;
644 if (ev & EV_TIMEOUT) {
645 (void)strlcat(buf, "EV_TIMEOUT", sizeof(buf));
646 ev &= ~EV_TIMEOUT;
647 n++;
650 if (ev & EV_READ) {
651 if (n)
652 (void)strlcat(buf, "|", sizeof(buf));
653 (void)strlcat(buf, "EV_READ", sizeof(buf));
654 ev &= ~EV_READ;
655 n++;
658 if (ev & EV_WRITE) {
659 if (n)
660 (void)strlcat(buf, "|", sizeof(buf));
661 (void)strlcat(buf, "EV_WRITE", sizeof(buf));
662 ev &= ~EV_WRITE;
663 n++;
666 if (ev & EV_SIGNAL) {
667 if (n)
668 (void)strlcat(buf, "|", sizeof(buf));
669 (void)strlcat(buf, "EV_SIGNAL", sizeof(buf));
670 ev &= ~EV_SIGNAL;
671 n++;
674 if (ev) {
675 if (n)
676 (void)strlcat(buf, "|", sizeof(buf));
677 (void)strlcat(buf, "EV_?=0x", sizeof(buf));
678 (void)snprintf(buf2, sizeof(buf2), "%hx", ev);
679 (void)strlcat(buf, buf2, sizeof(buf));
682 return buf;
685 void
686 io_dispatch(__unused int fd, short ev, void *humppa)
688 struct io *io = humppa;
689 size_t w;
690 ssize_t n;
691 int saved_errno;
693 io_frame_enter("io_dispatch", io, ev);
695 if (ev == EV_TIMEOUT) {
696 io_callback(io, IO_TIMEOUT);
697 goto leave;
700 if (ev & EV_WRITE && (w = io_queued(io))) {
701 if ((n = iobuf_write(&io->iobuf, io->sock)) < 0) {
702 if (n == IOBUF_WANT_WRITE) /* kqueue bug? */
703 goto read;
704 if (n == IOBUF_CLOSED)
705 io_callback(io, IO_DISCONNECTED);
706 else {
707 saved_errno = errno;
708 io->error = strerror(errno);
709 errno = saved_errno;
710 io_callback(io, IO_ERROR);
712 goto leave;
714 if (w > io->lowat && w - n <= io->lowat)
715 io_callback(io, IO_LOWAT);
717 read:
719 if (ev & EV_READ) {
720 iobuf_normalize(&io->iobuf);
721 if ((n = iobuf_read(&io->iobuf, io->sock)) < 0) {
722 if (n == IOBUF_CLOSED)
723 io_callback(io, IO_DISCONNECTED);
724 else {
725 saved_errno = errno;
726 io->error = strerror(errno);
727 errno = saved_errno;
728 io_callback(io, IO_ERROR);
730 goto leave;
732 if (n)
733 io_callback(io, IO_DATAIN);
736 leave:
737 io_frame_leave(io);
740 void
741 io_callback(struct io *io, int evt)
743 io->cb(io, evt, io->arg);
746 int
747 io_connect(struct io *io, const struct sockaddr *sa, const struct sockaddr *bsa)
749 int sock, errno_save;
751 if ((sock = socket(sa->sa_family, SOCK_STREAM, 0)) == -1)
752 goto fail;
754 io_set_nonblocking(sock);
755 io_set_nolinger(sock);
757 if (bsa && bind(sock, bsa, SA_LEN(bsa)) == -1)
758 goto fail;
760 if (connect(sock, sa, SA_LEN(sa)) == -1)
761 if (errno != EINPROGRESS)
762 goto fail;
764 io->sock = sock;
765 io_reset(io, EV_WRITE, io_dispatch_connect);
767 return (sock);
769 fail:
770 if (sock != -1) {
771 errno_save = errno;
772 close(sock);
773 errno = errno_save;
774 io->error = strerror(errno);
776 return (-1);
779 void
780 io_dispatch_connect(int fd, short ev, void *humppa)
782 struct io *io = humppa;
783 int r, e;
784 socklen_t sl;
786 io_frame_enter("io_dispatch_connect", io, ev);
788 if (ev == EV_TIMEOUT) {
789 close(fd);
790 io->sock = -1;
791 io_callback(io, IO_TIMEOUT);
792 } else {
793 sl = sizeof(e);
794 r = getsockopt(fd, SOL_SOCKET, SO_ERROR, &e, &sl);
795 if (r == -1) {
796 warn("io_dispatch_connect: getsockopt");
797 e = errno;
799 if (e) {
800 close(fd);
801 io->sock = -1;
802 io->error = strerror(e);
803 io_callback(io, e == ETIMEDOUT ? IO_TIMEOUT : IO_ERROR);
805 else {
806 io->state = IO_STATE_UP;
807 io_callback(io, IO_CONNECTED);
811 io_frame_leave(io);
814 #ifdef IO_TLS
816 static const char*
817 io_tls_error(void)
819 static char buf[128];
820 unsigned long e;
822 e = ERR_peek_last_error();
823 if (e) {
824 ERR_error_string(e, buf);
825 return (buf);
828 return ("No TLS error");
831 int
832 io_start_tls(struct io *io, void *tls)
834 int mode;
836 mode = io->flags & IO_RW;
837 if (mode == 0 || mode == IO_RW)
838 errx(1, "io_start_tls(): full-duplex or unset");
840 if (io->tls)
841 errx(1, "io_start_tls(): TLS already started");
842 io->tls = tls;
844 if (SSL_set_fd(io->tls, io->sock) == 0) {
845 ssl_error("io_start_tls:SSL_set_fd");
846 return (-1);
849 if (mode == IO_WRITE) {
850 io->state = IO_STATE_CONNECT_TLS;
851 SSL_set_connect_state(io->tls);
852 io_reset(io, EV_WRITE, io_dispatch_connect_tls);
853 } else {
854 io->state = IO_STATE_ACCEPT_TLS;
855 SSL_set_accept_state(io->tls);
856 io_reset(io, EV_READ, io_dispatch_accept_tls);
859 return (0);
862 void
863 io_dispatch_accept_tls(int fd, short event, void *humppa)
865 struct io *io = humppa;
866 int e, ret;
868 io_frame_enter("io_dispatch_accept_tls", io, event);
870 if (event == EV_TIMEOUT) {
871 io_callback(io, IO_TIMEOUT);
872 goto leave;
875 if ((ret = SSL_accept(io->tls)) > 0) {
876 io->state = IO_STATE_UP;
877 io_callback(io, IO_TLSREADY);
878 goto leave;
881 switch ((e = SSL_get_error(io->tls, ret))) {
882 case SSL_ERROR_WANT_READ:
883 io_reset(io, EV_READ, io_dispatch_accept_tls);
884 break;
885 case SSL_ERROR_WANT_WRITE:
886 io_reset(io, EV_WRITE, io_dispatch_accept_tls);
887 break;
888 default:
889 io->error = io_tls_error();
890 ssl_error("io_dispatch_accept_tls:SSL_accept");
891 io_callback(io, IO_ERROR);
892 break;
895 leave:
896 io_frame_leave(io);
899 void
900 io_dispatch_connect_tls(int fd, short event, void *humppa)
902 struct io *io = humppa;
903 int e, ret;
905 io_frame_enter("io_dispatch_connect_tls", io, event);
907 if (event == EV_TIMEOUT) {
908 io_callback(io, IO_TIMEOUT);
909 goto leave;
912 if ((ret = SSL_connect(io->tls)) > 0) {
913 io->state = IO_STATE_UP;
914 io_callback(io, IO_TLSREADY);
915 goto leave;
918 switch ((e = SSL_get_error(io->tls, ret))) {
919 case SSL_ERROR_WANT_READ:
920 io_reset(io, EV_READ, io_dispatch_connect_tls);
921 break;
922 case SSL_ERROR_WANT_WRITE:
923 io_reset(io, EV_WRITE, io_dispatch_connect_tls);
924 break;
925 default:
926 io->error = io_tls_error();
927 ssl_error("io_dispatch_connect_ssl:SSL_connect");
928 io_callback(io, IO_TLSERROR);
929 break;
932 leave:
933 io_frame_leave(io);
936 void
937 io_dispatch_read_tls(int fd, short event, void *humppa)
939 struct io *io = humppa;
940 int n, saved_errno;
942 io_frame_enter("io_dispatch_read_tls", io, event);
944 if (event == EV_TIMEOUT) {
945 io_callback(io, IO_TIMEOUT);
946 goto leave;
949 again:
950 iobuf_normalize(&io->iobuf);
951 switch ((n = iobuf_read_tls(&io->iobuf, (SSL*)io->tls))) {
952 case IOBUF_WANT_READ:
953 io_reset(io, EV_READ, io_dispatch_read_tls);
954 break;
955 case IOBUF_WANT_WRITE:
956 io_reset(io, EV_WRITE, io_dispatch_read_tls);
957 break;
958 case IOBUF_CLOSED:
959 io_callback(io, IO_DISCONNECTED);
960 break;
961 case IOBUF_ERROR:
962 saved_errno = errno;
963 io->error = strerror(errno);
964 errno = saved_errno;
965 io_callback(io, IO_ERROR);
966 break;
967 case IOBUF_TLSERROR:
968 io->error = io_tls_error();
969 ssl_error("io_dispatch_read_tls:SSL_read");
970 io_callback(io, IO_ERROR);
971 break;
972 default:
973 io_debug("io_dispatch_read_tls(...) -> r=%d\n", n);
974 io_callback(io, IO_DATAIN);
975 if (current == io && IO_READING(io) && SSL_pending(io->tls))
976 goto again;
979 leave:
980 io_frame_leave(io);
983 void
984 io_dispatch_write_tls(int fd, short event, void *humppa)
986 struct io *io = humppa;
987 int n, saved_errno;
988 size_t w2, w;
990 io_frame_enter("io_dispatch_write_tls", io, event);
992 if (event == EV_TIMEOUT) {
993 io_callback(io, IO_TIMEOUT);
994 goto leave;
997 w = io_queued(io);
998 switch ((n = iobuf_write_tls(&io->iobuf, (SSL*)io->tls))) {
999 case IOBUF_WANT_READ:
1000 io_reset(io, EV_READ, io_dispatch_write_tls);
1001 break;
1002 case IOBUF_WANT_WRITE:
1003 io_reset(io, EV_WRITE, io_dispatch_write_tls);
1004 break;
1005 case IOBUF_CLOSED:
1006 io_callback(io, IO_DISCONNECTED);
1007 break;
1008 case IOBUF_ERROR:
1009 saved_errno = errno;
1010 io->error = strerror(errno);
1011 errno = saved_errno;
1012 io_callback(io, IO_ERROR);
1013 break;
1014 case IOBUF_TLSERROR:
1015 io->error = io_tls_error();
1016 ssl_error("io_dispatch_write_tls:SSL_write");
1017 io_callback(io, IO_ERROR);
1018 break;
1019 default:
1020 io_debug("io_dispatch_write_tls(...) -> w=%d\n", n);
1021 w2 = io_queued(io);
1022 if (w > io->lowat && w2 <= io->lowat)
1023 io_callback(io, IO_LOWAT);
1024 break;
1027 leave:
1028 io_frame_leave(io);
1031 void
1032 io_reload_tls(struct io *io)
1034 short ev = 0;
1035 void (*dispatch)(int, short, void*) = NULL;
1037 switch (io->state) {
1038 case IO_STATE_CONNECT_TLS:
1039 ev = EV_WRITE;
1040 dispatch = io_dispatch_connect_tls;
1041 break;
1042 case IO_STATE_ACCEPT_TLS:
1043 ev = EV_READ;
1044 dispatch = io_dispatch_accept_tls;
1045 break;
1046 case IO_STATE_UP:
1047 ev = 0;
1048 if (IO_READING(io) && !(io->flags & IO_PAUSE_IN)) {
1049 ev = EV_READ;
1050 dispatch = io_dispatch_read_tls;
1052 else if (IO_WRITING(io) && !(io->flags & IO_PAUSE_OUT) &&
1053 io_queued(io)) {
1054 ev = EV_WRITE;
1055 dispatch = io_dispatch_write_tls;
1057 if (!ev)
1058 return; /* paused */
1059 break;
1060 default:
1061 errx(1, "io_reload_tls(): bad state");
1064 io_reset(io, ev, dispatch);
1067 #endif /* IO_TLS */