GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/switchctl/../switchd/imsg_util.c Lines: 0 68 0.0 %
Date: 2017-11-13 Branches: 0 38 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: imsg_util.c,v 1.5 2016/09/30 11:57:57 reyk Exp $	*/
2
3
/*
4
 * Copyright (c) 2010-2016 Reyk Floeter <reyk@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 <sys/queue.h>
20
#include <sys/socket.h>
21
#include <sys/uio.h>
22
23
#include <net/if.h>
24
#include <netinet/in_systm.h>
25
#include <netinet/in.h>
26
#include <netinet/ip.h>
27
#include <netinet/tcp.h>
28
#include <arpa/inet.h>
29
#include <netdb.h>
30
31
#include <stdio.h>
32
#include <stdlib.h>
33
#include <unistd.h>
34
#include <string.h>
35
#include <errno.h>
36
#include <fcntl.h>
37
#include <ctype.h>
38
#include <event.h>
39
#include <imsg.h>
40
41
#include "switchd.h"
42
43
/*
44
 * Extending the imsg buffer API for internal use
45
 */
46
47
int
48
ibuf_cat(struct ibuf *dst, struct ibuf *src)
49
{
50
	return (ibuf_add(dst, src->buf, ibuf_size(src)));
51
}
52
53
void
54
ibuf_zero(struct ibuf *buf)
55
{
56
	explicit_bzero(buf->buf, buf->wpos);
57
}
58
59
void
60
ibuf_reset(struct ibuf *buf)
61
{
62
	ibuf_zero(buf);
63
	buf->rpos = buf->wpos = 0;
64
}
65
66
struct ibuf *
67
ibuf_new(void *data, size_t len)
68
{
69
	struct ibuf	*buf;
70
71
	if ((buf = ibuf_dynamic(len,
72
	    SWITCHD_MSGBUF_MAX)) == NULL)
73
		return (NULL);
74
75
	ibuf_zero(buf);
76
77
	if (data == NULL && len) {
78
		if (ibuf_advance(buf, len) == NULL) {
79
			ibuf_free(buf);
80
			return (NULL);
81
		}
82
	} else {
83
		if (ibuf_add(buf, data, len) != 0) {
84
			ibuf_free(buf);
85
			return (NULL);
86
		}
87
	}
88
89
	return (buf);
90
}
91
92
struct ibuf *
93
ibuf_static(void)
94
{
95
	struct ibuf	*buf;
96
97
	if ((buf = ibuf_open(SWITCHD_MSGBUF_MAX)) == NULL)
98
		return (NULL);
99
100
	ibuf_zero(buf);
101
102
	return (buf);
103
}
104
105
void *
106
ibuf_advance(struct ibuf *buf, size_t len)
107
{
108
	void	*ptr;
109
110
	if ((ptr = ibuf_reserve(buf, len)) != NULL)
111
		memset(ptr, 0, len);
112
113
	return (ptr);
114
}
115
116
void
117
ibuf_release(struct ibuf *buf)
118
{
119
	if (buf == NULL)
120
		return;
121
	if (buf->buf != NULL) {
122
		ibuf_zero(buf);
123
		free(buf->buf);
124
	}
125
	free(buf);
126
}
127
128
size_t
129
ibuf_length(struct ibuf *buf)
130
{
131
	if (buf == NULL || buf->buf == NULL)
132
		return (0);
133
	return (ibuf_size(buf));
134
}
135
136
uint8_t *
137
ibuf_data(struct ibuf *buf)
138
{
139
	return (ibuf_seek(buf, 0, 0));
140
}
141
142
void *
143
ibuf_getdata(struct ibuf *buf, size_t len)
144
{
145
	void	*data;
146
147
	if ((data = ibuf_seek(buf, buf->rpos, len)) == NULL)
148
		return (NULL);
149
	buf->rpos += len;
150
151
	return (data);
152
}
153
154
ssize_t
155
ibuf_dataleft(struct ibuf *buf)
156
{
157
	return (buf->wpos - buf->rpos);
158
}
159
160
size_t
161
ibuf_dataoffset(struct ibuf *buf)
162
{
163
	return (buf->rpos);
164
}
165
166
struct ibuf *
167
ibuf_get(struct ibuf *buf, size_t len)
168
{
169
	void		*data;
170
171
	if ((data = ibuf_getdata(buf, len)) == NULL)
172
		return (NULL);
173
174
	return (ibuf_new(data, len));
175
}
176
177
struct ibuf *
178
ibuf_dup(struct ibuf *buf)
179
{
180
	if (buf == NULL)
181
		return (NULL);
182
	return (ibuf_new(ibuf_data(buf), ibuf_size(buf)));
183
}
184
185
struct ibuf *
186
ibuf_random(size_t len)
187
{
188
	struct ibuf	*buf;
189
	void		*ptr;
190
191
	if ((buf = ibuf_open(len)) == NULL)
192
		return (NULL);
193
	if ((ptr = ibuf_reserve(buf, len)) == NULL) {
194
		ibuf_free(buf);
195
		return (NULL);
196
	}
197
	arc4random_buf(ptr, len);
198
	return (buf);
199
}
200
201
int
202
ibuf_setsize(struct ibuf *buf, size_t len)
203
{
204
	if (len > buf->size)
205
		return (-1);
206
	buf->wpos = len;
207
	return (0);
208
}
209
210
int
211
ibuf_setmax(struct ibuf *buf, size_t len)
212
{
213
	if (len > buf->size)
214
		return (-1);
215
	buf->max = len;
216
	return (0);
217
}
218
219
int
220
ibuf_prepend(struct ibuf *buf, void *data, size_t len)
221
{
222
	struct ibuf	*new;
223
224
	/* Swap buffers (we could also use memmove here) */
225
	if ((new = ibuf_new(data, len)) == NULL)
226
		return (-1);
227
	if (ibuf_cat(new, buf) == -1) {
228
		ibuf_release(new);
229
		return (-1);
230
	}
231
	free(buf->buf);
232
	memcpy(buf, new, sizeof(*buf));
233
	free(new);
234
235
	return (0);
236
}