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