From: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
To: netfilter-devel@vger.kernel.org
Cc: pablo@netfilter.org
Subject: [RFC libnftnl PATCH 2/2] examples: use new mnlio API in nft-rule-add.c
Date: Wed, 05 Feb 2014 20:17:52 +0100 [thread overview]
Message-ID: <20140205191752.9979.97410.stgit@nfdev.cica.es> (raw)
In-Reply-To: <20140205191711.9979.23980.stgit@nfdev.cica.es>
Update this example to use the new mnlio API.
Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
---
examples/nft-rule-add.c | 87 ++++-------------------------------------------
1 file changed, 7 insertions(+), 80 deletions(-)
diff --git a/examples/nft-rule-add.c b/examples/nft-rule-add.c
index dbe93f5..7ee73d1 100644
--- a/examples/nft-rule-add.c
+++ b/examples/nft-rule-add.c
@@ -28,6 +28,7 @@
#include <libmnl/libmnl.h>
#include <libnftnl/rule.h>
#include <libnftnl/expr.h>
+#include <libnftnl/mnlio.h>
static void add_payload(struct nft_rule *r, uint32_t base, uint32_t dreg,
uint32_t offset, uint32_t len)
@@ -111,78 +112,12 @@ static struct nft_rule *setup_rule(uint8_t family, const char *table,
return r;
}
-static int seq;
-
-static void nft_mnl_batch_put(struct mnl_nlmsg_batch *batch, int type)
-{
- struct nlmsghdr *nlh;
- struct nfgenmsg *nfg;
-
- nlh = mnl_nlmsg_put_header(mnl_nlmsg_batch_current(batch));
- 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;
-
- mnl_nlmsg_batch_next(batch);
-}
-
-static int nft_mnl_batch_talk(struct mnl_socket *nl, struct mnl_nlmsg_batch *b)
-{
- int ret, fd = mnl_socket_get_fd(nl);
- char rcv_buf[MNL_SOCKET_BUFFER_SIZE];
- fd_set readfds;
- struct timeval tv = {
- .tv_sec = 0,
- .tv_usec = 0
- };
-
- ret = mnl_socket_sendto(nl, mnl_nlmsg_batch_head(b),
- mnl_nlmsg_batch_size(b));
- if (ret == -1)
- goto err;
-
- FD_ZERO(&readfds);
- FD_SET(fd, &readfds);
-
- /* receive and digest all the acknowledgments from the kernel. */
- ret = select(fd+1, &readfds, NULL, NULL, &tv);
- if (ret == -1)
- goto err;
-
- while (ret > 0 && FD_ISSET(fd, &readfds)) {
- ret = mnl_socket_recvfrom(nl, rcv_buf, sizeof(rcv_buf));
- if (ret == -1)
- goto err;
-
- ret = mnl_cb_run(rcv_buf, ret, 0, mnl_socket_get_portid(nl),
- NULL, NULL);
- if (ret < 0)
- goto err;
-
- ret = select(fd+1, &readfds, NULL, NULL, &tv);
- if (ret == -1)
- goto err;
-
- FD_ZERO(&readfds);
- FD_SET(fd, &readfds);
- }
-err:
- return ret;
-}
-
int main(int argc, char *argv[])
{
struct mnl_socket *nl;
struct nft_rule *r;
- struct nlmsghdr *nlh;
- struct mnl_nlmsg_batch *batch;
+ struct mnl_nlmsg_batch *batch = nft_mnlio_batch_alloc();
uint8_t family;
- char buf[4096];
if (argc != 4) {
fprintf(stderr, "Usage: %s <family> <table> <chain>\n", argv[0]);
@@ -211,28 +146,20 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
- batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
-
- nft_mnl_batch_put(batch, NFNL_MSG_BATCH_BEGIN);
+ nft_mnlio_batch_begin(batch);
- nlh = nft_rule_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
- NFT_MSG_NEWRULE,
- nft_rule_attr_get_u32(r, NFT_RULE_ATTR_FAMILY),
- NLM_F_APPEND|NLM_F_CREATE, seq);
-
- nft_rule_nlmsg_build_payload(nlh, r);
+ nft_mnlio_rule_add(r, NLM_F_APPEND, batch);
nft_rule_free(r);
- mnl_nlmsg_batch_next(batch);
- nft_mnl_batch_put(batch, NFNL_MSG_BATCH_END);
+ mnl_nlmsg_batch_next(batch);
+ nft_mnlio_batch_end(batch);
- if (nft_mnl_batch_talk(nl, batch) < 0) {
+ if (nft_mnlio_batch_talk(nl, batch) < 0) {
perror("Netlink problem");
exit(EXIT_FAILURE);
}
mnl_nlmsg_batch_stop(batch);
-
mnl_socket_close(nl);
return EXIT_SUCCESS;
next prev parent reply other threads:[~2014-02-05 19:17 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-02-05 19:17 [RFC libnftnl PATCH 0/2] New mnlio functions Arturo Borrero Gonzalez
2014-02-05 19:17 ` [RFC libnftnl PATCH 1/2] src: add mnlio API functions Arturo Borrero Gonzalez
2014-02-12 14:03 ` Pablo Neira Ayuso
2014-02-05 19:17 ` Arturo Borrero Gonzalez [this message]
2014-02-05 19:24 ` [RFC libnftnl PATCH 0/2] New mnlio functions Patrick McHardy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20140205191752.9979.97410.stgit@nfdev.cica.es \
--to=arturo.borrero.glez@gmail.com \
--cc=netfilter-devel@vger.kernel.org \
--cc=pablo@netfilter.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.