From a205a5022259f40aa3ad74017d39e64546d4155c Mon Sep 17 00:00:00 2001 From: Arturo Borrero Date: Sat, 18 Jan 2014 20:01:32 +0100 Subject: ct: use a string with 'dir' attribute This patch implements a string to represent directions in the CT expression: * original (0) * reply (1) Signed-off-by: Arturo Borrero Gonzalez Signed-off-by: Pablo Neira Ayuso --- src/expr/ct.c | 59 +++++++++++++++++++++++++++++++++-------- tests/xmlfiles/24-rule-ct.xml | 2 +- tests/xmlfiles/37-rule-real.xml | 2 +- tests/xmlfiles/39-rule-real.xml | 2 +- tests/xmlfiles/50-rule-real.xml | 2 +- tests/xmlfiles/51-rule-real.xml | 2 +- tests/xmlfiles/52-rule-real.xml | 2 +- tests/xmlfiles/53-rule-real.xml | 2 +- tests/xmlfiles/54-rule-real.xml | 2 +- tests/xmlfiles/55-rule-real.xml | 2 +- tests/xmlfiles/56-rule-real.xml | 2 +- tests/xmlfiles/57-rule-real.xml | 2 +- 12 files changed, 59 insertions(+), 22 deletions(-) diff --git a/src/expr/ct.c b/src/expr/ct.c index 3070795..a09976c 100644 --- a/src/expr/ct.c +++ b/src/expr/ct.c @@ -179,11 +179,38 @@ static inline int str2ctkey(const char *ctkey) return -1; } +static const char *ctdir2str(uint8_t ctdir) +{ + switch (ctdir) { + case IP_CT_DIR_ORIGINAL: + return "original"; + case IP_CT_DIR_REPLY: + return "reply"; + default: + return "unknow"; + } +} + +static int str2ctdir(const char *str, uint8_t *ctdir) +{ + if (strcmp(str, "original") == 0) { + *ctdir = IP_CT_DIR_ORIGINAL; + return 0; + } + + if (strcmp(str, "reply") == 0) { + *ctdir = IP_CT_DIR_REPLY; + return 0; + } + + return -1; +} + static int nft_rule_expr_ct_json_parse(struct nft_rule_expr *e, json_t *root, struct nft_parse_err *err) { #ifdef JSON_PARSING - const char *key_str; + const char *key_str, *dir_str; uint32_t reg; uint8_t dir; int key; @@ -207,12 +234,15 @@ static int nft_rule_expr_ct_json_parse(struct nft_rule_expr *e, json_t *root, } if (nft_jansson_node_exist(root, "dir")) { - if (nft_jansson_parse_val(root, "dir", NFT_TYPE_U8, &dir, - err) < 0) + dir_str = nft_jansson_parse_str(root, "dir", err); + if (dir_str == NULL) return -1; - if (dir != IP_CT_DIR_ORIGINAL && dir != IP_CT_DIR_REPLY) + if (str2ctdir(dir_str, &dir) != 0) { + err->node_name = "dir"; + err->error = NFT_PARSE_EBADTYPE; goto err; + } nft_rule_expr_set_u8(e, NFT_EXPR_CT_DIR, dir); } @@ -233,7 +263,7 @@ static int nft_rule_expr_ct_xml_parse(struct nft_rule_expr *e, mxml_node_t *tree { #ifdef XML_PARSING struct nft_expr_ct *ct = nft_expr_data(e); - const char *key_str; + const char *key_str, *dir_str; int key; uint8_t dir; uint32_t reg; @@ -257,10 +287,14 @@ static int nft_rule_expr_ct_xml_parse(struct nft_rule_expr *e, mxml_node_t *tree ct->key = key; e->flags |= (1 << NFT_EXPR_CT_KEY); - if (nft_mxml_num_parse(tree, "dir", MXML_DESCEND_FIRST, BASE_DEC, - &dir, NFT_TYPE_U8, NFT_XML_OPT, err) == 0) { - if (dir != IP_CT_DIR_ORIGINAL && dir != IP_CT_DIR_REPLY) + dir_str = nft_mxml_str_parse(tree, "dir", MXML_DESCEND_FIRST, + NFT_XML_OPT, err); + if (dir_str != NULL) { + if (str2ctdir(dir_str, &dir) != 0) { + err->node_name = "dir"; + err->error = NFT_PARSE_EBADTYPE; goto err; + } nft_rule_expr_set_u8(e, NFT_EXPR_CT_DIR, dir); } @@ -291,7 +325,8 @@ nft_expr_ct_snprintf_json(char *buf, size_t size, struct nft_rule_expr *e) } if (nft_rule_expr_is_set(e, NFT_EXPR_CT_DIR)) { - ret = snprintf(buf+offset, len, ",\"dir\":%u", ct->dir); + ret = snprintf(buf+offset, len, ",\"dir\":\"%s\"", + ctdir2str(ct->dir)); SNPRINTF_BUFFER_SIZE(ret, size, len, offset); } @@ -312,7 +347,8 @@ nft_expr_ct_snprintf_xml(char *buf, size_t size, struct nft_rule_expr *e) SNPRINTF_BUFFER_SIZE(ret, size, len, offset); if (nft_rule_expr_is_set(e, NFT_EXPR_CT_DIR)) { - ret = snprintf(buf+offset, len, "%u", ct->dir); + ret = snprintf(buf+offset, len, "%s", + ctdir2str(ct->dir)); SNPRINTF_BUFFER_SIZE(ret, size, len, offset); } @@ -330,7 +366,8 @@ nft_expr_ct_snprintf_default(char *buf, size_t size, struct nft_rule_expr *e) SNPRINTF_BUFFER_SIZE(ret, size, len, offset); if (nft_rule_expr_is_set(e, NFT_EXPR_CT_DIR)) { - ret = snprintf(buf+offset, len, "dir %u ", ct->dir); + ret = snprintf(buf+offset, len, ", dir %s ", + ctdir2str(ct->dir)); SNPRINTF_BUFFER_SIZE(ret, size, len, offset); } diff --git a/tests/xmlfiles/24-rule-ct.xml b/tests/xmlfiles/24-rule-ct.xml index b3f4ad8..1939e43 100644 --- a/tests/xmlfiles/24-rule-ct.xml +++ b/tests/xmlfiles/24-rule-ct.xml @@ -1 +1 @@ -ipfilter
INPUT1001state0
+ipfilter
INPUT1001stateoriginal
diff --git a/tests/xmlfiles/37-rule-real.xml b/tests/xmlfiles/37-rule-real.xml index 89b06b8..d9293f6 100644 --- a/tests/xmlfiles/37-rule-real.xml +++ b/tests/xmlfiles/37-rule-real.xml @@ -1 +1 @@ -ipfilter
INPUT251iifname1eq160x000000000x000000000x650000000x00306874191network1eq10x00000006122transport1eq20x000016001state011440x0000000a40x000000001neq40x0000000000testprefix100
+ipfilter
INPUT251iifname1eq160x000000000x000000000x650000000x00306874191network1eq10x00000006122transport1eq20x000016001stateoriginal11440x0000000a40x000000001neq40x0000000000testprefix100
diff --git a/tests/xmlfiles/39-rule-real.xml b/tests/xmlfiles/39-rule-real.xml index a307a2e..9a69a61 100644 --- a/tests/xmlfiles/39-rule-real.xml +++ b/tests/xmlfiles/39-rule-real.xml @@ -1 +1 @@ -ip6filter
test311iifname1eq160x000000000x000000000x6f6200000x0030646e1oifname1eq160x000000000x620000000x31646e6f0x0037322e1816network1eq160xc09a002a0x2700cac10x000000000x50010000161network1eq10x00000011122transport1eq20x000035001status01eq40x0000000100dns_drop2000drop
+ip6filter
test311iifname1eq160x000000000x000000000x6f6200000x0030646e1oifname1eq160x000000000x620000000x31646e6f0x0037322e1816network1eq160xc09a002a0x2700cac10x000000000x50010000161network1eq10x00000011122transport1eq20x000035001statusoriginal1eq40x0000000100dns_drop2000drop
diff --git a/tests/xmlfiles/50-rule-real.xml b/tests/xmlfiles/50-rule-real.xml index d15eff4..c52e00e 100644 --- a/tests/xmlfiles/50-rule-real.xml +++ b/tests/xmlfiles/50-rule-real.xml @@ -1 +1 @@ -ipfilter
output121state011440x0000000a40x000000001neq40x000000005511407
+ipfilter
output121stateoriginal11440x0000000a40x000000001neq40x000000005511407
diff --git a/tests/xmlfiles/51-rule-real.xml b/tests/xmlfiles/51-rule-real.xml index 471cd2b..93d6632 100644 --- a/tests/xmlfiles/51-rule-real.xml +++ b/tests/xmlfiles/51-rule-real.xml @@ -1 +1 @@ -ipfilter
output131direction01eq10x000000005160
+ipfilter
output131directionoriginal1eq10x000000005160
diff --git a/tests/xmlfiles/52-rule-real.xml b/tests/xmlfiles/52-rule-real.xml index 61a1269..c6ca9ec 100644 --- a/tests/xmlfiles/52-rule-real.xml +++ b/tests/xmlfiles/52-rule-real.xml @@ -1 +1 @@ -ipfilter
output141direction01eq10x000000015011247
+ipfilter
output141directionoriginal1eq10x000000015011247
diff --git a/tests/xmlfiles/53-rule-real.xml b/tests/xmlfiles/53-rule-real.xml index d835639..8cb0783 100644 --- a/tests/xmlfiles/53-rule-real.xml +++ b/tests/xmlfiles/53-rule-real.xml @@ -1 +1 @@ -ipfilter
output151status01eq40x0000000100
+ipfilter
output151statusoriginal1eq40x0000000100
diff --git a/tests/xmlfiles/54-rule-real.xml b/tests/xmlfiles/54-rule-real.xml index ed27e56..0c0dc9a 100644 --- a/tests/xmlfiles/54-rule-real.xml +++ b/tests/xmlfiles/54-rule-real.xml @@ -1 +1 @@ -ipfilter
output161mark01eq40x0000006400
+ipfilter
output161markoriginal1eq40x0000006400
diff --git a/tests/xmlfiles/55-rule-real.xml b/tests/xmlfiles/55-rule-real.xml index 2d2bf7f..fca8c99 100644 --- a/tests/xmlfiles/55-rule-real.xml +++ b/tests/xmlfiles/55-rule-real.xml @@ -1 +1 @@ -ipfilter
output171secmark01eq40x000000005511407
+ipfilter
output171secmarkoriginal1eq40x000000005511407
diff --git a/tests/xmlfiles/56-rule-real.xml b/tests/xmlfiles/56-rule-real.xml index 4596689..1ea7f0c 100644 --- a/tests/xmlfiles/56-rule-real.xml +++ b/tests/xmlfiles/56-rule-real.xml @@ -1 +1 @@ -ipfilter
output181expiration01eq40x0000001e00
+ipfilter
output181expirationoriginal1eq40x0000001e00
diff --git a/tests/xmlfiles/57-rule-real.xml b/tests/xmlfiles/57-rule-real.xml index 6a2ad52..6036973 100644 --- a/tests/xmlfiles/57-rule-real.xml +++ b/tests/xmlfiles/57-rule-real.xml @@ -1 +1 @@ -ipfilter
output191helper01eq40x0070746600
+ipfilter
output191helperoriginal1eq40x0070746600
-- cgit v1.2.3