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);
74 memcpy(data, s, vec_len(s));
79 /* Parse an IP4 address %d.%d.%d.%d. */
81 unformat_ip4_address (unformat_input_t * input, va_list * args)
83 u8 *result = va_arg (*args, u8 *);
86 if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
89 if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
100 /* Parse an IP6 address. */
102 unformat_ip6_address (unformat_input_t * input, va_list * args)
104 ip6_address_t *result = va_arg (*args, ip6_address_t *);
106 uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
107 uword c, n_colon, double_colon_index;
109 n_hex_quads = hex_quad = n_hex_digits = n_colon = 0;
110 double_colon_index = ARRAY_LEN (hex_quads);
111 while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
114 if (c >= '0' && c <= '9')
116 else if (c >= 'a' && c <= 'f')
117 hex_digit = c + 10 - 'a';
118 else if (c >= 'A' && c <= 'F')
119 hex_digit = c + 10 - 'A';
120 else if (c == ':' && n_colon < 2)
124 unformat_put_input (input);
128 /* Too many hex quads. */
129 if (n_hex_quads >= ARRAY_LEN (hex_quads))
134 hex_quad = (hex_quad << 4) | hex_digit;
136 /* Hex quad must fit in 16 bits. */
137 if (n_hex_digits >= 4)
144 /* Save position of :: */
147 /* More than one :: ? */
148 if (double_colon_index < ARRAY_LEN (hex_quads))
150 double_colon_index = n_hex_quads;
153 if (n_colon > 0 && n_hex_digits > 0)
155 hex_quads[n_hex_quads++] = hex_quad;
161 if (n_hex_digits > 0)
162 hex_quads[n_hex_quads++] = hex_quad;
168 /* Expand :: to appropriate number of zero hex quads. */
169 if (double_colon_index < ARRAY_LEN (hex_quads))
171 word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
173 for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
174 hex_quads[n_zero + i] = hex_quads[i];
176 for (i = 0; i < n_zero; i++)
178 ASSERT ((double_colon_index + i) < ARRAY_LEN (hex_quads));
179 hex_quads[double_colon_index + i] = 0;
182 n_hex_quads = ARRAY_LEN (hex_quads);
185 /* Too few hex quads given. */
186 if (n_hex_quads < ARRAY_LEN (hex_quads))
189 for (i = 0; i < ARRAY_LEN (hex_quads); i++)
190 result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
197 format_ip6_address (u8 * s, va_list * args)
199 ip6_address_t *a = va_arg (*args, ip6_address_t *);
200 u32 max_zero_run = 0, this_zero_run = 0;
201 int max_zero_run_index = -1, this_zero_run_index = 0;
202 int in_zero_run = 0, i;
203 int last_double_colon = 0;
205 /* Ugh, this is a pain. Scan forward looking for runs of 0's */
206 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
208 if (a->as_u16[i] == 0)
216 this_zero_run_index = i;
223 /* offer to compress the biggest run of > 1 zero */
224 if (this_zero_run > max_zero_run && this_zero_run > 1)
226 max_zero_run_index = this_zero_run_index;
227 max_zero_run = this_zero_run;
237 if (this_zero_run > max_zero_run && this_zero_run > 1)
239 max_zero_run_index = this_zero_run_index;
240 max_zero_run = this_zero_run;
244 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
246 if (i == max_zero_run_index)
248 s = format (s, "::");
249 i += max_zero_run - 1;
250 last_double_colon = 1;
254 s = format (s, "%s%x",
255 (last_double_colon || i == 0) ? "" : ":",
256 clib_net_to_host_u16 (a->as_u16[i]));
257 last_double_colon = 0;
265 vl_api_ip4_address_t_fromjson (void **mp, int *len, cJSON *o,
266 vl_api_ip4_address_t *a)
268 unformat_input_t input;
269 char *p = cJSON_GetStringValue(o);
272 unformat_init_string (&input, p, strlen(p));
273 unformat(&input, "%U", unformat_ip4_address, a);
278 vl_api_ip4_prefix_t_fromjson (void **mp, int *len, cJSON *o,
279 vl_api_ip4_prefix_t *a)
281 unformat_input_t input;
282 char *p = cJSON_GetStringValue(o);
285 unformat_init_string (&input, p, strlen(p));
286 unformat(&input, "%U/%d", unformat_ip4_address, &a->address, &a->len);
291 vl_api_ip4_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
292 vl_api_ip4_prefix_t *a)
294 return vl_api_ip4_prefix_t_fromjson(mp, len, o, a);
297 vl_api_ip6_address_t_fromjson (void **mp, int *len, cJSON *o,
298 vl_api_ip6_address_t *a)
300 unformat_input_t input;
301 char *p = cJSON_GetStringValue(o);
304 unformat_init_string (&input, p, strlen(p));
305 unformat(&input, "%U", unformat_ip6_address, a);
310 vl_api_ip6_prefix_t_fromjson (void **mp, int *len, cJSON *o,
311 vl_api_ip6_prefix_t *a)
313 unformat_input_t input;
314 char *p = cJSON_GetStringValue(o);
317 unformat_init_string (&input, p, strlen(p));
318 unformat(&input, "%U/%d", unformat_ip6_address, &a->address, &a->len);
323 vl_api_ip6_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
324 vl_api_ip6_prefix_t *a)
326 return vl_api_ip6_prefix_t_fromjson(mp, len, o, a);
330 vl_api_address_t_fromjson (void **mp, int *len, cJSON *o, vl_api_address_t *a)
332 unformat_input_t input;
334 char *p = cJSON_GetStringValue(o);
337 unformat_init_string (&input, p, strlen(p));
338 if (unformat (&input, "%U", unformat_ip4_address, &a->un.ip4))
340 else if (unformat (&input, "%U", unformat_ip6_address, &a->un.ip6))
348 vl_api_prefix_t_fromjson (void **mp, int *len, cJSON *o, vl_api_prefix_t *a)
350 unformat_input_t input;
352 char *p = cJSON_GetStringValue(o);
356 unformat_init_string (&input, p, strlen(p));
358 if (unformat (&input, "%U/%d", unformat_ip4_address, &a->address.un.ip4, &plen))
359 a->address.af = ADDRESS_IP4;
360 else if (unformat (&input, "%U/%d", unformat_ip6_address, &a->address.un.ip6, &plen))
361 a->address.af = ADDRESS_IP6;
369 vl_api_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
372 return vl_api_prefix_t_fromjson(mp, len, o, a);
376 unformat_mac_address (unformat_input_t * input, va_list * args)
378 mac_address_t *mac = va_arg (*args, mac_address_t *);
381 if (unformat (input, "%_%X:%X:%X:%X:%X:%X%_",
382 1, &mac->bytes[0], 1, &mac->bytes[1], 1, &mac->bytes[2],
383 1, &mac->bytes[3], 1, &mac->bytes[4], 1, &mac->bytes[5]))
385 else if (unformat (input, "%_%x.%x.%x%_", &a[0], &a[1], &a[2]))
387 for (i = 0; i < ARRAY_LEN (a); i++)
388 if (a[i] >= (1 << 16))
391 mac->bytes[0] = (a[0] >> 8) & 0xff;
392 mac->bytes[1] = (a[0] >> 0) & 0xff;
393 mac->bytes[2] = (a[1] >> 8) & 0xff;
394 mac->bytes[3] = (a[1] >> 0) & 0xff;
395 mac->bytes[4] = (a[2] >> 8) & 0xff;
396 mac->bytes[5] = (a[2] >> 0) & 0xff;
404 vl_api_mac_address_t_fromjson (void **mp, int *len, cJSON *o,
405 vl_api_mac_address_t *a)
407 unformat_input_t input;
409 char *p = cJSON_GetStringValue(o);
410 unformat_init_string (&input, p, strlen(p));
411 unformat(&input, "%U", unformat_mac_address, a);
415 /* Format an IP4 address. */
417 format_ip4_address (u8 * s, va_list * args)
419 u8 *a = va_arg (*args, u8 *);
420 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
424 vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
426 /* copy without nul terminator */
427 u32 len = strlen (buf);
429 clib_memcpy_fast (str->buf, buf, len);
430 str->length = htonl (len);
431 return len + sizeof (u32);
435 format_vl_api_interface_index_t (u8 *s, va_list *args)
437 u32 *a = va_arg (*args, u32 *);
438 return format (s, "%u", *a);
442 unformat_vl_api_interface_index_t (unformat_input_t * input, va_list * args)
444 u32 *a = va_arg (*args, u32 *);
446 if (!unformat (input, "%u", a))
452 vl_api_string_cJSON_AddToObject(cJSON * const object, const char * const name, vl_api_string_t *astr)
455 if (astr == 0) return;
456 u32 length = clib_net_to_host_u32 (astr->length);
458 char *cstr = malloc(length + 1);
459 memcpy(cstr, astr->buf, length);
461 cJSON_AddStringToObject(object, name, cstr);
466 format_vl_api_timestamp_t(u8 * s, va_list * args)
468 f64 timestamp = va_arg (*args, f64);
472 time_t t = timestamp;
474 msec = 1e6 * (timestamp - t);
475 return format (s, "%4d-%02d-%02dT%02d:%02d:%02d.%06dZ", 1900 + tm->tm_year,
476 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min,
481 format_vl_api_timedelta_t(u8 * s, va_list * args)
483 return format_vl_api_timestamp_t(s, args);
487 unformat_vl_api_timedelta_t(unformat_input_t * input, va_list * args)
493 unformat_vl_api_timestamp_t(unformat_input_t * input, va_list * args)
497 u8 *format_vl_api_gbp_scope_t(u8 * s, va_list * args)
501 uword unformat_vl_api_gbp_scope_t(unformat_input_t * input, va_list * args)
507 vl_api_ip4_address_with_prefix_t_tojson (vl_api_ip4_prefix_t *a) {
508 u8 *s = format(0, "%U", format_vl_api_ip4_address_t, a);
509 cJSON *o = cJSON_CreateString((char *)s);
514 vl_api_ip6_address_with_prefix_t_tojson (vl_api_ip6_prefix_t *a) {
515 u8 *s = format(0, "%U", format_vl_api_ip6_address_t, a);
516 cJSON *o = cJSON_CreateString((char *)s);
521 vl_api_address_with_prefix_t_tojson (vl_api_prefix_t *a) {
522 u8 *s = format(0, "%U", format_vl_api_address_t, a);
523 cJSON *o = cJSON_CreateString((char *)s);
528 format_vl_api_mac_address_t (u8 * s, va_list * args)
530 const mac_address_t *mac = va_arg (*args, mac_address_t *);
532 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
533 mac->bytes[0], mac->bytes[1], mac->bytes[2],
534 mac->bytes[3], mac->bytes[4], mac->bytes[5]);
537 cJSON *vl_api_ ##T## _t_tojson (vl_api_ ##T## _t *a) { \
538 u8 *s = format(0, "%U", format_vl_api_ ##T## _t, a); \
539 cJSON *o = cJSON_CreateString((char *)s); \