-
Notifications
You must be signed in to change notification settings - Fork 19
/
neat_json_helpers.c
217 lines (192 loc) · 6.27 KB
/
neat_json_helpers.c
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
#include "neat_internal.h"
#include "neat_json_helpers.h"
#include <assert.h>
#include <string.h>
#define BANNED_ENABLED 0
#define NEAT_TRANSPORT_PROPERTY(name, propname, protonum) \
{ \
#name, \
protonum \
}
#define NEAT_TRANSPORT(name) \
{ \
#name, \
NEAT_STACK_ ## name \
}
struct neat_transport_property {
const char *name;
neat_protocol_stack_type stack;
};
static struct neat_transport_property neat_transports[] = {
NEAT_TRANSPORT(TCP),
NEAT_TRANSPORT(MPTCP),
NEAT_TRANSPORT(SCTP),
NEAT_TRANSPORT(UDP),
{"UDPlite", NEAT_STACK_UDPLITE},
{"UDPLite", NEAT_STACK_UDPLITE},
{"UDP-Lite", NEAT_STACK_UDPLITE},
{"UDP-lite", NEAT_STACK_UDPLITE},
{"UDPLITE", NEAT_STACK_UDPLITE},
{"SCTP/UDP", NEAT_STACK_SCTP_UDP},
{"WEBRTC", NEAT_STACK_WEBRTC}
};
neat_protocol_stack_type
string_to_stack(const char *str)
{
for (size_t i = 0; i < sizeof(neat_transports) / sizeof(*neat_transports); ++i) {
if (strcmp(str, neat_transports[i].name) == 0) {
return neat_transports[i].stack;
}
}
return 0;
}
const char*
stack_to_string(neat_protocol_stack_type stack)
{
for (size_t i = 0; i < sizeof(neat_transports) / sizeof(*neat_transports); ++i) {
if (stack == neat_transports[i].stack) {
return neat_transports[i].name;
}
}
return NULL;
}
/*
* Parse the json structure to discover which protocols are enabled.
*
* TODO: Contemplate whether this can be written better somehow.
*/
void
nt_find_enabled_stacks(json_t *json, neat_protocol_stack_type *stacks,
size_t *stack_count, int *precedences)
{
json_t *transports, *transport;
json_error_t error;
size_t i;
neat_protocol_stack_type *stack_ptr = stacks;
size_t count = 0;
#if BANNED_ENABLED
neat_protocol_stack_type banned[NEAT_STACK_MAX_NUM];
neat_protocol_stack_type *banned_ptr = banned;
size_t ban_count = 0;
#endif
assert(json);
assert(stacks && stack_count);
// assert(*stack_count >= NEAT_MAX_NUM_PROTO);
transports = json_object_get(json, "transport");
if (transports == NULL) {
// The transport property is missing so we do not have a transport type.
// This should not happen if the Policy Manager is running. We'll use the
// following as a fallback:
const char *fallback_transports = "{\"value\": [\"TCP\", \"SCTP\", \"MPTCP\"]}";
transports = json_loads(fallback_transports, 0, &error);
nt_log(NULL, NEAT_LOG_DEBUG, "No transport property defined. Using fallback!");
}
if (json_is_object(transports)) {
// new properties format
int precedence = json_integer_value(json_object_get(transports, "precedence"));
const char* value;
json_t* val;
val = json_object_get(transports, "value");
assert(val);
// transport _values_ are either a single string e.g. {"value": "TCP"},
// or a dict of supported transport protocols e.g. {"value": ["TCP", "SCTP", "MPTCP"]}
if (json_typeof(val) == JSON_STRING) {
neat_protocol_stack_type stack;
value = json_string_value(val);
nt_log(NULL, NEAT_LOG_DEBUG, "Transport: %s", value);
if ((stack = string_to_stack(value)) != 0) {
*(stack_ptr++) = stack;
count++;
if (precedences) {
*(precedences++) = precedence;
}
} else {
nt_log(NULL, NEAT_LOG_DEBUG, "Unknown transport %s", value);
*stack_count = 0;
}
} else if (json_typeof(val) == JSON_ARRAY){
json_array_foreach(val, i, transport){
neat_protocol_stack_type stack;
value = json_string_value(transport);
nt_log(NULL, NEAT_LOG_DEBUG, "Transport: %s", value);
if ((stack = string_to_stack(value)) != 0) {
*(stack_ptr++) = stack;
count++;
if (precedences) {
*(precedences++) = precedence;
}
} else {
nt_log(NULL, NEAT_LOG_DEBUG, "Unknown transport %s", value);
}
}
}
} else {
fprintf(stderr, "ERROR: Invalid property format\n");
}
#if BANNED_ENABLED
// If only banned protocols are specified
if (ban_count > 0 && count == 0) {
// Add all known protocols, except those that are banned...
for (size_t i = 0; i < sizeof(neat_transports) / sizeof(*neat_transports); ++i) {
for (size_t j = 0; j < ban_count; ++j) {
if (neat_transports[i].stack == banned[j])
goto skip;
}
*(stack_ptr++) = neat_transports[i].stack;
count++;
if (precedences)
*(precedences++) = 1;
skip:
continue;
}
}
#endif
*stack_count = count;
}
json_t*
get_property(json_t *json, const char *key, json_type expected_type)
{
json_t *obj = json_object_get(json, key);
if (!obj) {
nt_log(NULL, NEAT_LOG_DEBUG, "Unable to find property with key \"%s\"", key);
return NULL;
}
obj = json_object_get(obj, "value");
if (!obj) {
nt_log(NULL, NEAT_LOG_DEBUG, "Object with key \"%s\" is missing value key");
return NULL;
}
if (json_typeof(obj) != expected_type) {
#if 0
// no ctx, can't log!
const char *typename = NULL;
switch (json_typeof(obj)) {
case JSON_OBJECT:
typename = "object";
break;
case JSON_ARRAY:
typename = "array";
break;
case JSON_INTEGER:
typename = "integer";
break;
case JSON_STRING:
typename = "string";
break;
case JSON_REAL:
typename = "real";
break;
case JSON_NULL:
typename = "null";
break;
case JSON_TRUE:
case JSON_FALSE:
typename = "bool";
break;
}
nt_log(ctx, NEAT_LOG_DEBUG, "Key \"%s\" had unexpected type: \"%s\"", key, typename);
#endif
return NULL;
}
return obj;
}