11 #include <libnftnl/udata.h>
19 EXPORT_SYMBOL(nftnl_udata_buf_alloc);
20 struct nftnl_udata_buf *nftnl_udata_buf_alloc(uint32_t data_size)
22 struct nftnl_udata_buf *buf;
24 buf = malloc(
sizeof(
struct nftnl_udata_buf) + data_size);
27 buf->size = data_size;
33 EXPORT_SYMBOL(nftnl_udata_buf_free);
34 void nftnl_udata_buf_free(
const struct nftnl_udata_buf *buf)
39 EXPORT_SYMBOL(nftnl_udata_buf_len);
40 uint32_t nftnl_udata_buf_len(
const struct nftnl_udata_buf *buf)
42 return (uint32_t)(buf->end - buf->data);
45 static uint32_t nftnl_udata_buf_space(
const struct nftnl_udata_buf *buf)
47 return buf->size - nftnl_udata_buf_len(buf);
50 EXPORT_SYMBOL(nftnl_udata_buf_data);
51 void *nftnl_udata_buf_data(
const struct nftnl_udata_buf *buf)
53 return (
void *)buf->data;
56 EXPORT_SYMBOL(nftnl_udata_buf_put);
57 void nftnl_udata_buf_put(
struct nftnl_udata_buf *buf,
const void *data,
60 memcpy(buf->data, data, len <= buf->size ? len : buf->size);
61 buf->end = buf->data + len;
64 EXPORT_SYMBOL(nftnl_udata_start);
65 struct nftnl_udata *nftnl_udata_start(
const struct nftnl_udata_buf *buf)
67 return (
struct nftnl_udata *)buf->data;
70 EXPORT_SYMBOL(nftnl_udata_end);
71 struct nftnl_udata *nftnl_udata_end(
const struct nftnl_udata_buf *buf)
73 return (
struct nftnl_udata *)buf->end;
76 EXPORT_SYMBOL(nftnl_udata_put);
77 bool nftnl_udata_put(
struct nftnl_udata_buf *buf, uint8_t type, uint32_t len,
80 struct nftnl_udata *attr;
82 if (len > UINT8_MAX ||
83 nftnl_udata_buf_space(buf) < len +
sizeof(
struct nftnl_udata))
86 attr = (
struct nftnl_udata *)buf->end;
89 memcpy(attr->value, value, len);
91 buf->end = (
char *)nftnl_udata_next(attr);
96 EXPORT_SYMBOL(nftnl_udata_put_strz);
97 bool nftnl_udata_put_strz(
struct nftnl_udata_buf *buf, uint8_t type,
100 return nftnl_udata_put(buf, type, strlen(strz) + 1, strz);
103 EXPORT_SYMBOL(nftnl_udata_put_u32);
104 bool nftnl_udata_put_u32(
struct nftnl_udata_buf *buf, uint8_t type,
107 return nftnl_udata_put(buf, type,
sizeof(data), &data);
110 EXPORT_SYMBOL(nftnl_udata_type);
111 uint8_t nftnl_udata_type(
const struct nftnl_udata *attr)
116 EXPORT_SYMBOL(nftnl_udata_len);
117 uint8_t nftnl_udata_len(
const struct nftnl_udata *attr)
122 EXPORT_SYMBOL(nftnl_udata_get);
123 void *nftnl_udata_get(
const struct nftnl_udata *attr)
125 return (
void *)attr->value;
128 EXPORT_SYMBOL(nftnl_udata_get_u32);
129 uint32_t nftnl_udata_get_u32(
const struct nftnl_udata *attr)
133 memcpy(&data, attr->value,
sizeof(data));
138 EXPORT_SYMBOL(nftnl_udata_next);
139 struct nftnl_udata *nftnl_udata_next(
const struct nftnl_udata *attr)
141 return (
struct nftnl_udata *)&attr->value[attr->len];
144 EXPORT_SYMBOL(nftnl_udata_parse);
145 int nftnl_udata_parse(
const void *data, uint32_t data_len, nftnl_udata_cb_t cb,
149 const struct nftnl_udata *attr;
151 nftnl_udata_for_each_data(data, data_len, attr) {
152 ret = cb(attr, cb_data);
160 EXPORT_SYMBOL(nftnl_udata_nest_start);
161 struct nftnl_udata *nftnl_udata_nest_start(
struct nftnl_udata_buf *buf,
164 struct nftnl_udata *ud = nftnl_udata_end(buf);
166 nftnl_udata_put(buf, type, 0, NULL);
171 EXPORT_SYMBOL(nftnl_udata_nest_end);
172 void nftnl_udata_nest_end(
struct nftnl_udata_buf *buf,
struct nftnl_udata *ud)
174 ud->len = buf->end - (
char *)ud->value;