From patchwork Thu Jun 27 16:55:47 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arturo Borrero X-Patchwork-Id: 255110 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 3F80F2C0079 for ; Fri, 28 Jun 2013 02:56:06 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752197Ab3F0Q4E (ORCPT ); Thu, 27 Jun 2013 12:56:04 -0400 Received: from smtp3.cica.es ([150.214.5.190]:33494 "EHLO smtp.cica.es" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752030Ab3F0Q4D (ORCPT ); Thu, 27 Jun 2013 12:56:03 -0400 Received: from localhost (unknown [127.0.0.1]) by smtp.cica.es (Postfix) with ESMTP id 9BCBD51ED2C for ; Thu, 27 Jun 2013 16:56:00 +0000 (UTC) X-Virus-Scanned: amavisd-new at cica.es Received: from smtp.cica.es ([127.0.0.1]) by localhost (mail.cica.es [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 0MsunwXEYkLx for ; Thu, 27 Jun 2013 18:55:51 +0200 (CEST) Received: from nfdev.cica.es (nfdev.cica.es [IPv6:2a00:9ac0:c1ca:31::220]) by smtp.cica.es (Postfix) with ESMTP id B576B51ED2B for ; Thu, 27 Jun 2013 18:55:49 +0200 (CEST) Subject: [libnftables PATCH v2] chain: add hooknum2str To: netfilter-devel@vger.kernel.org From: Arturo Borrero Gonzalez Date: Thu, 27 Jun 2013 18:55:47 +0200 Message-ID: <20130627165512.26972.47124.stgit@nfdev.cica.es> 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 This patch translates the Netfilter hooknumber to a readable string. Useful for printing and parsing in XML and JSON formats. Signed-off-by: Arturo Borrero Gonzalez --- v2: Add "" to Json strings. src/chain.c | 36 +++++++++++++++++++++++++++--------- test/nft-chain-xml-add.sh | 6 +++--- 2 files changed, 30 insertions(+), 12 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 6673b82..d290545 100644 --- a/src/chain.c +++ b/src/chain.c @@ -22,6 +22,7 @@ #include #include #include +#include #include @@ -42,6 +43,14 @@ struct nft_chain { uint32_t flags; }; +static const char *hooknum2str_array[NF_INET_NUMHOOKS] = { + [NF_INET_PRE_ROUTING] = "NF_INET_PRE_ROUTING", + [NF_INET_LOCAL_IN] = "NF_INET_LOCAL_IN", + [NF_INET_FORWARD] = "NF_INET_FORWARD", + [NF_INET_LOCAL_OUT] = "NF_INET_LOCAL_OUT", + [NF_INET_POST_ROUTING] = "NF_INET_POST_ROUTING", +}; + struct nft_chain *nft_chain_alloc(void) { return calloc(1, sizeof(struct nft_chain)); @@ -629,15 +638,22 @@ static int nft_chain_xml_parse(struct nft_chain *c, char *xml) mxmlDelete(tree); return -1; } - utmp = strtoull(node->child->value.opaque, &endptr, 10); - if (utmp > UINT32_MAX || utmp < 0 || *endptr) { + + /* iterate the list of hooks until a match is found */ + for (utmp = 0; utmp < NF_INET_NUMHOOKS; utmp++) { + if (strcmp(node->child->value.opaque, hooknum2str_array[utmp]) == 0) { + c->hooknum = utmp; + c->flags |= (1 << NFT_CHAIN_ATTR_HOOKNUM); + break; + } + } + + /* if no hook was found, error */ + if (!(c->flags & (1 << NFT_CHAIN_ATTR_HOOKNUM))) { mxmlDelete(tree); return -1; } - memcpy(&c->hooknum, &utmp, sizeof(c->hooknum)); - c->flags |= (1 << NFT_CHAIN_ATTR_HOOKNUM); - /* Get and set */ node = mxmlFindElement(tree, tree, "policy", NULL, NULL, MXML_DESCEND); if (node == NULL) { @@ -709,7 +725,7 @@ static int nft_chain_snprintf_json(char *buf, size_t size, struct nft_chain *c) "\"table\" : \"%s\"," "\"prio\" : %d," "\"use\" : %d," - "\"hooknum\" : %d," + "\"hooknum\" : \"%s\"," "\"policy\" : %d," "\"family\" : %d" "}" @@ -717,7 +733,8 @@ static int nft_chain_snprintf_json(char *buf, size_t size, struct nft_chain *c) "}", c->name, c->handle, c->bytes, c->packets, NFT_CHAIN_JSON_VERSION, c->type, c->table, - c->prio, c->use, c->hooknum, c->policy, c->family); + c->prio, c->use, hooknum2str_array[c->hooknum], + c->policy, c->family); } static int nft_chain_snprintf_xml(char *buf, size_t size, struct nft_chain *c) @@ -730,14 +747,15 @@ static int nft_chain_snprintf_xml(char *buf, size_t size, struct nft_chain *c) "%s
" "%d" "%d" - "%d" + "%s" "%d" "%d" "" "", c->name, c->handle, c->bytes, c->packets, NFT_CHAIN_XML_VERSION, c->type, c->table, - c->prio, c->use, c->hooknum, c->policy, c->family); + c->prio, c->use, hooknum2str_array[c->hooknum], + c->policy, c->family); } static int nft_chain_snprintf_default(char *buf, size_t size, struct nft_chain *c) diff --git a/test/nft-chain-xml-add.sh b/test/nft-chain-xml-add.sh index d1bd839..fda28cb 100755 --- a/test/nft-chain-xml-add.sh +++ b/test/nft-chain-xml-add.sh @@ -40,7 +40,7 @@ XML="filter 0 0 - 2 + NF_INET_LOCAL_IN 1 2 @@ -61,7 +61,7 @@ XML="filter 1 0 - 4 + NF_INET_POST_ROUTING 1 10 @@ -83,7 +83,7 @@ XML="filter 0 0 - 4 + NF_INET_FORWARD 1 2