1 ce062d50 2019-08-21 martijn /* $OpenBSD: iobuf.c,v 1.11 2019/06/12 17:42:53 eric Exp $ */
3 ce062d50 2019-08-21 martijn * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
5 ce062d50 2019-08-21 martijn * Permission to use, copy, modify, and distribute this software for any
6 ce062d50 2019-08-21 martijn * purpose with or without fee is hereby granted, provided that the above
7 ce062d50 2019-08-21 martijn * copyright notice and this permission notice appear in all copies.
9 ce062d50 2019-08-21 martijn * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 ce062d50 2019-08-21 martijn * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 ce062d50 2019-08-21 martijn * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 ce062d50 2019-08-21 martijn * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 ce062d50 2019-08-21 martijn * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 ce062d50 2019-08-21 martijn * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 ce062d50 2019-08-21 martijn * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 ce062d50 2019-08-21 martijn #include <sys/types.h>
19 ce062d50 2019-08-21 martijn #include <sys/socket.h>
20 ce062d50 2019-08-21 martijn #include <sys/uio.h>
22 ce062d50 2019-08-21 martijn #include <errno.h>
23 ce062d50 2019-08-21 martijn #include <limits.h>
24 ce062d50 2019-08-21 martijn #include <stdarg.h>
25 ce062d50 2019-08-21 martijn #include <stdio.h>
26 ce062d50 2019-08-21 martijn #include <stdlib.h>
27 ce062d50 2019-08-21 martijn #include <string.h>
28 ce062d50 2019-08-21 martijn #include <unistd.h>
30 ce062d50 2019-08-21 martijn #ifdef IO_TLS
31 ce062d50 2019-08-21 martijn #include <openssl/err.h>
32 ce062d50 2019-08-21 martijn #include <openssl/ssl.h>
35 ce062d50 2019-08-21 martijn #include "iobuf.h"
37 ce062d50 2019-08-21 martijn #define IOBUF_MAX 65536
38 ce062d50 2019-08-21 martijn #define IOBUFQ_MIN 4096
40 ce062d50 2019-08-21 martijn struct ioqbuf *ioqbuf_alloc(struct iobuf *, size_t);
41 ce062d50 2019-08-21 martijn void iobuf_drain(struct iobuf *, size_t);
44 ce062d50 2019-08-21 martijn iobuf_init(struct iobuf *io, size_t size, size_t max)
46 ce062d50 2019-08-21 martijn memset(io, 0, sizeof *io);
48 ce062d50 2019-08-21 martijn if (max == 0)
49 ce062d50 2019-08-21 martijn max = IOBUF_MAX;
51 ce062d50 2019-08-21 martijn if (size == 0)
52 ce062d50 2019-08-21 martijn size = max;
54 ce062d50 2019-08-21 martijn if (size > max)
55 ce062d50 2019-08-21 martijn return (-1);
57 ce062d50 2019-08-21 martijn if ((io->buf = calloc(size, 1)) == NULL)
58 ce062d50 2019-08-21 martijn return (-1);
60 ce062d50 2019-08-21 martijn io->size = size;
61 ce062d50 2019-08-21 martijn io->max = max;
63 ce062d50 2019-08-21 martijn return (0);
67 ce062d50 2019-08-21 martijn iobuf_clear(struct iobuf *io)
69 ce062d50 2019-08-21 martijn struct ioqbuf *q;
71 ce062d50 2019-08-21 martijn free(io->buf);
73 ce062d50 2019-08-21 martijn while ((q = io->outq)) {
74 ce062d50 2019-08-21 martijn io->outq = q->next;
78 ce062d50 2019-08-21 martijn memset(io, 0, sizeof (*io));
82 ce062d50 2019-08-21 martijn iobuf_drain(struct iobuf *io, size_t n)
84 ce062d50 2019-08-21 martijn struct ioqbuf *q;
85 ce062d50 2019-08-21 martijn size_t left = n;
87 ce062d50 2019-08-21 martijn while ((q = io->outq) && left) {
88 ce062d50 2019-08-21 martijn if ((q->wpos - q->rpos) > left) {
89 ce062d50 2019-08-21 martijn q->rpos += left;
90 ce062d50 2019-08-21 martijn left = 0;
92 ce062d50 2019-08-21 martijn left -= q->wpos - q->rpos;
93 ce062d50 2019-08-21 martijn io->outq = q->next;
98 ce062d50 2019-08-21 martijn io->queued -= (n - left);
99 ce062d50 2019-08-21 martijn if (io->outq == NULL)
100 ce062d50 2019-08-21 martijn io->outqlast = NULL;
104 ce062d50 2019-08-21 martijn iobuf_extend(struct iobuf *io, size_t n)
106 ce062d50 2019-08-21 martijn char *t;
108 ce062d50 2019-08-21 martijn if (n > io->max)
109 ce062d50 2019-08-21 martijn return (-1);
111 ce062d50 2019-08-21 martijn if (io->max - io->size < n)
112 ce062d50 2019-08-21 martijn return (-1);
114 ce062d50 2019-08-21 martijn t = recallocarray(io->buf, io->size, io->size + n, 1);
115 ce062d50 2019-08-21 martijn if (t == NULL)
116 ce062d50 2019-08-21 martijn return (-1);
118 ce062d50 2019-08-21 martijn io->size += n;
119 ce062d50 2019-08-21 martijn io->buf = t;
121 ce062d50 2019-08-21 martijn return (0);
125 ce062d50 2019-08-21 martijn iobuf_left(struct iobuf *io)
127 ce062d50 2019-08-21 martijn return io->size - io->wpos;
131 ce062d50 2019-08-21 martijn iobuf_space(struct iobuf *io)
133 ce062d50 2019-08-21 martijn return io->size - (io->wpos - io->rpos);
137 ce062d50 2019-08-21 martijn iobuf_len(struct iobuf *io)
139 ce062d50 2019-08-21 martijn return io->wpos - io->rpos;
143 ce062d50 2019-08-21 martijn iobuf_data(struct iobuf *io)
145 ce062d50 2019-08-21 martijn return io->buf + io->rpos;
149 ce062d50 2019-08-21 martijn iobuf_drop(struct iobuf *io, size_t n)
151 ce062d50 2019-08-21 martijn if (n >= iobuf_len(io)) {
152 ce062d50 2019-08-21 martijn io->rpos = io->wpos = 0;
156 ce062d50 2019-08-21 martijn io->rpos += n;
160 ce062d50 2019-08-21 martijn iobuf_getline(struct iobuf *iobuf, size_t *rlen)
162 ce062d50 2019-08-21 martijn char *buf;
163 ce062d50 2019-08-21 martijn size_t len, i;
165 ce062d50 2019-08-21 martijn buf = iobuf_data(iobuf);
166 ce062d50 2019-08-21 martijn len = iobuf_len(iobuf);
168 ce062d50 2019-08-21 martijn for (i = 0; i + 1 <= len; i++)
169 ce062d50 2019-08-21 martijn if (buf[i] == '\n') {
170 ce062d50 2019-08-21 martijn /* Note: the returned address points into the iobuf
171 ce062d50 2019-08-21 martijn * buffer. We NUL-end it for convenience, and discard
172 ce062d50 2019-08-21 martijn * the data from the iobuf, so that the caller doesn't
173 ce062d50 2019-08-21 martijn * have to do it. The data remains "valid" as long
174 ce062d50 2019-08-21 martijn * as the iobuf does not overwrite it, that is until
175 ce062d50 2019-08-21 martijn * the next call to iobuf_normalize() or iobuf_extend().
177 ce062d50 2019-08-21 martijn iobuf_drop(iobuf, i + 1);
178 ce062d50 2019-08-21 martijn len = (i && buf[i - 1] == '\r') ? i - 1 : i;
179 ce062d50 2019-08-21 martijn buf[len] = '\0';
180 ce062d50 2019-08-21 martijn if (rlen)
181 ce062d50 2019-08-21 martijn *rlen = len;
182 ce062d50 2019-08-21 martijn return (buf);
185 ce062d50 2019-08-21 martijn return (NULL);
189 ce062d50 2019-08-21 martijn iobuf_normalize(struct iobuf *io)
191 ce062d50 2019-08-21 martijn if (io->rpos == 0)
194 ce062d50 2019-08-21 martijn if (io->rpos == io->wpos) {
195 ce062d50 2019-08-21 martijn io->rpos = io->wpos = 0;
199 ce062d50 2019-08-21 martijn memmove(io->buf, io->buf + io->rpos, io->wpos - io->rpos);
200 ce062d50 2019-08-21 martijn io->wpos -= io->rpos;
201 ce062d50 2019-08-21 martijn io->rpos = 0;
205 ce062d50 2019-08-21 martijn iobuf_read(struct iobuf *io, int fd)
207 ce062d50 2019-08-21 martijn ssize_t n;
209 ce062d50 2019-08-21 martijn n = read(fd, io->buf + io->wpos, iobuf_left(io));
210 ce062d50 2019-08-21 martijn if (n == -1) {
211 ce062d50 2019-08-21 martijn /* XXX is this really what we want? */
212 ce062d50 2019-08-21 martijn if (errno == EAGAIN || errno == EINTR)
213 ce062d50 2019-08-21 martijn return (IOBUF_WANT_READ);
214 ce062d50 2019-08-21 martijn return (IOBUF_ERROR);
216 ce062d50 2019-08-21 martijn if (n == 0)
217 ce062d50 2019-08-21 martijn return (IOBUF_CLOSED);
219 ce062d50 2019-08-21 martijn io->wpos += n;
221 ce062d50 2019-08-21 martijn return (n);
224 ce062d50 2019-08-21 martijn struct ioqbuf *
225 ce062d50 2019-08-21 martijn ioqbuf_alloc(struct iobuf *io, size_t len)
227 ce062d50 2019-08-21 martijn struct ioqbuf *q;
229 ce062d50 2019-08-21 martijn if (len < IOBUFQ_MIN)
230 ce062d50 2019-08-21 martijn len = IOBUFQ_MIN;
232 ce062d50 2019-08-21 martijn if ((q = malloc(sizeof(*q) + len)) == NULL)
233 ce062d50 2019-08-21 martijn return (NULL);
235 ce062d50 2019-08-21 martijn q->rpos = 0;
236 ce062d50 2019-08-21 martijn q->wpos = 0;
237 ce062d50 2019-08-21 martijn q->size = len;
238 ce062d50 2019-08-21 martijn q->next = NULL;
239 ce062d50 2019-08-21 martijn q->buf = (char *)(q) + sizeof(*q);
241 ce062d50 2019-08-21 martijn if (io->outqlast == NULL)
242 ce062d50 2019-08-21 martijn io->outq = q;
244 ce062d50 2019-08-21 martijn io->outqlast->next = q;
245 ce062d50 2019-08-21 martijn io->outqlast = q;
247 ce062d50 2019-08-21 martijn return (q);
251 ce062d50 2019-08-21 martijn iobuf_queued(struct iobuf *io)
253 ce062d50 2019-08-21 martijn return io->queued;
257 ce062d50 2019-08-21 martijn iobuf_reserve(struct iobuf *io, size_t len)
259 ce062d50 2019-08-21 martijn struct ioqbuf *q;
260 ce062d50 2019-08-21 martijn void *r;
262 ce062d50 2019-08-21 martijn if (len == 0)
263 ce062d50 2019-08-21 martijn return (NULL);
265 ce062d50 2019-08-21 martijn if (((q = io->outqlast) == NULL) || q->size - q->wpos <= len) {
266 ce062d50 2019-08-21 martijn if ((q = ioqbuf_alloc(io, len)) == NULL)
267 ce062d50 2019-08-21 martijn return (NULL);
270 ce062d50 2019-08-21 martijn r = q->buf + q->wpos;
271 ce062d50 2019-08-21 martijn q->wpos += len;
272 ce062d50 2019-08-21 martijn io->queued += len;
274 ce062d50 2019-08-21 martijn return (r);
278 ce062d50 2019-08-21 martijn iobuf_queue(struct iobuf *io, const void *data, size_t len)
280 ce062d50 2019-08-21 martijn void *buf;
282 ce062d50 2019-08-21 martijn if (len == 0)
283 ce062d50 2019-08-21 martijn return (0);
285 ce062d50 2019-08-21 martijn if ((buf = iobuf_reserve(io, len)) == NULL)
286 ce062d50 2019-08-21 martijn return (-1);
288 ce062d50 2019-08-21 martijn memmove(buf, data, len);
290 ce062d50 2019-08-21 martijn return (len);
294 ce062d50 2019-08-21 martijn iobuf_queuev(struct iobuf *io, const struct iovec *iov, int iovcnt)
297 ce062d50 2019-08-21 martijn size_t len = 0;
298 ce062d50 2019-08-21 martijn char *buf;
300 ce062d50 2019-08-21 martijn for (i = 0; i < iovcnt; i++)
301 ce062d50 2019-08-21 martijn len += iov[i].iov_len;
303 ce062d50 2019-08-21 martijn if ((buf = iobuf_reserve(io, len)) == NULL)
304 ce062d50 2019-08-21 martijn return (-1);
306 ce062d50 2019-08-21 martijn for (i = 0; i < iovcnt; i++) {
307 ce062d50 2019-08-21 martijn if (iov[i].iov_len == 0)
308 ce062d50 2019-08-21 martijn continue;
309 ce062d50 2019-08-21 martijn memmove(buf, iov[i].iov_base, iov[i].iov_len);
310 ce062d50 2019-08-21 martijn buf += iov[i].iov_len;
313 ce062d50 2019-08-21 martijn return (0);
318 ce062d50 2019-08-21 martijn iobuf_fqueue(struct iobuf *io, const char *fmt, ...)
320 ce062d50 2019-08-21 martijn va_list ap;
321 ce062d50 2019-08-21 martijn int len;
323 ce062d50 2019-08-21 martijn va_start(ap, fmt);
324 ce062d50 2019-08-21 martijn len = iobuf_vfqueue(io, fmt, ap);
325 ce062d50 2019-08-21 martijn va_end(ap);
327 ce062d50 2019-08-21 martijn return (len);
331 ce062d50 2019-08-21 martijn iobuf_vfqueue(struct iobuf *io, const char *fmt, va_list ap)
333 ce062d50 2019-08-21 martijn char *buf;
334 ce062d50 2019-08-21 martijn int len;
336 ce062d50 2019-08-21 martijn len = vasprintf(&buf, fmt, ap);
338 ce062d50 2019-08-21 martijn if (len == -1)
339 ce062d50 2019-08-21 martijn return (-1);
341 ce062d50 2019-08-21 martijn len = iobuf_queue(io, buf, len);
342 ce062d50 2019-08-21 martijn free(buf);
344 ce062d50 2019-08-21 martijn return (len);
348 ce062d50 2019-08-21 martijn iobuf_write(struct iobuf *io, int fd)
350 ce062d50 2019-08-21 martijn struct iovec iov[IOV_MAX];
351 ce062d50 2019-08-21 martijn struct ioqbuf *q;
353 ce062d50 2019-08-21 martijn ssize_t n;
356 ce062d50 2019-08-21 martijn for (q = io->outq; q ; q = q->next) {
357 ce062d50 2019-08-21 martijn if (i >= IOV_MAX)
359 ce062d50 2019-08-21 martijn iov[i].iov_base = q->buf + q->rpos;
360 ce062d50 2019-08-21 martijn iov[i].iov_len = q->wpos - q->rpos;
364 ce062d50 2019-08-21 martijn n = writev(fd, iov, i);
365 ce062d50 2019-08-21 martijn if (n == -1) {
366 ce062d50 2019-08-21 martijn if (errno == EAGAIN || errno == EINTR)
367 ce062d50 2019-08-21 martijn return (IOBUF_WANT_WRITE);
368 ce062d50 2019-08-21 martijn if (errno == EPIPE)
369 ce062d50 2019-08-21 martijn return (IOBUF_CLOSED);
370 ce062d50 2019-08-21 martijn return (IOBUF_ERROR);
373 ce062d50 2019-08-21 martijn iobuf_drain(io, n);
375 ce062d50 2019-08-21 martijn return (n);
379 ce062d50 2019-08-21 martijn iobuf_flush(struct iobuf *io, int fd)
381 ce062d50 2019-08-21 martijn ssize_t s;
383 ce062d50 2019-08-21 martijn while (io->queued)
384 ce062d50 2019-08-21 martijn if ((s = iobuf_write(io, fd)) < 0)
385 ce062d50 2019-08-21 martijn return (s);
387 ce062d50 2019-08-21 martijn return (0);
390 ce062d50 2019-08-21 martijn #ifdef IO_TLS
393 ce062d50 2019-08-21 martijn iobuf_flush_tls(struct iobuf *io, void *tls)
395 ce062d50 2019-08-21 martijn ssize_t s;
397 ce062d50 2019-08-21 martijn while (io->queued)
398 ce062d50 2019-08-21 martijn if ((s = iobuf_write_tls(io, tls)) < 0)
399 ce062d50 2019-08-21 martijn return (s);
401 ce062d50 2019-08-21 martijn return (0);
405 ce062d50 2019-08-21 martijn iobuf_write_tls(struct iobuf *io, void *tls)
407 ce062d50 2019-08-21 martijn struct ioqbuf *q;
409 ce062d50 2019-08-21 martijn ssize_t n;
411 ce062d50 2019-08-21 martijn q = io->outq;
412 ce062d50 2019-08-21 martijn n = SSL_write(tls, q->buf + q->rpos, q->wpos - q->rpos);
413 ce062d50 2019-08-21 martijn if (n <= 0) {
414 ce062d50 2019-08-21 martijn switch ((r = SSL_get_error(tls, n))) {
415 ce062d50 2019-08-21 martijn case SSL_ERROR_WANT_READ:
416 ce062d50 2019-08-21 martijn return (IOBUF_WANT_READ);
417 ce062d50 2019-08-21 martijn case SSL_ERROR_WANT_WRITE:
418 ce062d50 2019-08-21 martijn return (IOBUF_WANT_WRITE);
419 ce062d50 2019-08-21 martijn case SSL_ERROR_ZERO_RETURN: /* connection closed */
420 ce062d50 2019-08-21 martijn return (IOBUF_CLOSED);
421 ce062d50 2019-08-21 martijn case SSL_ERROR_SYSCALL:
422 ce062d50 2019-08-21 martijn if (ERR_peek_last_error())
423 ce062d50 2019-08-21 martijn return (IOBUF_TLSERROR);
424 ce062d50 2019-08-21 martijn if (r == 0)
425 ce062d50 2019-08-21 martijn errno = EPIPE;
426 ce062d50 2019-08-21 martijn return (IOBUF_ERROR);
427 ce062d50 2019-08-21 martijn default:
428 ce062d50 2019-08-21 martijn return (IOBUF_TLSERROR);
431 ce062d50 2019-08-21 martijn iobuf_drain(io, n);
433 ce062d50 2019-08-21 martijn return (n);
437 ce062d50 2019-08-21 martijn iobuf_read_tls(struct iobuf *io, void *tls)
439 ce062d50 2019-08-21 martijn ssize_t n;
442 ce062d50 2019-08-21 martijn n = SSL_read(tls, io->buf + io->wpos, iobuf_left(io));
443 ce062d50 2019-08-21 martijn if (n < 0) {
444 ce062d50 2019-08-21 martijn switch ((r = SSL_get_error(tls, n))) {
445 ce062d50 2019-08-21 martijn case SSL_ERROR_WANT_READ:
446 ce062d50 2019-08-21 martijn return (IOBUF_WANT_READ);
447 ce062d50 2019-08-21 martijn case SSL_ERROR_WANT_WRITE:
448 ce062d50 2019-08-21 martijn return (IOBUF_WANT_WRITE);
449 ce062d50 2019-08-21 martijn case SSL_ERROR_SYSCALL:
450 ce062d50 2019-08-21 martijn if (ERR_peek_last_error())
451 ce062d50 2019-08-21 martijn return (IOBUF_TLSERROR);
452 ce062d50 2019-08-21 martijn if (r == 0)
453 ce062d50 2019-08-21 martijn errno = EPIPE;
454 ce062d50 2019-08-21 martijn return (IOBUF_ERROR);
455 ce062d50 2019-08-21 martijn default:
456 ce062d50 2019-08-21 martijn return (IOBUF_TLSERROR);
458 ce062d50 2019-08-21 martijn } else if (n == 0)
459 ce062d50 2019-08-21 martijn return (IOBUF_CLOSED);
461 ce062d50 2019-08-21 martijn io->wpos += n;
463 ce062d50 2019-08-21 martijn return (n);
466 ce062d50 2019-08-21 martijn #endif /* IO_TLS */