summaryrefslogtreecommitdiff
path: root/usr.sbin/bgpd/imsg.c
blob: 0424062d18ca705a4a51eabeb7db57fd3477573f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*	$OpenBSD: imsg.c,v 1.8 2003/12/21 23:28:39 henning Exp $ */

/*
 * Copyright (c) 2003 Henning Brauer <henning@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "bgpd.h"

void	imsg_init_readbuf(struct imsgbuf *);

void
imsg_init_readbuf(struct imsgbuf *ibuf)
{
	bzero(&ibuf->r, sizeof(ibuf->r));
	ibuf->r.wptr = ibuf->r.buf;
	ibuf->r.pkt_len = IMSG_HEADER_SIZE;
}

void
imsg_init(struct imsgbuf *ibuf, int sock)
{
	imsg_init_readbuf(ibuf);
	msgbuf_init(&ibuf->w);
	ibuf->sock = sock;
	ibuf->w.sock = sock;
}

int
imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
{
	struct imsg_hdr		*hdr;
	ssize_t			 n, read_total = 0, datalen = 0;
	u_char			*rptr;

	do {
		if ((n = read(ibuf->sock, ibuf->r.wptr,
		    ibuf->r.pkt_len - ibuf->r.read_len)) == -1) {
			if (errno != EAGAIN && errno != EINTR)
				fatal("pipe read error", errno);
			return (0);
		}
		read_total += n;
		ibuf->r.wptr += n;
		ibuf->r.read_len += n;
		if (ibuf->r.read_len == ibuf->r.pkt_len) {
			if (!ibuf->r.seen_hdr) {	/* got header */
				hdr = (struct imsg_hdr *)&ibuf->r.buf;
				ibuf->r.type = hdr->type;
				ibuf->r.pkt_len = hdr->len;
				ibuf->r.peerid = hdr->peerid;
				if (hdr->len < IMSG_HEADER_SIZE ||
				    hdr->len > MAX_IMSGSIZE)
					fatal("wrong imsg header len", 0);
				ibuf->r.seen_hdr = 1;
			} else {		/* we got the full packet */
				imsg->hdr.type = ibuf->r.type;
				imsg->hdr.len = ibuf->r.pkt_len;
				imsg->hdr.peerid = ibuf->r.peerid;
				datalen = ibuf->r.pkt_len - IMSG_HEADER_SIZE;
				rptr = ibuf->r.buf + IMSG_HEADER_SIZE;
				if ((imsg->data = malloc(datalen)) == NULL)
					fatal("get_imsg malloc", errno);
				memcpy(imsg->data, rptr, datalen);
				n = 0;	/* give others a chance */
				imsg_init_readbuf(ibuf);
			}
		}
	} while (n > 0);

	if (read_total == 0)	/* connection closed */
		return (0);

	return (datalen + IMSG_HEADER_SIZE);
}

int
imsg_compose(struct imsgbuf *ibuf, int type, u_int32_t peerid, void *data,
    u_int16_t datalen)
{
	struct buf	*wbuf;
	struct imsg_hdr	 hdr;
	int		 n;

	hdr.len = datalen + IMSG_HEADER_SIZE;
	hdr.type = type;
	hdr.peerid = peerid;
	wbuf = buf_open(hdr.len);
	if (wbuf == NULL)
		fatal("imsg_compose: buf_open error", 0);
	if (buf_add(wbuf, &hdr, sizeof(hdr)) == -1)
		fatal("imsg_compose: buf_add error", 0);
	if (datalen)
		if (buf_add(wbuf, data, datalen) == -1)
			fatal("imsg_compose: buf_add error", 0);
	if ((n = buf_close(&ibuf->w, wbuf)) == -1)
		fatal("imsg_compose: buf_close error", 0);

	return (n);
}

void
imsg_free(struct imsg *imsg)
{
	free(imsg->data);
}