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().
This commit is contained in:
Dan Winship
2014-08-05 17:11:57 -04:00
parent 6217c1e74c
commit 7d233cc956
15 changed files with 69 additions and 115 deletions

View File

@@ -907,6 +907,40 @@ get_new_connection_name (const GSList *existing,
return cname; 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 * const char *
nm_utils_get_ip_config_method (NMConnection *connection, nm_utils_get_ip_config_method (NMConnection *connection,
GType ip_setting_type) GType ip_setting_type)
@@ -954,18 +988,16 @@ void
nm_utils_complete_generic (NMConnection *connection, nm_utils_complete_generic (NMConnection *connection,
const char *ctype, const char *ctype,
const GSList *existing, const GSList *existing,
const char *preferred, const char *preferred_id,
const char *fallback_prefix, const char *fallback_id_prefix,
const char *ifname_prefix,
gboolean default_enable_ipv6) gboolean default_enable_ipv6)
{ {
NMSettingConnection *s_con; NMSettingConnection *s_con;
char *id, *uuid; char *id, *uuid, *ifname;
GHashTable *parameters = g_hash_table_new (g_str_hash, g_str_equal); GHashTable *parameters;
g_assert (fallback_prefix); g_assert (fallback_id_prefix);
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);
s_con = nm_connection_get_setting_connection (connection); s_con = nm_connection_get_setting_connection (connection);
if (!s_con) { if (!s_con) {
@@ -982,14 +1014,23 @@ nm_utils_complete_generic (NMConnection *connection,
/* Add a connection ID if absent */ /* Add a connection ID if absent */
if (!nm_setting_connection_get_id (s_con)) { 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_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_ID, id, NULL);
g_free (id); g_free (id);
} }
/* Normalize */ /* Add an interface name, if requested */
nm_connection_normalize (connection, parameters, NULL, NULL); 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); g_hash_table_destroy (parameters);
} }

View File

@@ -102,8 +102,9 @@ const char *nm_utils_get_ip_config_method (NMConnection *connection,
void nm_utils_complete_generic (NMConnection *connection, void nm_utils_complete_generic (NMConnection *connection,
const char *ctype, const char *ctype,
const GSList *existing, const GSList *existing,
const char *preferred, const char *preferred_id,
const char *fallback_prefix, const char *fallback_id_prefix,
const char *ifname_prefix,
gboolean default_enable_ipv6); gboolean default_enable_ipv6);
char *nm_utils_new_vlan_name (const char *parent_iface, guint32 vlan_id); char *nm_utils_new_vlan_name (const char *parent_iface, guint32 vlan_id);

View File

@@ -125,6 +125,7 @@ complete_connection (NMDevice *device,
existing_connections, existing_connections,
NULL, NULL,
_("ADSL connection"), _("ADSL connection"),
NULL,
FALSE); /* No IPv6 yet by default */ FALSE); /* No IPv6 yet by default */

View File

@@ -318,6 +318,7 @@ complete_connection (NMDevice *device,
existing_connections, existing_connections,
preferred, preferred,
fallback_prefix, fallback_prefix,
NULL,
is_dun ? FALSE : TRUE); /* No IPv6 yet for DUN */ is_dun ? FALSE : TRUE); /* No IPv6 yet for DUN */
setting_bdaddr = nm_setting_bluetooth_get_bdaddr (s_bt); setting_bdaddr = nm_setting_bluetooth_get_bdaddr (s_bt);

View File

@@ -128,17 +128,13 @@ complete_connection (NMDevice *device,
GError **error) GError **error)
{ {
NMSettingBond *s_bond; NMSettingBond *s_bond;
NMSettingConnection *s_con;
guint32 i = 0;
char *name;
const GSList *iter;
gboolean found;
nm_utils_complete_generic (connection, nm_utils_complete_generic (connection,
NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_SETTING_NAME,
existing_connections, existing_connections,
NULL, NULL,
_("Bond connection"), _("Bond connection"),
"bond",
TRUE); TRUE);
s_bond = nm_connection_get_setting_bond (connection); s_bond = nm_connection_get_setting_bond (connection);
@@ -146,35 +142,6 @@ complete_connection (NMDevice *device,
s_bond = (NMSettingBond *) nm_setting_bond_new (); s_bond = (NMSettingBond *) nm_setting_bond_new ();
nm_connection_add_setting (connection, NM_SETTING (s_bond)); 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; return TRUE;
} }

View File

@@ -136,17 +136,13 @@ complete_connection (NMDevice *device,
GError **error) GError **error)
{ {
NMSettingBridge *s_bridge; NMSettingBridge *s_bridge;
NMSettingConnection *s_con;
guint32 i = 0;
char *name;
const GSList *iter;
gboolean found;
nm_utils_complete_generic (connection, nm_utils_complete_generic (connection,
NM_SETTING_BRIDGE_SETTING_NAME, NM_SETTING_BRIDGE_SETTING_NAME,
existing_connections, existing_connections,
NULL, NULL,
_("Bridge connection"), _("Bridge connection"),
"bridge",
TRUE); TRUE);
s_bridge = nm_connection_get_setting_bridge (connection); s_bridge = nm_connection_get_setting_bridge (connection);
@@ -154,35 +150,6 @@ complete_connection (NMDevice *device,
s_bridge = (NMSettingBridge *) nm_setting_bridge_new (); s_bridge = (NMSettingBridge *) nm_setting_bridge_new ();
nm_connection_add_setting (connection, NM_SETTING (s_bridge)); 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; return TRUE;
} }

View File

@@ -1448,6 +1448,7 @@ complete_connection (NMDevice *device,
existing_connections, existing_connections,
NULL, NULL,
s_pppoe ? _("PPPoE connection") : _("Wired connection"), s_pppoe ? _("PPPoE connection") : _("Wired connection"),
NULL,
s_pppoe ? FALSE : TRUE); /* No IPv6 by default yet for PPPoE */ s_pppoe ? FALSE : TRUE); /* No IPv6 by default yet for PPPoE */
s_wired = nm_connection_get_setting_wired (connection); s_wired = nm_connection_get_setting_wired (connection);

