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;
261 void *vl_api_ip4_address_t_fromjson(void *mp, int *len, cJSON *o, vl_api_ip4_address_t *a)
263 unformat_input_t input;
264 char *p = cJSON_GetStringValue(o);
266 unformat_init_string (&input, p, strlen(p));
267 unformat(&input, "%U", unformat_ip4_address, a);
271 void *vl_api_ip4_prefix_t_fromjson(void *mp, int *len, cJSON *o, vl_api_ip4_prefix_t *a)
273 unformat_input_t input;
274 char *p = cJSON_GetStringValue(o);
276 unformat_init_string (&input, p, strlen(p));
277 unformat(&input, "%U/%d", unformat_ip4_address, &a->address, &a->len);
281 void *vl_api_ip4_address_with_prefix_t_fromjson(void *mp, int *len, cJSON *o, vl_api_ip4_prefix_t *a)
283 return vl_api_ip4_prefix_t_fromjson(mp, len, o, a);
285 void *vl_api_ip6_address_t_fromjson(void *mp, int *len, cJSON *o, vl_api_ip6_address_t *a)
287 unformat_input_t input;
288 char *p = cJSON_GetStringValue(o);
290 unformat_init_string (&input, p, strlen(p));
291 unformat(&input, "%U", unformat_ip6_address, a);
295 void *vl_api_ip6_prefix_t_fromjson(void *mp, int *len, cJSON *o, vl_api_ip6_prefix_t *a)
297 unformat_input_t input;
298 char *p = cJSON_GetStringValue(o);
300 unformat_init_string (&input, p, strlen(p));
301 unformat(&input, "%U/%d", unformat_ip6_address, &a->address, &a->len);
305 void *vl_api_ip6_address_with_prefix_t_fromjson(void *mp, int *len, cJSON *o, vl_api_ip6_prefix_t *a)
307 return vl_api_ip6_prefix_t_fromjson(mp, len, o, a);
310 void *vl_api_address_t_fromjson(void *mp, int *len, cJSON *o, vl_api_address_t *a)
312 unformat_input_t input;
314 char *p = cJSON_GetStringValue(o);
316 unformat_init_string (&input, p, strlen(p));
317 if (unformat (&input, "%U", unformat_ip4_address, &a->un.ip4))
319 else if (unformat (&input, "%U", unformat_ip6_address, &a->un.ip6))
326 void *vl_api_prefix_t_fromjson(void *mp, int *len, cJSON *o, vl_api_prefix_t *a)
328 unformat_input_t input;
330 char *p = cJSON_GetStringValue(o);
333 unformat_init_string (&input, p, strlen(p));
335 if (unformat (&input, "%U/%d", unformat_ip4_address, &a->address.un.ip4, &plen))
336 a->address.af = ADDRESS_IP4;
337 else if (unformat (&input, "%U/%d", unformat_ip6_address, &a->address.un.ip6, &plen))
338 a->address.af = ADDRESS_IP6;
345 void *vl_api_address_with_prefix_t_fromjson(void *mp, int *len, cJSON *o, vl_api_prefix_t *a)
347 return vl_api_prefix_t_fromjson(mp, len, o, a);
351 unformat_mac_address (unformat_input_t * input, va_list * args)
353 mac_address_t *mac = va_arg (*args, mac_address_t *);
356 if (unformat (input, "%_%X:%X:%X:%X:%X:%X%_",
357 1, &mac->bytes[0], 1, &mac->bytes[1], 1, &mac->bytes[2],
358 1, &mac->bytes[3], 1, &mac->bytes[4], 1, &mac->bytes[5]))
360 else if (unformat (input, "%_%x.%x.%x%_", &a[0], &a[1], &a[2]))
362 for (i = 0; i < ARRAY_LEN (a); i++)
363 if (a[i] >= (1 << 16))
366 mac->bytes[0] = (a[0] >> 8) & 0xff;
367 mac->bytes[1] = (a[0] >> 0) & 0xff;
368 mac->bytes[2] = (a[1] >> 8) & 0xff;
369 mac->bytes[3] = (a[1] >> 0) & 0xff;
370 mac->bytes[4] = (a[2] >> 8) & 0xff;
371 mac->bytes[5] = (a[2] >> 0) & 0xff;
378 void *vl_api_mac_address_t_fromjson(void *mp, int *len, cJSON *o, vl_api_mac_address_t *a)
380 unformat_input_t input;
382 char *p = cJSON_GetStringValue(o);
383 unformat_init_string (&input, p, strlen(p));
384 unformat(&input, "%U", unformat_mac_address, a);
388 /* Format an IP4 address. */
390 format_ip4_address (u8 * s, va_list * args)
392 u8 *a = va_arg (*args, u8 *);
393 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
397 vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
399 /* copy without nul terminator */
400 u32 len = strlen (buf);
402 clib_memcpy_fast (str->buf, buf, len);
403 str->length = htonl (len);
404 return len + sizeof (u32);
408 format_vl_api_interface_index_t (u8 *s, va_list *args)
410 u32 *a = va_arg (*args, u32 *);
411 return format (s, "%u", *a);
415 unformat_vl_api_interface_index_t (unformat_input_t * input, va_list * args)
417 u32 *a = va_arg (*args, u32 *);
419 if (!unformat (input, "%u", a))
425 vl_api_string_cJSON_AddToObject(cJSON * const object, const char * const name, vl_api_string_t *astr)
428 if (astr == 0) return;
429 u32 length = clib_net_to_host_u32 (astr->length);
431 char *cstr = malloc(length + 1);
432 memcpy(cstr, astr->buf, length);
434 cJSON_AddStringToObject(object, name, cstr);
439 format_vl_api_timestamp_t(u8 * s, va_list * args)
441 f64 timestamp = va_arg (*args, f64);
445 time_t t = timestamp;
447 msec = 1e6 * (timestamp - t);
448 return format (s, "%4d-%02d-%02dT%02d:%02d:%02d.%06dZ", 1900 + tm->tm_year,
449 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min,
454 format_vl_api_timedelta_t(u8 * s, va_list * args)
456 return format_vl_api_timestamp_t(s, args);
460 unformat_vl_api_timedelta_t(unformat_input_t * input, va_list * args)
466 unformat_vl_api_timestamp_t(unformat_input_t * input, va_list * args)
470 u8 *format_vl_api_gbp_scope_t(u8 * s, va_list * args)
474 uword unformat_vl_api_gbp_scope_t(unformat_input_t * input, va_list * args)
480 vl_api_ip4_address_with_prefix_t_tojson (vl_api_ip4_prefix_t *a) {
481 u8 *s = format(0, "%U", format_vl_api_ip4_address_t, a);
482 cJSON *o = cJSON_CreateString((char *)s);
487 vl_api_ip6_address_with_prefix_t_tojson (vl_api_ip6_prefix_t *a) {
488 u8 *s = format(0, "%U", format_vl_api_ip6_address_t, a);
489 cJSON *o = cJSON_CreateString((char *)s);
494 vl_api_address_with_prefix_t_tojson (vl_api_prefix_t *a) {
495 u8 *s = format(0, "%U", format_vl_api_address_t, a);
496 cJSON *o = cJSON_CreateString((char *)s);
501 format_vl_api_mac_address_t (u8 * s, va_list * args)
503 const mac_address_t *mac = va_arg (*args, mac_address_t *);
505 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
506 mac->bytes[0], mac->bytes[1], mac->bytes[2],
507 mac->bytes[3], mac->bytes[4], mac->bytes[5]);
510 cJSON *vl_api_ ##T## _t_tojson (vl_api_ ##T## _t *a) { \
511 u8 *s = format(0, "%U", format_vl_api_ ##T## _t, a); \
512 cJSON *o = cJSON_CreateString((char *)s); \