conntrack: api: add nfct_snprintf_labels
Florian Westphal
maintainerPATCH 1/1 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, 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
---
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(+)
include/internal/prototypes.h +2 -0
@@ -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
include/libnetfilter_conntrack/libnetfilter_conntrack.h +4 -0
@@ -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,
src/conntrack/api.c +16 -0
@@ -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
src/conntrack/labels.c +44 -0
47 lines not on this page · raw message
Signature · 1 line
1.8.1.5
Pablo Neira Ayuso
maintainerQuoted 10 lines · Florian Westphal
nfct_snprintfdoesn'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.
Quoted 8 lines
output looks like this:
eth0-in,eth1-inSigned-off-by Florian Westphal <[email protected]>---
I added an 'output_type' argument, not sure if this is useful. [ I don't see whyNFCT_O_XMLwould be useful here, OTOH
adding theoutput_typeargument would allow to add it later ]
That's a good idea. Users can benefit from the well structured XML output, with this parameter can be easily added in a follow up patch.
This looks good.
Quoted 119 lines
include/internal/prototypes.h | 2 + .../libnetfilter_conntrack.h | 4 ++ src/conntrack/api.c | 16 ++++++++ src/conntrack/labels.c | 44 ++++++++++++++++++++++ 4 files changed, 66 insertions(+)include/internal/prototypes.h +2
-0@@ -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 include/libnetfilter_conntrack/libnetfilter_conntrack.h +4
-0@@ -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,src/conntrack/api.c +16
-0@@ -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 src/conntrack/labels.c +44
-0@@ -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; +}Signature · 6 lines
1.8.1.5 -- To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html