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
|
/* $OpenBSD: ioev.c,v 1.2 2012/01/30 10:02:55 chl 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/param.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include "ioev.h"
#include "iobuf.h"
#ifdef IO_SSL
# include <openssl/ssl.h>
#endif
enum {
IO_STATE_NONE,
IO_STATE_CONNECT,
IO_STATE_CONNECT_SSL,
IO_STATE_ACCEPT_SSL,
IO_STATE_UP,
IO_STATE_MAX,
};
const char* io_strflags(int);
const char* io_evstr(short);
void _io_init(void);
void io_hold(struct io *);
void io_release(struct io *);
void io_callback(struct io*, int);
void io_dispatch(int, short, void *);
void io_dispatch_connect(int, short, void *);
size_t io_pending(struct io *);
size_t io_queued(struct io*);
void io_reset(struct io *, short, void (*)(int, short, void*));
void io_frame_enter(const char *, struct io *, int);
void io_frame_leave(struct io *);
#ifdef IO_SSL
void ssl_error(const char *); /* XXX external */
void io_dispatch_accept_ssl(int, short, void *);
void io_dispatch_connect_ssl(int, short, void *);
void io_dispatch_read_ssl(int, short, void *);
void io_dispatch_write_ssl(int, short, void *);
void io_reload_ssl(struct io *io);
#endif
static struct io *current = NULL;
static uint64_t frame = 0;
static int _io_debug = 0;
#define io_debug(args...) do { if (_io_debug) printf(args); } while(0)
const char*
io_strio(struct io *io)
{
static char buf[128];
snprintf(buf, sizeof buf, "<io:%p fd=%i to=%i fl=%s ib=%zu ob=%zu>",
io, io->sock, io->timeout, io_strflags(io->flags),
io_pending(io), io_queued(io));
return (buf);
}
#define CASE(x) case x : return #x
const char*
io_strevent(int evt)
{
static char buf[32];
switch(evt) {
CASE(IO_CONNECTED);
CASE(IO_TLSREADY);
CASE(IO_DATAIN);
CASE(IO_LOWAT);
CASE(IO_DISCONNECTED);
CASE(IO_TIMEOUT);
CASE(IO_ERROR);
default:
snprintf(buf, sizeof(buf), "IO_? %i", evt);
return buf;
}
}
void
io_set_blocking(int fd, int blocking)
{
int flags;
if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
err(1, "io_set_blocking:fcntl(F_GETFL)");
if (blocking)
flags &= ~O_NONBLOCK;
else
flags |= O_NONBLOCK;
if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
err(1, "io_set_blocking:fcntl(F_SETFL)");
}
void
io_set_linger(int fd, int linger)
{
struct linger l;
bzero(&l, sizeof(l));
l.l_onoff = linger ? 1 : 0;
l.l_linger = linger;
if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) == -1)
err(1, "io_set_linger:setsockopt()");
}
/*
* Event framing must not rely on an io pointer to refer to the "same" io
* throughout the frame, beacuse this is not always the case:
*
* 1) enter(addr0) -> free(addr0) -> leave(addr0) = SEGV
* 2) enter(addr0) -> free(addr0) -> malloc == addr0 -> leave(addr0) = BAD!
*
* In both case, the problem is that the io is freed in the callback, so
* the pointer becomes invalid. If that happens, the user is required to
* call io_clear, so we can adapt the frame state there.
*/
void
io_frame_enter(const char *where, struct io *io, int ev)
{
io_debug("\n=== %" PRIu64 " ===\n"
"io_frame_enter(%s, %s, %s)\n",
frame, where, io_evstr(ev), io_strio(io));
if (current)
errx(1, "io_frame_enter: interleaved frames");
current = io;
io_hold(io);
}
void
io_frame_leave(struct io *io)
{
io_debug("io_frame_leave(%" PRIu64 ")\n", frame);
if (current && current != io)
errx(1, "io_frame_leave: io mismatch");
/* 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
* immediatly 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("=== /%" PRIu64 "\n", frame);
frame += 1;
}
void
_io_init()
{
static int init = 0;
if (init)
return;
init = 1;
_io_debug = getenv("IO_DEBUG") != NULL;
}
void
io_init(struct io *io, int sock, void *arg,
void(*cb)(struct io*, int), struct iobuf *iobuf)
{
_io_init();
memset(io, 0, sizeof *io);
io->sock = sock;
io->timeout = -1;
io->arg = arg;
io->iobuf = iobuf;
io->cb = cb;
if (sock != -1)
io_reload(io);
}
void
io_clear(struct io *io)
{
io_debug("io_clear(%p)\n", io);
/* the current io is virtually dead */
if (io == current)
current = NULL;
#ifdef IO_SSL
if (io->ssl) {
SSL_free(io->ssl);
io->ssl = NULL;
}
#endif
event_del(&io->ev);
if (io->sock != -1) {
close(io->sock);
io->sock = -1;
}
}
void
io_hold(struct io *io)
{
io_debug("io_enter(%p)\n", io);
if (io->flags & IO_HELD)
errx(1, "io_hold: io is already held");
io->flags &= ~IO_RESET;
io->flags |= IO_HELD;
}
void
io_release(struct io *io)
{
if (!(io->flags & IO_HELD))
errx(1, "io_release: io is not held");
io->flags &= ~IO_HELD;
if (!(io->flags & IO_RESET))
io_reload(io);
}
void
io_set_timeout(struct io *io, int msec)
{
io_debug("io_set_timeout(%p, %i)\n", io, msec);
io->timeout = msec;
}
void
io_set_lowat(struct io *io, size_t lowat)
{
io_debug("io_set_lowat(%p, %zu)\n", io, lowat);
io->lowat = lowat;
}
void
io_pause(struct io *io, int dir)
{
io_debug("io_pause(%p, %x)\n", io, dir);
io->flags |= dir & (IO_PAUSE_IN | IO_PAUSE_OUT);
io_reload(io);
}
void
io_resume(struct io *io, int dir)
{
io_debug("io_resume(%p, %x)\n", io, dir);
io->flags &= ~(dir & (IO_PAUSE_IN | IO_PAUSE_OUT));
io_reload(io);
}
void
io_set_read(struct io *io)
{
int mode;
io_debug("io_set_read(%p)\n", io);
mode = io->flags & IO_RW;
if (!(mode == 0 || mode == IO_WRITE))
errx(1, "io_set_read(): full-duplex or reading");
io->flags &= ~IO_RW;
io->flags |= IO_READ;
io_reload(io);
}
void
io_set_write(struct io *io)
{
int mode;
io_debug("io_set_write(%p)\n", io);
mode = io->flags & IO_RW;
if (!(mode == 0 || mode == IO_READ))
errx(1, "io_set_write(): full-duplex or writing");
io->flags &= ~IO_RW;
io->flags |= IO_WRITE;
io_reload(io);
}
#define IO_READING(io) (((io)->flags & IO_RW) != IO_WRITE)
#define IO_WRITING(io) (((io)->flags & IO_RW) != IO_READ)
/*
* Setup the necessary events as required by the current io state,
* honouring duplex mode and i/o pauses.
*/
void
io_reload(struct io *io)
{
short events;
/* io will be reloaded at release time */
if (io->flags & IO_HELD)
return;
#ifdef IO_SSL
if (io->ssl) {
io_reload_ssl(io);
return;
}
#endif
io_debug("io_reload(%p)\n", 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);
}
/* Set the requested event. */
void
io_reset(struct io *io, short events, void (*dispatch)(int, short, void*))
{
struct timeval tv, *ptv;
io_debug("io_reset(%p, %s, %p) -> %s\n",
io, io_evstr(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;
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);
}
size_t
io_pending(struct io *io)
{
return iobuf_len(io->iobuf);
}
size_t
io_queued(struct io *io)
{
return iobuf_queued(io->iobuf);
}
const char*
io_strflags(int flags)
{
static char buf[64];
buf[0] = '\0';
switch(flags & IO_RW) {
case 0:
strlcat(buf, "rw", sizeof buf);
break;
case IO_READ:
strlcat(buf, "R", sizeof buf);
break;
case IO_WRITE:
strlcat(buf, "W", sizeof buf);
break;
case IO_RW:
strlcat(buf, "RW", sizeof buf);
break;
}
if (flags & IO_PAUSE_IN)
strlcat(buf, ",F_PI", sizeof buf);
if (flags & IO_PAUSE_OUT)
strlcat(buf, ",F_PO", sizeof buf);
return buf;
}
const char*
io_evstr(short ev)
{
static char buf[64];
char buf2[16];
int n;
n = 0;
buf[0] = '\0';
if (ev == 0) {
strlcat(buf, "<NONE>", sizeof(buf));
return buf;
}
if (ev & EV_TIMEOUT) {
if (n)
strlcat(buf, "|", sizeof(buf));
strlcat(buf, "EV_TIMEOUT", sizeof(buf));
ev &= ~EV_TIMEOUT;
n++;
}
if (ev & EV_READ) {
if (n)
strlcat(buf, "|", sizeof(buf));
strlcat(buf, "EV_READ", sizeof(buf));
ev &= ~EV_READ;
n++;
}
if (ev & EV_WRITE) {
if (n)
strlcat(buf, "|", sizeof(buf));
strlcat(buf, "EV_WRITE", sizeof(buf));
ev &= ~EV_WRITE;
n++;
}
if (ev & EV_SIGNAL) {
if (n)
strlcat(buf, "|", sizeof(buf));
strlcat(buf, "EV_SIGNAL", sizeof(buf));
ev &= ~EV_SIGNAL;
n++;
}
if (ev) {
if (n)
strlcat(buf, "|", sizeof(buf));
strlcat(buf, "EV_?=0x", sizeof(buf));
snprintf(buf2, sizeof(buf2), "%hx", ev);
strlcat(buf, buf2, sizeof(buf));
}
return buf;
}
void
io_dispatch(int fd, short ev, void *humppa)
{
struct io *io = humppa;
char buf[64];
size_t w;
ssize_t n;
io_frame_enter("io_dispatch", io, ev);
buf[0] = 0;
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) {
io_callback(io, n == IOBUF_CLOSED ?
IO_DISCONNECTED : IO_ERROR);
goto leave;
}
if (w > io->lowat && w - n <= io->lowat)
io_callback(io, IO_LOWAT);
}
if (ev & EV_READ) {
if ((n = iobuf_read(io->iobuf, io->sock)) < 0) {
io_callback(io, n == IOBUF_CLOSED ?
IO_DISCONNECTED : IO_ERROR);
goto leave;
}
if (n)
io_callback(io, IO_DATAIN);
}
leave:
io_frame_leave(io);
}
void
io_callback(struct io *io, int evt)
{
io->cb(io, evt);
}
int
io_connect(struct io *io, const struct sockaddr *sa)
{
int sock, errno_save;
if ((sock = socket(sa->sa_family, SOCK_STREAM, 0)) == -1)
goto fail;
io_set_blocking(sock, 0);
io_set_linger(sock, 0);
if (connect(sock, sa, sa->sa_len) == -1)
if (errno != EINPROGRESS)
goto fail;
io->sock = sock;
io_reset(io, EV_WRITE, io_dispatch_connect);
return (sock);
fail:
if (sock != -1) {
errno_save = errno;
close(sock);
errno = errno_save;
}
return (-1);
}
void
io_dispatch_connect(int fd, short ev, void *humppa)
{
struct io *io = humppa;
io_frame_enter("io_dispatch_connect", io, ev);
if (ev == EV_TIMEOUT) {
close(fd);
io->sock = -1;
io_callback(io, IO_TIMEOUT);
} else {
io->state = IO_STATE_UP;
io_callback(io, IO_CONNECTED);
}
io_frame_leave(io);
}
#ifdef IO_SSL
int
io_start_tls(struct io *io, void *ssl)
{
int mode;
mode = io->flags & IO_RW;
if (mode == 0 || mode == IO_RW)
errx(1, "io_start_tls(): full-duplex or unset");
if (io->ssl)
errx(1, "io_start_tls(): SSL already started");
io->ssl = ssl;
if (SSL_set_fd(io->ssl, io->sock) == 0) {
ssl_error("io_start_ssl:SSL_set_fd");
return (-1);
}
if (mode == IO_WRITE) {
io->state = IO_STATE_CONNECT_SSL;
SSL_set_connect_state(io->ssl);
io_reset(io, EV_READ | EV_WRITE, io_dispatch_connect_ssl);
} else {
io->state = IO_STATE_ACCEPT_SSL;
SSL_set_accept_state(io->ssl);
io_reset(io, EV_READ | EV_WRITE, io_dispatch_accept_ssl);
}
return (0);
}
void
io_dispatch_accept_ssl(int fd, short event, void *humppa)
{
struct io *io = humppa;
int e, ret;
io_frame_enter("io_dispatch_accept_ssl", io, event);
if (event == EV_TIMEOUT) {
io_callback(io, IO_TIMEOUT);
goto leave;
}
if ((ret = SSL_accept(io->ssl)) > 0) {
io->state = IO_STATE_UP;
io_callback(io, IO_TLSREADY);
goto leave;
}
switch ((e = SSL_get_error(io->ssl, ret))) {
case SSL_ERROR_WANT_READ:
io_reset(io, EV_READ, io_dispatch_accept_ssl);
break;
case SSL_ERROR_WANT_WRITE:
io_reset(io, EV_WRITE, io_dispatch_accept_ssl);
break;
default:
ssl_error("io_dispatch_accept_ssl:SSL_accept");
io_callback(io, IO_ERROR);
break;
}
leave:
io_frame_leave(io);
}
void
io_dispatch_connect_ssl(int fd, short event, void *humppa)
{
struct io *io = humppa;
int e, ret;
io_frame_enter("io_dispatch_connect_ssl", io, event);
if (event == EV_TIMEOUT) {
io_callback(io, IO_TIMEOUT);
goto leave;
}
if ((ret = SSL_connect(io->ssl)) > 0) {
io->state = IO_STATE_UP;
io_callback(io, IO_TLSREADY);
goto leave;
}
switch ((e = SSL_get_error(io->ssl, ret))) {
case SSL_ERROR_WANT_READ:
io_reset(io, EV_READ, io_dispatch_connect_ssl);
break;
case SSL_ERROR_WANT_WRITE:
io_reset(io, EV_WRITE, io_dispatch_connect_ssl);
break;
default:
io_callback(io, IO_ERROR);
break;
}
leave:
io_frame_leave(io);
}
void
io_dispatch_read_ssl(int fd, short event, void *humppa)
{
struct io *io = humppa;
int n;
io_frame_enter("io_dispatch_read_ssl", io, event);
if (event == EV_TIMEOUT) {
io_callback(io, IO_TIMEOUT);
goto leave;
}
switch ((n = iobuf_read_ssl(io->iobuf, (SSL*)io->ssl))) {
case IOBUF_WANT_READ:
io_reset(io, EV_READ, io_dispatch_read_ssl);
break;
case IOBUF_WANT_WRITE:
io_reset(io, EV_WRITE, io_dispatch_read_ssl);
break;
case IOBUF_CLOSED:
io_callback(io, IO_DISCONNECTED);
break;
case IOBUF_ERROR:
io_callback(io, IO_ERROR);
break;
default:
io_debug("io_dispatch_read_ssl(...) -> r=%i\n", n);
io_callback(io, IO_DATAIN);
}
leave:
io_frame_leave(io);
}
void
io_dispatch_write_ssl(int fd, short event, void *humppa)
{
struct io *io = humppa;
int n;
size_t w2, w;
io_frame_enter("io_dispatch_write_ssl", 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->ssl))) {
case IOBUF_WANT_READ:
io_reset(io, EV_READ, io_dispatch_write_ssl);
break;
case IOBUF_WANT_WRITE:
io_reset(io, EV_WRITE, io_dispatch_write_ssl);
break;
case IOBUF_CLOSED:
io_callback(io, IO_DISCONNECTED);
break;
case IOBUF_ERROR:
io_callback(io, IO_ERROR);
break;
default:
io_debug("io_dispatch_write_ssl(...) -> w=%i\n", n);
w2 = io_queued(io);
if (w > io->lowat && w2 <= io->lowat)
io_callback(io, IO_LOWAT);
break;
}
leave:
io_frame_leave(io);
}
void
io_reload_ssl(struct io *io)
{
void (*dispatch)(int, short, void*) = NULL;
switch(io->state) {
case IO_STATE_CONNECT_SSL:
dispatch = io_dispatch_connect_ssl;
break;
case IO_STATE_ACCEPT_SSL:
dispatch = io_dispatch_accept_ssl;
break;
case IO_STATE_UP:
if ((io->flags & IO_RW) == IO_READ)
dispatch = io_dispatch_read_ssl;
else {
if (io_queued(io) == 0)
return; /* nothing to write */
dispatch = io_dispatch_write_ssl;
}
break;
default:
errx(1, "io_reload_ssl(): bad state");
}
io_reset(io, EV_READ | EV_WRITE, dispatch);
}
#endif /* IO_SSL */
|