summaryrefslogtreecommitdiffstats
path: root/src/buffer.c
blob: f9d5a838c4ab65d7519bb3d77f4abb29c97b8625 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * (C) 2012-2014 by Pablo Neira Ayuso <pablo@netfilter.org>
 *
 * 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 <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <inttypes.h>
#include <string.h>
#include <buffer.h>
#include <libnftnl/common.h>
#include "internal.h"

int nftnl_buf_update(struct nftnl_buf *b, int ret)
{
	if (ret < 0) {
		b->fail = true;
	} else {
		b->off += ret;
		if (ret > b->len)
			ret = b->len;
		b->size += ret;
		b->len -= ret;
	}

	return ret;
}

int nftnl_buf_done(struct nftnl_buf *b)
{
	if (b->fail)
		return -1;

	/* Remove trailing comma in json */
	if (b->size > 0 && b->buf[b->size - 1] == ',') {
		b->off--;
		b->size--;
		b->len++;
	}

	return b->off;
}

static int nftnl_buf_put(struct nftnl_buf *b, const char *fmt, ...)
{
	int ret;
	va_list ap;

	va_start(ap, fmt);
	ret = vsnprintf(b->buf + b->off, b->len, fmt, ap);
	ret = nftnl_buf_update(b, ret);
	va_end(ap);

	return ret;
}

int nftnl_buf_open(struct nftnl_buf *b, int type, const char *tag)
{
	switch (type) {
	case NFTNL_OUTPUT_JSON:
		return nftnl_buf_put(b, "{\"%s\":{", tag);
	case NFTNL_OUTPUT_XML:
	default:
		return 0;
	}
}

int nftnl_buf_close(struct nftnl_buf *b, int type, const char *tag)
{
	switch (type) {
	case NFTNL_OUTPUT_JSON:
		/* Remove trailing comma in json */
		if (b->size > 0 && b->buf[b->size - 1] == ',') {
			b->off--;
			b->size--;
			b->len++;
		}

		return nftnl_buf_put(b, "}}");
	case NFTNL_OUTPUT_XML:
	default:
		return 0;
	}
}

int nftnl_buf_open_array(struct nftnl_buf *b, int type, const char *tag)
{
	switch (type) {
	case NFTNL_OUTPUT_JSON:
		return nftnl_buf_put(b, "{\"%s\":[", tag);
	case NFTNL_OUTPUT_XML:
	default:
		return 0;
	}
}

int nftnl_buf_close_array(struct nftnl_buf *b, int type, const char *tag)
{
	switch (type) {
	case NFTNL_OUTPUT_JSON:
		return nftnl_buf_put(b, "]}");
	case NFTNL_OUTPUT_XML:
	default:
		return 0;
	}
}

int nftnl_buf_u32(struct nftnl_buf *b, int type, uint32_t value, const char *tag)
{
	switch (type) {
	case NFTNL_OUTPUT_JSON:
		return nftnl_buf_put(b, "\"%s\":%u,", tag, value);
	case NFTNL_OUTPUT_XML:
	default:
		return 0;
	}
}

int nftnl_buf_s32(struct nftnl_buf *b, int type, uint32_t value, const char *tag)
{
	switch (type) {
	case NFTNL_OUTPUT_JSON:
		return nftnl_buf_put(b, "\"%s\":%d,", tag, value);
	case NFTNL_OUTPUT_XML:
	default:
		return 0;
	}
}

int nftnl_buf_u64(struct nftnl_buf *b, int type, uint64_t value, const char *tag)
{
	switch (type) {
	case NFTNL_OUTPUT_JSON:
		return nftnl_buf_put(b, "\"%s\":%"PRIu64",", tag, value);
	case NFTNL_OUTPUT_XML:
	default:
		return 0;
	}
}

int nftnl_buf_str(struct nftnl_buf *b, int type, const char *str, const char *tag)
{
	switch (type) {
	case NFTNL_OUTPUT_JSON:
		return nftnl_buf_put(b, "\"%s\":\"%s\",", tag, str);
	case NFTNL_OUTPUT_XML:
	default:
		return 0;
	}
}

int nftnl_buf_reg(struct nftnl_buf *b, int type, union nftnl_data_reg *reg,
		int reg_type, const char *tag)
{
	int ret;

	switch (type) {
	case NFTNL_OUTPUT_XML:
		return 0;
	case NFTNL_OUTPUT_JSON:
		nftnl_buf_put(b, "\"%s\":{", tag);
		ret = nftnl_data_reg_snprintf(b->buf + b->off, b->len, reg,
					    NFTNL_OUTPUT_JSON, 0, reg_type);
		nftnl_buf_update(b, ret);
		return nftnl_buf_put(b, "},");
	}
	return 0;
}

int nftnl_buf_expr_open(struct nftnl_buf *b, int type)
{
	switch (type) {
	case NFTNL_OUTPUT_XML:
		return 0;
	case NFTNL_OUTPUT_JSON:
		return nftnl_buf_put(b, "\"expr\":[");
	}
	return 0;
}

int nftnl_buf_expr_close(struct nftnl_buf *b, int type)
{
	switch (type) {
	case NFTNL_OUTPUT_XML:
		return 0;
	case NFTNL_OUTPUT_JSON:
		nftnl_buf_done(b);
		return nftnl_buf_put(b, "]");
	}
	return 0;
}

int nftnl_buf_expr(struct nftnl_buf *b, int type, uint32_t flags,
		   struct nftnl_expr *expr)
{
	int ret;

	switch (type) {
	case NFTNL_OUTPUT_XML:
		return 0;
	case NFTNL_OUTPUT_JSON:
		nftnl_buf_put(b, "{");
		nftnl_buf_str(b, type, expr->ops->name, TYPE);
		ret = expr->ops->snprintf(b->buf + b->off, b->len, type, flags,
					  expr);
		if (ret > 0)
			nftnl_buf_update(b, ret);
		else
			nftnl_buf_done(b);

		return nftnl_buf_put(b, "},");
	}
	return 0;
}