1 /* $OpenBSD: ioev.c,v 1.42 2019/06/12 17:42:53 eric Exp $ */
3 * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
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.
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.
20 #include <sys/types.h>
21 #include <sys/queue.h>
22 #include <sys/socket.h>
34 #include "openbsd-compat.h"
39 #include <openssl/err.h>
40 #include <openssl/ssl.h>
53 #define IO_PAUSE_IN IO_IN
54 #define IO_PAUSE_OUT IO_OUT
57 #define IO_RW (IO_READ | IO_WRITE)
58 #define IO_RESET 0x10 /* internal */
59 #define IO_HELD 0x20 /* internal */
64 void (*cb)(struct io*, int, void *);
72 const char *error; /* only valid immediately on callback */
75 const char* io_strflags(int);
76 const char* io_evstr(short);
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 *);
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);
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)
110 io_strio(struct io *io)
112 static char buf[128];
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));
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));
133 #define CASE(x) case x : return #x
145 CASE(IO_DISCONNECTED);
149 (void)snprintf(buf, sizeof(buf), "IO_? %d", evt);
155 io_set_nonblocking(int fd)
159 if ((flags = fcntl(fd, F_GETFL)) == -1)
160 err(1, "io_set_blocking:fcntl(F_GETFL)");
164 if (fcntl(fd, F_SETFL, flags) == -1)
165 err(1, "io_set_blocking:fcntl(F_SETFL)");
169 io_set_nolinger(int fd)
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()");
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.
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));
197 errx(1, "io_frame_enter: interleaved frames");
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 */
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.
227 /* Reload the io if it has not been reset already. */
231 io_debug("=== /%" PRIu64 "\n", frame);
245 _io_debug = getenv("IO_DEBUG") != NULL;
255 if ((io = calloc(1, sizeof(*io))) == NULL)
261 if (iobuf_init(&io->iobuf, 0, 0) == -1) {
270 io_free(struct io *io)
272 io_debug("io_clear(%p)\n", io);
274 /* the current io is virtually dead */
283 if (event_initialized(&io->ev))
285 if (io->sock != -1) {
290 iobuf_clear(&io->iobuf);
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;
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))
318 io_set_fd(struct io *io, int fd)
326 io_set_callback(struct io *io, void(*cb)(struct io *, int, void *), void *arg)
333 io_set_timeout(struct io *io, int msec)
335 io_debug("io_set_timeout(%p, %d)\n", io, msec);
341 io_set_lowat(struct io *io, size_t lowat)
343 io_debug("io_set_lowat(%p, %zu)\n", io, lowat);
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);
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));
367 io_set_read(struct io *io)
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");
378 io->flags |= IO_READ;
383 io_set_write(struct io *io)
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");
394 io->flags |= IO_WRITE;
399 io_error(struct io *io)
405 io_tls(struct io *io)
411 io_fileno(struct io *io)
417 io_paused(struct io *io, int what)
419 return (io->flags & (IO_PAUSE_IN | IO_PAUSE_OUT)) == what;
423 * Buffered output functions
427 io_write(struct io *io, const void *buf, size_t len)
431 r = iobuf_queue(&io->iobuf, buf, len);
439 io_writev(struct io *io, const struct iovec *iov, int iovcount)
443 r = iobuf_queuev(&io->iobuf, iov, iovcount);
451 io_print(struct io *io, const char *s)
453 return io_write(io, s, strlen(s));
457 io_printf(struct io *io, const char *fmt, ...)
463 r = io_vprintf(io, fmt, ap);
470 io_vprintf(struct io *io, const char *fmt, va_list ap)
476 len = vasprintf(&buf, fmt, ap);
479 len = io_write(io, buf, len);
486 io_queued(struct io *io)
488 return iobuf_queued(&io->iobuf);
492 * Buffered input functions
496 io_data(struct io *io)
498 return iobuf_data(&io->iobuf);
502 io_datalen(struct io *io)
504 return iobuf_len(&io->iobuf);
508 io_getline(struct io *io, size_t *sz)
510 return iobuf_getline(&io->iobuf, sz);
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)
524 * Setup the necessary events as required by the current io state,
525 * honouring duplex mode and i/o pauses.
528 io_reload(struct io *io)
532 /* io will be reloaded at release time */
533 if (io->flags & IO_HELD)
536 iobuf_normalize(&io->iobuf);
545 io_debug("io_reload(%p)\n", io);
548 if (IO_READING(io) && !(io->flags & IO_PAUSE_IN))
550 if (IO_WRITING(io) && !(io->flags & IO_PAUSE_OUT) && io_queued(io))
553 io_reset(io, events, io_dispatch);
556 /* Set the requested event. */
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));
566 * Indicate that the event has already been reset so that reload
567 * is not called on frame_leave.
569 io->flags |= IO_RESET;
571 if (event_initialized(&io->ev) &&
572 event_pending(&io->ev, EV_READ|EV_WRITE, NULL))
576 * The io is paused by the user, so we don't want the timeout to be
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;
590 event_add(&io->ev, ptv);
594 io_pending(struct io *io)
596 return iobuf_len(&io->iobuf);
600 io_strflags(int flags)
606 switch (flags & IO_RW) {
608 (void)strlcat(buf, "rw", sizeof buf);
611 (void)strlcat(buf, "R", sizeof buf);
614 (void)strlcat(buf, "W", sizeof buf);
617 (void)strlcat(buf, "RW", sizeof buf);
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);
640 (void)strlcat(buf, "<NONE>", sizeof(buf));
644 if (ev & EV_TIMEOUT) {
645 (void)strlcat(buf, "EV_TIMEOUT", sizeof(buf));
652 (void)strlcat(buf, "|", sizeof(buf));
653 (void)strlcat(buf, "EV_READ", sizeof(buf));
660 (void)strlcat(buf, "|", sizeof(buf));
661 (void)strlcat(buf, "EV_WRITE", sizeof(buf));
666 if (ev & EV_SIGNAL) {
668 (void)strlcat(buf, "|", sizeof(buf));
669 (void)strlcat(buf, "EV_SIGNAL", sizeof(buf));
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));
686 io_dispatch(__unused int fd, short ev, void *humppa)
688 struct io *io = humppa;
693 io_frame_enter("io_dispatch", io, ev);
695 if (ev == EV_TIMEOUT) {
696 io_callback(io, IO_TIMEOUT);
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? */
704 if (n == IOBUF_CLOSED)
705 io_callback(io, IO_DISCONNECTED);
708 io->error = strerror(errno);
710 io_callback(io, IO_ERROR);
714 if (w > io->lowat && w - n <= io->lowat)
715 io_callback(io, IO_LOWAT);
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);
726 io->error = strerror(errno);
728 io_callback(io, IO_ERROR);
733 io_callback(io, IO_DATAIN);
741 io_callback(struct io *io, int evt)
743 io->cb(io, evt, io->arg);
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)
754 io_set_nonblocking(sock);
755 io_set_nolinger(sock);
757 if (bsa && bind(sock, bsa, SA_LEN(bsa)) == -1)
760 if (connect(sock, sa, SA_LEN(sa)) == -1)
761 if (errno != EINPROGRESS)
765 io_reset(io, EV_WRITE, io_dispatch_connect);
774 io->error = strerror(errno);
780 io_dispatch_connect(int fd, short ev, void *humppa)
782 struct io *io = humppa;
786 io_frame_enter("io_dispatch_connect", io, ev);
788 if (ev == EV_TIMEOUT) {
791 io_callback(io, IO_TIMEOUT);
794 r = getsockopt(fd, SOL_SOCKET, SO_ERROR, &e, &sl);
796 warn("io_dispatch_connect: getsockopt");
802 io->error = strerror(e);
803 io_callback(io, e == ETIMEDOUT ? IO_TIMEOUT : IO_ERROR);
806 io->state = IO_STATE_UP;
807 io_callback(io, IO_CONNECTED);
819 static char buf[128];
822 e = ERR_peek_last_error();
824 ERR_error_string(e, buf);
828 return ("No TLS error");
832 io_start_tls(struct io *io, void *tls)
836 mode = io->flags & IO_RW;
837 if (mode == 0 || mode == IO_RW)
838 errx(1, "io_start_tls(): full-duplex or unset");
841 errx(1, "io_start_tls(): TLS already started");
844 if (SSL_set_fd(io->tls, io->sock) == 0) {
845 ssl_error("io_start_tls:SSL_set_fd");
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);
854 io->state = IO_STATE_ACCEPT_TLS;
855 SSL_set_accept_state(io->tls);
856 io_reset(io, EV_READ, io_dispatch_accept_tls);
863 io_dispatch_accept_tls(int fd, short event, void *humppa)
865 struct io *io = humppa;
868 io_frame_enter("io_dispatch_accept_tls", io, event);
870 if (event == EV_TIMEOUT) {
871 io_callback(io, IO_TIMEOUT);
875 if ((ret = SSL_accept(io->tls)) > 0) {
876 io->state = IO_STATE_UP;
877 io_callback(io, IO_TLSREADY);
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);
885 case SSL_ERROR_WANT_WRITE:
886 io_reset(io, EV_WRITE, io_dispatch_accept_tls);
889 io->error = io_tls_error();
890 ssl_error("io_dispatch_accept_tls:SSL_accept");
891 io_callback(io, IO_ERROR);
900 io_dispatch_connect_tls(int fd, short event, void *humppa)
902 struct io *io = humppa;
905 io_frame_enter("io_dispatch_connect_tls", io, event);
907 if (event == EV_TIMEOUT) {
908 io_callback(io, IO_TIMEOUT);
912 if ((ret = SSL_connect(io->tls)) > 0) {
913 io->state = IO_STATE_UP;
914 io_callback(io, IO_TLSREADY);
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);
922 case SSL_ERROR_WANT_WRITE:
923 io_reset(io, EV_WRITE, io_dispatch_connect_tls);
926 io->error = io_tls_error();
927 ssl_error("io_dispatch_connect_ssl:SSL_connect");
928 io_callback(io, IO_TLSERROR);
937 io_dispatch_read_tls(int fd, short event, void *humppa)
939 struct io *io = humppa;
942 io_frame_enter("io_dispatch_read_tls", io, event);
944 if (event == EV_TIMEOUT) {
945 io_callback(io, IO_TIMEOUT);
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);
955 case IOBUF_WANT_WRITE:
956 io_reset(io, EV_WRITE, io_dispatch_read_tls);
959 io_callback(io, IO_DISCONNECTED);
963 io->error = strerror(errno);
965 io_callback(io, IO_ERROR);
968 io->error = io_tls_error();
969 ssl_error("io_dispatch_read_tls:SSL_read");
970 io_callback(io, IO_ERROR);
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))
984 io_dispatch_write_tls(int fd, short event, void *humppa)
986 struct io *io = humppa;
990 io_frame_enter("io_dispatch_write_tls", io, event);
992 if (event == EV_TIMEOUT) {
993 io_callback(io, IO_TIMEOUT);
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);
1002 case IOBUF_WANT_WRITE:
1003 io_reset(io, EV_WRITE, io_dispatch_write_tls);
1006 io_callback(io, IO_DISCONNECTED);
1009 saved_errno = errno;
1010 io->error = strerror(errno);
1011 errno = saved_errno;
1012 io_callback(io, IO_ERROR);
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);
1020 io_debug("io_dispatch_write_tls(...) -> w=%d\n", n);
1022 if (w > io->lowat && w2 <= io->lowat)
1023 io_callback(io, IO_LOWAT);
1032 io_reload_tls(struct io *io)
1035 void (*dispatch)(int, short, void*) = NULL;
1037 switch (io->state) {
1038 case IO_STATE_CONNECT_TLS:
1040 dispatch = io_dispatch_connect_tls;
1042 case IO_STATE_ACCEPT_TLS:
1044 dispatch = io_dispatch_accept_tls;
1048 if (IO_READING(io) && !(io->flags & IO_PAUSE_IN)) {
1050 dispatch = io_dispatch_read_tls;
1052 else if (IO_WRITING(io) && !(io->flags & IO_PAUSE_OUT) &&
1055 dispatch = io_dispatch_write_tls;
1058 return; /* paused */
1061 errx(1, "io_reload_tls(): bad state");
1064 io_reset(io, ev, dispatch);