From 1cc367711d1aa574e8de66785472f274aa5809cc Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Wed, 6 Mar 2013 16:16:54 -0500 Subject: [PATCH] core: simplify nm_device_get_best_auto_connection() As with the other connection-matching methods, move the loop and the device-independent bits into NMDevice. By reusing nm_device_check_connection_compatible(), this means that most device types now no longer need any type-specific code for this. https://bugzilla.gnome.org/show_bug.cgi?id=693684 --- src/modem-manager/nm-modem-broadband.c | 42 ---------- src/modem-manager/nm-modem-cdma.c | 17 ----- src/modem-manager/nm-modem-gsm.c | 17 ----- src/modem-manager/nm-modem.c | 12 --- src/modem-manager/nm-modem.h | 8 -- src/nm-device-adsl.c | 20 ----- src/nm-device-bond.c | 29 +------ src/nm-device-bridge.c | 33 +------- src/nm-device-bt.c | 35 ++++----- src/nm-device-ethernet.c | 31 +------- src/nm-device-infiniband.c | 33 -------- src/nm-device-modem.c | 11 --- src/nm-device-olpc-mesh.c | 9 +++ src/nm-device-vlan.c | 35 ++------- src/nm-device-wifi.c | 102 +++++++++---------------- src/nm-device.c | 42 +++++----- src/nm-device.h | 6 +- src/wimax/nm-device-wimax.c | 49 ++++-------- 18 files changed, 108 insertions(+), 423 deletions(-) diff --git a/src/modem-manager/nm-modem-broadband.c b/src/modem-manager/nm-modem-broadband.c index 05cd35651d..88d5c8a6a7 100644 --- a/src/modem-manager/nm-modem-broadband.c +++ b/src/modem-manager/nm-modem-broadband.c @@ -374,47 +374,6 @@ act_stage1_prepare (NMModem *_self, /*****************************************************************************/ -static NMConnection * -get_best_auto_connection (NMModem *_self, - GSList *connections, - char **specific_object) -{ - NMModemBroadband *self = NM_MODEM_BROADBAND (_self); - MMModemCapability modem_caps; - GSList *iter; - - modem_caps = mm_modem_get_current_capabilities (self->priv->modem_iface); - - for (iter = connections; iter; iter = g_slist_next (iter)) { - NMConnection *connection = NM_CONNECTION (iter->data); - NMSettingConnection *s_con; - - s_con = nm_connection_get_setting_connection (connection); - g_assert (s_con); - - if (!nm_setting_connection_get_autoconnect (s_con)) - continue; - - /* If GSM settings given and our modem is 3GPP, those are the best ones */ - if ( g_str_equal (nm_setting_connection_get_connection_type (s_con), - NM_SETTING_GSM_SETTING_NAME) - && MODEM_CAPS_3GPP (modem_caps)) - return connection; - - /* If CDMA settings given and our modem is 3GPP2, return those */ - if ( g_str_equal (nm_setting_connection_get_connection_type (s_con), - NM_SETTING_CDMA_SETTING_NAME) - && MODEM_CAPS_3GPP2 (modem_caps)) - return connection; - - /* continue */ - } - - return NULL; -} - -/*****************************************************************************/ - static gboolean check_connection_compatible (NMModem *_self, NMConnection *connection, @@ -1017,7 +976,6 @@ nm_modem_broadband_class_init (NMModemBroadbandClass *klass) modem_class->set_mm_enabled = set_mm_enabled; modem_class->get_user_pass = get_user_pass; modem_class->get_setting_name = get_setting_name; - modem_class->get_best_auto_connection = get_best_auto_connection; modem_class->check_connection_compatible = check_connection_compatible; modem_class->complete_connection = complete_connection; modem_class->act_stage1_prepare = act_stage1_prepare; diff --git a/src/modem-manager/nm-modem-cdma.c b/src/modem-manager/nm-modem-cdma.c index 6bed8c2a48..45632b4994 100644 --- a/src/modem-manager/nm-modem-cdma.c +++ b/src/modem-manager/nm-modem-cdma.c @@ -192,22 +192,6 @@ act_stage1_prepare (NMModem *modem, return NM_ACT_STAGE_RETURN_POSTPONE; } -static NMConnection * -get_best_auto_connection (NMModem *modem, - GSList *connections, - char **specific_object) -{ - GSList *iter; - - for (iter = connections; iter; iter = g_slist_next (iter)) { - NMConnection *connection = NM_CONNECTION (iter->data); - - if (nm_connection_is_type (connection, NM_SETTING_CDMA_SETTING_NAME)) - return connection; - } - return NULL; -} - static gboolean check_connection_compatible (NMModem *modem, NMConnection *connection, @@ -348,7 +332,6 @@ nm_modem_cdma_class_init (NMModemCdmaClass *klass) object_class->dispose = dispose; modem_class->get_user_pass = get_user_pass; modem_class->get_setting_name = get_setting_name; - modem_class->get_best_auto_connection = get_best_auto_connection; modem_class->check_connection_compatible = check_connection_compatible; modem_class->complete_connection = complete_connection; modem_class->act_stage1_prepare = act_stage1_prepare; diff --git a/src/modem-manager/nm-modem-gsm.c b/src/modem-manager/nm-modem-gsm.c index c9a7ce1963..fdb5b8b7a7 100644 --- a/src/modem-manager/nm-modem-gsm.c +++ b/src/modem-manager/nm-modem-gsm.c @@ -464,22 +464,6 @@ act_stage1_prepare (NMModem *modem, return NM_ACT_STAGE_RETURN_POSTPONE; } -static NMConnection * -get_best_auto_connection (NMModem *modem, - GSList *connections, - char **specific_object) -{ - GSList *iter; - - for (iter = connections; iter; iter = g_slist_next (iter)) { - NMConnection *connection = NM_CONNECTION (iter->data); - - if (nm_connection_is_type (connection, NM_SETTING_GSM_SETTING_NAME)) - return connection; - } - return NULL; -} - static gboolean check_connection_compatible (NMModem *modem, NMConnection *connection, @@ -632,7 +616,6 @@ nm_modem_gsm_class_init (NMModemGsmClass *klass) object_class->dispose = dispose; modem_class->get_user_pass = get_user_pass; modem_class->get_setting_name = get_setting_name; - modem_class->get_best_auto_connection = get_best_auto_connection; modem_class->check_connection_compatible = check_connection_compatible; modem_class->complete_connection = complete_connection; modem_class->act_stage1_prepare = act_stage1_prepare; diff --git a/src/modem-manager/nm-modem.c b/src/modem-manager/nm-modem.c index 487b1c49bb..7c98f49d77 100644 --- a/src/modem-manager/nm-modem.c +++ b/src/modem-manager/nm-modem.c @@ -475,18 +475,6 @@ nm_modem_act_stage2_config (NMModem *self, /*****************************************************************************/ -NMConnection * -nm_modem_get_best_auto_connection (NMModem *self, - GSList *connections, - char **specific_object) -{ - if (NM_MODEM_GET_CLASS (self)->get_best_auto_connection) - return NM_MODEM_GET_CLASS (self)->get_best_auto_connection (self, connections, specific_object); - return NULL; -} - -/*****************************************************************************/ - gboolean nm_modem_check_connection_compatible (NMModem *self, NMConnection *connection, diff --git a/src/modem-manager/nm-modem.h b/src/modem-manager/nm-modem.h index d01aaf2cbe..add4bb31a7 100644 --- a/src/modem-manager/nm-modem.h +++ b/src/modem-manager/nm-modem.h @@ -79,10 +79,6 @@ typedef struct { const GSList *existing_connections, GError **error); - NMConnection * (*get_best_auto_connection) (NMModem *modem, - GSList *connections, - char **specific_object); - NMActStageReturn (*act_stage1_prepare) (NMModem *modem, NMActRequest *req, GPtrArray **out_hints, @@ -117,10 +113,6 @@ const char *nm_modem_get_uid (NMModem *modem); const char *nm_modem_get_control_port (NMModem *modem); const char *nm_modem_get_data_port (NMModem *modem); -NMConnection *nm_modem_get_best_auto_connection (NMModem *self, - GSList *connections, - char **specific_object); - gboolean nm_modem_check_connection_compatible (NMModem *self, NMConnection *connection, GError **error); diff --git a/src/nm-device-adsl.c b/src/nm-device-adsl.c index da546cdf25..f92f3c4064 100644 --- a/src/nm-device-adsl.c +++ b/src/nm-device-adsl.c @@ -198,25 +198,6 @@ complete_connection (NMDevice *device, return TRUE; } -static NMConnection * -get_best_auto_connection (NMDevice *dev, - GSList *connections, - char **specific_object) -{ - GSList *iter; - - for (iter = connections; iter; iter = g_slist_next (iter)) { - NMConnection *connection = NM_CONNECTION (iter->data); - - if (!nm_connection_is_type (connection, NM_SETTING_ADSL_SETTING_NAME)) - continue; - - if (nm_connection_get_setting_adsl (connection)) - return connection; - } - return NULL; -} - /**************************************************************/ static void @@ -807,7 +788,6 @@ nm_device_adsl_class_init (NMDeviceAdslClass *klass) parent_class->is_available = is_available; parent_class->check_connection_compatible = check_connection_compatible; - parent_class->get_best_auto_connection = get_best_auto_connection; parent_class->complete_connection = complete_connection; parent_class->get_hw_address = get_hw_address; diff --git a/src/nm-device-bond.c b/src/nm-device-bond.c index 341efeb61b..085eda8ef1 100644 --- a/src/nm-device-bond.c +++ b/src/nm-device-bond.c @@ -117,7 +117,9 @@ is_available (NMDevice *dev, gboolean need_carrier) } static gboolean -match_bond_connection (NMDevice *device, NMConnection *connection, GError **error) +check_connection_compatible (NMDevice *device, + NMConnection *connection, + GError **error) { const char *iface; NMSettingBond *s_bond; @@ -142,30 +144,6 @@ match_bond_connection (NMDevice *device, NMConnection *connection, GError **erro return TRUE; } -static NMConnection * -get_best_auto_connection (NMDevice *dev, - GSList *connections, - char **specific_object) -{ - GSList *iter; - - for (iter = connections; iter; iter = g_slist_next (iter)) { - NMConnection *connection = NM_CONNECTION (iter->data); - - if (match_bond_connection (dev, connection, NULL)) - return connection; - } - return NULL; -} - -static gboolean -check_connection_compatible (NMDevice *device, - NMConnection *connection, - GError **error) -{ - return match_bond_connection (device, connection, error); -} - static gboolean complete_connection (NMDevice *device, NMConnection *connection, @@ -403,7 +381,6 @@ nm_device_bond_class_init (NMDeviceBondClass *klass) parent_class->update_hw_address = update_hw_address; parent_class->get_hw_address = get_hw_address; parent_class->is_available = is_available; - parent_class->get_best_auto_connection = get_best_auto_connection; parent_class->check_connection_compatible = check_connection_compatible; parent_class->complete_connection = complete_connection; diff --git a/src/nm-device-bridge.c b/src/nm-device-bridge.c index 93a11edd43..f9ba0f72e5 100644 --- a/src/nm-device-bridge.c +++ b/src/nm-device-bridge.c @@ -117,7 +117,9 @@ is_available (NMDevice *dev, gboolean need_carrier) } static gboolean -match_bridge_connection (NMDevice *device, NMConnection *connection, GError **error) +check_connection_compatible (NMDevice *device, + NMConnection *connection, + GError **error) { const char *iface; NMSettingBridge *s_bridge; @@ -140,34 +142,6 @@ match_bridge_connection (NMDevice *device, NMConnection *connection, GError **er return TRUE; } -static NMConnection * -get_best_auto_connection (NMDevice *dev, - GSList *connections, - char **specific_object) -{ - GSList *iter; - - for (iter = connections; iter; iter = g_slist_next (iter)) { - NMConnection *connection = NM_CONNECTION (iter->data); - NMSettingConnection *s_con; - - s_con = nm_connection_get_setting_connection (connection); - g_assert (s_con); - if ( nm_setting_connection_get_autoconnect (s_con) - && match_bridge_connection (dev, connection, NULL)) - return connection; - } - return NULL; -} - -static gboolean -check_connection_compatible (NMDevice *device, - NMConnection *connection, - GError **error) -{ - return match_bridge_connection (device, connection, error); -} - static gboolean complete_connection (NMDevice *device, NMConnection *connection, @@ -459,7 +433,6 @@ nm_device_bridge_class_init (NMDeviceBridgeClass *klass) parent_class->update_hw_address = update_hw_address; parent_class->get_hw_address = get_hw_address; parent_class->is_available = is_available; - parent_class->get_best_auto_connection = get_best_auto_connection; parent_class->check_connection_compatible = check_connection_compatible; parent_class->complete_connection = complete_connection; diff --git a/src/nm-device-bt.c b/src/nm-device-bt.c index 6500d3731e..c517e28b76 100644 --- a/src/nm-device-bt.c +++ b/src/nm-device-bt.c @@ -138,32 +138,23 @@ get_connection_bt_type (NMConnection *connection) return NM_BT_CAPABILITY_NONE; } -static NMConnection * -get_best_auto_connection (NMDevice *device, - GSList *connections, - char **specific_object) +static gboolean +can_auto_connect (NMDevice *device, + NMConnection *connection, + char **specific_object) { NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (device); - GSList *iter; + guint32 bt_type; - for (iter = connections; iter; iter = g_slist_next (iter)) { - NMConnection *connection = NM_CONNECTION (iter->data); - guint32 bt_type; + if (!NM_DEVICE_CLASS (nm_device_bt_parent_class)->can_auto_connect (device, connection, specific_object)) + return FALSE; - if (!nm_connection_is_type (connection, NM_SETTING_BLUETOOTH_SETTING_NAME)) - continue; + /* Can't auto-activate a DUN connection without ModemManager */ + bt_type = get_connection_bt_type (connection); + if (bt_type == NM_BT_CAPABILITY_DUN && priv->mm_running == FALSE) + return FALSE; - bt_type = get_connection_bt_type (connection); - if (!(bt_type & priv->capabilities)) - continue; - - /* Can't auto-activate a DUN connection without ModemManager */ - if (bt_type == NM_BT_CAPABILITY_DUN && priv->mm_running == FALSE) - continue; - - return connection; - } - return NULL; + return TRUE; } static gboolean @@ -1307,7 +1298,7 @@ nm_device_bt_class_init (NMDeviceBtClass *klass) object_class->dispose = dispose; object_class->finalize = finalize; - device_class->get_best_auto_connection = get_best_auto_connection; + device_class->can_auto_connect = can_auto_connect; device_class->get_generic_capabilities = get_generic_capabilities; device_class->deactivate = deactivate; device_class->act_stage2_config = act_stage2_config; diff --git a/src/nm-device-ethernet.c b/src/nm-device-ethernet.c index bc94e8df11..0655fabdf6 100644 --- a/src/nm-device-ethernet.c +++ b/src/nm-device-ethernet.c @@ -545,8 +545,9 @@ match_subchans (NMDeviceEthernet *self, NMSettingWired *s_wired, gboolean *try_m } static gboolean -match_ethernet_connection (NMDevice *device, NMConnection *connection, - GError **error) +check_connection_compatible (NMDevice *device, + NMConnection *connection, + GError **error) { NMDeviceEthernet *self = NM_DEVICE_ETHERNET (device); NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (self); @@ -613,23 +614,6 @@ match_ethernet_connection (NMDevice *device, NMConnection *connection, return TRUE; } -static NMConnection * -get_best_auto_connection (NMDevice *dev, - GSList *connections, - char **specific_object) -{ - GSList *iter; - - for (iter = connections; iter; iter = g_slist_next (iter)) { - NMConnection *connection = NM_CONNECTION (iter->data); - - if (match_ethernet_connection (dev, connection, NULL)) - return connection; - } - - return NULL; -} - /* FIXME: Move it to nm-device.c and then get rid of all foo_device_get_setting() all around. It's here now to keep the patch short. */ static NMSetting * @@ -1278,14 +1262,6 @@ deactivate (NMDevice *device) _set_hw_addr (self, priv->initial_hw_addr, "reset"); } -static gboolean -check_connection_compatible (NMDevice *device, - NMConnection *connection, - GError **error) -{ - return match_ethernet_connection (device, connection, error); -} - static gboolean complete_connection (NMDevice *device, NMConnection *connection, @@ -1475,7 +1451,6 @@ nm_device_ethernet_class_init (NMDeviceEthernetClass *klass) parent_class->get_hw_address = get_hw_address; parent_class->update_permanent_hw_address = update_permanent_hw_address; parent_class->update_initial_hw_address = update_initial_hw_address; - parent_class->get_best_auto_connection = get_best_auto_connection; parent_class->check_connection_compatible = check_connection_compatible; parent_class->complete_connection = complete_connection; diff --git a/src/nm-device-infiniband.c b/src/nm-device-infiniband.c index 4613c66a76..a20b347e3a 100644 --- a/src/nm-device-infiniband.c +++ b/src/nm-device-infiniband.c @@ -148,38 +148,6 @@ get_generic_capabilities (NMDevice *dev) return NM_DEVICE_CAP_CARRIER_DETECT | NM_DEVICE_CAP_NM_SUPPORTED; } -static NMConnection * -get_best_auto_connection (NMDevice *dev, - GSList *connections, - char **specific_object) -{ - NMDeviceInfinibandPrivate *priv = NM_DEVICE_INFINIBAND_GET_PRIVATE (dev); - GSList *iter; - - for (iter = connections; iter; iter = g_slist_next (iter)) { - NMConnection *connection = NM_CONNECTION (iter->data); - NMSettingInfiniband *s_infiniband; - - if (!nm_connection_is_type (connection, NM_SETTING_INFINIBAND_SETTING_NAME)) - continue; - - s_infiniband = nm_connection_get_setting_infiniband (connection); - if (!s_infiniband) - continue; - - if (s_infiniband) { - const GByteArray *mac; - - mac = nm_setting_infiniband_get_mac_address (s_infiniband); - if (mac && memcmp (mac->data, priv->hw_addr, INFINIBAND_ALEN)) - continue; - } - - return connection; - } - return NULL; -} - static NMActStageReturn act_stage1_prepare (NMDevice *dev, NMDeviceStateReason *reason) { @@ -414,7 +382,6 @@ nm_device_infiniband_class_init (NMDeviceInfinibandClass *klass) parent_class->get_generic_capabilities = get_generic_capabilities; parent_class->update_hw_address = update_hw_address; parent_class->get_hw_address = get_hw_address; - parent_class->get_best_auto_connection = get_best_auto_connection; parent_class->check_connection_compatible = check_connection_compatible; parent_class->complete_connection = complete_connection; diff --git a/src/nm-device-modem.c b/src/nm-device-modem.c index 3c93c44872..0453d08036 100644 --- a/src/nm-device-modem.c +++ b/src/nm-device-modem.c @@ -221,16 +221,6 @@ get_generic_capabilities (NMDevice *device) return NM_DEVICE_CAP_NM_SUPPORTED; } -static NMConnection * -get_best_auto_connection (NMDevice *device, - GSList *connections, - char **specific_object) -{ - NMDeviceModemPrivate *priv = NM_DEVICE_MODEM_GET_PRIVATE (device); - - return nm_modem_get_best_auto_connection (priv->modem, connections, specific_object); -} - static gboolean check_connection_compatible (NMDevice *device, NMConnection *connection, @@ -487,7 +477,6 @@ nm_device_modem_class_init (NMDeviceModemClass *mclass) object_class->set_property = set_property; device_class->get_generic_capabilities = get_generic_capabilities; - device_class->get_best_auto_connection = get_best_auto_connection; device_class->check_connection_compatible = check_connection_compatible; device_class->complete_connection = complete_connection; device_class->deactivate = deactivate; diff --git a/src/nm-device-olpc-mesh.c b/src/nm-device-olpc-mesh.c index 1417736fbd..302383c898 100644 --- a/src/nm-device-olpc-mesh.c +++ b/src/nm-device-olpc-mesh.c @@ -252,6 +252,14 @@ check_connection_compatible (NMDevice *device, return TRUE; } +static gboolean +can_auto_connect (NMDevice *device, + NMConnection *connection, + char **specific_object) +{ + return FALSE; +} + #define DEFAULT_SSID "olpc-mesh" static gboolean @@ -515,6 +523,7 @@ nm_device_olpc_mesh_class_init (NMDeviceOlpcMeshClass *klass) parent_class->update_hw_address = update_hw_address; parent_class->get_hw_address = get_hw_address; parent_class->check_connection_compatible = check_connection_compatible; + parent_class->can_auto_connect = can_auto_connect; parent_class->complete_connection = complete_connection; parent_class->act_stage1_prepare = act_stage1_prepare; diff --git a/src/nm-device-vlan.c b/src/nm-device-vlan.c index bfa5d5fe2f..a72bf1dac7 100644 --- a/src/nm-device-vlan.c +++ b/src/nm-device-vlan.c @@ -240,9 +240,11 @@ match_parent (NMDeviceVlan *self, const char *parent, GError **error) } static gboolean -match_vlan_connection (NMDeviceVlan *self, NMConnection *connection, GError **error) +check_connection_compatible (NMDevice *device, + NMConnection *connection, + GError **error) { - NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (self); + NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (device); NMSettingVlan *s_vlan; const char *parent, *iface = NULL; @@ -262,7 +264,7 @@ match_vlan_connection (NMDeviceVlan *self, NMConnection *connection, GError **er /* Check parent interface; could be an interface name or a UUID */ parent = nm_setting_vlan_get_parent (s_vlan); if (parent) { - if (!match_parent (self, parent, error)) + if (!match_parent (NM_DEVICE_VLAN (device), parent, error)) return FALSE; } else { /* Parent could be a MAC address in a hardware-specific setting */ @@ -279,7 +281,7 @@ match_vlan_connection (NMDeviceVlan *self, NMConnection *connection, GError **er */ iface = nm_connection_get_virtual_iface_name (connection); if (iface) { - if (g_strcmp0 (nm_device_get_ip_iface (NM_DEVICE (self)), iface) != 0) { + if (g_strcmp0 (nm_device_get_ip_iface (device), iface) != 0) { g_set_error (error, NM_VLAN_ERROR, NM_VLAN_ERROR_CONNECTION_INVALID, "The VLAN connection virtual interface name did not match."); return FALSE; @@ -289,30 +291,6 @@ match_vlan_connection (NMDeviceVlan *self, NMConnection *connection, GError **er return TRUE; } -static NMConnection * -get_best_auto_connection (NMDevice *dev, - GSList *connections, - char **specific_object) -{ - GSList *iter; - - for (iter = connections; iter; iter = g_slist_next (iter)) { - NMConnection *connection = NM_CONNECTION (iter->data); - - if (match_vlan_connection (NM_DEVICE_VLAN (dev), connection, NULL)) - return connection; - } - return NULL; -} - -static gboolean -check_connection_compatible (NMDevice *device, - NMConnection *connection, - GError **error) -{ - return match_vlan_connection (NM_DEVICE_VLAN (device), connection, error); -} - static gboolean complete_connection (NMDevice *device, NMConnection *connection, @@ -619,7 +597,6 @@ nm_device_vlan_class_init (NMDeviceVlanClass *klass) parent_class->can_interrupt_activation = can_interrupt_activation; parent_class->is_available = is_available; - parent_class->get_best_auto_connection = get_best_auto_connection; parent_class->check_connection_compatible = check_connection_compatible; parent_class->complete_connection = complete_connection; parent_class->match_l2_config = match_l2_config; diff --git a/src/nm-device-wifi.c b/src/nm-device-wifi.c index 8949a95602..f0be008986 100644 --- a/src/nm-device-wifi.c +++ b/src/nm-device-wifi.c @@ -1389,83 +1389,49 @@ is_available (NMDevice *dev, gboolean need_carrier) return TRUE; } -static NMConnection * -get_best_auto_connection (NMDevice *dev, - GSList *connections, - char **specific_object) +static gboolean +can_auto_connect (NMDevice *dev, + NMConnection *connection, + char **specific_object) { NMDeviceWifi *self = NM_DEVICE_WIFI (dev); NMDeviceWifiPrivate *priv = NM_DEVICE_WIFI_GET_PRIVATE (self); - GSList *iter, *ap_iter; + GSList *ap_iter; + NMSettingIP4Config *s_ip4; + const char *method = NULL; + guint64 timestamp = 0; - for (iter = connections; iter; iter = g_slist_next (iter)) { - NMConnection *connection = NM_CONNECTION (iter->data); - NMSettingWireless *s_wireless; - const GByteArray *mac; - const GSList *mac_blacklist, *mac_blacklist_iter; - gboolean mac_blacklist_found = FALSE; - NMSettingIP4Config *s_ip4; - const char *method = NULL; - guint64 timestamp = 0; + if (!NM_DEVICE_CLASS (nm_device_wifi_parent_class)->can_auto_connect (dev, connection, specific_object)) + return FALSE; - if (!nm_connection_is_type (connection, NM_SETTING_WIRELESS_SETTING_NAME)) - continue; + /* Don't autoconnect to networks that have been tried at least once + * but haven't been successful, since these are often accidental choices + * from the menu and the user may not know the password. + */ + if (nm_settings_connection_get_timestamp (NM_SETTINGS_CONNECTION (connection), ×tamp)) { + if (timestamp == 0) + return FALSE; + } - /* Don't autoconnect to networks that have been tried at least once - * but haven't been successful, since these are often accidental choices - * from the menu and the user may not know the password. - */ - if (nm_settings_connection_get_timestamp (NM_SETTINGS_CONNECTION (connection), ×tamp)) { - if (timestamp == 0) - continue; - } + /* Use the connection if it's a shared connection */ + s_ip4 = nm_connection_get_setting_ip4_config (connection); + if (s_ip4) { + method = nm_setting_ip4_config_get_method (s_ip4); + if (!strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_SHARED)) + return TRUE; + } - s_wireless = nm_connection_get_setting_wireless (connection); - if (!s_wireless) - continue; + for (ap_iter = priv->ap_list; ap_iter; ap_iter = g_slist_next (ap_iter)) { + NMAccessPoint *ap = NM_AP (ap_iter->data); - mac = nm_setting_wireless_get_mac_address (s_wireless); - if (mac && memcmp (mac->data, &priv->perm_hw_addr, ETH_ALEN)) - continue; - - /* Check for MAC address blacklist */ - mac_blacklist = nm_setting_wireless_get_mac_address_blacklist (s_wireless); - for (mac_blacklist_iter = mac_blacklist; mac_blacklist_iter; - mac_blacklist_iter = g_slist_next (mac_blacklist_iter)) { - struct ether_addr addr; - - if (!ether_aton_r (mac_blacklist_iter->data, &addr)) { - g_warn_if_reached (); - continue; - } - if (memcmp (&addr, &priv->perm_hw_addr, ETH_ALEN) == 0) { - mac_blacklist_found = TRUE; - break; - } - } - /* Found device MAC address in the blacklist - do not use this connection */ - if (mac_blacklist_found) - continue; - - /* Use the connection if it's a shared connection */ - s_ip4 = nm_connection_get_setting_ip4_config (connection); - if (s_ip4) - method = nm_setting_ip4_config_get_method (s_ip4); - - if (s_ip4 && !strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_SHARED)) - return connection; - - for (ap_iter = priv->ap_list; ap_iter; ap_iter = g_slist_next (ap_iter)) { - NMAccessPoint *ap = NM_AP (ap_iter->data); - - if (nm_ap_check_compatible (ap, connection)) { - /* All good; connection is usable */ - *specific_object = (char *) nm_ap_get_dbus_path (ap); - return connection; - } + if (nm_ap_check_compatible (ap, connection)) { + /* All good; connection is usable */ + *specific_object = (char *) nm_ap_get_dbus_path (ap); + return TRUE; } } - return NULL; + + return FALSE; } static void @@ -3751,7 +3717,7 @@ nm_device_wifi_class_init (NMDeviceWifiClass *klass) parent_class->get_hw_address = get_hw_address; parent_class->update_permanent_hw_address = update_permanent_hw_address; parent_class->update_initial_hw_address = update_initial_hw_address; - parent_class->get_best_auto_connection = get_best_auto_connection; + parent_class->can_auto_connect = can_auto_connect; parent_class->is_available = is_available; parent_class->check_connection_compatible = check_connection_compatible; parent_class->check_connection_available = check_connection_available; diff --git a/src/nm-device.c b/src/nm-device.c index 5da51e0ac7..2b99e25b80 100644 --- a/src/nm-device.c +++ b/src/nm-device.c @@ -1375,14 +1375,27 @@ nm_device_autoconnect_allowed (NMDevice *self) return g_value_get_boolean (&retval); } +static gboolean +can_auto_connect (NMDevice *device, + NMConnection *connection, + char **specific_object) +{ + NMSettingConnection *s_con; + + s_con = nm_connection_get_setting_connection (connection); + if (!nm_setting_connection_get_autoconnect (s_con)) + return FALSE; + + return nm_device_check_connection_compatible (device, connection, NULL); +} + NMConnection * nm_device_get_best_auto_connection (NMDevice *dev, GSList *connections, char **specific_object) { guint32 caps; - GSList *iter, *available_conns; - NMConnection *best_connection; + GSList *iter; gboolean need_ignore_carrier = FALSE; g_return_val_if_fail (NM_IS_DEVICE (dev), NULL); @@ -1400,36 +1413,22 @@ nm_device_get_best_auto_connection (NMDevice *dev, need_ignore_carrier = TRUE; } - if (!NM_DEVICE_GET_CLASS (dev)->get_best_auto_connection) - return NULL; - - available_conns = NULL; for (iter = connections; iter; iter = iter->next) { NMConnection *connection = NM_CONNECTION (iter->data); - NMSettingConnection *s_con; - const char *carrier_detect; - - s_con = nm_connection_get_setting_connection (connection); - g_assert (s_con); - if (!nm_setting_connection_get_autoconnect (s_con)) - continue; if (need_ignore_carrier) { + const char *carrier_detect; + carrier_detect = nm_connection_get_carrier_detect (connection); if (g_strcmp0 (carrier_detect, "no") != 0) continue; } - available_conns = g_slist_prepend (available_conns, connection); + if (NM_DEVICE_GET_CLASS (dev)->can_auto_connect (dev, connection, specific_object)) + return connection; } - if (!available_conns) - return NULL; - - best_connection = NM_DEVICE_GET_CLASS (dev)->get_best_auto_connection (dev, available_conns, specific_object); - - g_slist_free (available_conns); - return best_connection; + return NULL; } gboolean @@ -4748,6 +4747,7 @@ nm_device_class_init (NMDeviceClass *klass) klass->have_any_ready_slaves = have_any_ready_slaves; klass->spec_match_list = spec_match_list; + klass->can_auto_connect = can_auto_connect; klass->check_connection_available = check_connection_available; klass->hw_is_up = hw_is_up; klass->hw_bring_up = hw_bring_up; diff --git a/src/nm-device.h b/src/nm-device.h index 2445b8ee92..f1b92c8488 100644 --- a/src/nm-device.h +++ b/src/nm-device.h @@ -125,9 +125,9 @@ typedef struct { void (* set_enabled) (NMDevice *self, gboolean enabled); - NMConnection * (* get_best_auto_connection) (NMDevice *self, - GSList *connections, - char **specific_object); + gboolean (* can_auto_connect) (NMDevice *self, + NMConnection *connection, + char **specific_object); /* Checks whether the connection is compatible with the device using * only the devices type and characteristics. Does not use any live diff --git a/src/wimax/nm-device-wimax.c b/src/wimax/nm-device-wimax.c index 7883ddd905..f704f7b051 100644 --- a/src/wimax/nm-device-wimax.c +++ b/src/wimax/nm-device-wimax.c @@ -577,50 +577,27 @@ complete_connection (NMDevice *device, return TRUE; } -static NMConnection * -get_best_auto_connection (NMDevice *device, - GSList *connections, - char **specific_object) +static gboolean +can_auto_connect (NMDevice *device, + NMConnection *connection, + char **specific_object) { NMDeviceWimaxPrivate *priv = NM_DEVICE_WIMAX_GET_PRIVATE (device); GSList *iter; - for (iter = connections; iter; iter = g_slist_next (iter)) { - NMConnection *connection = NM_CONNECTION (iter->data); - NMSettingConnection *s_con; - NMSettingWimax *s_wimax; - const char *connection_type; - const GByteArray *mac; + if (!NM_DEVICE_CLASS (nm_device_wimax_parent_class)->can_auto_connect (device, connection, specific_object)) + return FALSE; - s_con = nm_connection_get_setting_connection (connection); - g_assert (s_con); + for (iter = priv->nsp_list; iter; iter = iter->next) { + NMWimaxNsp *nsp = NM_WIMAX_NSP (iter->data); - if (!nm_setting_connection_get_autoconnect (s_con)) - continue; - - connection_type = nm_setting_connection_get_connection_type (s_con); - if (strcmp (connection_type, NM_SETTING_WIMAX_SETTING_NAME)) - continue; - - s_wimax = nm_connection_get_setting_wimax (connection); - if (!s_wimax) - continue; - - mac = nm_setting_wimax_get_mac_address (s_wimax); - if (mac && memcmp (mac->data, priv->hw_addr, ETH_ALEN)) - continue; - - for (iter = priv->nsp_list; iter; iter = iter->next) { - NMWimaxNsp *nsp = NM_WIMAX_NSP (iter->data); - - if (nm_wimax_nsp_check_compatible (nsp, connection)) { - *specific_object = (char *) nm_wimax_nsp_get_dbus_path (nsp); - return connection; - } + if (nm_wimax_nsp_check_compatible (nsp, connection)) { + *specific_object = (char *) nm_wimax_nsp_get_dbus_path (nsp); + return TRUE; } } - return NULL; + return FALSE; } static guint32 @@ -1491,7 +1468,7 @@ nm_device_wimax_class_init (NMDeviceWimaxClass *klass) device_class->check_connection_compatible = check_connection_compatible; device_class->check_connection_available = check_connection_available; device_class->complete_connection = complete_connection; - device_class->get_best_auto_connection = get_best_auto_connection; + device_class->can_auto_connect = can_auto_connect; device_class->get_generic_capabilities = get_generic_capabilities; device_class->is_available = is_available; device_class->act_stage1_prepare = act_stage1_prepare;