-
Notifications
You must be signed in to change notification settings - Fork 50
/
io.c
737 lines (623 loc) · 15.4 KB
/
io.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
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
/* $Id$ */
/*
* Copyright (c) 2005 Nicholas Marriott <nicholas.marriott@gmail.com>
*
* 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 MIND, 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/time.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include "fdm.h"
#define IO_DEBUG(io, fmt, ...)
#ifndef IO_DEBUG
#define IO_DEBUG(io, fmt, ...) \
log_debug3("%s: (%d) " fmt, __func__, io->fd, ## __VA_ARGS__)
#endif
int io_before_poll(struct io *, struct pollfd *);
int io_after_poll(struct io *, struct pollfd *);
int io_push(struct io *);
int io_fill(struct io *);
/* Create a struct io for the specified socket and SSL descriptors. */
struct io *
io_create(int fd, SSL *ssl, const char *eol)
{
struct io *io;
int mode;
io = xcalloc(1, sizeof *io);
io->fd = fd;
io->ssl = ssl;
io->dup_fd = -1;
/* Set non-blocking. */
if ((mode = fcntl(fd, F_GETFL)) == -1)
fatal("fcntl failed");
if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
fatal("fcntl failed");
io->flags = 0;
io->error = NULL;
io->rd = buffer_create(IO_BLOCKSIZE);
io->wr = buffer_create(IO_BLOCKSIZE);
io->lbuf = NULL;
io->llen = 0;
io->eol = eol;
return (io);
}
/* Mark io as read only. */
void
io_readonly(struct io *io)
{
buffer_destroy(io->wr);
io->wr = NULL;
}
/* Mark io as write only. */
void
io_writeonly(struct io *io)
{
buffer_destroy(io->rd);
io->rd = NULL;
}
/* Free a struct io. */
void
io_free(struct io *io)
{
if (io->lbuf != NULL)
xfree(io->lbuf);
if (io->error != NULL)
xfree(io->error);
if (io->rd != NULL)
buffer_destroy(io->rd);
if (io->wr != NULL)
buffer_destroy(io->wr);
xfree(io);
}
/* Close io sockets. */
void
io_close(struct io *io)
{
if (io->ssl != NULL) {
SSL_CTX_free(SSL_get_SSL_CTX(io->ssl));
SSL_free(io->ssl);
}
close(io->fd);
}
/* Poll the io. */
int
io_poll(struct io *io, int timeout, char **cause)
{
return (io_polln(&io, 1, NULL, timeout, cause));
}
/* Poll multiple IOs. */
int
io_polln(struct io **iop, u_int n, struct io **rio, int timeout, char **cause)
{
struct io *io;
struct pollfd *pfds;
int error;
u_int i;
/* Fill in all the pollfds. */
pfds = xcalloc(n, sizeof *pfds);
for (i = 0; i < n; i++) {
io = iop[i];
if (rio != NULL)
*rio = io;
switch (io_before_poll(io, &pfds[i])) {
case 0:
/* Found a closed io. */
xfree(pfds);
return (0);
case -1:
goto error;
}
}
/* Do the poll. */
error = poll(pfds, n, timeout);
if (error == 0 || error == -1) {
IO_DEBUG(io, "poll returned: %d (errno=%d)", error, errno);
xfree(pfds);
if (error == 0) {
if (timeout == 0) {
errno = EAGAIN;
return (-1);
}
errno = ETIMEDOUT;
}
if (errno == EINTR)
return (1);
if (rio != NULL)
*rio = NULL;
if (cause != NULL)
xasprintf(cause, "io: poll: %s", strerror(errno));
return (-1);
}
/* Check all the ios. */
for (i = 0; i < n; i++) {
io = iop[i];
if (rio != NULL)
*rio = io;
if (io_after_poll(io, &pfds[i]) == -1)
goto error;
}
xfree(pfds);
return (1);
error:
if (cause != NULL)
*cause = xstrdup(io->error);
xfree(pfds);
errno = 0;
return (-1);
}
/* Set up an io for polling. */
int
io_before_poll(struct io *io, struct pollfd *pfd)
{
/* If io is NULL, don't let poll do anything with this one. */
if (io == NULL) {
memset(pfd, 0, sizeof *pfd);
pfd->fd = -1;
return (1);
}
/* Check for errors or closure. */
if (io->error != NULL)
return (-1);
if (IO_CLOSED(io))
return (0);
/* Fill in pollfd. */
memset(pfd, 0, sizeof *pfd);
if (io->ssl != NULL)
pfd->fd = SSL_get_fd(io->ssl);
else
pfd->fd = io->fd;
if (io->rd != NULL)
pfd->events |= POLLIN;
if (io->wr != NULL && (BUFFER_USED(io->wr) != 0 ||
(io->flags & (IOF_NEEDFILL|IOF_NEEDPUSH|IOF_MUSTWR)) != 0))
pfd->events |= POLLOUT;
IO_DEBUG(io, "poll in: 0x%03x", pfd->events);
return (1);
}
/* Handle io after polling. */
int
io_after_poll(struct io *io, struct pollfd *pfd)
{
/* Ignore NULL ios. */
if (io == NULL)
return (1);
IO_DEBUG(io, "poll out: 0x%03x", pfd->revents);
/* Close on POLLERR or POLLNVAL hard. */
if (pfd->revents & (POLLERR|POLLNVAL)) {
io->flags |= IOF_CLOSED;
return (0);
}
/* Close on POLLHUP but only if there is nothing to read. */
if (pfd->revents & POLLHUP && (pfd->revents & POLLIN) == 0) {
io->flags |= IOF_CLOSED;
return (0);
}
/* Check for repeated read/write. */
if ((io->flags & (IOF_NEEDPUSH|IOF_NEEDFILL)) != 0) {
/*
* If a repeated read/write is necessary, the socket must be
* ready for both reading and writing
*/
if (pfd->revents & (POLLOUT|POLLIN)) {
if (io->flags & IOF_NEEDPUSH) {
switch (io_push(io)) {
case 0:
io->flags |= IOF_CLOSED;
return (0);
case -1:
return (-1);
}
}
if (io->flags & IOF_NEEDFILL) {
switch (io_fill(io)) {
case 0:
io->flags |= IOF_CLOSED;
return (0);
case -1:
return (-1);
}
}
}
return (1);
}
/* Otherwise try to read and write. */
if (io->wr != NULL && pfd->revents & POLLOUT) {
switch (io_push(io)) {
case 0:
io->flags |= IOF_CLOSED;
return (0);
case -1:
return (-1);
}
}
if (io->rd != NULL && pfd->revents & POLLIN) {
switch (io_fill(io)) {
case 0:
io->flags |= IOF_CLOSED;
return (0);
case -1:
return (-1);
}
}
return (1);
}
/*
* Fill read buffer. Returns 0 for closed, -1 for error, 1 for success,
* a la read(2).
*/
int
io_fill(struct io *io)
{
ssize_t n;
int error;
again:
/* Ensure there is at least some minimum space in the buffer. */
buffer_ensure(io->rd, IO_WATERMARK);
/* Attempt to read as much as the buffer has available. */
if (io->ssl == NULL) {
n = read(io->fd, BUFFER_IN(io->rd), BUFFER_FREE(io->rd));
IO_DEBUG(io, "read returned %zd (errno=%d)", n, errno);
if (n == 0 || (n == -1 && errno == EPIPE))
return (0);
if (n == -1 && errno != EINTR && errno != EAGAIN) {
if (io->error != NULL)
xfree(io->error);
xasprintf(&io->error, "io: read: %s", strerror(errno));
return (-1);
}
} else {
n = SSL_read(io->ssl, BUFFER_IN(io->rd), BUFFER_FREE(io->rd));
IO_DEBUG(io, "SSL_read returned %zd", n);
if (n == 0)
return (0);
if (n < 0) {
switch (error = SSL_get_error(io->ssl, n)) {
case SSL_ERROR_WANT_READ:
/*
* A repeat is certain (poll on the socket will
* still return data ready) so this can be
* ignored.
*/
break;
case SSL_ERROR_WANT_WRITE:
io->flags |= IOF_NEEDFILL;
break;
case SSL_ERROR_SYSCALL:
if (errno == EAGAIN || errno == EINTR)
break;
/* FALLTHROUGH */
default:
if (io->error != NULL)
xfree(io->error);
io->error = sslerror2(error, "SSL_read");
return (-1);
}
}
}
/* Test for > 0 since SSL_read can return any -ve on error. */
if (n > 0) {
IO_DEBUG(io, "read %zd bytes", n);
/* Copy out the duplicate fd. Errors are just ignored. */
if (io->dup_fd != -1) {
write(io->dup_fd, "< ", 2);
write(io->dup_fd, BUFFER_IN(io->rd), n);
}
/* Adjust the buffer size. */
buffer_add(io->rd, n);
/* Reset the need flags. */
io->flags &= ~IOF_NEEDFILL;
goto again;
}
return (1);
}
/* Empty write buffer. */
int
io_push(struct io *io)
{
ssize_t n;
int error;
/* If nothing to write, return. */
if (BUFFER_USED(io->wr) == 0)
return (1);
/* Write as much as possible. */
if (io->ssl == NULL) {
n = write(io->fd, BUFFER_OUT(io->wr), BUFFER_USED(io->wr));
IO_DEBUG(io, "write returned %zd (errno=%d)", n, errno);
if (n == 0 || (n == -1 && errno == EPIPE))
return (0);
if (n == -1 && errno != EINTR && errno != EAGAIN) {
if (io->error != NULL)
xfree(io->error);
xasprintf(&io->error, "io: write: %s", strerror(errno));
return (-1);
}
} else {
n = SSL_write(io->ssl, BUFFER_OUT(io->wr), BUFFER_USED(io->wr));
IO_DEBUG(io, "SSL_write returned %zd", n);
if (n == 0)
return (0);
if (n < 0) {
switch (error = SSL_get_error(io->ssl, n)) {
case SSL_ERROR_WANT_READ:
io->flags |= IOF_NEEDPUSH;
break;
case SSL_ERROR_WANT_WRITE:
/*
* A repeat is certain (buffer still has data)
* so this can be ignored
*/
break;
case SSL_ERROR_SYSCALL:
if (errno == EAGAIN || errno == EINTR)
break;
/* FALLTHROUGH */
default:
if (io->error != NULL)
xfree(io->error);
io->error = sslerror2(error, "SSL_write");
return (-1);
}
}
}
/* Test for > 0 since SSL_write can return any -ve on error. */
if (n > 0) {
IO_DEBUG(io, "wrote %zd bytes", n);
/* Copy out the duplicate fd. */
if (io->dup_fd != -1) {
write(io->dup_fd, "> ", 2);
write(io->dup_fd, BUFFER_OUT(io->wr), n);
}
/* Adjust the buffer size. */
buffer_remove(io->wr, n);
/* Reset the need flags. */
io->flags &= ~IOF_NEEDPUSH;
}
return (1);
}
/* Return a specific number of bytes from the read buffer, if available. */
void *
io_read(struct io *io, size_t len)
{
void *buf;
IO_DEBUG(io, "in: %zu bytes, rd: used=%zu, free=%zu", len,
BUFFER_USED(io->rd), BUFFER_FREE(io->rd));
if (io->error != NULL)
return (NULL);
if (BUFFER_USED(io->rd) < len)
return (NULL);
buf = xmalloc(len);
buffer_read(io->rd, buf, len);
IO_DEBUG(io, "out: %zu bytes, rd: used=%zu, free=%zu", len,
BUFFER_USED(io->rd), BUFFER_FREE(io->rd));
return (buf);
}
/* Return a specific number of bytes from the read buffer, if available. */
int
io_read2(struct io *io, void *buf, size_t len)
{
if (io->error != NULL)
return (-1);
IO_DEBUG(io, "in: %zu bytes, rd: used=%zu, free=%zu", len,
BUFFER_USED(io->rd), BUFFER_FREE(io->rd));
if (BUFFER_USED(io->rd) < len)
return (1);
buffer_read(io->rd, buf, len);
IO_DEBUG(io, "out: %zu bytes, rd: used=%zu, free=%zu", len,
BUFFER_USED(io->rd), BUFFER_FREE(io->rd));
return (0);
}
/* Write a block to the io write buffer. */
void
io_write(struct io *io, const void *buf, size_t len)
{
if (io->error != NULL)
return;
IO_DEBUG(io, "in: %zu bytes, wr: used=%zu, free=%zu", len,
BUFFER_USED(io->wr), BUFFER_FREE(io->wr));
buffer_write(io->wr, buf, len);
IO_DEBUG(io, "out: %zu bytes, wr: used=%zu, free=%zu", len,
BUFFER_USED(io->wr), BUFFER_FREE(io->wr));
}
/*
* Return a line from the read buffer. EOL is stripped and the string returned
* is zero-terminated.
*/
char *
io_readline2(struct io *io, char **buf, size_t *len)
{
char *ptr, *base;
size_t size, maxlen, eollen;
if (io->error != NULL)
return (NULL);
maxlen = BUFFER_USED(io->rd);
if (maxlen > IO_MAXLINELEN)
maxlen = IO_MAXLINELEN;
eollen = strlen(io->eol);
if (BUFFER_USED(io->rd) < eollen)
return (NULL);
IO_DEBUG(io, "in: rd: used=%zu, free=%zu",
BUFFER_USED(io->rd), BUFFER_FREE(io->rd));
base = ptr = BUFFER_OUT(io->rd);
for (;;) {
/* Find the first character in the EOL string. */
ptr = memchr(ptr, *io->eol, maxlen - (ptr - base));
if (ptr != NULL) {
/* Found. Is there enough space for the rest? */
if (ptr - base + eollen > maxlen) {
/*
* No, this isn't it. Set ptr to NULL to handle
* as not found.
*/
ptr = NULL;
} else if (strncmp(ptr, io->eol, eollen) == 0) {
/* This is an EOL. */
size = ptr - base;
break;
}
}
if (ptr == NULL) {
IO_DEBUG(io,
"not found (%zu, %d)", maxlen, IO_CLOSED(io));
/*
* Not found within the length searched. If that was
* the maximum length, this is an error.
*/
if (maxlen == IO_MAXLINELEN) {
if (io->error != NULL)
xfree(io->error);
io->error =
xstrdup("io: maximum line length exceeded");
return (NULL);
}
/*
* If the socket has closed, just return all the data
* (the buffer is known to be at least eollen long).
*/
if (!IO_CLOSED(io))
return (NULL);
size = BUFFER_USED(io->rd);
ENSURE_FOR(*buf, *len, size, 1);
buffer_read(io->rd, *buf, size);
(*buf)[size] = '\0';
return (*buf);
}
/* Start again from the next character. */
ptr++;
}
/* Copy the line and remove it from the buffer. */
ENSURE_FOR(*buf, *len, size, 1);
if (size != 0)
buffer_read(io->rd, *buf, size);
(*buf)[size] = '\0';
/* Discard the EOL from the buffer. */
buffer_remove(io->rd, eollen);
IO_DEBUG(io, "out: %zu bytes, rd: used=%zu, free=%zu",
size, BUFFER_USED(io->rd), BUFFER_FREE(io->rd));
return (*buf);
}
/* Return a line from the read buffer in a new buffer. */
char *
io_readline(struct io *io)
{
char *line;
if (io->error != NULL)
return (NULL);
if (io->lbuf == NULL) {
io->llen = IO_LINESIZE;
io->lbuf = xmalloc(io->llen);
}
if ((line = io_readline2(io, &io->lbuf, &io->llen)) != NULL)
io->lbuf = NULL;
return (line);
}
/* Write a line to the io write buffer. */
void printflike2
io_writeline(struct io *io, const char *fmt, ...)
{
va_list ap;
if (io->error != NULL)
return;
va_start(ap, fmt);
io_vwriteline(io, fmt, ap);
va_end(ap);
}
/* Write a line to the io write buffer from a va_list. */
void
io_vwriteline(struct io *io, const char *fmt, va_list ap)
{
int n;
va_list aq;
if (io->error != NULL)
return;
IO_DEBUG(io, "in: wr: used=%zu, free=%zu",
BUFFER_USED(io->wr), BUFFER_FREE(io->wr));
if (fmt != NULL) {
va_copy(aq, ap);
n = xvsnprintf(NULL, 0, fmt, aq);
va_end(aq);
if (n != 0) {
buffer_ensure(io->wr, n + 1);
xvsnprintf(BUFFER_IN(io->wr), n + 1, fmt, ap);
buffer_add(io->wr, n);
}
} else
n = 0;
io_write(io, io->eol, strlen(io->eol));
IO_DEBUG(io, "out: %zu bytes, wr: used=%zu, free=%zu",
n + strlen(io->eol), BUFFER_USED(io->wr), BUFFER_FREE(io->wr));
}
/* Poll until a line is received. */
int
io_pollline(struct io *io, char **line, int timeout, char **cause)
{
int res;
if (io->lbuf == NULL) {
io->llen = IO_LINESIZE;
io->lbuf = xmalloc(io->llen);
}
res = io_pollline2(io, line, &io->lbuf, &io->llen, timeout, cause);
if (res == 1)
io->lbuf = NULL;
return (res);
}
/* Poll until a line is received, using a user buffer. */
int
io_pollline2(struct io *io, char **line, char **buf, size_t *len, int timeout,
char **cause)
{
int res;
for (;;) {
*line = io_readline2(io, buf, len);
if (*line != NULL)
return (1);
if ((res = io_poll(io, timeout, cause)) != 1)
return (res);
}
}
/* Poll until all data in the write buffer has been written to the socket. */
int
io_flush(struct io *io, int timeout, char **cause)
{
while (BUFFER_USED(io->wr) != 0) {
if (io_poll(io, timeout, cause) != 1)
return (-1);
}
return (0);
}
/* Poll until len bytes have been read into the read buffer. */
int
io_wait(struct io *io, size_t len, int timeout, char **cause)
{
while (BUFFER_USED(io->rd) < len) {
if (io_poll(io, timeout, cause) != 1)
return (-1);
}
return (0);
}
/* Poll if there is lots of data to write. */
int
io_update(struct io *io, int timeout, char **cause)
{
if (BUFFER_USED(io->wr) < IO_FLUSHSIZE)
return (1);
return (io_poll(io, timeout, cause));
}