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