From 3c4559021754012a3155f092ac9787e33db50c38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Neira=20Ayuso?= Date: Fri, 9 Aug 2013 13:13:31 +0200 Subject: expr: bytecode: add nft_str2ntoh function Add function that will be use in the JSON parser. Signed-off-by: Alvaro Neira Ayuso Ayuso Signed-off-by: Pablo Neira Ayuso --- src/expr/byteorder.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/expr/byteorder.c b/src/expr/byteorder.c index 79ba439..e2d442c 100644 --- a/src/expr/byteorder.c +++ b/src/expr/byteorder.c @@ -181,13 +181,25 @@ static char *expr_byteorder_str[] = { [NFT_BYTEORDER_NTOH] = "ntoh", }; +static inline int nft_str2ntoh(const char *op) +{ + if (strcmp(op, "ntoh") == 0) + return NFT_BYTEORDER_NTOH; + else if (strcmp(op, "hton") == 0) + return NFT_BYTEORDER_HTON; + else { + errno = EINVAL; + return -1; + } +} + static int nft_rule_expr_byteorder_xml_parse(struct nft_rule_expr *e, mxml_node_t *tree) { #ifdef XML_PARSING struct nft_expr_byteorder *byteorder = nft_expr_data(e); const char *op; - int32_t reg; + int32_t reg, ntoh; reg = nft_mxml_reg_parse(tree, "sreg", MXML_DESCEND_FIRST); if (reg < 0) @@ -207,15 +219,11 @@ nft_rule_expr_byteorder_xml_parse(struct nft_rule_expr *e, mxml_node_t *tree) if (op == NULL) return -1; - if (strcmp(op, "ntoh") == 0) - byteorder->op = NFT_BYTEORDER_NTOH; - else if (strcmp(op, "hton") == 0) - byteorder->op = NFT_BYTEORDER_HTON; - else { - errno = EINVAL; + ntoh = nft_str2ntoh(op); + if (ntoh < 0) return -1; - } + byteorder->op = ntoh; e->flags |= (1 << NFT_EXPR_BYTEORDER_OP); if (nft_mxml_num_parse(tree, "len", MXML_DESCEND_FIRST, BASE_DEC, -- cgit v1.2.3