From: Florian Westphal <fw@strlen.de>
To: netfilter-devel@vger.kernel.org
Cc: Florian Westphal <fw@strlen.de>
Subject: [PATCH lnf-ct] conntrack: api: add nfct_snprintf_labels
Date: Sun, 30 Jun 2013 23:06:50 +0200 [thread overview]
Message-ID: <1372626410-19162-1-git-send-email-fw@strlen.de> (raw)
nfct_snprintf doesn't print connlabels, as they're system specific
and can easily generate lots of output.
This adds a new helper function, nfct_snprintf_labels, to print the mapped
label names to a buffer. Input is a bitmask to test, and a map
describing the names of the individual bits.
Those names whose bits are contained in the bitset are then
written to the buffer.
output looks like this:
eth0-in,eth1-in
Signed-off-by: Florian Westphal <fw@strlen.de>
---
I added an 'output_type' argument, not sure if this is useful.
[ I don't see why NFCT_O_XML would be useful here, OTOH
adding the output_type argument would allow to add it later ]
include/internal/prototypes.h | 2 +
.../libnetfilter_conntrack.h | 4 ++
src/conntrack/api.c | 16 ++++++++
src/conntrack/labels.c | 44 ++++++++++++++++++++++
4 files changed, 66 insertions(+)
diff --git a/include/internal/prototypes.h b/include/internal/prototypes.h
index 484deea..dded472 100644
--- a/include/internal/prototypes.h
+++ b/include/internal/prototypes.h
@@ -62,5 +62,7 @@ void __labelmap_destroy(struct nfct_labelmap *);
int __labelmap_get_bit(struct nfct_labelmap *map, const char *name);
const char *__labelmap_get_name(struct nfct_labelmap *map, unsigned int bit);
+int __labels_snprintf(char *buf, unsigned int len, struct nfct_labelmap *map,
+ const struct nfct_bitmask *b, unsigned int out_type);
#endif
diff --git a/include/libnetfilter_conntrack/libnetfilter_conntrack.h b/include/libnetfilter_conntrack/libnetfilter_conntrack.h
index 39dc24c..3dafe46 100644
--- a/include/libnetfilter_conntrack/libnetfilter_conntrack.h
+++ b/include/libnetfilter_conntrack/libnetfilter_conntrack.h
@@ -294,6 +294,10 @@ struct nfct_labelmap *nfct_labelmap_new(const char *mapfile);
void nfct_labelmap_destroy(struct nfct_labelmap *map);
const char *nfct_labelmap_get_name(struct nfct_labelmap *m, unsigned int bit);
int nfct_labelmap_get_bit(struct nfct_labelmap *m, const char *name);
+int nfct_snprintf_labels(char *buf, unsigned int len,
+ struct nfct_labelmap *labelmap,
+ const struct nfct_bitmask *b,
+ unsigned int out_type);
/* setter */
extern void nfct_set_attr(struct nf_conntrack *ct,
diff --git a/src/conntrack/api.c b/src/conntrack/api.c
index b6c453f..0a668ce 100644
--- a/src/conntrack/api.c
+++ b/src/conntrack/api.c
@@ -1523,6 +1523,22 @@ int nfct_labelmap_get_bit(struct nfct_labelmap *m, const char *name)
}
/**
+ * nfct_snprintf_labels - print a bitmask object to a buffer
+ * \param buf buffer used to build the names of the bitmask
+ * \param len size of the buffer
+ * \param map nfct_labelmap which maps the bit values to the desired strings
+ * \param b pointer to a valid bitmask object
+ * \param output_type print type (NFCT_O_DEFAULT only at this time)
+ */
+int nfct_snprintf_labels(char *buf, unsigned int len,
+ struct nfct_labelmap *map,
+ const struct nfct_bitmask *b,
+ unsigned int out_type)
+{
+ return __labels_snprintf(buf, len, map, b, out_type);
+}
+
+/**
* nfct_labelmap_new - create a new label map
*
* \param mapfile the file containing the bit <-> name mapping
diff --git a/src/conntrack/labels.c b/src/conntrack/labels.c
index 7dfb780..4511180 100644
--- a/src/conntrack/labels.c
+++ b/src/conntrack/labels.c
@@ -267,3 +267,47 @@ struct nfct_labelmap *__labelmap_new(const char *name)
__labelmap_destroy(map);
return NULL;
}
+
+static int
+__labels_snprintf_fmt(char *buf, unsigned int len,
+ struct nfct_labelmap *map,
+ const struct nfct_bitmask *b,
+ const char *fmt)
+{
+ unsigned int i, max;
+ int ret, size = 0, offset = 0;
+
+ max = nfct_bitmask_maxbit(b);
+ for (i = 0; i <= max && len; i++) {
+ const char *name;
+ if (!nfct_bitmask_test_bit(b, i))
+ continue;
+ name = nfct_labelmap_get_name(map, i);
+ if (!name || strcmp(name, "") == 0)
+ continue;
+ ret = snprintf(buf + offset, len, fmt, name);
+ BUFFER_SIZE(ret, size, len, offset);
+ }
+ return size;
+}
+
+int
+__labels_snprintf(char *buf, unsigned int len,
+ struct nfct_labelmap *map,
+ const struct nfct_bitmask *b, unsigned int out_type)
+{
+ int size = 0;
+
+ switch (out_type) {
+ case NFCT_O_DEFAULT:
+ size = __labels_snprintf_fmt(buf, len, map, b, "%s,");
+ if (size && size <= len)
+ buf[--size] = 0; /* strip trailing ',' */
+ break;
+ default:
+ errno = ENOENT;
+ return -1;
+ }
+
+ return size;
+}
--
1.8.1.5
next reply other threads:[~2013-06-30 21:08 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-06-30 21:06 Florian Westphal [this message]
2013-06-30 21:34 ` [PATCH lnf-ct] conntrack: api: add nfct_snprintf_labels Pablo Neira Ayuso
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=1372626410-19162-1-git-send-email-fw@strlen.de \
--to=fw@strlen.de \
--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).