]> git.ipfire.org Git - thirdparty/libnftnl.git/commitdiff
udata: add TLV user data infrastructure
authorCarlos Falgueras García <carlosfg@riseup.net>
Tue, 22 Mar 2016 19:46:24 +0000 (20:46 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Thu, 14 Apr 2016 00:23:26 +0000 (02:23 +0200)
These functions allow to create a buffer (struct nftnl_udata_buf) of
user data attributes in TLV format (struct nftnl_udata). It is inspired
by libmnl/src/attr.c. It can be used to store several TLVs sequentially
into an object.

Example:

struct nftnl_udata_buf *buf;
struct nftnl_udata *attr;
const char *str = "Hello World!";

buf = nftnl_udata_buf_alloc(UDATA_SIZE);
if (!buf) {
perror("OOM");
exit(EXIT_FAILURE);
}

if (!nftnl_udata_put_strz(buf, MY_TYPE, str)) {
perror("Can't put attribute \"%s\"", str);
exit(EXIT_FAILURE);
}

nftnl_udata_for_each(buf, attr)
printf("%s\n", (char *)nftnl_udata_attr_value(attr));

nftnl_udata_buf_free(buf);

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/Makefile.am
include/libnftnl/Makefile.am
include/libnftnl/udata.h [new file with mode: 0644]
include/udata.h [new file with mode: 0644]
src/Makefile.am
src/libnftnl.map
src/udata.c [new file with mode: 0644]

index be9eb9b71c66e90f5f7a27d6c3ae54bee9affd99..9f55737f529f5c49c89e6e6fdb86cea6484cebb6 100644 (file)
@@ -12,4 +12,5 @@ noinst_HEADERS = internal.h   \
                 expr.h         \
                 json.h         \
                 set_elem.h     \
+                udata.h        \
                 utils.h
index 84f01b68d82cd5bbbac30b521eecf862f3d8c0bd..457ec95bcbc5ac60fa0a6192b1df12af53e34f4c 100644 (file)
@@ -7,4 +7,5 @@ pkginclude_HEADERS = batch.h            \
                     set.h              \
                     ruleset.h          \
                     common.h           \
+                    udata.h            \
                     gen.h
diff --git a/include/libnftnl/udata.h b/include/libnftnl/udata.h
new file mode 100644 (file)
index 0000000..312ce26
--- /dev/null
@@ -0,0 +1,52 @@
+#ifndef _LIBNFTNL_UDATA_H_
+#define _LIBNFTNL_UDATA_H_
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+/*
+ * nftnl user data attributes API
+ */
+struct nftnl_udata;
+struct nftnl_udata_buf;
+
+/* nftnl_udata_buf */
+struct nftnl_udata_buf *nftnl_udata_buf_alloc(uint32_t data_size);
+void nftnl_udata_buf_free(struct nftnl_udata_buf *buf);
+uint32_t nftnl_udata_buf_len(const struct nftnl_udata_buf *buf);
+void *nftnl_udata_buf_data(const struct nftnl_udata_buf *buf);
+void nftnl_udata_buf_put(struct nftnl_udata_buf *buf, const void *data,
+                        uint32_t len);
+struct nftnl_udata *nftnl_udata_start(const struct nftnl_udata_buf *buf);
+struct nftnl_udata *nftnl_udata_end(const struct nftnl_udata_buf *buf);
+
+/* putters */
+bool nftnl_udata_put(struct nftnl_udata_buf *buf, uint8_t type, uint32_t len,
+                    const void *value);
+bool nftnl_udata_put_strz(struct nftnl_udata_buf *buf, uint8_t type,
+                         const char *strz);
+
+/* nftnl_udata_attr */
+uint8_t nftnl_udata_type(const struct nftnl_udata *attr);
+uint8_t nftnl_udata_len(const struct nftnl_udata *attr);
+void *nftnl_udata_get(const struct nftnl_udata *attr);
+
+/* iterator */
+struct nftnl_udata *nftnl_udata_next(const struct nftnl_udata *attr);
+
+#define nftnl_udata_for_each(buf, attr)                       \
+       for ((attr) = nftnl_udata_start(buf);                 \
+            (char *)(nftnl_udata_end(buf)) > (char *)(attr); \
+            (attr) = nftnl_udata_next(attr))
+
+#define nftnl_udata_for_each_data(data, data_len, attr)  \
+       for ((attr) = (struct nftnl_udata *)(data);      \
+            (char *)(data + data_len) > (char *)(attr); \
+            (attr) = nftnl_udata_next(attr))
+
+typedef int (*nftnl_udata_cb_t)(const struct nftnl_udata *attr, void *data);
+int nftnl_udata_parse(const void *data, uint32_t data_len, nftnl_udata_cb_t cb,
+                     void *cb_data);
+
+#endif /* _LIBNFTNL_UDATA_H_ */
diff --git a/include/udata.h b/include/udata.h
new file mode 100644 (file)
index 0000000..407a3b9
--- /dev/null
@@ -0,0 +1,40 @@
+#ifndef _LIBNFTNL_UDATA_INTERNAL_H_
+#define _LIBNFTNL_UDATA_INTERNAL_H_
+
+#include <stdint.h>
+#include <stddef.h>
+
+/*
+ * TLV structures:
+ * nftnl_udata
+ *  <-------- HEADER --------> <------ PAYLOAD ------>
+ * +------------+-------------+- - - - - - - - - - - -+
+ * |    type    |     len     |         value         |
+ * |  (1 byte)  |   (1 byte)  |                       |
+ * +--------------------------+- - - - - - - - - - - -+
+ *  <-- sizeof(nftnl_udata) -> <-- nftnl_udata->len -->
+ */
+struct nftnl_udata {
+       uint8_t         type;
+       uint8_t         len;
+       unsigned char   value[];
+} __attribute__((__packed__));
+
+/*
+ *              +---------------------------------++
+ *              | data[]                          ||
+ *              |   ||                            ||
+ *              |   \/                            \/
+ *  +-------+-------+-------+-------+ ... +-------+- - - - - - -+
+ *  | size  |  end  |  TLV  |  TLV  |     |  TLV  |    Empty    |
+ *  +-------+-------+-------+-------+ ... +-------+- - - - - - -+
+ *                  |<---- nftnl_udata_len() ---->|
+ *                  |<----------- nftnl_udata_size() ---------->|
+ */
+struct nftnl_udata_buf {
+       uint32_t        size;
+       char            *end;
+       char            data[];
+};
+
+#endif /* _LIBNFTNL_UDATA_INTERNAL_H_ */
index a27e292e7e573135b3f313e928d20932fe49e6f9..7e580e48c69365412717a902e69b1e7baa98bc59 100644 (file)
@@ -19,6 +19,7 @@ libnftnl_la_SOURCES = utils.c         \
                      ruleset.c         \
                      mxml.c            \
                      jansson.c         \
+                     udata.c           \
                      expr.c            \
                      expr_ops.c        \
                      expr/bitwise.c    \
index 2e193b72cdf0492273af75040a03437228a9e5b5..c38e08163d6af80812baaf0a59a9b310f72ee28e 100644 (file)
@@ -512,4 +512,19 @@ LIBNFTNL_4.1 {
        nftnl_trace_get_data;
 
        nftnl_trace_nlmsg_parse;
+
+       nftnl_udata_buf_alloc;
+       nftnl_udata_buf_free;
+       nftnl_udata_buf_len;
+       nftnl_udata_buf_data;
+       nftnl_udata_buf_put;
+       nftnl_udata_start;
+       nftnl_udata_end;
+       nftnl_udata_put;
+       nftnl_udata_put_strz;
+       nftnl_udata_type;
+       nftnl_udata_len;
+       nftnl_udata_get;
+       nftnl_udata_next;
+       nftnl_udata_parse;
 } LIBNFTNL_4;
diff --git a/src/udata.c b/src/udata.c
new file mode 100644 (file)
index 0000000..03aac63
--- /dev/null
@@ -0,0 +1,135 @@
+/*
+ * (C) 2012-2016 by Pablo Neira Ayuso <pablo@netfilter.org>
+ * (C) 2016 by Carlos Falgueras García <carlosfg@riseup.net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <libnftnl/udata.h>
+#include <udata.h>
+#include <utils.h>
+
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+struct nftnl_udata_buf *nftnl_udata_buf_alloc(uint32_t data_size)
+{
+       struct nftnl_udata_buf *buf;
+
+       buf = malloc(sizeof(struct nftnl_udata_buf) + data_size);
+       if (!buf)
+               return NULL;
+       buf->size = data_size;
+       buf->end = buf->data;
+
+       return buf;
+}
+EXPORT_SYMBOL(nftnl_udata_buf_alloc);
+
+void nftnl_udata_buf_free(struct nftnl_udata_buf *buf)
+{
+       free(buf);
+}
+EXPORT_SYMBOL(nftnl_udata_buf_free);
+
+uint32_t nftnl_udata_buf_len(const struct nftnl_udata_buf *buf)
+{
+       return (uint32_t)(buf->end - buf->data);
+}
+EXPORT_SYMBOL(nftnl_udata_buf_len);
+
+void *nftnl_udata_buf_data(const struct nftnl_udata_buf *buf)
+{
+       return (void *)buf->data;
+}
+EXPORT_SYMBOL(nftnl_udata_buf_data);
+
+void nftnl_udata_buf_put(struct nftnl_udata_buf *buf, const void *data,
+                        uint32_t len)
+{
+       memcpy(buf->data, data, len <= buf->size ? len : buf->size);
+       buf->end = buf->data + len;
+}
+EXPORT_SYMBOL(nftnl_udata_buf_put);
+
+struct nftnl_udata *nftnl_udata_start(const struct nftnl_udata_buf *buf)
+{
+       return (struct nftnl_udata *)buf->data;
+}
+EXPORT_SYMBOL(nftnl_udata_start);
+
+struct nftnl_udata *nftnl_udata_end(const struct nftnl_udata_buf *buf)
+{
+       return (struct nftnl_udata *)buf->end;
+}
+EXPORT_SYMBOL(nftnl_udata_end);
+
+bool nftnl_udata_put(struct nftnl_udata_buf *buf, uint8_t type, uint32_t len,
+                    const void *value)
+{
+       struct nftnl_udata *attr;
+
+       if (buf->size < len + sizeof(struct nftnl_udata))
+               return false;
+
+       attr = (struct nftnl_udata *)buf->end;
+       attr->len  = len;
+       attr->type = type;
+       memcpy(attr->value, value, len);
+
+       buf->end = (char *)nftnl_udata_next(attr);
+
+       return true;
+}
+EXPORT_SYMBOL(nftnl_udata_put);
+
+bool nftnl_udata_put_strz(struct nftnl_udata_buf *buf, uint8_t type,
+                         const char *strz)
+{
+       return nftnl_udata_put(buf, type, strlen(strz) + 1, strz);
+}
+EXPORT_SYMBOL(nftnl_udata_put_strz);
+
+uint8_t nftnl_udata_type(const struct nftnl_udata *attr)
+{
+       return attr->type;
+}
+EXPORT_SYMBOL(nftnl_udata_type);
+
+uint8_t nftnl_udata_len(const struct nftnl_udata *attr)
+{
+       return attr->len;
+}
+EXPORT_SYMBOL(nftnl_udata_len);
+
+void *nftnl_udata_get(const struct nftnl_udata *attr)
+{
+       return (void *)attr->value;
+}
+EXPORT_SYMBOL(nftnl_udata_get);
+
+struct nftnl_udata *nftnl_udata_next(const struct nftnl_udata *attr)
+{
+       return (struct nftnl_udata *)&attr->value[attr->len];
+}
+EXPORT_SYMBOL(nftnl_udata_next);
+
+int nftnl_udata_parse(const void *data, uint32_t data_len, nftnl_udata_cb_t cb,
+                     void *cb_data)
+{
+       int ret = 0;
+       const struct nftnl_udata *attr;
+
+       nftnl_udata_for_each_data(data, data_len, attr) {
+               ret = cb(attr, cb_data);
+               if (ret < 0)
+                       return ret;
+       }
+
+       return ret;
+}
+EXPORT_SYMBOL(nftnl_udata_parse);