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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
21 #include "jsonconvert.h"
24 int vl_api_ ##T## _fromjson(cJSON *o, T *d) \
26 if (!cJSON_IsNumber(o)) return -1; \
27 memcpy(d, &o->valueint, sizeof(T)); \
33 int vl_api_bool_fromjson(cJSON *o, bool *d)
35 if (!cJSON_IsBool(o)) return -1;
36 *d = o->valueint ? true : false;
40 int vl_api_u8_string_fromjson(cJSON *o, u8 *s, int len)
42 unformat_input_t input;
43 char *p = cJSON_GetStringValue(o);
44 unformat_init_string (&input, p, strlen(p));
45 if (!unformat (&input, "0x%U", unformat_hex_string, s))
51 u8string_fromjson(cJSON *o, char *fieldname)
54 unformat_input_t input;
55 cJSON *item = cJSON_GetObjectItem(o, fieldname);
57 printf("Illegal JSON, no such fieldname %s\n", fieldname);
61 char *p = cJSON_GetStringValue(item);
62 unformat_init_string (&input, p, strlen(p));
63 if (!unformat (&input, "0x%U", unformat_hex_string, &s))
69 u8string_fromjson2(cJSON *o, char *fieldname, u8 *data)
71 u8 *s = u8string_fromjson(o, fieldname);
73 memcpy(data, s, vec_len(s));
78 /* Parse an IP4 address %d.%d.%d.%d. */
80 unformat_ip4_address (unformat_input_t * input, va_list * args)
82 u8 *result = va_arg (*args, u8 *);
85 if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
88 if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
99 /* Parse an IP6 address. */
101 unformat_ip6_address (unformat_input_t * input, va_list * args)
103 ip6_address_t *result = va_arg (*args, ip6_address_t *);
105 uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
106 uword c, n_colon, double_colon_index;
108 n_hex_quads = hex_quad = n_hex_digits = n_colon = 0;
109 double_colon_index = ARRAY_LEN (hex_quads);
110 while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
113 if (c >= '0' && c <= '9')
115 else if (c >= 'a' && c <= 'f')
116 hex_digit = c + 10 - 'a';
117 else if (c >= 'A' && c <= 'F')
118 hex_digit = c + 10 - 'A';
119 else if (c == ':' && n_colon < 2)
123 unformat_put_input (input);
127 /* Too many hex quads. */
128 if (n_hex_quads >= ARRAY_LEN (hex_quads))
133 hex_quad = (hex_quad << 4) | hex_digit;
135 /* Hex quad must fit in 16 bits. */
136 if (n_hex_digits >= 4)
143 /* Save position of :: */
146 /* More than one :: ? */
147 if (double_colon_index < ARRAY_LEN (hex_quads))
149 double_colon_index = n_hex_quads;
152 if (n_colon > 0 && n_hex_digits > 0)
154 hex_quads[n_hex_quads++] = hex_quad;
160 if (n_hex_digits > 0)
161 hex_quads[n_hex_quads++] = hex_quad;
167 /* Expand :: to appropriate number of zero hex quads. */
168 if (double_colon_index < ARRAY_LEN (hex_quads))
170 word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
172 for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
173 hex_quads[n_zero + i] = hex_quads[i];
175 for (i = 0; i < n_zero; i++)
177 ASSERT ((double_colon_index + i) < ARRAY_LEN (hex_quads));
178 hex_quads[double_colon_index + i] = 0;
181 n_hex_quads = ARRAY_LEN (hex_quads);
184 /* Too few hex quads given. */
185 if (n_hex_quads < ARRAY_LEN (hex_quads))
188 for (i = 0; i < ARRAY_LEN (hex_quads); i++)
189 result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
196 format_ip6_address (u8 * s, va_list * args)
198 ip6_address_t *a = va_arg (*args, ip6_address_t *);
199 u32 max_zero_run = 0, this_zero_run = 0;
200 int max_zero_run_index = -1, this_zero_run_index = 0;
201 int in_zero_run = 0, i;
202 int last_double_colon = 0;
204 /* Ugh, this is a pain. Scan forward looking for runs of 0's */
205 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
207 if (a->as_u16[i] == 0)
215 this_zero_run_index = i;
222 /* offer to compress the biggest run of > 1 zero */
223 if (this_zero_run > max_zero_run && this_zero_run > 1)
225 max_zero_run_index = this_zero_run_index;
226 max_zero_run = this_zero_run;
236 if (this_zero_run > max_zero_run && this_zero_run > 1)
238 max_zero_run_index = this_zero_run_index;
239 max_zero_run = this_zero_run;
243 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
245 if (i == max_zero_run_index)
247 s = format (s, "::");
248 i += max_zero_run - 1;
249 last_double_colon = 1;
253 s = format (s, "%s%x",
254 (last_double_colon || i == 0) ? "" : ":",
255 clib_net_to_host_u16 (a->as_u16[i]));
256 last_double_colon = 0;
264 vl_api_ip4_address_t_fromjson (void **mp, int *len, cJSON *o,
265 vl_api_ip4_address_t *a)
267 unformat_input_t input;
268 char *p = cJSON_GetStringValue(o);
271 unformat_init_string (&input, p, strlen(p));
272 unformat(&input, "%U", unformat_ip4_address, a);
277 vl_api_ip4_prefix_t_fromjson (void **mp, int *len, cJSON *o,
278 vl_api_ip4_prefix_t *a)
280 unformat_input_t input;
281 char *p = cJSON_GetStringValue(o);
284 unformat_init_string (&input, p, strlen(p));
285 unformat(&input, "%U/%d", unformat_ip4_address, &a->address, &a->len);
290 vl_api_ip4_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
291 vl_api_ip4_prefix_t *a)
293 return vl_api_ip4_prefix_t_fromjson(mp, len, o, a);
296 vl_api_ip6_address_t_fromjson (void **mp, int *len, cJSON *o,
297 vl_api_ip6_address_t *a)
299 unformat_input_t input;
300 char *p = cJSON_GetStringValue(o);
303 unformat_init_string (&input, p, strlen(p));
304 unformat(&input, "%U", unformat_ip6_address, a);
309 vl_api_ip6_prefix_t_fromjson (void **mp, int *len, cJSON *o,
310 vl_api_ip6_prefix_t *a)
312 unformat_input_t input;
313 char *p = cJSON_GetStringValue(o);
316 unformat_init_string (&input, p, strlen(p));
317 unformat(&input, "%U/%d", unformat_ip6_address, &a->address, &a->len);
322 vl_api_ip6_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
323 vl_api_ip6_prefix_t *a)
325 return vl_api_ip6_prefix_t_fromjson(mp, len, o, a);
329 vl_api_address_t_fromjson (void **mp, int *len, cJSON *o, vl_api_address_t *a)
331 unformat_input_t input;
333 char *p = cJSON_GetStringValue(o);
336 unformat_init_string (&input, p, strlen(p));
337 if (unformat (&input, "%U", unformat_ip4_address, &a->un.ip4))
339 else if (unformat (&input, "%U", unformat_ip6_address, &a->un.ip6))
347 vl_api_prefix_t_fromjson (void **mp, int *len, cJSON *o, vl_api_prefix_t *a)
349 unformat_input_t input;
351 char *p = cJSON_GetStringValue(o);
355 unformat_init_string (&input, p, strlen(p));
357 if (unformat (&input, "%U/%d", unformat_ip4_address, &a->address.un.ip4, &plen))
358 a->address.af = ADDRESS_IP4;
359 else if (unformat (&input, "%U/%d", unformat_ip6_address, &a->address.un.ip6, &plen))
360 a->address.af = ADDRESS_IP6;
368 vl_api_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
371 return vl_api_prefix_t_fromjson(mp, len, o, a);
375 unformat_mac_address (unformat_input_t * input, va_list * args)
377 mac_address_t *mac = va_arg (*args, mac_address_t *);
380 if (unformat (input, "%_%X:%X:%X:%X:%X:%X%_",
381 1, &mac->bytes[0], 1, &mac->bytes[1], 1, &mac->bytes[2],
382 1, &mac->bytes[3], 1, &mac->bytes[4], 1, &mac->bytes[5]))
384 else if (unformat (input, "%_%x.%x.%x%_", &a[0], &a[1], &a[2]))
386 for (i = 0; i < ARRAY_LEN (a); i++)
387 if (a[i] >= (1 << 16))
390 mac->bytes[0] = (a[0] >> 8) & 0xff;
391 mac->bytes[1] = (a[0] >> 0) & 0xff;
392 mac->bytes[2] = (a[1] >> 8) & 0xff;
393 mac->bytes[3] = (a[1] >> 0) & 0xff;
394 mac->bytes[4] = (a[2] >> 8) & 0xff;
395 mac->bytes[5] = (a[2] >> 0) & 0xff;
403 vl_api_mac_address_t_fromjson (void **mp, int *len, cJSON *o,
404 vl_api_mac_address_t *a)
406 unformat_input_t input;
408 char *p = cJSON_GetStringValue(o);
409 unformat_init_string (&input, p, strlen(p));
410 unformat(&input, "%U", unformat_mac_address, a);
414 /* Format an IP4 address. */
416 format_ip4_address (u8 * s, va_list * args)
418 u8 *a = va_arg (*args, u8 *);
419 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
423 vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
425 /* copy without nul terminator */
426 u32 len = strlen (buf);
428 clib_memcpy_fast (str->buf, buf, len);
429 str->length = htonl (len);
430 return len + sizeof (u32);
434 format_vl_api_interface_index_t (u8 *s, va_list *args)
436 u32 *a = va_arg (*args, u32 *);
437 return format (s, "%u", *a);
441 unformat_vl_api_interface_index_t (unformat_input_t * input, va_list * args)
443 u32 *a = va_arg (*args, u32 *);
445 if (!unformat (input, "%u", a))
451 vl_api_string_cJSON_AddToObject(cJSON * const object, const char * const name, vl_api_string_t *astr)
454 if (astr == 0) return;
455 u32 length = clib_net_to_host_u32 (astr->length);
457 char *cstr = malloc(length + 1);
458 memcpy(cstr, astr->buf, length);
460 cJSON_AddStringToObject(object, name, cstr);
465 format_vl_api_timestamp_t(u8 * s, va_list * args)
467 f64 timestamp = va_arg (*args, f64);
471 time_t t = timestamp;
473 msec = 1e6 * (timestamp - t);
474 return format (s, "%4d-%02d-%02dT%02d:%02d:%02d.%06dZ", 1900 + tm->tm_year,
475 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min,
480 format_vl_api_timedelta_t(u8 * s, va_list * args)
482 return format_vl_api_timestamp_t(s, args);
486 unformat_vl_api_timedelta_t(unformat_input_t * input, va_list * args)
492 unformat_vl_api_timestamp_t(unformat_input_t * input, va_list * args)
496 u8 *format_vl_api_gbp_scope_t(u8 * s, va_list * args)
500 uword unformat_vl_api_gbp_scope_t(unformat_input_t * input, va_list * args)
506 vl_api_ip4_address_with_prefix_t_tojson (vl_api_ip4_prefix_t *a) {
507 u8 *s = format(0, "%U", format_vl_api_ip4_address_t, a);
508 cJSON *o = cJSON_CreateString((char *)s);
513 vl_api_ip6_address_with_prefix_t_tojson (vl_api_ip6_prefix_t *a) {
514 u8 *s = format(0, "%U", format_vl_api_ip6_address_t, a);
515 cJSON *o = cJSON_CreateString((char *)s);
520 vl_api_address_with_prefix_t_tojson (vl_api_prefix_t *a) {
521 u8 *s = format(0, "%U", format_vl_api_address_t, a);
522 cJSON *o = cJSON_CreateString((char *)s);
527 format_vl_api_mac_address_t (u8 * s, va_list * args)
529 const mac_address_t *mac = va_arg (*args, mac_address_t *);
531 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
532 mac->bytes[0], mac->bytes[1], mac->bytes[2],
533 mac->bytes[3], mac->bytes[4], mac->bytes[5]);
536 cJSON *vl_api_ ##T## _t_tojson (vl_api_ ##T## _t *a) { \
537 u8 *s = format(0, "%U", format_vl_api_ ##T## _t, a); \
538 cJSON *o = cJSON_CreateString((char *)s); \