[PATCH 2/4] device: deduplicate match_parent()
- From: Beniamino Galvani <bgalvani redhat com>
- To: networkmanager-list gnome org
- Subject: [PATCH 2/4] device: deduplicate match_parent()
- Date: Wed, 27 Sep 2017 11:02:41 +0200
---
src/devices/nm-device-ip-tunnel.c | 40 ++------------------------------------
src/devices/nm-device-macvlan.c | 41 +--------------------------------------
src/devices/nm-device-private.h | 2 ++
src/devices/nm-device-vlan.c | 40 +-------------------------------------
src/devices/nm-device-vxlan.c | 40 +-------------------------------------
src/devices/nm-device.c | 37 +++++++++++++++++++++++++++++++++++
6 files changed, 44 insertions(+), 156 deletions(-)
diff --git a/src/devices/nm-device-ip-tunnel.c b/src/devices/nm-device-ip-tunnel.c
index 317a5d4e7..af3cfe4c9 100644
--- a/src/devices/nm-device-ip-tunnel.c
+++ b/src/devices/nm-device-ip-tunnel.c
@@ -442,40 +442,6 @@ update_connection (NMDevice *device, NMConnection *connection)
}
static gboolean
-match_parent (NMDevice *dev_parent, const char *setting_parent)
-{
- g_return_val_if_fail (setting_parent, FALSE);
-
- if (!dev_parent)
- return FALSE;
-
- if (nm_utils_is_uuid (setting_parent)) {
- NMActRequest *parent_req;
- NMConnection *parent_connection;
-
- /* If the parent is a UUID, the connection matches if our parent
- * device has that connection activated.
- */
- parent_req = nm_device_get_act_request (dev_parent);
- if (!parent_req)
- return FALSE;
-
- parent_connection = nm_active_connection_get_applied_connection (NM_ACTIVE_CONNECTION
(parent_req));
- if (!parent_connection)
- return FALSE;
-
- if (g_strcmp0 (setting_parent, nm_connection_get_uuid (parent_connection)) != 0)
- return FALSE;
- } else {
- /* interface name */
- if (g_strcmp0 (setting_parent, nm_device_get_ip_iface (dev_parent)) != 0)
- return FALSE;
- }
-
- return TRUE;
-}
-
-static gboolean
check_connection_compatible (NMDevice *device, NMConnection *connection)
{
NMDeviceIPTunnel *self = NM_DEVICE_IP_TUNNEL (device);
@@ -496,10 +462,8 @@ check_connection_compatible (NMDevice *device, NMConnection *connection)
if (nm_device_is_real (device)) {
/* Check parent interface; could be an interface name or a UUID */
parent = nm_setting_ip_tunnel_get_parent (s_ip_tunnel);
- if (parent) {
- if (!match_parent (nm_device_parent_get_device (device), parent))
- return FALSE;
- }
+ if (parent && !nm_device_match_parent (device, parent))
+ return FALSE;
if (!address_equal_pp (priv->addr_family,
nm_setting_ip_tunnel_get_local (s_ip_tunnel),
diff --git a/src/devices/nm-device-macvlan.c b/src/devices/nm-device-macvlan.c
index 8803beb52..ffaa094cb 100644
--- a/src/devices/nm-device-macvlan.c
+++ b/src/devices/nm-device-macvlan.c
@@ -286,45 +286,6 @@ is_available (NMDevice *device, NMDeviceCheckDevAvailableFlags flags)
/*****************************************************************************/
-
-static gboolean
-match_parent (NMDeviceMacvlan *self, const char *parent)
-{
- NMDevice *parent_device;
-
- g_return_val_if_fail (parent != NULL, FALSE);
-
- parent_device = nm_device_parent_get_device (NM_DEVICE (self));
- if (!parent_device)
- return FALSE;
-
- if (nm_utils_is_uuid (parent)) {
- NMActRequest *parent_req;
- NMConnection *parent_connection;
-
- /* If the parent is a UUID, the connection matches if our parent
- * device has that connection activated.
- */
-
- parent_req = nm_device_get_act_request (parent_device);
- if (!parent_req)
- return FALSE;
-
- parent_connection = nm_active_connection_get_applied_connection (NM_ACTIVE_CONNECTION
(parent_req));
- if (!parent_connection)
- return FALSE;
-
- if (g_strcmp0 (parent, nm_connection_get_uuid (parent_connection)) != 0)
- return FALSE;
- } else {
- /* interface name */
- if (g_strcmp0 (parent, nm_device_get_ip_iface (parent_device)) != 0)
- return FALSE;
- }
-
- return TRUE;
-}
-
static gboolean
match_hwaddr (NMDevice *device, NMConnection *connection, gboolean fail_if_no_hwaddr)
{
@@ -378,7 +339,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection)
/* Check parent interface; could be an interface name or a UUID */
parent = nm_setting_macvlan_get_parent (s_macvlan);
if (parent) {
- if (!match_parent (NM_DEVICE_MACVLAN (device), parent))
+ if (!nm_device_match_parent (device, parent))
return FALSE;
} else {
/* Parent could be a MAC address in an NMSettingWired */
diff --git a/src/devices/nm-device-private.h b/src/devices/nm-device-private.h
index 28702f8af..f3c301d48 100644
--- a/src/devices/nm-device-private.h
+++ b/src/devices/nm-device-private.h
@@ -132,4 +132,6 @@ gboolean _nm_device_hash_check_invalid_keys (GHashTable *hash, const char *setti
#define nm_device_hash_check_invalid_keys(hash, setting_name, error, ...) \
_nm_device_hash_check_invalid_keys (hash, setting_name, error, ((const char *[]) { __VA_ARGS__, NULL
}))
+gboolean nm_device_match_parent (NMDevice *device, const char *parent);
+
#endif /* NM_DEVICE_PRIVATE_H */
diff --git a/src/devices/nm-device-vlan.c b/src/devices/nm-device-vlan.c
index 171af14e0..0badfd285 100644
--- a/src/devices/nm-device-vlan.c
+++ b/src/devices/nm-device-vlan.c
@@ -315,44 +315,6 @@ is_available (NMDevice *device, NMDeviceCheckDevAvailableFlags flags)
/*****************************************************************************/
static gboolean
-match_parent (NMDeviceVlan *self, const char *parent)
-{
- NMDevice *parent_device;
-
- g_return_val_if_fail (parent != NULL, FALSE);
-
- parent_device = nm_device_parent_get_device (NM_DEVICE (self));
- if (!parent_device)
- return FALSE;
-
- if (nm_utils_is_uuid (parent)) {
- NMActRequest *parent_req;
- NMConnection *parent_connection;
-
- /* If the parent is a UUID, the connection matches if our parent
- * device has that connection activated.
- */
-
- parent_req = nm_device_get_act_request (parent_device);
- if (!parent_req)
- return FALSE;
-
- parent_connection = nm_active_connection_get_applied_connection (NM_ACTIVE_CONNECTION
(parent_req));
- if (!parent_connection)
- return FALSE;
-
- if (g_strcmp0 (parent, nm_connection_get_uuid (parent_connection)) != 0)
- return FALSE;
- } else {
- /* interface name */
- if (g_strcmp0 (parent, nm_device_get_ip_iface (parent_device)) != 0)
- return FALSE;
- }
-
- return TRUE;
-}
-
-static gboolean
match_hwaddr (NMDevice *device, NMConnection *connection, gboolean fail_if_no_hwaddr)
{
NMSettingWired *s_wired;
@@ -398,7 +360,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection)
/* Check parent interface; could be an interface name or a UUID */
parent = nm_setting_vlan_get_parent (s_vlan);
if (parent) {
- if (!match_parent (NM_DEVICE_VLAN (device), parent))
+ if (!nm_device_match_parent (device, parent))
return FALSE;
} else {
/* Parent could be a MAC address in an NMSettingWired */
diff --git a/src/devices/nm-device-vxlan.c b/src/devices/nm-device-vxlan.c
index 204b85c00..d9efe8404 100644
--- a/src/devices/nm-device-vxlan.c
+++ b/src/devices/nm-device-vxlan.c
@@ -231,43 +231,6 @@ create_and_realize (NMDevice *device,
}
static gboolean
-match_parent (NMDeviceVxlan *self, const char *parent)
-{
- NMDevice *parent_device;
-
- g_return_val_if_fail (parent != NULL, FALSE);
-
- parent_device = nm_device_parent_get_device (NM_DEVICE (self));
- if (!parent_device)
- return FALSE;
-
- if (nm_utils_is_uuid (parent)) {
- NMActRequest *parent_req;
- NMConnection *parent_connection;
-
- /* If the parent is a UUID, the connection matches if our parent
- * device has that connection activated.
- */
- parent_req = nm_device_get_act_request (parent_device);
- if (!parent_req)
- return FALSE;
-
- parent_connection = nm_active_connection_get_applied_connection (NM_ACTIVE_CONNECTION
(parent_req));
- if (!parent_connection)
- return FALSE;
-
- if (g_strcmp0 (parent, nm_connection_get_uuid (parent_connection)) != 0)
- return FALSE;
- } else {
- /* interface name */
- if (g_strcmp0 (parent, nm_device_get_ip_iface (parent_device)) != 0)
- return FALSE;
- }
-
- return TRUE;
-}
-
-static gboolean
address_matches (const char *str, in_addr_t addr4, struct in6_addr *addr6)
{
in_addr_t new_addr4 = 0;
@@ -302,8 +265,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection)
if (nm_device_is_real (device)) {
parent = nm_setting_vxlan_get_parent (s_vxlan);
- if ( parent
- && !match_parent (NM_DEVICE_VXLAN (device), parent))
+ if (parent && !nm_device_match_parent (device, parent))
return FALSE;
if (priv->props.id != nm_setting_vxlan_get_id (s_vxlan))
diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c
index 86e2f9d36..e14a15f79 100644
--- a/src/devices/nm-device.c
+++ b/src/devices/nm-device.c
@@ -4390,6 +4390,43 @@ nm_device_complete_connection (NMDevice *self,
return success;
}
+gboolean
+nm_device_match_parent (NMDevice *self, const char *parent)
+{
+ NMDevice *parent_device;
+
+ g_return_val_if_fail (parent, FALSE);
+
+ parent_device = nm_device_parent_get_device (self);
+ if (!parent_device)
+ return FALSE;
+
+ if (nm_utils_is_uuid (parent)) {
+ NMActiveConnection *ac;
+ NMConnection *connection;
+
+ /* If the parent is a UUID, the connection matches if our parent
+ * device has that connection activated.
+ */
+ ac = (NMActiveConnection *) nm_device_get_act_request (parent_device);
+ if (!ac)
+ return FALSE;
+
+ connection = nm_active_connection_get_applied_connection (ac);
+ if (!connection)
+ return FALSE;
+
+ if (!nm_streq (parent, nm_connection_get_uuid (connection)))
+ return FALSE;
+ } else {
+ /* Interface name */
+ if (!nm_streq (parent, nm_device_get_ip_iface (parent_device)))
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
static gboolean
check_connection_compatible (NMDevice *self, NMConnection *connection)
{
--
2.13.5
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]