2 # Copyright (c) 2018 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:
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 from socket import inet_pton, AF_INET6, AF_INET
19 from . import macaddress
26 # Copies from vl_api_address_t definition
31 # Type conversion for input arguments and return values
35 def format_vl_api_address_t(args):
37 return {'un': {'ip6': inet_pton(AF_INET6, args)},
39 except socket.error as e:
40 return {'un': {'ip4': inet_pton(AF_INET, args)},
44 def format_vl_api_prefix_t(args):
45 if isinstance(args, (ipaddress.IPv4Network, ipaddress.IPv6Network)):
46 return {'prefix': format_vl_api_address_t(
47 text_type(args.network_address)),
48 'len': int(args.prefixlen)}
49 p, length = args.split('/')
50 return {'address': format_vl_api_address_t(p),
51 'address_length': int(length)}
54 def format_vl_api_ip6_prefix_t(args):
55 if isinstance(args, ipaddress.IPv6Network):
56 return {'prefix': args.network_address.packed,
57 'len': int(args.prefixlen)}
58 p, length = args.split('/')
59 return {'prefix': inet_pton(AF_INET6, p),
63 def format_vl_api_ip4_prefix_t(args):
64 if isinstance(args, ipaddress.IPv4Network):
65 return {'prefix': args.network_address.packed,
66 'len': int(args.prefixlen)}
67 p, length = args.split('/')
68 return {'prefix': inet_pton(AF_INET, p),
73 'vl_api_ip6_address_t':
75 'IPv6Address': lambda o: o.packed,
76 'str': lambda s: inet_pton(AF_INET6, s)
78 'vl_api_ip4_address_t':
80 'IPv4Address': lambda o: o.packed,
81 'str': lambda s: inet_pton(AF_INET, s)
83 'vl_api_ip6_prefix_t':
85 'IPv6Network': lambda o: {'prefix': o.network_address.packed,
87 'str': lambda s: format_vl_api_ip6_prefix_t(s)
89 'vl_api_ip4_prefix_t':
91 'IPv4Network': lambda o: {'prefix': o.network_address.packed,
93 'str': lambda s: format_vl_api_ip4_prefix_t(s)
97 'IPv4Address': lambda o: {'af': ADDRESS_IP4, 'un': {'ip4': o.packed}},
98 'IPv6Address': lambda o: {'af': ADDRESS_IP6, 'un': {'ip6': o.packed}},
99 'str': lambda s: format_vl_api_address_t(s)
103 'IPv4Network': lambda o: {'prefix':
104 {'af': ADDRESS_IP4, 'un':
105 {'ip4': o.network_address.packed}},
107 'IPv6Network': lambda o: {'prefix':
108 {'af': ADDRESS_IP6, 'un':
109 {'ip6': o.network_address.packed}},
111 'str': lambda s: format_vl_api_prefix_t(s)
113 'vl_api_mac_address_t':
115 'MACAddress': lambda o: o.packed,
116 'str': lambda s: macaddress.mac_pton(s)
121 def unformat_api_address_t(o):
123 return ipaddress.IPv6Address(o.un.ip6)
125 return ipaddress.IPv4Address(o.un.ip4)
128 def unformat_api_prefix_t(o):
129 if isinstance(o.address, ipaddress.IPv4Address):
130 return ipaddress.IPv4Network((o.address, o.address_length), False)
131 if isinstance(o.address, ipaddress.IPv6Address):
132 return ipaddress.IPv6Network((o.address, o.address_length), False)
135 conversion_unpacker_table = {
136 'vl_api_ip6_address_t': lambda o: ipaddress.IPv6Address(o),
137 'vl_api_ip6_prefix_t': lambda o: ipaddress.IPv6Network((o.prefix, o.len)),
138 'vl_api_ip4_address_t': lambda o: ipaddress.IPv4Address(o),
139 'vl_api_ip4_prefix_t': lambda o: ipaddress.IPv4Network((o.prefix, o.len)),
140 'vl_api_address_t': lambda o: unformat_api_address_t(o),
141 'vl_api_prefix_t': lambda o: unformat_api_prefix_t(o),
142 'vl_api_mac_address_t': lambda o: macaddress.MACAddress(o),