]> git.proxmox.com Git - mirror_iproute2.git/commitdiff
lib: Extract from devlink/mnlg a helper, mnlu_msg_prepare()
authorPetr Machata <me@pmachata.org>
Thu, 12 Nov 2020 22:24:42 +0000 (23:24 +0100)
committerDavid Ahern <dsahern@gmail.com>
Sat, 14 Nov 2020 02:43:15 +0000 (19:43 -0700)
Allocation of a new netlink message with the two usual headers is reusable
with other netlink netlink message types. Extract it into a helper,
mnlu_msg_prepare(). Take the second header as an argument, instead of
passing in parameters to initialize it, and copy it in.

Signed-off-by: Petr Machata <me@pmachata.org>
Signed-off-by: David Ahern <dsahern@gmail.com>
devlink/mnlg.c
include/mnl_utils.h
lib/mnl_utils.c

index 9817bbad5e7d8b4ca613d8261c79ad4a18619e37..4995b7af06a3953e68dc29924f91cfd4368c3a59 100644 (file)
@@ -14,7 +14,6 @@
 #include <string.h>
 #include <errno.h>
 #include <unistd.h>
-#include <time.h>
 #include <libmnl/libmnl.h>
 #include <linux/genetlink.h>
 
@@ -36,19 +35,14 @@ static struct nlmsghdr *__mnlg_msg_prepare(struct mnlg_socket *nlg, uint8_t cmd,
                                           uint16_t flags, uint32_t id,
                                           uint8_t version)
 {
+       struct genlmsghdr genl = {
+               .cmd = cmd,
+               .version = version,
+       };
        struct nlmsghdr *nlh;
-       struct genlmsghdr *genl;
-
-       nlh = mnl_nlmsg_put_header(nlg->buf);
-       nlh->nlmsg_type = id;
-       nlh->nlmsg_flags = flags;
-       nlg->seq = time(NULL);
-       nlh->nlmsg_seq = nlg->seq;
-
-       genl = mnl_nlmsg_put_extra_header(nlh, sizeof(struct genlmsghdr));
-       genl->cmd = cmd;
-       genl->version = version;
 
+       nlh = mnlu_msg_prepare(nlg->buf, id, flags, &genl, sizeof(genl));
+       nlg->seq = nlh->nlmsg_seq;
        return nlh;
 }
 
index 10a064afdfe87472be23274933514e0479e94ba9..86ce30f49a94c878e5dff73683e31bb734ccdd1b 100644 (file)
@@ -3,5 +3,7 @@
 #define __MNL_UTILS_H__ 1
 
 struct mnl_socket *mnlu_socket_open(int bus);
+struct nlmsghdr *mnlu_msg_prepare(void *buf, uint32_t nlmsg_type, uint16_t flags,
+                                 void *extra_header, size_t extra_header_size);
 
 #endif /* __MNL_UTILS_H__ */
index 2426912aa5111d54ae11074a6190a9b5d4e7b22e..61e8060ecbca1563777ab7a7aa92e26423305b5f 100644 (file)
@@ -3,6 +3,8 @@
  * mnl_utils.c Helpers for working with libmnl.
  */
 
+#include <string.h>
+#include <time.h>
 #include <libmnl/libmnl.h>
 
 #include "mnl_utils.h"
@@ -28,3 +30,20 @@ err_bind:
        mnl_socket_close(nl);
        return NULL;
 }
+
+struct nlmsghdr *mnlu_msg_prepare(void *buf, uint32_t nlmsg_type, uint16_t flags,
+                                 void *extra_header, size_t extra_header_size)
+{
+       struct nlmsghdr *nlh;
+       void *eh;
+
+       nlh = mnl_nlmsg_put_header(buf);
+       nlh->nlmsg_type = nlmsg_type;
+       nlh->nlmsg_flags = flags;
+       nlh->nlmsg_seq = time(NULL);
+
+       eh = mnl_nlmsg_put_extra_header(nlh, extra_header_size);
+       memcpy(eh, extra_header, extra_header_size);
+
+       return nlh;
+}