From patchwork Fri Aug 9 11:14:46 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alvaro Neira X-Patchwork-Id: 266003 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 EEEB22C00A3 for ; Fri, 9 Aug 2013 21:14:58 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S967760Ab3HILO6 (ORCPT ); Fri, 9 Aug 2013 07:14:58 -0400 Received: from mail-wi0-f172.google.com ([209.85.212.172]:64813 "EHLO mail-wi0-f172.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S967573Ab3HILO5 (ORCPT ); Fri, 9 Aug 2013 07:14:57 -0400 Received: by mail-wi0-f172.google.com with SMTP id hj13so1518080wib.5 for ; Fri, 09 Aug 2013 04:14:56 -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=5eUFSsQ5KngmrTfJHggXcwrAyqnAEd86/gnX6KmQOSk=; b=TDh4lpnlK6cIL/FElDG7pe+WGLdh+GYba/TeAdOUGmjEULbCx7LDa3Z3Ms7EQjzQnf Hr0rEZ2TFZJ/VJLlKLYc9gIfmBi9gEPoR7uiWFaXy7cegUtgZFNzcudXNp0DKzfRl5Qd lHZoJ0bXCf6qSTfkQD0HVnEFGVnSCk77UvU3EW0BqLYqNOsfCC9xLs2sP5cJVml0Xo4Z FVkvO2DbcGCxJ1jDG0W+nx+mA+tqVvhOJbbN33gR+EtDO5Tfiuk2AgjEs1Ccdh8sQ861 fPorSku92gtvXdWxqNZMmZFN2H86ZNZnQ7nMGgQQBCfQHWAMu069xkYRLXeOjlEGiQvN 2P7A== X-Received: by 10.180.75.16 with SMTP id y16mr44320wiv.59.1376046896437; Fri, 09 Aug 2013 04:14:56 -0700 (PDT) Received: from [127.0.1.1] ([90.174.0.186]) by mx.google.com with ESMTPSA id a4sm2180625wik.11.2013.08.09.04.14.49 for (version=TLSv1.2 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 09 Aug 2013 04:14:55 -0700 (PDT) Subject: [libnftables PATCH 13/13] jansson: Add nft_jansson_family function To: netfilter-devel@vger.kernel.org From: Alvaro Neira Cc: eric@regit.org Date: Fri, 09 Aug 2013 13:14:46 +0200 Message-ID: <20130809111446.29819.79492.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 Refactor some existing code with the new function nft_jansson_family Signed-off-by: Alvaro Neira Ayuso --- src/chain.c | 10 ++-------- src/internal.h | 1 + src/jansson.c | 18 ++++++++++++++++++ src/table.c | 9 ++------- 4 files changed, 23 insertions(+), 15 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/chain.c b/src/chain.c index e2e6f90..a6d99ca 100644 --- a/src/chain.c +++ b/src/chain.c @@ -517,14 +517,8 @@ static int nft_chain_json_parse(struct nft_chain *c, char *json) nft_chain_attr_set_u64(c, NFT_CHAIN_ATTR_PACKETS, uval64); - valstr = nft_jansson_value_parse_str(root, "family"); - - if (valstr == NULL) - goto err; - - val32 = nft_str2family(valstr); - if (val32 == -1) - goto err; + if (nft_jansson_parse_family(root, &val32) != 0) + return -1; nft_chain_attr_set_u32(c, NFT_CHAIN_ATTR_FAMILY, val32); diff --git a/src/internal.h b/src/internal.h index 17d1286..62eb3c5 100644 --- a/src/internal.h +++ b/src/internal.h @@ -49,6 +49,7 @@ int nft_jansson_value_parse_val(json_t *root, const char *tag, const char *nft_jansson_value_parse_str(json_t *root, const char *tag); bool nft_jansson_node_exist(json_t *root, const char *tag); json_t *nft_jansson_get_root(char *json, const char *tag, json_error_t *err); +int nft_jansson_parse_family(json_t *root, void *out); #endif const char *nft_family2str(uint32_t family); diff --git a/src/jansson.c b/src/jansson.c index 4c778d9..cc3ab23 100644 --- a/src/jansson.c +++ b/src/jansson.c @@ -90,4 +90,22 @@ json_t *nft_jansson_get_root(char *json, const char *tag, json_error_t *err) return root; } +int nft_jansson_parse_family(json_t *root, void *out) +{ + const char *str; + int family; + + str = nft_jansson_value_parse_str(root, "family"); + if (str == NULL) + return -1; + + family = nft_str2family(str); + if (family < 0) { + errno = EINVAL; + return -1; + } + + memcpy(out, &family, sizeof(family)); + return 0; +} #endif diff --git a/src/table.c b/src/table.c index 18d9077..1fa0dac 100644 --- a/src/table.c +++ b/src/table.c @@ -285,13 +285,8 @@ static int nft_table_json_parse(struct nft_table *t, char *json) nft_table_attr_set_str(t, NFT_TABLE_ATTR_NAME, strdup(str)); - str = nft_jansson_value_parse_str(root, "family"); - if (str == NULL) - goto err; - - family = nft_str2family(str); - if (family < 0) - goto err; + if (nft_jansson_parse_family(root, &family) != 0) + return -1; nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FAMILY, family);