From fa4c10f4d7a7b9e4b3217289d13cfe78061649dd Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Tue, 21 Jan 2014 17:32:32 +0100 Subject: [PATCH] genl: introduce genl_nlmsg_build_hdr and use it Add new generic function to build the netlink and genetlink headers. --- genl.c | 19 +++++++++++++++++++ genl.h | 3 +++ gtp-tunnel-add.c | 13 +++---------- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/genl.c b/genl.c index b2edec3..a682e1f 100644 --- a/genl.c +++ b/genl.c @@ -6,6 +6,25 @@ #include #include +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; diff --git a/genl.h b/genl.h index ccecfde..684c8d0 100644 --- a/genl.h +++ b/genl.h @@ -3,6 +3,9 @@ #include +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), diff --git a/gtp-tunnel-add.c b/gtp-tunnel-add.c index 2e34319..08679c2 100644 --- a/gtp-tunnel-add.c +++ b/gtp-tunnel-add.c @@ -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);