From patchwork Sun Mar 9 12:59:59 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alvaro Neira X-Patchwork-Id: 328325 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 A207B2C00AC for ; Mon, 10 Mar 2014 00:00:15 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752564AbaCINAO (ORCPT ); Sun, 9 Mar 2014 09:00:14 -0400 Received: from mail-bk0-f41.google.com ([209.85.214.41]:53968 "EHLO mail-bk0-f41.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752113AbaCINAO (ORCPT ); Sun, 9 Mar 2014 09:00:14 -0400 Received: by mail-bk0-f41.google.com with SMTP id d7so1110935bkh.0 for ; Sun, 09 Mar 2014 06:00:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:from:date:message-id:user-agent:mime-version :content-type:content-transfer-encoding; bh=dCg0VxqIYqC8+rgMpKYxR4yTwPuJ4k32TyRMw3xfBqE=; b=AE0/CXOiYeMdo7Ijy3VHaqOKwxBHgGbW4yjP3JihkTAa/ezysp5di89lZldVV6N/e9 lItnYbpnG69cqFz7LzfP6aOujgzlOSbtagsM6gJYfBRxW+bNx0pz7iWYPUp6GoCVEMt0 6dHN/Lc6hPkMxSo5JOEERb2JX8DpH7UeEHAe3z9M6m4rFx1etEOA4H93DuxH3MVN0GBq UU5rnX/p9C4lIRiXUJeeg+3EpyHHl2OXrNNlA0lQ2PE/pEpr4ZOpz7pvky5ZiyON/cmF eOSDNi/G7KTX7v0cu4pLeU2QRIdyMr9YVkVD6ynD9I0pRVD6nUEqGGBOG5kisKg6wD3a xFzg== X-Received: by 10.205.34.204 with SMTP id st12mr714238bkb.31.1394370012518; Sun, 09 Mar 2014 06:00:12 -0700 (PDT) Received: from [127.0.1.1] ([2001:bf0:c001:30:10b9:5ac6:6d77:434d]) by mx.google.com with ESMTPSA id mc2sm8455390bkb.12.2014.03.09.06.00.11 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 09 Mar 2014 06:00:11 -0700 (PDT) Subject: [libnftnl PATCH 1/2] src/rule: Removed mandatory attribute printing in rules To: netfilter-devel@vger.kernel.org From: Alvaro Neira Ayuso Date: Sun, 09 Mar 2014 13:59:59 +0100 Message-ID: <20140309125959.23270.43422.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 Before this patch, the program tried to print some attribute that maybe the user hasn't defined for printing. We can't assume that the user want to print some attribute that we have put mandatory in the rules. Example: Before this patch, it's mandatory have a rule with family and this is the output: {"rule":{"family":"ip","handle":4... ip4... Now, we can print rule without some attribute: {"rule":{"handle":4... 4... Signed-off-by: Alvaro Neira Ayuso --- src/rule.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 79 insertions(+), 13 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/rule.c b/src/rule.c index 162a0a1..f0cafd3 100644 --- a/src/rule.c +++ b/src/rule.c @@ -775,12 +775,32 @@ static int nft_rule_snprintf_json(char *buf, size_t size, struct nft_rule *r, int ret, len = size, offset = 0; struct nft_rule_expr *expr; - ret = snprintf(buf, len, "{\"rule\":{\"family\":\"%s\",\"table\":\"%s\"," - "\"chain\":\"%s\",\"handle\":%llu,", - nft_family2str(r->family), r->table, r->chain, - (unsigned long long)r->handle); + ret = snprintf(buf, len, "{\"rule\":{"); SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + if (r->flags & (1 << NFT_RULE_ATTR_FAMILY)) { + ret = snprintf(buf+offset, len, "\"family\":\"%s\",", + nft_family2str(r->family)); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + if (r->flags & (1 << NFT_RULE_ATTR_TABLE)) { + ret = snprintf(buf+offset, len, "\"table\":\"%s\",", + r->table); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + if (r->flags & (1 << NFT_RULE_ATTR_CHAIN)) { + ret = snprintf(buf+offset, len, "\"chain\":\"%s\",", + r->chain); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + if (r->flags & (1 << NFT_RULE_ATTR_HANDLE)) { + ret = snprintf(buf+offset, len, "\"handle\":%llu,", + (unsigned long long)r->handle); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + if (r->flags & (1 << NFT_RULE_ATTR_COMPAT_PROTO) || r->flags & (1 << NFT_RULE_ATTR_COMPAT_FLAGS)) { ret = snprintf(buf+offset, len, "\"compat_flags\":%u," @@ -824,13 +844,32 @@ static int nft_rule_snprintf_xml(char *buf, size_t size, struct nft_rule *r, int ret, len = size, offset = 0; struct nft_rule_expr *expr; - ret = snprintf(buf, len, "%s" - "%s
%s" - "%llu", - nft_family2str(r->family), r->table, r->chain, - (unsigned long long)r->handle); + ret = snprintf(buf, len, ""); SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + if (r->flags & (1 << NFT_RULE_ATTR_FAMILY)) { + ret = snprintf(buf+offset, len, "%s", + nft_family2str(r->family)); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + if (r->flags & (1 << NFT_RULE_ATTR_TABLE)) { + ret = snprintf(buf+offset, len, "%s
", + r->table); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + if (r->flags & (1 << NFT_RULE_ATTR_CHAIN)) { + ret = snprintf(buf+offset, len, "%s", + r->chain); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + if (r->flags & (1 << NFT_RULE_ATTR_HANDLE)) { + ret = snprintf(buf+offset, len, "%llu", + (unsigned long long)r->handle); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + if (r->compat.flags != 0 || r->compat.proto != 0) { ret = snprintf(buf+offset, len, "%u" @@ -865,15 +904,42 @@ static int nft_rule_snprintf_xml(char *buf, size_t size, struct nft_rule *r, return offset; } -static int nft_rule_snprintf_default(char *buf, size_t size, struct nft_rule *r, +static int nft_rule_snprintf_default(char *buf, size_t size, struct nft_rule *r, uint32_t type, uint32_t flags) { struct nft_rule_expr *expr; int ret, len = size, offset = 0, i; - ret = snprintf(buf, len, "%s %s %s %"PRIu64" %"PRIu64"\n", - nft_family2str(r->family), r->table, r->chain, - r->handle, r->position); + if (r->flags & (1 << NFT_RULE_ATTR_FAMILY)) { + ret = snprintf(buf+offset, len, "%s ", + nft_family2str(r->family)); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + if (r->flags & (1 << NFT_RULE_ATTR_TABLE)) { + ret = snprintf(buf+offset, len, "%s ", + r->table); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + if (r->flags & (1 << NFT_RULE_ATTR_CHAIN)) { + ret = snprintf(buf+offset, len, "%s ", + r->chain); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + if (r->flags & (1 << NFT_RULE_ATTR_TABLE)) { + ret = snprintf(buf+offset, len, "%llu ", + (unsigned long long)r->handle); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + if (r->flags & (1 << NFT_RULE_ATTR_POSITION)) { + ret = snprintf(buf+offset, len, "%llu ", + (unsigned long long)r->position); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + ret = snprintf(buf+offset, len, "\n"); SNPRINTF_BUFFER_SIZE(ret, size, len, offset); list_for_each_entry(expr, &r->expr_list, head) {