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 # PY2: raises socket.error
41 except (socket.error, OSError):
42 return {'un': {'ip4': inet_pton(AF_INET, args)},
46 def format_vl_api_prefix_t(args):
47 if isinstance(args, (ipaddress.IPv4Network, ipaddress.IPv6Network)):
48 return {'address': format_vl_api_address_t(
49 text_type(args.network_address)),
50 'len': int(args.prefixlen)}
51 p, length = args.split('/')
52 return {'address': format_vl_api_address_t(p),
56 def format_vl_api_ip6_prefix_t(args):
57 if isinstance(args, ipaddress.IPv6Network):
58 return {'address': args.network_address.packed,
59 'len': int(args.prefixlen)}
60 p, length = args.split('/')
61 return {'address': inet_pton(AF_INET6, p),
65 def format_vl_api_ip4_prefix_t(args):
66 if isinstance(args, ipaddress.IPv4Network):
67 return {'address': args.network_address.packed,
68 'len': int(args.prefixlen)}
69 p, length = args.split('/')
70 return {'address': inet_pton(AF_INET, p),
75 'vl_api_ip6_address_t':
77 'IPv6Address': lambda o: o.packed,
78 'str': lambda s: inet_pton(AF_INET6, s)
80 'vl_api_ip4_address_t':
82 'IPv4Address': lambda o: o.packed,
83 'str': lambda s: inet_pton(AF_INET, s)
85 'vl_api_ip6_prefix_t':
87 'IPv6Network': lambda o: {'address': o.network_address.packed,
89 'str': lambda s: format_vl_api_ip6_prefix_t(s)
91 'vl_api_ip4_prefix_t':
93 'IPv4Network': lambda o: {'address': o.network_address.packed,
95 'str': lambda s: format_vl_api_ip4_prefix_t(s)
99 'IPv4Address': lambda o: {'af': ADDRESS_IP4, 'un': {'ip4': o.packed}},
100 'IPv6Address': lambda o: {'af': ADDRESS_IP6, 'un': {'ip6': o.packed}},
101 'str': lambda s: format_vl_api_address_t(s)
105 'IPv4Network': lambda o: {'prefix':
106 {'af': ADDRESS_IP4, 'un':
107 {'ip4': o.network_address.packed}},
109 'IPv6Network': lambda o: {'prefix':
110 {'af': ADDRESS_IP6, 'un':
111 {'ip6': o.network_address.packed}},
113 'str': lambda s: format_vl_api_prefix_t(s)
115 'vl_api_mac_address_t':
117 'MACAddress': lambda o: o.packed,
118 'str': lambda s: macaddress.mac_pton(s)
123 def unformat_api_address_t(o):
125 return ipaddress.IPv6Address(o.un.ip6)
127 return ipaddress.IPv4Address(o.un.ip4)
130 def unformat_api_prefix_t(o):
131 if isinstance(o.address, ipaddress.IPv4Address):
132 return ipaddress.IPv4Network((o.address, o.len), False)
133 if isinstance(o.address, ipaddress.IPv6Address):
134 return ipaddress.IPv6Network((o.address, o.len), False)
137 conversion_unpacker_table = {
138 'vl_api_ip6_address_t': lambda o: ipaddress.IPv6Address(o),
139 'vl_api_ip6_prefix_t': lambda o: ipaddress.IPv6Network((o.address, o.len)),
140 'vl_api_ip4_address_t': lambda o: ipaddress.IPv4Address(o),
141 'vl_api_ip4_prefix_t': lambda o: ipaddress.IPv4Network((o.address, o.len)),
142 'vl_api_address_t': lambda o: unformat_api_address_t(o),
143 'vl_api_prefix_t': lambda o: unformat_api_prefix_t(o),
144 'vl_api_mac_address_t': lambda o: macaddress.MACAddress(o),