From: Alvaro Neira Ayuso <alvaroneay@gmail.com>
To: netfilter-devel@vger.kernel.org
Subject: [libnftnl PATCH 1/2] src/rule: Removed mandatory attribute printing in rules
Date: Sun, 09 Mar 2014 13:59:59 +0100 [thread overview]
Message-ID: <20140309125959.23270.43422.stgit@Ph0enix> (raw)
From: Álvaro Neira Ayuso <alvaroneay@gmail.com>
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...
<rule><family>ip</family><handle>4</handle>...
Now, we can print rule without some attribute:
{"rule":{"handle":4...
<rule><handle>4</handle>...
Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
---
src/rule.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 79 insertions(+), 13 deletions(-)
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, "<rule><family>%s</family>"
- "<table>%s</table><chain>%s</chain>"
- "<handle>%llu</handle>",
- 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</family>",
+ 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</table>",
+ r->table);
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ }
+
+ if (r->flags & (1 << NFT_RULE_ATTR_CHAIN)) {
+ ret = snprintf(buf+offset, len, "<chain>%s</chain>",
+ r->chain);
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ }
+ if (r->flags & (1 << NFT_RULE_ATTR_HANDLE)) {
+ ret = snprintf(buf+offset, len, "<handle>%llu</handle>",
+ (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,
"<compat_flags>%u</compat_flags>"
@@ -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) {
--
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
next reply other threads:[~2014-03-09 13:00 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-03-09 12:59 Alvaro Neira Ayuso [this message]
2014-03-09 13:00 ` [libnftnl PATCH 2/2] src/rule: Changed parser support for having consistent with the new print support Alvaro Neira Ayuso
2014-03-11 20:16 ` Arturo Borrero Gonzalez
2014-03-12 11:37 ` Álvaro Neira Ayuso
2014-03-09 13:14 ` [libnftnl PATCH 1/2] src/rule: Removed mandatory attribute printing in rules Arturo Borrero Gonzalez
2014-03-12 12:50 ` Pablo Neira Ayuso
2014-03-11 19:59 ` Arturo Borrero Gonzalez
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20140309125959.23270.43422.stgit@Ph0enix \
--to=alvaroneay@gmail.com \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).