libnftnl  1.0.6
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 uint8_t nftnl_udata_type(const struct nftnl_udata *attr)
98 {
99  return attr->type;
100 }
101 EXPORT_SYMBOL(nftnl_udata_type);
102 
103 uint8_t nftnl_udata_len(const struct nftnl_udata *attr)
104 {
105  return attr->len;
106 }
107 EXPORT_SYMBOL(nftnl_udata_len);
108 
109 void *nftnl_udata_get(const struct nftnl_udata *attr)
110 {
111  return (void *)attr->value;
112 }
113 EXPORT_SYMBOL(nftnl_udata_get);
114 
115 struct nftnl_udata *nftnl_udata_next(const struct nftnl_udata *attr)
116 {
117  return (struct nftnl_udata *)&attr->value[attr->len];
118 }
119 EXPORT_SYMBOL(nftnl_udata_next);
120 
121 int nftnl_udata_parse(const void *data, uint32_t data_len, nftnl_udata_cb_t cb,
122  void *cb_data)
123 {
124  int ret = 0;
125  const struct nftnl_udata *attr;
126 
127  nftnl_udata_for_each_data(data, data_len, attr) {
128  ret = cb(attr, cb_data);
129  if (ret < 0)
130  return ret;
131  }
132 
133  return ret;
134 }
135 EXPORT_SYMBOL(nftnl_udata_parse);