2019-09-10 11:19:01 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2013-12-02 16:20:26 -05:00
|
|
|
/*
|
2019-10-01 09:20:35 +02:00
|
|
|
* Copyright (C) 2012, 2013 Red Hat, Inc.
|
2013-12-02 16:20:26 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* SECTION:nm-editor-utils
|
|
|
|
|
* @short_description: Miscellaneous connection editor utilities
|
|
|
|
|
*
|
|
|
|
|
* nm-editor-utils contains helper functions for connection editors.
|
|
|
|
|
* The goal is that this should eventually be shared between nmtui,
|
|
|
|
|
* nm-connection-editor, and gnome-control-center.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-02-19 14:57:48 +01:00
|
|
|
#include "nm-default.h"
|
2013-12-02 16:20:26 -05:00
|
|
|
|
|
|
|
|
#include "nm-editor-utils.h"
|
|
|
|
|
#if 0
|
2015-11-23 17:47:00 +01:00
|
|
|
#include "nm-vpn-helpers.h"
|
2013-12-02 16:20:26 -05:00
|
|
|
|
|
|
|
|
static GSList *vpn_plugins;
|
|
|
|
|
|
all: don't use gchar/gshort/gint/glong but C types
We commonly don't use the glib typedefs for char/short/int/long,
but their C types directly.
$ git grep '\<g\(char\|short\|int\|long\|float\|double\)\>' | wc -l
587
$ git grep '\<\(char\|short\|int\|long\|float\|double\)\>' | wc -l
21114
One could argue that using the glib typedefs is preferable in
public API (of our glib based libnm library) or where it clearly
is related to glib, like during
g_object_set (obj, PROPERTY, (gint) value, NULL);
However, that argument does not seem strong, because in practice we don't
follow that argument today, and seldomly use the glib typedefs.
Also, the style guide for this would be hard to formalize, because
"using them where clearly related to a glib" is a very loose suggestion.
Also note that glib typedefs will always just be typedefs of the
underlying C types. There is no danger of glib changing the meaning
of these typedefs (because that would be a major API break of glib).
A simple style guide is instead: don't use these typedefs.
No manual actions, I only ran the bash script:
FILES=($(git ls-files '*.[hc]'))
sed -i \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>\( [^ ]\)/\1\2/g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\> /\1 /g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>/\1/g' \
"${FILES[@]}"
2018-07-11 07:40:19 +02:00
|
|
|
static int
|
2013-12-02 16:20:26 -05:00
|
|
|
sort_vpn_plugins (gconstpointer a, gconstpointer b)
|
|
|
|
|
{
|
2015-11-23 17:47:00 +01:00
|
|
|
NMVpnEditorPlugin *aa = NM_VPN_EDITOR_PLUGIN (a);
|
|
|
|
|
NMVpnEditorPlugin *bb = NM_VPN_EDITOR_PLUGIN (b);
|
2013-12-02 16:20:26 -05:00
|
|
|
char *aa_desc = NULL, *bb_desc = NULL;
|
|
|
|
|
int ret;
|
|
|
|
|
|
2015-11-23 17:47:00 +01:00
|
|
|
g_object_get (aa, NM_VPN_EDITOR_PLUGIN_NAME, &aa_desc, NULL);
|
|
|
|
|
g_object_get (bb, NM_VPN_EDITOR_PLUGIN_NAME, &bb_desc, NULL);
|
2013-12-02 16:20:26 -05:00
|
|
|
|
|
|
|
|
ret = g_strcmp0 (aa_desc, bb_desc);
|
|
|
|
|
|
|
|
|
|
g_free (aa_desc);
|
|
|
|
|
g_free (bb_desc);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
wifi_connection_setup_func (NMConnection *connection,
|
|
|
|
|
NMSettingConnection *s_con,
|
|
|
|
|
NMSetting *s_hw)
|
|
|
|
|
{
|
|
|
|
|
g_object_set (G_OBJECT (s_hw),
|
|
|
|
|
NM_SETTING_WIRELESS_MODE, NM_SETTING_WIRELESS_MODE_INFRA,
|
|
|
|
|
NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
bond_connection_setup_func (NMConnection *connection,
|
|
|
|
|
NMSettingConnection *s_con,
|
|
|
|
|
NMSetting *s_hw)
|
|
|
|
|
{
|
|
|
|
|
NMSettingBond *s_bond = NM_SETTING_BOND (s_hw);
|
2020-03-04 16:22:37 +01:00
|
|
|
guint i;
|
|
|
|
|
const char *value;
|
2019-06-10 15:30:53 +02:00
|
|
|
static const char *const options[] = {
|
|
|
|
|
NM_SETTING_BOND_OPTION_MODE,
|
2020-03-04 16:22:37 +01:00
|
|
|
NM_SETTING_BOND_OPTION_MIIMON,
|
|
|
|
|
NM_SETTING_BOND_OPTION_DOWNDELAY,
|
2019-06-10 15:30:53 +02:00
|
|
|
NM_SETTING_BOND_OPTION_UPDELAY,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (options); i++) {
|
2020-03-04 16:22:37 +01:00
|
|
|
value = nm_setting_bond_get_option_default (s_bond, options[i]);
|
|
|
|
|
if (value)
|
|
|
|
|
nm_setting_bond_add_option (s_bond, options[i], value);
|
2013-12-02 16:20:26 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef void (*NMEditorNewConnectionSetupFunc) (NMConnection *connection,
|
|
|
|
|
NMSettingConnection *s_con,
|
|
|
|
|
NMSetting *s_hw);
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
NMEditorConnectionTypeData data;
|
|
|
|
|
|
|
|
|
|
const char *id_format;
|
|
|
|
|
NMEditorNewConnectionSetupFunc connection_setup_func;
|
|
|
|
|
gboolean no_autoconnect;
|
|
|
|
|
} NMEditorConnectionTypeDataReal;
|
|
|
|
|
|
all: don't use gchar/gshort/gint/glong but C types
We commonly don't use the glib typedefs for char/short/int/long,
but their C types directly.
$ git grep '\<g\(char\|short\|int\|long\|float\|double\)\>' | wc -l
587
$ git grep '\<\(char\|short\|int\|long\|float\|double\)\>' | wc -l
21114
One could argue that using the glib typedefs is preferable in
public API (of our glib based libnm library) or where it clearly
is related to glib, like during
g_object_set (obj, PROPERTY, (gint) value, NULL);
However, that argument does not seem strong, because in practice we don't
follow that argument today, and seldomly use the glib typedefs.
Also, the style guide for this would be hard to formalize, because
"using them where clearly related to a glib" is a very loose suggestion.
Also note that glib typedefs will always just be typedefs of the
underlying C types. There is no danger of glib changing the meaning
of these typedefs (because that would be a major API break of glib).
A simple style guide is instead: don't use these typedefs.
No manual actions, I only ran the bash script:
FILES=($(git ls-files '*.[hc]'))
sed -i \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>\( [^ ]\)/\1\2/g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\> /\1 /g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>/\1/g' \
"${FILES[@]}"
2018-07-11 07:40:19 +02:00
|
|
|
static int
|
2013-12-02 16:20:26 -05:00
|
|
|
sort_types (gconstpointer a, gconstpointer b)
|
|
|
|
|
{
|
|
|
|
|
NMEditorConnectionTypeData *typea = *(NMEditorConnectionTypeData **)a;
|
|
|
|
|
NMEditorConnectionTypeData *typeb = *(NMEditorConnectionTypeData **)b;
|
|
|
|
|
|
|
|
|
|
if (typea->virtual && !typeb->virtual)
|
|
|
|
|
return 1;
|
|
|
|
|
else if (typeb->virtual && !typea->virtual)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if (typea->setting_type == NM_TYPE_SETTING_VPN &&
|
|
|
|
|
typeb->setting_type != NM_TYPE_SETTING_VPN)
|
|
|
|
|
return 1;
|
|
|
|
|
else if (typeb->setting_type == NM_TYPE_SETTING_VPN &&
|
|
|
|
|
typea->setting_type != NM_TYPE_SETTING_VPN)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
return g_utf8_collate (typea->name, typeb->name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* nm_editor_utils_get_connection_type_list:
|
|
|
|
|
*
|
|
|
|
|
* Gets an array of information about supported connection types. The
|
|
|
|
|
* array is sorted in a standard presentation order (hardware types
|
|
|
|
|
* first, alphabetized, then virtual types, alphabetized, then VPN
|
|
|
|
|
* types, alphabetized).
|
|
|
|
|
*
|
|
|
|
|
* Returns: the array of connection type information
|
|
|
|
|
*/
|
|
|
|
|
NMEditorConnectionTypeData **
|
|
|
|
|
nm_editor_utils_get_connection_type_list (void)
|
|
|
|
|
{
|
|
|
|
|
GPtrArray *array;
|
|
|
|
|
NMEditorConnectionTypeDataReal *item;
|
|
|
|
|
static NMEditorConnectionTypeData **list;
|
|
|
|
|
#if 0
|
|
|
|
|
GHashTable *vpn_plugins_hash;
|
|
|
|
|
gboolean have_vpn_plugins;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (list)
|
|
|
|
|
return list;
|
|
|
|
|
|
|
|
|
|
array = g_ptr_array_new ();
|
|
|
|
|
|
|
|
|
|
item = g_new0 (NMEditorConnectionTypeDataReal, 1);
|
|
|
|
|
item->data.name = _("Ethernet");
|
|
|
|
|
item->data.setting_type = NM_TYPE_SETTING_WIRED;
|
|
|
|
|
item->data.device_type = NM_TYPE_DEVICE_ETHERNET;
|
|
|
|
|
item->data.virtual = FALSE;
|
|
|
|
|
item->id_format = _("Ethernet connection %d");
|
|
|
|
|
g_ptr_array_add (array, item);
|
|
|
|
|
|
|
|
|
|
item = g_new0 (NMEditorConnectionTypeDataReal, 1);
|
|
|
|
|
item->data.name = _("Wi-Fi");
|
|
|
|
|
item->data.setting_type = NM_TYPE_SETTING_WIRELESS;
|
|
|
|
|
item->data.device_type = NM_TYPE_DEVICE_WIFI;
|
|
|
|
|
item->data.virtual = FALSE;
|
|
|
|
|
item->id_format = _("Wi-Fi connection %d");
|
|
|
|
|
item->connection_setup_func = wifi_connection_setup_func;
|
|
|
|
|
g_ptr_array_add (array, item);
|
|
|
|
|
|
|
|
|
|
item = g_new0 (NMEditorConnectionTypeDataReal, 1);
|
|
|
|
|
item->data.name = _("InfiniBand");
|
|
|
|
|
item->data.setting_type = NM_TYPE_SETTING_INFINIBAND;
|
|
|
|
|
item->data.device_type = NM_TYPE_DEVICE_INFINIBAND;
|
|
|
|
|
item->data.virtual = FALSE;
|
|
|
|
|
item->id_format = _("InfiniBand connection %d");
|
|
|
|
|
g_ptr_array_add (array, item);
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
item = g_new0 (NMEditorConnectionTypeDataReal, 1);
|
|
|
|
|
item->data.name = _("Mobile Broadband");
|
|
|
|
|
item->data.setting_type = NM_TYPE_SETTING_GSM;
|
|
|
|
|
item->data.virtual = FALSE;
|
|
|
|
|
item->id_format = _("Mobile broadband connection %d");
|
|
|
|
|
item->no_autoconnect = TRUE;
|
|
|
|
|
g_ptr_array_add (array, item);
|
2014-07-11 12:05:13 -04:00
|
|
|
#endif
|
2013-12-02 16:20:26 -05:00
|
|
|
|
|
|
|
|
item = g_new0 (NMEditorConnectionTypeDataReal, 1);
|
|
|
|
|
item->data.name = _("DSL");
|
|
|
|
|
item->data.setting_type = NM_TYPE_SETTING_PPPOE;
|
|
|
|
|
item->data.device_type = NM_TYPE_DEVICE_ETHERNET;
|
|
|
|
|
item->data.virtual = FALSE;
|
|
|
|
|
item->id_format = _("DSL connection %d");
|
|
|
|
|
item->no_autoconnect = TRUE;
|
|
|
|
|
g_ptr_array_add (array, item);
|
|
|
|
|
|
|
|
|
|
item = g_new0 (NMEditorConnectionTypeDataReal, 1);
|
|
|
|
|
item->data.name = _("Bond");
|
|
|
|
|
item->data.setting_type = NM_TYPE_SETTING_BOND;
|
|
|
|
|
item->data.device_type = NM_TYPE_DEVICE_BOND;
|
|
|
|
|
item->data.virtual = TRUE;
|
|
|
|
|
item->id_format = _("Bond connection %d");
|
|
|
|
|
item->connection_setup_func = bond_connection_setup_func;
|
|
|
|
|
g_ptr_array_add (array, item);
|
|
|
|
|
|
|
|
|
|
item = g_new0 (NMEditorConnectionTypeDataReal, 1);
|
|
|
|
|
item->data.name = _("Bridge");
|
|
|
|
|
item->data.setting_type = NM_TYPE_SETTING_BRIDGE;
|
|
|
|
|
item->data.slave_setting_type = NM_TYPE_SETTING_BRIDGE_PORT;
|
|
|
|
|
item->data.device_type = NM_TYPE_DEVICE_BRIDGE;
|
|
|
|
|
item->data.virtual = TRUE;
|
|
|
|
|
item->id_format = _("Bridge connection %d");
|
|
|
|
|
g_ptr_array_add (array, item);
|
|
|
|
|
|
|
|
|
|
item = g_new0 (NMEditorConnectionTypeDataReal, 1);
|
|
|
|
|
item->data.name = _("Team");
|
|
|
|
|
item->data.setting_type = NM_TYPE_SETTING_TEAM;
|
|
|
|
|
item->data.slave_setting_type = NM_TYPE_SETTING_TEAM_PORT;
|
|
|
|
|
item->data.device_type = NM_TYPE_DEVICE_TEAM;
|
|
|
|
|
item->data.virtual = TRUE;
|
|
|
|
|
item->id_format = _("Team connection %d");
|
|
|
|
|
g_ptr_array_add (array, item);
|
|
|
|
|
|
|
|
|
|
item = g_new0 (NMEditorConnectionTypeDataReal, 1);
|
|
|
|
|
item->data.name = _("VLAN");
|
|
|
|
|
item->data.setting_type = NM_TYPE_SETTING_VLAN;
|
|
|
|
|
item->data.device_type = NM_TYPE_DEVICE_VLAN;
|
|
|
|
|
item->data.virtual = TRUE;
|
|
|
|
|
item->id_format = _("VLAN connection %d");
|
|
|
|
|
g_ptr_array_add (array, item);
|
|
|
|
|
|
2016-11-04 14:00:04 +01:00
|
|
|
item = g_new0 (NMEditorConnectionTypeDataReal, 1);
|
|
|
|
|
item->data.name = _("IP tunnel");
|
|
|
|
|
item->data.setting_type = NM_TYPE_SETTING_IP_TUNNEL;
|
|
|
|
|
item->data.device_type = NM_TYPE_DEVICE_IP_TUNNEL;
|
|
|
|
|
item->data.virtual = TRUE;
|
|
|
|
|
item->id_format = _("IP tunnel connection %d");
|
|
|
|
|
g_ptr_array_add (array, item);
|
|
|
|
|
|
2013-12-02 16:20:26 -05:00
|
|
|
#if 0
|
|
|
|
|
/* Add "VPN" only if there are plugins */
|
2016-06-06 10:21:18 +02:00
|
|
|
vpn_plugins_hash = nm_vpn_get_plugin_infos ();
|
2013-12-02 16:20:26 -05:00
|
|
|
have_vpn_plugins = vpn_plugins_hash && g_hash_table_size (vpn_plugins_hash);
|
|
|
|
|
if (have_vpn_plugins) {
|
|
|
|
|
GHashTableIter iter;
|
|
|
|
|
gpointer name, plugin;
|
|
|
|
|
|
|
|
|
|
item = g_new0 (NMEditorConnectionTypeDataReal, 1);
|
|
|
|
|
item->data.name = _("VPN");
|
|
|
|
|
item->data.setting_type = NM_TYPE_SETTING_VPN;
|
|
|
|
|
item->data.virtual = TRUE;
|
|
|
|
|
item->id_format = _("VPN connection %d");
|
|
|
|
|
item->no_autoconnect = TRUE;
|
|
|
|
|
g_ptr_array_add (array, item);
|
|
|
|
|
|
|
|
|
|
vpn_plugins = NULL;
|
|
|
|
|
g_hash_table_iter_init (&iter, vpn_plugins_hash);
|
|
|
|
|
while (g_hash_table_iter_next (&iter, &name, &plugin))
|
|
|
|
|
vpn_plugins = g_slist_prepend (vpn_plugins, plugin);
|
|
|
|
|
vpn_plugins = g_slist_sort (vpn_plugins, sort_vpn_plugins);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
g_ptr_array_sort (array, sort_types);
|
|
|
|
|
g_ptr_array_add (array, NULL);
|
|
|
|
|
|
|
|
|
|
list = (NMEditorConnectionTypeData **)g_ptr_array_free (array, FALSE);
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-06 13:12:50 +02:00
|
|
|
static gboolean
|
2016-06-05 11:46:06 +02:00
|
|
|
_assert_format_int (const char *format)
|
|
|
|
|
{
|
|
|
|
|
g_assert (format);
|
|
|
|
|
format = strchr (format, '%');
|
|
|
|
|
g_assert (format);
|
2016-06-06 14:21:42 +02:00
|
|
|
format++;
|
2016-06-05 11:46:06 +02:00
|
|
|
g_assert (!strchr (format, '%'));
|
2016-06-18 18:15:41 +02:00
|
|
|
g_assert (format[0] == 'd');
|
2016-06-06 13:12:50 +02:00
|
|
|
return TRUE;
|
2016-06-05 11:46:06 +02:00
|
|
|
}
|
|
|
|
|
|
2013-12-02 16:20:26 -05:00
|
|
|
static char *
|
2014-09-29 10:58:16 -04:00
|
|
|
get_available_connection_name (const char *format,
|
|
|
|
|
NMClient *client)
|
2013-12-02 16:20:26 -05:00
|
|
|
{
|
2014-10-22 12:32:46 -04:00
|
|
|
const GPtrArray *conns;
|
|
|
|
|
GSList *names = NULL, *iter;
|
2013-12-02 16:20:26 -05:00
|
|
|
char *cname = NULL;
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
2016-06-06 13:12:50 +02:00
|
|
|
nm_assert (_assert_format_int (format));
|
2016-06-05 11:46:06 +02:00
|
|
|
|
2014-10-22 12:32:46 -04:00
|
|
|
conns = nm_client_get_connections (client);
|
|
|
|
|
for (i = 0; i < conns->len; i++) {
|
2013-12-02 16:20:26 -05:00
|
|
|
const char *id;
|
|
|
|
|
|
2014-10-22 12:32:46 -04:00
|
|
|
id = nm_connection_get_id (NM_CONNECTION (conns->pdata[i]));
|
2013-12-02 16:20:26 -05:00
|
|
|
g_assert (id);
|
|
|
|
|
names = g_slist_append (names, (gpointer) id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Find the next available unique connection name */
|
2014-11-11 16:41:22 -05:00
|
|
|
for (i = 1; !cname && i < 10000; i++) {
|
2013-12-02 16:20:26 -05:00
|
|
|
char *temp;
|
|
|
|
|
gboolean found = FALSE;
|
|
|
|
|
|
2016-06-05 11:46:06 +02:00
|
|
|
NM_PRAGMA_WARNING_DISABLE("-Wformat-nonliteral")
|
2013-12-02 16:20:26 -05:00
|
|
|
temp = g_strdup_printf (format, i);
|
2016-06-05 11:46:06 +02:00
|
|
|
NM_PRAGMA_WARNING_REENABLE
|
2013-12-02 16:20:26 -05:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-20 13:32:39 +01:00
|
|
|
static char *
|
|
|
|
|
get_available_iface_name (const char *try_name,
|
|
|
|
|
NMClient *client)
|
|
|
|
|
{
|
|
|
|
|
const GPtrArray *connections;
|
|
|
|
|
NMConnection *connection;
|
|
|
|
|
char *new_name;
|
2017-03-14 11:15:05 +01:00
|
|
|
unsigned num = 1;
|
2017-02-20 13:32:39 +01:00
|
|
|
int i = 0;
|
|
|
|
|
const char *ifname = NULL;
|
|
|
|
|
|
|
|
|
|
connections = nm_client_get_connections (client);
|
|
|
|
|
|
|
|
|
|
new_name = g_strdup (try_name);
|
|
|
|
|
while (i < connections->len) {
|
|
|
|
|
connection = NM_CONNECTION (connections->pdata[i]);
|
|
|
|
|
ifname = nm_connection_get_interface_name (connection);
|
|
|
|
|
if (g_strcmp0 (new_name, ifname) == 0) {
|
|
|
|
|
g_free (new_name);
|
|
|
|
|
new_name = g_strdup_printf ("%s%d", try_name, num++);
|
|
|
|
|
i = 0;
|
|
|
|
|
} else
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
return new_name;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-02 16:20:26 -05:00
|
|
|
/**
|
|
|
|
|
* nm_editor_utils_create_connection:
|
|
|
|
|
* @type: the type of the connection's primary #NMSetting
|
|
|
|
|
* @master: (allow-none): the connection's master, if any
|
2014-09-29 10:58:16 -04:00
|
|
|
* @client: an #NMClient
|
2013-12-02 16:20:26 -05:00
|
|
|
*
|
|
|
|
|
* Creates a new #NMConnection of the given type, automatically
|
|
|
|
|
* creating a UUID and an appropriate not-currently-in-use connection
|
|
|
|
|
* name, setting #NMSettingConnection:autoconnect appropriately for
|
|
|
|
|
* the connection type, filling in slave-related information if
|
|
|
|
|
* @master is not %NULL, and initializing any other mandatory-to-set
|
|
|
|
|
* properties to reasonable initial values.
|
|
|
|
|
*
|
|
|
|
|
* Returns: a new #NMConnection
|
|
|
|
|
*/
|
|
|
|
|
NMConnection *
|
2014-09-29 10:58:16 -04:00
|
|
|
nm_editor_utils_create_connection (GType type,
|
|
|
|
|
NMConnection *master,
|
|
|
|
|
NMClient *client)
|
2013-12-02 16:20:26 -05:00
|
|
|
{
|
|
|
|
|
NMEditorConnectionTypeData **types;
|
|
|
|
|
NMEditorConnectionTypeDataReal *type_data = NULL;
|
|
|
|
|
const char *master_setting_type = NULL, *master_uuid = NULL;
|
|
|
|
|
GType master_type = G_TYPE_INVALID, slave_setting_type = G_TYPE_INVALID;
|
|
|
|
|
NMConnection *connection;
|
|
|
|
|
NMSettingConnection *s_con;
|
|
|
|
|
NMSetting *s_hw, *s_slave;
|
2017-02-20 13:32:39 +01:00
|
|
|
char *uuid, *id, *ifname;
|
2013-12-02 16:20:26 -05:00
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if (master) {
|
|
|
|
|
NMSettingConnection *master_s_con;
|
|
|
|
|
|
|
|
|
|
master_s_con = nm_connection_get_setting_connection (master);
|
|
|
|
|
master_setting_type = nm_setting_connection_get_connection_type (master_s_con);
|
|
|
|
|
master_uuid = nm_setting_connection_get_uuid (master_s_con);
|
2014-08-12 17:25:26 -04:00
|
|
|
master_type = nm_setting_lookup_type (master_setting_type);
|
2013-12-02 16:20:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
types = nm_editor_utils_get_connection_type_list ();
|
|
|
|
|
for (i = 0; types[i]; i++) {
|
|
|
|
|
if (types[i]->setting_type == type)
|
|
|
|
|
type_data = (NMEditorConnectionTypeDataReal *)types[i];
|
|
|
|
|
if (types[i]->setting_type == master_type)
|
|
|
|
|
slave_setting_type = types[i]->slave_setting_type;
|
|
|
|
|
|
|
|
|
|
}
|
2014-04-15 16:54:38 +02:00
|
|
|
if (!type_data) {
|
|
|
|
|
g_return_val_if_reached (NULL);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2013-12-02 16:20:26 -05:00
|
|
|
|
2014-08-13 14:34:29 -04:00
|
|
|
connection = nm_simple_connection_new ();
|
2013-12-02 16:20:26 -05:00
|
|
|
|
|
|
|
|
s_con = NM_SETTING_CONNECTION (nm_setting_connection_new ());
|
|
|
|
|
nm_connection_add_setting (connection, NM_SETTING (s_con));
|
|
|
|
|
|
|
|
|
|
s_hw = g_object_new (type, NULL);
|
|
|
|
|
nm_connection_add_setting (connection, s_hw);
|
|
|
|
|
|
2017-02-20 13:32:39 +01:00
|
|
|
if (type == NM_TYPE_SETTING_BOND)
|
|
|
|
|
ifname = get_available_iface_name ("nm-bond", client);
|
|
|
|
|
else if (type == NM_TYPE_SETTING_TEAM)
|
|
|
|
|
ifname = get_available_iface_name ("nm-team", client);
|
|
|
|
|
else if (type == NM_TYPE_SETTING_BRIDGE)
|
|
|
|
|
ifname = get_available_iface_name ("nm-bridge", client);
|
|
|
|
|
else
|
|
|
|
|
ifname = NULL;
|
|
|
|
|
|
2013-12-02 16:20:26 -05:00
|
|
|
if (slave_setting_type != G_TYPE_INVALID) {
|
|
|
|
|
s_slave = g_object_new (slave_setting_type, NULL);
|
|
|
|
|
nm_connection_add_setting (connection, s_slave);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uuid = nm_utils_uuid_generate ();
|
2014-09-29 10:58:16 -04:00
|
|
|
id = get_available_connection_name (type_data->id_format, client);
|
2013-12-02 16:20:26 -05:00
|
|
|
|
|
|
|
|
g_object_set (s_con,
|
|
|
|
|
NM_SETTING_CONNECTION_UUID, uuid,
|
|
|
|
|
NM_SETTING_CONNECTION_ID, id,
|
|
|
|
|
NM_SETTING_CONNECTION_TYPE, nm_setting_get_name (s_hw),
|
|
|
|
|
NM_SETTING_CONNECTION_AUTOCONNECT, !type_data->no_autoconnect,
|
|
|
|
|
NM_SETTING_CONNECTION_MASTER, master_uuid,
|
|
|
|
|
NM_SETTING_CONNECTION_SLAVE_TYPE, master_setting_type,
|
2017-02-20 13:32:39 +01:00
|
|
|
NM_SETTING_CONNECTION_INTERFACE_NAME, ifname,
|
2013-12-02 16:20:26 -05:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
|
|
g_free (uuid);
|
|
|
|
|
g_free (id);
|
2017-02-20 13:32:39 +01:00
|
|
|
g_free (ifname);
|
2013-12-02 16:20:26 -05:00
|
|
|
|
|
|
|
|
if (type_data->connection_setup_func)
|
|
|
|
|
type_data->connection_setup_func (connection, s_con, s_hw);
|
|
|
|
|
|
|
|
|
|
return connection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* nm_editor_utils_get_connection_type_data:
|
|
|
|
|
* @conn: an #NMConnection
|
|
|
|
|
*
|
|
|
|
|
* Gets the #NMEditorConnectionTypeData corresponding to
|
|
|
|
|
* @conn's connection type.
|
|
|
|
|
*
|
|
|
|
|
* Returns: the #NMEditorConnectionTypeData
|
|
|
|
|
*/
|
|
|
|
|
NMEditorConnectionTypeData *
|
|
|
|
|
nm_editor_utils_get_connection_type_data (NMConnection *conn)
|
|
|
|
|
{
|
|
|
|
|
NMSettingConnection *s_con;
|
|
|
|
|
const char *conn_type;
|
|
|
|
|
GType conn_gtype;
|
|
|
|
|
NMEditorConnectionTypeData **types;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
s_con = nm_connection_get_setting_connection (conn);
|
|
|
|
|
g_return_val_if_fail (s_con != NULL, NULL);
|
|
|
|
|
|
|
|
|
|
conn_type = nm_setting_connection_get_connection_type (s_con);
|
2014-08-12 17:25:26 -04:00
|
|
|
conn_gtype = nm_setting_lookup_type (conn_type);
|
2013-12-02 16:20:26 -05:00
|
|
|
g_return_val_if_fail (conn_gtype != G_TYPE_INVALID, NULL);
|
|
|
|
|
|
|
|
|
|
types = nm_editor_utils_get_connection_type_list ();
|
|
|
|
|
for (i = 0; types[i]; i++) {
|
|
|
|
|
if (types[i]->setting_type == conn_gtype)
|
|
|
|
|
return types[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|