/* * Copyright (c) 2015 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * ip/ip6_format.c: ip6 formatting * * Copyright (c) 2008 Eliot Dresselhaus * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include /* Format an IP6 address. */ u8 * format_ip6_address (u8 * s, va_list * args) { ip6_address_t *a = va_arg (*args, ip6_address_t *); u32 max_zero_run = 0, this_zero_run = 0; int max_zero_run_index = -1, this_zero_run_index = 0; int in_zero_run = 0, i; int last_double_colon = 0; /* Ugh, this is a pain. Scan forward looking for runs of 0's */ for (i = 0; i < ARRAY_LEN (a->as_u16); i++) { if (a->as_u16[i] == 0) { if (in_zero_run) this_zero_run++; else { in_zero_run = 1; this_zero_run = 1; this_zero_run_index = i; } } else { if (in_zero_run) { /* offer to compress the biggest run of > 1 zero */ if (this_zero_run > max_zero_run && this_zero_run > 1) { max_zero_run_index = this_zero_run_index; max_zero_run = this_zero_run; } } in_zero_run = 0; this_zero_run = 0; } } if (in_zero_run) { if (this_zero_run > max_zero_run && this_zero_run > 1) { max_zero_run_index = this_zero_run_index; max_zero_run = this_zero_run; } } for (i = 0; i < ARRAY_LEN (a->as_u16); i++) { if (i == max_zero_run_index) { s = format (s, "::"); i += max_zero_run - 1; last_double_colon = 1; } else { s = format (s, "%s%x", (last_double_colon || i == 0) ? "" : ":", clib_net_to_host_u16 (a->as_u16[i])); last_double_colon = 0; } } return s; } /* Format an IP6 route destination and length. */ u8 * format_ip6_address_and_length (u8 * s, va_list * args) { ip6_address_t *a = va_arg (*args, ip6_address_t *); u8 l = va_arg (*args, u32); return format (s, "%U/%d", format_ip6_address, a, l); } u8 * format_ip6_address_and_mask (u8 * s, va_list * args) { ip6_address_and_mask_t *am = va_arg (*args, ip6_address_and_mask_t *); if (am->addr.as_u64[0] == 0 && am->addr.as_u64[1] == 0 && am->mask.as_u64[0] == 0 && am->mask.as_u64[1] == 0) return format (s, "any"); if (am->mask.as_u64[0] == ~0 && am->mask.as_u64[1] == ~0) return format (s, "%U", format_ip4_address, &am->addr); return format (s, "%U/%U", format_ip6_address, &am->addr, format_ip4_address, &am->mask); } /* Parse an IP6 address. */ uword unformat_ip6_address (unformat_input_t * input, va_list * args) { ip6_address_t *result = va_arg (*args, ip6_address_t *); u16 hex_quads[8]; uword hex_quad, n_hex_quads, hex_digit, n_hex_digits; uword c, n_colon, double_colon_index; n_hex_quads = hex_quad = n_hex_digits = n_colon = 0; double_colon_index = ARRAY_LEN (hex_quads); while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT) { hex_digit = 16; if (c >= '0' && c <= '9') hex_digit = c - '0'; else if (c >= 'a' && c <= 'f') hex_digit = c + 10 - 'a'; else if (c >= 'A' && c <= 'F') hex_digit = c + 10 - 'A'; else if (c == ':' && n_colon < 2) n_colon++; else { unformat_put_input (input); break; } /* Too many hex quads. */ if (n_hex_quads >= ARRAY_LEN (hex_quads)) return 0; if (hex_digit < 16) { hex_quad = (hex_quad << 4) | hex_digit; /* Hex quad must fit in 16 bits. */ if (n_hex_digits >= 4) return 0; n_colon = 0; n_hex_digits++; } /* Save position of :: */ if (n_colon == 2) { /* More than one :: ? */ if (double_colon_index < ARRAY_LEN (hex_quads)) return 0; double_colon_index = n_hex_quads; } if (n_colon > 0 && n_hex_digits > 0) { hex_quads[n_hex_quads++] = hex_quad; hex_quad = 0; n_hex_digits = 0; } } if (n_hex_digits > 0) hex_quads[n_hex_quads++] = hex_quad; { word i; /* Expand :: to appropriate number of zero hex quads. */ if (double_colon_index < ARRAY_LEN (hex_quads)) { word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads; for