Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 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> |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 21 | #include "jsonformat.h" |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 22 | |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 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 |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 32 | #undef _ |
| 33 | |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 34 | int |
| 35 | vl_api_bool_fromjson (cJSON *o, bool *d) |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 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)); |
Ole Troan | 5993a34 | 2021-02-16 00:42:21 +0100 | [diff] [blame] | 47 | if (!unformat (&input, "0x%U", unformat_hex_string, s)) |
| 48 | return -1; |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 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)); |
Ole Troan | 5993a34 | 2021-02-16 00:42:21 +0100 | [diff] [blame] | 65 | if (!unformat (&input, "0x%U", unformat_hex_string, &s)) |
| 66 | return 0; |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 67 | return s; |
| 68 | } |
| 69 | |
| 70 | int |
| 71 | u8string_fromjson2(cJSON *o, char *fieldname, u8 *data) |
| 72 | { |
| 73 | u8 *s = u8string_fromjson(o, fieldname); |
Ole Troan | 93c4b1b | 2021-02-16 18:09:51 +0100 | [diff] [blame] | 74 | if (!s) |
| 75 | return -1; |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 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 | |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 266 | int |
| 267 | vl_api_ip4_address_t_fromjson (void **mp, int *len, cJSON *o, |
| 268 | vl_api_ip4_address_t *a) |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 269 | { |
| 270 | unformat_input_t input; |
| 271 | char *p = cJSON_GetStringValue(o); |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 272 | if (!p) |
| 273 | return -1; |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 274 | unformat_init_string (&input, p, strlen(p)); |
Ole Troan | 71134f2 | 2021-02-17 14:10:04 +0100 | [diff] [blame] | 275 | if (!unformat (&input, "%U", unformat_ip4_address, a)) |
| 276 | return -1; |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 277 | return 0; |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 280 | int |
| 281 | vl_api_ip4_prefix_t_fromjson (void **mp, int *len, cJSON *o, |
| 282 | vl_api_ip4_prefix_t *a) |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 283 | { |
| 284 | unformat_input_t input; |
| 285 | char *p = cJSON_GetStringValue(o); |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 286 | if (!p) |
| 287 | return -1; |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 288 | unformat_init_string (&input, p, strlen(p)); |
Ole Troan | 71134f2 | 2021-02-17 14:10:04 +0100 | [diff] [blame] | 289 | if (!unformat (&input, "%U/%d", unformat_ip4_address, &a->address, |
| 290 | &a->len)) |
| 291 | return -1; |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 292 | return 0; |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 293 | } |
| 294 | |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 295 | int |
| 296 | vl_api_ip4_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o, |
| 297 | vl_api_ip4_prefix_t *a) |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 298 | { |
| 299 | return vl_api_ip4_prefix_t_fromjson(mp, len, o, a); |
| 300 | } |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 301 | int |
| 302 | vl_api_ip6_address_t_fromjson (void **mp, int *len, cJSON *o, |
| 303 | vl_api_ip6_address_t *a) |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 304 | { |
| 305 | unformat_input_t input; |
| 306 | char *p = cJSON_GetStringValue(o); |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 307 | if (!p) |
| 308 | return -1; |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 309 | unformat_init_string (&input, p, strlen(p)); |
Ole Troan | 71134f2 | 2021-02-17 14:10:04 +0100 | [diff] [blame] | 310 | if (!unformat (&input, "%U", unformat_ip6_address, a)) |
| 311 | return -1; |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 312 | return 0; |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 313 | } |
| 314 | |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 315 | int |
| 316 | vl_api_ip6_prefix_t_fromjson (void **mp, int *len, cJSON *o, |
| 317 | vl_api_ip6_prefix_t *a) |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 318 | { |
| 319 | unformat_input_t input; |
| 320 | char *p = cJSON_GetStringValue(o); |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 321 | if (!p) |
| 322 | return -1; |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 323 | unformat_init_string (&input, p, strlen(p)); |
Ole Troan | 71134f2 | 2021-02-17 14:10:04 +0100 | [diff] [blame] | 324 | if (!unformat (&input, "%U/%d", unformat_ip6_address, &a->address, &a->len)) |
| 325 | return -1; |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 326 | return 0; |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 327 | } |
| 328 | |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 329 | int |
| 330 | vl_api_ip6_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o, |
| 331 | vl_api_ip6_prefix_t *a) |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 332 | { |
| 333 | return vl_api_ip6_prefix_t_fromjson(mp, len, o, a); |
| 334 | } |
| 335 | |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 336 | int |
| 337 | vl_api_address_t_fromjson (void **mp, int *len, cJSON *o, vl_api_address_t *a) |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 338 | { |
| 339 | unformat_input_t input; |
| 340 | |
| 341 | char *p = cJSON_GetStringValue(o); |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 342 | if (!p) |
| 343 | return -1; |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 344 | unformat_init_string (&input, p, strlen(p)); |
Ole Troan | 793be46 | 2020-12-04 13:15:30 +0100 | [diff] [blame] | 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; |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 349 | else |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 350 | return -1; |
| 351 | return 0; |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 352 | } |
| 353 | |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 354 | int |
| 355 | vl_api_prefix_t_fromjson (void **mp, int *len, cJSON *o, vl_api_prefix_t *a) |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 356 | { |
| 357 | unformat_input_t input; |
| 358 | |
| 359 | char *p = cJSON_GetStringValue(o); |
Ole Troan | 793be46 | 2020-12-04 13:15:30 +0100 | [diff] [blame] | 360 | |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 361 | if (!p) |
| 362 | return -1; |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 363 | unformat_init_string (&input, p, strlen(p)); |
Ole Troan | 793be46 | 2020-12-04 13:15:30 +0100 | [diff] [blame] | 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; |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 369 | else |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 370 | return -1; |
Ole Troan | 793be46 | 2020-12-04 13:15:30 +0100 | [diff] [blame] | 371 | a->len = plen; |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 372 | return 0; |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 373 | } |
| 374 | |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 375 | int |
| 376 | vl_api_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o, |
| 377 | vl_api_prefix_t *a) |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 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 | |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 410 | int |
| 411 | vl_api_mac_address_t_fromjson (void **mp, int *len, cJSON *o, |
| 412 | vl_api_mac_address_t *a) |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 413 | { |
| 414 | unformat_input_t input; |
| 415 | |
| 416 | char *p = cJSON_GetStringValue(o); |
| 417 | unformat_init_string (&input, p, strlen(p)); |
Ole Troan | 71134f2 | 2021-02-17 14:10:04 +0100 | [diff] [blame] | 418 | if (!unformat (&input, "%U", unformat_mac_address, a)) |
| 419 | return -1; |
Ole Troan | 316967c | 2021-02-16 00:31:52 +0100 | [diff] [blame] | 420 | return 0; |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 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 | |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 431 | void |
| 432 | vl_api_string_cJSON_AddToObject(cJSON * const object, const char * const name, vl_api_string_t *astr) |
| 433 | { |
| 434 | |
| 435 | if (astr == 0) return; |
| 436 | u32 length = clib_net_to_host_u32 (astr->length); |
| 437 | |
| 438 | char *cstr = malloc(length + 1); |
| 439 | memcpy(cstr, astr->buf, length); |
| 440 | cstr[length] = '\0'; |
| 441 | cJSON_AddStringToObject(object, name, cstr); |
| 442 | free(cstr); |
| 443 | } |
| 444 | |
| 445 | u8 * |
| 446 | format_vl_api_timestamp_t(u8 * s, va_list * args) |
| 447 | { |
| 448 | f64 timestamp = va_arg (*args, f64); |
| 449 | struct tm *tm; |
| 450 | word msec; |
| 451 | |
| 452 | time_t t = timestamp; |
| 453 | tm = gmtime (&t); |
| 454 | msec = 1e6 * (timestamp - t); |
| 455 | return format (s, "%4d-%02d-%02dT%02d:%02d:%02d.%06dZ", 1900 + tm->tm_year, |
| 456 | 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, |
| 457 | tm->tm_sec, msec); |
| 458 | } |
| 459 | |
| 460 | u8 * |
| 461 | format_vl_api_timedelta_t(u8 * s, va_list * args) |
| 462 | { |
| 463 | return format_vl_api_timestamp_t(s, args); |
| 464 | } |
| 465 | |
| 466 | uword |
| 467 | unformat_vl_api_timedelta_t(unformat_input_t * input, va_list * args) |
| 468 | { |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | uword |
| 473 | unformat_vl_api_timestamp_t(unformat_input_t * input, va_list * args) |
| 474 | { |
| 475 | return 0; |
| 476 | } |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 477 | uword unformat_vl_api_gbp_scope_t(unformat_input_t * input, va_list * args) |
| 478 | { |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | cJSON * |
| 483 | vl_api_ip4_address_with_prefix_t_tojson (vl_api_ip4_prefix_t *a) { |
Ole Troan | 91144fb | 2021-08-17 12:57:00 +0200 | [diff] [blame] | 484 | return vl_api_ip4_prefix_t_tojson (a); |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 485 | } |
| 486 | cJSON * |
| 487 | vl_api_ip6_address_with_prefix_t_tojson (vl_api_ip6_prefix_t *a) { |
Ole Troan | 91144fb | 2021-08-17 12:57:00 +0200 | [diff] [blame] | 488 | return vl_api_ip6_prefix_t_tojson (a); |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 489 | } |
| 490 | cJSON * |
| 491 | vl_api_address_with_prefix_t_tojson (vl_api_prefix_t *a) { |
Ole Troan | 91144fb | 2021-08-17 12:57:00 +0200 | [diff] [blame] | 492 | return vl_api_prefix_t_tojson (a); |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 493 | } |
| 494 | u8 * |
| 495 | format_vl_api_mac_address_t (u8 * s, va_list * args) |
| 496 | { |
| 497 | const mac_address_t *mac = va_arg (*args, mac_address_t *); |
| 498 | |
| 499 | return format (s, "%02x:%02x:%02x:%02x:%02x:%02x", |
| 500 | mac->bytes[0], mac->bytes[1], mac->bytes[2], |
| 501 | mac->bytes[3], mac->bytes[4], mac->bytes[5]); |
| 502 | } |
| 503 | #define _(T) \ |
| 504 | cJSON *vl_api_ ##T## _t_tojson (vl_api_ ##T## _t *a) { \ |
| 505 | u8 *s = format(0, "%U", format_vl_api_ ##T## _t, a); \ |
| 506 | cJSON *o = cJSON_CreateString((char *)s); \ |
| 507 | vec_free(s); \ |
| 508 | return o; \ |
| 509 | } |
Filip Tehlar | 36217e3 | 2021-07-23 08:51:10 +0000 | [diff] [blame] | 510 | foreach_type_tojson |
Ole Troan | df87f80 | 2020-11-18 19:17:48 +0100 | [diff] [blame] | 511 | #undef _ |