]> git.proxmox.com Git - mirror_frr.git/blob - lib/imsg-buffer.c
Merge pull request #805 from Orange-OpenSource/master
[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 "openbsd-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 u_char *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 {
137 if (i >= IOV_MAX)
138 break;
139 iov[i].iov_base = buf->buf + buf->rpos;
140 iov[i].iov_len = buf->wpos - buf->rpos;
141 i++;
142 }
143
144 again:
145 if ((n = writev(msgbuf->fd, iov, i)) == -1) {
146 if (errno == EINTR)
147 goto again;
148 if (errno == ENOBUFS)
149 errno = EAGAIN;
150 return (-1);
151 }
152
153 if (n == 0) { /* connection closed */
154 errno = 0;
155 return (0);
156 }
157
158 msgbuf_drain(msgbuf, n);
159
160 return (1);
161 }
162
163 void ibuf_free(struct ibuf *buf)
164 {
165 if (buf == NULL)
166 return;
167 free(buf->buf);
168 free(buf);
169 }
170
171 void msgbuf_init(struct msgbuf *msgbuf)
172 {
173 msgbuf->queued = 0;
174 msgbuf->fd = -1;
175 TAILQ_INIT(&msgbuf->bufs);
176 }
177
178 void msgbuf_drain(struct msgbuf *msgbuf, size_t n)
179 {
180 struct ibuf *buf, *next;
181
182 for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
183 buf = next) {
184 next = TAILQ_NEXT(buf, entry);
185 if (buf->rpos + n >= buf->wpos) {
186 n -= buf->wpos - buf->rpos;
187 ibuf_dequeue(msgbuf, buf);
188 } else {
189 buf->rpos += n;
190 n = 0;
191 }
192 }
193 }
194
195 void msgbuf_clear(struct msgbuf *msgbuf)
196 {
197 struct ibuf *buf;
198
199 while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
200 ibuf_dequeue(msgbuf, buf);
201 }
202
203 int msgbuf_write(struct msgbuf *msgbuf)
204 {
205 struct iovec iov[IOV_MAX];
206 struct ibuf *buf;
207 unsigned int i = 0;
208 ssize_t n;
209 struct msghdr msg;
210 struct cmsghdr *cmsg;
211 union {
212 struct cmsghdr hdr;
213 char buf[CMSG_SPACE(sizeof(int))];
214 } cmsgbuf;
215
216 memset(&iov, 0, sizeof(iov));
217 memset(&msg, 0, sizeof(msg));
218 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
219 TAILQ_FOREACH(buf, &msgbuf->bufs, entry)
220 {
221 if (i >= IOV_MAX)
222 break;
223 iov[i].iov_base = buf->buf + buf->rpos;
224 iov[i].iov_len = buf->wpos - buf->rpos;
225 i++;
226 if (buf->fd != -1)
227 break;
228 }
229
230 msg.msg_iov = iov;
231 msg.msg_iovlen = i;
232
233 if (buf != NULL && buf->fd != -1) {
234 msg.msg_control = (caddr_t)&cmsgbuf.buf;
235 msg.msg_controllen = sizeof(cmsgbuf.buf);
236 cmsg = CMSG_FIRSTHDR(&msg);
237 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
238 cmsg->cmsg_level = SOL_SOCKET;
239 cmsg->cmsg_type = SCM_RIGHTS;
240 memcpy(CMSG_DATA(cmsg), &buf->fd, sizeof(int));
241 }
242
243 again:
244 if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
245 if (errno == EINTR)
246 goto again;
247 if (errno == ENOBUFS)
248 errno = EAGAIN;
249 return (-1);
250 }
251
252 if (n == 0) { /* connection closed */
253 errno = 0;
254 return (0);
255 }
256
257 /*
258 * assumption: fd got sent if sendmsg sent anything
259 * this works because fds are passed one at a time
260 */
261 if (buf != NULL && buf->fd != -1) {
262 close(buf->fd);
263 buf->fd = -1;
264 }
265
266 msgbuf_drain(msgbuf, n);
267
268 return (1);
269 }
270
271 void ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
272 {
273 TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
274 msgbuf->queued++;
275 }
276
277 void ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
278 {
279 TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
280
281 if (buf->fd != -1)
282 close(buf->fd);
283
284 msgbuf->queued--;
285 ibuf_free(buf);
286 }