summaryrefslogtreecommitdiffstats
path: root/include/udata.h
diff options
context:
space:
mode:
authorCarlos Falgueras García <carlosfg@riseup.net>2016-03-22 20:46:24 +0100
committerPablo Neira Ayuso <pablo@netfilter.org>2016-04-14 02:23:26 +0200
commit5c3bc232dc9d1dd01d589fab096f67d944621fc2 (patch)
tree51d84afbca7fe6adc6b7c0b941ae3dba4f9117bc /include/udata.h
parent2d5f3998fa478a4555b0d0599e015b763f03576c (diff)
udata: add TLV user data infrastructure
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>
Diffstat (limited to 'include/udata.h')
-rw-r--r--include/udata.h40
1 files changed, 40 insertions, 0 deletions
diff --git a/include/udata.h b/include/udata.h
new file mode 100644
index 0000000..407a3b9
--- /dev/null
+++ b/include/udata.h
@@ -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_ */