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 unformat(&input, "0x%U", unformat_hex_string, s);
50 u8string_fromjson(cJSON *o, char *fieldname)
53 unformat_input_t input;
54 cJSON *item = cJSON_GetObjectItem(o, fieldname);
56 printf("Illegal JSON, no such fieldname %s\n", fieldname);
60 char *p = cJSON_GetStringValue(item);
61 unformat_init_string (&input, p, strlen(p));
62 unformat(&input, "0x%U", unformat_hex_string, &s);
67 u8string_fromjson2(cJSON *o, char *fieldname, u8 *data)
69 u8 *s = u8string_fromjson(o, fieldname);
71 memcpy(data, s, vec_len(s));
76 /* Parse an IP4 address %d.%d.%d.%d. */
78 unformat_ip4_address (unformat_input_t * input, va_list * args)
80 u8 *result = va_arg (*args, u8 *);
83 if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
86 if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
97 /* Parse an IP6 address. */
99 unformat_ip6_address (unformat_input_t * input, va_list * args)
101 ip6_address_t *result = va_arg (*args, ip6_address_t *);
103 uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
104 uword c, n_colon, double_colon_index;
106 n_hex_quads = hex_quad = n_hex_digits = n_colon = 0;
107 double_colon_index = ARRAY_LEN (hex_quads);
108 while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
111 if (c >= '0' && c <= '9')
113 else if (c >= 'a' && c <= 'f')
114 hex_digit = c + 10 - 'a';
115 else if (c >= 'A' && c <= 'F')
116 hex_digit = c + 10 - 'A';
117 else if (c == ':' && n_colon < 2)
121 unformat_put_input (input);
125 /* Too many hex quads. */
126 if (n_hex_quads >= ARRAY_LEN (hex_quads))
131 hex_quad = (hex_quad << 4) | hex_digit;
133 /* Hex quad must fit in 16 bits. */
134 if (n_hex_digits >= 4)
141 /* Save position of :: */
144 /* More than one :: ? */
145 if (double_colon_index < ARRAY_LEN (hex_quads))
147 double_colon_index = n_hex_quads;
150 if (n_colon > 0 && n_hex_digits > 0)
152 hex_quads[n_hex_quads++] = hex_quad;
158 if (n_hex_digits > 0)
159 hex_quads[n_hex_quads++] = hex_quad;
165 /* Expand :: to appropriate number of zero hex quads. */
166 if (double_colon_index < ARRAY_LEN (hex_quads))
168 word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
170 for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
171 hex_quads[n_zero + i] = hex_quads[i];
173 for (i = 0; i < n_zero; i++)
175 ASSERT ((double_colon_index + i) < ARRAY_LEN (hex_quads));
176 hex_quads[double_colon_index + i] = 0;
179 n_hex_quads = ARRAY_LEN (hex_quads);
182 /* Too few hex quads given. */
183 if (n_hex_quads < ARRAY_LEN (hex_quads))
186 for (i = 0; i < ARRAY_LEN (hex_quads); i++)
187 result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
194 format_ip6_address (u8 * s, va_list * args)
196 ip6_address_t *a = va_arg (*args, ip6_address_t *);
197 u32 max_zero_run = 0, this_zero_run = 0;
198 int max_zero_run_index = -1, this_zero_run_index = 0;
199 int in_zero_run = 0, i;
200 int last_double_colon = 0;
202 /* Ugh, this is a pain. Scan forward looking for runs of 0's */
203 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
205 if (a->as_u16[i] == 0)
213 this_zero_run_index = i;
220 /* offer to compress the biggest run of > 1 zero */
221 if (this_zero_run > max_zero_run && this_zero_run > 1)
223 max_zero_run_index = this_zero_run_index;
224 max_zero_run = this_zero_run;
234 if (this_zero_run > max_zero_run && this_zero_run > 1)
236 max_zero_run_index = this_zero_run_index;
237 max_zero_run = this_zero_run;
241 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
243 if (i == max_zero_run_index)
245 s = format (s, "::");
246 i += max_zero_run - 1;
247 last_double_colon = 1;
251 s = format (s, "%s%x",
252 (last_double_colon || i == 0) ? "" : ":",
253 clib_net_to_host_u16 (a->as_u16[i]));
254 last_double_colon = 0;
262 vl_api_ip4_address_t_fromjson (void **mp, int *len, cJSON *o,
263 vl_api_ip4_address_t *a)
265 unformat_input_t input;
266 char *p = cJSON_GetStringValue(o);
269 unformat_init_string (&input, p, strlen(p));
270 unformat(&input, "%U", unformat_ip4_address, a);
275 vl_api_ip4_prefix_t_fromjson (void **mp, int *len, cJSON *o,
276 vl_api_ip4_prefix_t *a)
278 unformat_input_t input;
279 char *p = cJSON_GetStringValue(o);
282 unformat_init_string (&input, p, strlen(p));
283 unformat(&input, "%U/%d", unformat_ip4_address, &a->address, &a->len);
288 vl_api_ip4_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
289 vl_api_ip4_prefix_t *a)
291 return vl_api_ip4_prefix_t_fromjson(mp, len, o, a);
294 vl_api_ip6_address_t_fromjson (void **mp, int *len, cJSON *o,
295 vl_api_ip6_address_t *a)
297 unformat_input_t input;
298 char *p = cJSON_GetStringValue(o);
301 unformat_init_string (&input, p, strlen(p));
302 unformat(&input, "%U", unformat_ip6_address, a);
307 vl_api_ip6_prefix_t_fromjson (void **mp, int *len, cJSON *o,
308 vl_api_ip6_prefix_t *a)
310 unformat_input_t input;
311 char *p = cJSON_GetStringValue(o);
314 unformat_init_string (&input, p, strlen(p));
315 unformat(&input, "%U/%d", unformat_ip6_address, &a->address, &a->len);
320 vl_api_ip6_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
321 vl_api_ip6_prefix_t *a)
323 return vl_api_ip6_prefix_t_fromjson(mp, len, o, a);
327 vl_api_address_t_fromjson (void **mp, int *len, cJSON *o, vl_api_address_t *a)
329 unformat_input_t input;
331 char *p = cJSON_GetStringValue(o);
334 unformat_init_string (&input, p, strlen(p));
335 if (unformat (&input, "%U", unformat_ip4_address, &a->un.ip4))
337 else if (unformat (&input, "%U", unformat_ip6_address, &a->un.ip6))
345 vl_api_prefix_t_fromjson (void **mp, int *len, cJSON *o, vl_api_prefix_t *a)
347 unformat_input_t input;
349 char *p = cJSON_GetStringValue(o);
353 unformat_init_string (&input, p, strlen(p));
355 if (unformat (&input, "%U/%d", unformat_ip4_address, &a->address.un.ip4, &plen))
356 a->address.af = ADDRESS_IP4;
357 else if (unformat (&input, "%U/%d", unformat_ip6_address, &a->address.un.ip6, &plen))
358 a->address.af = ADDRESS_IP6;
366 vl_api_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
369 return vl_api_prefix_t_fromjson(mp, len, o, a);
373 unformat_mac_address (unformat_input_t * input, va_list * args)
375 mac_address_t *mac = va_arg (*args, mac_address_t *);
378 if (unformat (input, "%_%X:%X:%X:%X:%X:%X%_",
379 1, &mac->bytes[0], 1, &mac->bytes[1], 1, &mac->bytes[2],
380 1, &mac->bytes[3], 1, &mac->bytes[4], 1, &mac->bytes[5]))
382 else if (unformat (input, "%_%x.%x.%x%_", &a[0], &a[1], &a[2]))
384 for (i = 0; i < ARRAY_LEN (a); i++)
385 if (a[i] >= (1 << 16))
388 mac->bytes[0] = (a[0] >> 8) & 0xff;
389 mac->bytes[1] = (a[0] >> 0) & 0xff;
390 mac->bytes[2] = (a[1] >> 8) & 0xff;
391 mac->bytes[3] = (a[1] >> 0) & 0xff;
392 mac->bytes[4] = (a[2] >> 8) & 0xff;
393 mac->bytes[5] = (a[2] >> 0) & 0xff;
401 vl_api_mac_address_t_fromjson (void **mp, int *len, cJSON *o,
402 vl_api_mac_address_t *a)
404 unformat_input_t input;
406 char *p = cJSON_GetStringValue(o);
407 unformat_init_string (&input, p, strlen(p));
408 unformat(&input, "%U", unformat_mac_address, a);
412 /* Format an IP4 address. */
414 format_ip4_address (u8 * s, va_list * args)
416 u8 *a = va_arg (*args, u8 *);
417 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
421 vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
423 /* copy without nul terminator */
424 u32 len = strlen (buf);
426 clib_memcpy_fast (str->buf, buf, len);
427 str->length = htonl (len);
428 return len + sizeof (u32);
432 format_vl_api_interface_index_t (u8 *s, va_list *args)
434 u32 *a = va_arg (*args, u32 *);
435 return format (s, "%u", *a);
439 unformat_vl_api_interface_index_t (unformat_input_t * input, va_list * args)
441 u32 *a = va_arg (*args, u32 *);
443 if (!unformat (input, "%u", a))
449 vl_api_string_cJSON_AddToObject(cJSON * const object, const char * const name, vl_api_string_t *astr)
452 if (astr == 0) return;
453 u32 length = clib_net_to_host_u32 (astr->length);
455 char *cstr = malloc(length + 1);
456 memcpy(cstr, astr->buf, length);
458 cJSON_AddStringToObject(object, name, cstr);
463 format_vl_api_timestamp_t(u8 * s, va_list * args)
465 f64 timestamp = va_arg (*args, f64);
469 time_t t = timestamp;
471 msec = 1e6 * (timestamp - t);
472 return format (s, "%4d-%02d-%02dT%02d:%02d:%02d.%06dZ", 1900 + tm->tm_year,
473 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min,
478 format_vl_api_timedelta_t(u8 * s, va_list * args)
480 return format_vl_api_timestamp_t(s, args);
484 unformat_vl_api_timedelta_t(unformat_input_t * input, va_list * args)
490 unformat_vl_api_timestamp_t(unformat_input_t * input, va_list * args)
494 u8 *format_vl_api_gbp_scope_t(u8 * s, va_list * args)
498 uword unformat_vl_api_gbp_scope_t(unformat_input_t * input, va_list * args)
504 vl_api_ip4_address_with_prefix_t_tojson (vl_api_ip4_prefix_t *a) {
505 u8 *s = format(0, "%U", format_vl_api_ip4_address_t, a);
506 cJSON *o = cJSON_CreateString((char *)s);
511 vl_api_ip6_address_with_prefix_t_tojson (vl_api_ip6_prefix_t *a) {
512 u8 *s = format(0, "%U", format_vl_api_ip6_address_t, a);
513 cJSON *o = cJSON_CreateString((char *)s);
518 vl_api_address_with_prefix_t_tojson (vl_api_prefix_t *a) {
519 u8 *s = format(0, "%U", format_vl_api_address_t, a);
520 cJSON *o = cJSON_CreateString((char *)s);
525 format_vl_api_mac_address_t (u8 * s, va_list * args)
527 const mac_address_t *mac = va_arg (*args, mac_address_t *);
529 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
530 mac->bytes[0], mac->bytes[1], mac->bytes[2],
531 mac->bytes[3], mac->bytes[4], mac->bytes[5]);
534 cJSON *vl_api_ ##T## _t_tojson (vl_api_ ##T## _t *a) { \
535 u8 *s = format(0, "%U", format_vl_api_ ##T## _t, a); \
536 cJSON *o = cJSON_CreateString((char *)s); \