libnftnl  1.2.8
udata.c
1 /*
2  * (C) 2012-2016 by Pablo Neira Ayuso <pablo@netfilter.org>
3  * (C) 2016 by Carlos Falgueras GarcĂ­a <carlosfg@riseup.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published
7  * by the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  */
10 
11 #include <libnftnl/udata.h>
12 #include <udata.h>
13 #include <utils.h>
14 
15 #include <stdlib.h>
16 #include <stdint.h>
17 #include <string.h>
18 
19 EXPORT_SYMBOL(nftnl_udata_buf_alloc);
20 struct nftnl_udata_buf *nftnl_udata_buf_alloc(uint32_t data_size)
21 {
22  struct nftnl_udata_buf *buf;
23 
24  buf = malloc(sizeof(struct nftnl_udata_buf) + data_size);
25  if (!buf)
26  return NULL;
27  buf->size = data_size;
28  buf->end = buf->data;
29 
30  return buf;
31 }
32 
33 EXPORT_SYMBOL(nftnl_udata_buf_free);
34 void nftnl_udata_buf_free(const struct nftnl_udata_buf *buf)
35 {
36  xfree(buf);
37 }
38 
39 EXPORT_SYMBOL(nftnl_udata_buf_len);
40 uint32_t nftnl_udata_buf_len(const struct nftnl_udata_buf *buf)
41 {
42  return (uint32_t)(buf->end - buf->data);
43 }
44 
45 static uint32_t nftnl_udata_buf_space(const struct nftnl_udata_buf *buf)
46 {
47  return buf->size - nftnl_udata_buf_len(buf);
48 }
49 
50 EXPORT_SYMBOL(nftnl_udata_buf_data);
51 void *nftnl_udata_buf_data(const struct nftnl_udata_buf *buf)
52 {
53  return (void *)buf->data;
54 }
55 
56 EXPORT_SYMBOL(nftnl_udata_buf_put);
57 void nftnl_udata_buf_put(struct nftnl_udata_buf *buf, const void *data,
58  uint32_t len)
59 {
60  memcpy(buf->data, data, len <= buf->size ? len : buf->size);
61  buf->end = buf->data + len;
62 }
63 
64 EXPORT_SYMBOL(nftnl_udata_start);
65 struct nftnl_udata *nftnl_udata_start(const struct nftnl_udata_buf *buf)
66 {
67  return (struct nftnl_udata *)buf->data;
68 }
69 
70 EXPORT_SYMBOL(nftnl_udata_end);
71 struct nftnl_udata *nftnl_udata_end(const struct nftnl_udata_buf *buf)
72 {
73  return (struct nftnl_udata *)buf->end;
74 }
75 
76 EXPORT_SYMBOL(nftnl_udata_put);
77 bool nftnl_udata_put(struct nftnl_udata_buf *buf, uint8_t type, uint32_t len,
78  const void *value)
79 {
80  struct nftnl_udata *attr;
81 
82  if (len > UINT8_MAX ||
83  nftnl_udata_buf_space(buf) < len + sizeof(struct nftnl_udata))
84  return false;
85 
86  attr = (struct nftnl_udata *)buf->end;
87  attr->len = len;
88  attr->type = type;
89  memcpy(attr->value, value, len);
90 
91  buf->end = (char *)nftnl_udata_next(attr);
92 
93  return true;
94 }
95 
96 EXPORT_SYMBOL(nftnl_udata_put_strz);
97 bool nftnl_udata_put_strz(struct nftnl_udata_buf *buf, uint8_t type,
98  const char *strz)
99 {
100  return nftnl_udata_put(buf, type, strlen(strz) + 1, strz);
101 }
102 
103 EXPORT_SYMBOL(nftnl_udata_put_u32);
104 bool nftnl_udata_put_u32(struct nftnl_udata_buf *buf, uint8_t type,
105  uint32_t data)
106 {
107  return nftnl_udata_put(buf, type, sizeof(data), &data);
108 }
109 
110 EXPORT_SYMBOL(nftnl_udata_type);
111 uint8_t nftnl_udata_type(const struct nftnl_udata *attr)
112 {
113  return attr->type;
114 }
115 
116 EXPORT_SYMBOL(nftnl_udata_len);
117 uint8_t nftnl_udata_len(const struct nftnl_udata *attr)
118 {
119  return attr->len;
120 }
121 
122 EXPORT_SYMBOL(nftnl_udata_get);
123 void *nftnl_udata_get(const struct nftnl_udata *attr)
124 {
125  return (void *)attr->value;
126 }
127 
128 EXPORT_SYMBOL(nftnl_udata_get_u32);
129 uint32_t nftnl_udata_get_u32(const struct nftnl_udata *attr)
130 {
131  uint32_t data;
132 
133  memcpy(&data, attr->value, sizeof(data));
134 
135  return data;
136 }
137 
138 EXPORT_SYMBOL(nftnl_udata_next);
139 struct nftnl_udata *nftnl_udata_next(const struct nftnl_udata *attr)
140 {
141  return (struct nftnl_udata *)&attr->value[attr->len];
142 }
143 
144 EXPORT_SYMBOL(nftnl_udata_parse);
145 int nftnl_udata_parse(const void *data, uint32_t data_len, nftnl_udata_cb_t cb,
146  void *cb_data)
147 {
148  int ret = 0;
149  const struct nftnl_udata *attr;
150 
151  nftnl_udata_for_each_data(data, data_len, attr) {
152  ret = cb(attr, cb_data);
153  if (ret < 0)
154  return ret;
155  }
156 
157  return ret;
158 }
159 
160 EXPORT_SYMBOL(nftnl_udata_nest_start);
161 struct nftnl_udata *nftnl_udata_nest_start(struct nftnl_udata_buf *buf,
162  uint8_t type)
163 {
164  struct nftnl_udata *ud = nftnl_udata_end(buf);
165 
166  nftnl_udata_put(buf, type, 0, NULL);
167 
168  return ud;
169 }
170 
171 EXPORT_SYMBOL(nftnl_udata_nest_end);
172 void nftnl_udata_nest_end(struct nftnl_udata_buf *buf, struct nftnl_udata *ud)
173 {
174  ud->len = buf->end - (char *)ud->value;
175 }