diff options
author | Florian Westphal <fw@strlen.de> | 2013-07-03 13:06:22 +0200 |
---|---|---|
committer | Florian Westphal <fw@strlen.de> | 2013-07-04 22:25:01 +0200 |
commit | 033ec6be245261cd5e53e5a01435afea71ef6230 (patch) | |
tree | c8e8059c58103fd92998ba9ef2616989f1ee2e0a /src/conntrack/snprintf_default.c | |
parent | 1e4d02009d4517af4ee8ba0fe6d620a61978ac51 (diff) |
conntrack: api: add nfct_snprintf_labels
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. It behaves like
nfct_snprintf, except that the label names in the labelmap whose bits are
contained in connlabel attribute bitset are added to the buffer.
output looks like this:
output looks like this:
... mark=0 use=1 labels=eth0-in,eth1-in
or
<labels>
<label>eth0-in</label>
<label>eth1-in</label>
</labels>
Signed-off-by: Florian Westphal <fw@strlen.de>
Diffstat (limited to 'src/conntrack/snprintf_default.c')
-rw-r--r-- | src/conntrack/snprintf_default.c | 56 |
1 files changed, 55 insertions, 1 deletions
diff --git a/src/conntrack/snprintf_default.c b/src/conntrack/snprintf_default.c index 911faea..24e2f28 100644 --- a/src/conntrack/snprintf_default.c +++ b/src/conntrack/snprintf_default.c @@ -288,11 +288,60 @@ __snprintf_helper_name(char *buf, unsigned int len, const struct nf_conntrack *c return (snprintf(buf, len, "helper=%s ", ct->helper_name)); } +int +__snprintf_connlabels(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; +} + +static int +__snprintf_clabels(char *buf, unsigned int len, + const struct nf_conntrack *ct, struct nfct_labelmap *map) +{ + const struct nfct_bitmask *b = nfct_get_attr(ct, ATTR_CONNLABELS); + int ret, size = 0, offset = 0; + + if (!b) + return 0; + + ret = snprintf(buf, len, "labels="); + BUFFER_SIZE(ret, size, len, offset); + + ret = __snprintf_connlabels(buf + offset, len, map, b, "%s,"); + + BUFFER_SIZE(ret, size, len, offset); + + offset--; /* remove last , */ + size--; + ret = snprintf(buf + offset, len, " "); + BUFFER_SIZE(ret, size, len, offset); + + return size; +} + int __snprintf_conntrack_default(char *buf, unsigned int len, const struct nf_conntrack *ct, unsigned int msg_type, - unsigned int flags) + unsigned int flags, + struct nfct_labelmap *map) { int ret = 0, size = 0, offset = 0; @@ -426,6 +475,11 @@ int __snprintf_conntrack_default(char *buf, BUFFER_SIZE(ret, size, len, offset); } + if (map && test_bit(ATTR_CONNLABELS, ct->head.set)) { + ret = __snprintf_clabels(buf+offset, len, ct, map); + BUFFER_SIZE(ret, size, len, offset); + } + /* Delete the last blank space */ size--; |