xref: /openbsd/usr.sbin/lpd/io.h (revision 73471bf0)
1 /*	$OpenBSD: io.h,v 1.1.1.1 2018/04/27 16:14:36 eric Exp $	*/
2 
3 /*
4  * Copyright (c) 2017 Eric Faurot <eric@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <event.h>
20 
21 enum {
22 	IO_CONNECTED = 0, 	/* connection successful	*/
23 	IO_TLSREADY,		/* TLS started successfully	*/
24 	IO_DATAIN,		/* new data in input buffer	*/
25 	IO_LOWAT,		/* output queue running low	*/
26 	IO_CLOSED,		/* normally terminated		*/
27 	IO_DISCONNECTED,	/* error?			*/
28 	IO_TIMEOUT,		/* error?			*/
29 	IO_ERROR,		/* details?			*/
30 	IO_TLSERROR,		/* XXX - needs more work	*/
31 };
32 
33 #define IO_IN	0x1
34 #define IO_OUT	0x2
35 
36 struct io;
37 
38 void io_trace(int);
39 const char* io_strio(struct io *);
40 const char* io_strevent(int);
41 
42 /* IO management */
43 struct io *io_new(void);
44 void	io_free(struct io *);
45 
46 /* IO setup */
47 int	io_set_callback(struct io *, void(*)(struct io *, int, void *), void *);
48 int	io_set_bindaddr(struct io *, const struct sockaddr *);
49 int	io_set_bufsize(struct io *, size_t);
50 void	io_set_timeout(struct io *, int);
51 void	io_set_lowat(struct io *, size_t);
52 
53 /* State retreival */
54 const char *io_error(struct io *);
55 int	io_fileno(struct io *);
56 
57 /* Connection management */
58 int	io_attach(struct io *io, int);
59 int	io_detach(struct io *io);
60 int	io_close(struct io *io);
61 int	io_connect(struct io *, struct addrinfo *);
62 int	io_disconnect(struct io *io);
63 int	io_starttls(struct io *, void *);
64 
65 /* Flow control  */
66 void	io_pause(struct io *, int);
67 void	io_resume(struct io *, int);
68 
69 /* IO direction */
70 void	io_set_read(struct io *);
71 void	io_set_write(struct io *);
72 
73 /* Output buffering */
74 int	io_write(struct io *, const void *, size_t);
75 int	io_writev(struct io *, const struct iovec *, int);
76 int	io_print(struct io *, const char *);
77 int	io_printf(struct io *, const char *, ...);
78 int	io_vprintf(struct io *, const char *, va_list);
79 size_t	io_queued(struct io *);
80 
81 /* Buffered input */
82 void *	io_data(struct io *);
83 size_t	io_datalen(struct io *);
84 char *	io_getline(struct io *, size_t *);
85 void	io_drop(struct io *, size_t);
86