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 "jsonformat.h"
24 int vl_api_##T##_fromjson (cJSON *o, T *d) \
26 if (!cJSON_IsNumber (o)) \
28 d[0] = (T) cJSON_GetNumberValue (o); \
35 vl_api_bool_fromjson (cJSON *o, bool *d)
37 if (!cJSON_IsBool(o)) return -1;
38 *d = o->valueint ? true : false;
42 int vl_api_u8_string_fromjson(cJSON *o, u8 *s, int len)
44 unformat_input_t input;
45 char *p = cJSON_GetStringValue(o);
46 unformat_init_string (&input, p, strlen(p));
47 if (!unformat (&input, "0x%U", unformat_hex_string, s))
53 u8string_fromjson(cJSON *o, char *fieldname)
56 unformat_input_t input;
57 cJSON *item = cJSON_GetObjectItem(o, fieldname);
59 printf("Illegal JSON, no such fieldname %s\n", fieldname);
63 char *p = cJSON_GetStringValue(item);
64 unformat_init_string (&input, p, strlen(p));
65 if (!unformat (&input, "0x%U", unformat_hex_string, &s))
71 u8string_fromjson2(cJSON *o, char *fieldname, u8 *data)
73 u8 *s = u8string_fromjson(o, fieldname);
76 memcpy(data, s, vec_len(s));
81 /* Parse an IP4 address %d.%d.%d.%d. */
83 unformat_ip4_address (unformat_input_t * input, va_list * args)
85 u8 *result = va_arg (*args, u8 *);
88 if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
91 if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
102 /* Parse an IP6 address. */
104 unformat_ip6_address (unformat_input_t * input, va_list * args)
106 ip6_address_t *result = va_arg (*args, ip6_address_t *);
108 uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
109 uword c, n_colon, double_colon_index;
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)
116 if (c >= '0' && c <= '9')
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)
126 unformat_put_input (input);
130 /* Too many hex quads. */
131 if (n_hex_quads >= ARRAY_LEN (hex_quads))
136 hex_quad = (hex_quad << 4) | hex_digit;
138 /* Hex quad must fit in 16 bits. */
139 if (n_hex_digits >= 4)
146 /* Save position of :: */
149 /* More than one :: ? */
150 if (double_colon_index < ARRAY_LEN (hex_quads))
152 double_colon_index = n_hex_quads;
155 if (n_colon > 0 && n_hex_digits > 0)
157 hex_quads[n_hex_quads++] = hex_quad;
163 if (n_hex_digits > 0)
164 hex_quads[n_hex_quads++] = hex_quad;
170 /* Expand :: to appropriate number of zero hex quads. */
171 if (double_colon_index < ARRAY_LEN (hex_quads))
173 word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
175 for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
176 hex_quads[n_zero + i] = hex_quads[i];
178 for (i = 0; i < n_zero; i++)
180 ASSERT ((double_colon_index + i) < ARRAY_LEN (hex_quads));
181 hex_quads[double_colon_index + i] = 0;
184 n_hex_quads = ARRAY_LEN (hex_quads);
187 /* Too few hex quads given. */
188 if (n_hex_quads < ARRAY_LEN (hex_quads))
191 for (i = 0; i < ARRAY_LEN (hex_quads); i++)
192 result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
199 format_ip6_address (u8 * s, va_list * args)
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;
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++)
210 if (a->as_u16[i] == 0)
218 this_zero_run_index = i;
225 /* offer to compress the biggest run of > 1 zero */
226 if (this_zero_run > max_zero_run && this_zero_run > 1)
228 max_zero_run_index = this_zero_run_index;
229 max_zero_run = this_zero_run;
239 if (this_zero_run > max_zero_run && this_zero_run > 1)
241 max_zero_run_index = this_zero_run_index;
242 max_zero_run = this_zero_run;
246 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
248 if (i == max_zero_run_index)
250 s = format (s, "::");
251 i += max_zero_run - 1;
252 last_double_colon = 1;
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;
267 vl_api_ip4_address_t_fromjson (void **mp, int *len, cJSON *o,
268 vl_api_ip4_address_t *a)
270 unformat_input_t input;
271 char *p = cJSON_GetStringValue(o);
274 unformat_init_string (&input, p, strlen(p));
275 if (!unformat (&input, "%U", unformat_ip4_address, a))
281 vl_api_ip4_prefix_t_fromjson (void **mp, int *len, cJSON *o,
282 vl_api_ip4_prefix_t *a)
284 unformat_input_t input;
285 char *p = cJSON_GetStringValue(o);
288 unformat_init_string (&input, p, strlen(p));
289 if (!unformat (&input, "%U/%d", unformat_ip4_address, &a->address,
296 vl_api_ip4_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
297 vl_api_ip4_prefix_t *a)
299 return vl_api_ip4_prefix_t_fromjson(mp, len, o, a);
302 vl_api_ip6_address_t_fromjson (void **mp, int *len, cJSON *o,
303 vl_api_ip6_address_t *a)
305 unformat_input_t input;
306 char *p = cJSON_GetStringValue(o);
309 unformat_init_string (&input, p, strlen(p));
310 if (!unformat (&input, "%U", unformat_ip6_address, a))
316 vl_api_ip6_prefix_t_fromjson (void **mp, int *len, cJSON *o,
317 vl_api_ip6_prefix_t *a)
319 unformat_input_t input;
320 char *p = cJSON_GetStringValue(o);
323 unformat_init_string (&input, p, strlen(p));
324 if (!unformat (&input, "%U/%d", unformat_ip6_address, &a->address, &a->len))
330 vl_api_ip6_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
331 vl_api_ip6_prefix_t *a)
333 return vl_api_ip6_prefix_t_fromjson(mp, len, o, a);
337 vl_api_address_t_fromjson (void **mp, int *len, cJSON *o, vl_api_address_t *a)
339 unformat_input_t input;
341 char *p = cJSON_GetStringValue(o);
344 unformat_init_string (&input, p, strlen(p));
345 if (unformat (&input, "%U", unformat_ip4_address, &a->un.ip4))
347 else if (unformat (&input, "%U", unformat_ip6_address, &a->un.ip6))
355 vl_api_prefix_t_fromjson (void **mp, int *len, cJSON *o, vl_api_prefix_t *a)
357 unformat_input_t input;
359 char *p = cJSON_GetStringValue(o);
363 unformat_init_string (&input, p, strlen(p));
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;
376 vl_api_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
379 return vl_api_prefix_t_fromjson(mp, len, o, a);
383 unformat_mac_address (unformat_input_t * input, va_list * args)
385 mac_address_t *mac = va_arg (*args, mac_address_t *);
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]))
392 else if (unformat (input, "%_%x.%x.%x%_", &a[0], &a[1], &a[2]))
394 for (i = 0; i < ARRAY_LEN (a); i++)
395 if (a[i] >= (1 << 16))
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;
411 vl_api_mac_address_t_fromjson (void **mp, int *len, cJSON *o,
412 vl_api_mac_address_t *a)
414 unformat_input_t input;
416 char *p = cJSON_GetStringValue(o);
417 unformat_init_string (&input, p, strlen(p));
418 if (!unformat (&input, "%U", unformat_mac_address, a))
423 /* Format an IP4 address. */
425 format_ip4_address (u8 * s, va_list * args)
427 u8 *a = va_arg (*args, u8 *);
428 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
432 vl_api_string_cJSON_AddToObject(cJSON * const object, const char * const name, vl_api_string_t *astr)
435 if (astr == 0) return;
436 u32 length = clib_net_to_host_u32 (astr->length);
438 char *cstr = malloc(length + 1);
439 memcpy(cstr, astr->buf, length);
441 cJSON_AddStringToObject(object, name, cstr);
446 format_vl_api_timestamp_t(u8 * s, va_list * args)
448 f64 timestamp = va_arg (*args, f64);
452 time_t t = timestamp;
454 msec = 1e6 * (timestamp - t);
455 return format (s, "%4d-%02d-%02dT%02d:%02d:%02d.%06dZ", 1900 + tm->tm_year,
456 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min,
461 format_vl_api_timedelta_t(u8 * s, va_list * args)
463 return format_vl_api_timestamp_t(s, args);
467 unformat_vl_api_timedelta_t(unformat_input_t * input, va_list * args)
473 unformat_vl_api_timestamp_t(unformat_input_t * input, va_list * args)
477 uword unformat_vl_api_gbp_scope_t(unformat_input_t * input, va_list * args)
483 vl_api_ip4_address_with_prefix_t_tojson (vl_api_ip4_prefix_t *a) {
484 return vl_api_ip4_prefix_t_tojson (a);
487 vl_api_ip6_address_with_prefix_t_tojson (vl_api_ip6_prefix_t *a) {
488 return vl_api_ip6_prefix_t_tojson (a);
491 vl_api_address_with_prefix_t_tojson (vl_api_prefix_t *a) {
492 return vl_api_prefix_t_tojson (a);
495 format_vl_api_mac_address_t (u8 * s, va_list * args)
497 const mac_address_t *mac = va_arg (*args, mac_address_t *);
499 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
500 mac->bytes[0], mac->bytes[1], mac->bytes[2],
501 mac->bytes[3], mac->bytes[4], mac->bytes[5]);
504 cJSON *vl_api_ ##T## _t_tojson (vl_api_ ##T## _t *a) { \
505 u8 *s = format(0, "%U", format_vl_api_ ##T## _t, a); \
506 cJSON *o = cJSON_CreateString((char *)s); \