misc: vpe.api messages dynamically allocated
[vpp.git] / src / vppinfra / jsonformat.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
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"
22
23 #define _(T)                                                                  \
24   int vl_api_##T##_fromjson (cJSON *o, T *d)                                  \
25   {                                                                           \
26     if (!cJSON_IsNumber (o))                                                  \
27       return -1;                                                              \
28     d[0] = (T) cJSON_GetNumberValue (o);                                      \
29     return 0;                                                                 \
30   }
31 foreach_type_fromjson
32 #undef _
33
34   int
35   vl_api_bool_fromjson (cJSON *o, bool *d)
36 {
37     if (!cJSON_IsBool(o)) return -1;
38     *d = o->valueint ? true : false;
39     return 0;
40 }
41
42 int vl_api_u8_string_fromjson(cJSON *o, u8 *s, int len)
43 {
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))
48       return -1;
49     return 0;
50 }
51
52 u8 *
53 u8string_fromjson(cJSON *o, char *fieldname)
54 {
55     u8 *s = 0;
56     unformat_input_t input;
57     cJSON *item = cJSON_GetObjectItem(o, fieldname);
58     if (!item) {
59         printf("Illegal JSON, no such fieldname %s\n", fieldname);
60         return 0;
61     }
62
63     char *p = cJSON_GetStringValue(item);
64     unformat_init_string (&input, p, strlen(p));
65     if (!unformat (&input, "0x%U", unformat_hex_string, &s))
66       return 0;
67     return s;
68 }
69
70 int
71 u8string_fromjson2(cJSON *o, char *fieldname, u8 *data)
72 {
73     u8 *s = u8string_fromjson(o, fieldname);
74     if (!s)
75       return -1;
76     memcpy(data, s, vec_len(s));
77     vec_free(s);
78     return 0;
79 }
80
81 /* Parse an IP4 address %d.%d.%d.%d. */
82 uword
83 unformat_ip4_address (unformat_input_t * input, va_list * args)
84 {
85   u8 *result = va_arg (*args, u8 *);
86   unsigned a[4];
87
88   if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
89     return 0;
90
91   if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
92     return 0;
93
94   result[0] = a[0];
95   result[1] = a[1];
96   result[2] = a[2];
97   result[3] = a[3];
98
99   return 1;
100 }
101
102 /* Parse an IP6 address. */
103 uword
104 unformat_ip6_address (unformat_input_t * input, va_list * args)
105 {
106   ip6_address_t *result = va_arg (*args, ip6_address_t *);
107   u16 hex_quads[8];
108   uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
109   uword c, n_colon, double_colon_index;
110
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)
114     {
115       hex_digit = 16;
116       if (c >= '0' && c <= '9')
117         hex_digit = c - '0';
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)
123         n_colon++;
124       else
125         {
126           unformat_put_input (input);
127           break;
128         }
129
130       /* Too many hex quads. */
131       if (n_hex_quads >= ARRAY_LEN (hex_quads))
132         return 0;
133
134       if (hex_digit < 16)
135         {
136           hex_quad = (hex_quad << 4) | hex_digit;
137
138           /* Hex quad must fit in 16 bits. */
139           if (n_hex_digits >= 4)
140             return 0;
141
142           n_colon = 0;
143           n_hex_digits++;
144         }
145
146       /* Save position of :: */
147       if (n_colon == 2)
148         {
149           /* More than one :: ? */
150           if (double_colon_index < ARRAY_LEN (hex_quads))
151             return 0;
152           double_colon_index = n_hex_quads;
153         }
154
155       if (n_colon > 0 && n_hex_digits > 0)
156         {
157           hex_quads[n_hex_quads++] = hex_quad;
158           hex_quad = 0;
159           n_hex_digits = 0;
160         }
161     }
162
163   if (n_hex_digits > 0)
164     hex_quads[n_hex_quads++] = hex_quad;
165
166
167   {
168     word i;
169
170     /* Expand :: to appropriate number of zero hex quads. */
171     if (double_colon_index < ARRAY_LEN (hex_quads))
172       {
173         word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
174
175         for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
176           hex_quads[n_zero + i] = hex_quads[i];
177
178         for (i = 0; i < n_zero; i++)
179           {
180             ASSERT ((double_colon_index + i) < ARRAY_LEN (hex_quads));
181             hex_quads[double_colon_index + i] = 0;
182           }
183
184         n_hex_quads = ARRAY_LEN (hex_quads);
185       }
186
187     /* Too few hex quads given. */
188     if (n_hex_quads < ARRAY_LEN (hex_quads))
189       return 0;
190
191     for (i = 0; i < ARRAY_LEN (hex_quads); i++)
192       result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
193
194     return 1;
195   }
196 }
197
198 u8 *
199 format_ip6_address (u8 * s, va_list * args)
200 {
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;
206
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++)
209     {
210       if (a->as_u16[i] == 0)
211         {
212           if (in_zero_run)
213             this_zero_run++;
214           else
215             {
216               in_zero_run = 1;
217               this_zero_run = 1;
218               this_zero_run_index = i;
219             }
220         }
221       else
222         {
223           if (in_zero_run)
224             {
225               /* offer to compress the biggest run of > 1 zero */
226               if (this_zero_run > max_zero_run && this_zero_run > 1)
227                 {
228                   max_zero_run_index = this_zero_run_index;
229                   max_zero_run = this_zero_run;
230                 }
231             }
232           in_zero_run = 0;
233           this_zero_run = 0;
234         }
235     }
236
237   if (in_zero_run)
238     {
239       if (this_zero_run > max_zero_run && this_zero_run > 1)
240         {
241           max_zero_run_index = this_zero_run_index;
242           max_zero_run = this_zero_run;
243         }
244     }
245
246   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
247     {
248       if (i == max_zero_run_index)
249         {
250           s = format (s, "::");
251           i += max_zero_run - 1;
252           last_double_colon = 1;
253         }
254       else
255         {
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;
260         }
261     }
262
263   return s;
264 }
265
266 int
267 vl_api_ip4_address_t_fromjson (void **mp, int *len, cJSON *o,
268                                vl_api_ip4_address_t *a)
269 {
270     unformat_input_t input;
271     char *p = cJSON_GetStringValue(o);
272     if (!p)
273       return -1;
274     unformat_init_string (&input, p, strlen(p));
275     if (!unformat (&input, "%U", unformat_ip4_address, a))
276       return -1;
277     return 0;
278 }
279
280 int
281 vl_api_ip4_prefix_t_fromjson (void **mp, int *len, cJSON *o,
282                               vl_api_ip4_prefix_t *a)
283 {
284     unformat_input_t input;
285     char *p = cJSON_GetStringValue(o);
286     if (!p)
287       return -1;
288     unformat_init_string (&input, p, strlen(p));
289     if (!unformat (&input, "%U/%d", unformat_ip4_address, &a->address,
290                    &a->len))
291       return -1;
292     return 0;
293 }
294
295 int
296 vl_api_ip4_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
297                                            vl_api_ip4_prefix_t *a)
298 {
299   return vl_api_ip4_prefix_t_fromjson(mp, len, o, a);
300 }
301 int
302 vl_api_ip6_address_t_fromjson (void **mp, int *len, cJSON *o,
303                                vl_api_ip6_address_t *a)
304 {
305     unformat_input_t input;
306     char *p = cJSON_GetStringValue(o);
307     if (!p)
308       return -1;
309     unformat_init_string (&input, p, strlen(p));
310     if (!unformat (&input, "%U", unformat_ip6_address, a))
311       return -1;
312     return 0;
313 }
314
315 int
316 vl_api_ip6_prefix_t_fromjson (void **mp, int *len, cJSON *o,
317                               vl_api_ip6_prefix_t *a)
318 {
319   unformat_input_t input;
320   char *p = cJSON_GetStringValue(o);
321   if (!p)
322     return -1;
323   unformat_init_string (&input, p, strlen(p));
324   if (!unformat (&input, "%U/%d", unformat_ip6_address, &a->address, &a->len))
325     return -1;
326   return 0;
327 }
328
329 int
330 vl_api_ip6_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
331                                            vl_api_ip6_prefix_t *a)
332 {
333   return vl_api_ip6_prefix_t_fromjson(mp, len, o, a);
334 }
335
336 int
337 vl_api_address_t_fromjson (void **mp, int *len, cJSON *o, vl_api_address_t *a)
338 {
339   unformat_input_t input;
340
341   char *p = cJSON_GetStringValue(o);
342   if (!p)
343     return -1;
344   unformat_init_string (&input, p, strlen(p));
345   if (unformat (&input, "%U", unformat_ip4_address, &a->un.ip4))
346     a->af = ADDRESS_IP4;
347   else if (unformat (&input, "%U", unformat_ip6_address, &a->un.ip6))
348     a->af = ADDRESS_IP6;
349   else
350     return -1;
351   return 0;
352 }
353
354 int
355 vl_api_prefix_t_fromjson (void **mp, int *len, cJSON *o, vl_api_prefix_t *a)
356 {
357   unformat_input_t input;
358
359   char *p = cJSON_GetStringValue(o);
360
361   if (!p)
362     return -1;
363   unformat_init_string (&input, p, strlen(p));
364   int plen;
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;
369   else
370     return -1;
371   a->len = plen;
372   return 0;
373 }
374
375 int
376 vl_api_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
377                                        vl_api_prefix_t *a)
378 {
379   return vl_api_prefix_t_fromjson(mp, len, o, a);
380 }
381
382 uword
383 unformat_mac_address (unformat_input_t * input, va_list * args)
384 {
385   mac_address_t *mac = va_arg (*args, mac_address_t *);
386   u32 i, a[3];
387
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]))
391     return (1);
392   else if (unformat (input, "%_%x.%x.%x%_", &a[0], &a[1], &a[2]))
393     {
394       for (i = 0; i < ARRAY_LEN (a); i++)
395         if (a[i] >= (1 << 16))
396           return 0;
397
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;
404
405       return (1);
406     }
407   return (0);
408 }
409
410 int
411 vl_api_mac_address_t_fromjson (void **mp, int *len, cJSON *o,
412                                vl_api_mac_address_t *a)
413 {
414   unformat_input_t input;
415
416   char *p = cJSON_GetStringValue(o);
417   unformat_init_string (&input, p, strlen(p));
418   if (!unformat (&input, "%U", unformat_mac_address, a))
419     return -1;
420   return 0;
421 }
422
423 /* Format an IP4 address. */
424 u8 *
425 format_ip4_address (u8 * s, va_list * args)
426 {
427   u8 *a = va_arg (*args, u8 *);
428   return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
429 }
430
431 int
432 vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
433 {
434   /* copy without nul terminator */
435   u32 len = strlen (buf);
436   if (len > 0)
437     clib_memcpy_fast (str->buf, buf, len);
438   str->length = htonl (len);
439   return len + sizeof (u32);
440 }
441
442 void
443 vl_api_string_cJSON_AddToObject(cJSON * const object, const char * const name, vl_api_string_t *astr)
444 {
445
446     if (astr == 0) return;
447     u32 length = clib_net_to_host_u32 (astr->length);
448
449     char *cstr = malloc(length + 1);
450     memcpy(cstr, astr->buf, length);
451     cstr[length] = '\0';
452     cJSON_AddStringToObject(object, name, cstr);
453     free(cstr);
454 }
455
456 u8 *
457 format_vl_api_timestamp_t(u8 * s, va_list * args)
458 {
459     f64 timestamp = va_arg (*args, f64);
460     struct tm *tm;
461     word msec;
462
463     time_t t = timestamp;
464     tm = gmtime (&t);
465     msec = 1e6 * (timestamp - t);
466     return format (s, "%4d-%02d-%02dT%02d:%02d:%02d.%06dZ", 1900 + tm->tm_year,
467                    1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min,
468                    tm->tm_sec, msec);
469 }
470
471 u8 *
472 format_vl_api_timedelta_t(u8 * s, va_list * args)
473 {
474     return format_vl_api_timestamp_t(s, args);
475 }
476
477 uword
478 unformat_vl_api_timedelta_t(unformat_input_t * input, va_list * args)
479 {
480     return 0;
481 }
482
483 uword
484 unformat_vl_api_timestamp_t(unformat_input_t * input, va_list * args)
485 {
486     return 0;
487 }
488 uword unformat_vl_api_gbp_scope_t(unformat_input_t * input, va_list * args)
489 {
490     return 0;
491 }
492
493 cJSON *
494 vl_api_ip4_address_with_prefix_t_tojson (vl_api_ip4_prefix_t *a) {
495   return vl_api_ip4_prefix_t_tojson (a);
496 }
497 cJSON *
498 vl_api_ip6_address_with_prefix_t_tojson (vl_api_ip6_prefix_t *a) {
499   return vl_api_ip6_prefix_t_tojson (a);
500 }
501 cJSON *
502 vl_api_address_with_prefix_t_tojson (vl_api_prefix_t *a) {
503   return vl_api_prefix_t_tojson (a);
504 }
505 u8 *
506 format_vl_api_mac_address_t (u8 * s, va_list * args)
507 {
508   const mac_address_t *mac = va_arg (*args, mac_address_t *);
509
510   return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
511                  mac->bytes[0], mac->bytes[1], mac->bytes[2],
512                  mac->bytes[3], mac->bytes[4], mac->bytes[5]);
513 }
514 #define _(T)                                                \
515   cJSON *vl_api_ ##T## _t_tojson (vl_api_ ##T## _t *a) {   \
516   u8 *s = format(0, "%U", format_vl_api_ ##T## _t, a);      \
517   cJSON *o = cJSON_CreateString((char *)s);                 \
518   vec_free(s);                                              \
519   return o;                                                 \
520   }
521 foreach_type_tojson
522 #undef _