]> git.proxmox.com Git - mirror_frr.git/blob - lib/imsg.c
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[mirror_frr.git] / lib / imsg.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 imsg_fd_overhead = 0;
25
26 int imsg_get_fd(struct imsgbuf *);
27
28 #ifndef __OpenBSD__
29 /*
30 * The original code calls getdtablecount() which is OpenBSD specific. Use
31 * available_fds() from OpenSMTPD instead.
32 */
33 static int available_fds(unsigned int n)
34 {
35 unsigned int i;
36 int ret, fds[256];
37
38 if (n > (sizeof(fds) / sizeof(fds[0])))
39 return (1);
40
41 ret = 0;
42 for (i = 0; i < n; i++) {
43 fds[i] = -1;
44 if ((fds[i] = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
45 if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT)
46 fds[i] = socket(AF_INET6, SOCK_DGRAM, 0);
47 if (fds[i] < 0) {
48 ret = 1;
49 break;
50 }
51 }
52 }
53
54 for (i = 0; i < n && fds[i] >= 0; i++)
55 close(fds[i]);
56
57 return (ret);
58 }
59 #endif
60
61 void imsg_init(struct imsgbuf *ibuf, int fd)
62 {
63 msgbuf_init(&ibuf->w);
64 memset(&ibuf->r, 0, sizeof(ibuf->r));
65 ibuf->fd = fd;
66 ibuf->w.fd = fd;
67 ibuf->pid = getpid();
68 TAILQ_INIT(&ibuf->fds);
69 }
70
71 ssize_t imsg_read(struct imsgbuf *ibuf)
72 {
73 struct msghdr msg;
74 struct cmsghdr *cmsg;
75 union {
76 struct cmsghdr hdr;
77 char buf[CMSG_SPACE(sizeof(int) * 1)];
78 } cmsgbuf;
79 struct iovec iov;
80 ssize_t n;
81 int fd;
82 struct imsg_fd *ifd;
83
84 memset(&msg, 0, sizeof(msg));
85 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
86
87 iov.iov_base = ibuf->r.buf + ibuf->r.wpos;
88 iov.iov_len = sizeof(ibuf->r.buf) - ibuf->r.wpos;
89 msg.msg_iov = &iov;
90 msg.msg_iovlen = 1;
91 msg.msg_control = &cmsgbuf.buf;
92 msg.msg_controllen = sizeof(cmsgbuf.buf);
93
94 if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL)
95 return (-1);
96
97 again:
98 #ifdef __OpenBSD__
99 if (getdtablecount() + imsg_fd_overhead
100 + (int)((CMSG_SPACE(sizeof(int)) - CMSG_SPACE(0))
101 / sizeof(int))
102 >= getdtablesize()) {
103 #else
104 if (available_fds(imsg_fd_overhead
105 + (CMSG_SPACE(sizeof(int)) - CMSG_SPACE(0))
106 / sizeof(int))) {
107 #endif
108 errno = EAGAIN;
109 free(ifd);
110 return (-1);
111 }
112
113 n = recvmsg(ibuf->fd, &msg, 0);
114 if (n == -1) {
115 if (errno == EINTR)
116 goto again;
117 goto fail;
118 }
119
120 ibuf->r.wpos += n;
121
122 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
123 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
124 if (cmsg->cmsg_level == SOL_SOCKET
125 && cmsg->cmsg_type == SCM_RIGHTS) {
126 int i;
127 int j;
128
129 /*
130 * We only accept one file descriptor. Due to C
131 * padding rules, our control buffer might contain
132 * more than one fd, and we must close them.
133 */
134 j = ((char *)cmsg + cmsg->cmsg_len
135 - (char *)CMSG_DATA(cmsg))
136 / sizeof(int);
137 for (i = 0; i < j; i++) {
138 fd = ((int *)CMSG_DATA(cmsg))[i];
139 if (ifd != NULL) {
140 ifd->fd = fd;
141 TAILQ_INSERT_TAIL(&ibuf->fds, ifd,
142 entry);
143 ifd = NULL;
144 } else
145 close(fd);
146 }
147 }
148 /* we do not handle other ctl data level */
149 }
150
151 fail:
152 free(ifd);
153 return (n);
154 }
155
156 ssize_t imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
157 {
158 size_t av, left, datalen;
159
160 av = ibuf->r.wpos;
161
162 if (IMSG_HEADER_SIZE > av)
163 return (0);
164
165 memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr));
166 if (imsg->hdr.len < IMSG_HEADER_SIZE || imsg->hdr.len > MAX_IMSGSIZE) {
167 errno = ERANGE;
168 return (-1);
169 }
170 if (imsg->hdr.len > av)
171 return (0);
172 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
173 ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE;
174 if (datalen == 0)
175 imsg->data = NULL;
176 else if ((imsg->data = malloc(datalen)) == NULL)
177 return (-1);
178
179 if (imsg->hdr.flags & IMSGF_HASFD)
180 imsg->fd = imsg_get_fd(ibuf);
181 else
182 imsg->fd = -1;
183
184 if (imsg->data)
185 memcpy(imsg->data, ibuf->r.rptr, datalen);
186
187 if (imsg->hdr.len < av) {
188 left = av - imsg->hdr.len;
189 memmove(&ibuf->r.buf, ibuf->r.buf + imsg->hdr.len, left);
190 ibuf->r.wpos = left;
191 } else
192 ibuf->r.wpos = 0;
193
194 return (datalen + IMSG_HEADER_SIZE);
195 }
196
197 int imsg_compose(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid,
198 pid_t pid, int fd, const void *data, uint16_t datalen)
199 {
200 struct ibuf *wbuf;
201
202 if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
203 return (-1);
204
205 if (imsg_add(wbuf, data, datalen) == -1)
206 return (-1);
207
208 wbuf->fd = fd;
209
210 imsg_close(ibuf, wbuf);
211
212 return (1);
213 }
214
215 int imsg_composev(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid,
216 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
217 {
218 struct ibuf *wbuf;
219 int i, datalen = 0;
220
221 for (i = 0; i < iovcnt; i++)
222 datalen += iov[i].iov_len;
223
224 if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
225 return (-1);
226
227 for (i = 0; i < iovcnt; i++)
228 if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
229 return (-1);
230
231 wbuf->fd = fd;
232
233 imsg_close(ibuf, wbuf);
234
235 return (1);
236 }
237
238 /* ARGSUSED */
239 struct ibuf *imsg_create(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid,
240 pid_t pid, uint16_t datalen)
241 {
242 struct ibuf *wbuf;
243 struct imsg_hdr hdr;
244
245 memset(&hdr, 0x00, IMSG_HEADER_SIZE);
246
247 datalen += IMSG_HEADER_SIZE;
248 if (datalen > MAX_IMSGSIZE) {
249 errno = ERANGE;
250 return (NULL);
251 }
252
253 hdr.type = type;
254 hdr.flags = 0;
255 hdr.peerid = peerid;
256 if ((hdr.pid = pid) == 0)
257 hdr.pid = ibuf->pid;
258 if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
259 return (NULL);
260 }
261 if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
262 return (NULL);
263
264 return (wbuf);
265 }
266
267 int imsg_add(struct ibuf *msg, const void *data, uint16_t datalen)
268 {
269 if (datalen)
270 if (ibuf_add(msg, data, datalen) == -1) {
271 ibuf_free(msg);
272 return (-1);
273 }
274 return (datalen);
275 }
276
277 void imsg_close(struct imsgbuf *ibuf, struct ibuf *msg)
278 {
279 struct imsg_hdr *hdr;
280
281 hdr = (struct imsg_hdr *)msg->buf;
282
283 hdr->flags &= ~IMSGF_HASFD;
284 if (msg->fd != -1)
285 hdr->flags |= IMSGF_HASFD;
286
287 hdr->len = (uint16_t)msg->wpos;
288
289 ibuf_close(&ibuf->w, msg);
290 }
291
292 void imsg_free(struct imsg *imsg)
293 {
294 free(imsg->data);
295 }
296
297 int imsg_get_fd(struct imsgbuf *ibuf)
298 {
299 int fd;
300 struct imsg_fd *ifd;
301
302 if ((ifd = TAILQ_POP_FIRST(&ibuf->fds, entry)) == NULL)
303 return (-1);
304
305 fd = ifd->fd;
306 free(ifd);
307
308 return (fd);
309 }
310
311 int imsg_flush(struct imsgbuf *ibuf)
312 {
313 while (ibuf->w.queued)
314 if (msgbuf_write(&ibuf->w) <= 0)
315 return (-1);
316 return (0);
317 }
318
319 void imsg_clear(struct imsgbuf *ibuf)
320 {
321 int fd;
322
323 msgbuf_clear(&ibuf->w);
324 while ((fd = imsg_get_fd(ibuf)) != -1)
325 close(fd);
326 }