summaryrefslogtreecommitdiffstats
path: root/src/jansson.c
diff options
context:
space:
mode:
authorÁlvaro Neira Ayuso <alvaroneay@gmail.com>2013-08-20 17:58:46 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2013-08-20 21:01:09 +0200
commitc83c53a26de25c1e2900f7841647cec3b57c7438 (patch)
tree4c0291b9a495e44911ba104650e2b04b4d4eb776 /src/jansson.c
parent978988a0f4730f1da2cfd7e3fa0670711bb6b802 (diff)
rule: Add json parser support
Add function for parsing rules in JSON format Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/jansson.c')
-rw-r--r--src/jansson.c91
1 files changed, 91 insertions, 0 deletions
diff --git a/src/jansson.c b/src/jansson.c
index ca29863..f0ef04c 100644
--- a/src/jansson.c
+++ b/src/jansson.c
@@ -15,6 +15,10 @@
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
+#include "expr_ops.h"
+
+#include <libnftables/expr.h>
+#include <linux/netfilter/nf_tables.h>
#ifdef JSON_PARSING
@@ -121,4 +125,91 @@ int nft_jansson_parse_family(json_t *root, void *out)
memcpy(out, &family, sizeof(family));
return 0;
}
+
+int nft_jansson_value_parse_reg(json_t *root, const char *tag, int type,
+ void *out)
+{
+ if (nft_jansson_value_parse_val(root, tag, type, out) != 0)
+ return -1;
+
+ if (*((uint32_t *)out) > NFT_REG_MAX){
+ errno = ERANGE;
+ return -1;
+ }
+
+ return 0;
+}
+
+int nft_jansson_str2num(json_t *root, const char *tag, int base,
+ void *out, enum nft_type type)
+{
+ const char *str;
+
+ str = nft_jansson_value_parse_str(root, tag);
+ if (str == NULL)
+ return -1;
+
+ return nft_strtoi(str, base, out, type);
+}
+
+struct nft_rule_expr *nft_jansson_expr_parse(json_t *root)
+{
+ struct nft_rule_expr *e;
+ const char *type;
+ int ret;
+
+ type = nft_jansson_value_parse_str(root, "type");
+ if (type == NULL)
+ return NULL;
+
+ e = nft_rule_expr_alloc(type);
+ if (e == NULL)
+ return NULL;;
+
+ ret = e->ops->json_parse(e, root);
+
+ return ret < 0 ? NULL : e;
+}
+
+int nft_jansson_data_reg_parse(json_t *root, const char *tag,
+ union nft_data_reg *data_reg)
+{
+ json_t *data;
+ const char *type;
+ int ret;
+
+ data = json_object_get(root, tag);
+ if (data == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ data = json_object_get(data, "data_reg");
+ if (data == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ ret = nft_data_reg_json_parse(data_reg, data);
+
+ if (ret < 0) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ type = nft_jansson_value_parse_str(data, "type");
+ if (type == NULL)
+ return -1;
+
+ if (strcmp(type, "value") == 0)
+ return DATA_VALUE;
+ else if (strcmp(type, "verdict") == 0)
+ return DATA_VERDICT;
+ else if (strcmp(type, "chain") == 0)
+ return DATA_CHAIN;
+ else {
+ errno = EINVAL;
+ return -1;
+ }
+}
#endif