1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA.
19 * Copyright 2007 - 2013 Red Hat, Inc.
20 * Copyright 2007 - 2008 Novell, Inc.
23 #include "nm-default.h"
27 #include "nm-connection.h"
28 #include "nm-connection-private.h"
30 #include "nm-setting-private.h"
31 #include "nm-core-internal.h"
34 * SECTION:nm-connection
35 * @short_description: Describes a connection to specific network or provider
37 * An #NMConnection describes all the settings and configuration values that
38 * are necessary to configure network devices for operation on a specific
39 * network. Connections are the fundamental operating object for
40 * NetworkManager; no device is connected without a #NMConnection, or
41 * disconnected without having been connected with a #NMConnection.
43 * Each #NMConnection contains a list of #NMSetting objects usually referenced
44 * by name (using nm_connection_get_setting_by_name()) or by type (with
45 * nm_connection_get_setting()). The settings describe the actual parameters
46 * with which the network devices are configured, including device-specific
47 * parameters (MTU, SSID, APN, channel, rate, etc) and IP-level parameters
48 * (addresses, routes, addressing methods, etc).
57 /* D-Bus path of the connection, if any */
59 } NMConnectionPrivate;
61 static NMConnectionPrivate *nm_connection_get_private (NMConnection *connection);
62 #define NM_CONNECTION_GET_PRIVATE(o) (nm_connection_get_private ((NMConnection *)o))
64 G_DEFINE_INTERFACE (NMConnection, nm_connection, G_TYPE_OBJECT)
73 static guint signals[LAST_SIGNAL] = { 0 };
76 static NMSettingVerifyResult _nm_connection_verify (NMConnection *connection, GError **error);
79 /*************************************************************/
82 setting_changed_cb (NMSetting *setting,
86 g_signal_emit (self, signals[CHANGED], 0);
90 _setting_release (gpointer key, gpointer value, gpointer user_data)
92 g_signal_handlers_disconnect_by_func (user_data, setting_changed_cb, value);
97 _nm_connection_add_setting (NMConnection *connection, NMSetting *setting)
99 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (connection);
100 const char *name = G_OBJECT_TYPE_NAME (setting);
103 if ((s_old = g_hash_table_lookup (priv->settings, (gpointer) name)))
104 g_signal_handlers_disconnect_by_func (s_old, setting_changed_cb, connection);
105 g_hash_table_insert (priv->settings, (gpointer) name, setting);
106 /* Listen for property changes so we can emit the 'changed' signal */
107 g_signal_connect (setting, "notify", (GCallback) setting_changed_cb, connection);
111 * nm_connection_add_setting:
112 * @connection: a #NMConnection
113 * @setting: (transfer full): the #NMSetting to add to the connection object
115 * Adds a #NMSetting to the connection, replacing any previous #NMSetting of the
116 * same name which has previously been added to the #NMConnection. The
117 * connection takes ownership of the #NMSetting object and does not increase
118 * the setting object's reference count.
121 nm_connection_add_setting (NMConnection *connection, NMSetting *setting)
123 g_return_if_fail (NM_IS_CONNECTION (connection));
124 g_return_if_fail (NM_IS_SETTING (setting));
126 _nm_connection_add_setting (connection, setting);
127 g_signal_emit (connection, signals[CHANGED], 0);
131 * nm_connection_remove_setting:
132 * @connection: a #NMConnection
133 * @setting_type: the #GType of the setting object to remove
135 * Removes the #NMSetting with the given #GType from the #NMConnection. This
136 * operation dereferences the #NMSetting object.
139 nm_connection_remove_setting (NMConnection *connection, GType setting_type)
141 NMConnectionPrivate *priv;
143 const char *setting_name;
145 g_return_if_fail (NM_IS_CONNECTION (connection));
146 g_return_if_fail (g_type_is_a (setting_type, NM_TYPE_SETTING));
148 priv = NM_CONNECTION_GET_PRIVATE (connection);
149 setting_name = g_type_name (setting_type);
150 setting = g_hash_table_lookup (priv->settings, setting_name);
152 g_signal_handlers_disconnect_by_func (setting, setting_changed_cb, connection);
153 g_hash_table_remove (priv->settings, setting_name);
154 g_signal_emit (connection, signals[CHANGED], 0);
159 * nm_connection_get_setting:
160 * @connection: a #NMConnection
161 * @setting_type: the #GType of the setting object to return
163 * Gets the #NMSetting with the given #GType, if one has been previously added
164 * to the #NMConnection.
166 * Returns: (transfer none): the #NMSetting, or %NULL if no setting of that type was previously
167 * added to the #NMConnection
170 nm_connection_get_setting (NMConnection *connection, GType setting_type)
172 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
173 g_return_val_if_fail (g_type_is_a (setting_type, NM_TYPE_SETTING), NULL);
175 return (NMSetting *) g_hash_table_lookup (NM_CONNECTION_GET_PRIVATE (connection)->settings,
176 g_type_name (setting_type));
180 * nm_connection_get_setting_by_name:
181 * @connection: a #NMConnection
182 * @name: a setting name
184 * Gets the #NMSetting with the given name, if one has been previously added
187 * Returns: (transfer none): the #NMSetting, or %NULL if no setting with that name was previously
188 * added to the #NMConnection
191 nm_connection_get_setting_by_name (NMConnection *connection, const char *name)
195 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
196 g_return_val_if_fail (name != NULL, NULL);
198 type = nm_setting_lookup_type (name);
200 return type ? nm_connection_get_setting (connection, type) : NULL;
204 validate_permissions_type (GVariant *variant, GError **error)
207 GVariant *permissions;
208 gboolean valid = TRUE;
210 /* Ensure the connection::permissions item (if present) is the correct
211 * type, otherwise the g_object_set() will throw a warning and ignore the
212 * error, leaving us with no permissions.
214 s_con = g_variant_lookup_value (variant, NM_SETTING_CONNECTION_SETTING_NAME, NM_VARIANT_TYPE_SETTING);
218 permissions = g_variant_lookup_value (s_con, NM_SETTING_CONNECTION_PERMISSIONS, NULL);
220 if (!g_variant_is_of_type (permissions, G_VARIANT_TYPE_STRING_ARRAY)) {
221 g_set_error_literal (error,
223 NM_CONNECTION_ERROR_INVALID_PROPERTY,
224 _("wrong type; should be a list of strings."));
225 g_prefix_error (error, "%s.%s: ", NM_SETTING_CONNECTION_SETTING_NAME, NM_SETTING_CONNECTION_PERMISSIONS);
228 g_variant_unref (permissions);
231 g_variant_unref (s_con);
236 * nm_connection_replace_settings:
237 * @connection: a #NMConnection
238 * @new_settings: a #GVariant of type %NM_VARIANT_TYPE_CONNECTION, with the new settings
239 * @error: location to store error, or %NULL
241 * Replaces @connection's settings with @new_settings (which must be
242 * syntactically valid, and describe a known type of connection, but does not
243 * need to result in a connection that passes nm_connection_verify()).
245 * Returns: %TRUE if connection was updated, %FALSE if @new_settings could not
246 * be deserialized (in which case @connection will be unchanged).
249 nm_connection_replace_settings (NMConnection *connection,
250 GVariant *new_settings,
253 NMConnectionPrivate *priv;
255 const char *setting_name;
256 GVariant *setting_dict;
257 GSList *settings = NULL, *s;
260 g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
261 g_return_val_if_fail (g_variant_is_of_type (new_settings, NM_VARIANT_TYPE_CONNECTION), FALSE);
262 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
264 priv = NM_CONNECTION_GET_PRIVATE (connection);
266 if (!validate_permissions_type (new_settings, error))
269 g_variant_iter_init (&iter, new_settings);
270 while (g_variant_iter_next (&iter, "{&s@a{sv}}", &setting_name, &setting_dict)) {
274 type = nm_setting_lookup_type (setting_name);
275 if (type == G_TYPE_INVALID) {
276 g_set_error_literal (error,
278 NM_CONNECTION_ERROR_INVALID_SETTING,
279 _("unknown setting name"));
280 g_prefix_error (error, "%s: ", setting_name);
281 g_variant_unref (setting_dict);
282 g_slist_free_full (settings, g_object_unref);
286 setting = _nm_setting_new_from_dbus (type, setting_dict, new_settings, error);
287 g_variant_unref (setting_dict);
290 g_slist_free_full (settings, g_object_unref);
294 settings = g_slist_prepend (settings, setting);
297 if (g_hash_table_size (priv->settings) > 0) {
298 g_hash_table_foreach_remove (priv->settings, _setting_release, connection);
301 changed = (settings != NULL);
303 for (s = settings; s; s = s->next)
304 _nm_connection_add_setting (connection, s->data);
306 g_slist_free (settings);
309 g_signal_emit (connection, signals[CHANGED], 0);
314 * nm_connection_replace_settings_from_connection:
315 * @connection: a #NMConnection
316 * @new_connection: a #NMConnection to replace the settings of @connection with
318 * Deep-copies the settings of @new_connection and replaces the settings of @connection
319 * with the copied settings.
322 nm_connection_replace_settings_from_connection (NMConnection *connection,
323 NMConnection *new_connection)
325 NMConnectionPrivate *priv, *new_priv;
330 g_return_if_fail (NM_IS_CONNECTION (connection));
331 g_return_if_fail (NM_IS_CONNECTION (new_connection));
333 /* When 'connection' and 'new_connection' are the same object simply return
334 * in order not to destroy 'connection'.
336 if (connection == new_connection)
339 /* No need to validate permissions like nm_connection_replace_settings()
340 * since we're dealing with an NMConnection which has already done that.
343 priv = NM_CONNECTION_GET_PRIVATE (connection);
344 new_priv = NM_CONNECTION_GET_PRIVATE (new_connection);
346 if ((changed = g_hash_table_size (priv->settings) > 0))
347 g_hash_table_foreach_remove (priv->settings, _setting_release, connection);
349 if (g_hash_table_size (new_priv->settings)) {
350 g_hash_table_iter_init (&iter, new_priv->settings);
351 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting))
352 _nm_connection_add_setting (connection, nm_setting_duplicate (setting));
357 g_signal_emit (connection, signals[CHANGED], 0);
361 * nm_connection_clear_settings:
362 * @connection: a #NMConnection
364 * Deletes all of @connection's settings.
367 nm_connection_clear_settings (NMConnection *connection)
369 NMConnectionPrivate *priv;
371 g_return_if_fail (NM_IS_CONNECTION (connection));
373 priv = NM_CONNECTION_GET_PRIVATE (connection);
375 if (g_hash_table_size (priv->settings) > 0) {
376 g_hash_table_foreach_remove (priv->settings, _setting_release, connection);
377 g_signal_emit (connection, signals[CHANGED], 0);
382 * nm_connection_compare:
383 * @a: a #NMConnection
384 * @b: a second #NMConnection to compare with the first
385 * @flags: compare flags, e.g. %NM_SETTING_COMPARE_FLAG_EXACT
387 * Compares two #NMConnection objects for similarity, with comparison behavior
388 * modified by a set of flags. See nm_setting_compare() for a description of
389 * each flag's behavior.
391 * Returns: %TRUE if the comparison succeeds, %FALSE if it does not
394 nm_connection_compare (NMConnection *a,
396 NMSettingCompareFlags flags)
406 /* B / A: ensure settings in B that are not in A make the comparison fail */
407 if (g_hash_table_size (NM_CONNECTION_GET_PRIVATE (a)->settings) !=
408 g_hash_table_size (NM_CONNECTION_GET_PRIVATE (b)->settings))
411 /* A / B: ensure all settings in A match corresponding ones in B */
412 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (a)->settings);
413 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &src)) {
414 NMSetting *cmp = nm_connection_get_setting (b, G_OBJECT_TYPE (src));
416 if (!cmp || !nm_setting_compare (src, cmp, flags))
425 diff_one_connection (NMConnection *a,
427 NMSettingCompareFlags flags,
428 gboolean invert_results,
431 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (a);
433 NMSetting *a_setting = NULL;
435 g_hash_table_iter_init (&iter, priv->settings);
436 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &a_setting)) {
437 NMSetting *b_setting = NULL;
438 const char *setting_name = nm_setting_get_name (a_setting);
440 gboolean new_results = TRUE;
443 b_setting = nm_connection_get_setting (b, G_OBJECT_TYPE (a_setting));
445 results = g_hash_table_lookup (diffs, setting_name);
449 if (!nm_setting_diff (a_setting, b_setting, flags, invert_results, &results)) {
451 g_hash_table_insert (diffs, g_strdup (setting_name), results);
457 * nm_connection_diff:
458 * @a: a #NMConnection
459 * @b: a second #NMConnection to compare with the first
460 * @flags: compare flags, e.g. %NM_SETTING_COMPARE_FLAG_EXACT
461 * @out_settings: (element-type utf8 GLib.HashTable): if the
462 * connections differ, on return a hash table mapping setting names to
463 * second-level GHashTable (utf8 to guint32), which contains the key names that
464 * differ mapped to one or more of %NMSettingDiffResult as a bitfield
466 * Compares two #NMConnection objects for similarity, with comparison behavior
467 * modified by a set of flags. See nm_setting_compare() for a description of
468 * each flag's behavior. If the connections differ, settings and keys within
469 * each setting that differ are added to the returned @out_settings hash table.
470 * No values are returned, only key names.
472 * Returns: %TRUE if the connections contain the same values, %FALSE if they do
476 nm_connection_diff (NMConnection *a,
478 NMSettingCompareFlags flags,
479 GHashTable **out_settings)
483 g_return_val_if_fail (NM_IS_CONNECTION (a), FALSE);
484 g_return_val_if_fail (out_settings != NULL, FALSE);
485 g_return_val_if_fail (*out_settings == NULL, FALSE);
487 g_return_val_if_fail (NM_IS_CONNECTION (b), FALSE);
492 diffs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_destroy);
494 /* Diff A to B, then B to A to capture keys in B that aren't in A */
495 diff_one_connection (a, b, flags, FALSE, diffs);
497 diff_one_connection (b, a, flags, TRUE, diffs);
499 if (g_hash_table_size (diffs) == 0)
500 g_hash_table_destroy (diffs);
502 *out_settings = diffs;
504 return *out_settings ? FALSE : TRUE;
508 _nm_connection_find_base_type_setting (NMConnection *connection)
510 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (connection);
512 NMSetting *setting = NULL, *s_iter;
514 g_hash_table_iter_init (&iter, priv->settings);
515 while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &s_iter)) {
516 if (!_nm_setting_is_base_type (s_iter))
520 /* FIXME: currently, if there is more than one matching base type,
521 * we cannot detect the base setting.
522 * See: https://bugzilla.gnome.org/show_bug.cgi?id=696936#c8 */
531 _normalize_connection_uuid (NMConnection *self)
533 NMSettingConnection *s_con = nm_connection_get_setting_connection (self);
538 if (nm_setting_connection_get_uuid (s_con))
541 uuid = nm_utils_uuid_generate ();
542 g_object_set (s_con, NM_SETTING_CONNECTION_UUID, uuid, NULL);
549 _normalize_connection_type (NMConnection *self)
551 NMSettingConnection *s_con = nm_connection_get_setting_connection (self);
552 NMSetting *s_base = NULL;
555 type = nm_setting_connection_get_connection_type (s_con);
558 s_base = nm_connection_get_setting_by_name (self, type);
561 GType base_type = nm_setting_lookup_type (type);
563 g_return_val_if_fail (base_type, FALSE);
564 nm_connection_add_setting (self, g_object_new (base_type, NULL));
568 s_base = _nm_connection_find_base_type_setting (self);
569 g_return_val_if_fail (s_base, FALSE);
571 type = nm_setting_get_name (s_base);
572 g_object_set (s_con, NM_SETTING_CONNECTION_TYPE, type, NULL);
580 _nm_connection_detect_slave_type (NMConnection *connection, NMSetting **out_s_port)
582 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (connection);
584 const char *slave_type = NULL;
585 NMSetting *s_port = NULL, *s_iter;
587 g_hash_table_iter_init (&iter, priv->settings);
588 while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &s_iter)) {
589 const char *name = nm_setting_get_name (s_iter);
590 const char *i_slave_type = NULL;
592 if (!strcmp (name, NM_SETTING_BRIDGE_PORT_SETTING_NAME))
593 i_slave_type = NM_SETTING_BRIDGE_SETTING_NAME;
594 else if (!strcmp (name, NM_SETTING_TEAM_PORT_SETTING_NAME))
595 i_slave_type = NM_SETTING_TEAM_SETTING_NAME;
600 /* there are more then one matching port types, cannot detect the slave type. */
605 slave_type = i_slave_type;
610 *out_s_port = s_port;
615 _normalize_connection_slave_type (NMConnection *self)
617 NMSettingConnection *s_con = nm_connection_get_setting_connection (self);
618 const char *slave_type, *port_type;
622 if (!nm_setting_connection_get_master (s_con))
625 slave_type = nm_setting_connection_get_slave_type (s_con);
627 if ( _nm_setting_slave_type_is_valid (slave_type, &port_type)
631 s_port = nm_connection_get_setting_by_name (self, port_type);
633 GType p_type = nm_setting_lookup_type (port_type);
635 g_return_val_if_fail (p_type, FALSE);
636 nm_connection_add_setting (self, g_object_new (p_type, NULL));
641 if ((slave_type = _nm_connection_detect_slave_type (self, NULL))) {
642 g_object_set (s_con, NM_SETTING_CONNECTION_SLAVE_TYPE, slave_type, NULL);
650 _normalize_ip_config (NMConnection *self, GHashTable *parameters)
652 NMSettingConnection *s_con = nm_connection_get_setting_connection (self);
653 const char *default_ip4_method = NM_SETTING_IP4_CONFIG_METHOD_AUTO;
654 const char *default_ip6_method = NULL;
655 NMSettingIPConfig *s_ip4, *s_ip6;
659 default_ip6_method = g_hash_table_lookup (parameters, NM_CONNECTION_NORMALIZE_PARAM_IP6_CONFIG_METHOD);
660 if (!default_ip6_method)
661 default_ip6_method = NM_SETTING_IP6_CONFIG_METHOD_AUTO;
663 s_ip4 = nm_connection_get_setting_ip4_config (self);
664 s_ip6 = nm_connection_get_setting_ip6_config (self);
666 if (nm_setting_connection_get_master (s_con)) {
667 /* Slave connections don't have IP configuration. */
670 nm_connection_remove_setting (self, NM_TYPE_SETTING_IP4_CONFIG);
673 nm_connection_remove_setting (self, NM_TYPE_SETTING_IP6_CONFIG);
675 return s_ip4 || s_ip6;
677 /* Ensure all non-slave connections have IP4 and IP6 settings objects. If no
678 * IP6 setting was specified, then assume that means IP6 config is allowed
679 * to fail. But if no IP4 setting was specified, assume the caller was just
683 setting = nm_setting_ip4_config_new ();
685 g_object_set (setting,
686 NM_SETTING_IP_CONFIG_METHOD, default_ip4_method,
688 nm_connection_add_setting (self, setting);
691 setting = nm_setting_ip6_config_new ();
693 g_object_set (setting,
694 NM_SETTING_IP_CONFIG_METHOD, default_ip6_method,
695 NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE,
697 nm_connection_add_setting (self, setting);
699 return !s_ip4 || !s_ip6;
704 _normalize_infiniband_mtu (NMConnection *self, GHashTable *parameters)
706 NMSettingInfiniband *s_infini = nm_connection_get_setting_infiniband (self);
709 const char *transport_mode = nm_setting_infiniband_get_transport_mode (s_infini);
712 if (transport_mode) {
713 if (!strcmp (transport_mode, "datagram"))
715 else if (!strcmp (transport_mode, "connected"))
718 if (max_mtu && nm_setting_infiniband_get_mtu (s_infini) > max_mtu) {
719 g_object_set (s_infini, NM_SETTING_INFINIBAND_MTU, max_mtu, NULL);
728 _normalize_bond_mode (NMConnection *self, GHashTable *parameters)
730 NMSettingBond *s_bond = nm_connection_get_setting_bond (self);
732 /* Convert mode from numeric to string notation */
734 const char *mode = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_MODE);
735 int mode_int = nm_utils_bond_mode_string_to_int (mode);
737 if (mode_int != -1) {
738 const char *mode_new = nm_utils_bond_mode_int_to_string (mode_int);
739 if (g_strcmp0 (mode_new, mode) != 0) {
740 nm_setting_bond_add_option (s_bond, NM_SETTING_BOND_OPTION_MODE, mode_new);
749 * nm_connection_verify:
750 * @connection: the #NMConnection to verify
751 * @error: location to store error, or %NULL
753 * Validates the connection and all its settings. Each setting's properties
754 * have allowed values, and some values are dependent on other values. For
755 * example, if a Wi-Fi connection is security enabled, the #NMSettingWireless
756 * setting object's 'security' property must contain the setting name of the
757 * #NMSettingWirelessSecurity object, which must also be present in the
758 * connection for the connection to be valid. As another example, the
759 * #NMSettingWired object's 'mac-address' property must be a validly formatted
760 * MAC address. The returned #GError contains information about which
761 * setting and which property failed validation, and how it failed validation.
763 * Returns: %TRUE if the connection is valid, %FALSE if it is not
766 nm_connection_verify (NMConnection *connection, GError **error)
768 NMSettingVerifyResult result;
770 result = _nm_connection_verify (connection, error);
772 /* we treat normalizable connections as valid. */
773 if (result == NM_SETTING_VERIFY_NORMALIZABLE)
774 g_clear_error (error);
776 return result == NM_SETTING_VERIFY_SUCCESS || result == NM_SETTING_VERIFY_NORMALIZABLE;
779 static NMSettingVerifyResult
780 _nm_connection_verify (NMConnection *connection, GError **error)
782 NMConnectionPrivate *priv;
783 NMSettingConnection *s_con;
784 NMSettingIPConfig *s_ip4, *s_ip6;
787 GSList *all_settings = NULL, *setting_i;
788 NMSettingVerifyResult success = NM_SETTING_VERIFY_ERROR;
789 GError *normalizable_error = NULL;
790 NMSettingVerifyResult normalizable_error_type = NM_SETTING_VERIFY_SUCCESS;
792 g_return_val_if_fail (NM_IS_CONNECTION (connection), NM_SETTING_VERIFY_ERROR);
793 g_return_val_if_fail (!error || !*error, NM_SETTING_VERIFY_ERROR);
795 priv = NM_CONNECTION_GET_PRIVATE (connection);
797 /* First, make sure there's at least 'connection' setting */
798 s_con = nm_connection_get_setting_connection (connection);
800 g_set_error_literal (error,
802 NM_CONNECTION_ERROR_MISSING_SETTING,
803 _("setting not found"));
804 g_prefix_error (error, "%s: ", NM_SETTING_CONNECTION_SETTING_NAME);
808 /* Build up the list of settings */
809 g_hash_table_iter_init (&iter, priv->settings);
810 while (g_hash_table_iter_next (&iter, NULL, &value)) {
811 /* Order NMSettingConnection so that it will be verified first.
812 * The reason is, that errors in this setting might be more fundamental
813 * and should be checked and reported with higher priority.
816 all_settings = g_slist_append (all_settings, value);
818 all_settings = g_slist_prepend (all_settings, value);
820 all_settings = g_slist_reverse (all_settings);
822 /* Now, run the verify function of each setting */
823 for (setting_i = all_settings; setting_i; setting_i = setting_i->next) {
824 GError *verify_error = NULL;
825 NMSettingVerifyResult verify_result;
827 /* verify all settings. We stop if we find the first non-normalizable
828 * @NM_SETTING_VERIFY_ERROR. If we find normalizable errors we continue
829 * but remember the error to return it to the user.
830 * @NM_SETTING_VERIFY_NORMALIZABLE_ERROR has a higher priority then
831 * @NM_SETTING_VERIFY_NORMALIZABLE, so, if we encounter such an error type,
832 * we remember it instead (to return it as output).
834 verify_result = _nm_setting_verify (NM_SETTING (setting_i->data), connection, &verify_error);
835 if (verify_result == NM_SETTING_VERIFY_NORMALIZABLE ||
836 verify_result == NM_SETTING_VERIFY_NORMALIZABLE_ERROR) {
837 if ( verify_result == NM_SETTING_VERIFY_NORMALIZABLE_ERROR
838 && normalizable_error_type == NM_SETTING_VERIFY_NORMALIZABLE) {
839 /* NORMALIZABLE_ERROR has higher priority. */
840 g_clear_error (&normalizable_error);
842 if (!normalizable_error) {
843 g_propagate_error (&normalizable_error, verify_error);
845 normalizable_error_type = verify_result;
847 } else if (verify_result != NM_SETTING_VERIFY_SUCCESS) {
848 g_propagate_error (error, verify_error);
849 g_slist_free (all_settings);
850 g_return_val_if_fail (verify_result == NM_SETTING_VERIFY_ERROR, success);
853 g_clear_error (&verify_error);
855 g_slist_free (all_settings);
857 s_ip4 = nm_connection_get_setting_ip4_config (connection);
858 s_ip6 = nm_connection_get_setting_ip6_config (connection);
860 if (nm_setting_connection_get_master (s_con)) {
861 if ((normalizable_error_type == NM_SETTING_VERIFY_SUCCESS ||
862 (normalizable_error_type == NM_SETTING_VERIFY_NORMALIZABLE)) && (s_ip4 || s_ip6)) {
863 g_clear_error (&normalizable_error);
864 g_set_error_literal (&normalizable_error,
866 NM_CONNECTION_ERROR_INVALID_SETTING,
867 _("setting not allowed in slave connection"));
868 g_prefix_error (&normalizable_error, "%s: ",
869 s_ip4 ? NM_SETTING_IP4_CONFIG_SETTING_NAME : NM_SETTING_IP6_CONFIG_SETTING_NAME);
870 /* having a slave with IP config *was* and is a verify() error. */
871 normalizable_error_type = NM_SETTING_VERIFY_NORMALIZABLE_ERROR;
874 if (normalizable_error_type == NM_SETTING_VERIFY_SUCCESS && (!s_ip4 || !s_ip6)) {
875 g_set_error_literal (&normalizable_error,
877 NM_CONNECTION_ERROR_MISSING_SETTING,
878 _("setting is required for non-slave connections"));
879 g_prefix_error (&normalizable_error, "%s: ",
880 !s_ip4 ? NM_SETTING_IP4_CONFIG_SETTING_NAME : NM_SETTING_IP6_CONFIG_SETTING_NAME);
881 /* having a master without IP config was not a verify() error, accept
882 * it for backward compatibility. */
883 normalizable_error_type = NM_SETTING_VERIFY_NORMALIZABLE;
887 if (normalizable_error_type != NM_SETTING_VERIFY_SUCCESS) {
888 g_propagate_error (error, normalizable_error);
889 normalizable_error = NULL;
890 success = normalizable_error_type;
892 success = NM_SETTING_VERIFY_SUCCESS;
895 g_clear_error (&normalizable_error);
900 * nm_connection_verify_secrets:
901 * @connection: the #NMConnection to verify in
902 * @error: location to store error, or %NULL
904 * Verifies the secrets in the connection.
906 * Returns: %TRUE if the secrets are valid, %FALSE if they are not
911 nm_connection_verify_secrets (NMConnection *connection, GError **error)
916 g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
917 g_return_val_if_fail (!error || !*error, FALSE);
919 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
920 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting)) {
921 if (!nm_setting_verify_secrets (setting, connection, error))
928 * nm_connection_normalize:
929 * @connection: the #NMConnection to normalize
930 * @parameters: (allow-none) (element-type utf8 gpointer): a #GHashTable with
931 * normalization parameters to allow customization of the normalization by providing
932 * specific arguments. Unknown arguments will be ignored and the default will be
933 * used. The keys must be strings, hashed by g_str_hash() and g_str_equal() functions.
934 * The values are opaque and depend on the parameter name.
935 * @modified: (out) (allow-none): outputs whether any settings were modified.
936 * @error: location to store error, or %NULL. Contains the reason,
937 * why the connection is invalid, if the function returns an error.
939 * Does some basic normalization and fixup of well known inconsistencies
940 * and deprecated fields. If the connection was modified in any way,
941 * the output parameter @modified is set %TRUE.
943 * Finally the connection will be verified and %TRUE returns if the connection
944 * is valid. As this function only performs some specific normalization steps
945 * it cannot repair all connections. If the connection has errors that
946 * cannot be normalized, the connection will not be modified.
948 * Returns: %TRUE if the connection is valid, %FALSE if it is not
951 nm_connection_normalize (NMConnection *connection,
952 GHashTable *parameters,
956 NMSettingVerifyResult success;
957 gboolean was_modified = FALSE;
958 GError *normalizable_error = NULL;
960 success = _nm_connection_verify (connection, &normalizable_error);
962 if (success == NM_SETTING_VERIFY_ERROR ||
963 success == NM_SETTING_VERIFY_SUCCESS) {
964 if (normalizable_error)
965 g_propagate_error (error, normalizable_error);
968 if (success == NM_SETTING_VERIFY_ERROR && error && !*error) {
969 g_set_error_literal (error,
971 NM_CONNECTION_ERROR_FAILED,
972 _("Unexpected failure to verify the connection"));
973 g_return_val_if_reached (FALSE);
975 return success == NM_SETTING_VERIFY_SUCCESS;
977 g_assert (success == NM_SETTING_VERIFY_NORMALIZABLE || success == NM_SETTING_VERIFY_NORMALIZABLE_ERROR);
978 g_clear_error (&normalizable_error);
980 /* Try to perform all kind of normalizations on the settings to fix it.
981 * We only do this, after verifying that the connection contains no un-normalizable
982 * errors, because in that case we rather fail without touching the settings. */
984 was_modified |= _normalize_connection_uuid (connection);
985 was_modified |= _normalize_connection_type (connection);
986 was_modified |= _normalize_connection_slave_type (connection);
987 was_modified |= _normalize_ip_config (connection, parameters);
988 was_modified |= _normalize_infiniband_mtu (connection, parameters);
989 was_modified |= _normalize_bond_mode (connection, parameters);
992 success = _nm_connection_verify (connection, error);
995 *modified = was_modified;
997 if (success != NM_SETTING_VERIFY_SUCCESS) {
998 /* we would expect, that after normalization, the connection can be verified.
999 * Also treat NM_SETTING_VERIFY_NORMALIZABLE as failure, because there is something
1001 if (error && !*error) {
1002 g_set_error_literal (error,
1003 NM_CONNECTION_ERROR,
1004 NM_CONNECTION_ERROR_FAILED,
1005 _("Unexpected failure to normalize the connection"));
1007 g_return_val_if_reached (FALSE);
1010 /* we would expect, that the connection was modified during normalization. */
1011 g_return_val_if_fail (was_modified, TRUE);
1017 * nm_connection_update_secrets:
1018 * @connection: the #NMConnection
1019 * @setting_name: the setting object name to which the secrets apply
1020 * @secrets: a #GVariant of secrets, of type %NM_VARIANT_TYPE_CONNECTION
1021 * or %NM_VARIANT_TYPE_SETTING
1022 * @error: location to store error, or %NULL
1024 * Update the specified setting's secrets, given a dictionary of secrets
1025 * intended for that setting (deserialized from D-Bus for example). Will also
1026 * extract the given setting's secrets hash if given a connection dictionary.
1027 * If @setting_name is %NULL, expects a fully serialized #NMConnection as
1028 * returned by nm_connection_to_dbus() and will update all secrets from all
1029 * settings contained in @secrets.
1031 * Returns: %TRUE if the secrets were successfully updated, %FALSE if the update
1032 * failed (tried to update secrets for a setting that doesn't exist, etc)
1035 nm_connection_update_secrets (NMConnection *connection,
1036 const char *setting_name,
1041 gboolean success = TRUE, updated = FALSE;
1042 GVariant *setting_dict = NULL;
1045 gboolean full_connection;
1048 g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
1049 g_return_val_if_fail ( g_variant_is_of_type (secrets, NM_VARIANT_TYPE_SETTING)
1050 || g_variant_is_of_type (secrets, NM_VARIANT_TYPE_CONNECTION), FALSE);
1052 g_return_val_if_fail (*error == NULL, FALSE);
1054 full_connection = g_variant_is_of_type (secrets, NM_VARIANT_TYPE_CONNECTION);
1055 g_return_val_if_fail (setting_name != NULL || full_connection, FALSE);
1057 /* Empty @secrets means success */
1058 if (g_variant_n_children (secrets) == 0)
1062 /* Update just one setting's secrets */
1063 setting = nm_connection_get_setting_by_name (connection, setting_name);
1065 g_set_error_literal (error,
1066 NM_CONNECTION_ERROR,
1067 NM_CONNECTION_ERROR_SETTING_NOT_FOUND,
1072 if (full_connection) {
1073 setting_dict = g_variant_lookup_value (secrets, setting_name, NM_VARIANT_TYPE_SETTING);
1074 if (!setting_dict) {
1075 /* The connection dictionary didn't contain any secrets for
1076 * @setting_name; just return success.
1082 g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection);
1083 success_detail = _nm_setting_update_secrets (setting,
1084 setting_dict ? setting_dict : secrets,
1086 g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection);
1088 g_clear_pointer (&setting_dict, g_variant_unref);
1090 if (success_detail == NM_SETTING_UPDATE_SECRET_ERROR)
1092 if (success_detail == NM_SETTING_UPDATE_SECRET_SUCCESS_MODIFIED)
1095 /* check first, whether all the settings exist... */
1096 g_variant_iter_init (&iter, secrets);
1097 while (g_variant_iter_next (&iter, "{&s@a{sv}}", &key, NULL)) {
1098 setting = nm_connection_get_setting_by_name (connection, key);
1100 g_set_error_literal (error,
1101 NM_CONNECTION_ERROR,
1102 NM_CONNECTION_ERROR_SETTING_NOT_FOUND,
1108 /* Update each setting with any secrets from the connection dictionary */
1109 g_variant_iter_init (&iter, secrets);
1110 while (g_variant_iter_next (&iter, "{&s@a{sv}}", &key, &setting_dict)) {
1111 /* Update the secrets for this setting */
1112 setting = nm_connection_get_setting_by_name (connection, key);
1114 g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection);
1115 success_detail = _nm_setting_update_secrets (setting, setting_dict, error);
1116 g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection);
1118 g_variant_unref (setting_dict);
1120 if (success_detail == NM_SETTING_UPDATE_SECRET_ERROR) {
1124 if (success_detail == NM_SETTING_UPDATE_SECRET_SUCCESS_MODIFIED)
1130 g_signal_emit (connection, signals[SECRETS_UPDATED], 0, setting_name);
1131 g_signal_emit (connection, signals[CHANGED], 0);
1138 * nm_connection_need_secrets:
1139 * @connection: the #NMConnection
1140 * @hints: (out) (element-type utf8) (allow-none) (transfer container):
1141 * the address of a pointer to a #GPtrArray, initialized to %NULL, which on
1142 * return points to an allocated #GPtrArray containing the property names of
1143 * secrets of the #NMSetting which may be required; the caller owns the array
1144 * and must free the array itself with g_ptr_array_free(), but not free its
1147 * Returns the name of the first setting object in the connection which would
1148 * need secrets to make a successful connection. The returned hints are only
1149 * intended as a guide to what secrets may be required, because in some
1150 * circumstances, there is no way to conclusively determine exactly which
1151 * secrets are needed.
1153 * Returns: the setting name of the #NMSetting object which has invalid or
1157 nm_connection_need_secrets (NMConnection *connection,
1160 NMConnectionPrivate *priv;
1161 GHashTableIter hiter;
1162 GSList *settings = NULL;
1164 const char *name = NULL;
1167 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1169 g_return_val_if_fail (*hints == NULL, NULL);
1171 priv = NM_CONNECTION_GET_PRIVATE (connection);
1173 /* Get list of settings in priority order */
1174 g_hash_table_iter_init (&hiter, priv->settings);
1175 while (g_hash_table_iter_next (&hiter, NULL, (gpointer) &setting))
1176 settings = g_slist_insert_sorted (settings, setting, _nm_setting_compare_priority);
1178 for (iter = settings; iter; iter = g_slist_next (iter)) {
1181 setting = NM_SETTING (iter->data);
1182 secrets = _nm_setting_need_secrets (setting);
1187 g_ptr_array_free (secrets, TRUE);
1189 name = nm_setting_get_name (setting);
1194 g_slist_free (settings);
1199 * nm_connection_clear_secrets:
1200 * @connection: the #NMConnection
1202 * Clears and frees any secrets that may be stored in the connection, to avoid
1203 * keeping secret data in memory when not needed.
1206 nm_connection_clear_secrets (NMConnection *connection)
1208 GHashTableIter iter;
1210 gboolean changed = FALSE;
1212 g_return_if_fail (NM_IS_CONNECTION (connection));
1214 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1215 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting)) {
1216 g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection);
1217 changed |= _nm_setting_clear_secrets (setting);
1218 g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection);
1221 g_signal_emit (connection, signals[SECRETS_CLEARED], 0);
1223 g_signal_emit (connection, signals[CHANGED], 0);
1227 * nm_connection_clear_secrets_with_flags:
1228 * @connection: the #NMConnection
1229 * @func: (scope call): function to be called to determine whether a
1230 * specific secret should be cleared or not
1231 * @user_data: caller-supplied data passed to @func
1233 * Clears and frees secrets determined by @func.
1236 nm_connection_clear_secrets_with_flags (NMConnection *connection,
1237 NMSettingClearSecretsWithFlagsFn func,
1240 GHashTableIter iter;
1242 gboolean changed = FALSE;
1244 g_return_if_fail (NM_IS_CONNECTION (connection));
1246 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1247 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting)) {
1248 g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection);
1249 changed |= _nm_setting_clear_secrets_with_flags (setting, func, user_data);
1250 g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection);
1253 g_signal_emit (connection, signals[SECRETS_CLEARED], 0);
1255 g_signal_emit (connection, signals[CHANGED], 0);
1259 * nm_connection_to_dbus:
1260 * @connection: the #NMConnection
1261 * @flags: serialization flags, e.g. %NM_CONNECTION_SERIALIZE_ALL
1263 * Converts the #NMConnection into a #GVariant of type
1264 * %NM_VARIANT_TYPE_CONNECTION describing the connection, suitable for
1265 * marshalling over D-Bus or otherwise serializing.
1267 * Returns: (transfer none): a new floating #GVariant describing the connection,
1268 * its settings, and each setting's properties.
1271 nm_connection_to_dbus (NMConnection *connection,
1272 NMConnectionSerializationFlags flags)
1274 NMConnectionPrivate *priv;
1275 GVariantBuilder builder;
1276 GHashTableIter iter;
1278 GVariant *setting_dict, *ret;
1280 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1281 priv = NM_CONNECTION_GET_PRIVATE (connection);
1283 g_variant_builder_init (&builder, NM_VARIANT_TYPE_CONNECTION);
1285 /* Add each setting's hash to the main hash */
1286 g_hash_table_iter_init (&iter, priv->settings);
1287 while (g_hash_table_iter_next (&iter, &key, &data)) {
1288 NMSetting *setting = NM_SETTING (data);
1290 setting_dict = _nm_setting_to_dbus (setting, connection, flags);
1292 g_variant_builder_add (&builder, "{s@a{sv}}", nm_setting_get_name (setting), setting_dict);
1295 ret = g_variant_builder_end (&builder);
1297 /* Don't send empty hashes */
1298 if (g_variant_n_children (ret) == 0) {
1299 g_variant_unref (ret);
1307 * nm_connection_is_type:
1308 * @connection: the #NMConnection
1309 * @type: a setting name to check the connection's type against (like
1310 * %NM_SETTING_WIRELESS_SETTING_NAME or %NM_SETTING_WIRED_SETTING_NAME)
1312 * A convenience function to check if the given @connection is a particular
1313 * type (ie wired, Wi-Fi, ppp, etc). Checks the #NMSettingConnection:type
1314 * property of the connection and matches that against @type.
1316 * Returns: %TRUE if the connection is of the given @type, %FALSE if not
1319 nm_connection_is_type (NMConnection *connection, const char *type)
1321 NMSettingConnection *s_con;
1324 g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
1325 g_return_val_if_fail (type != NULL, FALSE);
1327 s_con = nm_connection_get_setting_connection (connection);
1331 type2 = nm_setting_connection_get_connection_type (s_con);
1333 return (g_strcmp0 (type2, type) == 0);
1337 _for_each_sort (NMSetting **p_a, NMSetting **p_b, void *unused)
1339 NMSetting *a = *p_a;
1340 NMSetting *b = *p_b;
1343 c = _nm_setting_compare_priority (a, b);
1346 return strcmp (nm_setting_get_name (a), nm_setting_get_name (b));
1350 * nm_connection_for_each_setting_value:
1351 * @connection: the #NMConnection
1352 * @func: (scope call): user-supplied function called for each setting's property
1353 * @user_data: user data passed to @func at each invocation
1355 * Iterates over the properties of each #NMSetting object in the #NMConnection,
1356 * calling the supplied user function for each property.
1359 nm_connection_for_each_setting_value (NMConnection *connection,
1360 NMSettingValueIterFn func,
1363 NMConnectionPrivate *priv;
1364 gs_free NMSetting **arr_free = NULL;
1365 NMSetting *arr_temp[20], **arr;
1366 GHashTableIter iter;
1370 g_return_if_fail (NM_IS_CONNECTION (connection));
1371 g_return_if_fail (func != NULL);
1373 priv = NM_CONNECTION_GET_PRIVATE (connection);
1375 size = g_hash_table_size (priv->settings);
1379 if (size > G_N_ELEMENTS (arr_temp))
1380 arr = arr_free = g_new (NMSetting *, size);
1384 g_hash_table_iter_init (&iter, priv->settings);
1385 for (i = 0; g_hash_table_iter_next (&iter, NULL, &value); i++)
1386 arr[i] = NM_SETTING (value);
1387 g_assert (i == size);
1389 /* sort the settings. This has an effect on the order in which keyfile
1392 g_qsort_with_data (arr, size, sizeof (NMSetting *), (GCompareDataFunc) _for_each_sort, NULL);
1394 for (i = 0; i < size; i++)
1395 nm_setting_enumerate_values (arr[i], func, user_data);
1399 * nm_connection_dump:
1400 * @connection: the #NMConnection
1402 * Print the connection to stdout. For debugging purposes ONLY, should NOT
1403 * be used for serialization of the connection or machine-parsed in any way. The
1404 * output format is not guaranteed to be stable and may change at any time.
1407 nm_connection_dump (NMConnection *connection)
1409 GHashTableIter iter;
1411 const char *setting_name;
1417 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1418 while (g_hash_table_iter_next (&iter, (gpointer) &setting_name, (gpointer) &setting)) {
1419 str = nm_setting_to_string (setting);
1420 g_print ("%s\n", str);
1426 * nm_connection_set_path:
1427 * @connection: the #NMConnection
1428 * @path: the D-Bus path of the connection as given by the settings service
1429 * which provides the connection
1431 * Sets the D-Bus path of the connection. This property is not serialized, and
1432 * is only for the reference of the caller. Sets the #NMConnection:path
1436 nm_connection_set_path (NMConnection *connection, const char *path)
1438 NMConnectionPrivate *priv;
1440 g_return_if_fail (NM_IS_CONNECTION (connection));
1442 priv = NM_CONNECTION_GET_PRIVATE (connection);
1444 g_free (priv->path);
1448 priv->path = g_strdup (path);
1452 * nm_connection_get_path:
1453 * @connection: the #NMConnection
1455 * Returns the connection's D-Bus path.
1457 * Returns: the D-Bus path of the connection, previously set by a call to
1458 * nm_connection_set_path().
1461 nm_connection_get_path (NMConnection *connection)
1463 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1465 return NM_CONNECTION_GET_PRIVATE (connection)->path;
1469 * nm_connection_get_interface_name:
1470 * @connection: The #NMConnection
1472 * Returns the interface name as stored in NMSettingConnection:interface_name.
1473 * If the connection contains no NMSettingConnection, it will return %NULL.
1475 * For hardware devices and software devices created outside of NetworkManager,
1476 * this name is used to match the device. for software devices created by
1477 * NetworkManager, this is the name of the created interface.
1479 * Returns: Name of the kernel interface or %NULL
1482 nm_connection_get_interface_name (NMConnection *connection)
1484 NMSettingConnection *s_con;
1486 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1488 s_con = nm_connection_get_setting_connection (connection);
1490 return s_con ? nm_setting_connection_get_interface_name (s_con) : NULL;
1494 _nm_connection_verify_required_interface_name (NMConnection *connection,
1497 const char *interface_name;
1499 interface_name = nm_connection_get_interface_name (connection);
1503 g_set_error_literal (error,
1504 NM_CONNECTION_ERROR,
1505 NM_CONNECTION_ERROR_MISSING_PROPERTY,
1506 _("property is missing"));
1507 g_prefix_error (error, "%s.%s: ", NM_SETTING_CONNECTION_SETTING_NAME, NM_SETTING_CONNECTION_INTERFACE_NAME);
1512 * nm_connection_get_uuid:
1513 * @connection: the #NMConnection
1515 * A shortcut to return the UUID from the connection's #NMSettingConnection.
1517 * Returns: the UUID from the connection's 'connection' setting
1520 nm_connection_get_uuid (NMConnection *connection)
1522 NMSettingConnection *s_con;
1524 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1526 s_con = nm_connection_get_setting_connection (connection);
1530 return nm_setting_connection_get_uuid (s_con);
1534 * nm_connection_get_id:
1535 * @connection: the #NMConnection
1537 * A shortcut to return the ID from the connection's #NMSettingConnection.
1539 * Returns: the ID from the connection's 'connection' setting
1542 nm_connection_get_id (NMConnection *connection)
1544 NMSettingConnection *s_con;
1546 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1548 s_con = nm_connection_get_setting_connection (connection);
1552 return nm_setting_connection_get_id (s_con);
1556 * nm_connection_get_connection_type:
1557 * @connection: the #NMConnection
1559 * A shortcut to return the type from the connection's #NMSettingConnection.
1561 * Returns: the type from the connection's 'connection' setting
1564 nm_connection_get_connection_type (NMConnection *connection)
1566 NMSettingConnection *s_con;
1568 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1570 s_con = nm_connection_get_setting_connection (connection);
1574 return nm_setting_connection_get_connection_type (s_con);
1578 * nm_connection_is_virtual:
1579 * @connection: an #NMConnection
1581 * Checks if @connection refers to a virtual device (and thus can potentially be
1582 * activated even if the device it refers to doesn't exist).
1584 * Returns: whether @connection refers to a virtual device
1587 nm_connection_is_virtual (NMConnection *connection)
1591 type = nm_connection_get_connection_type (connection);
1592 g_return_val_if_fail (type != NULL, FALSE);
1594 if ( !strcmp (type, NM_SETTING_BOND_SETTING_NAME)
1595 || !strcmp (type, NM_SETTING_TEAM_SETTING_NAME)
1596 || !strcmp (type, NM_SETTING_BRIDGE_SETTING_NAME)
1597 || !strcmp (type, NM_SETTING_VLAN_SETTING_NAME)
1598 || !strcmp (type, NM_SETTING_TUN_SETTING_NAME)
1599 || !strcmp (type, NM_SETTING_IP_TUNNEL_SETTING_NAME)
1600 || !strcmp (type, NM_SETTING_MACVLAN_SETTING_NAME)
1601 || !strcmp (type, NM_SETTING_VXLAN_SETTING_NAME))
1604 if (!strcmp (type, NM_SETTING_INFINIBAND_SETTING_NAME)) {
1605 NMSettingInfiniband *s_ib;
1607 s_ib = nm_connection_get_setting_infiniband (connection);
1608 g_return_val_if_fail (s_ib != NULL, FALSE);
1609 return nm_setting_infiniband_get_virtual_interface_name (s_ib) != NULL;
1616 * nm_connection_get_virtual_device_description:
1617 * @connection: an #NMConnection for a virtual device type
1619 * Returns the name that nm_device_disambiguate_names() would
1620 * return for the virtual device that would be created for @connection.
1621 * Eg, "VLAN (eth1.1)".
1623 * Returns: (transfer full): the name of @connection's device,
1624 * or %NULL if @connection is not a virtual connection type
1627 nm_connection_get_virtual_device_description (NMConnection *connection)
1630 const char *iface = NULL, *display_type = NULL;
1632 iface = nm_connection_get_interface_name (connection);
1634 type = nm_connection_get_connection_type (connection);
1635 g_return_val_if_fail (type != NULL, FALSE);
1637 if (!strcmp (type, NM_SETTING_BOND_SETTING_NAME))
1638 display_type = _("Bond");
1639 else if (!strcmp (type, NM_SETTING_TEAM_SETTING_NAME))
1640 display_type = _("Team");
1641 else if (!strcmp (type, NM_SETTING_BRIDGE_SETTING_NAME))
1642 display_type = _("Bridge");
1643 else if (!strcmp (type, NM_SETTING_VLAN_SETTING_NAME))
1644 display_type = _("VLAN");
1645 else if (!strcmp (type, NM_SETTING_INFINIBAND_SETTING_NAME)) {
1646 display_type = _("InfiniBand");
1647 iface = nm_setting_infiniband_get_virtual_interface_name (nm_connection_get_setting_infiniband (connection));
1650 if (!iface || !display_type)
1653 return g_strdup_printf ("%s (%s)", display_type, iface);
1656 /*************************************************************/
1659 * nm_connection_get_setting_802_1x:
1660 * @connection: the #NMConnection
1662 * A shortcut to return any #NMSetting8021x the connection might contain.
1664 * Returns: (transfer none): an #NMSetting8021x if the connection contains one, otherwise %NULL
1667 nm_connection_get_setting_802_1x (NMConnection *connection)
1669 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1671 return (NMSetting8021x *) nm_connection_get_setting (connection, NM_TYPE_SETTING_802_1X);
1675 * nm_connection_get_setting_bluetooth:
1676 * @connection: the #NMConnection
1678 * A shortcut to return any #NMSettingBluetooth the connection might contain.
1680 * Returns: (transfer none): an #NMSettingBluetooth if the connection contains one, otherwise %NULL
1682 NMSettingBluetooth *
1683 nm_connection_get_setting_bluetooth (NMConnection *connection)
1685 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1687 return (NMSettingBluetooth *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BLUETOOTH);
1691 * nm_connection_get_setting_bond:
1692 * @connection: the #NMConnection
1694 * A shortcut to return any #NMSettingBond the connection might contain.
1696 * Returns: (transfer none): an #NMSettingBond if the connection contains one, otherwise %NULL
1699 nm_connection_get_setting_bond (NMConnection *connection)
1701 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1703 return (NMSettingBond *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BOND);
1707 * nm_connection_get_setting_team:
1708 * @connection: the #NMConnection
1710 * A shortcut to return any #NMSettingTeam the connection might contain.
1712 * Returns: (transfer none): an #NMSettingTeam if the connection contains one, otherwise %NULL
1715 nm_connection_get_setting_team (NMConnection *connection)
1717 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1719 return (NMSettingTeam *) nm_connection_get_setting (connection, NM_TYPE_SETTING_TEAM);
1723 * nm_connection_get_setting_team_port:
1724 * @connection: the #NMConnection
1726 * A shortcut to return any #NMSettingTeamPort the connection might contain.
1728 * Returns: (transfer none): an #NMSettingTeamPort if the connection contains one, otherwise %NULL
1731 nm_connection_get_setting_team_port (NMConnection *connection)
1733 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1735 return (NMSettingTeamPort *) nm_connection_get_setting (connection, NM_TYPE_SETTING_TEAM_PORT);
1739 * nm_connection_get_setting_bridge:
1740 * @connection: the #NMConnection
1742 * A shortcut to return any #NMSettingBridge the connection might contain.
1744 * Returns: (transfer none): an #NMSettingBridge if the connection contains one, otherwise %NULL
1747 nm_connection_get_setting_bridge (NMConnection *connection)
1749 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1751 return (NMSettingBridge *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BRIDGE);
1755 * nm_connection_get_setting_cdma:
1756 * @connection: the #NMConnection
1758 * A shortcut to return any #NMSettingCdma the connection might contain.
1760 * Returns: (transfer none): an #NMSettingCdma if the connection contains one, otherwise %NULL
1763 nm_connection_get_setting_cdma (NMConnection *connection)
1765 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1767 return (NMSettingCdma *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CDMA);
1771 * nm_connection_get_setting_connection:
1772 * @connection: the #NMConnection
1774 * A shortcut to return any #NMSettingConnection the connection might contain.
1776 * Returns: (transfer none): an #NMSettingConnection if the connection contains one, otherwise %NULL
1778 NMSettingConnection *
1779 nm_connection_get_setting_connection (NMConnection *connection)
1781 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1783 return (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
1787 * nm_connection_get_setting_dcb:
1788 * @connection: the #NMConnection
1790 * A shortcut to return any #NMSettingDcb the connection might contain.
1792 * Returns: (transfer none): an #NMSettingDcb if the connection contains one, otherwise NULL
1795 nm_connection_get_setting_dcb (NMConnection *connection)
1797 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1799 return (NMSettingDcb *) nm_connection_get_setting (connection, NM_TYPE_SETTING_DCB);
1803 * nm_connection_get_setting_generic:
1804 * @connection: the #NMConnection
1806 * A shortcut to return any #NMSettingGeneric the connection might contain.
1808 * Returns: (transfer none): an #NMSettingGeneric if the connection contains one, otherwise NULL
1811 nm_connection_get_setting_generic (NMConnection *connection)
1813 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1815 return (NMSettingGeneric *) nm_connection_get_setting (connection, NM_TYPE_SETTING_GENERIC);
1819 * nm_connection_get_setting_gsm:
1820 * @connection: the #NMConnection
1822 * A shortcut to return any #NMSettingGsm the connection might contain.
1824 * Returns: (transfer none): an #NMSettingGsm if the connection contains one, otherwise %NULL
1827 nm_connection_get_setting_gsm (NMConnection *connection)
1829 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1831 return (NMSettingGsm *) nm_connection_get_setting (connection, NM_TYPE_SETTING_GSM);
1835 * nm_connection_get_setting_infiniband:
1836 * @connection: the #NMConnection
1838 * A shortcut to return any #NMSettingInfiniband the connection might contain.
1840 * Returns: (transfer none): an #NMSettingInfiniband if the connection contains one, otherwise %NULL
1842 NMSettingInfiniband *
1843 nm_connection_get_setting_infiniband (NMConnection *connection)
1845 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1847 return (NMSettingInfiniband *) nm_connection_get_setting (connection, NM_TYPE_SETTING_INFINIBAND);
1851 * nm_connection_get_setting_ip4_config:
1852 * @connection: the #NMConnection
1854 * A shortcut to return any #NMSettingIP4Config the connection might contain.
1856 * Note that it returns the value as type #NMSettingIPConfig, since the vast
1857 * majority of IPv4-setting-related methods are on that type, not
1858 * #NMSettingIP4Config.
1860 * Returns: (type NMSettingIP4Config) (transfer none): an #NMSettingIP4Config if the
1861 * connection contains one, otherwise %NULL
1864 nm_connection_get_setting_ip4_config (NMConnection *connection)
1866 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1868 return (NMSettingIPConfig *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG);
1872 * nm_connection_get_setting_ip_tunnel:
1873 * @connection: the #NMConnection
1875 * A shortcut to return any #NMSettingIPTunnel the connection might contain.
1877 * Returns: (transfer none): an #NMSettingIPTunnel if the connection contains one, otherwise %NULL
1882 nm_connection_get_setting_ip_tunnel (NMConnection *connection)
1884 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1886 return (NMSettingIPTunnel *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP_TUNNEL);
1890 * nm_connection_get_setting_ip6_config:
1891 * @connection: the #NMConnection
1893 * A shortcut to return any #NMSettingIP6Config the connection might contain.
1895 * Note that it returns the value as type #NMSettingIPConfig, since the vast
1896 * majority of IPv6-setting-related methods are on that type, not
1897 * #NMSettingIP6Config.
1899 * Returns: (type NMSettingIP6Config) (transfer none): an #NMSettingIP6Config if the
1900 * connection contains one, otherwise %NULL
1903 nm_connection_get_setting_ip6_config (NMConnection *connection)
1905 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1907 return (NMSettingIPConfig *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP6_CONFIG);
1911 * nm_connection_get_setting_macvlan:
1912 * @connection: the #NMConnection
1914 * A shortcut to return any #NMSettingMacvlan the connection might contain.
1916 * Returns: (transfer none): an #NMSettingMacvlan if the connection contains one, otherwise %NULL
1921 nm_connection_get_setting_macvlan (NMConnection *connection)
1923 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1925 return (NMSettingMacvlan *) nm_connection_get_setting (connection, NM_TYPE_SETTING_MACVLAN);
1929 * nm_connection_get_setting_olpc_mesh:
1930 * @connection: the #NMConnection
1932 * A shortcut to return any #NMSettingOlpcMesh the connection might contain.
1934 * Returns: (transfer none): an #NMSettingOlpcMesh if the connection contains one, otherwise %NULL
1937 nm_connection_get_setting_olpc_mesh (NMConnection *connection)
1939 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1941 return (NMSettingOlpcMesh *) nm_connection_get_setting (connection, NM_TYPE_SETTING_OLPC_MESH);
1945 * nm_connection_get_setting_ppp:
1946 * @connection: the #NMConnection
1948 * A shortcut to return any #NMSettingPpp the connection might contain.
1950 * Returns: (transfer none): an #NMSettingPpp if the connection contains one, otherwise %NULL
1953 nm_connection_get_setting_ppp (NMConnection *connection)
1955 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1957 return (NMSettingPpp *) nm_connection_get_setting (connection, NM_TYPE_SETTING_PPP);
1961 * nm_connection_get_setting_pppoe:
1962 * @connection: the #NMConnection
1964 * A shortcut to return any #NMSettingPppoe the connection might contain.
1966 * Returns: (transfer none): an #NMSettingPppoe if the connection contains one, otherwise %NULL
1969 nm_connection_get_setting_pppoe (NMConnection *connection)
1971 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1973 return (NMSettingPppoe *) nm_connection_get_setting (connection, NM_TYPE_SETTING_PPPOE);
1977 * nm_connection_get_setting_serial:
1978 * @connection: the #NMConnection
1980 * A shortcut to return any #NMSettingSerial the connection might contain.
1982 * Returns: (transfer none): an #NMSettingSerial if the connection contains one, otherwise %NULL
1985 nm_connection_get_setting_serial (NMConnection *connection)
1987 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1989 return (NMSettingSerial *) nm_connection_get_setting (connection, NM_TYPE_SETTING_SERIAL);
1993 * nm_connection_get_setting_tun:
1994 * @connection: the #NMConnection
1996 * A shortcut to return any #NMSettingTun the connection might contain.
1998 * Returns: (transfer none): an #NMSettingTun if the connection contains one, otherwise %NULL
2003 nm_connection_get_setting_tun (NMConnection *connection)
2005 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2007 return (NMSettingTun *) nm_connection_get_setting (connection, NM_TYPE_SETTING_TUN);
2011 * nm_connection_get_setting_vpn:
2012 * @connection: the #NMConnection
2014 * A shortcut to return any #NMSettingVpn the connection might contain.
2016 * Returns: (transfer none): an #NMSettingVpn if the connection contains one, otherwise %NULL
2019 nm_connection_get_setting_vpn (NMConnection *connection)
2021 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2023 return (NMSettingVpn *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VPN);
2027 * nm_connection_get_setting_vxlan:
2028 * @connection: the #NMConnection
2030 * A shortcut to return any #NMSettingVxlan the connection might contain.
2032 * Returns: (transfer none): an #NMSettingVxlan if the connection contains one, otherwise %NULL
2037 nm_connection_get_setting_vxlan (NMConnection *connection)
2039 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2041 return (NMSettingVxlan *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VXLAN);
2045 * nm_connection_get_setting_wimax:
2046 * @connection: the #NMConnection
2048 * A shortcut to return any #NMSettingWimax the connection might contain.
2050 * Returns: (transfer none): an #NMSettingWimax if the connection contains one, otherwise %NULL
2053 nm_connection_get_setting_wimax (NMConnection *connection)
2055 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2057 return (NMSettingWimax *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIMAX);
2061 * nm_connection_get_setting_wired:
2062 * @connection: the #NMConnection
2064 * A shortcut to return any #NMSettingWired the connection might contain.
2066 * Returns: (transfer none): an #NMSettingWired if the connection contains one, otherwise %NULL
2069 nm_connection_get_setting_wired (NMConnection *connection)
2071 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2073 return (NMSettingWired *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRED);
2077 * nm_connection_get_setting_adsl:
2078 * @connection: the #NMConnection
2080 * A shortcut to return any #NMSettingAdsl the connection might contain.
2082 * Returns: (transfer none): an #NMSettingAdsl if the connection contains one, otherwise %NULL
2085 nm_connection_get_setting_adsl (NMConnection *connection)
2087 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2089 return (NMSettingAdsl *) nm_connection_get_setting (connection, NM_TYPE_SETTING_ADSL);
2093 * nm_connection_get_setting_wireless:
2094 * @connection: the #NMConnection
2096 * A shortcut to return any #NMSettingWireless the connection might contain.
2098 * Returns: (transfer none): an #NMSettingWireless if the connection contains one, otherwise %NULL
2101 nm_connection_get_setting_wireless (NMConnection *connection)
2103 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2105 return (NMSettingWireless *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS);
2109 * nm_connection_get_setting_wireless_security:
2110 * @connection: the #NMConnection
2112 * A shortcut to return any #NMSettingWirelessSecurity the connection might contain.
2114 * Returns: (transfer none): an #NMSettingWirelessSecurity if the connection contains one, otherwise %NULL
2116 NMSettingWirelessSecurity *
2117 nm_connection_get_setting_wireless_security (NMConnection *connection)
2119 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2121 return (NMSettingWirelessSecurity *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS_SECURITY);
2125 * nm_connection_get_setting_bridge_port:
2126 * @connection: the #NMConnection
2128 * A shortcut to return any #NMSettingBridgePort the connection might contain.
2130 * Returns: (transfer none): an #NMSettingBridgePort if the connection contains one, otherwise %NULL
2132 NMSettingBridgePort *
2133 nm_connection_get_setting_bridge_port (NMConnection *connection)
2135 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2137 return (NMSettingBridgePort *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BRIDGE_PORT);
2141 * nm_connection_get_setting_vlan:
2142 * @connection: the #NMConnection
2144 * A shortcut to return any #NMSettingVlan the connection might contain.
2146 * Returns: (transfer none): an #NMSettingVlan if the connection contains one, otherwise %NULL
2149 nm_connection_get_setting_vlan (NMConnection *connection)
2151 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2153 return (NMSettingVlan *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VLAN);
2156 /*************************************************************/
2159 nm_connection_private_free (NMConnectionPrivate *priv)
2161 NMConnection *self = priv->self;
2163 g_hash_table_foreach_remove (priv->settings, _setting_release, self);
2164 g_hash_table_destroy (priv->settings);
2165 g_free (priv->path);
2167 g_slice_free (NMConnectionPrivate, priv);
2170 static NMConnectionPrivate *
2171 nm_connection_get_private (NMConnection *connection)
2173 NMConnectionPrivate *priv;
2175 priv = g_object_get_data (G_OBJECT (connection), "NMConnectionPrivate");
2177 priv = g_slice_new0 (NMConnectionPrivate);
2178 g_object_set_data_full (G_OBJECT (connection), "NMConnectionPrivate",
2179 priv, (GDestroyNotify) nm_connection_private_free);
2181 priv->self = connection;
2182 priv->settings = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_object_unref);
2189 nm_connection_default_init (NMConnectionInterface *iface)
2194 * NMConnection::secrets-updated:
2195 * @connection: the object on which the signal is emitted
2196 * @setting_name: the setting name of the #NMSetting for which secrets were
2199 * The ::secrets-updated signal is emitted when the secrets of a setting
2200 * have been changed.
2202 signals[SECRETS_UPDATED] =
2203 g_signal_new (NM_CONNECTION_SECRETS_UPDATED,
2206 G_STRUCT_OFFSET (NMConnectionInterface, secrets_updated),
2208 g_cclosure_marshal_VOID__STRING,
2213 * NMConnection::secrets-cleared:
2214 * @connection: the object on which the signal is emitted
2216 * The ::secrets-cleared signal is emitted when the secrets of a connection
2219 signals[SECRETS_CLEARED] =
2220 g_signal_new (NM_CONNECTION_SECRETS_CLEARED,
2223 G_STRUCT_OFFSET (NMConnectionInterface, secrets_cleared),
2225 g_cclosure_marshal_VOID__VOID,
2229 * NMConnection::changed:
2230 * @connection: the object on which the signal is emitted
2232 * The ::changed signal is emitted when any property of any property
2233 * (including secrets) of any setting of the connection is modified,
2234 * or when settings are added or removed.
2237 g_signal_new (NM_CONNECTION_CHANGED,
2240 G_STRUCT_OFFSET (NMConnectionInterface, changed),
2242 g_cclosure_marshal_VOID__VOID,