@@ -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;
}
@@ -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__ */
@@ -8,6 +8,8 @@
*
*/
+#include <string.h>
+#include <time.h>
#include <libmnl/libmnl.h>
#include "mnl_utils.h"
@@ -33,3 +35,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;
+}
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> --- devlink/mnlg.c | 18 ++++++------------ include/mnl_utils.h | 2 ++ lib/mnl_utils.c | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 12 deletions(-)