From: Alvaro Neira <alvaroneay@gmail.com>
To: netfilter-devel@vger.kernel.org
Cc: eric@regit.org
Subject: [libnftables PATCH 4/6] chain: json: add function for parsing chain
Date: Thu, 25 Jul 2013 22:52:39 +0200 [thread overview]
Message-ID: <20130725205239.26223.9493.stgit@Ph0enix> (raw)
In-Reply-To: <20130725205215.26223.77001.stgit@Ph0enix>
From: Álvaro Neira Ayuso <alvaroneay@gmail.com>
Add function for parsing chains in format JSON
Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
---
include/libnftables/chain.h | 1
src/chain.c | 138 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 139 insertions(+)
diff --git a/include/libnftables/chain.h b/include/libnftables/chain.h
index 382947f..53fd407 100644
--- a/include/libnftables/chain.h
+++ b/include/libnftables/chain.h
@@ -52,6 +52,7 @@ enum {
enum nft_chain_parse_type {
NFT_CHAIN_PARSE_NONE = 0,
NFT_CHAIN_PARSE_XML,
+ NFT_CHAIN_PARSE_JSON,
NFT_CHAIN_PARSE_MAX
};
diff --git a/src/chain.c b/src/chain.c
index 1e07044..d9d41ee 100644
--- a/src/chain.c
+++ b/src/chain.c
@@ -468,6 +468,141 @@ int nft_chain_nlmsg_parse(const struct nlmsghdr *nlh, struct nft_chain *c)
}
EXPORT_SYMBOL(nft_chain_nlmsg_parse);
+static int str2policy(const char *policy)
+{
+ if (strcmp("accept", policy) == 0) {
+ return NF_ACCEPT;
+ } else if (strcmp("drop", policy) == 0) {
+ return NF_DROP;
+ } else {
+ return -1;
+ }
+}
+
+static int nft_chain_json_parse(struct nft_chain *c, char *json)
+{
+#ifdef JSON_PARSING
+ json_t *root;
+ json_error_t error;
+ uint64_t val64;
+ uint32_t hooknum;
+ int32_t prio;
+ const char *valstr;
+
+ /* Load the tree */
+ root = json_loadb (json, strlen(json), 0, &error);
+ if (!root) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ root = json_object_get(root, "chain");
+ if (root == NULL) {
+ errno = ERANGE;
+ return -1;
+ }
+
+ if (nft_jansson_value_parse_val(root, "version", NFT_TYPE_U64,
+ &val64) == -1)
+ goto err;
+
+ if (val64 != NFT_CHAIN_JSON_VERSION)
+ goto err;
+
+ valstr = nft_jansson_value_parse_str(root, "name");
+ if (valstr == NULL)
+ goto err;
+
+ nft_chain_attr_set_str(c, NFT_CHAIN_ATTR_NAME, strdup(valstr));
+
+ if (nft_jansson_value_parse_val(root, "handle", NFT_TYPE_U64,
+ &val64) == -1)
+ goto err;
+
+ nft_chain_attr_set_u64(c,NFT_CHAIN_ATTR_HANDLE, val64);
+
+ if (nft_jansson_value_parse_val(root, "bytes", NFT_TYPE_U64,
+ &val64) == -1)
+ goto err;
+
+ nft_chain_attr_set_u64(c, NFT_CHAIN_ATTR_BYTES, val64);
+
+ if (nft_jansson_value_parse_val(root, "packets", NFT_TYPE_U64,
+ &val64) == -1)
+ goto err;
+
+ if (val64 < 0)
+ goto err;
+
+ nft_chain_attr_set_u64(c, NFT_CHAIN_ATTR_PACKETS, val64);
+
+ root = json_object_get(root, "properties");
+
+ valstr = nft_jansson_value_parse_str(root, "family");
+
+ if (valstr == NULL)
+ goto err;
+
+ if (nft_str2family(valstr) == -1)
+ goto err;
+
+ nft_chain_attr_set_u32(c, NFT_CHAIN_ATTR_FAMILY, nft_str2family(valstr));
+
+ valstr = nft_jansson_value_parse_str(root, "table");
+
+ if (valstr == NULL)
+ goto err;
+
+ nft_chain_attr_set_str(c, NFT_CHAIN_ATTR_TABLE, strdup(valstr));
+
+ if (nft_jansson_node_exist(root, "hooknum")) {
+ valstr = nft_jansson_value_parse_str(root, "type");
+
+ if (valstr == NULL)
+ goto err;
+
+ nft_chain_attr_set_str(c, NFT_CHAIN_ATTR_TYPE, strdup(valstr));
+
+ if (nft_jansson_value_parse_val(root, "prio", NFT_TYPE_S32,
+ &prio) == -1)
+ goto err;
+
+ nft_chain_attr_set_s32(c, NFT_CHAIN_ATTR_PRIO, prio);
+
+ valstr = nft_jansson_value_parse_str(root, "hooknum");
+ for (hooknum = 0; hooknum < NF_INET_NUMHOOKS; hooknum++) {
+ if (strcmp(valstr, hooknum2str_array[hooknum]) == 0) {
+ nft_chain_attr_set_u32(c, NFT_CHAIN_ATTR_HOOKNUM,
+ hooknum);
+ break;
+ }
+ }
+
+ valstr = nft_jansson_value_parse_str(root, "policy");
+
+ if (valstr == NULL)
+ goto err;
+
+ if (str2policy(valstr) == -1)
+ goto err;
+
+ nft_chain_attr_set_u32(c, NFT_CHAIN_ATTR_POLICY,
+ str2policy(valstr));
+ }
+
+ free(root);
+ return 0;
+
+err:
+ free(root);
+ errno = ERANGE;
+ return -1;
+#else
+ errno = EOPNOTSUPP;
+ return -1;
+#endif
+}
+
static int nft_chain_xml_parse(struct nft_chain *c, char *xml)
{
#ifdef XML_PARSING
@@ -661,6 +796,9 @@ int nft_chain_parse(struct nft_chain *c, enum nft_chain_parse_type type,
case NFT_CHAIN_PARSE_XML:
ret = nft_chain_xml_parse(c, data);
break;
+ case NFT_CHAIN_PARSE_JSON:
+ ret = nft_chain_json_parse(c, data);
+ break;
default:
ret = -1;
errno = EOPNOTSUPP;
--
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:[~2013-07-25 20:52 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-07-25 20:52 [libnftables PATCH 1/6] table: json: Add Json parser support Alvaro Neira
2013-07-25 20:52 ` [libnftables PATCH 2/6] table : tests: test the table json " Alvaro Neira
2013-07-25 21:07 ` Pablo Neira Ayuso
2013-07-25 20:52 ` [libnftables PATCH 3/6] examples: Add nft-table-json-add Alvaro Neira
2013-07-25 21:07 ` Pablo Neira Ayuso
2013-07-25 20:52 ` Alvaro Neira [this message]
2013-07-25 21:13 ` [libnftables PATCH 4/6] chain: json: add function for parsing chain Pablo Neira Ayuso
2013-07-25 20:52 ` [libnftables PATCH 5/6] chain: test: test the chain parser support Alvaro Neira
2013-07-25 20:52 ` [libnftables PATCH 6/6] examples: Add nft-chain-json-add Alvaro Neira
2013-07-25 21:07 ` [libnftables PATCH 1/6] table: json: Add Json parser support 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=20130725205239.26223.9493.stgit@Ph0enix \
--to=alvaroneay@gmail.com \
--cc=eric@regit.org \
--cc=netfilter-devel@vger.kernel.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).