blob: 17b3ee9d3f816d38f11611539348e8099a318647 [file] [log] [blame]
Ole Troandf87f802020-11-18 19:17:48 +01001/*
2 * Copyright (c) 2020 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vppinfra/cJSON.h>
17#include <vnet/ethernet/mac_address.h>
18#include <vnet/ip/ip6_packet.h>
19#include <vnet/ip/ip_format_fns.h>
20#include <vpp/api/types.h>
Filip Tehlar36217e32021-07-23 08:51:10 +000021#include "jsonformat.h"
Ole Troandf87f802020-11-18 19:17:48 +010022
Filip Tehlar36217e32021-07-23 08:51:10 +000023#define _(T) \
24 int vl_api_##T##_fromjson (cJSON *o, T *d) \
25 { \
26 if (!cJSON_IsNumber (o)) \
27 return -1; \
28 d[0] = (T) cJSON_GetNumberValue (o); \
29 return 0; \
30 }
31foreach_type_fromjson
Ole Troandf87f802020-11-18 19:17:48 +010032#undef _
33
Filip Tehlar36217e32021-07-23 08:51:10 +000034 int
35 vl_api_bool_fromjson (cJSON *o, bool *d)
Ole Troandf87f802020-11-18 19:17:48 +010036{
37 if (!cJSON_IsBool(o)) return -1;
38 *d = o->valueint ? true : false;
39 return 0;
40}
41
42int vl_api_u8_string_fromjson(cJSON *o, u8 *s, int len)
43{
44 unformat_input_t input;
45 char *p = cJSON_GetStringValue(o);
46 unformat_init_string (&input, p, strlen(p));
Ole Troan5993a342021-02-16 00:42:21 +010047 if (!unformat (&input, "0x%U", unformat_hex_string, s))
48 return -1;
Ole Troandf87f802020-11-18 19:17:48 +010049 return 0;
50}
51
52u8 *
53u8string_fromjson(cJSON *o, char *fieldname)
54{
55 u8 *s = 0;
56 unformat_input_t input;
57 cJSON *item = cJSON_GetObjectItem(o, fieldname);
58 if (!item) {
59 printf("Illegal JSON, no such fieldname %s\n", fieldname);
60 return 0;
61 }
62
63 char *p = cJSON_GetStringValue(item);
64 unformat_init_string (&input, p, strlen(p));
Ole Troan5993a342021-02-16 00:42:21 +010065 if (!unformat (&input, "0x%U", unformat_hex_string, &s))
66 return 0;
Ole Troandf87f802020-11-18 19:17:48 +010067 return s;
68}
69
70int
71u8string_fromjson2(cJSON *o, char *fieldname, u8 *data)
72{
73 u8 *s = u8string_fromjson(o, fieldname);
Ole Troan93c4b1b2021-02-16 18:09:51 +010074 if (!s)
75 return -1;
Ole Troandf87f802020-11-18 19:17:48 +010076 memcpy(data, s, vec_len(s));
77 vec_free(s);
78 return 0;
79}
80
81/* Parse an IP4 address %d.%d.%d.%d. */
82uword
83unformat_ip4_address (unformat_input_t * input, va_list * args)
84{
85 u8 *result = va_arg (*args, u8 *);
86 unsigned a[4];
87
88 if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
89 return 0;
90
91 if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
92 return 0;
93
94 result[0] = a[0];
95 result[1] = a[1];
96 result[2] = a[2];
97 result[3] = a[3];
98
99 return 1;
100}
101
102/* Parse an IP6 address. */
103uword
104unformat_ip6_address (unformat_input_t * input, va_list * args)
105{
106 ip6_address_t *result = va_arg (*args, ip6_address_t *);
107 u16 hex_quads[8];
108 uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
109 uword c, n_colon, double_colon_index;
110
111 n_hex_quads = hex_quad = n_hex_digits = n_colon = 0;
112 double_colon_index = ARRAY_LEN (hex_quads);
113 while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
114 {
115 hex_digit = 16;
116 if (c >= '0' && c <= '9')
117 hex_digit = c - '0';
118 else if (c >= 'a' && c <= 'f')
119 hex_digit = c + 10 - 'a';
120 else if (c >= 'A' && c <= 'F')
121 hex_digit = c + 10 - 'A';
122 else if (c == ':' && n_colon < 2)
123 n_colon++;
124 else
125 {
126 unformat_put_input (input);
127 break;
128 }
129
130 /* Too many hex quads. */
131 if (n_hex_quads >= ARRAY_LEN (hex_quads))
132 return 0;
133
134 if (hex_digit < 16)
135 {
136 hex_quad = (hex_quad << 4) | hex_digit;
137
138 /* Hex quad must fit in 16 bits. */
139 if (n_hex_digits >= 4)
140 return 0;
141
142 n_colon = 0;
143 n_hex_digits++;
144 }
145
146 /* Save position of :: */
147 if (n_colon == 2)
148 {
149 /* More than one :: ? */
150 if (double_colon_index < ARRAY_LEN (hex_quads))
151 return 0;
152 double_colon_index = n_hex_quads;
153 }
154
155 if (n_colon > 0 && n_hex_digits > 0)
156 {
157 hex_quads[n_hex_quads++] = hex_quad;
158 hex_quad = 0;
159 n_hex_digits = 0;
160 }
161 }
162
163 if (n_hex_digits > 0)
164 hex_quads[n_hex_quads++] = hex_quad;
165
166
167 {
168 word i;
169
170 /* Expand :: to appropriate number of zero hex quads. */
171 if (double_colon_index < ARRAY_LEN (hex_quads))
172 {
173 word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
174
175 for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
176 hex_quads[n_zero + i] = hex_quads[i];
177
178 for (i = 0; i < n_zero; i++)
179 {
180 ASSERT ((double_colon_index + i) < ARRAY_LEN (hex_quads));
181 hex_quads[double_colon_index + i] = 0;
182 }
183
184 n_hex_quads = ARRAY_LEN (hex_quads);
185 }
186
187 /* Too few hex quads given. */
188 if (n_hex_quads < ARRAY_LEN (hex_quads))
189 return 0;
190
191 for (i = 0; i < ARRAY_LEN (hex_quads); i++)
192 result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
193
194 return 1;
195 }
196}
197
198u8 *
199format_ip6_address (u8 * s, va_list * args)
200{
201 ip6_address_t *a = va_arg (*args, ip6_address_t *);
202 u32 max_zero_run = 0, this_zero_run = 0;
203 int max_zero_run_index = -1, this_zero_run_index = 0;
204 int in_zero_run = 0, i;
205 int last_double_colon = 0;
206
207 /* Ugh, this is a pain. Scan forward looking for runs of 0's */
208 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
209 {
210 if (a->as_u16[i] == 0)
211 {
212 if (in_zero_run)
213 this_zero_run++;
214 else
215 {
216 in_zero_run = 1;
217 this_zero_run = 1;
218 this_zero_run_index = i;
219 }
220 }
221 else
222 {
223 if (in_zero_run)
224 {
225 /* offer to compress the biggest run of > 1 zero */
226 if (this_zero_run > max_zero_run && this_zero_run > 1)
227 {
228 max_zero_run_index = this_zero_run_index;
229 max_zero_run = this_zero_run;
230 }
231 }
232 in_zero_run = 0;
233 this_zero_run = 0;
234 }
235 }
236
237 if (in_zero_run)
238 {
239 if (this_zero_run > max_zero_run && this_zero_run > 1)
240 {
241 max_zero_run_index = this_zero_run_index;
242 max_zero_run = this_zero_run;
243 }
244 }
245
246 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
247 {
248 if (i == max_zero_run_index)
249 {
250 s = format (s, "::");
251 i += max_zero_run - 1;
252 last_double_colon = 1;
253 }
254 else
255 {
256 s = format (s, "%s%x",
257 (last_double_colon || i == 0) ? "" : ":",
258 clib_net_to_host_u16 (a->as_u16[i]));
259 last_double_colon = 0;
260 }
261 }
262
263 return s;
264}
265
Ole Troan316967c2021-02-16 00:31:52 +0100266int
267vl_api_ip4_address_t_fromjson (void **mp, int *len, cJSON *o,
268 vl_api_ip4_address_t *a)
Ole Troandf87f802020-11-18 19:17:48 +0100269{
270 unformat_input_t input;
271 char *p = cJSON_GetStringValue(o);
Ole Troan316967c2021-02-16 00:31:52 +0100272 if (!p)
273 return -1;
Ole Troandf87f802020-11-18 19:17:48 +0100274 unformat_init_string (&input, p, strlen(p));
Ole Troan71134f22021-02-17 14:10:04 +0100275 if (!unformat (&input, "%U", unformat_ip4_address, a))
276 return -1;
Ole Troan316967c2021-02-16 00:31:52 +0100277 return 0;
Ole Troandf87f802020-11-18 19:17:48 +0100278}
279
Ole Troan316967c2021-02-16 00:31:52 +0100280int
281vl_api_ip4_prefix_t_fromjson (void **mp, int *len, cJSON *o,
282 vl_api_ip4_prefix_t *a)
Ole Troandf87f802020-11-18 19:17:48 +0100283{
284 unformat_input_t input;
285 char *p = cJSON_GetStringValue(o);
Ole Troan316967c2021-02-16 00:31:52 +0100286 if (!p)
287 return -1;
Ole Troandf87f802020-11-18 19:17:48 +0100288 unformat_init_string (&input, p, strlen(p));
Ole Troan71134f22021-02-17 14:10:04 +0100289 if (!unformat (&input, "%U/%d", unformat_ip4_address, &a->address,
290 &a->len))
291 return -1;
Ole Troan316967c2021-02-16 00:31:52 +0100292 return 0;
Ole Troandf87f802020-11-18 19:17:48 +0100293}
294
Ole Troan316967c2021-02-16 00:31:52 +0100295int
296vl_api_ip4_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
297 vl_api_ip4_prefix_t *a)
Ole Troandf87f802020-11-18 19:17:48 +0100298{
299 return vl_api_ip4_prefix_t_fromjson(mp, len, o, a);
300}
Ole Troan316967c2021-02-16 00:31:52 +0100301int
302vl_api_ip6_address_t_fromjson (void **mp, int *len, cJSON *o,
303 vl_api_ip6_address_t *a)
Ole Troandf87f802020-11-18 19:17:48 +0100304{
305 unformat_input_t input;
306 char *p = cJSON_GetStringValue(o);
Ole Troan316967c2021-02-16 00:31:52 +0100307 if (!p)
308 return -1;
Ole Troandf87f802020-11-18 19:17:48 +0100309 unformat_init_string (&input, p, strlen(p));
Ole Troan71134f22021-02-17 14:10:04 +0100310 if (!unformat (&input, "%U", unformat_ip6_address, a))
311 return -1;
Ole Troan316967c2021-02-16 00:31:52 +0100312 return 0;
Ole Troandf87f802020-11-18 19:17:48 +0100313}
314
Ole Troan316967c2021-02-16 00:31:52 +0100315int
316vl_api_ip6_prefix_t_fromjson (void **mp, int *len, cJSON *o,
317 vl_api_ip6_prefix_t *a)
Ole Troandf87f802020-11-18 19:17:48 +0100318{
319 unformat_input_t input;
320 char *p = cJSON_GetStringValue(o);
Ole Troan316967c2021-02-16 00:31:52 +0100321 if (!p)
322 return -1;
Ole Troandf87f802020-11-18 19:17:48 +0100323 unformat_init_string (&input, p, strlen(p));
Ole Troan71134f22021-02-17 14:10:04 +0100324 if (!unformat (&input, "%U/%d", unformat_ip6_address, &a->address, &a->len))
325 return -1;
Ole Troan316967c2021-02-16 00:31:52 +0100326 return 0;
Ole Troandf87f802020-11-18 19:17:48 +0100327}
328
Ole Troan316967c2021-02-16 00:31:52 +0100329int
330vl_api_ip6_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
331 vl_api_ip6_prefix_t *a)
Ole Troandf87f802020-11-18 19:17:48 +0100332{
333 return vl_api_ip6_prefix_t_fromjson(mp, len, o, a);
334}
335
Ole Troan316967c2021-02-16 00:31:52 +0100336int
337vl_api_address_t_fromjson (void **mp, int *len, cJSON *o, vl_api_address_t *a)
Ole Troandf87f802020-11-18 19:17:48 +0100338{
339 unformat_input_t input;
340
341 char *p = cJSON_GetStringValue(o);
Ole Troan316967c2021-02-16 00:31:52 +0100342 if (!p)
343 return -1;
Ole Troandf87f802020-11-18 19:17:48 +0100344 unformat_init_string (&input, p, strlen(p));
Ole Troan793be462020-12-04 13:15:30 +0100345 if (unformat (&input, "%U", unformat_ip4_address, &a->un.ip4))
346 a->af = ADDRESS_IP4;
347 else if (unformat (&input, "%U", unformat_ip6_address, &a->un.ip6))
348 a->af = ADDRESS_IP6;
Ole Troandf87f802020-11-18 19:17:48 +0100349 else
Ole Troan316967c2021-02-16 00:31:52 +0100350 return -1;
351 return 0;
Ole Troandf87f802020-11-18 19:17:48 +0100352}
353
Ole Troan316967c2021-02-16 00:31:52 +0100354int
355vl_api_prefix_t_fromjson (void **mp, int *len, cJSON *o, vl_api_prefix_t *a)
Ole Troandf87f802020-11-18 19:17:48 +0100356{
357 unformat_input_t input;
358
359 char *p = cJSON_GetStringValue(o);
Ole Troan793be462020-12-04 13:15:30 +0100360
Ole Troan316967c2021-02-16 00:31:52 +0100361 if (!p)
362 return -1;
Ole Troandf87f802020-11-18 19:17:48 +0100363 unformat_init_string (&input, p, strlen(p));
Ole Troan793be462020-12-04 13:15:30 +0100364 int plen;
365 if (unformat (&input, "%U/%d", unformat_ip4_address, &a->address.un.ip4, &plen))
366 a->address.af = ADDRESS_IP4;
367 else if (unformat (&input, "%U/%d", unformat_ip6_address, &a->address.un.ip6, &plen))
368 a->address.af = ADDRESS_IP6;
Ole Troandf87f802020-11-18 19:17:48 +0100369 else
Ole Troan316967c2021-02-16 00:31:52 +0100370 return -1;
Ole Troan793be462020-12-04 13:15:30 +0100371 a->len = plen;
Ole Troan316967c2021-02-16 00:31:52 +0100372 return 0;
Ole Troandf87f802020-11-18 19:17:48 +0100373}
374
Ole Troan316967c2021-02-16 00:31:52 +0100375int
376vl_api_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
377 vl_api_prefix_t *a)
Ole Troandf87f802020-11-18 19:17:48 +0100378{
379 return vl_api_prefix_t_fromjson(mp, len, o, a);
380}
381
382uword
383unformat_mac_address (unformat_input_t * input, va_list * args)
384{
385 mac_address_t *mac = va_arg (*args, mac_address_t *);
386 u32 i, a[3];
387
388 if (unformat (input, "%_%X:%X:%X:%X:%X:%X%_",
389 1, &mac->bytes[0], 1, &mac->bytes[1], 1, &mac->bytes[2],
390 1, &mac->bytes[3], 1, &mac->bytes[4], 1, &mac->bytes[5]))
391 return (1);
392 else if (unformat (input, "%_%x.%x.%x%_", &a[0], &a[1], &a[2]))
393 {
394 for (i = 0; i < ARRAY_LEN (a); i++)
395 if (a[i] >= (1 << 16))
396 return 0;
397
398 mac->bytes[0] = (a[0] >> 8) & 0xff;
399 mac->bytes[1] = (a[0] >> 0) & 0xff;
400 mac->bytes[2] = (a[1] >> 8) & 0xff;
401 mac->bytes[3] = (a[1] >> 0) & 0xff;
402 mac->bytes[4] = (a[2] >> 8) & 0xff;
403 mac->bytes[5] = (a[2] >> 0) & 0xff;
404
405 return (1);
406 }
407 return (0);
408}
409
Ole Troan316967c2021-02-16 00:31:52 +0100410int
411vl_api_mac_address_t_fromjson (void **mp, int *len, cJSON *o,
412 vl_api_mac_address_t *a)
Ole Troandf87f802020-11-18 19:17:48 +0100413{
414 unformat_input_t input;
415
416 char *p = cJSON_GetStringValue(o);
417 unformat_init_string (&input, p, strlen(p));
Ole Troan71134f22021-02-17 14:10:04 +0100418 if (!unformat (&input, "%U", unformat_mac_address, a))
419 return -1;
Ole Troan316967c2021-02-16 00:31:52 +0100420 return 0;
Ole Troandf87f802020-11-18 19:17:48 +0100421}
422
423/* Format an IP4 address. */
424u8 *
425format_ip4_address (u8 * s, va_list * args)
426{
427 u8 *a = va_arg (*args, u8 *);
428 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
429}
430
431int
432vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
433{
434 /* copy without nul terminator */
435 u32 len = strlen (buf);
436 if (len > 0)
437 clib_memcpy_fast (str->buf, buf, len);
438 str->length = htonl (len);
439 return len + sizeof (u32);
440}
441
Ole Troandf87f802020-11-18 19:17:48 +0100442void
443vl_api_string_cJSON_AddToObject(cJSON * const object, const char * const name, vl_api_string_t *astr)
444{
445
446 if (astr == 0) return;
447 u32 length = clib_net_to_host_u32 (astr->length);
448
449 char *cstr = malloc(length + 1);
450 memcpy(cstr, astr->buf, length);
451 cstr[length] = '\0';
452 cJSON_AddStringToObject(object, name, cstr);
453 free(cstr);
454}
455
456u8 *
457format_vl_api_timestamp_t(u8 * s, va_list * args)
458{
459 f64 timestamp = va_arg (*args, f64);
460 struct tm *tm;
461 word msec;
462
463 time_t t = timestamp;
464 tm = gmtime (&t);
465 msec = 1e6 * (timestamp - t);
466 return format (s, "%4d-%02d-%02dT%02d:%02d:%02d.%06dZ", 1900 + tm->tm_year,
467 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min,
468 tm->tm_sec, msec);
469}
470
471u8 *
472format_vl_api_timedelta_t(u8 * s, va_list * args)
473{
474 return format_vl_api_timestamp_t(s, args);
475}
476
477uword
478unformat_vl_api_timedelta_t(unformat_input_t * input, va_list * args)
479{
480 return 0;
481}
482
483uword
484unformat_vl_api_timestamp_t(unformat_input_t * input, va_list * args)
485{
486 return 0;
487}
Ole Troandf87f802020-11-18 19:17:48 +0100488uword unformat_vl_api_gbp_scope_t(unformat_input_t * input, va_list * args)
489{
490 return 0;
491}
492
493cJSON *
494vl_api_ip4_address_with_prefix_t_tojson (vl_api_ip4_prefix_t *a) {
Ole Troan91144fb2021-08-17 12:57:00 +0200495 return vl_api_ip4_prefix_t_tojson (a);
Ole Troandf87f802020-11-18 19:17:48 +0100496}
497cJSON *
498vl_api_ip6_address_with_prefix_t_tojson (vl_api_ip6_prefix_t *a) {
Ole Troan91144fb2021-08-17 12:57:00 +0200499 return vl_api_ip6_prefix_t_tojson (a);
Ole Troandf87f802020-11-18 19:17:48 +0100500}
501cJSON *
502vl_api_address_with_prefix_t_tojson (vl_api_prefix_t *a) {
Ole Troan91144fb2021-08-17 12:57:00 +0200503 return vl_api_prefix_t_tojson (a);
Ole Troandf87f802020-11-18 19:17:48 +0100504}
505u8 *
506format_vl_api_mac_address_t (u8 * s, va_list * args)
507{
508 const mac_address_t *mac = va_arg (*args, mac_address_t *);
509
510 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
511 mac->bytes[0], mac->bytes[1], mac->bytes[2],
512 mac->bytes[3], mac->bytes[4], mac->bytes[5]);
513}
514#define _(T) \
515 cJSON *vl_api_ ##T## _t_tojson (vl_api_ ##T## _t *a) { \
516 u8 *s = format(0, "%U", format_vl_api_ ##T## _t, a); \
517 cJSON *o = cJSON_CreateString((char *)s); \
518 vec_free(s); \
519 return o; \
520 }
Filip Tehlar36217e32021-07-23 08:51:10 +0000521foreach_type_tojson
Ole Troandf87f802020-11-18 19:17:48 +0100522#undef _