core: move generic connection complete function to a generic location
Out of NMDevice specific places to the utils code, so it can be used more easily from everywhere. There's nothing device-specific about it anyway.
This commit is contained in:
@@ -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 <netlink/addr.h>
|
||||
#include <netinet/in.h>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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 */
|
||||
|
@@ -305,7 +305,7 @@ real_complete_connection (NMModem *modem,
|
||||
nm_connection_add_setting (connection, NM_SETTING (s_ppp));
|
||||
}
|
||||
|
||||
nm_device_complete_generic (connection,
|
||||
nm_utils_complete_generic (connection,
|
||||
NM_SETTING_CDMA_SETTING_NAME,
|
||||
existing_connections,
|
||||
_("CDMA connection %d"),
|
||||
|
@@ -508,7 +508,7 @@ real_complete_connection (NMModem *modem,
|
||||
nm_connection_add_setting (connection, NM_SETTING (s_ppp));
|
||||
}
|
||||
|
||||
nm_device_complete_generic (connection,
|
||||
nm_utils_complete_generic (connection,
|
||||
NM_SETTING_GSM_SETTING_NAME,
|
||||
existing_connections,
|
||||
_("GSM connection %d"),
|
||||
|
@@ -373,7 +373,7 @@ real_complete_connection (NMDevice *device,
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
nm_device_complete_generic (connection,
|
||||
nm_utils_complete_generic (connection,
|
||||
NM_SETTING_BLUETOOTH_SETTING_NAME,
|
||||
existing_connections,
|
||||
format,
|
||||
|
@@ -1647,7 +1647,7 @@ 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,
|
||||
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"),
|
||||
|
@@ -416,7 +416,7 @@ real_complete_connection (NMDevice *device,
|
||||
|
||||
}
|
||||
|
||||
nm_device_complete_generic (connection,
|
||||
nm_utils_complete_generic (connection,
|
||||
NM_SETTING_OLPC_MESH_SETTING_NAME,
|
||||
existing_connections,
|
||||
_("Mesh %d"),
|
||||
|
@@ -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 */
|
||||
|
@@ -1462,7 +1462,7 @@ 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_utils_complete_generic (connection,
|
||||
NM_SETTING_WIRELESS_SETTING_NAME,
|
||||
existing_connections,
|
||||
format,
|
||||
|
115
src/nm-device.c
115
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)
|
||||
{
|
||||
|
@@ -559,7 +559,7 @@ real_complete_connection (NMDevice *device,
|
||||
|
||||
g_assert (nsp_name);
|
||||
format = g_strdup_printf ("%s %%d", nsp_name);
|
||||
nm_device_complete_generic (connection,
|
||||
nm_utils_complete_generic (connection,
|
||||
NM_SETTING_WIMAX_SETTING_NAME,
|
||||
existing_connections,
|
||||
format,
|
||||
|
Reference in New Issue
Block a user