From: "Carlos Falgueras García" <carlosfg@riseup.net>
To: netfilter-devel@vger.kernel.org
Cc: pablo@netfilter.org, kaber@trash.net
Subject: [PATCH 4/4] libnftnl: examples: Modify the example to allow add TLV objects as user data.
Date: Sun, 3 Jan 2016 20:38:20 +0100 [thread overview]
Message-ID: <1451849900-18077-4-git-send-email-carlosfg@riseup.net> (raw)
In-Reply-To: <1451849900-18077-1-git-send-email-carlosfg@riseup.net>
Example usage:
> nft add table mytable
> exmample/nft-set-add ip mytable myset "5:my data 0" "5:my data 1"
> example/nft-set-get ip json | jq #jq: https://stedolan.github.io/jq/
{
"set": {
"name": "myset",
"table": "mytable",
"flags": 2,
"family": "ip",
"key_type": 0,
"key_len": 2,
"userdata_len": 48,
"userdata": [
{
"type": 5,
"len": 9,
"val": "my data 0"
},
{
"type": 5,
"len": 9,
"val": "my data 1"
}
]
}
}
Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
---
examples/nft-set-add.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 49 insertions(+), 4 deletions(-)
diff --git a/examples/nft-set-add.c b/examples/nft-set-add.c
index e040aca..42ff3c8 100644
--- a/examples/nft-set-add.c
+++ b/examples/nft-set-add.c
@@ -28,8 +28,14 @@
#include <libmnl/libmnl.h>
#include <libnftnl/set.h>
+#define ATTRBUF_SIZE 256
+
+static bool parse_user_data(char *argv[], int argc,
+ struct nftnl_attrbuf *attrbuf);
+
static struct nftnl_set *setup_set(uint8_t family, const char *table,
- const char *name)
+ const char *name, const char *udata,
+ unsigned int udlen)
{
struct nftnl_set *s = NULL;
@@ -44,6 +50,8 @@ static struct nftnl_set *setup_set(uint8_t family, const char *table,
nftnl_set_set_u32(s, NFTNL_SET_FAMILY, family);
nftnl_set_set_u32(s, NFTNL_SET_KEY_LEN, 2);
nftnl_set_set_u32(s, NFTNL_SET_ID, 1);
+ if (udata != NULL)
+ nftnl_set_set_data(s, NFTNL_SET_USERDATA, udata, udlen);
nftnl_set_set_u32(s, NFTNL_SET_FLAGS, NFT_SET_CONSTANT);
return s;
@@ -71,13 +79,17 @@ int main(int argc, char *argv[])
struct nftnl_set *s;
struct nlmsghdr *nlh;
struct mnl_nlmsg_batch *batch;
+ struct nftnl_attrbuf *attrbuf = NULL;
uint8_t family;
char buf[MNL_SOCKET_BUFFER_SIZE];
uint32_t seq = time(NULL);
int ret;
- if (argc != 4) {
- fprintf(stderr, "Usage: %s <family> <table> <setname>\n", argv[0]);
+ if (argc < 4) {
+ fprintf(stderr,
+ "Usage: %s <family> <table> <setname> [<type:user_data>[ <type:user_data>...]]\n",
+ argv[0]
+ );
exit(EXIT_FAILURE);
}
@@ -94,7 +106,21 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
- s = setup_set(family, argv[2], argv[3]);
+ if (argc >= 5) {
+ attrbuf = nftnl_attrbuf_alloc(ATTRBUF_SIZE);
+ if (!parse_user_data(argv, argc, attrbuf)) {
+ fprintf(stderr, "Error parsing user data\n");
+ free(attrbuf);
+ return EXIT_FAILURE;
+ }
+
+ s = setup_set(family, argv[2], argv[3],
+ (char *)nftnl_attrbuf_data(attrbuf),
+ nftnl_attrbuf_len(attrbuf)
+ );
+ } else {
+ s = setup_set(family, argv[2], argv[3], NULL, 0);
+ }
nl = mnl_socket_open(NETLINK_NETFILTER);
if (nl == NULL) {
@@ -147,6 +173,25 @@ int main(int argc, char *argv[])
}
mnl_socket_close(nl);
+ if (attrbuf)
+ free(attrbuf);
return EXIT_SUCCESS;
}
+
+static bool parse_user_data(char *argv[], int argc,
+ struct nftnl_attrbuf *attrbuf)
+{
+ int i;
+ char *type, *value;
+
+ for (i = 4; i < argc; i++) {
+ type = strchr(argv[i], ':') - 1;
+ value = type + 2;
+ if (!nftnl_attr_put_check(attrbuf, ATTRBUF_SIZE, atoi(type),
+ strlen(value), value))
+ return false;
+ }
+
+ return true;
+}
--
2.6.4
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
next prev parent reply other threads:[~2016-01-03 19:38 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-01-03 19:38 [PATCH 1/4] nf: netfilter: nf_tables_api: Add new attributes into nft_set to store user data Carlos Falgueras García
2016-01-03 19:38 ` [PATCH 2/4] libnftnl: set: Add new attribute into 'set' to store an arbitrary length " Carlos Falgueras García
2016-01-03 19:38 ` [PATCH 3/4] libnftnl: set: Implement new buffer of TLV objects Carlos Falgueras García
2016-01-04 12:06 ` Patrick McHardy
2016-01-03 19:38 ` Carlos Falgueras García [this message]
2016-01-04 12:32 ` [PATCH 1/4] nf: netfilter: nf_tables_api: Add new attributes into nft_set to store user data Patrick McHardy
2016-01-05 10:34 ` Pablo Neira Ayuso
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=1451849900-18077-4-git-send-email-carlosfg@riseup.net \
--to=carlosfg@riseup.net \
--cc=kaber@trash.net \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).