summaryrefslogtreecommitdiffstats
path: root/src/udata.c
blob: 0cc3520ccede3185969b26fd7e36e7e7604aa104 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * (C) 2012-2016 by Pablo Neira Ayuso <pablo@netfilter.org>
 * (C) 2016 by Carlos Falgueras García <carlosfg@riseup.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <libnftnl/udata.h>
#include <udata.h>
#include <utils.h>

#include <stdlib.h>
#include <stdint.h>
#include <string.h>

EXPORT_SYMBOL(nftnl_udata_buf_alloc);
struct nftnl_udata_buf *nftnl_udata_buf_alloc(uint32_t data_size)
{
	struct nftnl_udata_buf *buf;

	buf = malloc(sizeof(struct nftnl_udata_buf) + data_size);
	if (!buf)
		return NULL;
	buf->size = data_size;
	buf->end = buf->data;

	return buf;
}

EXPORT_SYMBOL(nftnl_udata_buf_free);
void nftnl_udata_buf_free(const struct nftnl_udata_buf *buf)
{
	xfree(buf);
}

EXPORT_SYMBOL(nftnl_udata_buf_len);
uint32_t nftnl_udata_buf_len(const struct nftnl_udata_buf *buf)
{
	return (uint32_t)(buf->end - buf->data);
}

EXPORT_SYMBOL(nftnl_udata_buf_data);
void *nftnl_udata_buf_data(const struct nftnl_udata_buf *buf)
{
	return (void *)buf->data;
}

EXPORT_SYMBOL(nftnl_udata_buf_put);
void nftnl_udata_buf_put(struct nftnl_udata_buf *buf, const void *data,
			 uint32_t len)
{
	memcpy(buf->data, data, len <= buf->size ? len : buf->size);
	buf->end = buf->data + len;
}

EXPORT_SYMBOL(nftnl_udata_start);
struct nftnl_udata *nftnl_udata_start(const struct nftnl_udata_buf *buf)
{
	return (struct nftnl_udata *)buf->data;
}

EXPORT_SYMBOL(nftnl_udata_end);
struct nftnl_udata *nftnl_udata_end(const struct nftnl_udata_buf *buf)
{
	return (struct nftnl_udata *)buf->end;
}

EXPORT_SYMBOL(nftnl_udata_put);
bool nftnl_udata_put(struct nftnl_udata_buf *buf, uint8_t type, uint32_t len,
		     const void *value)
{
	struct nftnl_udata *attr;

	if (len > UINT8_MAX || buf->size < len + sizeof(struct nftnl_udata))
		return false;

	attr = (struct nftnl_udata *)buf->end;
	attr->len  = len;
	attr->type = type;
	memcpy(attr->value, value, len);

	buf->end = (char *)nftnl_udata_next(attr);

	return true;
}

EXPORT_SYMBOL(nftnl_udata_put_strz);
bool nftnl_udata_put_strz(struct nftnl_udata_buf *buf, uint8_t type,
			  const char *strz)
{
	return nftnl_udata_put(buf, type, strlen(strz) + 1, strz);
}

EXPORT_SYMBOL(nftnl_udata_put_u32);
bool nftnl_udata_put_u32(struct nftnl_udata_buf *buf, uint8_t type,
			 uint32_t data)
{
	return nftnl_udata_put(buf, type, sizeof(data), &data);
}

EXPORT_SYMBOL(nftnl_udata_type);
uint8_t nftnl_udata_type(const struct nftnl_udata *attr)
{
	return attr->type;
}

EXPORT_SYMBOL(nftnl_udata_len);
uint8_t nftnl_udata_len(const struct nftnl_udata *attr)
{
	return attr->len;
}

EXPORT_SYMBOL(nftnl_udata_get);
void *nftnl_udata_get(const struct nftnl_udata *attr)
{
	return (void *)attr->value;
}

EXPORT_SYMBOL(nftnl_udata_get_u32);
uint32_t nftnl_udata_get_u32(const struct nftnl_udata *attr)
{
	uint32_t data;

	memcpy(&data, attr->value, sizeof(data));

	return data;
}

EXPORT_SYMBOL(nftnl_udata_next);
struct nftnl_udata *nftnl_udata_next(const struct nftnl_udata *attr)
{
	return (struct nftnl_udata *)&attr->value[attr->len];
}

EXPORT_SYMBOL(nftnl_udata_parse);
int nftnl_udata_parse(const void *data, uint32_t data_len, nftnl_udata_cb_t cb,
		      void *cb_data)
{
	int ret = 0;
	const struct nftnl_udata *attr;

	nftnl_udata_for_each_data(data, data_len, attr) {
		ret = cb(attr, cb_data);
		if (ret < 0)
			return ret;
	}

	return ret;
}

EXPORT_SYMBOL(nftnl_udata_nest_start);
struct nftnl_udata *nftnl_udata_nest_start(struct nftnl_udata_buf *buf,
                                           uint8_t type)
{
	struct nftnl_udata *ud = nftnl_udata_end(buf);

	nftnl_udata_put(buf, type, 0, NULL);

	return ud;
}

EXPORT_SYMBOL(nftnl_udata_nest_end);
void nftnl_udata_nest_end(struct nftnl_udata_buf *buf, struct nftnl_udata *ud)
{
	ud->len = buf->end - (char *)ud->value;
}