7 #include <libnftnl/udata.h>
15 EXPORT_SYMBOL(nftnl_udata_buf_alloc);
16 struct nftnl_udata_buf *nftnl_udata_buf_alloc(uint32_t data_size)
18 struct nftnl_udata_buf *buf;
20 buf = malloc(
sizeof(
struct nftnl_udata_buf) + data_size);
23 buf->size = data_size;
29 EXPORT_SYMBOL(nftnl_udata_buf_free);
30 void nftnl_udata_buf_free(
const struct nftnl_udata_buf *buf)
35 EXPORT_SYMBOL(nftnl_udata_buf_len);
36 uint32_t nftnl_udata_buf_len(
const struct nftnl_udata_buf *buf)
38 return (uint32_t)(buf->end - buf->data);
41 static uint32_t nftnl_udata_buf_space(
const struct nftnl_udata_buf *buf)
43 return buf->size - nftnl_udata_buf_len(buf);
46 EXPORT_SYMBOL(nftnl_udata_buf_data);
47 void *nftnl_udata_buf_data(
const struct nftnl_udata_buf *buf)
49 return (
void *)buf->data;
52 EXPORT_SYMBOL(nftnl_udata_buf_put);
53 void nftnl_udata_buf_put(
struct nftnl_udata_buf *buf,
const void *data,
56 memcpy(buf->data, data, len <= buf->size ? len : buf->size);
57 buf->end = buf->data + len;
60 EXPORT_SYMBOL(nftnl_udata_start);
61 struct nftnl_udata *nftnl_udata_start(
const struct nftnl_udata_buf *buf)
63 return (
struct nftnl_udata *)buf->data;
66 EXPORT_SYMBOL(nftnl_udata_end);
67 struct nftnl_udata *nftnl_udata_end(
const struct nftnl_udata_buf *buf)
69 return (
struct nftnl_udata *)buf->end;
72 EXPORT_SYMBOL(nftnl_udata_put);
73 bool nftnl_udata_put(
struct nftnl_udata_buf *buf, uint8_t type, uint32_t len,
76 struct nftnl_udata *attr;
78 if (len > UINT8_MAX ||
79 nftnl_udata_buf_space(buf) < len +
sizeof(
struct nftnl_udata))
82 attr = (
struct nftnl_udata *)buf->end;
85 memcpy(attr->value, value, len);
87 buf->end = (
char *)nftnl_udata_next(attr);
92 EXPORT_SYMBOL(nftnl_udata_put_strz);
93 bool nftnl_udata_put_strz(
struct nftnl_udata_buf *buf, uint8_t type,
96 return nftnl_udata_put(buf, type, strlen(strz) + 1, strz);
99 EXPORT_SYMBOL(nftnl_udata_put_u32);
100 bool nftnl_udata_put_u32(
struct nftnl_udata_buf *buf, uint8_t type,
103 return nftnl_udata_put(buf, type,
sizeof(data), &data);
106 EXPORT_SYMBOL(nftnl_udata_type);
107 uint8_t nftnl_udata_type(
const struct nftnl_udata *attr)
112 EXPORT_SYMBOL(nftnl_udata_len);
113 uint8_t nftnl_udata_len(
const struct nftnl_udata *attr)
118 EXPORT_SYMBOL(nftnl_udata_get);
119 void *nftnl_udata_get(
const struct nftnl_udata *attr)
121 return (
void *)attr->value;
124 EXPORT_SYMBOL(nftnl_udata_get_u32);
125 uint32_t nftnl_udata_get_u32(
const struct nftnl_udata *attr)
129 memcpy(&data, attr->value,
sizeof(data));
134 EXPORT_SYMBOL(nftnl_udata_next);
135 struct nftnl_udata *nftnl_udata_next(
const struct nftnl_udata *attr)
137 return (
struct nftnl_udata *)&attr->value[attr->len];
140 EXPORT_SYMBOL(nftnl_udata_parse);
141 int nftnl_udata_parse(
const void *data, uint32_t data_len, nftnl_udata_cb_t cb,
145 const struct nftnl_udata *attr;
147 nftnl_udata_for_each_data(data, data_len, attr) {
148 ret = cb(attr, cb_data);
156 EXPORT_SYMBOL(nftnl_udata_nest_start);
157 struct nftnl_udata *nftnl_udata_nest_start(
struct nftnl_udata_buf *buf,
160 struct nftnl_udata *ud = nftnl_udata_end(buf);
162 nftnl_udata_put(buf, type, 0, NULL);
167 EXPORT_SYMBOL(nftnl_udata_nest_end);
168 void nftnl_udata_nest_end(
struct nftnl_udata_buf *buf,
struct nftnl_udata *ud)
170 ud->len = buf->end - (
char *)ud->value;