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
22 # Copies from vl_api_address_t definition
27 def verify_enum_hint(e):
28 return (e.ADDRESS_IP4.value == ADDRESS_IP4) and\
29 (e.ADDRESS_IP6.value == ADDRESS_IP6)
32 # Type conversion for input arguments and return values
36 def format_vl_api_address_t(args):
38 return {'un': {'ip6': inet_pton(AF_INET6, args)},
40 # PY2: raises socket.error
42 except (socket.error, OSError):
43 return {'un': {'ip4': inet_pton(AF_INET, args)},
47 def format_vl_api_prefix_t(args):
48 if isinstance(args, (ipaddress.IPv4Network, ipaddress.IPv6Network)):
49 return {'address': format_vl_api_address_t(
50 str(args.network_address)),
51 'len': int(args.prefixlen)}
52 p, length = args.split('/')
53 return {'address': format_vl_api_address_t(p),
57 def format_vl_api_address_with_prefix_t(args):
58 if isinstance(args, (ipaddress.IPv4Interface, ipaddress.IPv6Interface)):
59 return {'address': format_vl_api_address_t(
60 str(args.network_address)),
61 'len': int(args.prefixlen)}
62 p, length = args.split('/')
63 return {'address': format_vl_api_address_t(p),
67 def format_vl_api_ip6_prefix_t(args):
68 if isinstance(args, ipaddress.IPv6Network):
69 return {'address': args.network_address.packed,
70 'len': int(args.prefixlen)}
71 p, length = args.split('/')
72 return {'address': inet_pton(AF_INET6, p),
76 def format_vl_api_ip6_address_with_prefix_t(args):
77 if isinstance(args, ipaddress.IPv6Interface):
78 return {'address': args.network_address.packed,
79 'len': int(args.prefixlen)}
80 p, length = args.split('/')
81 return {'address': inet_pton(AF_INET6, p),
85 def format_vl_api_ip4_prefix_t(args):
86 if isinstance(args, ipaddress.IPv4Network):
87 return {'address': args.network_address.packed,
88 'len': int(args.prefixlen)}
89 p, length = args.split('/')
90 return {'address': inet_pton(AF_INET, p),
94 def format_vl_api_ip4_address_with_prefix_t(args):
95 if isinstance(args, ipaddress.IPv4Interface):
96 return {'address': args.network_address.packed,
97 'len': int(args.prefixlen)}
98 p, length = args.split('/')
99 return {'address': inet_pton(AF_INET, p),
104 'vl_api_ip6_address_t':
106 'IPv6Address': lambda o: o.packed,
107 'str': lambda s: inet_pton(AF_INET6, s)
109 'vl_api_ip4_address_t':
111 'IPv4Address': lambda o: o.packed,
112 'str': lambda s: inet_pton(AF_INET, s)
114 'vl_api_ip6_prefix_t':
116 'IPv6Network': lambda o: {'address': o.network_address.packed,
118 'str': lambda s: format_vl_api_ip6_prefix_t(s)
120 'vl_api_ip4_prefix_t':
122 'IPv4Network': lambda o: {'address': o.network_address.packed,
124 'str': lambda s: format_vl_api_ip4_prefix_t(s)
128 'IPv4Address': lambda o: {'af': ADDRESS_IP4, 'un': {'ip4': o.packed}},
129 'IPv6Address': lambda o: {'af': ADDRESS_IP6, 'un': {'ip6': o.packed}},
130 'str': lambda s: format_vl_api_address_t(s)
134 'IPv4Network': lambda o: {'address':
135 {'af': ADDRESS_IP4, 'un':
136 {'ip4': o.network_address.packed}},
138 'IPv6Network': lambda o: {'address':
139 {'af': ADDRESS_IP6, 'un':
140 {'ip6': o.network_address.packed}},
142 'str': lambda s: format_vl_api_prefix_t(s)
144 'vl_api_address_with_prefix_t':
146 'IPv4Interface': lambda o: {'address':
147 {'af': ADDRESS_IP4, 'un':
149 'len': o.network.prefixlen},
150 'IPv6Interface': lambda o: {'address':
151 {'af': ADDRESS_IP6, 'un':
153 'len': o.network.prefixlen},
154 'str': lambda s: format_vl_api_address_with_prefix_t(s)
156 'vl_api_ip4_address_with_prefix_t':
158 'IPv4Interface': lambda o: {'address': o.packed,
159 'len': o.network.prefixlen},
160 'str': lambda s: format_vl_api_ip4_address_with_prefix_t(s)
162 'vl_api_ip6_address_with_prefix_t':
164 'IPv6Interface': lambda o: {'address': o.packed,
165 'len': o.network.prefixlen},
166 'str': lambda s: format_vl_api_ip6_address_with_prefix_t(s)
168 'vl_api_mac_address_t':
170 'MACAddress': lambda o: o.packed,
171 'str': lambda s: macaddress.mac_pton(s)
173 'vl_api_timestamp_t':
175 'datetime.datetime': lambda o:
176 (o - datetime.datetime(1970, 1, 1)).total_seconds()
181 def unformat_api_address_t(o):
183 return ipaddress.IPv6Address(o.un.ip6)
185 return ipaddress.IPv4Address(o.un.ip4)
189 def unformat_api_prefix_t(o):
190 if o.address.af == 1:
191 return ipaddress.IPv6Network((o.address.un.ip6, o.len), False)
192 if o.address.af == 0:
193 return ipaddress.IPv4Network((o.address.un.ip4, o.len), False)
196 if isinstance(o.address, ipaddress.IPv4Address):
197 return ipaddress.IPv4Network((o.address, o.len), False)
198 if isinstance(o.address, ipaddress.IPv6Address):
199 return ipaddress.IPv6Network((o.address, o.len), False)
200 raise ValueError('Unknown instance {}', format(o))
203 def unformat_api_address_with_prefix_t(o):
204 if o.address.af == 1:
205 return ipaddress.IPv6Interface((o.address.un.ip6, o.len))
206 if o.address.af == 0:
207 return ipaddress.IPv4Interface((o.address.un.ip4, o.len))
211 def unformat_api_ip4_address_with_prefix_t(o):
212 return ipaddress.IPv4Interface((o.address, o.len))
215 def unformat_api_ip6_address_with_prefix_t(o):
216 return ipaddress.IPv6Interface((o.address, o.len))
219 conversion_unpacker_table = {
220 'vl_api_ip6_address_t': lambda o: ipaddress.IPv6Address(o),
221 'vl_api_ip6_prefix_t': lambda o: ipaddress.IPv6Network((o.address, o.len)),
222 'vl_api_ip4_address_t': lambda o: ipaddress.IPv4Address(o),
223 'vl_api_ip4_prefix_t': lambda o: ipaddress.IPv4Network((o.address, o.len)),
224 'vl_api_address_t': lambda o: unformat_api_address_t(o),
225 'vl_api_prefix_t': lambda o: unformat_api_prefix_t(o),
226 'vl_api_address_with_prefix_t': lambda o: unformat_api_address_with_prefix_t(o),
227 'vl_api_ip4_address_with_prefix_t': lambda o: unformat_api_ip4_address_with_prefix_t(o),
228 'vl_api_ip6_address_with_prefix_t': lambda o: unformat_api_ip6_address_with_prefix_t(o),
229 'vl_api_mac_address_t': lambda o: macaddress.MACAddress(o),
230 'vl_api_timestamp_t': lambda o: datetime.datetime.fromtimestamp(o),
231 'vl_api_timedelta_t': lambda o: datetime.timedelta(seconds=o),