libnftnl  1.0.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 struct nftnl_udata_buf *nftnl_udata_buf_alloc(uint32_t data_size)
20 {
21  struct nftnl_udata_buf *buf;
22 
23  buf = malloc(sizeof(struct nftnl_udata_buf) + data_size);
24  if (!buf)
25  return NULL;
26  buf->size = data_size;
27  buf->end = buf->data;
28 
29  return buf;
30 }
31 EXPORT_SYMBOL(nftnl_udata_buf_alloc);
32 
33 void nftnl_udata_buf_free(const struct nftnl_udata_buf *buf)
34 {
35  xfree(buf);
36 }
37 EXPORT_SYMBOL(nftnl_udata_buf_free);
38 
39 uint32_t nftnl_udata_buf_len(const struct nftnl_udata_buf *buf)
40 {
41  return (uint32_t)(buf->end - buf->data);
42 }
43 EXPORT_SYMBOL(nftnl_udata_buf_len);
44 
45 void *nftnl_udata_buf_data(const struct nftnl_udata_buf *buf)
46 {
47  return (void *)buf->data;
48 }
49 EXPORT_SYMBOL(nftnl_udata_buf_data);
50 
51 void nftnl_udata_buf_put(struct nftnl_udata_buf *buf, const void *data,
52  uint32_t len)
53 {
54  memcpy(buf->data, data, len <= buf->size ? len : buf->size);
55  buf->end = buf->data + len;
56 }
57 EXPORT_SYMBOL(nftnl_udata_buf_put);
58 
59 struct nftnl_udata *nftnl_udata_start(const struct nftnl_udata_buf *buf)
60 {
61  return (struct nftnl_udata *)buf->data;
62 }
63 EXPORT_SYMBOL(nftnl_udata_start);
64 
65 struct nftnl_udata *nftnl_udata_end(const struct nftnl_udata_buf *buf)
66 {
67  return (struct nftnl_udata *)buf->end;
68 }
69 EXPORT_SYMBOL(nftnl_udata_end);
70 
71 bool nftnl_udata_put(struct nftnl_udata_buf *buf, uint8_t type, uint32_t len,
72  const void *value)
73 {
74  struct nftnl_udata *attr;
75 
76  if (buf->size < len + sizeof(struct nftnl_udata))
77  return false;
78 
79  attr = (struct nftnl_udata *)buf->end;
80  attr->len = len;
81  attr->type = type;
82  memcpy(attr->value, value, len);
83 
84  buf->end = (char *)nftnl_udata_next(attr);
85 
86  return true;
87 }
88 EXPORT_SYMBOL(nftnl_udata_put);
89 
90 bool nftnl_udata_put_strz(struct nftnl_udata_buf *buf, uint8_t type,
91  const char *strz)
92 {
93  return nftnl_udata_put(buf, type, strlen(strz) + 1, strz);
94 }
95 EXPORT_SYMBOL(nftnl_udata_put_strz);
96 
97 bool nftnl_udata_put_u32(struct nftnl_udata_buf *buf, uint8_t type,
98  uint32_t data)
99 {
100  return nftnl_udata_put(buf, type, sizeof(data), &data);
101 }
102 EXPORT_SYMBOL(nftnl_udata_put_u32);
103 
104 uint8_t nftnl_udata_type(const struct nftnl_udata *attr)
105 {
106  return attr->type;
107 }
108 EXPORT_SYMBOL(nftnl_udata_type);
109 
110 uint8_t nftnl_udata_len(const struct nftnl_udata *attr)
111 {
112  return attr->len;
113 }
114 EXPORT_SYMBOL(nftnl_udata_len);
115 
116 void *nftnl_udata_get(const struct nftnl_udata *attr)
117 {
118  return (void *)attr->value;
119 }
120 EXPORT_SYMBOL(nftnl_udata_get);
121 
122 uint32_t nftnl_udata_get_u32(const struct nftnl_udata *attr)
123 {
124  uint32_t *data = (uint32_t *)attr->value;
125 
126  return *data;
127 }
128 EXPORT_SYMBOL(nftnl_udata_get_u32);
129 
130 struct nftnl_udata *nftnl_udata_next(const struct nftnl_udata *attr)
131 {
132  return (struct nftnl_udata *)&attr->value[attr->len];
133 }
134 EXPORT_SYMBOL(nftnl_udata_next);
135 
136 int nftnl_udata_parse(const void *data, uint32_t data_len, nftnl_udata_cb_t cb,
137  void *cb_data)
138 {
139  int ret = 0;
140  const struct nftnl_udata *attr;
141 
142  nftnl_udata_for_each_data(data, data_len, attr) {
143  ret = cb(attr, cb_data);
144  if (ret < 0)
145  return ret;
146  }
147 
148  return ret;
149 }
150 EXPORT_SYMBOL(nftnl_udata_parse);