summaryrefslogtreecommitdiffstats
path: root/src/jansson.c
blob: f0ef04cb8b64b6dd4cd4724f7478bbc86dc21a58 (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
/*
 * (C) 2013 by Álvaro Neira Ayuso <alvaroneay@gmail.com>
 *
 * 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 <internal.h>
#include <stdlib.h>
#include <limits.h>
#include <stdint.h>
#include <stdbool.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include "expr_ops.h"

#include <libnftables/expr.h>
#include <linux/netfilter/nf_tables.h>

#ifdef JSON_PARSING

static int nft_jansson_load_int_node(json_t *root, const char *tag,
				      json_int_t *val)
{
	json_t *node;

	node = json_object_get(root, tag);
	if (node == NULL) {
		errno = EINVAL;
		return -1;
	}

	if (!json_is_integer(node)) {
		errno = ERANGE;
		return -1;
	}
	*val = json_integer_value(node);

	return 0;
}

const char *nft_jansson_value_parse_str(json_t *root, const char *tag)
{
	json_t *node;
	const char *val;

	node = json_object_get(root, tag);
	if (node == NULL) {
		errno = EINVAL;
		return NULL;
	}
	val = json_string_value(node);

	return val;
}

int nft_jansson_value_parse_val(json_t *root, const char *tag, int type,
				  void *out)
{
	json_int_t val;

	if (nft_jansson_load_int_node(root, tag, &val) == -1)
		return -1;

	if (nft_get_value(type, &val, out) == -1)
		return -1;

	return 0;
}

bool nft_jansson_node_exist(json_t *root, const char *tag)
{
	return json_object_get(root, tag) != NULL;
}

json_t *nft_jansson_create_root(char *json, json_error_t *err)
{
	json_t *root;

	root = json_loadb(json, strlen(json), 0, err);
	if (root == NULL) {
		errno = EINVAL;
		return NULL;
	}

	return root;
}

json_t *nft_jansson_get_node(json_t *root, const char *tag)
{
	json_t *node;

	node = json_object_get(root, tag);
	if (node == NULL) {
		errno = EINVAL;
		return NULL;
	}

	return node;
}

void nft_jansson_free_root(json_t *root)
{
	json_decref(root);
}

int nft_jansson_parse_family(json_t *root, void *out)
{
	const char *str;
	int family;

	str = nft_jansson_value_parse_str(root, "family");
	if (str == NULL)
		return -1;

	family = nft_str2family(str);
	if (family < 0) {
		errno = EINVAL;
		return -1;
	}

	memcpy(out, &family, sizeof(family));
	return 0;
}

int nft_jansson_value_parse_reg(json_t *root, const char *tag, int type,
				void *out)
{
	if (nft_jansson_value_parse_val(root, tag, type, out) != 0)
		return -1;

	if (*((uint32_t *)out) > NFT_REG_MAX){
		errno = ERANGE;
		return -1;
	}

	return 0;
}

int nft_jansson_str2num(json_t *root, const char *tag, int base,
			void *out, enum nft_type type)
{
	const char *str;

	str = nft_jansson_value_parse_str(root, tag);
	if (str == NULL)
		return -1;

	return nft_strtoi(str, base, out, type);
}

struct nft_rule_expr *nft_jansson_expr_parse(json_t *root)
{
	struct nft_rule_expr *e;
	const char *type;
	int ret;

	type = nft_jansson_value_parse_str(root, "type");
	if (type == NULL)
		return NULL;

	e = nft_rule_expr_alloc(type);
	if (e == NULL)
		return NULL;;

	ret = e->ops->json_parse(e, root);

	return ret < 0 ? NULL : e;
}

int nft_jansson_data_reg_parse(json_t *root, const char *tag,
			       union nft_data_reg *data_reg)
{
	json_t *data;
	const char *type;
	int ret;

	data = json_object_get(root, tag);
	if (data == NULL) {
		errno = EINVAL;
		return -1;
	}

	data = json_object_get(data, "data_reg");
	if (data == NULL) {
		errno = EINVAL;
		return -1;
	}

	ret = nft_data_reg_json_parse(data_reg, data);

	if (ret < 0) {
		errno = EINVAL;
		return -1;
	}

	type = nft_jansson_value_parse_str(data, "type");
	if (type == NULL)
		return -1;

	if (strcmp(type, "value") == 0)
		return DATA_VALUE;
	else if (strcmp(type, "verdict") == 0)
		return DATA_VERDICT;
	else if (strcmp(type, "chain") == 0)
		return DATA_CHAIN;
	else {
		errno = EINVAL;
		return -1;
	}
}
#endif