lib: Extract from devlink/mnlg a helper, mnlu_msg_prepare()
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>
This commit is contained in:
parent
72858c7b77
commit
dd78dfc7be
|
|
@ -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__ */
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue