2 * This is free and unencumbered software released into the public domain.
4 * Anyone is free to copy, modify, publish, use, compile, sell, or
5 * distribute this software, either in source code form or as a compiled
6 * binary, for any purpose, commercial or non-commercial, and by any
9 * In jurisdictions that recognize copyright laws, the author or authors
10 * of this software dedicate any and all copyright interest in the
11 * software to the public domain. We make this dedication for the benefit
12 * of the public at large and to the detriment of our heirs and
13 * successors. We intend this dedication to be an overt act of
14 * relinquishment in perpetuity of all present and future rights to this
15 * software under copyright law.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
41 buf_init(struct buf *buf)
43 const size_t cap = BIO_CHUNK;
45 memset(buf, 0, sizeof(*buf));
46 if ((buf->buf = malloc(cap)) == NULL)
53 buf_grow(struct buf *buf)
58 newcap = buf->cap + BIO_CHUNK;
59 t = realloc(buf->buf, newcap);
68 buf_has_line(struct buf *buf, const char *nl)
70 return (memmem(buf->buf, buf->len, nl, strlen(nl)) != NULL);
74 buf_drain(struct buf *buf, size_t l)
83 memmove(buf->buf, buf->buf + l, buf->len - l);
88 buf_drain_line(struct buf *buf, const char *nl)
94 if ((endln = memmem(buf->buf, buf->len, nl, nlen)) == NULL)
96 buf_drain(buf, endln + nlen - buf->buf);
100 buf_free(struct buf *buf)
103 memset(buf, 0, sizeof(*buf));
107 bufio_init(struct bufio *bio)
109 memset(bio, 0, sizeof(*bio));
112 if (buf_init(&bio->wbuf) == -1)
114 if (buf_init(&bio->rbuf) == -1) {
115 buf_free(&bio->wbuf);
122 bufio_free(struct bufio *bio)
132 buf_free(&bio->rbuf);
133 buf_free(&bio->wbuf);
137 bufio_close(struct bufio *bio)
139 if (bio->ctx == NULL)
142 switch (tls_close(bio->ctx)) {
145 case TLS_WANT_POLLIN:
147 bio->pflags = BUFIO_WANT_READ;
149 case TLS_WANT_POLLOUT:
151 bio->pflags = BUFIO_WANT_WRITE;
159 bufio_reset(struct bufio *bio)
162 return (bufio_init(bio));
166 bufio_set_fd(struct bufio *bio, int fd)
172 bufio_starttls(struct bufio *bio, const char *host, int insecure,
173 const uint8_t *cert, size_t certlen, const uint8_t *key, size_t keylen)
175 struct tls_config *conf;
177 if ((conf = tls_config_new()) == NULL)
181 tls_config_insecure_noverifycert(conf);
182 tls_config_insecure_noverifyname(conf);
183 tls_config_insecure_noverifytime(conf);
186 if (cert && tls_config_set_keypair_mem(conf, cert, certlen,
187 key, keylen) == -1) {
188 tls_config_free(conf);
192 if ((bio->ctx = tls_client()) == NULL) {
193 tls_config_free(conf);
197 if (tls_configure(bio->ctx, conf) == -1) {
198 tls_config_free(conf);
202 tls_config_free(conf);
204 if (tls_connect_socket(bio->ctx, bio->fd, host) == -1)
211 bufio_ev(struct bufio *bio)
216 return (bio->pflags);
218 ev = BUFIO_WANT_READ;
219 if (bio->wbuf.len != 0)
220 ev |= BUFIO_WANT_WRITE;
226 bufio_handshake(struct bufio *bio)
228 if (bio->ctx == NULL) {
233 switch (tls_handshake(bio->ctx)) {
236 case TLS_WANT_POLLIN:
238 bio->pflags = BUFIO_WANT_READ;
240 case TLS_WANT_POLLOUT:
242 bio->pflags = BUFIO_WANT_WRITE;
250 bufio_read(struct bufio *bio)
252 struct buf *rbuf = &bio->rbuf;
255 assert(rbuf->cap >= rbuf->len);
256 if (rbuf->cap - rbuf->len < BIO_CHUNK) {
257 if (buf_grow(rbuf) == -1)
262 r = tls_read(bio->ctx, rbuf->buf + rbuf->len,
263 rbuf->cap - rbuf->len);
265 case TLS_WANT_POLLIN:
267 bio->pflags = BUFIO_WANT_READ;
269 case TLS_WANT_POLLOUT:
271 bio->pflags = BUFIO_WANT_WRITE;
282 r = read(bio->fd, rbuf->buf + rbuf->len, rbuf->cap - rbuf->len);
290 bufio_drain(struct bufio *bio, void *d, size_t len)
292 struct buf *rbuf = &bio->rbuf;
296 memcpy(d, rbuf->buf, len);
297 buf_drain(rbuf, len);
302 bufio_write(struct bufio *bio)
304 struct buf *wbuf = &bio->wbuf;
308 switch (w = tls_write(bio->ctx, wbuf->buf, wbuf->len)) {
309 case TLS_WANT_POLLIN:
311 bio->pflags = BUFIO_WANT_READ;
313 case TLS_WANT_POLLOUT:
315 bio->pflags = BUFIO_WANT_WRITE;
326 w = write(bio->fd, wbuf->buf, wbuf->len);
334 bufio_compose(struct bufio *bio, const void *d, size_t len)
336 struct buf *wbuf = &bio->wbuf;
338 while (wbuf->cap - wbuf->len < len) {
339 if (buf_grow(wbuf) == -1)
343 memcpy(wbuf->buf + wbuf->len, d, len);
349 bufio_compose_str(struct bufio *bio, const char *str)
351 return (bufio_compose(bio, str, strlen(str)));
355 bufio_compose_fmt(struct bufio *bio, const char *fmt, ...)
362 r = vasprintf(&str, fmt, ap);
367 r = bufio_compose(bio, str, r);
373 bufio_rewind_cursor(struct bufio *bio)
379 bufio_get_cb(void *d)
381 struct bufio *bio = d;
382 struct buf *rbuf = &bio->rbuf;
384 if (rbuf->cur >= rbuf->len)
386 return (rbuf->buf[rbuf->cur++]);
390 bufio_peek_cb(void *d)
392 struct bufio *bio = d;
393 struct buf *rbuf = &bio->rbuf;
395 if (rbuf->cur >= rbuf->len)
397 return (rbuf->buf[rbuf->cur]);