xref: /openbsd/usr.sbin/smtpd/iobuf.h (revision ceeebefe)
1 /*	$OpenBSD: iobuf.h,v 1.7 2021/07/28 19:39:50 benno Exp $	*/
2 /*
3  * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
4  *
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.
8  *
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.
16  */
17 
18 struct ioqbuf {
19 	struct ioqbuf	*next;
20 	char		*buf;
21 	size_t		 size;
22 	size_t		 wpos;
23 	size_t		 rpos;
24 };
25 
26 struct iobuf {
27 	char		*buf;
28 	size_t		 max;
29 	size_t		 size;
30 	size_t		 wpos;
31 	size_t		 rpos;
32 
33 	size_t		 queued;
34 	struct ioqbuf	*outq;
35 	struct ioqbuf	*outqlast;
36 };
37 
38 struct tls;
39 
40 #define IOBUF_WANT_READ		-1
41 #define IOBUF_WANT_WRITE	-2
42 #define IOBUF_CLOSED		-3
43 #define IOBUF_ERROR		-4
44 
45 int	iobuf_init(struct iobuf *, size_t, size_t);
46 void	iobuf_clear(struct iobuf *);
47 
48 int	iobuf_extend(struct iobuf *, size_t);
49 void	iobuf_normalize(struct iobuf *);
50 void	iobuf_drop(struct iobuf *, size_t);
51 size_t	iobuf_space(struct iobuf *);
52 size_t	iobuf_len(struct iobuf *);
53 size_t	iobuf_left(struct iobuf *);
54 char   *iobuf_data(struct iobuf *);
55 char   *iobuf_getline(struct iobuf *, size_t *);
56 ssize_t	iobuf_read(struct iobuf *, int);
57 ssize_t	iobuf_read_tls(struct iobuf *, struct tls *);
58 
59 size_t  iobuf_queued(struct iobuf *);
60 void*   iobuf_reserve(struct iobuf *, size_t);
61 int	iobuf_queue(struct iobuf *, const void*, size_t);
62 int	iobuf_queuev(struct iobuf *, const struct iovec *, int);
63 int	iobuf_fqueue(struct iobuf *, const char *, ...)
64     __attribute__((__format__ (printf, 2, 3)));
65 int	iobuf_vfqueue(struct iobuf *, const char *, va_list);
66 int	iobuf_flush(struct iobuf *, int);
67 int	iobuf_flush_tls(struct iobuf *, struct tls *);
68 ssize_t	iobuf_write(struct iobuf *, int);
69 ssize_t	iobuf_write_tls(struct iobuf *, struct tls *);
70