summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Winship <danw@gnome.org>2014-08-05 17:11:57 -0400
committerDan Winship <danw@gnome.org>2014-09-04 09:18:44 -0400
commit7d233cc956cbd12090e517a4adb369c30ad4d38a (patch)
treeab04c8cf8d7e4590614344c636b40d711d3fecde
parent6217c1e74c093b95b16880a30286c8e60a6fb746 (diff)
downloadNetworkManager-7d233cc956cbd12090e517a4adb369c30ad4d38a.tar.gz
core: abstract out the duplicated default-ifname-generating code
NMDeviceBond, NMDeviceBridge, and NMDeviceTeam all used basically the same code to generate a default interface name. Move it into nm_utils_complete_generic().
-rw-r--r--src/NetworkManagerUtils.c61
-rw-r--r--src/NetworkManagerUtils.h5
-rw-r--r--src/devices/adsl/nm-device-adsl.c1
-rw-r--r--src/devices/bluetooth/nm-device-bt.c1
-rw-r--r--src/devices/nm-device-bond.c35
-rw-r--r--src/devices/nm-device-bridge.c35
-rw-r--r--src/devices/nm-device-ethernet.c1
-rw-r--r--src/devices/nm-device-infiniband.c1
-rw-r--r--src/devices/nm-device-vlan.c1
-rw-r--r--src/devices/team/nm-device-team.c35
-rw-r--r--src/devices/wifi/nm-device-olpc-mesh.c1
-rw-r--r--src/devices/wifi/nm-device-wifi.c1
-rw-r--r--src/devices/wimax/nm-device-wimax.c1
-rw-r--r--src/devices/wwan/nm-modem-broadband.c2
-rw-r--r--src/nm-manager.c1
15 files changed, 68 insertions, 114 deletions
diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c
index af566faf8b..827bc55ab4 100644
--- a/src/NetworkManagerUtils.c
+++ b/src/NetworkManagerUtils.c
@@ -907,6 +907,40 @@ get_new_connection_name (const GSList *existing,
return cname;
}
+static char *
+get_new_connection_ifname (const GSList *existing,
+ const char *prefix)
+{
+ int i;
+ char *name;
+ const GSList *iter;
+ gboolean found;
+
+ for (i = 0; i < 500; i++) {
+ name = g_strdup_printf ("%s%d", prefix, i);
+
+ if (nm_platform_link_exists (name))
+ goto next;
+
+ for (iter = existing, found = FALSE; iter; iter = g_slist_next (iter)) {
+ NMConnection *candidate = iter->data;
+
+ if (g_strcmp0 (nm_connection_get_interface_name (candidate), name) == 0) {
+ found = TRUE;
+ break;
+ }
+ }
+
+ if (!found)
+ return name;
+
+ next:
+ g_free (name);
+ }
+
+ return NULL;
+}
+
const char *
nm_utils_get_ip_config_method (NMConnection *connection,
GType ip_setting_type)
@@ -954,18 +988,16 @@ void
nm_utils_complete_generic (NMConnection *connection,
const char *ctype,
const GSList *existing,
- const char *preferred,
- const char *fallback_prefix,
+ const char *preferred_id,
+ const char *fallback_id_prefix,
+ const char *ifname_prefix,
gboolean default_enable_ipv6)
{
NMSettingConnection *s_con;
- char *id, *uuid;
- GHashTable *parameters = g_hash_table_new (g_str_hash, g_str_equal);
-
- g_assert (fallback_prefix);
+ char *id, *uuid, *ifname;
+ GHashTable *parameters;
- g_hash_table_insert (parameters, NM_CONNECTION_NORMALIZE_PARAM_IP6_CONFIG_METHOD,
- default_enable_ipv6 ? NM_SETTING_IP6_CONFIG_METHOD_AUTO : NM_SETTING_IP6_CONFIG_METHOD_IGNORE);
+ g_assert (fallback_id_prefix);
s_con = nm_connection_get_setting_connection (connection);
if (!s_con) {
@@ -982,14 +1014,23 @@ nm_utils_complete_generic (NMConnection *connection,
/* Add a connection ID if absent */
if (!nm_setting_connection_get_id (s_con)) {
- id = get_new_connection_name (existing, preferred, fallback_prefix);
+ id = get_new_connection_name (existing, preferred_id, fallback_id_prefix);
g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_ID, id, NULL);
g_free (id);
}
+ /* Add an interface name, if requested */
+ if (ifname_prefix && !nm_setting_connection_get_interface_name (s_con)) {
+ ifname = get_new_connection_ifname (existing, ifname_prefix);
+ g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_INTERFACE_NAME, ifname, NULL);
+ g_free (ifname);
+ }
+
/* Normalize */
+ parameters = g_hash_table_new (g_str_hash, g_str_equal);
+ g_hash_table_insert (parameters, NM_CONNECTION_NORMALIZE_PARAM_IP6_CONFIG_METHOD,
+ default_enable_ipv6 ? NM_SETTING_IP6_CONFIG_METHOD_AUTO : NM_SETTING_IP6_CONFIG_METHOD_IGNORE);
nm_connection_normalize (connection, parameters, NULL, NULL);
-
g_hash_table_destroy (parameters);
}
diff --git a/src/NetworkManagerUtils.h b/src/NetworkManagerUtils.h
index 92dd5ec4e4..1a9859f003 100644
--- a/src/NetworkManagerUtils.h
+++ b/src/NetworkManagerUtils.h
@@ -102,8 +102,9 @@ const char *nm_utils_get_ip_config_method (NMConnection *connection,
void nm_utils_complete_generic (NMConnection *connection,
const char *ctype,
const GSList *existing,
- const char *preferred,
- const char *fallback_prefix,
+ const char *preferred_id,
+ const char *fallback_id_prefix,
+ const char *ifname_prefix,
gboolean default_enable_ipv6);
char *nm_utils_new_vlan_name (const char *parent_iface, guint32 vlan_id);
diff --git a/src/devices/adsl/nm-device-adsl.c b/src/devices/adsl/nm-device-adsl.c
index 7aa84a9dd4..abe2b3301f 100644
--- a/src/devices/adsl/nm-device-adsl.c
+++ b/src/devices/adsl/nm-device-adsl.c
@@ -125,6 +125,7 @@ complete_connection (NMDevice *device,
existing_connections,
NULL,
_("ADSL connection"),
+ NULL,
FALSE); /* No IPv6 yet by default */
diff --git a/src/devices/bluetooth/nm-device-bt.c b/src/devices/bluetooth/nm-device-bt.c
index a78ab81e1b..9edcf28cdb 100644
--- a/src/devices/bluetooth/nm-device-bt.c
+++ b/src/devices/bluetooth/nm-device-bt.c
@@ -318,6 +318,7 @@ complete_connection (NMDevice *device,
existing_connections,
preferred,
fallback_prefix,
+ NULL,
is_dun ? FALSE : TRUE); /* No IPv6 yet for DUN */
setting_bdaddr = nm_setting_bluetooth_get_bdaddr (s_bt);
diff --git a/src/devices/nm-device-bond.c b/src/devices/nm-device-bond.c
index 92a5911c8e..d2a0712851 100644
--- a/src/devices/nm-device-bond.c
+++ b/src/devices/nm-device-bond.c
@@ -128,17 +128,13 @@ complete_connection (NMDevice *device,
GError **error)
{
NMSettingBond *s_bond;
- NMSettingConnection *s_con;
- guint32 i = 0;
- char *name;
- const GSList *iter;
- gboolean found;
nm_utils_complete_generic (connection,
NM_SETTING_BOND_SETTING_NAME,
existing_connections,
NULL,
_("Bond connection"),
+ "bond",
TRUE);
s_bond = nm_connection_get_setting_bond (connection);
@@ -146,35 +142,6 @@ complete_connection (NMDevice *device,
s_bond = (NMSettingBond *) nm_setting_bond_new ();
nm_connection_add_setting (connection, NM_SETTING (s_bond));
}
- s_con = nm_connection_get_setting_connection (connection);
- g_return_val_if_fail (s_con != NULL, FALSE);
-
- /* Grab the first name that doesn't exist in either our connections
- * or a device on the system.
- */
- while (i < 500 && !nm_setting_connection_get_interface_name (s_con)) {
- name = g_strdup_printf ("bond%u", i);
- /* check interface names */
- if (!nm_platform_link_exists (name)) {
- /* check existing bond connections */
- for (iter = existing_connections, found = FALSE; iter; iter = g_slist_next (iter)) {
- NMConnection *candidate = iter->data;
-
- if (nm_connection_is_type (candidate, NM_SETTING_BOND_SETTING_NAME)) {
- if (g_strcmp0 (nm_connection_get_interface_name (candidate), name) == 0) {
- found = TRUE;
- break;
- }
- }
- }
-
- if (!found)
- g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_INTERFACE_NAME, name, NULL);
- }
-
- g_free (name);
- i++;
- }
return TRUE;
}
diff --git a/src/devices/nm-device-bridge.c b/src/devices/nm-device-bridge.c
index 47c5f7e0c0..63bde86075 100644
--- a/src/devices/nm-device-bridge.c
+++ b/src/devices/nm-device-bridge.c
@@ -136,17 +136,13 @@ complete_connection (NMDevice *device,
GError **error)
{
NMSettingBridge *s_bridge;
- NMSettingConnection *s_con;
- guint32 i = 0;
- char *name;
- const GSList *iter;
- gboolean found;
nm_utils_complete_generic (connection,
NM_SETTING_BRIDGE_SETTING_NAME,
existing_connections,
NULL,
_("Bridge connection"),
+ "bridge",
TRUE);
s_bridge = nm_connection_get_setting_bridge (connection);
@@ -154,35 +150,6 @@ complete_connection (NMDevice *device,
s_bridge = (NMSettingBridge *) nm_setting_bridge_new ();
nm_connection_add_setting (connection, NM_SETTING (s_bridge));
}
- s_con = nm_connection_get_setting_connection (connection);
- g_return_val_if_fail (s_con != NULL, FALSE);
-
- /* Grab the first name that doesn't exist in either our connections
- * or a device on the system.
- */
- while (i < 500 && !nm_setting_connection_get_interface_name (s_con)) {
- name = g_strdup_printf ("br%u", i);
- /* check interface names */
- if (!nm_platform_link_exists (name)) {
- /* check existing bridge connections */
- for (iter = existing_connections, found = FALSE; iter; iter = g_slist_next (iter)) {
- NMConnection *candidate = iter->data;
-
- if (nm_connection_is_type (candidate, NM_SETTING_BRIDGE_SETTING_NAME)) {
- if (g_strcmp0 (nm_connection_get_interface_name (candidate), name) == 0) {
- found = TRUE;
- break;
- }
- }
- }
-
- if (!found)
- g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_INTERFACE_NAME, name, NULL);
- }
-
- g_free (name);
- i++;
- }
return TRUE;
}
diff --git a/src/devices/nm-device-ethernet.c b/src/devices/nm-device-ethernet.c
index 44a9d89bbc..5b27999c8c 100644
--- a/src/devices/nm-device-ethernet.c
+++ b/src/devices/nm-device-ethernet.c
@@ -1448,6 +1448,7 @@ complete_connection (NMDevice *device,
existing_connections,
NULL,
s_pppoe ? _("PPPoE connection") : _("Wired connection"),
+ NULL,
s_pppoe ? FALSE : TRUE); /* No IPv6 by default yet for PPPoE */
s_wired = nm_connection_get_setting_wired (connection);
diff --git a/src/devices/nm-device-infiniband.c b/src/devices/nm-device-infiniband.c
index 11335f4ed9..81a9017075 100644
--- a/src/devices/nm-device-infiniband.c
+++ b/src/devices/nm-device-infiniband.c
@@ -233,6 +233,7 @@ complete_connection (NMDevice *device,
existing_connections,
NULL,
_("InfiniBand connection"),
+ NULL,
TRUE);
s_infiniband = nm_connection_get_setting_infiniband (connection);
diff --git a/src/devices/nm-device-vlan.c b/src/devices/nm-device-vlan.c
index 3316bd67bd..817e3d2ac0 100644
--- a/src/devices/nm-device-vlan.c
+++ b/src/devices/nm-device-vlan.c
@@ -228,6 +228,7 @@ complete_connection (NMDevice *device,
existing_connections,
NULL,
_("VLAN connection"),
+ NULL,
TRUE);
s_vlan = nm_connection_get_setting_vlan (connection);
diff --git a/src/devices/team/nm-device-team.c b/src/devices/team/nm-device-team.c
index 2f296aadf9..b524ba7efd 100644
--- a/src/devices/team/nm-device-team.c
+++ b/src/devices/team/nm-device-team.c
@@ -139,17 +139,13 @@ complete_connection (NMDevice *device,
GError **error)
{
NMSettingTeam *s_team;
- NMSettingConnection *s_con;
- guint32 i = 0;
- char *name;
- const GSList *iter;
- gboolean found;
nm_utils_complete_generic (connection,
NM_SETTING_TEAM_SETTING_NAME,
existing_connections,
NULL,
_("Team connection"),
+ "team",
TRUE);
s_team = nm_connection_get_setting_team (connection);
@@ -157,35 +153,6 @@ complete_connection (NMDevice *device,
s_team = (NMSettingTeam *) nm_setting_team_new ();
nm_connection_add_setting (connection, NM_SETTING (s_team));
}
- s_con = nm_connection_get_setting_connection (connection);
- g_return_val_if_fail (s_con != NULL, FALSE);
-
- /* Grab the first name that doesn't exist in either our connections
- * or a device on the system.
- */
- while (i < 500 && !nm_setting_connection_get_interface_name (s_con)) {
- name = g_strdup_printf ("team%u", i);
- /* check interface names */
- if (!nm_platform_link_exists (name)) {
- /* check existing team connections */
- for (iter = existing_connections, found = FALSE; iter; iter = g_slist_next (iter)) {
- NMConnection *candidate = iter->data;
-
- if (nm_connection_is_type (candidate, NM_SETTING_TEAM_SETTING_NAME)) {
- if (g_strcmp0 (nm_connection_get_interface_name (candidate), name) == 0) {
- found = TRUE;
- break;
- }
- }
- }
-
- if (!found)
- g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_INTERFACE_NAME, name, NULL);
- }
-
- g_free (name);
- i++;
- }
return TRUE;
}
diff --git a/src/devices/wifi/nm-device-olpc-mesh.c b/src/devices/wifi/nm-device-olpc-mesh.c
index fea6aa5c4c..94e7eb7721 100644
--- a/src/devices/wifi/nm-device-olpc-mesh.c
+++ b/src/devices/wifi/nm-device-olpc-mesh.c
@@ -164,6 +164,7 @@ complete_connection (NMDevice *device,
existing_connections,
NULL,
_("Mesh"),
+ NULL,
FALSE); /* No IPv6 by default */
return TRUE;
diff --git a/src/devices/wifi/nm-device-wifi.c b/src/devices/wifi/nm-device-wifi.c
index 8741569951..e22b785f17 100644
--- a/src/devices/wifi/nm-device-wifi.c
+++ b/src/devices/wifi/nm-device-wifi.c
@@ -1109,6 +1109,7 @@ complete_connection (NMDevice *device,
existing_connections,
str_ssid,
str_ssid,
+ NULL,
TRUE);
g_free (str_ssid);
diff --git a/src/devices/wimax/nm-device-wimax.c b/src/devices/wimax/nm-device-wimax.c
index ddc501b3c9..9ffbd89fa6 100644
--- a/src/devices/wimax/nm-device-wimax.c
+++ b/src/devices/wimax/nm-device-wimax.c
@@ -442,6 +442,7 @@ complete_connection (NMDevice *device,
existing_connections,
nsp_name,
nsp_name,
+ NULL,
TRUE);
g_object_set (G_OBJECT (s_wimax), NM_SETTING_WIMAX_NETWORK_NAME, nsp_name, NULL);
diff --git a/src/devices/wwan/nm-modem-broadband.c b/src/devices/wwan/nm-modem-broadband.c
index 37426c7177..7cf75c28ec 100644
--- a/src/devices/wwan/nm-modem-broadband.c
+++ b/src/devices/wwan/nm-modem-broadband.c
@@ -481,6 +481,7 @@ complete_connection (NMModem *_self,
existing_connections,
NULL,
_("GSM connection"),
+ NULL,
FALSE); /* No IPv6 yet by default */
return TRUE;
@@ -503,6 +504,7 @@ complete_connection (NMModem *_self,
existing_connections,
NULL,
_("CDMA connection"),
+ NULL,
FALSE); /* No IPv6 yet by default */
return TRUE;
diff --git a/src/nm-manager.c b/src/nm-manager.c
index f56fa76f00..8597e3b533 100644
--- a/src/nm-manager.c
+++ b/src/nm-manager.c
@@ -3413,6 +3413,7 @@ impl_manager_add_and_activate_connection (NMManager *self,
all_connections,
NULL,
_("VPN connection"),
+ NULL,
FALSE); /* No IPv6 by default for now */
} else {
/* Let each device subclass complete the connection */