From patchwork Fri Aug 9 11:13:59 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alvaro Neira X-Patchwork-Id: 266000 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 500802C00A3 for ; Fri, 9 Aug 2013 21:14:12 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S967624Ab3HILOL (ORCPT ); Fri, 9 Aug 2013 07:14:11 -0400 Received: from mail-we0-f178.google.com ([74.125.82.178]:43160 "EHLO mail-we0-f178.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S967573Ab3HILOK (ORCPT ); Fri, 9 Aug 2013 07:14:10 -0400 Received: by mail-we0-f178.google.com with SMTP id u57so3469443wes.9 for ; Fri, 09 Aug 2013 04:14:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:from:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-type:content-transfer-encoding; bh=norfSp/YIuCuBz1XmUOYzqyGfz3y7NpwO70u3rVMh9w=; b=Zu0DRD6/OfTel6HcJCN03hPnLTuTZHTia7FowkJySA8QRDDZ4qrLnxZxEL7SXgp1HR h+jStqbXZQwXAszNq1cyX9BHGO6MqYVbOhem2uLVWFT31YEn5ib7Zaq794GjfGZO5yiI i88l+DKIj749qJVMBl+m7URCHftYosaOwMYN/fZca7c1UEtzqwttTl5satGCmjffMZZ7 hTbe27Od+F33FknQX+yzy7iQW2Cu1juoW0ONVXaoLKZXFnECdqZykQnjKfNBEgnNmYvK UuG93RCMGWSqEdbDWuXyM1yPysNY3UzggRzNReXxuaPBXA/oC0XVnW9kI8q94VERI4aV wvoQ== X-Received: by 10.180.12.243 with SMTP id b19mr63332wic.18.1376046849242; Fri, 09 Aug 2013 04:14:09 -0700 (PDT) Received: from [127.0.1.1] ([90.174.0.186]) by mx.google.com with ESMTPSA id a8sm2189553wie.6.2013.08.09.04.14.03 for (version=TLSv1.2 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 09 Aug 2013 04:14:08 -0700 (PDT) Subject: [libnftables PATCH 10/13] expr: nat: add nft_str2nat function To: netfilter-devel@vger.kernel.org From: Alvaro Neira Cc: eric@regit.org Date: Fri, 09 Aug 2013 13:13:59 +0200 Message-ID: <20130809111359.29819.5548.stgit@Ph0enix> In-Reply-To: <20130809111148.29819.95689.stgit@Ph0enix> References: <20130809111148.29819.95689.stgit@Ph0enix> User-Agent: StGit/0.15 MIME-Version: 1.0 Sender: netfilter-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netfilter-devel@vger.kernel.org From: Álvaro Neira Ayuso Add function that will be use in the JSON parser Signed-off-by: Alvaro Neira Ayuso --- src/expr/nat.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/src/expr/nat.c b/src/expr/nat.c index 654d4d7..c81dc7f 100644 --- a/src/expr/nat.c +++ b/src/expr/nat.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -183,6 +184,17 @@ nft_rule_expr_nat_build(struct nlmsghdr *nlh, struct nft_rule_expr *e) htonl(nat->sreg_proto_max)); } +static inline int nft_str2nat(const char *nat) +{ + if (strcmp(nat, "snat") == 0) + return NFT_NAT_SNAT; + else if (strcmp(nat, "dnat") == 0) + return NFT_NAT_DNAT; + else { + errno = EINVAL; + return -1; + } +} static int nft_rule_expr_nat_xml_parse(struct nft_rule_expr *e, mxml_node_t *tree) { @@ -190,19 +202,17 @@ static int nft_rule_expr_nat_xml_parse(struct nft_rule_expr *e, mxml_node_t *tre struct nft_expr_nat *nat = nft_expr_data(e); const char *nat_type; int32_t reg; - int family; + int family, nat_type_value; nat_type = nft_mxml_str_parse(tree, "nat_type", MXML_DESCEND_FIRST); if (nat_type == NULL) return -1; - if (strcmp(nat_type, "snat") == 0) { - nat->type = NFT_NAT_SNAT; - } else if (strcmp(nat_type, "dnat") == 0) { - nat->type = NFT_NAT_DNAT; - } else - goto err; + nat_type_value = nft_str2nat(nat_type); + if (nat_type_value < 0) + return -1; + nat->type = nat_type_value; e->flags |= (1 << NFT_EXPR_NAT_TYPE); family = nft_mxml_family_parse(tree, "family", MXML_DESCEND_FIRST); @@ -243,9 +253,6 @@ static int nft_rule_expr_nat_xml_parse(struct nft_rule_expr *e, mxml_node_t *tre e->flags |= (1 << NFT_EXPR_NAT_REG_PROTO_MAX); return 0; -err: - errno = EINVAL; - return -1; #else errno = EOPNOTSUPP; return -1;