From c83c53a26de25c1e2900f7841647cec3b57c7438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Neira=20Ayuso?= Date: Tue, 20 Aug 2013 17:58:46 +0200 Subject: rule: Add json parser support Add function for parsing rules in JSON format Signed-off-by: Alvaro Neira Ayuso Signed-off-by: Pablo Neira Ayuso --- src/jansson.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) (limited to 'src/jansson.c') 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 #include #include +#include "expr_ops.h" + +#include +#include #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 -- cgit v1.2.3