mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-01-21 07:55:00 +00:00
networks: Refactor & fix extracting netmask
- Use host byte ordering instead of big-endian.
This commit is contained in:
parent
da99a7cb9d
commit
ba5c7a772d
@ -181,8 +181,8 @@ def edit(request, uuid):
|
||||
if settings_connection.get_connection_type() != 'pppoe':
|
||||
settings_ipv4 = connection.get_setting_ip4_config()
|
||||
form_data['ipv4_method'] = settings_ipv4.get_method()
|
||||
address = network.get_first_ip_address_from_connection(connection)
|
||||
netmask = network.get_first_netmask_from_connection(connection)
|
||||
address, netmask = network.get_first_ip_address_from_connection(
|
||||
connection)
|
||||
gateway = settings_ipv4.get_gateway()
|
||||
dns = settings_ipv4.get_dns(0)
|
||||
second_dns = settings_ipv4.get_dns(1)
|
||||
|
||||
@ -52,7 +52,12 @@ class DeviceNotFound(Exception):
|
||||
|
||||
def ipv4_string_to_int(address):
|
||||
"""Return an integer equivalent of a string contain IPv4 address."""
|
||||
return struct.unpack("=I", socket.inet_aton(address))[0]
|
||||
return struct.unpack('=I', socket.inet_aton(address))[0]
|
||||
|
||||
|
||||
def ipv4_int_to_string(address_int):
|
||||
"""Return an string equivalent of a integer IPv4 address."""
|
||||
return socket.inet_ntoa(struct.pack('=I', address_int))
|
||||
|
||||
|
||||
def _callback(source_object, result, user_data):
|
||||
@ -198,28 +203,14 @@ def get_first_ip_address_from_connection(connection):
|
||||
'ipv4.addresses', 'connection', 'show', connection.get_uuid()]
|
||||
|
||||
output = subprocess.check_output(command).decode()
|
||||
return output.strip().split(', ')[0].split('/')[0]
|
||||
first = output.strip().split(', ')[0]
|
||||
if not first:
|
||||
return None, None
|
||||
|
||||
ip_address, prefix = first.split('/')
|
||||
|
||||
def get_first_netmask_from_connection(connection):
|
||||
"""Return the first IP address of a connection setting.
|
||||
|
||||
XXX: Work around a bug in NetworkManager/Python GI. Remove after
|
||||
the bug if fixed.
|
||||
https://bugzilla.gnome.org/show_bug.cgi?id=756380.
|
||||
"""
|
||||
command = ['nmcli', '--terse', '--mode', 'tabular', '--fields',
|
||||
'ipv4.addresses', 'connection', 'show', connection.get_uuid()]
|
||||
|
||||
output = subprocess.check_output(command).decode()
|
||||
if '/' not in output:
|
||||
return None
|
||||
|
||||
CIDR = output.strip().split(', ')[0].split('/')[1]
|
||||
netmask = socket.inet_ntoa(struct.pack(">I", (0xffffffff <<
|
||||
(32 - int(CIDR))) &
|
||||
0xffffffff))
|
||||
return netmask
|
||||
netmask = nm.utils_ip4_prefix_to_netmask(int(prefix))
|
||||
return ip_address, ipv4_int_to_string(netmask)
|
||||
|
||||
|
||||
def get_connection_list():
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user