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"
25 #include <dbus/dbus-glib.h>
28 #include "nm-connection.h"
30 #include "nm-dbus-glib-types.h"
31 #include "nm-setting-private.h"
33 #include "nm-setting-8021x.h"
34 #include "nm-setting-bluetooth.h"
35 #include "nm-setting-connection.h"
36 #include "nm-setting-infiniband.h"
37 #include "nm-setting-ip4-config.h"
38 #include "nm-setting-ip6-config.h"
39 #include "nm-setting-ppp.h"
40 #include "nm-setting-pppoe.h"
41 #include "nm-setting-wimax.h"
42 #include "nm-setting-wired.h"
43 #include "nm-setting-adsl.h"
44 #include "nm-setting-wireless.h"
45 #include "nm-setting-wireless-security.h"
46 #include "nm-setting-serial.h"
47 #include "nm-setting-vpn.h"
48 #include "nm-setting-olpc-mesh.h"
49 #include "nm-setting-bond.h"
50 #include "nm-setting-team.h"
51 #include "nm-setting-team-port.h"
52 #include "nm-setting-bridge.h"
53 #include "nm-setting-bridge-port.h"
54 #include "nm-setting-vlan.h"
55 #include "nm-setting-serial.h"
56 #include "nm-setting-gsm.h"
57 #include "nm-setting-cdma.h"
60 * SECTION:nm-connection
61 * @short_description: Describes a connection to specific network or provider
62 * @include: nm-connection.h
64 * An #NMConnection describes all the settings and configuration values that
65 * are necessary to configure network devices for operation on a specific
66 * network. Connections are the fundamental operating object for
67 * NetworkManager; no device is connected without a #NMConnection, or
68 * disconnected without having been connected with a #NMConnection.
70 * Each #NMConnection contains a list of #NMSetting objects usually referenced
71 * by name (using nm_connection_get_setting_by_name()) or by type (with
72 * nm_connection_get_setting()). The settings describe the actual parameters
73 * with which the network devices are configured, including device-specific
74 * parameters (MTU, SSID, APN, channel, rate, etc) and IP-level parameters
75 * (addresses, routes, addressing methods, etc).
80 * nm_connection_error_quark:
82 * Registers an error quark for #NMConnection if necessary.
84 * Returns: the error quark used for #NMConnection errors.
87 nm_connection_error_quark (void)
91 if (G_UNLIKELY (!quark))
92 quark = g_quark_from_static_string ("nm-connection-error-quark");
99 /* D-Bus path of the connection, if any */
101 } NMConnectionPrivate;
103 #define NM_CONNECTION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_CONNECTION, NMConnectionPrivate))
105 G_DEFINE_TYPE (NMConnection, nm_connection, G_TYPE_OBJECT)
121 static guint signals[LAST_SIGNAL] = { 0 };
124 static NMSettingVerifyResult _nm_connection_verify (NMConnection *connection, GError **error);
127 /*************************************************************/
130 * nm_connection_lookup_setting_type:
131 * @name: a setting name
133 * Returns the #GType of the setting's class for a given setting name.
135 * Returns: the #GType of the setting's class
138 nm_connection_lookup_setting_type (const char *name)
140 return _nm_setting_lookup_setting_type (name);
144 * nm_connection_lookup_setting_type_by_quark:
145 * @error_quark: a setting error quark
147 * Returns the #GType of the setting's class for a given setting error quark.
148 * Useful for figuring out which setting a returned error is for.
150 * Returns: the #GType of the setting's class
153 nm_connection_lookup_setting_type_by_quark (GQuark error_quark)
155 return _nm_setting_lookup_setting_type_by_quark (error_quark);
159 * nm_connection_create_setting:
160 * @name: a setting name
162 * Create a new #NMSetting object of the desired type, given a setting name.
164 * Returns: (transfer full): the new setting object, or %NULL if the setting name was unknown
167 nm_connection_create_setting (const char *name)
170 NMSetting *setting = NULL;
172 g_return_val_if_fail (name != NULL, NULL);
174 type = nm_connection_lookup_setting_type (name);
176 setting = (NMSetting *) g_object_new (type, NULL);
182 setting_changed_cb (NMSetting *setting,
186 g_signal_emit (self, signals[CHANGED], 0);
190 _setting_release (gpointer key, gpointer value, gpointer user_data)
192 g_signal_handlers_disconnect_by_func (user_data, setting_changed_cb, value);
197 _nm_connection_add_setting (NMConnection *connection, NMSetting *setting)
199 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (connection);
200 const char *name = G_OBJECT_TYPE_NAME (setting);
203 if ((s_old = g_hash_table_lookup (priv->settings, (gpointer) name)))
204 g_signal_handlers_disconnect_by_func (s_old, setting_changed_cb, connection);
205 g_hash_table_insert (priv->settings, (gpointer) name, setting);
206 /* Listen for property changes so we can emit the 'changed' signal */
207 g_signal_connect (setting, "notify", (GCallback) setting_changed_cb, connection);
211 * nm_connection_add_setting:
212 * @connection: a #NMConnection
213 * @setting: (transfer full): the #NMSetting to add to the connection object
215 * Adds a #NMSetting to the connection, replacing any previous #NMSetting of the
216 * same name which has previously been added to the #NMConnection. The
217 * connection takes ownership of the #NMSetting object and does not increase
218 * the setting object's reference count.
221 nm_connection_add_setting (NMConnection *connection, NMSetting *setting)
223 g_return_if_fail (NM_IS_CONNECTION (connection));
224 g_return_if_fail (NM_IS_SETTING (setting));
226 _nm_connection_add_setting (connection, setting);
227 g_signal_emit (connection, signals[CHANGED], 0);
231 * nm_connection_remove_setting:
232 * @connection: a #NMConnection
233 * @setting_type: the #GType of the setting object to remove
235 * Removes the #NMSetting with the given #GType from the #NMConnection. This
236 * operation dereferences the #NMSetting object.
239 nm_connection_remove_setting (NMConnection *connection, GType setting_type)
241 NMConnectionPrivate *priv;
243 const char *setting_name;
245 g_return_if_fail (NM_IS_CONNECTION (connection));
246 g_return_if_fail (g_type_is_a (setting_type, NM_TYPE_SETTING));
248 priv = NM_CONNECTION_GET_PRIVATE (connection);
249 setting_name = g_type_name (setting_type);
250 setting = g_hash_table_lookup (priv->settings, setting_name);
252 g_signal_handlers_disconnect_by_func (setting, setting_changed_cb, connection);
253 g_hash_table_remove (priv->settings, setting_name);
254 g_signal_emit (connection, signals[CHANGED], 0);
259 * nm_connection_get_setting:
260 * @connection: a #NMConnection
261 * @setting_type: the #GType of the setting object to return
263 * Gets the #NMSetting with the given #GType, if one has been previously added
264 * to the #NMConnection.
266 * Returns: (transfer none): the #NMSetting, or %NULL if no setting of that type was previously
267 * added to the #NMConnection
270 nm_connection_get_setting (NMConnection *connection, GType setting_type)
272 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
273 g_return_val_if_fail (g_type_is_a (setting_type, NM_TYPE_SETTING), NULL);
275 return (NMSetting *) g_hash_table_lookup (NM_CONNECTION_GET_PRIVATE (connection)->settings,
276 g_type_name (setting_type));
280 * nm_connection_get_setting_by_name:
281 * @connection: a #NMConnection
282 * @name: a setting name
284 * Gets the #NMSetting with the given name, if one has been previously added
287 * Returns: (transfer none): the #NMSetting, or %NULL if no setting with that name was previously
288 * added to the #NMConnection
291 nm_connection_get_setting_by_name (NMConnection *connection, const char *name)
295 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
296 g_return_val_if_fail (name != NULL, NULL);
298 type = nm_connection_lookup_setting_type (name);
300 return type ? nm_connection_get_setting (connection, type) : NULL;
304 validate_permissions_type (GHashTable *hash, GError **error)
309 /* Ensure the connection::permissions item (if present) is the correct
310 * type, otherwise the g_object_set() will throw a warning and ignore the
311 * error, leaving us with no permissions.
313 s_con = g_hash_table_lookup (hash, NM_SETTING_CONNECTION_SETTING_NAME);
315 permissions = g_hash_table_lookup (s_con, NM_SETTING_CONNECTION_PERMISSIONS);
317 if ( !G_VALUE_HOLDS (permissions, G_TYPE_STRV)
318 && !G_VALUE_HOLDS (permissions, DBUS_TYPE_G_LIST_OF_STRING)) {
319 g_set_error_literal (error,
321 NM_SETTING_ERROR_PROPERTY_TYPE_MISMATCH,
322 "Wrong permissions property type; should be a list of strings.");
331 * _nm_connection_replace_settings:
332 * @connection: a #NMConnection
333 * @new_settings: (element-type utf8 GLib.HashTable): a #GHashTable of settings
336 _nm_connection_replace_settings (NMConnection *connection,
337 GHashTable *new_settings)
339 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (connection);
341 const char *setting_name;
342 GHashTable *setting_hash;
345 g_return_if_fail (NM_IS_CONNECTION (connection));
346 g_return_if_fail (new_settings != NULL);
348 priv = NM_CONNECTION_GET_PRIVATE (connection);
350 if ((changed = g_hash_table_size (priv->settings) > 0))
351 g_hash_table_foreach_remove (priv->settings, _setting_release, connection);
353 g_hash_table_iter_init (&iter, new_settings);
354 while (g_hash_table_iter_next (&iter, (gpointer) &setting_name, (gpointer) &setting_hash)) {
355 GType type = nm_connection_lookup_setting_type (setting_name);
358 NMSetting *setting = nm_setting_new_from_hash (type, setting_hash);
361 _nm_connection_add_setting (connection, setting);
368 g_signal_emit (connection, signals[CHANGED], 0);
372 * nm_connection_replace_settings:
373 * @connection: a #NMConnection
374 * @new_settings: (element-type utf8 GLib.HashTable): a #GHashTable of settings
375 * @error: location to store error, or %NULL
377 * Returns: %TRUE if the settings were valid and added to the connection, %FALSE
381 nm_connection_replace_settings (NMConnection *connection,
382 GHashTable *new_settings,
385 g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
386 g_return_val_if_fail (new_settings != NULL, FALSE);
387 g_return_val_if_fail (!error || !*error, FALSE);
389 if (!validate_permissions_type (new_settings, error))
392 _nm_connection_replace_settings (connection, new_settings);
393 return nm_connection_verify (connection, error);
397 * nm_connection_replace_settings_from_connection:
398 * @connection: a #NMConnection
399 * @new_connection: a #NMConnection to replace the settings of @connection with
400 * @error: location to store error, or %NULL
402 * Deep-copies the settings of @new_conenction and replaces the settings of @connection
403 * with the copied settings.
405 * Returns: %TRUE if the settings were valid after replacing the connection, %FALSE
406 * if they were not. Regardless of whether %TRUE or %FALSE is returned, the connection
407 * is successfully replaced. %FALSE only means, that the connection does not verify
408 * at the end of the operation.
413 nm_connection_replace_settings_from_connection (NMConnection *connection,
414 NMConnection *new_connection,
417 NMConnectionPrivate *priv;
420 gboolean changed = FALSE;
423 g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
424 g_return_val_if_fail (NM_IS_CONNECTION (new_connection), FALSE);
425 g_return_val_if_fail (!error || !*error, FALSE);
427 /* When 'connection' and 'new_connection' are the same object simply return
428 * in order not to destroy 'connection' */
429 if (connection == new_connection)
432 /* No need to validate permissions like nm_connection_replace_settings()
433 * since we're dealing with an NMConnection which has already done that.
436 priv = NM_CONNECTION_GET_PRIVATE (connection);
437 if ((changed = g_hash_table_size (priv->settings) > 0))
438 g_hash_table_foreach_remove (priv->settings, _setting_release, connection);
440 if (g_hash_table_size (NM_CONNECTION_GET_PRIVATE (new_connection)->settings)) {
441 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (new_connection)->settings);
442 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting))
443 _nm_connection_add_setting (connection, nm_setting_duplicate (setting));
448 valid = nm_connection_verify (connection, error);
450 g_signal_emit (connection, signals[CHANGED], 0);
455 * nm_connection_compare:
456 * @a: a #NMConnection
457 * @b: a second #NMConnection to compare with the first
458 * @flags: compare flags, e.g. %NM_SETTING_COMPARE_FLAG_EXACT
460 * Compares two #NMConnection objects for similarity, with comparison behavior
461 * modified by a set of flags. See nm_setting_compare() for a description of
462 * each flag's behavior.
464 * Returns: %TRUE if the comparison succeeds, %FALSE if it does not
467 nm_connection_compare (NMConnection *a,
469 NMSettingCompareFlags flags)
479 /* B / A: ensure settings in B that are not in A make the comparison fail */
480 if (g_hash_table_size (NM_CONNECTION_GET_PRIVATE (a)->settings) !=
481 g_hash_table_size (NM_CONNECTION_GET_PRIVATE (b)->settings))
484 /* A / B: ensure all settings in A match corresponding ones in B */
485 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (a)->settings);
486 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &src)) {
487 NMSetting *cmp = nm_connection_get_setting (b, G_OBJECT_TYPE (src));
489 if (!cmp || !nm_setting_compare (src, cmp, flags))
498 diff_one_connection (NMConnection *a,
500 NMSettingCompareFlags flags,
501 gboolean invert_results,
504 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (a);
506 NMSetting *a_setting = NULL;
508 g_hash_table_iter_init (&iter, priv->settings);
509 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &a_setting)) {
510 NMSetting *b_setting = NULL;
511 const char *setting_name = nm_setting_get_name (a_setting);
513 gboolean new_results = TRUE;
516 b_setting = nm_connection_get_setting (b, G_OBJECT_TYPE (a_setting));
518 results = g_hash_table_lookup (diffs, setting_name);
522 if (!nm_setting_diff (a_setting, b_setting, flags, invert_results, &results)) {
524 g_hash_table_insert (diffs, g_strdup (setting_name), results);
530 * nm_connection_diff:
531 * @a: a #NMConnection
532 * @b: a second #NMConnection to compare with the first
533 * @flags: compare flags, e.g. %NM_SETTING_COMPARE_FLAG_EXACT
534 * @out_settings: (element-type utf8 GLib.HashTable): if the
535 * connections differ, on return a hash table mapping setting names to
536 * second-level GHashTable (utf8 to guint32), which contains the key names that
537 * differ mapped to one or more of %NMSettingDiffResult as a bitfield
539 * Compares two #NMConnection objects for similarity, with comparison behavior
540 * modified by a set of flags. See nm_setting_compare() for a description of
541 * each flag's behavior. If the connections differ, settings and keys within
542 * each setting that differ are added to the returned @out_settings hash table.
543 * No values are returned, only key names.
545 * Returns: %TRUE if the connections contain the same values, %FALSE if they do
549 nm_connection_diff (NMConnection *a,
551 NMSettingCompareFlags flags,
552 GHashTable **out_settings)
556 g_return_val_if_fail (NM_IS_CONNECTION (a), FALSE);
557 g_return_val_if_fail (out_settings != NULL, FALSE);
558 g_return_val_if_fail (*out_settings == NULL, FALSE);
560 g_return_val_if_fail (NM_IS_CONNECTION (b), FALSE);
565 diffs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_destroy);
567 /* Diff A to B, then B to A to capture keys in B that aren't in A */
568 diff_one_connection (a, b, flags, FALSE, diffs);
570 diff_one_connection (b, a, flags, TRUE, diffs);
572 if (g_hash_table_size (diffs) == 0)
573 g_hash_table_destroy (diffs);
575 *out_settings = diffs;
577 return *out_settings ? FALSE : TRUE;
581 _normalize_virtual_iface_name (NMConnection *self)
583 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (self);
584 GHashTableIter h_iter;
586 NMSettingConnection *s_con;
587 const char *interface_name;
588 char *virtual_iface_name = NULL;
589 gboolean was_modified = FALSE;
590 const char *prop_name = NULL;
592 /* search for settings that might need normalization of the interface name. */
593 g_hash_table_iter_init (&h_iter, priv->settings);
595 && g_hash_table_iter_next (&h_iter, NULL, (void **) &setting)) {
596 if (NM_IS_SETTING_BOND (setting))
597 prop_name = NM_SETTING_BOND_INTERFACE_NAME;
598 else if (NM_IS_SETTING_BRIDGE (setting))
599 prop_name = NM_SETTING_BRIDGE_INTERFACE_NAME;
600 else if (NM_IS_SETTING_TEAM (setting))
601 prop_name = NM_SETTING_TEAM_INTERFACE_NAME;
602 else if (NM_IS_SETTING_VLAN (setting))
603 prop_name = NM_SETTING_VLAN_INTERFACE_NAME;
608 s_con = nm_connection_get_setting_connection (self);
609 g_return_val_if_fail (s_con, FALSE);
611 interface_name = nm_setting_connection_get_interface_name (s_con);
613 /* read the potential virtual_iface_name from the setting. */
614 g_object_get (setting, prop_name, &virtual_iface_name, NULL);
616 if (g_strcmp0 (interface_name, virtual_iface_name) != 0) {
617 if (interface_name) {
618 /* interface_name is set and overwrites the virtual_iface_name. */
619 g_object_set (setting, prop_name, interface_name, NULL);
621 /* interface in NMSettingConnection must be set. */
622 g_object_set (s_con, NM_SETTING_CONNECTION_INTERFACE_NAME, virtual_iface_name, NULL);
627 g_free (virtual_iface_name);
633 _normalize_ip_config (NMConnection *self, GHashTable *parameters)
635 NMSettingConnection *s_con = nm_connection_get_setting_connection (self);
636 const char *default_ip4_method = NM_SETTING_IP4_CONFIG_METHOD_AUTO;
637 const char *default_ip6_method = NULL;
638 NMSettingIP4Config *s_ip4;
639 NMSettingIP6Config *s_ip6;
643 default_ip6_method = g_hash_table_lookup (parameters, NM_CONNECTION_NORMALIZE_PARAM_IP6_CONFIG_METHOD);
644 if (!default_ip6_method)
645 default_ip6_method = NM_SETTING_IP6_CONFIG_METHOD_AUTO;
647 s_ip4 = nm_connection_get_setting_ip4_config (self);
648 s_ip6 = nm_connection_get_setting_ip6_config (self);
650 if (nm_setting_connection_get_master (s_con)) {
651 /* Slave connections don't have IP configuration. */
654 nm_connection_remove_setting (self, NM_TYPE_SETTING_IP4_CONFIG);
657 nm_connection_remove_setting (self, NM_TYPE_SETTING_IP6_CONFIG);
659 return s_ip4 || s_ip6;
661 /* Ensure all non-slave connections have IP4 and IP6 settings objects. If no
662 * IP6 setting was specified, then assume that means IP6 config is allowed
663 * to fail. But if no IP4 setting was specified, assume the caller was just
667 setting = nm_setting_ip4_config_new ();
669 g_object_set (setting,
670 NM_SETTING_IP4_CONFIG_METHOD, default_ip4_method,
672 nm_connection_add_setting (self, setting);
675 setting = nm_setting_ip6_config_new ();
677 g_object_set (setting,
678 NM_SETTING_IP6_CONFIG_METHOD, default_ip6_method,
679 NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE,
681 nm_connection_add_setting (self, setting);
683 return !s_ip4 || !s_ip6;
688 * nm_connection_verify:
689 * @connection: the #NMConnection to verify
690 * @error: location to store error, or %NULL
692 * Validates the connection and all its settings. Each setting's properties
693 * have allowed values, and some values are dependent on other values. For
694 * example, if a Wi-Fi connection is security enabled, the #NMSettingWireless
695 * setting object's 'security' property must contain the setting name of the
696 * #NMSettingWirelessSecurity object, which must also be present in the
697 * connection for the connection to be valid. As another example, the
698 * #NMSettingWired object's 'mac-address' property must be a validly formatted
699 * MAC address. The returned #GError contains information about which
700 * setting and which property failed validation, and how it failed validation.
702 * Returns: %TRUE if the connection is valid, %FALSE if it is not
705 nm_connection_verify (NMConnection *connection, GError **error)
707 NMSettingVerifyResult result;
709 result = _nm_connection_verify (connection, error);
711 /* we treat normalizable connections as valid. */
712 if (result == NM_SETTING_VERIFY_NORMALIZABLE)
713 g_clear_error (error);
715 return result == NM_SETTING_VERIFY_SUCCESS || result == NM_SETTING_VERIFY_NORMALIZABLE;
718 static NMSettingVerifyResult
719 _nm_connection_verify (NMConnection *connection, GError **error)
721 NMConnectionPrivate *priv;
722 NMSettingConnection *s_con;
723 NMSettingIP4Config *s_ip4;
724 NMSettingIP6Config *s_ip6;
727 GSList *all_settings = NULL, *setting_i;
728 NMSettingVerifyResult success = NM_SETTING_VERIFY_ERROR;
731 GError *normalizable_error = NULL;
732 NMSettingVerifyResult normalizable_error_type = NM_SETTING_VERIFY_SUCCESS;
735 g_return_val_if_fail (*error == NULL, NM_SETTING_VERIFY_ERROR);
737 if (!NM_IS_CONNECTION (connection)) {
738 g_set_error_literal (error,
739 NM_SETTING_CONNECTION_ERROR,
740 NM_SETTING_CONNECTION_ERROR_UNKNOWN,
741 "invalid connection; failed verification");
742 g_return_val_if_fail (NM_IS_CONNECTION (connection), NM_SETTING_VERIFY_ERROR);
745 priv = NM_CONNECTION_GET_PRIVATE (connection);
747 /* First, make sure there's at least 'connection' setting */
748 s_con = nm_connection_get_setting_connection (connection);
750 g_set_error_literal (error,
752 NM_CONNECTION_ERROR_CONNECTION_SETTING_NOT_FOUND,
753 "connection setting not found");
757 /* Build up the list of settings */
758 g_hash_table_iter_init (&iter, priv->settings);
759 while (g_hash_table_iter_next (&iter, NULL, &value)) {
760 /* Order NMSettingConnection so that it will be verified first.
761 * The reason is, that NMSettingConnection:verify() modifies the connection
762 * by setting NMSettingConnection:interface_name. So we want to call that
763 * verify() first, because the order can affect the outcome.
764 * Another reason is, that errors in this setting might be more fundamental
765 * and should be checked and reported with higher priority.
766 * Another reason is, that some settings look especially at the
767 * NMSettingConnection, so they find it first in the all_settings list. */
769 all_settings = g_slist_append (all_settings, value);
771 all_settings = g_slist_prepend (all_settings, value);
773 all_settings = g_slist_reverse (all_settings);
775 /* Now, run the verify function of each setting */
776 for (setting_i = all_settings; setting_i; setting_i = setting_i->next) {
777 GError *verify_error = NULL;
778 NMSettingVerifyResult verify_result;
780 /* verify all settings. We stop if we find the first non-normalizable
781 * @NM_SETTING_VERIFY_ERROR. If we find normalizable errors we continue
782 * but remember the error to return it to the user.
783 * @NM_SETTING_VERIFY_NORMALIZABLE_ERROR has a higher priority then
784 * @NM_SETTING_VERIFY_NORMALIZABLE, so, if we encounter such an error type,
785 * we remember it instead (to return it as output).
787 verify_result = _nm_setting_verify (NM_SETTING (setting_i->data), all_settings, &verify_error);
788 if (verify_result == NM_SETTING_VERIFY_NORMALIZABLE ||
789 verify_result == NM_SETTING_VERIFY_NORMALIZABLE_ERROR) {
790 if ( verify_result == NM_SETTING_VERIFY_NORMALIZABLE_ERROR
791 && normalizable_error_type == NM_SETTING_VERIFY_NORMALIZABLE) {
792 /* NORMALIZABLE_ERROR has higher priority. */
793 g_clear_error (&normalizable_error);
795 if (!normalizable_error) {
796 g_propagate_error (&normalizable_error, verify_error);
798 normalizable_error_type = verify_result;
800 } else if (verify_result != NM_SETTING_VERIFY_SUCCESS) {
801 g_propagate_error (error, verify_error);
802 g_slist_free (all_settings);
803 g_return_val_if_fail (verify_result == NM_SETTING_VERIFY_ERROR, success);
806 g_clear_error (&verify_error);
808 g_slist_free (all_settings);
810 /* Now make sure the given 'type' setting can actually be the base setting
811 * of the connection. Can't have type=ppp for example.
813 ctype = nm_setting_connection_get_connection_type (s_con);
815 g_set_error_literal (error,
817 NM_CONNECTION_ERROR_CONNECTION_TYPE_INVALID,
818 "connection type missing");
822 base = nm_connection_get_setting_by_name (connection, ctype);
824 g_set_error_literal (error,
826 NM_CONNECTION_ERROR_CONNECTION_TYPE_INVALID,
827 "base setting GType not found");
831 if (!_nm_setting_is_base_type (base)) {
834 NM_CONNECTION_ERROR_CONNECTION_TYPE_INVALID,
835 "connection type '%s' is not a base type",
840 s_ip4 = nm_connection_get_setting_ip4_config (connection);
841 s_ip6 = nm_connection_get_setting_ip6_config (connection);
843 if (nm_setting_connection_get_master (s_con)) {
844 if ((normalizable_error_type == NM_SETTING_VERIFY_SUCCESS ||
845 (normalizable_error_type == NM_SETTING_VERIFY_NORMALIZABLE)) && (s_ip4 || s_ip6)) {
846 g_clear_error (&normalizable_error);
847 g_set_error (&normalizable_error,
849 NM_CONNECTION_ERROR_INVALID_SETTING,
850 "slave connection cannot have an IP%c setting",
852 /* having a slave with IP config *was* and is a verify() error. */
853 normalizable_error_type = NM_SETTING_VERIFY_NORMALIZABLE_ERROR;
856 if (normalizable_error_type == NM_SETTING_VERIFY_SUCCESS && (!s_ip4 || !s_ip6)) {
857 g_set_error (&normalizable_error,
859 NM_CONNECTION_ERROR_SETTING_NOT_FOUND,
860 "connection needs an IP%c setting",
862 /* having a master without IP config was not a verify() error, accept
863 * it for backward compatibility. */
864 normalizable_error_type = NM_SETTING_VERIFY_NORMALIZABLE;
868 if (normalizable_error_type != NM_SETTING_VERIFY_SUCCESS) {
869 g_propagate_error (error, normalizable_error);
870 normalizable_error = NULL;
871 success = normalizable_error_type;
873 success = NM_SETTING_VERIFY_SUCCESS;
876 g_clear_error (&normalizable_error);
881 * nm_connection_normalize:
882 * @connection: the #NMConnection to normalize
883 * @parameters: (allow-none) (element-type utf8 gpointer): a #GHashTable with
884 * normalization parameters to allow customization of the normalization by providing
885 * specific arguments. Unknown arguments will be ignored and the default will be
886 * used. The keys must be strings, hashed by g_str_hash() and g_str_equal() functions.
887 * The values are opaque and depend on the parameter name.
888 * @modified: (out) (allow-none): outputs whether any settings were modified.
889 * @error: location to store error, or %NULL. Contains the reason,
890 * why the connection is invalid, if the function returns an error.
892 * Does some basic normalization and fixup of well known inconsistencies
893 * and deprecated fields. If the connection was modified in any way,
894 * the output parameter @modified is set %TRUE.
896 * Finally the connection will be verified and %TRUE returns if the connection
897 * is valid. As this function only performs some specific normalization steps
898 * it cannot repair all connections. If the connection has errors that
899 * cannot be normalized, the connection will not be modified.
901 * Returns: %TRUE if the connection is valid, %FALSE if it is not
906 nm_connection_normalize (NMConnection *connection,
907 GHashTable *parameters,
911 NMSettingVerifyResult success;
912 gboolean was_modified = FALSE;
913 GError *normalizable_error = NULL;
915 success = _nm_connection_verify (connection, &normalizable_error);
917 if (success == NM_SETTING_VERIFY_ERROR ||
918 success == NM_SETTING_VERIFY_SUCCESS) {
919 if (normalizable_error)
920 g_propagate_error (error, normalizable_error);
923 g_assert (success == NM_SETTING_VERIFY_NORMALIZABLE || success == NM_SETTING_VERIFY_NORMALIZABLE_ERROR);
924 g_clear_error (&normalizable_error);
926 /* Try to perform all kind of normalizations on the settings to fix it.
927 * We only do this, after verifying that the connection contains no un-normalizable
928 * errors, because in that case we rather fail without touching the settings. */
930 was_modified |= _normalize_virtual_iface_name (connection);
931 was_modified |= _normalize_ip_config (connection, parameters);
934 success = _nm_connection_verify (connection, error);
936 /* we would expect, that after normalization, the connection can be verified. */
937 g_return_val_if_fail (success == NM_SETTING_VERIFY_SUCCESS, success);
939 /* we would expect, that the connection was modified during normalization. */
940 g_return_val_if_fail (was_modified, success);
944 *modified = was_modified;
946 return success == NM_SETTING_VERIFY_SUCCESS;
950 * nm_connection_update_secrets:
951 * @connection: the #NMConnection
952 * @setting_name: the setting object name to which the secrets apply
953 * @secrets: (element-type utf8 GObject.Value): a #GHashTable mapping
954 * string:#GValue of setting property names and secrets of the given @setting_name
955 * @error: location to store error, or %NULL
957 * Update the specified setting's secrets, given a hash table of secrets
958 * intended for that setting (deserialized from D-Bus for example). Will also
959 * extract the given setting's secrets hash if given a hash of hashes, as would
960 * be returned from nm_connection_to_hash(). If @setting_name is %NULL, expects
961 * a fully serialized #NMConnection as returned by nm_connection_to_hash() and
962 * will update all secrets from all settings contained in @secrets.
964 * Returns: %TRUE if the secrets were successfully updated, %FALSE if the update
965 * failed (tried to update secrets for a setting that doesn't exist, etc)
968 nm_connection_update_secrets (NMConnection *connection,
969 const char *setting_name,
974 gboolean success = TRUE, updated = FALSE;
975 GHashTable *setting_hash = NULL;
978 gboolean hashed_connection = FALSE;
981 g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
982 g_return_val_if_fail (secrets != NULL, FALSE);
984 g_return_val_if_fail (*error == NULL, FALSE);
986 /* Empty @secrets means success */
987 if (g_hash_table_size (secrets) == 0)
990 /* For backwards compatibility, this function accepts either a hashed
991 * connection (GHashTable of GHashTables of GValues) or a single hashed
992 * setting (GHashTable of GValues).
994 g_hash_table_iter_init (&iter, secrets);
995 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL)) {
996 if (_nm_setting_lookup_setting_type (key) != G_TYPE_INVALID) {
997 /* @secrets looks like a hashed connection */
998 hashed_connection = TRUE;
1004 /* Update just one setting's secrets */
1005 setting = nm_connection_get_setting_by_name (connection, setting_name);
1007 g_set_error_literal (error,
1008 NM_CONNECTION_ERROR,
1009 NM_CONNECTION_ERROR_SETTING_NOT_FOUND,
1014 if (hashed_connection) {
1015 setting_hash = g_hash_table_lookup (secrets, setting_name);
1016 if (!setting_hash) {
1017 /* The hashed connection that didn't contain any secrets for
1018 * @setting_name; just return success.
1024 g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection);
1025 success_detail = _nm_setting_update_secrets (setting,
1026 setting_hash ? setting_hash : secrets,
1028 g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection);
1030 if (success_detail == NM_SETTING_UPDATE_SECRET_ERROR)
1032 if (success_detail == NM_SETTING_UPDATE_SECRET_SUCCESS_MODIFIED)
1035 if (!hashed_connection) {
1036 g_set_error_literal (error,
1037 NM_CONNECTION_ERROR,
1038 NM_CONNECTION_ERROR_SETTING_NOT_FOUND,
1043 /* check first, whether all the settings exist... */
1044 g_hash_table_iter_init (&iter, secrets);
1045 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL)) {
1046 setting = nm_connection_get_setting_by_name (connection, key);
1048 g_set_error_literal (error,
1049 NM_CONNECTION_ERROR,
1050 NM_CONNECTION_ERROR_SETTING_NOT_FOUND,
1056 /* Update each setting with any secrets from the hashed connection */
1057 g_hash_table_iter_init (&iter, secrets);
1058 while (g_hash_table_iter_next (&iter, (gpointer) &key, (gpointer) &setting_hash)) {
1059 /* Update the secrets for this setting */
1060 setting = nm_connection_get_setting_by_name (connection, key);
1062 g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection);
1063 success_detail = _nm_setting_update_secrets (setting, setting_hash, error);
1064 g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection);
1066 if (success_detail == NM_SETTING_UPDATE_SECRET_ERROR) {
1070 if (success_detail == NM_SETTING_UPDATE_SECRET_SUCCESS_MODIFIED)
1076 g_signal_emit (connection, signals[SECRETS_UPDATED], 0, setting_name);
1077 g_signal_emit (connection, signals[CHANGED], 0);
1084 * nm_connection_need_secrets:
1085 * @connection: the #NMConnection
1086 * @hints: (out) (element-type utf8) (allow-none) (transfer container):
1087 * the address of a pointer to a #GPtrArray, initialized to %NULL, which on
1088 * return points to an allocated #GPtrArray containing the property names of
1089 * secrets of the #NMSetting which may be required; the caller owns the array
1090 * and must free the array itself with g_ptr_array_free(), but not free its
1093 * Returns the name of the first setting object in the connection which would
1094 * need secrets to make a successful connection. The returned hints are only
1095 * intended as a guide to what secrets may be required, because in some
1096 * circumstances, there is no way to conclusively determine exactly which
1097 * secrets are needed.
1099 * Returns: the setting name of the #NMSetting object which has invalid or
1103 nm_connection_need_secrets (NMConnection *connection,
1106 NMConnectionPrivate *priv;
1107 GHashTableIter hiter;
1108 GSList *settings = NULL;
1110 const char *name = NULL;
1113 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1115 g_return_val_if_fail (*hints == NULL, NULL);
1117 priv = NM_CONNECTION_GET_PRIVATE (connection);
1119 /* Get list of settings in priority order */
1120 g_hash_table_iter_init (&hiter, priv->settings);
1121 while (g_hash_table_iter_next (&hiter, NULL, (gpointer) &setting))
1122 settings = g_slist_insert_sorted (settings, setting, _nm_setting_compare_priority);
1124 for (iter = settings; iter; iter = g_slist_next (iter)) {
1127 setting = NM_SETTING (iter->data);
1128 secrets = nm_setting_need_secrets (setting);
1133 g_ptr_array_free (secrets, TRUE);
1135 name = nm_setting_get_name (setting);
1140 g_slist_free (settings);
1145 * nm_connection_clear_secrets:
1146 * @connection: the #NMConnection
1148 * Clears and frees any secrets that may be stored in the connection, to avoid
1149 * keeping secret data in memory when not needed.
1152 nm_connection_clear_secrets (NMConnection *connection)
1154 GHashTableIter iter;
1156 gboolean changed = FALSE;
1158 g_return_if_fail (NM_IS_CONNECTION (connection));
1160 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1161 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting)) {
1162 g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection);
1163 changed |= _nm_setting_clear_secrets (setting);
1164 g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection);
1167 g_signal_emit (connection, signals[SECRETS_CLEARED], 0);
1169 g_signal_emit (connection, signals[CHANGED], 0);
1173 * nm_connection_clear_secrets_with_flags:
1174 * @connection: the #NMConnection
1175 * @func: (scope call): function to be called to determine whether a
1176 * specific secret should be cleared or not
1177 * @user_data: caller-supplied data passed to @func
1179 * Clears and frees secrets determined by @func.
1182 nm_connection_clear_secrets_with_flags (NMConnection *connection,
1183 NMSettingClearSecretsWithFlagsFn func,
1186 GHashTableIter iter;
1188 gboolean changed = FALSE;
1190 g_return_if_fail (NM_IS_CONNECTION (connection));
1192 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1193 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting)) {
1194 g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection);
1195 changed |= _nm_setting_clear_secrets_with_flags (setting, func, user_data);
1196 g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection);
1199 g_signal_emit (connection, signals[SECRETS_CLEARED], 0);
1201 g_signal_emit (connection, signals[CHANGED], 0);
1205 * nm_connection_to_hash:
1206 * @connection: the #NMConnection
1207 * @flags: hash flags, e.g. %NM_SETTING_HASH_FLAG_ALL
1209 * Converts the #NMConnection into a #GHashTable describing the connection,
1210 * suitable for marshalling over D-Bus or serializing. The hash table mapping
1211 * is string:#GHashTable with each element in the returned hash representing
1212 * a #NMSetting object. The keys are setting object names, and the values
1213 * are #GHashTables mapping string:GValue, each of which represents the
1214 * properties of the #NMSetting object.
1216 * Returns: (transfer full) (element-type utf8 GLib.HashTable): a new
1217 * #GHashTable describing the connection, its settings, and each setting's
1218 * properties. The caller owns the hash table and must unref the hash table
1219 * with g_hash_table_unref() when it is no longer needed.
1222 nm_connection_to_hash (NMConnection *connection, NMSettingHashFlags flags)
1224 NMConnectionPrivate *priv;
1225 GHashTableIter iter;
1227 GHashTable *ret, *setting_hash;
1229 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1231 ret = g_hash_table_new_full (g_str_hash, g_str_equal,
1232 g_free, (GDestroyNotify) g_hash_table_unref);
1234 priv = NM_CONNECTION_GET_PRIVATE (connection);
1236 /* Add each setting's hash to the main hash */
1237 g_hash_table_iter_init (&iter, priv->settings);
1238 while (g_hash_table_iter_next (&iter, &key, &data)) {
1239 NMSetting *setting = NM_SETTING (data);
1241 setting_hash = nm_setting_to_hash (setting, flags);
1243 g_hash_table_insert (ret, g_strdup (nm_setting_get_name (setting)), setting_hash);
1246 /* Don't send empty hashes */
1247 if (g_hash_table_size (ret) < 1) {
1248 g_hash_table_destroy (ret);
1256 * nm_connection_is_type:
1257 * @connection: the #NMConnection
1258 * @type: a setting name to check the connection's type against (like
1259 * %NM_SETTING_WIRELESS_SETTING_NAME or %NM_SETTING_WIRED_SETTING_NAME)
1261 * A convenience function to check if the given @connection is a particular
1262 * type (ie wired, Wi-Fi, ppp, etc). Checks the #NMSettingConnection:type
1263 * property of the connection and matches that against @type.
1265 * Returns: %TRUE if the connection is of the given @type, %FALSE if not
1268 nm_connection_is_type (NMConnection *connection, const char *type)
1270 NMSettingConnection *s_con;
1273 g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
1274 g_return_val_if_fail (type != NULL, FALSE);
1276 s_con = nm_connection_get_setting_connection (connection);
1280 type2 = nm_setting_connection_get_connection_type (s_con);
1282 return (g_strcmp0 (type2, type) == 0);
1286 * nm_connection_for_each_setting_value:
1287 * @connection: the #NMConnection
1288 * @func: (scope call): user-supplied function called for each setting's property
1289 * @user_data: user data passed to @func at each invocation
1291 * Iterates over the properties of each #NMSetting object in the #NMConnection,
1292 * calling the supplied user function for each property.
1295 nm_connection_for_each_setting_value (NMConnection *connection,
1296 NMSettingValueIterFn func,
1299 GHashTableIter iter;
1302 g_return_if_fail (NM_IS_CONNECTION (connection));
1303 g_return_if_fail (func != NULL);
1305 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1306 while (g_hash_table_iter_next (&iter, NULL, &value))
1307 nm_setting_enumerate_values (NM_SETTING (value), func, user_data);
1311 * nm_connection_dump:
1312 * @connection: the #NMConnection
1314 * Print the connection to stdout. For debugging purposes ONLY, should NOT
1315 * be used for serialization of the connection or machine-parsed in any way. The
1316 * output format is not guaranteed to be stable and may change at any time.
1319 nm_connection_dump (NMConnection *connection)
1321 GHashTableIter iter;
1323 const char *setting_name;
1329 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1330 while (g_hash_table_iter_next (&iter, (gpointer) &setting_name, (gpointer) &setting)) {
1331 str = nm_setting_to_string (setting);
1332 g_print ("%s\n", str);
1338 * nm_connection_set_path:
1339 * @connection: the #NMConnection
1340 * @path: the D-Bus path of the connection as given by the settings service
1341 * which provides the connection
1343 * Sets the D-Bus path of the connection. This property is not serialized, and
1344 * is only for the reference of the caller. Sets the #NMConnection:path
1348 nm_connection_set_path (NMConnection *connection, const char *path)
1350 NMConnectionPrivate *priv;
1352 g_return_if_fail (NM_IS_CONNECTION (connection));
1354 priv = NM_CONNECTION_GET_PRIVATE (connection);
1356 g_free (priv->path);
1360 priv->path = g_strdup (path);
1364 * nm_connection_get_path:
1365 * @connection: the #NMConnection
1367 * Returns the connection's D-Bus path.
1369 * Returns: the D-Bus path of the connection, previously set by a call to
1370 * nm_connection_set_path().
1373 nm_connection_get_path (NMConnection *connection)
1375 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1377 return NM_CONNECTION_GET_PRIVATE (connection)->path;
1381 * nm_connection_get_interface_name:
1382 * @connection: The #NMConnection
1384 * Returns the interface name as stored in NMSettingConnection:interface_name.
1385 * If the connection contains no NMSettingConnection, it will return %NULL.
1387 * For hardware devices and software devices created outside of NetworkManager,
1388 * this name is used to match the device. for software devices created by
1389 * NetworkManager, this is the name of the created interface.
1391 * Returns: Name of the kernel interface or %NULL
1396 nm_connection_get_interface_name (NMConnection *connection)
1398 NMSettingConnection *s_con;
1400 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1402 s_con = nm_connection_get_setting_connection (connection);
1404 return s_con ? nm_setting_connection_get_interface_name (s_con) : NULL;
1408 * nm_connection_get_virtual_iface_name:
1409 * @connection: The #NMConnection
1411 * Returns the name of the virtual kernel interface which the connection
1412 * needs to use if specified in the settings. This function abstracts all
1413 * connection types which require this functionality. For all other
1414 * connection types, this function will return %NULL.
1416 * Returns: Name of the kernel interface or %NULL
1419 nm_connection_get_virtual_iface_name (NMConnection *connection)
1421 NMSettingConnection *s_con;
1425 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1427 s_con = nm_connection_get_setting_connection (connection);
1428 g_return_val_if_fail (s_con, NULL);
1430 type = nm_setting_connection_get_connection_type (s_con);
1431 g_return_val_if_fail (type, NULL);
1433 base = nm_connection_get_setting_by_name (connection, type);
1437 return nm_setting_get_virtual_iface_name (base);
1441 * nm_connection_new:
1443 * Creates a new #NMConnection object with no #NMSetting objects.
1445 * Returns: the new empty #NMConnection object
1448 nm_connection_new (void)
1450 return (NMConnection *) g_object_new (NM_TYPE_CONNECTION, NULL);
1454 * _nm_connection_new_from_hash:
1455 * @hash: (element-type utf8 GLib.HashTable): the #GHashTable describing
1458 * Creates a new #NMConnection from a hash table describing the connection. See
1459 * nm_connection_to_hash() for a description of the expected hash table.
1461 * Returns: the new #NMConnection object, populated with settings created
1462 * from the values in the hash table.
1465 _nm_connection_new_from_hash (GHashTable *hash)
1467 NMConnection *connection;
1469 g_return_val_if_fail (hash != NULL, NULL);
1471 connection = nm_connection_new ();
1472 _nm_connection_replace_settings (connection, hash);
1477 * nm_connection_new_from_hash:
1478 * @hash: (element-type utf8 GLib.HashTable): the #GHashTable describing
1480 * @error: on unsuccessful return, an error
1482 * Creates a new #NMConnection from a hash table describing the connection. See
1483 * nm_connection_to_hash() for a description of the expected hash table.
1485 * Returns: the new #NMConnection object, populated with settings created
1486 * from the values in the hash table, or %NULL if the connection failed to
1490 nm_connection_new_from_hash (GHashTable *hash, GError **error)
1492 NMConnection *connection;
1494 g_return_val_if_fail (hash != NULL, NULL);
1496 if (!validate_permissions_type (hash, error))
1499 connection = _nm_connection_new_from_hash (hash);
1500 if (!nm_connection_verify (connection, error))
1501 g_clear_object (&connection);
1506 * nm_connection_duplicate:
1507 * @connection: the #NMConnection to duplicate
1509 * Duplicates a #NMConnection.
1511 * Returns: (transfer full): a new #NMConnection containing the same settings and properties
1512 * as the source #NMConnection
1515 nm_connection_duplicate (NMConnection *connection)
1518 GHashTableIter iter;
1521 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1523 dup = nm_connection_new ();
1524 nm_connection_set_path (dup, nm_connection_get_path (connection));
1526 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1527 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting))
1528 _nm_connection_add_setting (dup, nm_setting_duplicate (setting));
1534 * nm_connection_get_uuid:
1535 * @connection: the #NMConnection
1537 * A shortcut to return the UUID from the connection's #NMSettingConnection.
1539 * Returns: the UUID from the connection's 'connection' setting
1542 nm_connection_get_uuid (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);
1549 g_return_val_if_fail (s_con != NULL, NULL);
1551 return nm_setting_connection_get_uuid (s_con);
1555 * nm_connection_get_id:
1556 * @connection: the #NMConnection
1558 * A shortcut to return the ID from the connection's #NMSettingConnection.
1560 * Returns: the ID from the connection's 'connection' setting
1563 nm_connection_get_id (NMConnection *connection)
1565 NMSettingConnection *s_con;
1567 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1569 s_con = nm_connection_get_setting_connection (connection);
1570 g_return_val_if_fail (s_con != NULL, NULL);
1572 return nm_setting_connection_get_id (s_con);
1576 * nm_connection_get_connection_type:
1577 * @connection: the #NMConnection
1579 * A shortcut to return the type from the connection's #NMSettingConnection.
1581 * Returns: the type from the connection's 'connection' setting
1586 nm_connection_get_connection_type (NMConnection *connection)
1588 NMSettingConnection *s_con;
1590 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1592 s_con = nm_connection_get_setting_connection (connection);
1593 g_return_val_if_fail (s_con != NULL, NULL);
1595 return nm_setting_connection_get_connection_type (s_con);
1599 * nm_connection_get_virtual_device_description:
1600 * @connection: an #NMConnection for a virtual device type
1602 * Returns the name that nm_device_disambiguate_names() would
1603 * return for the virtual device that would be created for @connection.
1604 * Eg, "VLAN (eth1.1)".
1606 * Returns: (transfer full): the name of @connection's device,
1607 * or %NULL if @connection is not a virtual connection type
1612 nm_connection_get_virtual_device_description (NMConnection *connection)
1614 const char *iface, *type, *display_type;
1615 NMSettingConnection *s_con;
1617 iface = nm_connection_get_virtual_iface_name (connection);
1621 s_con = nm_connection_get_setting_connection (connection);
1622 g_return_val_if_fail (s_con != NULL, NULL);
1623 type = nm_setting_connection_get_connection_type (s_con);
1625 if (!strcmp (type, NM_SETTING_BOND_SETTING_NAME))
1626 display_type = _("Bond");
1627 else if (!strcmp (type, NM_SETTING_TEAM_SETTING_NAME))
1628 display_type = _("Team");
1629 else if (!strcmp (type, NM_SETTING_BRIDGE_SETTING_NAME))
1630 display_type = _("Bridge");
1631 else if (!strcmp (type, NM_SETTING_VLAN_SETTING_NAME))
1632 display_type = _("VLAN");
1634 g_warning ("Unrecognized virtual device type '%s'", type);
1635 display_type = type;
1638 return g_strdup_printf ("%s (%s)", display_type, iface);
1641 /*************************************************************/
1644 * nm_connection_get_setting_802_1x:
1645 * @connection: the #NMConnection
1647 * A shortcut to return any #NMSetting8021x the connection might contain.
1649 * Returns: (transfer none): an #NMSetting8021x if the connection contains one, otherwise %NULL
1652 nm_connection_get_setting_802_1x (NMConnection *connection)
1654 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1656 return (NMSetting8021x *) nm_connection_get_setting (connection, NM_TYPE_SETTING_802_1X);
1660 * nm_connection_get_setting_bluetooth:
1661 * @connection: the #NMConnection
1663 * A shortcut to return any #NMSettingBluetooth the connection might contain.
1665 * Returns: (transfer none): an #NMSettingBluetooth if the connection contains one, otherwise %NULL
1667 NMSettingBluetooth *
1668 nm_connection_get_setting_bluetooth (NMConnection *connection)
1670 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1672 return (NMSettingBluetooth *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BLUETOOTH);
1676 * nm_connection_get_setting_bond:
1677 * @connection: the #NMConnection
1679 * A shortcut to return any #NMSettingBond the connection might contain.
1681 * Returns: (transfer none): an #NMSettingBond if the connection contains one, otherwise %NULL
1684 nm_connection_get_setting_bond (NMConnection *connection)
1686 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1688 return (NMSettingBond *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BOND);
1692 * nm_connection_get_setting_team:
1693 * @connection: the #NMConnection
1695 * A shortcut to return any #NMSettingTeam the connection might contain.
1697 * Returns: (transfer none): an #NMSettingTeam if the connection contains one, otherwise %NULL
1702 nm_connection_get_setting_team (NMConnection *connection)
1704 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1706 return (NMSettingTeam *) nm_connection_get_setting (connection, NM_TYPE_SETTING_TEAM);
1710 * nm_connection_get_setting_team_port:
1711 * @connection: the #NMConnection
1713 * A shortcut to return any #NMSettingTeamPort the connection might contain.
1715 * Returns: (transfer none): an #NMSettingTeamPort if the connection contains one, otherwise %NULL
1720 nm_connection_get_setting_team_port (NMConnection *connection)
1722 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1724 return (NMSettingTeamPort *) nm_connection_get_setting (connection, NM_TYPE_SETTING_TEAM_PORT);
1728 * nm_connection_get_setting_bridge:
1729 * @connection: the #NMConnection
1731 * A shortcut to return any #NMSettingBridge the connection might contain.
1733 * Returns: (transfer none): an #NMSettingBridge if the connection contains one, otherwise %NULL
1736 nm_connection_get_setting_bridge (NMConnection *connection)
1738 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1740 return (NMSettingBridge *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BRIDGE);
1744 * nm_connection_get_setting_cdma:
1745 * @connection: the #NMConnection
1747 * A shortcut to return any #NMSettingCdma the connection might contain.
1749 * Returns: (transfer none): an #NMSettingCdma if the connection contains one, otherwise %NULL
1752 nm_connection_get_setting_cdma (NMConnection *connection)
1754 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1756 return (NMSettingCdma *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CDMA);
1760 * nm_connection_get_setting_connection:
1761 * @connection: the #NMConnection
1763 * A shortcut to return any #NMSettingConnection the connection might contain.
1765 * Returns: (transfer none): an #NMSettingConnection if the connection contains one, otherwise %NULL
1767 NMSettingConnection *
1768 nm_connection_get_setting_connection (NMConnection *connection)
1770 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1772 return (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
1776 * nm_connection_get_setting_dcb:
1777 * @connection: the #NMConnection
1779 * A shortcut to return any #NMSettingDcb the connection might contain.
1781 * Returns: (transfer none): an #NMSettingDcb if the connection contains one, otherwise NULL
1786 nm_connection_get_setting_dcb (NMConnection *connection)
1788 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1790 return (NMSettingDcb *) nm_connection_get_setting (connection, NM_TYPE_SETTING_DCB);
1794 * nm_connection_get_setting_generic:
1795 * @connection: the #NMConnection
1797 * A shortcut to return any #NMSettingGeneric the connection might contain.
1799 * Returns: (transfer none): an #NMSettingGeneric if the connection contains one, otherwise NULL
1804 nm_connection_get_setting_generic (NMConnection *connection)
1806 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1808 return (NMSettingGeneric *) nm_connection_get_setting (connection, NM_TYPE_SETTING_GENERIC);
1812 * nm_connection_get_setting_gsm:
1813 * @connection: the #NMConnection
1815 * A shortcut to return any #NMSettingGsm the connection might contain.
1817 * Returns: (transfer none): an #NMSettingGsm if the connection contains one, otherwise %NULL
1820 nm_connection_get_setting_gsm (NMConnection *connection)
1822 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1824 return (NMSettingGsm *) nm_connection_get_setting (connection, NM_TYPE_SETTING_GSM);
1828 * nm_connection_get_setting_infiniband:
1829 * @connection: the #NMConnection
1831 * A shortcut to return any #NMSettingInfiniband the connection might contain.
1833 * Returns: (transfer none): an #NMSettingInfiniband if the connection contains one, otherwise %NULL
1835 NMSettingInfiniband *
1836 nm_connection_get_setting_infiniband (NMConnection *connection)
1838 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1840 return (NMSettingInfiniband *) nm_connection_get_setting (connection, NM_TYPE_SETTING_INFINIBAND);
1844 * nm_connection_get_setting_ip4_config:
1845 * @connection: the #NMConnection
1847 * A shortcut to return any #NMSettingIP4Config the connection might contain.
1849 * Returns: (transfer none): an #NMSettingIP4Config if the connection contains one, otherwise %NULL
1851 NMSettingIP4Config *
1852 nm_connection_get_setting_ip4_config (NMConnection *connection)
1854 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1856 return (NMSettingIP4Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG);
1860 * nm_connection_get_setting_ip6_config:
1861 * @connection: the #NMConnection
1863 * A shortcut to return any #NMSettingIP6Config the connection might contain.
1865 * Returns: (transfer none): an #NMSettingIP6Config if the connection contains one, otherwise %NULL
1867 NMSettingIP6Config *
1868 nm_connection_get_setting_ip6_config (NMConnection *connection)
1870 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1872 return (NMSettingIP6Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP6_CONFIG);
1876 * nm_connection_get_setting_olpc_mesh:
1877 * @connection: the #NMConnection
1879 * A shortcut to return any #NMSettingOlpcMesh the connection might contain.
1881 * Returns: (transfer none): an #NMSettingOlpcMesh if the connection contains one, otherwise %NULL
1884 nm_connection_get_setting_olpc_mesh (NMConnection *connection)
1886 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1888 return (NMSettingOlpcMesh *) nm_connection_get_setting (connection, NM_TYPE_SETTING_OLPC_MESH);
1892 * nm_connection_get_setting_ppp:
1893 * @connection: the #NMConnection
1895 * A shortcut to return any #NMSettingPPP the connection might contain.
1897 * Returns: (transfer none): an #NMSettingPPP if the connection contains one, otherwise %NULL
1900 nm_connection_get_setting_ppp (NMConnection *connection)
1902 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1904 return (NMSettingPPP *) nm_connection_get_setting (connection, NM_TYPE_SETTING_PPP);
1908 * nm_connection_get_setting_pppoe:
1909 * @connection: the #NMConnection
1911 * A shortcut to return any #NMSettingPPPOE the connection might contain.
1913 * Returns: (transfer none): an #NMSettingPPPOE if the connection contains one, otherwise %NULL
1916 nm_connection_get_setting_pppoe (NMConnection *connection)
1918 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1920 return (NMSettingPPPOE *) nm_connection_get_setting (connection, NM_TYPE_SETTING_PPPOE);
1924 * nm_connection_get_setting_serial:
1925 * @connection: the #NMConnection
1927 * A shortcut to return any #NMSettingSerial the connection might contain.
1929 * Returns: (transfer none): an #NMSettingSerial if the connection contains one, otherwise %NULL
1932 nm_connection_get_setting_serial (NMConnection *connection)
1934 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1936 return (NMSettingSerial *) nm_connection_get_setting (connection, NM_TYPE_SETTING_SERIAL);
1940 * nm_connection_get_setting_vpn:
1941 * @connection: the #NMConnection
1943 * A shortcut to return any #NMSettingVPN the connection might contain.
1945 * Returns: (transfer none): an #NMSettingVPN if the connection contains one, otherwise %NULL
1948 nm_connection_get_setting_vpn (NMConnection *connection)
1950 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1952 return (NMSettingVPN *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VPN);
1956 * nm_connection_get_setting_wimax:
1957 * @connection: the #NMConnection
1959 * A shortcut to return any #NMSettingWimax the connection might contain.
1961 * Returns: (transfer none): an #NMSettingWimax if the connection contains one, otherwise %NULL
1964 nm_connection_get_setting_wimax (NMConnection *connection)
1966 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1968 return (NMSettingWimax *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIMAX);
1972 * nm_connection_get_setting_wired:
1973 * @connection: the #NMConnection
1975 * A shortcut to return any #NMSettingWired the connection might contain.
1977 * Returns: (transfer none): an #NMSettingWired if the connection contains one, otherwise %NULL
1980 nm_connection_get_setting_wired (NMConnection *connection)
1982 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1984 return (NMSettingWired *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRED);
1988 * nm_connection_get_setting_adsl:
1989 * @connection: the #NMConnection
1991 * A shortcut to return any #NMSettingAdsl the connection might contain.
1993 * Returns: (transfer none): an #NMSettingAdsl if the connection contains one, otherwise %NULL
1996 nm_connection_get_setting_adsl (NMConnection *connection)
1998 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2000 return (NMSettingAdsl *) nm_connection_get_setting (connection, NM_TYPE_SETTING_ADSL);
2004 * nm_connection_get_setting_wireless:
2005 * @connection: the #NMConnection
2007 * A shortcut to return any #NMSettingWireless the connection might contain.
2009 * Returns: (transfer none): an #NMSettingWireless if the connection contains one, otherwise %NULL
2012 nm_connection_get_setting_wireless (NMConnection *connection)
2014 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2016 return (NMSettingWireless *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS);
2020 * nm_connection_get_setting_wireless_security:
2021 * @connection: the #NMConnection
2023 * A shortcut to return any #NMSettingWirelessSecurity the connection might contain.
2025 * Returns: (transfer none): an #NMSettingWirelessSecurity if the connection contains one, otherwise %NULL
2027 NMSettingWirelessSecurity *
2028 nm_connection_get_setting_wireless_security (NMConnection *connection)
2030 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2032 return (NMSettingWirelessSecurity *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS_SECURITY);
2036 * nm_connection_get_setting_bridge_port:
2037 * @connection: the #NMConnection
2039 * A shortcut to return any #NMSettingBridgePort the connection might contain.
2041 * Returns: (transfer none): an #NMSettingBridgePort if the connection contains one, otherwise %NULL
2043 NMSettingBridgePort *
2044 nm_connection_get_setting_bridge_port (NMConnection *connection)
2046 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2048 return (NMSettingBridgePort *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BRIDGE_PORT);
2052 * nm_connection_get_setting_vlan:
2053 * @connection: the #NMConnection
2055 * A shortcut to return any #NMSettingVlan the connection might contain.
2057 * Returns: (transfer none): an #NMSettingVlan if the connection contains one, otherwise %NULL
2060 nm_connection_get_setting_vlan (NMConnection *connection)
2062 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2064 return (NMSettingVlan *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VLAN);
2067 /*************************************************************/
2070 nm_connection_init (NMConnection *connection)
2072 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (connection);
2074 priv->settings = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_object_unref);
2078 dispose (GObject *object)
2080 NMConnection *self = NM_CONNECTION (object);
2081 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (self);
2083 g_hash_table_foreach_remove (priv->settings, _setting_release, self);
2085 G_OBJECT_CLASS (nm_connection_parent_class)->dispose (object);
2089 finalize (GObject *object)
2091 NMConnection *connection = NM_CONNECTION (object);
2092 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (connection);
2094 g_assert (g_hash_table_size (priv->settings) == 0);
2095 g_hash_table_destroy (priv->settings);
2096 g_free (priv->path);
2098 G_OBJECT_CLASS (nm_connection_parent_class)->finalize (object);
2102 set_property (GObject *object, guint prop_id,
2103 const GValue *value, GParamSpec *pspec)
2105 NMConnection *connection = NM_CONNECTION (object);
2109 nm_connection_set_path (connection, g_value_get_string (value));
2112 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2118 get_property (GObject *object, guint prop_id,
2119 GValue *value, GParamSpec *pspec)
2121 NMConnection *connection = NM_CONNECTION (object);
2125 g_value_set_string (value, nm_connection_get_path (connection));
2128 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2134 nm_connection_class_init (NMConnectionClass *klass)
2136 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2138 g_type_class_add_private (klass, sizeof (NMConnectionPrivate));
2140 /* virtual methods */
2141 object_class->set_property = set_property;
2142 object_class->get_property = get_property;
2143 object_class->dispose = dispose;
2144 object_class->finalize = finalize;
2149 * NMConnection:path:
2151 * The connection's D-Bus path, used only by the calling process as a record
2152 * of the D-Bus path of the connection as provided by a settings service.
2154 g_object_class_install_property
2155 (object_class, PROP_PATH,
2156 g_param_spec_string (NM_CONNECTION_PATH, "", "",
2160 G_PARAM_STATIC_STRINGS));
2165 * NMConnection::secrets-updated:
2166 * @connection: the object on which the signal is emitted
2167 * @setting_name: the setting name of the #NMSetting for which secrets were
2170 * The ::secrets-updated signal is emitted when the secrets of a setting
2171 * have been changed.
2173 signals[SECRETS_UPDATED] =
2174 g_signal_new (NM_CONNECTION_SECRETS_UPDATED,
2175 G_OBJECT_CLASS_TYPE (object_class),
2177 G_STRUCT_OFFSET (NMConnectionClass, secrets_updated),
2179 g_cclosure_marshal_VOID__STRING,
2184 * NMConnection::secrets-cleared:
2185 * @connection: the object on which the signal is emitted
2187 * The ::secrets-cleared signal is emitted when the secrets of a connection
2190 signals[SECRETS_CLEARED] =
2191 g_signal_new (NM_CONNECTION_SECRETS_CLEARED,
2192 G_OBJECT_CLASS_TYPE (object_class),
2195 g_cclosure_marshal_VOID__VOID,
2199 * NMConnection::changed:
2200 * @connection: the object on which the signal is emitted
2202 * The ::changed signal is emitted when any property of any property
2203 * (including secrets) of any setting of the connection is modified,
2204 * or when settings are added or removed.
2209 g_signal_new (NM_CONNECTION_CHANGED,
2210 G_OBJECT_CLASS_TYPE (object_class),
2213 g_cclosure_marshal_VOID__VOID,