From 845952009ee304cc523bb428eef56c5bbbd2c786 Mon Sep 17 00:00:00 2001 From: Arturo Borrero Gonzalez Date: Wed, 26 Jun 2013 13:37:15 +0200 Subject: payload: xml: use string for base attribute This patch implements using a string instead of a number for the node. Signed-off-by: Arturo Borrero Gonzalez Signed-off-by: Pablo Neira Ayuso --- src/expr/payload.c | 55 ++++++++++++++++++++++++++++++++++++++++-------- test/nft-rule-xml-add.sh | 6 +++--- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/src/expr/payload.c b/src/expr/payload.c index dc42918..ae72fa2 100644 --- a/src/expr/payload.c +++ b/src/expr/payload.c @@ -212,13 +212,19 @@ nft_rule_expr_payload_xml_parse(struct nft_rule_expr *e, char *xml) /* Get and set . Not mandatory */ node = mxmlFindElement(tree, tree, "base", NULL, NULL, MXML_DESCEND); if (node != NULL) { - tmp = strtoull(node->child->value.opaque, &endptr, 10); - if (tmp > UINT32_MAX || tmp < 0 || *endptr) { + + if (strcmp(node->child->value.opaque, "link") == 0) { + payload->base = NFT_PAYLOAD_LL_HEADER; + } else if (strcmp(node->child->value.opaque, "network") == 0) { + payload->base = NFT_PAYLOAD_NETWORK_HEADER; + } else if (strcmp(node->child->value.opaque, + "transport") == 0) { + payload->base = NFT_PAYLOAD_TRANSPORT_HEADER; + } else { mxmlDelete(tree); return -1; } - payload->base = (uint32_t)tmp; e->flags |= (1 << NFT_EXPR_PAYLOAD_BASE); } @@ -256,6 +262,41 @@ nft_rule_expr_payload_xml_parse(struct nft_rule_expr *e, char *xml) #endif } +static int +nft_rule_expr_payload_snprintf_xml(char *buf, size_t len, uint32_t flags, + struct nft_expr_payload *p) +{ + int size = len, offset = 0, ret; + + ret = snprintf(buf, len, "%u%u" + "%u", p->dreg, p->offset, p->len); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + + /* A default option is not provided. + * The node will be missing; Is not mandatory. + */ + + switch (p->base) { + case NFT_PAYLOAD_LL_HEADER: + ret = snprintf(buf+offset, len, "link"); + break; + case NFT_PAYLOAD_NETWORK_HEADER: + ret = snprintf(buf+offset, len, "network"); + break; + case NFT_PAYLOAD_TRANSPORT_HEADER: + ret = snprintf(buf+offset, len, "transport"); + break; + default: + ret = snprintf(buf+offset, len, "unknown"); + break; + } + + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + + return offset; +} + + static int nft_rule_expr_payload_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, struct nft_rule_expr *e) @@ -264,12 +305,8 @@ nft_rule_expr_payload_snprintf(char *buf, size_t len, uint32_t type, switch(type) { case NFT_RULE_O_XML: - return snprintf(buf, len, "%u" - "%u%u" - "%u", - payload->dreg, payload->base, - payload->offset, payload->len); - + return nft_rule_expr_payload_snprintf_xml(buf, len, flags, + payload); case NFT_RULE_O_DEFAULT: return snprintf(buf, len, "dreg=%u base=%u offset=%u len=%u ", payload->dreg, payload->base, diff --git a/test/nft-rule-xml-add.sh b/test/nft-rule-xml-add.sh index 322e70c..e1e35d9 100755 --- a/test/nft-rule-xml-add.sh +++ b/test/nft-rule-xml-add.sh @@ -53,7 +53,7 @@ XML=" 1 - 1 + transport 12 4 @@ -69,7 +69,7 @@ XML=" 1 - 1 + link 16 4 @@ -85,7 +85,7 @@ XML=" 1 - 1 + network 9 1 -- cgit v1.2.3