genl: introduce genl_nlmsg_build_hdr and use it

Add new generic function to build the netlink and genetlink headers.
This commit is contained in:
Pablo Neira Ayuso 2014-01-21 17:32:32 +01:00
parent 49ed1fb379
commit fa4c10f4d7
3 changed files with 25 additions and 10 deletions

19
genl.c
View File

@ -6,6 +6,25 @@
#include <libmnl/libmnl.h>
#include <linux/genetlink.h>
struct nlmsghdr *
genl_nlmsg_build_hdr(char *buf, uint16_t type, uint16_t flags, uint32_t seq,
uint8_t cmd)
{
struct nlmsghdr *nlh;
struct genlmsghdr *genl;
nlh = mnl_nlmsg_put_header(buf);
nlh->nlmsg_type = type;
nlh->nlmsg_flags = NLM_F_REQUEST | flags;
nlh->nlmsg_seq = seq;
genl = mnl_nlmsg_put_extra_header(nlh, sizeof(struct genlmsghdr));
genl->cmd = cmd;
genl->version = 0;
return nlh;
}
static int genl_ctrl_validate_cb(const struct nlattr *attr, void *data)
{
const struct nlattr **tb = data;

3
genl.h
View File

@ -3,6 +3,9 @@
#include <libmnl/libmnl.h>
struct nlmsghdr *genl_nlmsg_build_hdr(char *buf, uint16_t type, uint16_t flags,
uint32_t seq, uint8_t cmd);
struct mnl_socket *genl_socket_open(void);
int genl_socket_talk(struct mnl_socket *nl, struct nlmsghdr *nlh,
int (*cb)(const struct nlmsghdr *nlh, void *data),

View File

@ -11,19 +11,12 @@
static uint32_t seq;
static struct nlmsghdr *build_msg(int genl_type, char *buf, int i, uint32_t ifidx)
static struct nlmsghdr *build_msg(int type, char *buf, int i, uint32_t ifidx)
{
struct nlmsghdr *nlh;
struct genlmsghdr *genl;
nlh = mnl_nlmsg_put_header(buf);
nlh->nlmsg_type = genl_type;
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
nlh->nlmsg_seq = ++seq;
genl = mnl_nlmsg_put_extra_header(nlh, sizeof(struct genlmsghdr));
genl->cmd = GTP_CMD_TUNNEL_NEW;
genl->version = 0;
nlh = genl_nlmsg_build_hdr(buf, type, NLM_F_ACK, ++seq,
GTP_CMD_TUNNEL_NEW);
mnl_attr_put_u32(nlh, GTPA_VERSION, GTP_V0);
mnl_attr_put_u32(nlh, GTPA_LINK, ifidx);