From dbbec9b7d75b2afaefd991ac7d1c0404515bbe31 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Mon, 11 Mar 2013 10:16:24 -0400 Subject: [PATCH] libnm-glib: match interface name in nm_device_connection_compatible() Do NMSettingConnection:interface-name matching on the client side as well, so that, eg, nm-applet does not list connections under the wrong device. (Also, move some return-if-fail checks from the subclass method implementations into the wrapper function.) https://bugzilla.gnome.org/show_bug.cgi?id=693684 --- libnm-glib/libnm-glib.ver | 2 ++ libnm-glib/nm-device-adsl.c | 2 +- libnm-glib/nm-device-bond.c | 4 +-- libnm-glib/nm-device-bridge.c | 4 +-- libnm-glib/nm-device-bt.c | 4 +-- libnm-glib/nm-device-ethernet.c | 4 +-- libnm-glib/nm-device-infiniband.c | 4 +-- libnm-glib/nm-device-modem.c | 4 +-- libnm-glib/nm-device-olpc-mesh.c | 4 +-- libnm-glib/nm-device-vlan.c | 4 +-- libnm-glib/nm-device-wifi.c | 4 +-- libnm-glib/nm-device-wimax.c | 4 +-- libnm-glib/nm-device.c | 47 +++++++++++++++++++++++++++++-- libnm-glib/nm-device.h | 14 +++++++++ 14 files changed, 71 insertions(+), 34 deletions(-) diff --git a/libnm-glib/libnm-glib.ver b/libnm-glib/libnm-glib.ver index 571bc7d25c..e17e368f75 100644 --- a/libnm-glib/libnm-glib.ver +++ b/libnm-glib/libnm-glib.ver @@ -89,6 +89,8 @@ global: nm_device_connection_compatible; nm_device_connection_valid; nm_device_disconnect; + nm_device_error_get_type; + nm_device_error_quark; nm_device_ethernet_error_get_type; nm_device_ethernet_error_quark; nm_device_ethernet_get_carrier; diff --git a/libnm-glib/nm-device-adsl.c b/libnm-glib/nm-device-adsl.c index bef3ceae42..a2c21746bb 100644 --- a/libnm-glib/nm-device-adsl.c +++ b/libnm-glib/nm-device-adsl.c @@ -131,7 +131,7 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro return FALSE; } - return TRUE; + return NM_DEVICE_CLASS (nm_device_adsl_parent_class)->connection_compatible (device, connection, error); } /******************************************************************/ diff --git a/libnm-glib/nm-device-bond.c b/libnm-glib/nm-device-bond.c index 6261db9120..5cecadf5ef 100644 --- a/libnm-glib/nm-device-bond.c +++ b/libnm-glib/nm-device-bond.c @@ -165,8 +165,6 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro NMSettingBond *s_bond; const char *ctype, *dev_iface_name, *bond_iface_name; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - s_con = nm_connection_get_setting_connection (connection); g_assert (s_con); @@ -194,7 +192,7 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro /* FIXME: check slaves? */ - return TRUE; + return NM_DEVICE_CLASS (nm_device_bond_parent_class)->connection_compatible (device, connection, error); } /***********************************************************/ diff --git a/libnm-glib/nm-device-bridge.c b/libnm-glib/nm-device-bridge.c index 9327a93196..3bf470abe1 100644 --- a/libnm-glib/nm-device-bridge.c +++ b/libnm-glib/nm-device-bridge.c @@ -173,8 +173,6 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro NMSettingBridge *s_bridge; const char *ctype, *dev_iface_name, *bridge_iface_name; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - s_con = nm_connection_get_setting_connection (connection); g_assert (s_con); @@ -202,7 +200,7 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro /* FIXME: check ports? */ - return TRUE; + return NM_DEVICE_CLASS (nm_device_bridge_parent_class)->connection_compatible (device, connection, error); } /***********************************************************/ diff --git a/libnm-glib/nm-device-bt.c b/libnm-glib/nm-device-bt.c index ccfa83ca72..050767b85d 100644 --- a/libnm-glib/nm-device-bt.c +++ b/libnm-glib/nm-device-bt.c @@ -182,8 +182,6 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro NMBluetoothCapabilities dev_caps; NMBluetoothCapabilities bt_type; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - s_con = nm_connection_get_setting_connection (connection); g_assert (s_con); @@ -226,7 +224,7 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro return FALSE; } - return TRUE; + return NM_DEVICE_CLASS (nm_device_bt_parent_class)->connection_compatible (device, connection, error); } /************************************************************/ diff --git a/libnm-glib/nm-device-ethernet.c b/libnm-glib/nm-device-ethernet.c index e9d1b4ad0b..376533e439 100644 --- a/libnm-glib/nm-device-ethernet.c +++ b/libnm-glib/nm-device-ethernet.c @@ -183,8 +183,6 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro const char *ctype; gboolean is_pppoe = FALSE; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - s_con = nm_connection_get_setting_connection (connection); g_assert (s_con); @@ -230,7 +228,7 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro } } - return TRUE; + return NM_DEVICE_CLASS (nm_device_ethernet_parent_class)->connection_compatible (device, connection, error); } /***********************************************************/ diff --git a/libnm-glib/nm-device-infiniband.c b/libnm-glib/nm-device-infiniband.c index d611993be2..9bde425bef 100644 --- a/libnm-glib/nm-device-infiniband.c +++ b/libnm-glib/nm-device-infiniband.c @@ -143,8 +143,6 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro const GByteArray *mac; guint8 *hwaddr, hwaddr_buf[INFINIBAND_ALEN]; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - s_con = nm_connection_get_setting_connection (connection); g_assert (s_con); @@ -178,7 +176,7 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro } } - return TRUE; + return NM_DEVICE_CLASS (nm_device_infiniband_parent_class)->connection_compatible (device, connection, error); } /***********************************************************/ diff --git a/libnm-glib/nm-device-modem.c b/libnm-glib/nm-device-modem.c index a6998f3268..785eaad53a 100644 --- a/libnm-glib/nm-device-modem.c +++ b/libnm-glib/nm-device-modem.c @@ -123,8 +123,6 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro const char *ctype; NMDeviceModemCapabilities current_caps; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - s_con = nm_connection_get_setting_connection (connection); g_assert (s_con); @@ -152,7 +150,7 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro return FALSE; } - return TRUE; + return NM_DEVICE_CLASS (nm_device_modem_parent_class)->connection_compatible (device, connection, error); } /*******************************************************************/ diff --git a/libnm-glib/nm-device-olpc-mesh.c b/libnm-glib/nm-device-olpc-mesh.c index 7ca36890f8..95a60256e6 100644 --- a/libnm-glib/nm-device-olpc-mesh.c +++ b/libnm-glib/nm-device-olpc-mesh.c @@ -161,8 +161,6 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro NMSettingOlpcMesh *s_olpc_mesh; const char *ctype; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - s_con = nm_connection_get_setting_connection (connection); g_assert (s_con); @@ -180,7 +178,7 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro return FALSE; } - return TRUE; + return NM_DEVICE_CLASS (nm_device_olpc_mesh_parent_class)->connection_compatible (device, connection, error); } /**************************************************************/ diff --git a/libnm-glib/nm-device-vlan.c b/libnm-glib/nm-device-vlan.c index 13ca3ac335..8028bbdde2 100644 --- a/libnm-glib/nm-device-vlan.c +++ b/libnm-glib/nm-device-vlan.c @@ -158,8 +158,6 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro NMSettingVlan *s_vlan; const char *ctype, *dev_iface_name, *vlan_iface_name; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - s_con = nm_connection_get_setting_connection (connection); g_assert (s_con); @@ -191,7 +189,7 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro return FALSE; } - return TRUE; + return NM_DEVICE_CLASS (nm_device_vlan_parent_class)->connection_compatible (device, connection, error); } /***********************************************************/ diff --git a/libnm-glib/nm-device-wifi.c b/libnm-glib/nm-device-wifi.c index 47594a50d3..ca2dff4f0a 100644 --- a/libnm-glib/nm-device-wifi.c +++ b/libnm-glib/nm-device-wifi.c @@ -491,8 +491,6 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro NMDeviceWifiCapabilities wifi_caps; const char *key_mgmt; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - s_con = nm_connection_get_setting_connection (connection); g_assert (s_con); @@ -554,7 +552,7 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro } } - return TRUE; + return NM_DEVICE_CLASS (nm_device_wifi_parent_class)->connection_compatible (device, connection, error); } /**************************************************************/ diff --git a/libnm-glib/nm-device-wimax.c b/libnm-glib/nm-device-wimax.c index 62e3f77231..46dafec27b 100644 --- a/libnm-glib/nm-device-wimax.c +++ b/libnm-glib/nm-device-wimax.c @@ -390,8 +390,6 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro const char *hw_str; struct ether_addr *hw_mac; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - s_con = nm_connection_get_setting_connection (connection); g_assert (s_con); @@ -426,7 +424,7 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro } } - return TRUE; + return NM_DEVICE_CLASS (nm_device_wimax_parent_class)->connection_compatible (device, connection, error); } /**************************************************************/ diff --git a/libnm-glib/nm-device.c b/libnm-glib/nm-device.c index 5f492e2cac..769d2e5d2c 100644 --- a/libnm-glib/nm-device.c +++ b/libnm-glib/nm-device.c @@ -53,6 +53,7 @@ static void _nm_device_type_for_path_async (DBusGConnection *connection, const char *path, NMObjectTypeCallbackFunc callback, gpointer user_data); +gboolean connection_compatible (NMDevice *device, NMConnection *connection, GError **error); G_DEFINE_TYPE_WITH_CODE (NMDevice, nm_device, NM_TYPE_OBJECT, _nm_object_register_type_func (g_define_type_id, _nm_device_type_for_path, @@ -128,6 +129,22 @@ enum { static guint signals[LAST_SIGNAL] = { 0 }; +/** + * nm_device_error_quark: + * + * Registers an error quark for #NMDevice if necessary. + * + * Returns: the error quark used for #NMDevice errors. + **/ +GQuark +nm_device_error_quark (void) +{ + static GQuark quark = 0; + + if (G_UNLIKELY (quark == 0)) + quark = g_quark_from_static_string ("nm-device-error-quark"); + return quark; +} static void nm_device_init (NMDevice *device) @@ -492,6 +509,8 @@ nm_device_class_init (NMDeviceClass *device_class) object_class->dispose = dispose; object_class->finalize = finalize; + device_class->connection_compatible = connection_compatible; + /* properties */ /** @@ -1589,6 +1608,26 @@ nm_device_connection_valid (NMDevice *device, NMConnection *connection) return nm_device_connection_compatible (device, connection, NULL); } +gboolean +connection_compatible (NMDevice *device, NMConnection *connection, GError **error) +{ + NMSettingConnection *s_con; + const char *config_iface, *device_iface; + + s_con = nm_connection_get_setting_connection (connection); + g_assert (s_con); + + config_iface = nm_setting_connection_get_interface_name (s_con); + device_iface = nm_device_get_iface (device); + if (config_iface && g_strcmp0 (config_iface, device_iface) != 0) { + g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INTERFACE_MISMATCH, + "The interface names of the device and the connection didn't match."); + return FALSE; + } + + return TRUE; +} + /** * nm_device_connection_compatible: * @device: an #NMDevice to validate @connection against @@ -1613,9 +1652,11 @@ nm_device_connection_valid (NMDevice *device, NMConnection *connection) gboolean nm_device_connection_compatible (NMDevice *device, NMConnection *connection, GError **error) { - if (NM_DEVICE_GET_CLASS (device)->connection_compatible) - return NM_DEVICE_GET_CLASS (device)->connection_compatible (device, connection, error); - return FALSE; + g_return_val_if_fail (NM_IS_DEVICE (device), FALSE); + g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE); + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + + return NM_DEVICE_GET_CLASS (device)->connection_compatible (device, connection, error); } /** diff --git a/libnm-glib/nm-device.h b/libnm-glib/nm-device.h index c49675bccf..0f0e277629 100644 --- a/libnm-glib/nm-device.h +++ b/libnm-glib/nm-device.h @@ -45,6 +45,20 @@ G_BEGIN_DECLS #define NM_IS_DEVICE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_DEVICE)) #define NM_DEVICE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_DEVICE, NMDeviceClass)) +/** + * NMDeviceError: + * @NM_DEVICE_ERROR_UNKNOWN: unknown or unclassified error + * @NM_DEVICE_ERROR_INTERFACE_MISMATCH: the interface names of the connection and the + * device mismatched + */ +typedef enum { + NM_DEVICE_ERROR_UNKNOWN = 0, /*< nick=UnknownError >*/ + NM_DEVICE_ERROR_INTERFACE_MISMATCH, /*< nick=InterfaceMismatch >*/ +} NMDeviceError; + +#define NM_DEVICE_ERROR nm_device_error_quark () +GQuark nm_device_error_quark (void); + #define NM_DEVICE_DEVICE_TYPE "device-type" #define NM_DEVICE_UDI "udi" #define NM_DEVICE_INTERFACE "interface"