diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c index 0f67889719..ee8c61f370 100644 --- a/src/NetworkManagerUtils.c +++ b/src/NetworkManagerUtils.c @@ -36,6 +36,9 @@ #include "nm-dbus-manager.h" #include "nm-dispatcher-action.h" #include "nm-dbus-glib-types.h" +#include "nm-setting-connection.h" +#include "nm-setting-ip4-config.h" +#include "nm-setting-ip6-config.h" #include #include @@ -708,3 +711,118 @@ nm_utils_get_proc_sys_net_value (const char *path, return success; } +static char * +get_new_connection_name (const GSList *existing, + const char *format, + const char *preferred) +{ + GSList *names = NULL; + const GSList *iter; + char *cname = NULL; + int i = 0; + gboolean preferred_found = FALSE; + + for (iter = existing; iter; iter = g_slist_next (iter)) { + NMConnection *candidate = NM_CONNECTION (iter->data); + NMSettingConnection *s_con; + const char *id; + + s_con = NM_SETTING_CONNECTION (nm_connection_get_setting (candidate, NM_TYPE_SETTING_CONNECTION)); + id = nm_setting_connection_get_id (s_con); + g_assert (id); + names = g_slist_append (names, (gpointer) id); + + if (preferred && !preferred_found && (strcmp (preferred, id) == 0)) + preferred_found = TRUE; + } + + /* Return the preferred name if it was unique */ + if (preferred && !preferred_found) { + g_slist_free (names); + return g_strdup (preferred); + } + + /* Otherwise find the next available unique connection name using the given + * connection name template. + */ + while (!cname && (i++ < 10000)) { + char *temp; + gboolean found = FALSE; + + temp = g_strdup_printf (format, i); + for (iter = names; iter; iter = g_slist_next (iter)) { + if (!strcmp (iter->data, temp)) { + found = TRUE; + break; + } + } + if (!found) + cname = temp; + else + g_free (temp); + } + + g_slist_free (names); + return cname; +} + +void +nm_utils_complete_generic (NMConnection *connection, + const char *ctype, + const GSList *existing, + const char *format, + const char *preferred) +{ + NMSettingConnection *s_con; + NMSettingIP4Config *s_ip4; + NMSettingIP6Config *s_ip6; + const char *method; + char *id, *uuid; + + s_con = (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION); + if (!s_con) { + s_con = (NMSettingConnection *) nm_setting_connection_new (); + nm_connection_add_setting (connection, NM_SETTING (s_con)); + } + g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_TYPE, ctype, NULL); + + if (!nm_setting_connection_get_uuid (s_con)) { + uuid = nm_utils_uuid_generate (); + g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_UUID, uuid, NULL); + g_free (uuid); + } + + /* Add a connection ID if absent */ + if (!nm_setting_connection_get_id (s_con)) { + id = get_new_connection_name (existing, format, preferred); + g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_ID, id, NULL); + g_free (id); + } + + /* Add an 'auto' IPv4 connection if present */ + s_ip4 = (NMSettingIP4Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG); + if (!s_ip4) { + s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + nm_connection_add_setting (connection, NM_SETTING (s_ip4)); + } + method = nm_setting_ip4_config_get_method (s_ip4); + if (!method) { + g_object_set (G_OBJECT (s_ip4), + NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NULL); + } + + s_ip6 = (NMSettingIP6Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP6_CONFIG); + if (!s_ip6) { + s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + nm_connection_add_setting (connection, NM_SETTING (s_ip6)); + } + method = nm_setting_ip6_config_get_method (s_ip6); + if (!method) { + g_object_set (G_OBJECT (s_ip6), + NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, + NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NULL); + } +} + diff --git a/src/NetworkManagerUtils.h b/src/NetworkManagerUtils.h index 72c0e532b8..1edd87746f 100644 --- a/src/NetworkManagerUtils.h +++ b/src/NetworkManagerUtils.h @@ -78,4 +78,10 @@ gboolean nm_utils_get_proc_sys_net_value (const char *path, const char *iface, guint32 *out_value); +void nm_utils_complete_generic (NMConnection *connection, + const char *ctype, + const GSList *existing, + const char *format, + const char *preferred); + #endif /* NETWORK_MANAGER_UTILS_H */ diff --git a/src/modem-manager/nm-modem-cdma.c b/src/modem-manager/nm-modem-cdma.c index 63b2d66fdc..7000c5de82 100644 --- a/src/modem-manager/nm-modem-cdma.c +++ b/src/modem-manager/nm-modem-cdma.c @@ -305,11 +305,11 @@ real_complete_connection (NMModem *modem, nm_connection_add_setting (connection, NM_SETTING (s_ppp)); } - nm_device_complete_generic (connection, - NM_SETTING_CDMA_SETTING_NAME, - existing_connections, - _("CDMA connection %d"), - NULL); + nm_utils_complete_generic (connection, + NM_SETTING_CDMA_SETTING_NAME, + existing_connections, + _("CDMA connection %d"), + NULL); return TRUE; } diff --git a/src/modem-manager/nm-modem-gsm.c b/src/modem-manager/nm-modem-gsm.c index 4672f4cee1..525aa55b05 100644 --- a/src/modem-manager/nm-modem-gsm.c +++ b/src/modem-manager/nm-modem-gsm.c @@ -508,11 +508,11 @@ real_complete_connection (NMModem *modem, nm_connection_add_setting (connection, NM_SETTING (s_ppp)); } - nm_device_complete_generic (connection, - NM_SETTING_GSM_SETTING_NAME, - existing_connections, - _("GSM connection %d"), - NULL); + nm_utils_complete_generic (connection, + NM_SETTING_GSM_SETTING_NAME, + existing_connections, + _("GSM connection %d"), + NULL); return TRUE; } diff --git a/src/nm-device-bt.c b/src/nm-device-bt.c index f2ce78e638..8c8545b4bd 100644 --- a/src/nm-device-bt.c +++ b/src/nm-device-bt.c @@ -373,11 +373,11 @@ real_complete_connection (NMDevice *device, return FALSE; } - nm_device_complete_generic (connection, - NM_SETTING_BLUETOOTH_SETTING_NAME, - existing_connections, - format, - preferred); + nm_utils_complete_generic (connection, + NM_SETTING_BLUETOOTH_SETTING_NAME, + existing_connections, + format, + preferred); setting_bdaddr = nm_setting_bluetooth_get_bdaddr (s_bt); if (setting_bdaddr) { diff --git a/src/nm-device-ethernet.c b/src/nm-device-ethernet.c index 90137b49ac..c318f0869a 100644 --- a/src/nm-device-ethernet.c +++ b/src/nm-device-ethernet.c @@ -1647,11 +1647,11 @@ real_complete_connection (NMDevice *device, /* Default to an ethernet-only connection, but if a PPPoE setting was given * then PPPoE should be our connection type. */ - nm_device_complete_generic (connection, - s_pppoe ? NM_SETTING_PPPOE_SETTING_NAME : NM_SETTING_CONNECTION_SETTING_NAME, - existing_connections, - s_pppoe ? _("PPPoE connection %d") : _("Wired connection %d"), - NULL); + nm_utils_complete_generic (connection, + s_pppoe ? NM_SETTING_PPPOE_SETTING_NAME : NM_SETTING_CONNECTION_SETTING_NAME, + existing_connections, + s_pppoe ? _("PPPoE connection %d") : _("Wired connection %d"), + NULL); s_wired = (NMSettingWired *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRED); if (!s_wired) { diff --git a/src/nm-device-olpc-mesh.c b/src/nm-device-olpc-mesh.c index f9d0eb4462..36b056e8fe 100644 --- a/src/nm-device-olpc-mesh.c +++ b/src/nm-device-olpc-mesh.c @@ -416,11 +416,11 @@ real_complete_connection (NMDevice *device, } - nm_device_complete_generic (connection, - NM_SETTING_OLPC_MESH_SETTING_NAME, - existing_connections, - _("Mesh %d"), - NULL); + nm_utils_complete_generic (connection, + NM_SETTING_OLPC_MESH_SETTING_NAME, + existing_connections, + _("Mesh %d"), + NULL); return TRUE; } diff --git a/src/nm-device-private.h b/src/nm-device-private.h index 82f429fc61..a7d238b10b 100644 --- a/src/nm-device-private.h +++ b/src/nm-device-private.h @@ -46,10 +46,4 @@ gboolean nm_device_get_firmware_missing (NMDevice *self); void nm_device_set_firmware_missing (NMDevice *self, gboolean missing); -void nm_device_complete_generic (NMConnection *connection, - const char *ctype, - const GSList *existing, - const char *format, - const char *preferred); - #endif /* NM_DEVICE_PRIVATE_H */ diff --git a/src/nm-device-wifi.c b/src/nm-device-wifi.c index 9f821be8c0..c19557b004 100644 --- a/src/nm-device-wifi.c +++ b/src/nm-device-wifi.c @@ -1462,11 +1462,11 @@ real_complete_connection (NMDevice *device, str_ssid = nm_utils_ssid_to_utf8 ((const char *) ssid, ssid->len); format = g_strdup_printf ("%s %%d", str_ssid); - nm_device_complete_generic (connection, - NM_SETTING_WIRELESS_SETTING_NAME, - existing_connections, - format, - str_ssid); + nm_utils_complete_generic (connection, + NM_SETTING_WIRELESS_SETTING_NAME, + existing_connections, + format, + str_ssid); g_free (str_ssid); g_free (format); diff --git a/src/nm-device.c b/src/nm-device.c index c3e5c75b5a..2fae4adaae 100644 --- a/src/nm-device.c +++ b/src/nm-device.c @@ -604,121 +604,6 @@ nm_device_complete_connection (NMDevice *self, return success; } -static char * -nm_device_new_connection_name (const GSList *existing, - const char *format, - const char *preferred) -{ - GSList *names = NULL; - const GSList *iter; - char *cname = NULL; - int i = 0; - gboolean preferred_found = FALSE; - - for (iter = existing; iter; iter = g_slist_next (iter)) { - NMConnection *candidate = NM_CONNECTION (iter->data); - NMSettingConnection *s_con; - const char *id; - - s_con = NM_SETTING_CONNECTION (nm_connection_get_setting (candidate, NM_TYPE_SETTING_CONNECTION)); - id = nm_setting_connection_get_id (s_con); - g_assert (id); - names = g_slist_append (names, (gpointer) id); - - if (preferred && !preferred_found && (strcmp (preferred, id) == 0)) - preferred_found = TRUE; - } - - /* Return the preferred name if it was unique */ - if (preferred && !preferred_found) { - g_slist_free (names); - return g_strdup (preferred); - } - - /* Otherwise find the next available unique connection name using the given - * connection name template. - */ - while (!cname && (i++ < 10000)) { - char *temp; - gboolean found = FALSE; - - temp = g_strdup_printf (format, i); - for (iter = names; iter; iter = g_slist_next (iter)) { - if (!strcmp (iter->data, temp)) { - found = TRUE; - break; - } - } - if (!found) - cname = temp; - else - g_free (temp); - } - - g_slist_free (names); - return cname; -} - -void -nm_device_complete_generic (NMConnection *connection, - const char *ctype, - const GSList *existing, - const char *format, - const char *preferred) -{ - NMSettingConnection *s_con; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; - const char *method; - char *id, *uuid; - - s_con = (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION); - if (!s_con) { - s_con = (NMSettingConnection *) nm_setting_connection_new (); - nm_connection_add_setting (connection, NM_SETTING (s_con)); - } - g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_TYPE, ctype, NULL); - - if (!nm_setting_connection_get_uuid (s_con)) { - uuid = nm_utils_uuid_generate (); - g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_UUID, uuid, NULL); - g_free (uuid); - } - - /* Add a connection ID if absent */ - if (!nm_setting_connection_get_id (s_con)) { - id = nm_device_new_connection_name (existing, format, preferred); - g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_ID, id, NULL); - g_free (id); - } - - /* Add an 'auto' IPv4 connection if present */ - s_ip4 = (NMSettingIP4Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG); - if (!s_ip4) { - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); - nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - } - method = nm_setting_ip4_config_get_method (s_ip4); - if (!method) { - g_object_set (G_OBJECT (s_ip4), - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, - NULL); - } - - s_ip6 = (NMSettingIP6Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP6_CONFIG); - if (!s_ip6) { - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); - nm_connection_add_setting (connection, NM_SETTING (s_ip6)); - } - method = nm_setting_ip6_config_get_method (s_ip6); - if (!method) { - g_object_set (G_OBJECT (s_ip6), - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, - NULL); - } -} - static void dnsmasq_state_changed_cb (NMDnsMasqManager *manager, guint32 status, gpointer user_data) { diff --git a/src/wimax/nm-device-wimax.c b/src/wimax/nm-device-wimax.c index ad57458ba9..4b35a1cf45 100644 --- a/src/wimax/nm-device-wimax.c +++ b/src/wimax/nm-device-wimax.c @@ -559,11 +559,11 @@ real_complete_connection (NMDevice *device, g_assert (nsp_name); format = g_strdup_printf ("%s %%d", nsp_name); - nm_device_complete_generic (connection, - NM_SETTING_WIMAX_SETTING_NAME, - existing_connections, - format, - nsp_name); + nm_utils_complete_generic (connection, + NM_SETTING_WIMAX_SETTING_NAME, + existing_connections, + format, + nsp_name); g_free (format); g_object_set (G_OBJECT (s_wimax), NM_SETTING_WIMAX_NETWORK_NAME, nsp_name, NULL);