2 83fccf2d 2024-03-27 op * bufio.c was written by Omar Polo <op@omarpolo.com>
4 5565365c 2024-03-27 op * This is free and unencumbered software released into the public domain.
6 5565365c 2024-03-27 op * Anyone is free to copy, modify, publish, use, compile, sell, or
7 5565365c 2024-03-27 op * distribute this software, either in source code form or as a compiled
8 5565365c 2024-03-27 op * binary, for any purpose, commercial or non-commercial, and by any
11 5565365c 2024-03-27 op * In jurisdictions that recognize copyright laws, the author or authors
12 5565365c 2024-03-27 op * of this software dedicate any and all copyright interest in the
13 5565365c 2024-03-27 op * software to the public domain. We make this dedication for the benefit
14 5565365c 2024-03-27 op * of the public at large and to the detriment of our heirs and
15 5565365c 2024-03-27 op * successors. We intend this dedication to be an overt act of
16 5565365c 2024-03-27 op * relinquishment in perpetuity of all present and future rights to this
17 5565365c 2024-03-27 op * software under copyright law.
19 5565365c 2024-03-27 op * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 5565365c 2024-03-27 op * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 5565365c 2024-03-27 op * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 5565365c 2024-03-27 op * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 5565365c 2024-03-27 op * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 5565365c 2024-03-27 op * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 5565365c 2024-03-27 op * OTHER DEALINGS IN THE SOFTWARE.
28 5565365c 2024-03-27 op #include <assert.h>
29 5565365c 2024-03-27 op #include <errno.h>
30 5565365c 2024-03-27 op #include <stdarg.h>
31 5565365c 2024-03-27 op #include <stdint.h>
32 5565365c 2024-03-27 op #include <stdio.h>
33 5565365c 2024-03-27 op #include <stdlib.h>
34 5565365c 2024-03-27 op #include <string.h>
35 5565365c 2024-03-27 op #include <tls.h>
36 5565365c 2024-03-27 op #include <unistd.h>
38 5565365c 2024-03-27 op #include "bufio.h"
41 5565365c 2024-03-27 op buf_init(struct buf *buf)
43 5565365c 2024-03-27 op const size_t cap = BIO_CHUNK;
45 5565365c 2024-03-27 op memset(buf, 0, sizeof(*buf));
46 5565365c 2024-03-27 op if ((buf->buf = malloc(cap)) == NULL)
48 5565365c 2024-03-27 op buf->cap = cap;
53 5565365c 2024-03-27 op buf_grow(struct buf *buf)
55 5565365c 2024-03-27 op size_t newcap;
58 5565365c 2024-03-27 op newcap = buf->cap + BIO_CHUNK;
59 5565365c 2024-03-27 op t = realloc(buf->buf, newcap);
60 5565365c 2024-03-27 op if (t == NULL)
63 5565365c 2024-03-27 op buf->cap = newcap;
68 5565365c 2024-03-27 op buf_has_line(struct buf *buf, const char *nl)
70 5565365c 2024-03-27 op return (memmem(buf->buf, buf->len, nl, strlen(nl)) != NULL);
74 5565365c 2024-03-27 op buf_getdelim(struct buf *buf, const char *nl, size_t *len)
76 5565365c 2024-03-27 op uint8_t *endl;
81 5565365c 2024-03-27 op nlen = strlen(nl);
82 5565365c 2024-03-27 op if ((endl = memmem(buf->buf, buf->len, nl, nlen)) == NULL)
83 5565365c 2024-03-27 op return (NULL);
84 5565365c 2024-03-27 op *len = endl + nlen - buf->buf;
86 5565365c 2024-03-27 op return (buf->buf);
90 5565365c 2024-03-27 op buf_drain(struct buf *buf, size_t l)
94 5565365c 2024-03-27 op if (l >= buf->len) {
99 5565365c 2024-03-27 op memmove(buf->buf, buf->buf + l, buf->len - l);
100 5565365c 2024-03-27 op buf->len -= l;
104 5565365c 2024-03-27 op buf_drain_line(struct buf *buf, const char *nl)
106 5565365c 2024-03-27 op uint8_t *endln;
109 5565365c 2024-03-27 op nlen = strlen(nl);
110 5565365c 2024-03-27 op if ((endln = memmem(buf->buf, buf->len, nl, nlen)) == NULL)
112 5565365c 2024-03-27 op buf_drain(buf, endln + nlen - buf->buf);
116 5565365c 2024-03-27 op buf_free(struct buf *buf)
118 5565365c 2024-03-27 op free(buf->buf);
119 5565365c 2024-03-27 op memset(buf, 0, sizeof(*buf));
123 5565365c 2024-03-27 op bufio_init(struct bufio *bio)
125 5565365c 2024-03-27 op memset(bio, 0, sizeof(*bio));
126 5565365c 2024-03-27 op bio->fd = -1;
128 5565365c 2024-03-27 op if (buf_init(&bio->wbuf) == -1)
130 5565365c 2024-03-27 op if (buf_init(&bio->rbuf) == -1) {
131 5565365c 2024-03-27 op buf_free(&bio->wbuf);
138 5565365c 2024-03-27 op bufio_free(struct bufio *bio)
140 5565365c 2024-03-27 op if (bio->ctx)
141 5565365c 2024-03-27 op tls_free(bio->ctx);
142 5565365c 2024-03-27 op bio->ctx = NULL;
144 5565365c 2024-03-27 op if (bio->fd != -1)
145 5565365c 2024-03-27 op close(bio->fd);
146 5565365c 2024-03-27 op bio->fd = -1;
148 5565365c 2024-03-27 op buf_free(&bio->rbuf);
149 5565365c 2024-03-27 op buf_free(&bio->wbuf);
153 5565365c 2024-03-27 op bufio_close(struct bufio *bio)
155 5565365c 2024-03-27 op if (bio->ctx == NULL)
158 5565365c 2024-03-27 op switch (tls_close(bio->ctx)) {
161 5565365c 2024-03-27 op case TLS_WANT_POLLIN:
162 5565365c 2024-03-27 op errno = EAGAIN;
163 5565365c 2024-03-27 op bio->wantev = BUFIO_WANT_READ;
165 5565365c 2024-03-27 op case TLS_WANT_POLLOUT:
166 5565365c 2024-03-27 op errno = EAGAIN;
167 5565365c 2024-03-27 op bio->wantev = BUFIO_WANT_WRITE;
175 5565365c 2024-03-27 op bufio_reset(struct bufio *bio)
177 5565365c 2024-03-27 op bufio_free(bio);
178 5565365c 2024-03-27 op return (bufio_init(bio));
182 5565365c 2024-03-27 op bufio_set_fd(struct bufio *bio, int fd)
184 5565365c 2024-03-27 op bio->fd = fd;
188 5565365c 2024-03-27 op bufio_starttls(struct bufio *bio, const char *host, int insecure,
189 5565365c 2024-03-27 op const uint8_t *cert, size_t certlen, const uint8_t *key, size_t keylen)
191 5565365c 2024-03-27 op struct tls_config *conf;
193 5565365c 2024-03-27 op if ((conf = tls_config_new()) == NULL)
196 5565365c 2024-03-27 op if (insecure) {
197 5565365c 2024-03-27 op tls_config_insecure_noverifycert(conf);
198 5565365c 2024-03-27 op tls_config_insecure_noverifyname(conf);
199 5565365c 2024-03-27 op tls_config_insecure_noverifytime(conf);
202 5565365c 2024-03-27 op if (cert && tls_config_set_keypair_mem(conf, cert, certlen,
203 5565365c 2024-03-27 op key, keylen) == -1) {
204 5565365c 2024-03-27 op tls_config_free(conf);
208 5565365c 2024-03-27 op if ((bio->ctx = tls_client()) == NULL) {
209 5565365c 2024-03-27 op tls_config_free(conf);
213 5565365c 2024-03-27 op if (tls_configure(bio->ctx, conf) == -1) {
214 5565365c 2024-03-27 op tls_config_free(conf);
218 5565365c 2024-03-27 op tls_config_free(conf);
220 5565365c 2024-03-27 op if (tls_connect_socket(bio->ctx, bio->fd, host) == -1)
227 5565365c 2024-03-27 op bufio_ev(struct bufio *bio)
231 5565365c 2024-03-27 op if (bio->wantev)
232 5565365c 2024-03-27 op return (bio->wantev);
234 5565365c 2024-03-27 op ev = BUFIO_WANT_READ;
235 5565365c 2024-03-27 op if (bio->wbuf.len != 0)
236 5565365c 2024-03-27 op ev |= BUFIO_WANT_WRITE;
242 5565365c 2024-03-27 op bufio_handshake(struct bufio *bio)
244 5565365c 2024-03-27 op if (bio->ctx == NULL) {
245 5565365c 2024-03-27 op errno = EINVAL;
249 5565365c 2024-03-27 op switch (tls_handshake(bio->ctx)) {
252 5565365c 2024-03-27 op case TLS_WANT_POLLIN:
253 5565365c 2024-03-27 op errno = EAGAIN;
254 5565365c 2024-03-27 op bio->wantev = BUFIO_WANT_READ;
256 5565365c 2024-03-27 op case TLS_WANT_POLLOUT:
257 5565365c 2024-03-27 op errno = EAGAIN;
258 5565365c 2024-03-27 op bio->wantev = BUFIO_WANT_WRITE;
266 5565365c 2024-03-27 op bufio_read(struct bufio *bio)
268 5565365c 2024-03-27 op struct buf *rbuf = &bio->rbuf;
271 5565365c 2024-03-27 op assert(rbuf->cap >= rbuf->len);
272 5565365c 2024-03-27 op if (rbuf->cap - rbuf->len < BIO_CHUNK) {
273 5565365c 2024-03-27 op if (buf_grow(rbuf) == -1)
277 5565365c 2024-03-27 op if (bio->ctx) {
278 5565365c 2024-03-27 op r = tls_read(bio->ctx, rbuf->buf + rbuf->len,
279 5565365c 2024-03-27 op rbuf->cap - rbuf->len);
281 5565365c 2024-03-27 op case TLS_WANT_POLLIN:
282 5565365c 2024-03-27 op errno = EAGAIN;
283 5565365c 2024-03-27 op bio->wantev = BUFIO_WANT_READ;
285 5565365c 2024-03-27 op case TLS_WANT_POLLOUT:
286 5565365c 2024-03-27 op errno = EAGAIN;
287 5565365c 2024-03-27 op bio->wantev = BUFIO_WANT_WRITE;
290 813645df 2024-04-15 me bio->wantev = 0;
294 5565365c 2024-03-27 op bio->wantev = 0;
295 5565365c 2024-03-27 op rbuf->len += r;
300 5565365c 2024-03-27 op r = read(bio->fd, rbuf->buf + rbuf->len, rbuf->cap - rbuf->len);
303 5565365c 2024-03-27 op rbuf->len += r;
308 5565365c 2024-03-27 op bufio_drain(struct bufio *bio, void *d, size_t len)
310 5565365c 2024-03-27 op struct buf *rbuf = &bio->rbuf;
312 5565365c 2024-03-27 op if (len > rbuf->len)
313 5565365c 2024-03-27 op len = rbuf->len;
314 5565365c 2024-03-27 op memcpy(d, rbuf->buf, len);
315 5565365c 2024-03-27 op buf_drain(rbuf, len);
316 5565365c 2024-03-27 op return (len);
320 5565365c 2024-03-27 op bufio_write(struct bufio *bio)
322 5565365c 2024-03-27 op struct buf *wbuf = &bio->wbuf;
325 5565365c 2024-03-27 op if (bio->ctx) {
326 5565365c 2024-03-27 op switch (w = tls_write(bio->ctx, wbuf->buf, wbuf->len)) {
327 5565365c 2024-03-27 op case TLS_WANT_POLLIN:
328 5565365c 2024-03-27 op errno = EAGAIN;
329 5565365c 2024-03-27 op bio->wantev = BUFIO_WANT_READ;
331 5565365c 2024-03-27 op case TLS_WANT_POLLOUT:
332 5565365c 2024-03-27 op errno = EAGAIN;
333 5565365c 2024-03-27 op bio->wantev = BUFIO_WANT_WRITE;
338 5565365c 2024-03-27 op bio->wantev = 0;
339 5565365c 2024-03-27 op buf_drain(wbuf, w);
344 5565365c 2024-03-27 op w = write(bio->fd, wbuf->buf, wbuf->len);
347 5565365c 2024-03-27 op buf_drain(wbuf, w);
352 5565365c 2024-03-27 op bufio_io_err(struct bufio *bio)
354 5565365c 2024-03-27 op if (bio->ctx)
355 5565365c 2024-03-27 op return tls_error(bio->ctx);
357 5565365c 2024-03-27 op return strerror(errno);
361 5565365c 2024-03-27 op bufio_compose(struct bufio *bio, const void *d, size_t len)
363 5565365c 2024-03-27 op struct buf *wbuf = &bio->wbuf;
365 5565365c 2024-03-27 op while (wbuf->cap - wbuf->len < len) {
366 5565365c 2024-03-27 op if (buf_grow(wbuf) == -1)
370 5565365c 2024-03-27 op memcpy(wbuf->buf + wbuf->len, d, len);
371 5565365c 2024-03-27 op wbuf->len += len;
376 5565365c 2024-03-27 op bufio_compose_str(struct bufio *bio, const char *str)
378 5565365c 2024-03-27 op return (bufio_compose(bio, str, strlen(str)));
382 5565365c 2024-03-27 op bufio_compose_fmt(struct bufio *bio, const char *fmt, ...)
388 5565365c 2024-03-27 op va_start(ap, fmt);
389 5565365c 2024-03-27 op r = vasprintf(&str, fmt, ap);
394 5565365c 2024-03-27 op r = bufio_compose(bio, str, r);
400 5565365c 2024-03-27 op bufio_rewind_cursor(struct bufio *bio)
402 5565365c 2024-03-27 op bio->rbuf.cur = 0;
406 5565365c 2024-03-27 op bufio_get_cb(void *d)
408 5565365c 2024-03-27 op struct bufio *bio = d;
409 5565365c 2024-03-27 op struct buf *rbuf = &bio->rbuf;
411 5565365c 2024-03-27 op if (rbuf->cur >= rbuf->len)
412 5565365c 2024-03-27 op return (EOF);
413 5565365c 2024-03-27 op return (rbuf->buf[rbuf->cur++]);
417 5565365c 2024-03-27 op bufio_peek_cb(void *d)
419 5565365c 2024-03-27 op struct bufio *bio = d;
420 5565365c 2024-03-27 op struct buf *rbuf = &bio->rbuf;
422 5565365c 2024-03-27 op if (rbuf->cur >= rbuf->len)
423 5565365c 2024-03-27 op return (EOF);
424 5565365c 2024-03-27 op return (rbuf->buf[rbuf->cur]);