Commit 7ed9848e authored by Thomas Gambier's avatar Thomas Gambier 🚴🏼

format: support newer versions of netifaces

follow up of f2798c25

it was still crashing when "create_tap = True"
parent 126c08e1
...@@ -172,16 +172,6 @@ def netmaskToPrefixIPv4(netmask): ...@@ -172,16 +172,6 @@ def netmaskToPrefixIPv4(netmask):
return netaddr.strategy.ipv4.netmask_to_prefix[ return netaddr.strategy.ipv4.netmask_to_prefix[
netaddr.strategy.ipv4.str_to_int(netmask)] netaddr.strategy.ipv4.str_to_int(netmask)]
def netmaskToPrefixIPv6(netmask):
"""Convert string represented netmask to its integer prefix"""
# Since version 0.10.7 of netifaces, the netmask is something like "ffff::/16",
# (it used to be "ffff::"). For old versions of netifaces, interpret the netmask
# as an address and return its netmask, but for newer versions returns the prefixlen.
try:
return netaddr.IPAddress(netmask).netmask_bits()
except ValueError:
return netaddr.IPNetwork(netmask).prefixlen
def getIfaceAddressIPv4(iface): def getIfaceAddressIPv4(iface):
"""return dict containing ipv4 address netmask, network and broadcast address """return dict containing ipv4 address netmask, network and broadcast address
of interface""" of interface"""
...@@ -498,7 +488,7 @@ class Computer(object): ...@@ -498,7 +488,7 @@ class Computer(object):
try: try:
for address in partition.address_list: for address in partition.address_list:
try: try:
netmask = netmaskToPrefixIPv6(address['netmask']) netmask = lenNetmaskIpv6(address['netmask'])
except: except:
continue continue
callAndRead(['ip', 'addr', 'add', callAndRead(['ip', 'addr', 'add',
...@@ -1059,7 +1049,7 @@ class Interface(object): ...@@ -1059,7 +1049,7 @@ class Interface(object):
""" """
if ipv6: if ipv6:
address_string = '%s/%s' % (address, netmaskToPrefixIPv6(netmask)) address_string = '%s/%s' % (address, lenNetmaskIpv6(netmask))
af = socket.AF_INET6 af = socket.AF_INET6
interface_name = self.ipv6_interface or self.name interface_name = self.ipv6_interface or self.name
else: else:
...@@ -1188,9 +1178,9 @@ class Interface(object): ...@@ -1188,9 +1178,9 @@ class Interface(object):
(tap and lenNetmaskIpv6(netmask) == 128): (tap and lenNetmaskIpv6(netmask) == 128):
# same netmask, so there is a chance to add good one # same netmask, so there is a chance to add good one
interface_network = netaddr.ip.IPNetwork('%s/%s' % (address_dict['addr'], interface_network = netaddr.ip.IPNetwork('%s/%s' % (address_dict['addr'],
netmaskToPrefixIPv6(address_dict['netmask']))) lenNetmaskIpv6(address_dict['netmask'])))
requested_network = netaddr.ip.IPNetwork('%s/%s' % (addr, requested_network = netaddr.ip.IPNetwork('%s/%s' % (addr,
netmaskToPrefixIPv6(address_dict['netmask']))) lenNetmaskIpv6(address_dict['netmask'])))
if interface_network.network == requested_network.network: if interface_network.network == requested_network.network:
# same network, try to add # same network, try to add
if self._addSystemAddress(addr, netmask, tap=tap): if self._addSystemAddress(addr, netmask, tap=tap):
......
...@@ -135,7 +135,14 @@ def ipv6FromBin(ip, suffix=''): ...@@ -135,7 +135,14 @@ def ipv6FromBin(ip, suffix=''):
struct.pack('>QQ', int(ip[:64], 2), int(ip[64:], 2))) struct.pack('>QQ', int(ip[:64], 2), int(ip[64:], 2)))
def lenNetmaskIpv6(netmask): def lenNetmaskIpv6(netmask):
return len(binFromIpv6(netmask).rstrip('0')) """Convert string represented netmask to its integer prefix"""
# Since version 0.10.7 of netifaces, the netmask is something like "ffff::/16",
# (it used to be "ffff::"). For old versions of netifaces, interpret the netmask
# as an address and return its netmask, but for newer versions returns the prefixlen.
try:
return netaddr.IPAddress(netmask).netmask_bits()
except ValueError:
return netaddr.IPNetwork(netmask).prefixlen
# Used for Python 2-3 compatibility # Used for Python 2-3 compatibility
if str is bytes: if str is bytes:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment