From mboxrd@z Thu Jan 1 00:00:00 1970 From: Arturo Borrero Gonzalez Subject: [libnftables PATCH 01/21] chain: add hooknum2str Date: Wed, 26 Jun 2013 13:36:57 +0200 Message-ID: <20130626113656.23511.96919.stgit@nfdev.cica.es> References: <20130626113509.23511.14359.stgit@nfdev.cica.es> Mime-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Cc: pablo@netfilter.org To: netfilter-devel@vger.kernel.org Return-path: Received: from smtp3.cica.es ([150.214.5.190]:54643 "EHLO smtp.cica.es" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751683Ab3FZLhG (ORCPT ); Wed, 26 Jun 2013 07:37:06 -0400 In-Reply-To: <20130626113509.23511.14359.stgit@nfdev.cica.es> Sender: netfilter-devel-owner@vger.kernel.org List-ID: 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 --- src/chain.c | 36 +++++++++++++++++++++++++++--------- test/nft-chain-xml-add.sh | 6 +++--- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/chain.c b/src/chain.c index 6673b82..f3ba532 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