libnftnl  1.2.1
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 EXPORT_SYMBOL(nftnl_udata_buf_data);
46 void *nftnl_udata_buf_data(const struct nftnl_udata_buf *buf)
47 {
48  return (void *)buf->data;
49 }
50 
51 EXPORT_SYMBOL(nftnl_udata_buf_put);
52 void nftnl_udata_buf_put(struct nftnl_udata_buf *buf, const void *data,
53  uint32_t len)
54 {
55  memcpy(buf->data, data, len <= buf->size ? len : buf->size);
56  buf->end = buf->data + len;
57 }
58 
59 EXPORT_SYMBOL(nftnl_udata_start);
60 struct nftnl_udata *nftnl_udata_start(const struct nftnl_udata_buf *buf)
61 {
62  return (struct nftnl_udata *)buf->data;
63 }
64 
65 EXPORT_SYMBOL(nftnl_udata_end);
66 struct nftnl_udata *nftnl_udata_end(const struct nftnl_udata_buf *buf)
67 {
68  return (struct nftnl_udata *)buf->end;
69 }
70 
71 EXPORT_SYMBOL(nftnl_udata_put);
72 bool nftnl_udata_put(struct nftnl_udata_buf *buf, uint8_t type, uint32_t len,
73  const void *value)
74 {
75  struct nftnl_udata *attr;
76 
77  if (len > UINT8_MAX || buf->size < len + sizeof(struct nftnl_udata))
78  return false;
79 
80  attr = (struct nftnl_udata *)buf->end;
81  attr->len = len;
82  attr->type = type;
83  memcpy(attr->value, value, len);
84 
85  buf->end = (char *)nftnl_udata_next(attr);
86 
87  return true;
88 }
89 
90 EXPORT_SYMBOL(nftnl_udata_put_strz);
91 bool nftnl_udata_put_strz(struct nftnl_udata_buf *buf, uint8_t type,
92  const char *strz)
93 {
94  return nftnl_udata_put(buf, type, strlen(strz) + 1, strz);
95 }
96 
97 EXPORT_SYMBOL(nftnl_udata_put_u32);
98 bool nftnl_udata_put_u32(struct nftnl_udata_buf *buf, uint8_t type,
99  uint32_t data)
100 {
101  return nftnl_udata_put(buf, type, sizeof(data), &data);
102 }
103 
104 EXPORT_SYMBOL(nftnl_udata_type);
105 uint8_t nftnl_udata_type(const struct nftnl_udata *attr)
106 {
107  return attr->type;
108 }
109 
110 EXPORT_SYMBOL(nftnl_udata_len);
111 uint8_t nftnl_udata_len(const struct nftnl_udata *attr)
112 {
113  return attr->len;
114 }
115 
116 EXPORT_SYMBOL(nftnl_udata_get);
117 void *nftnl_udata_get(const struct nftnl_udata *attr)
118 {
119  return (void *)attr->value;
120 }
121 
122 EXPORT_SYMBOL(nftnl_udata_get_u32);
123 uint32_t nftnl_udata_get_u32(const struct nftnl_udata *attr)
124 {
125  uint32_t data;
126 
127  memcpy(&data, attr->value, sizeof(data));
128 
129  return data;
130 }
131 
132 EXPORT_SYMBOL(nftnl_udata_next);
133 struct nftnl_udata *nftnl_udata_next(const struct nftnl_udata *attr)
134 {
135  return (struct nftnl_udata *)&attr->value[attr->len];
136 }
137 
138 EXPORT_SYMBOL(nftnl_udata_parse);
139 int nftnl_udata_parse(const void *data, uint32_t data_len, nftnl_udata_cb_t cb,
140  void *cb_data)
141 {
142  int ret = 0;
143  const struct nftnl_udata *attr;
144 
145  nftnl_udata_for_each_data(data, data_len, attr) {
146  ret = cb(attr, cb_data);
147  if (ret < 0)
148  return ret;
149  }
150 
151  return ret;
152 }
153 
154 EXPORT_SYMBOL(nftnl_udata_nest_start);
155 struct nftnl_udata *nftnl_udata_nest_start(struct nftnl_udata_buf *buf,
156  uint8_t type)
157 {
158  struct nftnl_udata *ud = nftnl_udata_end(buf);
159 
160  nftnl_udata_put(buf, type, 0, NULL);
161 
162  return ud;
163 }
164 
165 EXPORT_SYMBOL(nftnl_udata_nest_end);
166 void nftnl_udata_nest_end(struct nftnl_udata_buf *buf, struct nftnl_udata *ud)
167 {
168  ud->len = buf->end - (char *)ud->value;
169 }