1/*	$OpenBSD: imsg.h,v 1.3 2013/12/26 17:32:33 eric Exp $	*/
2
3/*
4 * Copyright (c) 2006, 2007 Pierre-Yves Ritschard <pyr@openbsd.org>
5 * Copyright (c) 2006, 2007, 2008 Reyk Floeter <reyk@openbsd.org>
6 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *
20 * $FreeBSD$
21 */
22
23#ifndef _IMSG_H_
24#define _IMSG_H_
25
26#define IBUF_READ_SIZE		65535
27#define IMSG_HEADER_SIZE	sizeof(struct imsg_hdr)
28#define MAX_IMSGSIZE		16384
29
30struct ibuf {
31	TAILQ_ENTRY(ibuf)	 entry;
32	u_char			*buf;
33	size_t			 size;
34	size_t			 max;
35	size_t			 wpos;
36	size_t			 rpos;
37	int			 fd;
38};
39
40struct msgbuf {
41	TAILQ_HEAD(, ibuf)	 bufs;
42	u_int32_t		 queued;
43	int			 fd;
44};
45
46struct ibuf_read {
47	u_char			 buf[IBUF_READ_SIZE];
48	u_char			*rptr;
49	size_t			 wpos;
50};
51
52struct imsg_fd {
53	TAILQ_ENTRY(imsg_fd)	entry;
54	int			fd;
55};
56
57struct imsgbuf {
58	TAILQ_HEAD(, imsg_fd)	 fds;
59	struct ibuf_read	 r;
60	struct msgbuf		 w;
61	int			 fd;
62	pid_t			 pid;
63};
64
65#define IMSGF_HASFD	1
66
67struct imsg_hdr {
68	u_int32_t	 type;
69	u_int16_t	 len;
70	u_int16_t	 flags;
71	u_int32_t	 peerid;
72	u_int32_t	 pid;
73};
74
75struct imsg {
76	struct imsg_hdr	 hdr;
77	int		 fd;
78	void		*data;
79};
80
81
82/* buffer.c */
83struct ibuf	*ibuf_open(size_t);
84struct ibuf	*ibuf_dynamic(size_t, size_t);
85int		 ibuf_add(struct ibuf *, const void *, size_t);
86void		*ibuf_reserve(struct ibuf *, size_t);
87void		*ibuf_seek(struct ibuf *, size_t, size_t);
88size_t		 ibuf_size(struct ibuf *);
89size_t		 ibuf_left(struct ibuf *);
90void		 ibuf_close(struct msgbuf *, struct ibuf *);
91int		 ibuf_write(struct msgbuf *);
92void		 ibuf_free(struct ibuf *);
93void		 msgbuf_init(struct msgbuf *);
94void		 msgbuf_clear(struct msgbuf *);
95int		 msgbuf_write(struct msgbuf *);
96void		 msgbuf_drain(struct msgbuf *, size_t);
97
98/* imsg.c */
99void	 imsg_init(struct imsgbuf *, int);
100ssize_t	 imsg_read(struct imsgbuf *);
101ssize_t	 imsg_get(struct imsgbuf *, struct imsg *);
102int	 imsg_compose(struct imsgbuf *, u_int32_t, u_int32_t, pid_t,
103	    int, const void *, u_int16_t);
104int	 imsg_composev(struct imsgbuf *, u_int32_t, u_int32_t,  pid_t,
105	    int, const struct iovec *, int);
106struct ibuf *imsg_create(struct imsgbuf *, u_int32_t, u_int32_t, pid_t,
107	    u_int16_t);
108int	 imsg_add(struct ibuf *, const void *, u_int16_t);
109void	 imsg_close(struct imsgbuf *, struct ibuf *);
110void	 imsg_free(struct imsg *);
111int	 imsg_flush(struct imsgbuf *);
112void	 imsg_clear(struct imsgbuf *);
113
114#endif
115