From ba5c7a772de3d1ab753363e23b64e2c22a38473f Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Wed, 9 Dec 2015 12:29:35 +0530 Subject: [PATCH] networks: Refactor & fix extracting netmask - Use host byte ordering instead of big-endian. --- plinth/modules/networks/networks.py | 4 ++-- plinth/network.py | 33 +++++++++++------------------ 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/plinth/modules/networks/networks.py b/plinth/modules/networks/networks.py index d676033cb..9cbbc3e86 100644 --- a/plinth/modules/networks/networks.py +++ b/plinth/modules/networks/networks.py @@ -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) diff --git a/plinth/network.py b/plinth/network.py index 2f28450dd..2df9edbf7 100644 --- a/plinth/network.py +++ b/plinth/network.py @@ -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():