Blob Blame History Raw
From 5f41f061390876f4c43c2306911d9b3482aed396 Mon Sep 17 00:00:00 2001
From: Lubomir Rintel <lkundrak@v3.sk>
Date: Mon, 16 Jul 2018 17:42:34 +0200
Subject: [PATCH 1/3] firewall.core.fw_nm: avoid iterating NM devices,
 connections

NetworkManager has an API to do the lookups.

(cherry picked from commit 65f92930a5d049404dac780c15eebe2d788e6285)
---
 src/firewall/core/fw_nm.py | 70 ++++++++++++++++++----------------------------
 1 file changed, 27 insertions(+), 43 deletions(-)

diff --git a/src/firewall/core/fw_nm.py b/src/firewall/core/fw_nm.py
index f75733fe65f6..76901cee2adf 100644
--- a/src/firewall/core/fw_nm.py
+++ b/src/firewall/core/fw_nm.py
@@ -73,22 +73,18 @@ def nm_get_zone_of_connection(connection):
     """
     check_nm_imported()
 
-    active_connections = nm_get_client().get_active_connections()
+    con = nm_get_client().get_connection_by_id(connection)
+    if con is None:
+        return False
 
-    for active_con in active_connections:
-        if active_con.get_id() == connection:
-            con = active_con.get_connection()
-            if con is None:
-                continue
-            setting_con = con.get_setting_connection()
-            if setting_con is None:
-                continue
-            zone = setting_con.get_zone()
-            if zone is None:
-                zone = ""
-            return zone
+    setting_con = con.get_setting_connection()
+    if setting_con is None:
+        return False
 
-    return None
+    zone = setting_con.get_zone()
+    if zone is None:
+        zone = ""
+    return zone
 
 def nm_set_zone_of_connection(zone, connection):
     """Set the zone for a connection
@@ -98,24 +94,18 @@ def nm_set_zone_of_connection(zone, connection):
     """
     check_nm_imported()
 
-    active_connections = nm_get_client().get_active_connections()
-
-    for active_con in active_connections:
-        con = active_con.get_connection()
-        if con is None:
-            continue
+    con = nm_get_client().get_connection_by_id(connection)
+    if con is None:
+        return False
 
-        if active_con.get_id() == connection:
-            setting_con = con.get_setting_connection()
-            if setting_con is None:
-                continue
-            if zone == "":
-                zone = None
-            setting_con.set_property("zone", zone)
-            con.commit_changes(True, None)
-            return True
+    setting_con = con.get_setting_connection()
+    if setting_con is None:
+        return False
 
-    return False
+    if zone == "":
+        zone = None
+    setting_con.set_property("zone", zone)
+    return con.commit_changes(True, None)
 
 def nm_get_connections(connections, connections_uuid):
     """Get active connections from NM
@@ -150,21 +140,15 @@ def nm_get_connection_of_interface(interface):
     """
     check_nm_imported()
 
-    active_connections = nm_get_client().get_active_connections()
-
-    for active_con in active_connections:
-        # ignore vpn devices for now
-        if active_con.get_vpn():
-            continue
-
-        devices = active_con.get_devices()
-
-        for dev in devices:
-            if dev.get_iface() == interface:
-                return active_con.get_id()
+    device = nm_get_client().get_device_by_iface(interface)
+    if device is None:
+        return None
 
+    active_con = device.get_active_connection()
+    if active_con is None:
+        return None
 
-    return None
+    return active_con.get_id()
 
 def nm_get_bus_name():
     if not _nm_imported:
-- 
2.16.3