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, inet_ntop, AF_INET6, AF_INET
19 from . import macaddress
21 # Copies from vl_api_address_t definition
26 # Type conversion for input arguments and return values
30 def format_vl_api_address_t(args):
32 return {'un': {'ip6': inet_pton(AF_INET6, args)},
34 except socket.error as e:
35 return {'un': {'ip4': inet_pton(AF_INET, args)},
39 def format_vl_api_prefix_t(args):
40 p, length = args.split('/')
41 return {'address': format_vl_api_address_t(p),
42 'address_length': int(length)}
45 def format_vl_api_ip6_prefix_t(args):
46 p, length = args.split('/')
47 return {'prefix': inet_pton(AF_INET6, p),
51 def format_vl_api_ip4_prefix_t(args):
52 p, length = args.split('/')
53 return {'prefix': inet_pton(AF_INET, p),
58 'vl_api_ip6_address_t':
60 'IPv6Address': lambda o: o.packed,
61 'str': lambda s: inet_pton(AF_INET6, s)
63 'vl_api_ip4_address_t':
65 'IPv4Address': lambda o: o.packed,
66 'str': lambda s: inet_pton(AF_INET, s)
68 'vl_api_ip6_prefix_t':
70 'IPv6Network': lambda o: {'prefix': o.network_address.packed,
72 'str': lambda s: format_vl_api_ip6_prefix_t(s)
74 'vl_api_ip4_prefix_t':
76 'IPv4Network': lambda o: {'prefix': o.network_address.packed,
78 'str': lambda s: format_vl_api_ip4_prefix_t(s)
82 'IPv4Address': lambda o: {'af': ADDRESS_IP4, 'un': {'ip4': o.packed}},
83 'IPv6Address': lambda o: {'af': ADDRESS_IP6, 'un': {'ip6': o.packed}},
84 'str': lambda s: format_vl_api_address_t(s)
88 'IPv4Network': lambda o: {'prefix':
89 {'af': ADDRESS_IP4, 'un':
90 {'ip4': o.network_address.packed}},
92 'IPv6Network': lambda o: {'prefix':
93 {'af': ADDRESS_IP6, 'un':
94 {'ip6': o.network_address.packed}},
96 'str': lambda s: format_vl_api_prefix_t(s)
98 'vl_api_mac_address_t':
100 'MACAddress': lambda o: o.packed,
101 'str': lambda s: macaddress.mac_pton(s)
106 def unformat_api_address_t(o):
108 return ipaddress.IPv6Address(o.un.ip6)
110 return ipaddress.IPv4Address(o.un.ip4)
113 def unformat_api_prefix_t(o):
114 if isinstance(o.address, ipaddress.IPv4Address):
115 return ipaddress.IPv4Network((o.address, o.address_length), False)
116 if isinstance(o.address, ipaddress.IPv6Address):
117 return ipaddress.IPv6Network((o.address, o.address_length), False)
120 conversion_unpacker_table = {
121 'vl_api_ip6_address_t': lambda o: ipaddress.IPv6Address(o),
122 'vl_api_ip6_prefix_t': lambda o: ipaddress.IPv6Network((o.prefix, o.len)),
123 'vl_api_ip4_address_t': lambda o: ipaddress.IPv4Address(o),
124 'vl_api_ip4_prefix_t': lambda o: ipaddress.IPv4Network((o.prefix, o.len)),
125 'vl_api_address_t': lambda o: unformat_api_address_t(o),
126 'vl_api_prefix_t': lambda o: unformat_api_prefix_t(o),
127 'vl_api_mac_address_t': lambda o: macaddress.MACAddress(o),