diff mbox series

[11/12] netlink: add netlink_message_from_nlmsg

Message ID 20240722190443.43196-11-denkenz@gmail.com (mailing list archive)
State New
Headers show
Series [01/12] useful: Add utility to find the next power of two | expand

Checks

Context Check Description
tedd_an/pre-ci_am success Success

Commit Message

Denis Kenzior July 22, 2024, 7:04 p.m. UTC
This is a low level utility to create a l_netlink_message from raw data
represented by struct nlmsghdr.  Typically used on the RX path when
reading from the socket, or by unit tests.
---
 ell/netlink-private.h |  2 ++
 ell/netlink.c         | 11 +++++++++++
 2 files changed, 13 insertions(+)
diff mbox series

Patch

diff --git a/ell/netlink-private.h b/ell/netlink-private.h
index db4317786a8f..b114f62f6bf3 100644
--- a/ell/netlink-private.h
+++ b/ell/netlink-private.h
@@ -47,3 +47,5 @@  bool netlink_parse_ext_ack_error(const struct nlmsghdr *nlmsg,
 					uint32_t *out_error_offset);
 int netlink_message_reserve_header(struct l_netlink_message *message,
 					size_t header_len, void **out_header);
+struct l_netlink_message *netlink_message_from_nlmsg(
+						const struct nlmsghdr *nlmsg);
diff --git a/ell/netlink.c b/ell/netlink.c
index f73b876b4ef0..024740b62fc3 100644
--- a/ell/netlink.c
+++ b/ell/netlink.c
@@ -781,6 +781,17 @@  LIB_EXPORT struct l_netlink_message *l_netlink_message_new(uint16_t type,
 	return l_netlink_message_new_sized(type, flags, 256 - NLMSG_HDRLEN);
 }
 
+struct l_netlink_message *netlink_message_from_nlmsg(
+						const struct nlmsghdr *nlmsg)
+{
+	struct l_netlink_message *message = l_new(struct l_netlink_message, 1);
+
+	message->hdr = l_memdup(nlmsg, nlmsg->nlmsg_len);
+	message->size = nlmsg->nlmsg_len;
+
+	return l_netlink_message_ref(message);
+}
+
 LIB_EXPORT struct l_netlink_message *l_netlink_message_ref(
 					struct l_netlink_message *message)
 {