FD.io VPP  v18.04-17-g3a0d853
Vector Packet Processing
util.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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 <lb/util.h>
17 
18 void ip46_prefix_normalize(ip46_address_t *prefix, u8 plen)
19 {
20  if (plen == 0) {
21  prefix->as_u64[0] = 0;
22  prefix->as_u64[1] = 0;
23  } else if (plen <= 64) {
24  prefix->as_u64[0] &= clib_host_to_net_u64(0xffffffffffffffffL << (64 - plen));
25  prefix->as_u64[1] = 0;
26  } else {
27  prefix->as_u64[1] &= clib_host_to_net_u64(0xffffffffffffffffL << (128 - plen));
28  }
29 
30 }
31 
32 uword unformat_ip46_prefix (unformat_input_t * input, va_list * args)
33 {
34  ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
35  u8 *len = va_arg (*args, u8 *);
36  ip46_type_t type = va_arg (*args, ip46_type_t);
37 
38  u32 l;
39  if ((type != IP46_TYPE_IP6) && unformat(input, "%U/%u", unformat_ip4_address, &ip46->ip4, &l)) {
40  if (l > 32)
41  return 0;
42  *len = l + 96;
43  ip46->pad[0] = ip46->pad[1] = ip46->pad[2] = 0;
44  } else if ((type != IP46_TYPE_IP4) && unformat(input, "%U/%u", unformat_ip6_address, &ip46->ip6, &l)) {
45  if (l > 128)
46  return 0;
47  *len = l;
48  } else {
49  return 0;
50  }
51  return 1;
52 }
53 
54 u8 *format_ip46_prefix (u8 * s, va_list * args)
55 {
56  ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
57  u32 len = va_arg (*args, u32); //va_arg cannot use u8 or u16
58  ip46_type_t type = va_arg (*args, ip46_type_t);
59 
60  int is_ip4 = 0;
61  if (type == IP46_TYPE_IP4)
62  is_ip4 = 1;
63  else if (type == IP46_TYPE_IP6)
64  is_ip4 = 0;
65  else
66  is_ip4 = (len >= 96) && ip46_address_is_ip4(ip46);
67 
68  return is_ip4 ?
69  format(s, "%U/%d", format_ip4_address, &ip46->ip4, len - 96):
70  format(s, "%U/%d", format_ip6_address, &ip46->ip6, len);
71 }
72 
ip46_type_t
Definition: format.h:63
u8 * format_ip46_prefix(u8 *s, va_list *args)
Definition: util.c:54
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
format_function_t format_ip4_address
Definition: format.h:79
unformat_function_t unformat_ip4_address
Definition: format.h:76
struct _unformat_input_t unformat_input_t
void ip46_prefix_normalize(ip46_address_t *prefix, u8 plen)
Definition: util.c:18
#define ip46_address_is_ip4(ip46)
Definition: ip6_packet.h:76
unformat_function_t unformat_ip6_address
Definition: format.h:94
format_function_t format_ip6_address
Definition: format.h:95
unsigned int u32
Definition: types.h:88
u64 uword
Definition: types.h:112
unsigned char u8
Definition: types.h:56
uword unformat_ip46_prefix(unformat_input_t *input, va_list *args)
Definition: util.c:32
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972