1 /* $OpenBSD: imsg-buffer.c,v 1.18 2023/12/12 15:47:41 claudio Exp $ */
4 * Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org>
5 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/types.h>
21 #include <sys/socket.h>
23 #include <arpa/inet.h>
32 #include "got_compat.h"
35 static int ibuf_realloc(struct ibuf
*, size_t);
36 static void ibuf_enqueue(struct msgbuf
*, struct ibuf
*);
37 static void ibuf_dequeue(struct msgbuf
*, struct ibuf
*);
38 static void msgbuf_drain(struct msgbuf
*, size_t);
49 if ((buf
= calloc(1, sizeof(struct ibuf
))) == NULL
)
51 if ((buf
->buf
= calloc(len
, 1)) == NULL
) {
55 buf
->size
= buf
->max
= len
;
62 ibuf_dynamic(size_t len
, size_t max
)
66 if (max
== 0 || max
< len
) {
71 if ((buf
= calloc(1, sizeof(struct ibuf
))) == NULL
)
74 if ((buf
->buf
= calloc(len
, 1)) == NULL
) {
87 ibuf_realloc(struct ibuf
*buf
, size_t len
)
91 /* on static buffers max is eq size and so the following fails */
92 if (len
> SIZE_MAX
- buf
->wpos
|| buf
->wpos
+ len
> buf
->max
) {
97 b
= recallocarray(buf
->buf
, buf
->size
, buf
->wpos
+ len
, 1);
101 buf
->size
= buf
->wpos
+ len
;
107 ibuf_reserve(struct ibuf
*buf
, size_t len
)
111 if (len
> SIZE_MAX
- buf
->wpos
|| buf
->max
== 0) {
116 if (buf
->wpos
+ len
> buf
->size
)
117 if (ibuf_realloc(buf
, len
) == -1)
120 b
= buf
->buf
+ buf
->wpos
;
126 ibuf_add(struct ibuf
*buf
, const void *data
, size_t len
)
130 if ((b
= ibuf_reserve(buf
, len
)) == NULL
)
133 memcpy(b
, data
, len
);
138 ibuf_add_ibuf(struct ibuf
*buf
, const struct ibuf
*from
)
140 return ibuf_add(buf
, ibuf_data(from
), ibuf_size(from
));
143 /* remove after tree is converted */
145 ibuf_add_buf(struct ibuf
*buf
, const struct ibuf
*from
)
147 return ibuf_add_ibuf(buf
, from
);
151 ibuf_add_n8(struct ibuf
*buf
, uint64_t value
)
155 if (value
> UINT8_MAX
) {
160 return ibuf_add(buf
, &v
, sizeof(v
));
164 ibuf_add_n16(struct ibuf
*buf
, uint64_t value
)
168 if (value
> UINT16_MAX
) {
173 return ibuf_add(buf
, &v
, sizeof(v
));
177 ibuf_add_n32(struct ibuf
*buf
, uint64_t value
)
181 if (value
> UINT32_MAX
) {
186 return ibuf_add(buf
, &v
, sizeof(v
));
190 ibuf_add_n64(struct ibuf
*buf
, uint64_t value
)
192 value
= htobe64(value
);
193 return ibuf_add(buf
, &value
, sizeof(value
));
197 ibuf_add_h16(struct ibuf
*buf
, uint64_t value
)
201 if (value
> UINT16_MAX
) {
206 return ibuf_add(buf
, &v
, sizeof(v
));
210 ibuf_add_h32(struct ibuf
*buf
, uint64_t value
)
214 if (value
> UINT32_MAX
) {
219 return ibuf_add(buf
, &v
, sizeof(v
));
223 ibuf_add_h64(struct ibuf
*buf
, uint64_t value
)
225 return ibuf_add(buf
, &value
, sizeof(value
));
229 ibuf_add_zero(struct ibuf
*buf
, size_t len
)
233 if ((b
= ibuf_reserve(buf
, len
)) == NULL
)
240 ibuf_seek(struct ibuf
*buf
, size_t pos
, size_t len
)
242 /* only allow seeking between rpos and wpos */
243 if (ibuf_size(buf
) < pos
|| SIZE_MAX
- pos
< len
||
244 ibuf_size(buf
) < pos
+ len
) {
249 return (buf
->buf
+ buf
->rpos
+ pos
);
253 ibuf_set(struct ibuf
*buf
, size_t pos
, const void *data
, size_t len
)
257 if ((b
= ibuf_seek(buf
, pos
, len
)) == NULL
)
260 memcpy(b
, data
, len
);
265 ibuf_set_n8(struct ibuf
*buf
, size_t pos
, uint64_t value
)
269 if (value
> UINT8_MAX
) {
274 return (ibuf_set(buf
, pos
, &v
, sizeof(v
)));
278 ibuf_set_n16(struct ibuf
*buf
, size_t pos
, uint64_t value
)
282 if (value
> UINT16_MAX
) {
287 return (ibuf_set(buf
, pos
, &v
, sizeof(v
)));
291 ibuf_set_n32(struct ibuf
*buf
, size_t pos
, uint64_t value
)
295 if (value
> UINT32_MAX
) {
300 return (ibuf_set(buf
, pos
, &v
, sizeof(v
)));
304 ibuf_set_n64(struct ibuf
*buf
, size_t pos
, uint64_t value
)
306 value
= htobe64(value
);
307 return (ibuf_set(buf
, pos
, &value
, sizeof(value
)));
311 ibuf_set_h16(struct ibuf
*buf
, size_t pos
, uint64_t value
)
315 if (value
> UINT16_MAX
) {
320 return (ibuf_set(buf
, pos
, &v
, sizeof(v
)));
324 ibuf_set_h32(struct ibuf
*buf
, size_t pos
, uint64_t value
)
328 if (value
> UINT32_MAX
) {
333 return (ibuf_set(buf
, pos
, &v
, sizeof(v
)));
337 ibuf_set_h64(struct ibuf
*buf
, size_t pos
, uint64_t value
)
339 return (ibuf_set(buf
, pos
, &value
, sizeof(value
)));
343 ibuf_data(const struct ibuf
*buf
)
345 return (buf
->buf
+ buf
->rpos
);
349 ibuf_size(const struct ibuf
*buf
)
351 return (buf
->wpos
- buf
->rpos
);
355 ibuf_left(const struct ibuf
*buf
)
359 return (buf
->max
- buf
->wpos
);
363 ibuf_truncate(struct ibuf
*buf
, size_t len
)
365 if (ibuf_size(buf
) >= len
) {
366 buf
->wpos
= buf
->rpos
+ len
;
370 /* only allow to truncate down */
374 return ibuf_add_zero(buf
, len
- ibuf_size(buf
));
378 ibuf_rewind(struct ibuf
*buf
)
384 ibuf_close(struct msgbuf
*msgbuf
, struct ibuf
*buf
)
386 ibuf_enqueue(msgbuf
, buf
);
390 ibuf_from_buffer(struct ibuf
*buf
, void *data
, size_t len
)
392 memset(buf
, 0, sizeof(*buf
));
394 buf
->size
= buf
->wpos
= len
;
399 ibuf_from_ibuf(struct ibuf
*buf
, const struct ibuf
*from
)
401 ibuf_from_buffer(buf
, ibuf_data(from
), ibuf_size(from
));
405 ibuf_get(struct ibuf
*buf
, void *data
, size_t len
)
407 if (ibuf_size(buf
) < len
) {
412 memcpy(data
, ibuf_data(buf
), len
);
418 ibuf_get_ibuf(struct ibuf
*buf
, size_t len
, struct ibuf
*new)
420 if (ibuf_size(buf
) < len
) {
425 ibuf_from_buffer(new, ibuf_data(buf
), len
);
431 ibuf_get_n8(struct ibuf
*buf
, uint8_t *value
)
433 return ibuf_get(buf
, value
, sizeof(*value
));
437 ibuf_get_n16(struct ibuf
*buf
, uint16_t *value
)
441 rv
= ibuf_get(buf
, value
, sizeof(*value
));
442 *value
= be16toh(*value
);
447 ibuf_get_n32(struct ibuf
*buf
, uint32_t *value
)
451 rv
= ibuf_get(buf
, value
, sizeof(*value
));
452 *value
= be32toh(*value
);
457 ibuf_get_n64(struct ibuf
*buf
, uint64_t *value
)
461 rv
= ibuf_get(buf
, value
, sizeof(*value
));
462 *value
= be64toh(*value
);
467 ibuf_get_h16(struct ibuf
*buf
, uint16_t *value
)
469 return ibuf_get(buf
, value
, sizeof(*value
));
473 ibuf_get_h32(struct ibuf
*buf
, uint32_t *value
)
475 return ibuf_get(buf
, value
, sizeof(*value
));
479 ibuf_get_h64(struct ibuf
*buf
, uint64_t *value
)
481 return ibuf_get(buf
, value
, sizeof(*value
));
485 ibuf_skip(struct ibuf
*buf
, size_t len
)
487 if (ibuf_size(buf
) < len
) {
497 ibuf_free(struct ibuf
*buf
)
501 if (buf
->max
== 0) /* if buf lives on the stack */
502 abort(); /* abort before causing more harm */
505 freezero(buf
->buf
, buf
->size
);
510 ibuf_fd_avail(struct ibuf
*buf
)
512 return (buf
->fd
!= -1);
516 ibuf_fd_get(struct ibuf
*buf
)
526 ibuf_fd_set(struct ibuf
*buf
, int fd
)
528 if (buf
->max
== 0) /* if buf lives on the stack */
529 abort(); /* abort before causing more harm */
536 ibuf_write(struct msgbuf
*msgbuf
)
538 struct iovec iov
[IOV_MAX
];
543 memset(&iov
, 0, sizeof(iov
));
544 TAILQ_FOREACH(buf
, &msgbuf
->bufs
, entry
) {
547 iov
[i
].iov_base
= ibuf_data(buf
);
548 iov
[i
].iov_len
= ibuf_size(buf
);
553 if ((n
= writev(msgbuf
->fd
, iov
, i
)) == -1) {
556 if (errno
== ENOBUFS
)
561 if (n
== 0) { /* connection closed */
566 msgbuf_drain(msgbuf
, n
);
572 msgbuf_init(struct msgbuf
*msgbuf
)
576 TAILQ_INIT(&msgbuf
->bufs
);
580 msgbuf_drain(struct msgbuf
*msgbuf
, size_t n
)
582 struct ibuf
*buf
, *next
;
584 for (buf
= TAILQ_FIRST(&msgbuf
->bufs
); buf
!= NULL
&& n
> 0;
586 next
= TAILQ_NEXT(buf
, entry
);
587 if (n
>= ibuf_size(buf
)) {
589 ibuf_dequeue(msgbuf
, buf
);
598 msgbuf_clear(struct msgbuf
*msgbuf
)
602 while ((buf
= TAILQ_FIRST(&msgbuf
->bufs
)) != NULL
)
603 ibuf_dequeue(msgbuf
, buf
);
607 msgbuf_write(struct msgbuf
*msgbuf
)
609 struct iovec iov
[IOV_MAX
];
610 struct ibuf
*buf
, *buf0
= NULL
;
614 struct cmsghdr
*cmsg
;
617 char buf
[CMSG_SPACE(sizeof(int))];
620 memset(&iov
, 0, sizeof(iov
));
621 memset(&msg
, 0, sizeof(msg
));
622 memset(&cmsgbuf
, 0, sizeof(cmsgbuf
));
623 TAILQ_FOREACH(buf
, &msgbuf
->bufs
, entry
) {
626 if (i
> 0 && buf
->fd
!= -1)
628 iov
[i
].iov_base
= ibuf_data(buf
);
629 iov
[i
].iov_len
= ibuf_size(buf
);
639 msg
.msg_control
= (caddr_t
)&cmsgbuf
.buf
;
640 msg
.msg_controllen
= sizeof(cmsgbuf
.buf
);
641 cmsg
= CMSG_FIRSTHDR(&msg
);
642 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
643 cmsg
->cmsg_level
= SOL_SOCKET
;
644 cmsg
->cmsg_type
= SCM_RIGHTS
;
645 *(int *)CMSG_DATA(cmsg
) = buf0
->fd
;
649 if ((n
= sendmsg(msgbuf
->fd
, &msg
, 0)) == -1) {
652 if (errno
== ENOBUFS
)
657 if (n
== 0) { /* connection closed */
663 * assumption: fd got sent if sendmsg sent anything
664 * this works because fds are passed one at a time
671 msgbuf_drain(msgbuf
, n
);
677 msgbuf_queuelen(struct msgbuf
*msgbuf
)
679 return (msgbuf
->queued
);
683 ibuf_enqueue(struct msgbuf
*msgbuf
, struct ibuf
*buf
)
685 if (buf
->max
== 0) /* if buf lives on the stack */
686 abort(); /* abort before causing more harm */
687 TAILQ_INSERT_TAIL(&msgbuf
->bufs
, buf
, entry
);
692 ibuf_dequeue(struct msgbuf
*msgbuf
, struct ibuf
*buf
)
694 TAILQ_REMOVE(&msgbuf
->bufs
, buf
, entry
);