]> git.proxmox.com Git - mirror_frr.git/blob - lib/imsg-buffer.c
Merge pull request #1966 from donaldsharp/vrf_late_to_the_party
[mirror_frr.git] / lib / imsg-buffer.c
1 /* $OpenBSD$ */
2
3 /*
4 * Copyright (c) 2003, 2004 Henning Brauer <henning@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 <zebra.h>
20
21 #include "queue.h"
22 #include "imsg.h"
23
24 int ibuf_realloc(struct ibuf *, size_t);
25 void ibuf_enqueue(struct msgbuf *, struct ibuf *);
26 void ibuf_dequeue(struct msgbuf *, struct ibuf *);
27
28 struct ibuf *ibuf_open(size_t len)
29 {
30 struct ibuf *buf;
31
32 if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
33 return (NULL);
34 if ((buf->buf = malloc(len)) == NULL) {
35 free(buf);
36 return (NULL);
37 }
38 buf->size = buf->max = len;
39 buf->fd = -1;
40
41 return (buf);
42 }
43
44 struct ibuf *ibuf_dynamic(size_t len, size_t max)
45 {
46 struct ibuf *buf;
47
48 if (max < len)
49 return (NULL);
50
51 if ((buf = ibuf_open(len)) == NULL)
52 return (NULL);
53
54 if (max > 0)
55 buf->max = max;
56
57 return (buf);
58 }
59
60 int ibuf_realloc(struct ibuf *buf, size_t len)
61 {
62 uint8_t *b;
63
64 /* on static buffers max is eq size and so the following fails */
65 if (buf->wpos + len > buf->max) {
66 errno = ERANGE;
67 return (-1);
68 }
69
70 b = realloc(buf->buf, buf->wpos + len);
71 if (b == NULL)
72 return (-1);
73 buf->buf = b;
74 buf->size = buf->wpos + len;
75
76 return (0);
77 }
78
79 int ibuf_add(struct ibuf *buf, const void *data, size_t len)
80 {
81 if (buf->wpos + len > buf->size)
82 if (ibuf_realloc(buf, len) == -1)
83 return (-1);
84
85 memcpy(buf->buf + buf->wpos, data, len);
86 buf->wpos += len;
87 return (0);
88 }
89
90 void *ibuf_reserve(struct ibuf *buf, size_t len)
91 {
92 void *b;
93
94 if (buf->wpos + len > buf->size)
95 if (ibuf_realloc(buf, len) == -1)
96 return (NULL);
97
98 b = buf->buf + buf->wpos;
99 buf->wpos += len;
100 return (b);
101 }
102
103 void *ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
104 {
105 /* only allowed to seek in already written parts */
106 if (pos + len > buf->wpos)
107 return (NULL);
108
109 return (buf->buf + pos);
110 }
111
112 size_t ibuf_size(struct ibuf *buf)
113 {
114 return (buf->wpos);
115 }
116
117 size_t ibuf_left(struct ibuf *buf)
118 {
119 return (buf->max - buf->wpos);
120 }
121
122 void ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
123 {
124 ibuf_enqueue(msgbuf, buf);
125 }
126
127 int ibuf_write(struct msgbuf *msgbuf)
128 {
129 struct iovec iov[IOV_MAX];
130 struct ibuf *buf;
131 unsigned int i = 0;
132 ssize_t n;
133
134 memset(&iov, 0, sizeof(iov));
135 TAILQ_FOREACH (buf, &msgbuf->bufs, entry) {
136 if (i >= IOV_MAX)
137 break;
138 iov[i].iov_base = buf->buf + buf->rpos;
139 iov[i].iov_len = buf->wpos - buf->rpos;
140 i++;
141 }
142
143 again:
144 if ((n = writev(msgbuf->fd, iov, i)) == -1) {
145 if (errno == EINTR)
146 goto again;
147 if (errno == ENOBUFS)
148 errno = EAGAIN;
149 return (-1);
150 }
151
152 if (n == 0) { /* connection closed */
153 errno = 0;
154 return (0);
155 }
156
157 msgbuf_drain(msgbuf, n);
158
159 return (1);
160 }
161
162 void ibuf_free(struct ibuf *buf)
163 {
164 if (buf == NULL)
165 return;
166 free(buf->buf);
167 free(buf);
168 }
169
170 void msgbuf_init(struct msgbuf *msgbuf)
171 {
172 msgbuf->queued = 0;
173 msgbuf->fd = -1;
174 TAILQ_INIT(&msgbuf->bufs);
175 }
176
177 void msgbuf_drain(struct msgbuf *msgbuf, size_t n)
178 {
179 struct ibuf *buf, *next;
180
181 for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
182 buf = next) {
183 next = TAILQ_NEXT(buf, entry);
184 if (buf->rpos + n >= buf->wpos) {
185 n -= buf->wpos - buf->rpos;
186 ibuf_dequeue(msgbuf, buf);
187 } else {
188 buf->rpos += n;
189 n = 0;
190 }
191 }
192 }
193
194 void msgbuf_clear(struct msgbuf *msgbuf)
195 {
196 struct ibuf *buf;
197
198 while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
199 ibuf_dequeue(msgbuf, buf);
200 }
201
202 int msgbuf_write(struct msgbuf *msgbuf)
203 {
204 struct iovec iov[IOV_MAX];
205 struct ibuf *buf;
206 unsigned int i = 0;
207 ssize_t n;
208 struct msghdr msg;
209 struct cmsghdr *cmsg;
210 union {
211 struct cmsghdr hdr;
212 char buf[CMSG_SPACE(sizeof(int))];
213 } cmsgbuf;
214
215 memset(&iov, 0, sizeof(iov));
216 memset(&msg, 0, sizeof(msg));
217 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
218 TAILQ_FOREACH (buf, &msgbuf->bufs, entry) {
219 if (i >= IOV_MAX)
220 break;
221 iov[i].iov_base = buf->buf + buf->rpos;
222 iov[i].iov_len = buf->wpos - buf->rpos;
223 i++;
224 if (buf->fd != -1)
225 break;
226 }
227
228 msg.msg_iov = iov;
229 msg.msg_iovlen = i;
230
231 if (buf != NULL && buf->fd != -1) {
232 msg.msg_control = (caddr_t)&cmsgbuf.buf;
233 msg.msg_controllen = sizeof(cmsgbuf.buf);
234 cmsg = CMSG_FIRSTHDR(&msg);
235 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
236 cmsg->cmsg_level = SOL_SOCKET;
237 cmsg->cmsg_type = SCM_RIGHTS;
238 memcpy(CMSG_DATA(cmsg), &buf->fd, sizeof(int));
239 }
240
241 again:
242 if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
243 if (errno == EINTR)
244 goto again;
245 if (errno == ENOBUFS)
246 errno = EAGAIN;
247 return (-1);
248 }
249
250 if (n == 0) { /* connection closed */
251 errno = 0;
252 return (0);
253 }
254
255 /*
256 * assumption: fd got sent if sendmsg sent anything
257 * this works because fds are passed one at a time
258 */
259 if (buf != NULL && buf->fd != -1) {
260 close(buf->fd);
261 buf->fd = -1;
262 }
263
264 msgbuf_drain(msgbuf, n);
265
266 return (1);
267 }
268
269 void ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
270 {
271 TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
272 msgbuf->queued++;
273 }
274
275 void ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
276 {
277 TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
278
279 if (buf->fd != -1)
280 close(buf->fd);
281
282 msgbuf->queued--;
283 ibuf_free(buf);
284 }