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 (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)}, "af": ADDRESS_IP6}
39 # PY2: raises socket.error
41 except (socket.error, OSError):
42 return {"un": {"ip4": inet_pton(AF_INET, args)}, "af": ADDRESS_IP4}
45 def format_vl_api_prefix_t(args):
46 if isinstance(args, (ipaddress.IPv4Network, ipaddress.IPv6Network)):
48 "address": format_vl_api_address_t(str(args.network_address)),
49 "len": int(args.prefixlen),
51 p, length = args.split("/")
52 return {"address": format_vl_api_address_t(p), "len": int(length)}
55 def format_vl_api_address_with_prefix_t(args):
56 if isinstance(args, (ipaddress.IPv4Interface, ipaddress.IPv6Interface)):
58 "address": format_vl_api_address_t(str(args.network_address)),
59 "len": int(args.prefixlen),
61 p, length = args.split("/")
62 return {"address": format_vl_api_address_t(p), "len": int(length)}
65 def format_vl_api_ip6_prefix_t(args):
66 if isinstance(args, ipaddress.IPv6Network):
67 return {"address": args.network_address.packed, "len": int(args.prefixlen)}
68 p, length = args.split("/")
69 return {"address": inet_pton(AF_INET6, p), "len": int(length)}
72 def format_vl_api_ip6_address_with_prefix_t(args):
73 if isinstance(args, ipaddress.IPv6Interface):
74 return {"address": args.network_address.packed, "len": int(args.prefixlen)}
75 p, length = args.split("/")
76 return {"address": inet_pton(AF_INET6, p), "len": int(length)}
79 def format_vl_api_ip4_prefix_t(args):
80 if isinstance(args, ipaddress.IPv4Network):
81 return {"address": args.network_address.packed, "len": int(args.prefixlen)}
82 p, length = args.split("/")
83 return {"address": inet_pton(AF_INET, p), "len": int(length)}
86 def format_vl_api_ip4_address_with_prefix_t(args):
87 if isinstance(args, ipaddress.IPv4Interface):
88 return {"address": args.network_address.packed, "len": int(args.prefixlen)}
89 p, length = args.split("/")
90 return {"address": inet_pton(AF_INET, p), "len": int(length)}
94 "vl_api_ip6_address_t": {
95 "IPv6Address": lambda o: o.packed,
96 "str": lambda s: inet_pton(AF_INET6, s),
98 "vl_api_ip4_address_t": {
99 "IPv4Address": lambda o: o.packed,
100 "str": lambda s: inet_pton(AF_INET, s),
102 "vl_api_ip6_prefix_t": {
103 "IPv6Network": lambda o: {
104 "address": o.network_address.packed,
107 "str": lambda s: format_vl_api_ip6_prefix_t(s),
109 "vl_api_ip4_prefix_t": {
110 "IPv4Network": lambda o: {
111 "address": o.network_address.packed,
114 "str": lambda s: format_vl_api_ip4_prefix_t(s),
116 "vl_api_address_t": {
117 "IPv4Address": lambda o: {"af": ADDRESS_IP4, "un": {"ip4": o.packed}},
118 "IPv6Address": lambda o: {"af": ADDRESS_IP6, "un": {"ip6": o.packed}},
119 "str": lambda s: format_vl_api_address_t(s),
122 "IPv4Network": lambda o: {
123 "address": {"af": ADDRESS_IP4, "un": {"ip4": o.network_address.packed}},
126 "IPv6Network": lambda o: {
127 "address": {"af": ADDRESS_IP6, "un": {"ip6": o.network_address.packed}},
130 "str": lambda s: format_vl_api_prefix_t(s),
132 "vl_api_address_with_prefix_t": {
133 "IPv4Interface": lambda o: {
134 "address": {"af": ADDRESS_IP4, "un": {"ip4": o.packed}},
135 "len": o.network.prefixlen,
137 "IPv6Interface": lambda o: {
138 "address": {"af": ADDRESS_IP6, "un": {"ip6": o.packed}},
139 "len": o.network.prefixlen,
141 "str": lambda s: format_vl_api_address_with_prefix_t(s),
143 "vl_api_ip4_address_with_prefix_t": {
144 "IPv4Interface": lambda o: {"address": o.packed, "len": o.network.prefixlen},
145 "str": lambda s: format_vl_api_ip4_address_with_prefix_t(s),
147 "vl_api_ip6_address_with_prefix_t": {
148 "IPv6Interface": lambda o: {"address": o.packed, "len": o.network.prefixlen},
149 "str": lambda s: format_vl_api_ip6_address_with_prefix_t(s),
151 "vl_api_mac_address_t": {
152 "MACAddress": lambda o: o.packed,
153 "str": lambda s: macaddress.mac_pton(s),
155 "vl_api_timestamp_t": {
156 "datetime.datetime": lambda o: (
157 o - datetime.datetime(1970, 1, 1)
163 def unformat_api_address_t(o):
165 return ipaddress.IPv6Address(o.un.ip6)
167 return ipaddress.IPv4Address(o.un.ip4)
171 def unformat_api_prefix_t(o):
172 if o.address.af == 1:
173 return ipaddress.IPv6Network((o.address.un.ip6, o.len), False)
174 if o.address.af == 0:
175 return ipaddress.IPv4Network((o.address.un.ip4, o.len), False)
178 if isinstance(o.address, ipaddress.IPv4Address):
179 return ipaddress.IPv4Network((o.address, o.len), False)
180 if isinstance(o.address, ipaddress.IPv6Address):
181 return ipaddress.IPv6Network((o.address, o.len), False)
182 raise ValueError("Unknown instance {}", format(o))
185 def unformat_api_address_with_prefix_t(o):
186 if o.address.af == 1:
187 return ipaddress.IPv6Interface((o.address.un.ip6, o.len))
188 if o.address.af == 0:
189 return ipaddress.IPv4Interface((o.address.un.ip4, o.len))
193 def unformat_api_ip4_address_with_prefix_t(o):
194 return ipaddress.IPv4Interface((o.address, o.len))
197 def unformat_api_ip6_address_with_prefix_t(o):
198 return ipaddress.IPv6Interface((o.address, o.len))
201 conversion_unpacker_table = {
202 "vl_api_ip6_address_t": lambda o: ipaddress.IPv6Address(o),
203 "vl_api_ip6_prefix_t": lambda o: ipaddress.IPv6Network((o.address, o.len)),
204 "vl_api_ip4_address_t": lambda o: ipaddress.IPv4Address(o),
205 "vl_api_ip4_prefix_t": lambda o: ipaddress.IPv4Network((o.address, o.len)),
206 "vl_api_address_t": lambda o: unformat_api_address_t(o),
207 "vl_api_prefix_t": lambda o: unformat_api_prefix_t(o),
208 "vl_api_address_with_prefix_t": lambda o: unformat_api_address_with_prefix_t(o),
209 "vl_api_ip4_address_with_prefix_t": lambda o: unformat_api_ip4_address_with_prefix_t(
212 "vl_api_ip6_address_with_prefix_t": lambda o: unformat_api_ip6_address_with_prefix_t(
215 "vl_api_mac_address_t": lambda o: macaddress.MACAddress(o),
216 "vl_api_timestamp_t": lambda o: datetime.datetime.fromtimestamp(o),
217 "vl_api_timedelta_t": lambda o: datetime.timedelta(seconds=o),