summaryrefslogtreecommitdiffstats
path: root/examples/nft-map-add.c
diff options
context:
space:
mode:
authorElise Lennion <elise.lennion@gmail.com>2017-01-12 17:22:55 -0200
committerPablo Neira Ayuso <pablo@netfilter.org>2017-01-16 11:38:04 +0100
commit7e549c5e7874016aad52681001d455666d24266d (patch)
tree36711c6d7370a85d006a83b9a671632a9a388a05 /examples/nft-map-add.c
parent6876487abd8041cd2a718ed7dd9ff6d86560b1ee (diff)
examples: Remove the use of nftnl_mnl_batch_put()
use nftnl_batch_begin() and nftnl_batch_end() instead, to keep examples consistent and avoid code duplication. Signed-off-by: Elise Lennion <elise.lennion@gmail.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'examples/nft-map-add.c')
-rw-r--r--examples/nft-map-add.c38
1 files changed, 15 insertions, 23 deletions
diff --git a/examples/nft-map-add.c b/examples/nft-map-add.c
index 53244f5..a906d2d 100644
--- a/examples/nft-map-add.c
+++ b/examples/nft-map-add.c
@@ -54,22 +54,6 @@ static struct nftnl_set *setup_set(uint8_t family, const char *table,
return s;
}
-static void nftnl_mnl_batch_put(char *buf, uint16_t type, uint32_t seq)
-{
- struct nlmsghdr *nlh;
- struct nfgenmsg *nfg;
-
- nlh = mnl_nlmsg_put_header(buf);
- nlh->nlmsg_type = type;
- nlh->nlmsg_flags = NLM_F_REQUEST;
- nlh->nlmsg_seq = seq;
-
- nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg));
- nfg->nfgen_family = AF_INET;
- nfg->version = NFNETLINK_V0;
- nfg->res_id = NFNL_SUBSYS_NFTABLES;
-}
-
int main(int argc, char *argv[])
{
struct mnl_socket *nl;
@@ -79,7 +63,7 @@ int main(int argc, char *argv[])
uint8_t family;
char buf[MNL_SOCKET_BUFFER_SIZE];
uint32_t seq = time(NULL);
- int ret;
+ int ret, batching;
if (argc != 4) {
fprintf(stderr, "Usage: %s <family> <table> <setname>\n", argv[0]);
@@ -112,11 +96,18 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
+ batching = nftnl_batch_is_supported();
+ if (batching < 0) {
+ perror("cannot talk to nfnetlink");
+ exit(EXIT_FAILURE);
+ }
+
batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
- nftnl_mnl_batch_put(mnl_nlmsg_batch_current(batch),
- NFNL_MSG_BATCH_BEGIN, seq++);
- mnl_nlmsg_batch_next(batch);
+ if (batching) {
+ nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
+ mnl_nlmsg_batch_next(batch);
+ }
nlh = nftnl_set_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
NFT_MSG_NEWSET, family,
@@ -126,9 +117,10 @@ int main(int argc, char *argv[])
nftnl_set_free(s);
mnl_nlmsg_batch_next(batch);
- nftnl_mnl_batch_put(mnl_nlmsg_batch_current(batch), NFNL_MSG_BATCH_END,
- seq++);
- mnl_nlmsg_batch_next(batch);
+ if (batching) {
+ nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
+ mnl_nlmsg_batch_next(batch);
+ }
ret = mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
mnl_nlmsg_batch_size(batch));