]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
xtables: add xt_xlate_add_comment()
authorPablo Neira Ayuso <pablo@netfilter.org>
Mon, 1 Feb 2016 18:29:51 +0000 (19:29 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Tue, 16 Feb 2016 18:30:25 +0000 (19:30 +0100)
This new function allows us to add comments to the nft rule. This
can be used to provide a translation for the comment match.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/xtables.h
libxtables/xtables.c

index 82aa2bbe1f3ef4a255433c3ad2aa2c911af3d3d8..6fd3bdfc1548d75e8dc7844db5e00063c352878e 100644 (file)
@@ -573,6 +573,7 @@ extern const char *xtables_lmap_id2name(const struct xtables_lmap *, int);
 struct xt_xlate *xt_xlate_alloc(int size);
 void xt_xlate_free(struct xt_xlate *xl);
 void xt_xlate_add(struct xt_xlate *xl, const char *fmt, ...);
+void xt_xlate_add_comment(struct xt_xlate *xl, const char *comment);
 const char *xt_xlate_get(struct xt_xlate *xl);
 
 #ifdef XTABLES_INTERNAL
index 32d6a5a2bbe6e363338ffdc5be3deaf51fb8e48a..c4b86f51da3819d5ff76e729e8741c14ebe03935 100644 (file)
@@ -1987,11 +1987,16 @@ void get_kernel_version(void)
        kernel_version = LINUX_VERSION(x, y, z);
 }
 
+#include <linux/netfilter/nf_tables.h>
+
 struct xt_xlate {
-       char    *data;
-       int     size;
-       int     rem;
-       int     off;
+       struct {
+               char    *data;
+               int     size;
+               int     rem;
+               int     off;
+       } buf;
+       char comment[NFT_USERDATA_MAXLEN];
 };
 
 struct xt_xlate *xt_xlate_alloc(int size)
@@ -2002,20 +2007,20 @@ struct xt_xlate *xt_xlate_alloc(int size)
        if (xl == NULL)
                xtables_error(RESOURCE_PROBLEM, "OOM");
 
-       xl->data = malloc(size);
-       if (xl->data == NULL)
+       xl->buf.data = malloc(size);
+       if (xl->buf.data == NULL)
                xtables_error(RESOURCE_PROBLEM, "OOM");
 
-       xl->size = size;
-       xl->rem = size;
-       xl->off = 0;
+       xl->buf.size = size;
+       xl->buf.rem = size;
+       xl->buf.off = 0;
 
        return xl;
 }
 
 void xt_xlate_free(struct xt_xlate *xl)
 {
-       free(xl->data);
+       free(xl->buf.data);
        free(xl);
 }
 
@@ -2025,16 +2030,22 @@ void xt_xlate_add(struct xt_xlate *xl, const char *fmt, ...)
        int len;
 
        va_start(ap, fmt);
-       len = vsnprintf(xl->data + xl->off, xl->rem, fmt, ap);
-       if (len < 0 || len >= xl->rem)
+       len = vsnprintf(xl->buf.data + xl->buf.off, xl->buf.rem, fmt, ap);
+       if (len < 0 || len >= xl->buf.rem)
                xtables_error(RESOURCE_PROBLEM, "OOM");
 
        va_end(ap);
-       xl->rem -= len;
-       xl->off += len;
+       xl->buf.rem -= len;
+       xl->buf.off += len;
+}
+
+void xt_xlate_add_comment(struct xt_xlate *xl, const char *comment)
+{
+       strncpy(xl->comment, comment, NFT_USERDATA_MAXLEN - 1);
+       xl->comment[NFT_USERDATA_MAXLEN - 1] = '\0';
 }
 
 const char *xt_xlate_get(struct xt_xlate *xl)
 {
-       return xl->data;
+       return xl->buf.data;
 }