Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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 | #include <vat/vat.h> |
| 16 | #include <vnet/ip/ip.h> |
| 17 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 18 | uword |
| 19 | unformat_sw_if_index (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 20 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 21 | vat_main_t *vam = va_arg (*args, vat_main_t *); |
| 22 | u32 *result = va_arg (*args, u32 *); |
| 23 | u8 *if_name; |
| 24 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 25 | |
| 26 | if (!unformat (input, "%s", &if_name)) |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 27 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 28 | |
| 29 | p = hash_get_mem (vam->sw_if_index_by_interface_name, if_name); |
| 30 | if (p == 0) |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 31 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 32 | *result = p[0]; |
| 33 | return 1; |
| 34 | } |
| 35 | |
| 36 | /* Parse an IP4 address %d.%d.%d.%d. */ |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 37 | uword |
| 38 | unformat_ip4_address (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 39 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 40 | u8 *result = va_arg (*args, u8 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 41 | unsigned a[4]; |
| 42 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 43 | if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3])) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | return 0; |
| 45 | |
| 46 | if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256) |
| 47 | return 0; |
| 48 | |
| 49 | result[0] = a[0]; |
| 50 | result[1] = a[1]; |
| 51 | result[2] = a[2]; |
| 52 | result[3] = a[3]; |
| 53 | |
| 54 | return 1; |
| 55 | } |
| 56 | |
| 57 | uword |
| 58 | unformat_ethernet_address (unformat_input_t * input, va_list * args) |
| 59 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 60 | u8 *result = va_arg (*args, u8 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | u32 i, a[6]; |
| 62 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 63 | if (!unformat (input, "%_%x:%x:%x:%x:%x:%x%_", |
| 64 | &a[0], &a[1], &a[2], &a[3], &a[4], &a[5])) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | return 0; |
| 66 | |
| 67 | /* Check range. */ |
| 68 | for (i = 0; i < 6; i++) |
| 69 | if (a[i] >= (1 << 8)) |
| 70 | return 0; |
| 71 | |
| 72 | for (i = 0; i < 6; i++) |
| 73 | result[i] = a[i]; |
| 74 | |
| 75 | return 1; |
| 76 | } |
| 77 | |
| 78 | /* Returns ethernet type as an int in host byte order. */ |
| 79 | uword |
| 80 | unformat_ethernet_type_host_byte_order (unformat_input_t * input, |
| 81 | va_list * args) |
| 82 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 83 | u16 *result = va_arg (*args, u16 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | int type; |
| 85 | |
| 86 | /* Numeric type. */ |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 87 | if (unformat (input, "0x%x", &type) || unformat (input, "%d", &type)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | { |
| 89 | if (type >= (1 << 16)) |
| 90 | return 0; |
| 91 | *result = type; |
| 92 | return 1; |
| 93 | } |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | /* Parse an IP6 address. */ |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 98 | uword |
| 99 | unformat_ip6_address (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 101 | ip6_address_t *result = va_arg (*args, ip6_address_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | u16 hex_quads[8]; |
| 103 | uword hex_quad, n_hex_quads, hex_digit, n_hex_digits; |
| 104 | uword c, n_colon, double_colon_index; |
| 105 | |
| 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) |
| 109 | { |
| 110 | hex_digit = 16; |
| 111 | if (c >= '0' && c <= '9') |
| 112 | hex_digit = c - '0'; |
| 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) |
| 118 | n_colon++; |
| 119 | else |
| 120 | { |
| 121 | unformat_put_input (input); |
| 122 | break; |
| 123 | } |
| 124 | |
| 125 | /* Too many hex quads. */ |
| 126 | if (n_hex_quads >= ARRAY_LEN (hex_quads)) |
| 127 | return 0; |
| 128 | |
| 129 | if (hex_digit < 16) |
| 130 | { |
| 131 | hex_quad = (hex_quad << 4) | hex_digit; |
| 132 | |
| 133 | /* Hex quad must fit in 16 bits. */ |
| 134 | if (n_hex_digits >= 4) |
| 135 | return 0; |
| 136 | |
| 137 | n_colon = 0; |
| 138 | n_hex_digits++; |
| 139 | } |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 140 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | /* Save position of :: */ |
| 142 | if (n_colon == 2) |
| 143 | { |
| 144 | /* More than one :: ? */ |
| 145 | if (double_colon_index < ARRAY_LEN (hex_quads)) |
| 146 | return 0; |
| 147 | double_colon_index = n_hex_quads; |
| 148 | } |
| 149 | |
| 150 | if (n_colon > 0 && n_hex_digits > 0) |
| 151 | { |
| 152 | hex_quads[n_hex_quads++] = hex_quad; |
| 153 | hex_quad = 0; |
| 154 | n_hex_digits = 0; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (n_hex_digits > 0) |
| 159 | hex_quads[n_hex_quads++] = hex_quad; |
| 160 | |
| 161 | { |
| 162 | word i; |
| 163 | |
| 164 | /* Expand :: to appropriate number of zero hex quads. */ |
| 165 | if (double_colon_index < ARRAY_LEN (hex_quads)) |
| 166 | { |
| 167 | word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads; |
| 168 | |
| 169 | for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--) |
| 170 | hex_quads[n_zero + i] = hex_quads[i]; |
| 171 | |
| 172 | for (i = 0; i < n_zero; i++) |
| 173 | hex_quads[double_colon_index + i] = 0; |
| 174 | |
| 175 | n_hex_quads = ARRAY_LEN (hex_quads); |
| 176 | } |
| 177 | |
| 178 | /* Too few hex quads given. */ |
| 179 | if (n_hex_quads < ARRAY_LEN (hex_quads)) |
| 180 | return 0; |
| 181 | |
| 182 | for (i = 0; i < ARRAY_LEN (hex_quads); i++) |
| 183 | result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]); |
| 184 | |
| 185 | return 1; |
| 186 | } |
| 187 | } |
| 188 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 189 | u8 * |
| 190 | format_ip4_address (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 192 | u8 *a = va_arg (*args, u8 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]); |
| 194 | } |
| 195 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 196 | u8 * |
Jon Loeliger | 229a6b7 | 2017-05-08 16:53:44 -0500 | [diff] [blame] | 197 | format_ip6_address (u8 * s, va_list * args) |
| 198 | { |
| 199 | ip6_address_t *a = va_arg (*args, ip6_address_t *); |
| 200 | u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon; |
| 201 | |
| 202 | i_max_n_zero = ARRAY_LEN (a->as_u16); |
| 203 | max_n_zeros = 0; |
| 204 | i_first_zero = i_max_n_zero; |
| 205 | n_zeros = 0; |
| 206 | for (i = 0; i < ARRAY_LEN (a->as_u16); i++) |
| 207 | { |
| 208 | u32 is_zero = a->as_u16[i] == 0; |
| 209 | if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16)) |
| 210 | { |
| 211 | i_first_zero = i; |
| 212 | n_zeros = 0; |
| 213 | } |
| 214 | n_zeros += is_zero; |
| 215 | if ((!is_zero && n_zeros > max_n_zeros) |
| 216 | || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros)) |
| 217 | { |
| 218 | i_max_n_zero = i_first_zero; |
| 219 | max_n_zeros = n_zeros; |
| 220 | i_first_zero = ARRAY_LEN (a->as_u16); |
| 221 | n_zeros = 0; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | last_double_colon = 0; |
| 226 | for (i = 0; i < ARRAY_LEN (a->as_u16); i++) |
| 227 | { |
| 228 | if (i == i_max_n_zero && max_n_zeros > 1) |
| 229 | { |
| 230 | s = format (s, "::"); |
| 231 | i += max_n_zeros - 1; |
| 232 | last_double_colon = 1; |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | s = format (s, "%s%x", |
| 237 | (last_double_colon || i == 0) ? "" : ":", |
| 238 | clib_net_to_host_u16 (a->as_u16[i])); |
| 239 | last_double_colon = 0; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | return s; |
| 244 | } |
| 245 | |
| 246 | /* Format an IP46 address. */ |
| 247 | u8 * |
| 248 | format_ip46_address (u8 * s, va_list * args) |
| 249 | { |
| 250 | ip46_address_t *ip46 = va_arg (*args, ip46_address_t *); |
| 251 | ip46_type_t type = va_arg (*args, ip46_type_t); |
| 252 | int is_ip4 = 1; |
| 253 | |
| 254 | switch (type) |
| 255 | { |
| 256 | case IP46_TYPE_ANY: |
| 257 | is_ip4 = ip46_address_is_ip4 (ip46); |
| 258 | break; |
| 259 | case IP46_TYPE_IP4: |
| 260 | is_ip4 = 1; |
| 261 | break; |
| 262 | case IP46_TYPE_IP6: |
| 263 | is_ip4 = 0; |
| 264 | break; |
| 265 | } |
| 266 | |
| 267 | return is_ip4 ? |
| 268 | format (s, "%U", format_ip4_address, &ip46->ip4) : |
| 269 | format (s, "%U", format_ip6_address, &ip46->ip6); |
| 270 | } |
| 271 | |
| 272 | u8 * |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 273 | format_ethernet_address (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 274 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 275 | u8 *a = va_arg (*args, u8 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 276 | |
| 277 | return format (s, "%02x:%02x:%02x:%02x:%02x:%02x", |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 278 | a[0], a[1], a[2], a[3], a[4], a[5]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 281 | void |
| 282 | vat_plugin_api_reference (void) |
| 283 | { |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * fd.io coding-style-patch-verification: ON |
| 288 | * |
| 289 | * Local Variables: |
| 290 | * eval: (c-set-style "gnu") |
| 291 | * End: |
| 292 | */ |