1290375Srodrigc/*	$OpenBSD: imsg.h,v 1.3 2013/12/26 17:32:33 eric Exp $	*/
2290375Srodrigc
3290375Srodrigc/*
4290375Srodrigc * Copyright (c) 2006, 2007 Pierre-Yves Ritschard <pyr@openbsd.org>
5290375Srodrigc * Copyright (c) 2006, 2007, 2008 Reyk Floeter <reyk@openbsd.org>
6290375Srodrigc * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
7290375Srodrigc *
8290375Srodrigc * Permission to use, copy, modify, and distribute this software for any
9290375Srodrigc * purpose with or without fee is hereby granted, provided that the above
10290375Srodrigc * copyright notice and this permission notice appear in all copies.
11290375Srodrigc *
12290375Srodrigc * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13290375Srodrigc * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14290375Srodrigc * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15290375Srodrigc * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16290375Srodrigc * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17290375Srodrigc * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18290375Srodrigc * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19290375Srodrigc *
20290375Srodrigc * $FreeBSD: releng/11.0/lib/libopenbsd/imsg.h 290375 2015-11-04 23:52:19Z rodrigc $
21290375Srodrigc */
22290375Srodrigc
23290375Srodrigc#ifndef _IMSG_H_
24290375Srodrigc#define _IMSG_H_
25290375Srodrigc
26290375Srodrigc#define IBUF_READ_SIZE		65535
27290375Srodrigc#define IMSG_HEADER_SIZE	sizeof(struct imsg_hdr)
28290375Srodrigc#define MAX_IMSGSIZE		16384
29290375Srodrigc
30290375Srodrigcstruct ibuf {
31290375Srodrigc	TAILQ_ENTRY(ibuf)	 entry;
32290375Srodrigc	u_char			*buf;
33290375Srodrigc	size_t			 size;
34290375Srodrigc	size_t			 max;
35290375Srodrigc	size_t			 wpos;
36290375Srodrigc	size_t			 rpos;
37290375Srodrigc	int			 fd;
38290375Srodrigc};
39290375Srodrigc
40290375Srodrigcstruct msgbuf {
41290375Srodrigc	TAILQ_HEAD(, ibuf)	 bufs;
42290375Srodrigc	u_int32_t		 queued;
43290375Srodrigc	int			 fd;
44290375Srodrigc};
45290375Srodrigc
46290375Srodrigcstruct ibuf_read {
47290375Srodrigc	u_char			 buf[IBUF_READ_SIZE];
48290375Srodrigc	u_char			*rptr;
49290375Srodrigc	size_t			 wpos;
50290375Srodrigc};
51290375Srodrigc
52290375Srodrigcstruct imsg_fd {
53290375Srodrigc	TAILQ_ENTRY(imsg_fd)	entry;
54290375Srodrigc	int			fd;
55290375Srodrigc};
56290375Srodrigc
57290375Srodrigcstruct imsgbuf {
58290375Srodrigc	TAILQ_HEAD(, imsg_fd)	 fds;
59290375Srodrigc	struct ibuf_read	 r;
60290375Srodrigc	struct msgbuf		 w;
61290375Srodrigc	int			 fd;
62290375Srodrigc	pid_t			 pid;
63290375Srodrigc};
64290375Srodrigc
65290375Srodrigc#define IMSGF_HASFD	1
66290375Srodrigc
67290375Srodrigcstruct imsg_hdr {
68290375Srodrigc	u_int32_t	 type;
69290375Srodrigc	u_int16_t	 len;
70290375Srodrigc	u_int16_t	 flags;
71290375Srodrigc	u_int32_t	 peerid;
72290375Srodrigc	u_int32_t	 pid;
73290375Srodrigc};
74290375Srodrigc
75290375Srodrigcstruct imsg {
76290375Srodrigc	struct imsg_hdr	 hdr;
77290375Srodrigc	int		 fd;
78290375Srodrigc	void		*data;
79290375Srodrigc};
80290375Srodrigc
81290375Srodrigc
82290375Srodrigc/* buffer.c */
83290375Srodrigcstruct ibuf	*ibuf_open(size_t);
84290375Srodrigcstruct ibuf	*ibuf_dynamic(size_t, size_t);
85290375Srodrigcint		 ibuf_add(struct ibuf *, const void *, size_t);
86290375Srodrigcvoid		*ibuf_reserve(struct ibuf *, size_t);
87290375Srodrigcvoid		*ibuf_seek(struct ibuf *, size_t, size_t);
88290375Srodrigcsize_t		 ibuf_size(struct ibuf *);
89290375Srodrigcsize_t		 ibuf_left(struct ibuf *);
90290375Srodrigcvoid		 ibuf_close(struct msgbuf *, struct ibuf *);
91290375Srodrigcint		 ibuf_write(struct msgbuf *);
92290375Srodrigcvoid		 ibuf_free(struct ibuf *);
93290375Srodrigcvoid		 msgbuf_init(struct msgbuf *);
94290375Srodrigcvoid		 msgbuf_clear(struct msgbuf *);
95290375Srodrigcint		 msgbuf_write(struct msgbuf *);
96290375Srodrigcvoid		 msgbuf_drain(struct msgbuf *, size_t);
97290375Srodrigc
98290375Srodrigc/* imsg.c */
99290375Srodrigcvoid	 imsg_init(struct imsgbuf *, int);
100290375Srodrigcssize_t	 imsg_read(struct imsgbuf *);
101290375Srodrigcssize_t	 imsg_get(struct imsgbuf *, struct imsg *);
102290375Srodrigcint	 imsg_compose(struct imsgbuf *, u_int32_t, u_int32_t, pid_t,
103290375Srodrigc	    int, const void *, u_int16_t);
104290375Srodrigcint	 imsg_composev(struct imsgbuf *, u_int32_t, u_int32_t,  pid_t,
105290375Srodrigc	    int, const struct iovec *, int);
106290375Srodrigcstruct ibuf *imsg_create(struct imsgbuf *, u_int32_t, u_int32_t, pid_t,
107290375Srodrigc	    u_int16_t);
108290375Srodrigcint	 imsg_add(struct ibuf *, const void *, u_int16_t);
109290375Srodrigcvoid	 imsg_close(struct imsgbuf *, struct ibuf *);
110290375Srodrigcvoid	 imsg_free(struct imsg *);
111290375Srodrigcint	 imsg_flush(struct imsgbuf *);
112290375Srodrigcvoid	 imsg_clear(struct imsgbuf *);
113290375Srodrigc
114290375Srodrigc#endif
115