View File

@@ -233,6 +233,7 @@ complete_connection (NMDevice *device,
existing_connections, existing_connections,
NULL, NULL,
_("InfiniBand connection"), _("InfiniBand connection"),
NULL,
TRUE); TRUE);
s_infiniband = nm_connection_get_setting_infiniband (connection); s_infiniband = nm_connection_get_setting_infiniband (connection);

View File

@@ -228,6 +228,7 @@ complete_connection (NMDevice *device,
existing_connections, existing_connections,
NULL, NULL,
_("VLAN connection"), _("VLAN connection"),
NULL,
TRUE); TRUE);
s_vlan = nm_connection_get_setting_vlan (connection); s_vlan = nm_connection_get_setting_vlan (connection);

View File

@@ -139,17 +139,13 @@ complete_connection (NMDevice *device,
GError **error) GError **error)
{ {
NMSettingTeam *s_team; NMSettingTeam *s_team;
NMSettingConnection *s_con;
guint32 i = 0;
char *name;
const GSList *iter;
gboolean found;
nm_utils_complete_generic (connection, nm_utils_complete_generic (connection,
NM_SETTING_TEAM_SETTING_NAME, NM_SETTING_TEAM_SETTING_NAME,
existing_connections, existing_connections,
NULL, NULL,
_("Team connection"), _("Team connection"),
"team",
TRUE); TRUE);
s_team = nm_connection_get_setting_team (connection); s_team = nm_connection_get_setting_team (connection);
@@ -157,35 +153,6 @@ complete_connection (NMDevice *device,
s_team = (NMSettingTeam *) nm_setting_team_new (); s_team = (NMSettingTeam *) nm_setting_team_new ();
nm_connection_add_setting (connection, NM_SETTING (s_team)); 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; return TRUE;
} }

View File

@@ -164,6 +164,7 @@ complete_connection (NMDevice *device,
existing_connections, existing_connections,
NULL, NULL,
_("Mesh"), _("Mesh"),
NULL,
FALSE); /* No IPv6 by default */ FALSE); /* No IPv6 by default */
return TRUE; return TRUE;

View File

@@ -1109,6 +1109,7 @@ complete_connection (NMDevice *device,
existing_connections, existing_connections,
str_ssid, str_ssid,
str_ssid, str_ssid,
NULL,
TRUE); TRUE);
g_free (str_ssid); g_free (str_ssid);

View File

@@ -442,6 +442,7 @@ complete_connection (NMDevice *device,
existing_connections, existing_connections,
nsp_name, nsp_name,
nsp_name, nsp_name,
NULL,
TRUE); TRUE);
g_object_set (G_OBJECT (s_wimax), NM_SETTING_WIMAX_NETWORK_NAME, nsp_name, NULL); g_object_set (G_OBJECT (s_wimax), NM_SETTING_WIMAX_NETWORK_NAME, nsp_name, NULL);

View File

@@ -481,6 +481,7 @@ complete_connection (NMModem *_self,
existing_connections, existing_connections,
NULL, NULL,
_("GSM connection"), _("GSM connection"),
NULL,
FALSE); /* No IPv6 yet by default */ FALSE); /* No IPv6 yet by default */
return TRUE; return TRUE;
@@ -503,6 +504,7 @@ complete_connection (NMModem *_self,
existing_connections, existing_connections,
NULL, NULL,
_("CDMA connection"), _("CDMA connection"),
NULL,
FALSE); /* No IPv6 yet by default */ FALSE); /* No IPv6 yet by default */
return TRUE; return TRUE;

View File

@@ -3413,6 +3413,7 @@ impl_manager_add_and_activate_connection (NMManager *self,
all_connections, all_connections,
NULL, NULL,
_("VPN connection"), _("VPN connection"),
NULL,
FALSE); /* No IPv6 by default for now */ FALSE); /* No IPv6 by default for now */
} else { } else {
/* Let each device subclass complete the connection */ /* Let each device subclass complete the connection */