6 from ipaddress import ip_address
7 from socket import AF_INET, AF_INET6
10 class IpAddressFamily:
19 DPO_PROTO_ETHERNET = 3
24 INVALID_INDEX = 0xffffffff
27 class VppIpAddressUnion():
28 def __init__(self, addr):
30 self.ip_addr = ip_address(unicode(self.addr))
36 'address': self.ip_addr.packed
42 'address': self.ip_addr.packed
48 return self.ip_addr.version
63 return self.ip_addr.packed
65 def __eq__(self, other):
66 if isinstance(other, self.__class__):
67 return self.ip_addr == other.ip_addr
68 elif hasattr(other, "ip4") and hasattr(other, "ip6"):
69 # vl_api_address_union_t
71 return self.ip_addr.packed == other.ip4.address
73 return self.ip_addr.packed == other.ip6.address
75 raise Exception("Comparing VppIpAddresUnions:%s"
76 " with unknown type: %s" %
83 def __init__(self, addr):
84 self.addr = VppIpAddressUnion(addr)
87 if self.addr.version is 6:
89 'af': IpAddressFamily.ADDRESS_IP6,
90 'un': self.addr.encode()
94 'af': IpAddressFamily.ADDRESS_IP4,
95 'un': self.addr.encode()
98 def __eq__(self, other):
99 if isinstance(other, self.__class__):
100 return self.addr == other.addr
101 elif hasattr(other, "af") and hasattr(other, "un"):
103 if 4 is self.version:
104 return other.af == IpAddressFamily.ADDRESS_IP4 and \
105 other.un == self.addr
107 return other.af == IpAddressFamily.ADDRESS_IP6 and \
108 other.un == self.addr
110 raise Exception("Comparing VppIpAddress:%s with unknown type: %s" %
114 def __ne__(self, other):
115 return not (self == other)
122 return self.addr.bytes
126 return self.addr.address
130 return self.addr.length
134 return self.addr.version
138 return (self.version == 6)
142 if self.version == 6:
149 if self.version is 6:
150 return DpoProto.DPO_PROTO_IP6
152 return DpoProto.DPO_PROTO_IP4
156 def __init__(self, addr, len):
157 self.addr = VppIpAddress(addr)
160 def __eq__(self, other):
161 if self.addr == other.addr and self.len == other.len:
166 return {'address': self.addr.encode(),
167 'address_length': self.len}
171 return self.addr.address
178 return "%s/%d" % (self.address, self.length)
180 def __eq__(self, other):
181 if isinstance(other, self.__class__):
182 return (self.len == other.len and self.addr == other.addr)
183 elif hasattr(other, "address") and hasattr(other, "address_length"):
185 return self.len == other.address_length and \
186 self.addr == other.address
188 raise Exception("Comparing VppIpPrefix:%s with unknown type: %s" %
193 class VppIpMPrefix():
194 def __init__(self, saddr, gaddr, len):
198 self.ip_saddr = ip_address(unicode(self.saddr))
199 self.ip_gaddr = ip_address(unicode(self.gaddr))
203 if 6 is self.ip_saddr.version:
205 'af': IpAddressFamily.ADDRESS_IP6,
208 'address': self.ip_gaddr.packed
213 'address': self.ip_saddr.packed
216 'grp_address_length': self.len,
220 'af': IpAddressFamily.ADDRESS_IP4,
223 'address': self.ip_gaddr.packed
228 'address': self.ip_saddr.packed
231 'grp_address_length': self.len,