summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudio Jeker <claudio@cvs.openbsd.org>2024-11-21 12:42:15 +0000
committerClaudio Jeker <claudio@cvs.openbsd.org>2024-11-21 12:42:15 +0000
commitfd5a9d3c4d521a97faeb5ff6fc6a11bc36b3d432 (patch)
tree3a39050ea53ec987674fe97afd2179c306e2fb7a
parent955dd76905f58ec28f0ac31a679c6a885bb0ca5d (diff)
Add ibuf_get_string() to the ibuf API (used by bgpd and xlockmore)
OK tb@
-rw-r--r--lib/libutil/Symbols.map1
-rw-r--r--lib/libutil/ibuf_add.319
-rw-r--r--lib/libutil/imsg-buffer.c45
-rw-r--r--lib/libutil/imsg.h3
4 files changed, 51 insertions, 17 deletions
diff --git a/lib/libutil/Symbols.map b/lib/libutil/Symbols.map
index c1bac13aa21..9b6ddfe712d 100644
--- a/lib/libutil/Symbols.map
+++ b/lib/libutil/Symbols.map
@@ -46,6 +46,7 @@
ibuf_get_n32;
ibuf_get_n64;
ibuf_get_n8;
+ ibuf_get_string;
ibuf_left;
ibuf_open;
ibuf_reserve;
diff --git a/lib/libutil/ibuf_add.3 b/lib/libutil/ibuf_add.3
index fa3e2dace5e..e8486aa7324 100644
--- a/lib/libutil/ibuf_add.3
+++ b/lib/libutil/ibuf_add.3
@@ -1,4 +1,4 @@
-.\" $OpenBSD: ibuf_add.3,v 1.1 2023/12/12 15:49:21 claudio Exp $
+.\" $OpenBSD: ibuf_add.3,v 1.2 2024/11/21 12:42:14 claudio Exp $
.\"
.\" Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org>
.\" Copyright (c) 2010 Nicholas Marriott <nicm@openbsd.org>
@@ -15,7 +15,7 @@
.\" IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
.\" OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
-.Dd $Mdocdate: December 12 2023 $
+.Dd $Mdocdate: November 21 2024 $
.Dt IBUF_ADD 3
.Os
.Sh NAME
@@ -47,6 +47,7 @@
.Nm ibuf_get_n32 ,
.Nm ibuf_get_n64 ,
.Nm ibuf_get_n8 ,
+.Nm ibuf_get_string ,
.Nm ibuf_left ,
.Nm ibuf_open ,
.Nm ibuf_reserve ,
@@ -128,6 +129,8 @@
.Fn ibuf_get_n64 "struct ibuf *buf" "uint64_t *value"
.Ft int
.Fn ibuf_get_n8 "struct ibuf *buf" "uint8_t *value"
+.Ft "char *"
+.Fn ibuf_get_string "struct ibuf *buf" "size_t len"
.Ft size_t
.Fn ibuf_left "const struct ibuf *buf"
.Ft "struct ibuf *"
@@ -355,6 +358,18 @@ from
converting the value from network to host byte order.
0 is returned on success and \-1 on failure.
.Pp
+.Fn ibuf_get_string
+consumes
+.Fa len
+bytes from the buffer
+.Fa buf
+and returns the result of passing the bytes and len to
+.Xr strndup(3) .
+The returned pointer should be passed to
+.Xr free 3
+when it is no longer needed.
+On error NULL is returned.
+.Pp
The
.Fn ibuf_open
function allocates a fixed-length buffer.
diff --git a/lib/libutil/imsg-buffer.c b/lib/libutil/imsg-buffer.c
index d45802d8d04..4a3a352e92c 100644
--- a/lib/libutil/imsg-buffer.c
+++ b/lib/libutil/imsg-buffer.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: imsg-buffer.c,v 1.19 2024/08/26 13:57:34 claudio Exp $ */
+/* $OpenBSD: imsg-buffer.c,v 1.20 2024/11/21 12:42:14 claudio Exp $ */
/*
* Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org>
@@ -429,6 +429,24 @@ ibuf_get_ibuf(struct ibuf *buf, size_t len, struct ibuf *new)
}
int
+ibuf_get_h16(struct ibuf *buf, uint16_t *value)
+{
+ return ibuf_get(buf, value, sizeof(*value));
+}
+
+int
+ibuf_get_h32(struct ibuf *buf, uint32_t *value)
+{
+ return ibuf_get(buf, value, sizeof(*value));
+}
+
+int
+ibuf_get_h64(struct ibuf *buf, uint64_t *value)
+{
+ return ibuf_get(buf, value, sizeof(*value));
+}
+
+int
ibuf_get_n8(struct ibuf *buf, uint8_t *value)
{
return ibuf_get(buf, value, sizeof(*value));
@@ -464,22 +482,21 @@ ibuf_get_n64(struct ibuf *buf, uint64_t *value)
return (rv);
}
-int
-ibuf_get_h16(struct ibuf *buf, uint16_t *value)
+char *
+ibuf_get_string(struct ibuf *buf, size_t len)
{
- return ibuf_get(buf, value, sizeof(*value));
-}
+ char *str;
-int
-ibuf_get_h32(struct ibuf *buf, uint32_t *value)
-{
- return ibuf_get(buf, value, sizeof(*value));
-}
+ if (ibuf_size(buf) < len) {
+ errno = EBADMSG;
+ return (NULL);
+ }
-int
-ibuf_get_h64(struct ibuf *buf, uint64_t *value)
-{
- return ibuf_get(buf, value, sizeof(*value));
+ str = strndup(ibuf_data(buf), len);
+ if (str == NULL)
+ return (NULL);
+ buf->rpos += len;
+ return (str);
}
int
diff --git a/lib/libutil/imsg.h b/lib/libutil/imsg.h
index dd47b1889da..e105de71573 100644
--- a/lib/libutil/imsg.h
+++ b/lib/libutil/imsg.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: imsg.h,v 1.8 2023/12/12 15:47:41 claudio Exp $ */
+/* $OpenBSD: imsg.h,v 1.9 2024/11/21 12:42:14 claudio Exp $ */
/*
* Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org>
@@ -119,6 +119,7 @@ int ibuf_get_n64(struct ibuf *, uint64_t *);
int ibuf_get_h16(struct ibuf *, uint16_t *);
int ibuf_get_h32(struct ibuf *, uint32_t *);
int ibuf_get_h64(struct ibuf *, uint64_t *);
+char *ibuf_get_string(struct ibuf *, size_t);
int ibuf_skip(struct ibuf *, size_t);
void ibuf_free(struct ibuf *);
int ibuf_fd_avail(struct ibuf *);