1 /* $OpenBSD: iobuf.c,v 1.11 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.
21 #include <sys/types.h>
22 #include <sys/socket.h>
34 #include <openssl/err.h>
35 #include <openssl/ssl.h>
38 #include "openbsd-compat.h"
41 #define IOBUF_MAX 65536
42 #define IOBUFQ_MIN 4096
44 struct ioqbuf *ioqbuf_alloc(struct iobuf *, size_t);
45 void iobuf_drain(struct iobuf *, size_t);
48 iobuf_init(struct iobuf *io, size_t size, size_t max)
50 memset(io, 0, sizeof *io);
61 if ((io->buf = calloc(size, 1)) == NULL)
71 iobuf_clear(struct iobuf *io)
77 while ((q = io->outq)) {
82 memset(io, 0, sizeof (*io));
86 iobuf_drain(struct iobuf *io, size_t n)
91 while ((q = io->outq) && left) {
92 if ((q->wpos - q->rpos) > left) {
96 left -= q->wpos - q->rpos;
102 io->queued -= (n - left);
103 if (io->outq == NULL)
108 iobuf_extend(struct iobuf *io, size_t n)
115 if (io->max - io->size < n)
118 t = recallocarray(io->buf, io->size, io->size + n, 1);
129 iobuf_left(struct iobuf *io)
131 return io->size - io->wpos;
135 iobuf_space(struct iobuf *io)
137 return io->size - (io->wpos - io->rpos);
141 iobuf_len(struct iobuf *io)
143 return io->wpos - io->rpos;
147 iobuf_data(struct iobuf *io)
149 return io->buf + io->rpos;
153 iobuf_drop(struct iobuf *io, size_t n)
155 if (n >= iobuf_len(io)) {
156 io->rpos = io->wpos = 0;
164 iobuf_getline(struct iobuf *iobuf, size_t *rlen)
169 buf = iobuf_data(iobuf);
170 len = iobuf_len(iobuf);
172 for (i = 0; i + 1 <= len; i++)
173 if (buf[i] == '\n') {
174 /* Note: the returned address points into the iobuf
175 * buffer. We NUL-end it for convenience, and discard
176 * the data from the iobuf, so that the caller doesn't
177 * have to do it. The data remains "valid" as long
178 * as the iobuf does not overwrite it, that is until
179 * the next call to iobuf_normalize() or iobuf_extend().
181 iobuf_drop(iobuf, i + 1);
182 len = (i && buf[i - 1] == '\r') ? i - 1 : i;
193 iobuf_normalize(struct iobuf *io)
198 if (io->rpos == io->wpos) {
199 io->rpos = io->wpos = 0;
203 memmove(io->buf, io->buf + io->rpos, io->wpos - io->rpos);
204 io->wpos -= io->rpos;
209 iobuf_read(struct iobuf *io, int fd)
213 n = read(fd, io->buf + io->wpos, iobuf_left(io));
215 /* XXX is this really what we want? */
216 if (errno == EAGAIN || errno == EINTR)
217 return (IOBUF_WANT_READ);
218 return (IOBUF_ERROR);
221 return (IOBUF_CLOSED);
229 ioqbuf_alloc(struct iobuf *io, size_t len)
233 if (len < IOBUFQ_MIN)
236 if ((q = malloc(sizeof(*q) + len)) == NULL)
243 q->buf = (char *)(q) + sizeof(*q);
245 if (io->outqlast == NULL)
248 io->outqlast->next = q;
255 iobuf_queued(struct iobuf *io)
261 iobuf_reserve(struct iobuf *io, size_t len)
269 if (((q = io->outqlast) == NULL) || q->size - q->wpos <= len) {
270 if ((q = ioqbuf_alloc(io, len)) == NULL)
274 r = q->buf + q->wpos;
282 iobuf_queue(struct iobuf *io, const void *data, size_t len)
289 if ((buf = iobuf_reserve(io, len)) == NULL)
292 memmove(buf, data, len);
298 iobuf_queuev(struct iobuf *io, const struct iovec *iov, int iovcnt)
304 for (i = 0; i < iovcnt; i++)
305 len += iov[i].iov_len;
307 if ((buf = iobuf_reserve(io, len)) == NULL)
310 for (i = 0; i < iovcnt; i++) {
311 if (iov[i].iov_len == 0)
313 memmove(buf, iov[i].iov_base, iov[i].iov_len);
314 buf += iov[i].iov_len;
322 iobuf_fqueue(struct iobuf *io, const char *fmt, ...)
328 len = iobuf_vfqueue(io, fmt, ap);
335 iobuf_vfqueue(struct iobuf *io, const char *fmt, va_list ap)
340 len = vasprintf(&buf, fmt, ap);
345 len = iobuf_queue(io, buf, len);
352 iobuf_write(struct iobuf *io, int fd)
354 struct iovec iov[IOV_MAX];
360 for (q = io->outq; q ; q = q->next) {
363 iov[i].iov_base = q->buf + q->rpos;
364 iov[i].iov_len = q->wpos - q->rpos;
368 n = writev(fd, iov, i);
370 if (errno == EAGAIN || errno == EINTR)
371 return (IOBUF_WANT_WRITE);
373 return (IOBUF_CLOSED);
374 return (IOBUF_ERROR);
383 iobuf_flush(struct iobuf *io, int fd)
388 if ((s = iobuf_write(io, fd)) < 0)
397 iobuf_flush_tls(struct iobuf *io, void *tls)
402 if ((s = iobuf_write_tls(io, tls)) < 0)
409 iobuf_write_tls(struct iobuf *io, void *tls)
416 n = SSL_write(tls, q->buf + q->rpos, q->wpos - q->rpos);
418 switch ((r = SSL_get_error(tls, n))) {
419 case SSL_ERROR_WANT_READ:
420 return (IOBUF_WANT_READ);
421 case SSL_ERROR_WANT_WRITE:
422 return (IOBUF_WANT_WRITE);
423 case SSL_ERROR_ZERO_RETURN: /* connection closed */
424 return (IOBUF_CLOSED);
425 case SSL_ERROR_SYSCALL:
426 if (ERR_peek_last_error())
427 return (IOBUF_TLSERROR);
430 return (IOBUF_ERROR);
432 return (IOBUF_TLSERROR);
441 iobuf_read_tls(struct iobuf *io, void *tls)
446 n = SSL_read(tls, io->buf + io->wpos, iobuf_left(io));
448 switch ((r = SSL_get_error(tls, n))) {
449 case SSL_ERROR_WANT_READ:
450 return (IOBUF_WANT_READ);
451 case SSL_ERROR_WANT_WRITE:
452 return (IOBUF_WANT_WRITE);
453 case SSL_ERROR_SYSCALL:
454 if (ERR_peek_last_error())
455 return (IOBUF_TLSERROR);
458 return (IOBUF_ERROR);
460 return (IOBUF_TLSERROR);
463 return (IOBUF_CLOSED);