From 5c3bc232dc9d1dd01d589fab096f67d944621fc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Falgueras=20Garc=C3=ADa?= Date: Tue, 22 Mar 2016 20:46:24 +0100 Subject: udata: add TLV user data infrastructure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Pablo Neira Ayuso --- include/libnftnl/udata.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 include/libnftnl/udata.h (limited to 'include/libnftnl/udata.h') diff --git a/include/libnftnl/udata.h b/include/libnftnl/udata.h new file mode 100644 index 0000000..312ce26 --- /dev/null +++ b/include/libnftnl/udata.h @@ -0,0 +1,52 @@ +#ifndef _LIBNFTNL_UDATA_H_ +#define _LIBNFTNL_UDATA_H_ + +#include +#include +#include + +/* + * 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_ */ -- cgit v1.2.3