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 hash_to_connection (NMConnection *connection, GHashTable *new, GError **error)
334 const char *setting_name;
335 GHashTable *setting_hash;
336 gboolean changed, valid;
337 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (connection);
339 if ((changed = g_hash_table_size (priv->settings) > 0))
340 g_hash_table_foreach_remove (priv->settings, _setting_release, connection);
342 g_hash_table_iter_init (&iter, new);
343 while (g_hash_table_iter_next (&iter, (gpointer) &setting_name, (gpointer) &setting_hash)) {
344 GType type = nm_connection_lookup_setting_type (setting_name);
347 NMSetting *setting = nm_setting_new_from_hash (type, setting_hash);
350 _nm_connection_add_setting (connection, setting);
356 valid = nm_connection_verify (connection, error);
358 g_signal_emit (connection, signals[CHANGED], 0);
363 * nm_connection_replace_settings:
364 * @connection: a #NMConnection
365 * @new_settings: (element-type utf8 GLib.HashTable): a #GHashTable of settings
366 * @error: location to store error, or %NULL
368 * Returns: %TRUE if the settings were valid and added to the connection, %FALSE
372 nm_connection_replace_settings (NMConnection *connection,
373 GHashTable *new_settings,
376 gboolean valid = FALSE;
378 g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
379 g_return_val_if_fail (new_settings != NULL, FALSE);
381 g_return_val_if_fail (*error == NULL, FALSE);
383 if (validate_permissions_type (new_settings, error))
384 valid = hash_to_connection (connection, new_settings, error);
389 * nm_connection_replace_settings_from_connection:
390 * @connection: a #NMConnection
391 * @new_connection: a #NMConnection to replace the settings of @connection with
392 * @error: location to store error, or %NULL
394 * Deep-copies the settings of @new_conenction and replaces the settings of @connection
395 * with the copied settings.
397 * Returns: %TRUE if the settings were valid after replacing the connection, %FALSE
398 * if they were not. Regardless of whether %TRUE or %FALSE is returned, the connection
399 * is successfully replaced. %FALSE only means, that the connection does not verify
400 * at the end of the operation.
405 nm_connection_replace_settings_from_connection (NMConnection *connection,
406 NMConnection *new_connection,
409 NMConnectionPrivate *priv;
412 gboolean changed = FALSE;
415 g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
416 g_return_val_if_fail (NM_IS_CONNECTION (new_connection), FALSE);
417 g_return_val_if_fail (!error || !*error, FALSE);
419 /* When 'connection' and 'new_connection' are the same object simply return
420 * in order not to destroy 'connection' */
421 if (connection == new_connection)
424 /* No need to validate permissions like nm_connection_replace_settings()
425 * since we're dealing with an NMConnection which has already done that.
428 priv = NM_CONNECTION_GET_PRIVATE (connection);
429 if ((changed = g_hash_table_size (priv->settings) > 0))
430 g_hash_table_foreach_remove (priv->settings, _setting_release, connection);
432 if (g_hash_table_size (NM_CONNECTION_GET_PRIVATE (new_connection)->settings)) {
433 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (new_connection)->settings);
434 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting))
435 _nm_connection_add_setting (connection, nm_setting_duplicate (setting));
440 valid = nm_connection_verify (connection, error);
442 g_signal_emit (connection, signals[CHANGED], 0);
447 * nm_connection_compare:
448 * @a: a #NMConnection
449 * @b: a second #NMConnection to compare with the first
450 * @flags: compare flags, e.g. %NM_SETTING_COMPARE_FLAG_EXACT
452 * Compares two #NMConnection objects for similarity, with comparison behavior
453 * modified by a set of flags. See nm_setting_compare() for a description of
454 * each flag's behavior.
456 * Returns: %TRUE if the comparison succeeds, %FALSE if it does not
459 nm_connection_compare (NMConnection *a,
461 NMSettingCompareFlags flags)
471 /* B / A: ensure settings in B that are not in A make the comparison fail */
472 if (g_hash_table_size (NM_CONNECTION_GET_PRIVATE (a)->settings) !=
473 g_hash_table_size (NM_CONNECTION_GET_PRIVATE (b)->settings))
476 /* A / B: ensure all settings in A match corresponding ones in B */
477 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (a)->settings);
478 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &src)) {
479 NMSetting *cmp = nm_connection_get_setting (b, G_OBJECT_TYPE (src));
481 if (!cmp || !nm_setting_compare (src, cmp, flags))
490 diff_one_connection (NMConnection *a,
492 NMSettingCompareFlags flags,
493 gboolean invert_results,
496 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (a);
498 NMSetting *a_setting = NULL;
500 g_hash_table_iter_init (&iter, priv->settings);
501 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &a_setting)) {
502 NMSetting *b_setting = NULL;
503 const char *setting_name = nm_setting_get_name (a_setting);
505 gboolean new_results = TRUE;
508 b_setting = nm_connection_get_setting (b, G_OBJECT_TYPE (a_setting));
510 results = g_hash_table_lookup (diffs, setting_name);
514 if (!nm_setting_diff (a_setting, b_setting, flags, invert_results, &results)) {
516 g_hash_table_insert (diffs, g_strdup (setting_name), results);
522 * nm_connection_diff:
523 * @a: a #NMConnection
524 * @b: a second #NMConnection to compare with the first
525 * @flags: compare flags, e.g. %NM_SETTING_COMPARE_FLAG_EXACT
526 * @out_settings: (element-type utf8 GLib.HashTable): if the
527 * connections differ, on return a hash table mapping setting names to
528 * second-level GHashTable (utf8 to guint32), which contains the key names that
529 * differ mapped to one or more of %NMSettingDiffResult as a bitfield
531 * Compares two #NMConnection objects for similarity, with comparison behavior
532 * modified by a set of flags. See nm_setting_compare() for a description of
533 * each flag's behavior. If the connections differ, settings and keys within
534 * each setting that differ are added to the returned @out_settings hash table.
535 * No values are returned, only key names.
537 * Returns: %TRUE if the connections contain the same values, %FALSE if they do
541 nm_connection_diff (NMConnection *a,
543 NMSettingCompareFlags flags,
544 GHashTable **out_settings)
548 g_return_val_if_fail (NM_IS_CONNECTION (a), FALSE);
549 g_return_val_if_fail (out_settings != NULL, FALSE);
550 g_return_val_if_fail (*out_settings == NULL, FALSE);
552 g_return_val_if_fail (NM_IS_CONNECTION (b), FALSE);
557 diffs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_destroy);
559 /* Diff A to B, then B to A to capture keys in B that aren't in A */
560 diff_one_connection (a, b, flags, FALSE, diffs);
562 diff_one_connection (b, a, flags, TRUE, diffs);
564 if (g_hash_table_size (diffs) == 0)
565 g_hash_table_destroy (diffs);
567 *out_settings = diffs;
569 return *out_settings ? FALSE : TRUE;
573 _normalize_virtual_iface_name (NMConnection *self)
575 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (self);
576 GHashTableIter h_iter;
578 NMSettingConnection *s_con;
579 const char *interface_name;
580 char *virtual_iface_name = NULL;
581 gboolean was_modified = FALSE;
582 const char *prop_name = NULL;
584 /* search for settings that might need normalization of the interface name. */
585 g_hash_table_iter_init (&h_iter, priv->settings);
587 && g_hash_table_iter_next (&h_iter, NULL, (void **) &setting)) {
588 if (NM_IS_SETTING_BOND (setting))
589 prop_name = NM_SETTING_BOND_INTERFACE_NAME;
590 else if (NM_IS_SETTING_BRIDGE (setting))
591 prop_name = NM_SETTING_BRIDGE_INTERFACE_NAME;
592 else if (NM_IS_SETTING_TEAM (setting))
593 prop_name = NM_SETTING_TEAM_INTERFACE_NAME;
594 else if (NM_IS_SETTING_VLAN (setting))
595 prop_name = NM_SETTING_VLAN_INTERFACE_NAME;
600 s_con = nm_connection_get_setting_connection (self);
601 g_return_val_if_fail (s_con, FALSE);
603 interface_name = nm_setting_connection_get_interface_name (s_con);
605 /* read the potential virtual_iface_name from the setting. */
606 g_object_get (setting, prop_name, &virtual_iface_name, NULL);
608 if (g_strcmp0 (interface_name, virtual_iface_name) != 0) {
609 if (interface_name) {
610 /* interface_name is set and overwrites the virtual_iface_name. */
611 g_object_set (setting, prop_name, interface_name, NULL);
613 /* interface in NMSettingConnection must be set. */
614 g_object_set (s_con, NM_SETTING_CONNECTION_INTERFACE_NAME, virtual_iface_name, NULL);
619 g_free (virtual_iface_name);
625 _normalize_ip_config (NMConnection *self, GHashTable *parameters)
627 NMSettingConnection *s_con = nm_connection_get_setting_connection (self);
628 const char *default_ip4_method = NM_SETTING_IP4_CONFIG_METHOD_AUTO;
629 const char *default_ip6_method = NULL;
630 NMSettingIP4Config *s_ip4;
631 NMSettingIP6Config *s_ip6;
635 default_ip6_method = g_hash_table_lookup (parameters, NM_CONNECTION_NORMALIZE_PARAM_IP6_CONFIG_METHOD);
636 if (!default_ip6_method)
637 default_ip6_method = NM_SETTING_IP6_CONFIG_METHOD_AUTO;
639 s_ip4 = nm_connection_get_setting_ip4_config (self);
640 s_ip6 = nm_connection_get_setting_ip6_config (self);
642 if (nm_setting_connection_get_master (s_con)) {
643 /* Slave connections don't have IP configuration. */
646 nm_connection_remove_setting (self, NM_TYPE_SETTING_IP4_CONFIG);
649 nm_connection_remove_setting (self, NM_TYPE_SETTING_IP6_CONFIG);
651 return s_ip4 || s_ip6;
653 /* Ensure all non-slave connections have IP4 and IP6 settings objects. If no
654 * IP6 setting was specified, then assume that means IP6 config is allowed
655 * to fail. But if no IP4 setting was specified, assume the caller was just
659 setting = nm_setting_ip4_config_new ();
661 g_object_set (setting,
662 NM_SETTING_IP4_CONFIG_METHOD, default_ip4_method,
664 nm_connection_add_setting (self, setting);
667 setting = nm_setting_ip6_config_new ();
669 g_object_set (setting,
670 NM_SETTING_IP6_CONFIG_METHOD, default_ip6_method,
671 NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE,
673 nm_connection_add_setting (self, setting);
675 return !s_ip4 || !s_ip6;
680 * nm_connection_verify:
681 * @connection: the #NMConnection to verify
682 * @error: location to store error, or %NULL
684 * Validates the connection and all its settings. Each setting's properties
685 * have allowed values, and some values are dependent on other values. For
686 * example, if a Wi-Fi connection is security enabled, the #NMSettingWireless
687 * setting object's 'security' property must contain the setting name of the
688 * #NMSettingWirelessSecurity object, which must also be present in the
689 * connection for the connection to be valid. As another example, the
690 * #NMSettingWired object's 'mac-address' property must be a validly formatted
691 * MAC address. The returned #GError contains information about which
692 * setting and which property failed validation, and how it failed validation.
694 * Returns: %TRUE if the connection is valid, %FALSE if it is not
697 nm_connection_verify (NMConnection *connection, GError **error)
699 NMSettingVerifyResult result;
701 result = _nm_connection_verify (connection, error);
703 /* we treat normalizable connections as valid. */
704 if (result == NM_SETTING_VERIFY_NORMALIZABLE)
705 g_clear_error (error);
707 return result == NM_SETTING_VERIFY_SUCCESS || result == NM_SETTING_VERIFY_NORMALIZABLE;
710 static NMSettingVerifyResult
711 _nm_connection_verify (NMConnection *connection, GError **error)
713 NMConnectionPrivate *priv;
714 NMSettingConnection *s_con;
715 NMSettingIP4Config *s_ip4;
716 NMSettingIP6Config *s_ip6;
719 GSList *all_settings = NULL, *setting_i;
720 NMSettingVerifyResult success = NM_SETTING_VERIFY_ERROR;
723 GError *normalizable_error = NULL;
724 NMSettingVerifyResult normalizable_error_type = NM_SETTING_VERIFY_SUCCESS;
727 g_return_val_if_fail (*error == NULL, NM_SETTING_VERIFY_ERROR);
729 if (!NM_IS_CONNECTION (connection)) {
730 g_set_error_literal (error,
731 NM_SETTING_CONNECTION_ERROR,
732 NM_SETTING_CONNECTION_ERROR_UNKNOWN,
733 "invalid connection; failed verification");
734 g_return_val_if_fail (NM_IS_CONNECTION (connection), NM_SETTING_VERIFY_ERROR);
737 priv = NM_CONNECTION_GET_PRIVATE (connection);
739 /* First, make sure there's at least 'connection' setting */
740 s_con = nm_connection_get_setting_connection (connection);
742 g_set_error_literal (error,
744 NM_CONNECTION_ERROR_CONNECTION_SETTING_NOT_FOUND,
745 "connection setting not found");
749 /* Build up the list of settings */
750 g_hash_table_iter_init (&iter, priv->settings);
751 while (g_hash_table_iter_next (&iter, NULL, &value)) {
752 /* Order NMSettingConnection so that it will be verified first.
753 * The reason is, that NMSettingConnection:verify() modifies the connection
754 * by setting NMSettingConnection:interface_name. So we want to call that
755 * verify() first, because the order can affect the outcome.
756 * Another reason is, that errors in this setting might be more fundamental
757 * and should be checked and reported with higher priority.
758 * Another reason is, that some settings look especially at the
759 * NMSettingConnection, so they find it first in the all_settings list. */
761 all_settings = g_slist_append (all_settings, value);
763 all_settings = g_slist_prepend (all_settings, value);
765 all_settings = g_slist_reverse (all_settings);
767 /* Now, run the verify function of each setting */
768 for (setting_i = all_settings; setting_i; setting_i = setting_i->next) {
769 GError *verify_error = NULL;
770 NMSettingVerifyResult verify_result;
772 /* verify all settings. We stop if we find the first non-normalizable
773 * @NM_SETTING_VERIFY_ERROR. If we find normalizable errors we continue
774 * but remember the error to return it to the user.
775 * @NM_SETTING_VERIFY_NORMALIZABLE_ERROR has a higher priority then
776 * @NM_SETTING_VERIFY_NORMALIZABLE, so, if we encounter such an error type,
777 * we remember it instead (to return it as output).
779 verify_result = _nm_setting_verify (NM_SETTING (setting_i->data), all_settings, &verify_error);
780 if (verify_result == NM_SETTING_VERIFY_NORMALIZABLE ||
781 verify_result == NM_SETTING_VERIFY_NORMALIZABLE_ERROR) {
782 if ( verify_result == NM_SETTING_VERIFY_NORMALIZABLE_ERROR
783 && normalizable_error_type == NM_SETTING_VERIFY_NORMALIZABLE) {
784 /* NORMALIZABLE_ERROR has higher priority. */
785 g_clear_error (&normalizable_error);
787 if (!normalizable_error) {
788 g_propagate_error (&normalizable_error, verify_error);
790 normalizable_error_type = verify_result;
792 } else if (verify_result != NM_SETTING_VERIFY_SUCCESS) {
793 g_propagate_error (error, verify_error);
794 g_slist_free (all_settings);
795 g_return_val_if_fail (verify_result == NM_SETTING_VERIFY_ERROR, success);
798 g_clear_error (&verify_error);
800 g_slist_free (all_settings);
802 /* Now make sure the given 'type' setting can actually be the base setting
803 * of the connection. Can't have type=ppp for example.
805 ctype = nm_setting_connection_get_connection_type (s_con);
807 g_set_error_literal (error,
809 NM_CONNECTION_ERROR_CONNECTION_TYPE_INVALID,
810 "connection type missing");
814 base = nm_connection_get_setting_by_name (connection, ctype);
816 g_set_error_literal (error,
818 NM_CONNECTION_ERROR_CONNECTION_TYPE_INVALID,
819 "base setting GType not found");
823 if (!_nm_setting_is_base_type (base)) {
826 NM_CONNECTION_ERROR_CONNECTION_TYPE_INVALID,
827 "connection type '%s' is not a base type",
832 s_ip4 = nm_connection_get_setting_ip4_config (connection);
833 s_ip6 = nm_connection_get_setting_ip6_config (connection);
835 if (nm_setting_connection_get_master (s_con)) {
836 if ((normalizable_error_type == NM_SETTING_VERIFY_SUCCESS ||
837 (normalizable_error_type == NM_SETTING_VERIFY_NORMALIZABLE)) && (s_ip4 || s_ip6)) {
838 g_clear_error (&normalizable_error);
839 g_set_error (&normalizable_error,
841 NM_CONNECTION_ERROR_INVALID_SETTING,
842 "slave connection cannot have an IP%c setting",
844 /* having a slave with IP config *was* and is a verify() error. */
845 normalizable_error_type = NM_SETTING_VERIFY_NORMALIZABLE_ERROR;
848 if (normalizable_error_type == NM_SETTING_VERIFY_SUCCESS && (!s_ip4 || !s_ip6)) {
849 g_set_error (&normalizable_error,
851 NM_CONNECTION_ERROR_SETTING_NOT_FOUND,
852 "connection needs an IP%c setting",
854 /* having a master without IP config was not a verify() error, accept
855 * it for backward compatibility. */
856 normalizable_error_type = NM_SETTING_VERIFY_NORMALIZABLE;
860 if (normalizable_error_type != NM_SETTING_VERIFY_SUCCESS) {
861 g_propagate_error (error, normalizable_error);
862 normalizable_error = NULL;
863 success = normalizable_error_type;
865 success = NM_SETTING_VERIFY_SUCCESS;
868 g_clear_error (&normalizable_error);
873 * nm_connection_normalize:
874 * @connection: the #NMConnection to normalize
875 * @parameters: (allow-none) (element-type utf8 gpointer): a #GHashTable with
876 * normalization parameters to allow customization of the normalization by providing
877 * specific arguments. Unknown arguments will be ignored and the default will be
878 * used. The keys must be strings, hashed by g_str_hash() and g_str_equal() functions.
879 * The values are opaque and depend on the parameter name.
880 * @modified: (out) (allow-none): outputs whether any settings were modified.
881 * @error: location to store error, or %NULL. Contains the reason,
882 * why the connection is invalid, if the function returns an error.
884 * Does some basic normalization and fixup of well known inconsistencies
885 * and deprecated fields. If the connection was modified in any way,
886 * the output parameter @modified is set %TRUE.
888 * Finally the connection will be verified and %TRUE returns if the connection
889 * is valid. As this function only performs some specific normalization steps
890 * it cannot repair all connections. If the connection has errors that
891 * cannot be normalized, the connection will not be modified.
893 * Returns: %TRUE if the connection is valid, %FALSE if it is not
898 nm_connection_normalize (NMConnection *connection,
899 GHashTable *parameters,
903 NMSettingVerifyResult success;
904 gboolean was_modified = FALSE;
905 GError *normalizable_error = NULL;
907 success = _nm_connection_verify (connection, &normalizable_error);
909 if (success == NM_SETTING_VERIFY_ERROR ||
910 success == NM_SETTING_VERIFY_SUCCESS) {
911 if (normalizable_error)
912 g_propagate_error (error, normalizable_error);
915 g_assert (success == NM_SETTING_VERIFY_NORMALIZABLE || success == NM_SETTING_VERIFY_NORMALIZABLE_ERROR);
916 g_clear_error (&normalizable_error);
918 /* Try to perform all kind of normalizations on the settings to fix it.
919 * We only do this, after verifying that the connection contains no un-normalizable
920 * errors, because in that case we rather fail without touching the settings. */
922 was_modified |= _normalize_virtual_iface_name (connection);
923 was_modified |= _normalize_ip_config (connection, parameters);
926 success = _nm_connection_verify (connection, error);
928 /* we would expect, that after normalization, the connection can be verified. */
929 g_return_val_if_fail (success == NM_SETTING_VERIFY_SUCCESS, success);
931 /* we would expect, that the connection was modified during normalization. */
932 g_return_val_if_fail (was_modified, success);
936 *modified = was_modified;
938 return success == NM_SETTING_VERIFY_SUCCESS;
942 * nm_connection_update_secrets:
943 * @connection: the #NMConnection
944 * @setting_name: the setting object name to which the secrets apply
945 * @secrets: (element-type utf8 GObject.Value): a #GHashTable mapping
946 * string:#GValue of setting property names and secrets of the given @setting_name
947 * @error: location to store error, or %NULL
949 * Update the specified setting's secrets, given a hash table of secrets
950 * intended for that setting (deserialized from D-Bus for example). Will also
951 * extract the given setting's secrets hash if given a hash of hashes, as would
952 * be returned from nm_connection_to_hash(). If @setting_name is %NULL, expects
953 * a fully serialized #NMConnection as returned by nm_connection_to_hash() and
954 * will update all secrets from all settings contained in @secrets.
956 * Returns: %TRUE if the secrets were successfully updated, %FALSE if the update
957 * failed (tried to update secrets for a setting that doesn't exist, etc)
960 nm_connection_update_secrets (NMConnection *connection,
961 const char *setting_name,
966 gboolean success = TRUE, updated = FALSE;
967 GHashTable *setting_hash = NULL;
970 gboolean hashed_connection = FALSE;
973 g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
974 g_return_val_if_fail (secrets != NULL, FALSE);
976 g_return_val_if_fail (*error == NULL, FALSE);
978 /* Empty @secrets means success */
979 if (g_hash_table_size (secrets) == 0)
982 /* For backwards compatibility, this function accepts either a hashed
983 * connection (GHashTable of GHashTables of GValues) or a single hashed
984 * setting (GHashTable of GValues).
986 g_hash_table_iter_init (&iter, secrets);
987 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL)) {
988 if (_nm_setting_lookup_setting_type (key) != G_TYPE_INVALID) {
989 /* @secrets looks like a hashed connection */
990 hashed_connection = TRUE;
996 /* Update just one setting's secrets */
997 setting = nm_connection_get_setting_by_name (connection, setting_name);
999 g_set_error_literal (error,
1000 NM_CONNECTION_ERROR,
1001 NM_CONNECTION_ERROR_SETTING_NOT_FOUND,
1006 if (hashed_connection) {
1007 setting_hash = g_hash_table_lookup (secrets, setting_name);
1008 if (!setting_hash) {
1009 /* The hashed connection that didn't contain any secrets for
1010 * @setting_name; just return success.
1016 g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection);
1017 success_detail = _nm_setting_update_secrets (setting,
1018 setting_hash ? setting_hash : secrets,
1020 g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection);
1022 if (success_detail == NM_SETTING_UPDATE_SECRET_ERROR)
1024 if (success_detail == NM_SETTING_UPDATE_SECRET_SUCCESS_MODIFIED)
1027 if (!hashed_connection) {
1028 g_set_error_literal (error,
1029 NM_CONNECTION_ERROR,
1030 NM_CONNECTION_ERROR_SETTING_NOT_FOUND,
1035 /* check first, whether all the settings exist... */
1036 g_hash_table_iter_init (&iter, secrets);
1037 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL)) {
1038 setting = nm_connection_get_setting_by_name (connection, key);
1040 g_set_error_literal (error,
1041 NM_CONNECTION_ERROR,
1042 NM_CONNECTION_ERROR_SETTING_NOT_FOUND,
1048 /* Update each setting with any secrets from the hashed connection */
1049 g_hash_table_iter_init (&iter, secrets);
1050 while (g_hash_table_iter_next (&iter, (gpointer) &key, (gpointer) &setting_hash)) {
1051 /* Update the secrets for this setting */
1052 setting = nm_connection_get_setting_by_name (connection, key);
1054 g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection);
1055 success_detail = _nm_setting_update_secrets (setting, setting_hash, error);
1056 g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection);
1058 if (success_detail == NM_SETTING_UPDATE_SECRET_ERROR) {
1062 if (success_detail == NM_SETTING_UPDATE_SECRET_SUCCESS_MODIFIED)
1068 g_signal_emit (connection, signals[SECRETS_UPDATED], 0, setting_name);
1069 g_signal_emit (connection, signals[CHANGED], 0);
1076 * nm_connection_need_secrets:
1077 * @connection: the #NMConnection
1078 * @hints: (out) (element-type utf8) (allow-none) (transfer container):
1079 * the address of a pointer to a #GPtrArray, initialized to %NULL, which on
1080 * return points to an allocated #GPtrArray containing the property names of
1081 * secrets of the #NMSetting which may be required; the caller owns the array
1082 * and must free the array itself with g_ptr_array_free(), but not free its
1085 * Returns the name of the first setting object in the connection which would
1086 * need secrets to make a successful connection. The returned hints are only
1087 * intended as a guide to what secrets may be required, because in some
1088 * circumstances, there is no way to conclusively determine exactly which
1089 * secrets are needed.
1091 * Returns: the setting name of the #NMSetting object which has invalid or
1095 nm_connection_need_secrets (NMConnection *connection,
1098 NMConnectionPrivate *priv;
1099 GHashTableIter hiter;
1100 GSList *settings = NULL;
1102 const char *name = NULL;
1105 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1107 g_return_val_if_fail (*hints == NULL, NULL);
1109 priv = NM_CONNECTION_GET_PRIVATE (connection);
1111 /* Get list of settings in priority order */
1112 g_hash_table_iter_init (&hiter, priv->settings);
1113 while (g_hash_table_iter_next (&hiter, NULL, (gpointer) &setting))
1114 settings = g_slist_insert_sorted (settings, setting, _nm_setting_compare_priority);
1116 for (iter = settings; iter; iter = g_slist_next (iter)) {
1119 setting = NM_SETTING (iter->data);
1120 secrets = nm_setting_need_secrets (setting);
1125 g_ptr_array_free (secrets, TRUE);
1127 name = nm_setting_get_name (setting);
1132 g_slist_free (settings);
1137 * nm_connection_clear_secrets:
1138 * @connection: the #NMConnection
1140 * Clears and frees any secrets that may be stored in the connection, to avoid
1141 * keeping secret data in memory when not needed.
1144 nm_connection_clear_secrets (NMConnection *connection)
1146 GHashTableIter iter;
1148 gboolean changed = FALSE;
1150 g_return_if_fail (NM_IS_CONNECTION (connection));
1152 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1153 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting)) {
1154 g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection);
1155 changed |= _nm_setting_clear_secrets (setting);
1156 g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection);
1159 g_signal_emit (connection, signals[SECRETS_CLEARED], 0);
1161 g_signal_emit (connection, signals[CHANGED], 0);
1165 * nm_connection_clear_secrets_with_flags:
1166 * @connection: the #NMConnection
1167 * @func: (scope call): function to be called to determine whether a
1168 * specific secret should be cleared or not
1169 * @user_data: caller-supplied data passed to @func
1171 * Clears and frees secrets determined by @func.
1174 nm_connection_clear_secrets_with_flags (NMConnection *connection,
1175 NMSettingClearSecretsWithFlagsFn func,
1178 GHashTableIter iter;
1180 gboolean changed = FALSE;
1182 g_return_if_fail (NM_IS_CONNECTION (connection));
1184 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1185 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting)) {
1186 g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection);
1187 changed |= _nm_setting_clear_secrets_with_flags (setting, func, user_data);
1188 g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection);
1191 g_signal_emit (connection, signals[SECRETS_CLEARED], 0);
1193 g_signal_emit (connection, signals[CHANGED], 0);
1197 * nm_connection_to_hash:
1198 * @connection: the #NMConnection
1199 * @flags: hash flags, e.g. %NM_SETTING_HASH_FLAG_ALL
1201 * Converts the #NMConnection into a #GHashTable describing the connection,
1202 * suitable for marshalling over D-Bus or serializing. The hash table mapping
1203 * is string:#GHashTable with each element in the returned hash representing
1204 * a #NMSetting object. The keys are setting object names, and the values
1205 * are #GHashTables mapping string:GValue, each of which represents the
1206 * properties of the #NMSetting object.
1208 * Returns: (transfer full) (element-type utf8 GLib.HashTable): a new
1209 * #GHashTable describing the connection, its settings, and each setting's
1210 * properties. The caller owns the hash table and must unref the hash table
1211 * with g_hash_table_unref() when it is no longer needed.
1214 nm_connection_to_hash (NMConnection *connection, NMSettingHashFlags flags)
1216 NMConnectionPrivate *priv;
1217 GHashTableIter iter;
1219 GHashTable *ret, *setting_hash;
1221 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1223 ret = g_hash_table_new_full (g_str_hash, g_str_equal,
1224 g_free, (GDestroyNotify) g_hash_table_unref);
1226 priv = NM_CONNECTION_GET_PRIVATE (connection);
1228 /* Add each setting's hash to the main hash */
1229 g_hash_table_iter_init (&iter, priv->settings);
1230 while (g_hash_table_iter_next (&iter, &key, &data)) {
1231 NMSetting *setting = NM_SETTING (data);
1233 setting_hash = nm_setting_to_hash (setting, flags);
1235 g_hash_table_insert (ret, g_strdup (nm_setting_get_name (setting)), setting_hash);
1238 /* Don't send empty hashes */
1239 if (g_hash_table_size (ret) < 1) {
1240 g_hash_table_destroy (ret);
1248 * nm_connection_is_type:
1249 * @connection: the #NMConnection
1250 * @type: a setting name to check the connection's type against (like
1251 * %NM_SETTING_WIRELESS_SETTING_NAME or %NM_SETTING_WIRED_SETTING_NAME)
1253 * A convenience function to check if the given @connection is a particular
1254 * type (ie wired, Wi-Fi, ppp, etc). Checks the #NMSettingConnection:type
1255 * property of the connection and matches that against @type.
1257 * Returns: %TRUE if the connection is of the given @type, %FALSE if not
1260 nm_connection_is_type (NMConnection *connection, const char *type)
1262 NMSettingConnection *s_con;
1265 g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
1266 g_return_val_if_fail (type != NULL, FALSE);
1268 s_con = nm_connection_get_setting_connection (connection);
1272 type2 = nm_setting_connection_get_connection_type (s_con);
1274 return (g_strcmp0 (type2, type) == 0);
1278 * nm_connection_for_each_setting_value:
1279 * @connection: the #NMConnection
1280 * @func: (scope call): user-supplied function called for each setting's property
1281 * @user_data: user data passed to @func at each invocation
1283 * Iterates over the properties of each #NMSetting object in the #NMConnection,
1284 * calling the supplied user function for each property.
1287 nm_connection_for_each_setting_value (NMConnection *connection,
1288 NMSettingValueIterFn func,
1291 GHashTableIter iter;
1294 g_return_if_fail (NM_IS_CONNECTION (connection));
1295 g_return_if_fail (func != NULL);
1297 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1298 while (g_hash_table_iter_next (&iter, NULL, &value))
1299 nm_setting_enumerate_values (NM_SETTING (value), func, user_data);
1303 * nm_connection_dump:
1304 * @connection: the #NMConnection
1306 * Print the connection to stdout. For debugging purposes ONLY, should NOT
1307 * be used for serialization of the connection or machine-parsed in any way. The
1308 * output format is not guaranteed to be stable and may change at any time.
1311 nm_connection_dump (NMConnection *connection)
1313 GHashTableIter iter;
1315 const char *setting_name;
1321 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1322 while (g_hash_table_iter_next (&iter, (gpointer) &setting_name, (gpointer) &setting)) {
1323 str = nm_setting_to_string (setting);
1324 g_print ("%s\n", str);
1330 * nm_connection_set_path:
1331 * @connection: the #NMConnection
1332 * @path: the D-Bus path of the connection as given by the settings service
1333 * which provides the connection
1335 * Sets the D-Bus path of the connection. This property is not serialized, and
1336 * is only for the reference of the caller. Sets the #NMConnection:path
1340 nm_connection_set_path (NMConnection *connection, const char *path)
1342 NMConnectionPrivate *priv;
1344 g_return_if_fail (NM_IS_CONNECTION (connection));
1346 priv = NM_CONNECTION_GET_PRIVATE (connection);
1348 g_free (priv->path);
1352 priv->path = g_strdup (path);
1356 * nm_connection_get_path:
1357 * @connection: the #NMConnection
1359 * Returns the connection's D-Bus path.
1361 * Returns: the D-Bus path of the connection, previously set by a call to
1362 * nm_connection_set_path().
1365 nm_connection_get_path (NMConnection *connection)
1367 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1369 return NM_CONNECTION_GET_PRIVATE (connection)->path;
1373 * nm_connection_get_interface_name:
1374 * @connection: The #NMConnection
1376 * Returns the interface name as stored in NMSettingConnection:interface_name.
1377 * If the connection contains no NMSettingConnection, it will return %NULL.
1379 * For hardware devices and software devices created outside of NetworkManager,
1380 * this name is used to match the device. for software devices created by
1381 * NetworkManager, this is the name of the created interface.
1383 * Returns: Name of the kernel interface or %NULL
1388 nm_connection_get_interface_name (NMConnection *connection)
1390 NMSettingConnection *s_con;
1392 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1394 s_con = nm_connection_get_setting_connection (connection);
1396 return s_con ? nm_setting_connection_get_interface_name (s_con) : NULL;
1400 * nm_connection_get_virtual_iface_name:
1401 * @connection: The #NMConnection
1403 * Returns the name of the virtual kernel interface which the connection
1404 * needs to use if specified in the settings. This function abstracts all
1405 * connection types which require this functionality. For all other
1406 * connection types, this function will return %NULL.
1408 * Returns: Name of the kernel interface or %NULL
1411 nm_connection_get_virtual_iface_name (NMConnection *connection)
1413 NMSettingConnection *s_con;
1417 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1419 s_con = nm_connection_get_setting_connection (connection);
1420 g_return_val_if_fail (s_con, NULL);
1422 type = nm_setting_connection_get_connection_type (s_con);
1423 g_return_val_if_fail (type, NULL);
1425 base = nm_connection_get_setting_by_name (connection, type);
1429 return nm_setting_get_virtual_iface_name (base);
1433 * nm_connection_new:
1435 * Creates a new #NMConnection object with no #NMSetting objects.
1437 * Returns: the new empty #NMConnection object
1440 nm_connection_new (void)
1442 return (NMConnection *) g_object_new (NM_TYPE_CONNECTION, NULL);
1446 * nm_connection_new_from_hash:
1447 * @hash: (element-type utf8 GLib.HashTable): the #GHashTable describing
1449 * @error: on unsuccessful return, an error
1451 * Creates a new #NMConnection from a hash table describing the connection. See
1452 * nm_connection_to_hash() for a description of the expected hash table.
1454 * Returns: the new #NMConnection object, populated with settings created
1455 * from the values in the hash table, or %NULL if the connection failed to
1459 nm_connection_new_from_hash (GHashTable *hash, GError **error)
1461 NMConnection *connection;
1463 g_return_val_if_fail (hash != NULL, NULL);
1465 if (!validate_permissions_type (hash, error))
1468 connection = nm_connection_new ();
1469 if (!hash_to_connection (connection, hash, error)) {
1470 g_object_unref (connection);
1477 * nm_connection_duplicate:
1478 * @connection: the #NMConnection to duplicate
1480 * Duplicates a #NMConnection.
1482 * Returns: (transfer full): a new #NMConnection containing the same settings and properties
1483 * as the source #NMConnection
1486 nm_connection_duplicate (NMConnection *connection)
1489 GHashTableIter iter;
1492 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1494 dup = nm_connection_new ();
1495 nm_connection_set_path (dup, nm_connection_get_path (connection));
1497 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1498 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting))
1499 _nm_connection_add_setting (dup, nm_setting_duplicate (setting));
1505 * nm_connection_get_uuid:
1506 * @connection: the #NMConnection
1508 * A shortcut to return the UUID from the connection's #NMSettingConnection.
1510 * Returns: the UUID from the connection's 'connection' setting
1513 nm_connection_get_uuid (NMConnection *connection)
1515 NMSettingConnection *s_con;
1517 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1519 s_con = nm_connection_get_setting_connection (connection);
1520 g_return_val_if_fail (s_con != NULL, NULL);
1522 return nm_setting_connection_get_uuid (s_con);
1526 * nm_connection_get_id:
1527 * @connection: the #NMConnection
1529 * A shortcut to return the ID from the connection's #NMSettingConnection.
1531 * Returns: the ID from the connection's 'connection' setting
1534 nm_connection_get_id (NMConnection *connection)
1536 NMSettingConnection *s_con;
1538 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1540 s_con = nm_connection_get_setting_connection (connection);
1541 g_return_val_if_fail (s_con != NULL, NULL);
1543 return nm_setting_connection_get_id (s_con);
1547 * nm_connection_get_connection_type:
1548 * @connection: the #NMConnection
1550 * A shortcut to return the type from the connection's #NMSettingConnection.
1552 * Returns: the type from the connection's 'connection' setting
1557 nm_connection_get_connection_type (NMConnection *connection)
1559 NMSettingConnection *s_con;
1561 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1563 s_con = nm_connection_get_setting_connection (connection);
1564 g_return_val_if_fail (s_con != NULL, NULL);
1566 return nm_setting_connection_get_connection_type (s_con);
1570 * nm_connection_get_virtual_device_description:
1571 * @connection: an #NMConnection for a virtual device type
1573 * Returns the name that nm_device_disambiguate_names() would
1574 * return for the virtual device that would be created for @connection.
1575 * Eg, "VLAN (eth1.1)".
1577 * Returns: (transfer full): the name of @connection's device,
1578 * or %NULL if @connection is not a virtual connection type
1583 nm_connection_get_virtual_device_description (NMConnection *connection)
1585 const char *iface, *type, *display_type;
1586 NMSettingConnection *s_con;
1588 iface = nm_connection_get_virtual_iface_name (connection);
1592 s_con = nm_connection_get_setting_connection (connection);
1593 g_return_val_if_fail (s_con != NULL, NULL);
1594 type = nm_setting_connection_get_connection_type (s_con);
1596 if (!strcmp (type, NM_SETTING_BOND_SETTING_NAME))
1597 display_type = _("Bond");
1598 else if (!strcmp (type, NM_SETTING_TEAM_SETTING_NAME))
1599 display_type = _("Team");
1600 else if (!strcmp (type, NM_SETTING_BRIDGE_SETTING_NAME))
1601 display_type = _("Bridge");
1602 else if (!strcmp (type, NM_SETTING_VLAN_SETTING_NAME))
1603 display_type = _("VLAN");
1605 g_warning ("Unrecognized virtual device type '%s'", type);
1606 display_type = type;
1609 return g_strdup_printf ("%s (%s)", display_type, iface);
1612 /*************************************************************/
1615 * nm_connection_get_setting_802_1x:
1616 * @connection: the #NMConnection
1618 * A shortcut to return any #NMSetting8021x the connection might contain.
1620 * Returns: (transfer none): an #NMSetting8021x if the connection contains one, otherwise %NULL
1623 nm_connection_get_setting_802_1x (NMConnection *connection)
1625 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1627 return (NMSetting8021x *) nm_connection_get_setting (connection, NM_TYPE_SETTING_802_1X);
1631 * nm_connection_get_setting_bluetooth:
1632 * @connection: the #NMConnection
1634 * A shortcut to return any #NMSettingBluetooth the connection might contain.
1636 * Returns: (transfer none): an #NMSettingBluetooth if the connection contains one, otherwise %NULL
1638 NMSettingBluetooth *
1639 nm_connection_get_setting_bluetooth (NMConnection *connection)
1641 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1643 return (NMSettingBluetooth *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BLUETOOTH);
1647 * nm_connection_get_setting_bond:
1648 * @connection: the #NMConnection
1650 * A shortcut to return any #NMSettingBond the connection might contain.
1652 * Returns: (transfer none): an #NMSettingBond if the connection contains one, otherwise %NULL
1655 nm_connection_get_setting_bond (NMConnection *connection)
1657 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1659 return (NMSettingBond *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BOND);
1663 * nm_connection_get_setting_team:
1664 * @connection: the #NMConnection
1666 * A shortcut to return any #NMSettingTeam the connection might contain.
1668 * Returns: (transfer none): an #NMSettingTeam if the connection contains one, otherwise %NULL
1673 nm_connection_get_setting_team (NMConnection *connection)
1675 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1677 return (NMSettingTeam *) nm_connection_get_setting (connection, NM_TYPE_SETTING_TEAM);
1681 * nm_connection_get_setting_team_port:
1682 * @connection: the #NMConnection
1684 * A shortcut to return any #NMSettingTeamPort the connection might contain.
1686 * Returns: (transfer none): an #NMSettingTeamPort if the connection contains one, otherwise %NULL
1691 nm_connection_get_setting_team_port (NMConnection *connection)
1693 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1695 return (NMSettingTeamPort *) nm_connection_get_setting (connection, NM_TYPE_SETTING_TEAM_PORT);
1699 * nm_connection_get_setting_bridge:
1700 * @connection: the #NMConnection
1702 * A shortcut to return any #NMSettingBridge the connection might contain.
1704 * Returns: (transfer none): an #NMSettingBridge if the connection contains one, otherwise %NULL
1707 nm_connection_get_setting_bridge (NMConnection *connection)
1709 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1711 return (NMSettingBridge *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BRIDGE);
1715 * nm_connection_get_setting_cdma:
1716 * @connection: the #NMConnection
1718 * A shortcut to return any #NMSettingCdma the connection might contain.
1720 * Returns: (transfer none): an #NMSettingCdma if the connection contains one, otherwise %NULL
1723 nm_connection_get_setting_cdma (NMConnection *connection)
1725 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1727 return (NMSettingCdma *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CDMA);
1731 * nm_connection_get_setting_connection:
1732 * @connection: the #NMConnection
1734 * A shortcut to return any #NMSettingConnection the connection might contain.
1736 * Returns: (transfer none): an #NMSettingConnection if the connection contains one, otherwise %NULL
1738 NMSettingConnection *
1739 nm_connection_get_setting_connection (NMConnection *connection)
1741 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1743 return (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
1747 * nm_connection_get_setting_dcb:
1748 * @connection: the #NMConnection
1750 * A shortcut to return any #NMSettingDcb the connection might contain.
1752 * Returns: (transfer none): an #NMSettingDcb if the connection contains one, otherwise NULL
1757 nm_connection_get_setting_dcb (NMConnection *connection)
1759 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1761 return (NMSettingDcb *) nm_connection_get_setting (connection, NM_TYPE_SETTING_DCB);
1765 * nm_connection_get_setting_generic:
1766 * @connection: the #NMConnection
1768 * A shortcut to return any #NMSettingGeneric the connection might contain.
1770 * Returns: (transfer none): an #NMSettingGeneric if the connection contains one, otherwise NULL
1775 nm_connection_get_setting_generic (NMConnection *connection)
1777 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1779 return (NMSettingGeneric *) nm_connection_get_setting (connection, NM_TYPE_SETTING_GENERIC);
1783 * nm_connection_get_setting_gsm:
1784 * @connection: the #NMConnection
1786 * A shortcut to return any #NMSettingGsm the connection might contain.
1788 * Returns: (transfer none): an #NMSettingGsm if the connection contains one, otherwise %NULL
1791 nm_connection_get_setting_gsm (NMConnection *connection)
1793 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1795 return (NMSettingGsm *) nm_connection_get_setting (connection, NM_TYPE_SETTING_GSM);
1799 * nm_connection_get_setting_infiniband:
1800 * @connection: the #NMConnection
1802 * A shortcut to return any #NMSettingInfiniband the connection might contain.
1804 * Returns: (transfer none): an #NMSettingInfiniband if the connection contains one, otherwise %NULL
1806 NMSettingInfiniband *
1807 nm_connection_get_setting_infiniband (NMConnection *connection)
1809 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1811 return (NMSettingInfiniband *) nm_connection_get_setting (connection, NM_TYPE_SETTING_INFINIBAND);
1815 * nm_connection_get_setting_ip4_config:
1816 * @connection: the #NMConnection
1818 * A shortcut to return any #NMSettingIP4Config the connection might contain.
1820 * Returns: (transfer none): an #NMSettingIP4Config if the connection contains one, otherwise %NULL
1822 NMSettingIP4Config *
1823 nm_connection_get_setting_ip4_config (NMConnection *connection)
1825 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1827 return (NMSettingIP4Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG);
1831 * nm_connection_get_setting_ip6_config:
1832 * @connection: the #NMConnection
1834 * A shortcut to return any #NMSettingIP6Config the connection might contain.
1836 * Returns: (transfer none): an #NMSettingIP6Config if the connection contains one, otherwise %NULL
1838 NMSettingIP6Config *
1839 nm_connection_get_setting_ip6_config (NMConnection *connection)
1841 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1843 return (NMSettingIP6Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP6_CONFIG);
1847 * nm_connection_get_setting_olpc_mesh:
1848 * @connection: the #NMConnection
1850 * A shortcut to return any #NMSettingOlpcMesh the connection might contain.
1852 * Returns: (transfer none): an #NMSettingOlpcMesh if the connection contains one, otherwise %NULL
1855 nm_connection_get_setting_olpc_mesh (NMConnection *connection)
1857 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1859 return (NMSettingOlpcMesh *) nm_connection_get_setting (connection, NM_TYPE_SETTING_OLPC_MESH);
1863 * nm_connection_get_setting_ppp:
1864 * @connection: the #NMConnection
1866 * A shortcut to return any #NMSettingPPP the connection might contain.
1868 * Returns: (transfer none): an #NMSettingPPP if the connection contains one, otherwise %NULL
1871 nm_connection_get_setting_ppp (NMConnection *connection)
1873 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1875 return (NMSettingPPP *) nm_connection_get_setting (connection, NM_TYPE_SETTING_PPP);
1879 * nm_connection_get_setting_pppoe:
1880 * @connection: the #NMConnection
1882 * A shortcut to return any #NMSettingPPPOE the connection might contain.
1884 * Returns: (transfer none): an #NMSettingPPPOE if the connection contains one, otherwise %NULL
1887 nm_connection_get_setting_pppoe (NMConnection *connection)
1889 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1891 return (NMSettingPPPOE *) nm_connection_get_setting (connection, NM_TYPE_SETTING_PPPOE);
1895 * nm_connection_get_setting_serial:
1896 * @connection: the #NMConnection
1898 * A shortcut to return any #NMSettingSerial the connection might contain.
1900 * Returns: (transfer none): an #NMSettingSerial if the connection contains one, otherwise %NULL
1903 nm_connection_get_setting_serial (NMConnection *connection)
1905 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1907 return (NMSettingSerial *) nm_connection_get_setting (connection, NM_TYPE_SETTING_SERIAL);
1911 * nm_connection_get_setting_vpn:
1912 * @connection: the #NMConnection
1914 * A shortcut to return any #NMSettingVPN the connection might contain.
1916 * Returns: (transfer none): an #NMSettingVPN if the connection contains one, otherwise %NULL
1919 nm_connection_get_setting_vpn (NMConnection *connection)
1921 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1923 return (NMSettingVPN *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VPN);
1927 * nm_connection_get_setting_wimax:
1928 * @connection: the #NMConnection
1930 * A shortcut to return any #NMSettingWimax the connection might contain.
1932 * Returns: (transfer none): an #NMSettingWimax if the connection contains one, otherwise %NULL
1935 nm_connection_get_setting_wimax (NMConnection *connection)
1937 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1939 return (NMSettingWimax *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIMAX);
1943 * nm_connection_get_setting_wired:
1944 * @connection: the #NMConnection
1946 * A shortcut to return any #NMSettingWired the connection might contain.
1948 * Returns: (transfer none): an #NMSettingWired if the connection contains one, otherwise %NULL
1951 nm_connection_get_setting_wired (NMConnection *connection)
1953 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1955 return (NMSettingWired *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRED);
1959 * nm_connection_get_setting_adsl:
1960 * @connection: the #NMConnection
1962 * A shortcut to return any #NMSettingAdsl the connection might contain.
1964 * Returns: (transfer none): an #NMSettingAdsl if the connection contains one, otherwise %NULL
1967 nm_connection_get_setting_adsl (NMConnection *connection)
1969 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1971 return (NMSettingAdsl *) nm_connection_get_setting (connection, NM_TYPE_SETTING_ADSL);
1975 * nm_connection_get_setting_wireless:
1976 * @connection: the #NMConnection
1978 * A shortcut to return any #NMSettingWireless the connection might contain.
1980 * Returns: (transfer none): an #NMSettingWireless if the connection contains one, otherwise %NULL
1983 nm_connection_get_setting_wireless (NMConnection *connection)
1985 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1987 return (NMSettingWireless *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS);
1991 * nm_connection_get_setting_wireless_security:
1992 * @connection: the #NMConnection
1994 * A shortcut to return any #NMSettingWirelessSecurity the connection might contain.
1996 * Returns: (transfer none): an #NMSettingWirelessSecurity if the connection contains one, otherwise %NULL
1998 NMSettingWirelessSecurity *
1999 nm_connection_get_setting_wireless_security (NMConnection *connection)
2001 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2003 return (NMSettingWirelessSecurity *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS_SECURITY);
2007 * nm_connection_get_setting_bridge_port:
2008 * @connection: the #NMConnection
2010 * A shortcut to return any #NMSettingBridgePort the connection might contain.
2012 * Returns: (transfer none): an #NMSettingBridgePort if the connection contains one, otherwise %NULL
2014 NMSettingBridgePort *
2015 nm_connection_get_setting_bridge_port (NMConnection *connection)
2017 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2019 return (NMSettingBridgePort *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BRIDGE_PORT);
2023 * nm_connection_get_setting_vlan:
2024 * @connection: the #NMConnection
2026 * A shortcut to return any #NMSettingVlan the connection might contain.
2028 * Returns: (transfer none): an #NMSettingVlan if the connection contains one, otherwise %NULL
2031 nm_connection_get_setting_vlan (NMConnection *connection)
2033 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2035 return (NMSettingVlan *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VLAN);
2038 /*************************************************************/
2041 nm_connection_init (NMConnection *connection)
2043 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (connection);
2045 priv->settings = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_object_unref);
2049 dispose (GObject *object)
2051 NMConnection *self = NM_CONNECTION (object);
2052 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (self);
2054 g_hash_table_foreach_remove (priv->settings, _setting_release, self);
2056 G_OBJECT_CLASS (nm_connection_parent_class)->dispose (object);
2060 finalize (GObject *object)
2062 NMConnection *connection = NM_CONNECTION (object);
2063 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (connection);
2065 g_assert (g_hash_table_size (priv->settings) == 0);
2066 g_hash_table_destroy (priv->settings);
2067 g_free (priv->path);
2069 G_OBJECT_CLASS (nm_connection_parent_class)->finalize (object);
2073 set_property (GObject *object, guint prop_id,
2074 const GValue *value, GParamSpec *pspec)
2076 NMConnection *connection = NM_CONNECTION (object);
2080 nm_connection_set_path (connection, g_value_get_string (value));
2083 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2089 get_property (GObject *object, guint prop_id,
2090 GValue *value, GParamSpec *pspec)
2092 NMConnection *connection = NM_CONNECTION (object);
2096 g_value_set_string (value, nm_connection_get_path (connection));
2099 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2105 nm_connection_class_init (NMConnectionClass *klass)
2107 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2109 g_type_class_add_private (klass, sizeof (NMConnectionPrivate));
2111 /* virtual methods */
2112 object_class->set_property = set_property;
2113 object_class->get_property = get_property;
2114 object_class->dispose = dispose;
2115 object_class->finalize = finalize;
2120 * NMConnection:path:
2122 * The connection's D-Bus path, used only by the calling process as a record
2123 * of the D-Bus path of the connection as provided by a settings service.
2125 g_object_class_install_property
2126 (object_class, PROP_PATH,
2127 g_param_spec_string (NM_CONNECTION_PATH, "", "",
2131 G_PARAM_STATIC_STRINGS));
2136 * NMConnection::secrets-updated:
2137 * @connection: the object on which the signal is emitted
2138 * @setting_name: the setting name of the #NMSetting for which secrets were
2141 * The ::secrets-updated signal is emitted when the secrets of a setting
2142 * have been changed.
2144 signals[SECRETS_UPDATED] =
2145 g_signal_new (NM_CONNECTION_SECRETS_UPDATED,
2146 G_OBJECT_CLASS_TYPE (object_class),
2148 G_STRUCT_OFFSET (NMConnectionClass, secrets_updated),
2150 g_cclosure_marshal_VOID__STRING,
2155 * NMConnection::secrets-cleared:
2156 * @connection: the object on which the signal is emitted
2158 * The ::secrets-cleared signal is emitted when the secrets of a connection
2161 signals[SECRETS_CLEARED] =
2162 g_signal_new (NM_CONNECTION_SECRETS_CLEARED,
2163 G_OBJECT_CLASS_TYPE (object_class),
2166 g_cclosure_marshal_VOID__VOID,
2170 * NMConnection::changed:
2171 * @connection: the object on which the signal is emitted
2173 * The ::changed signal is emitted when any property of any property
2174 * (including secrets) of any setting of the connection is modified,
2175 * or when settings are added or removed.
2180 g_signal_new (NM_CONNECTION_CHANGED,
2181 G_OBJECT_CLASS_TYPE (object_class),
2184 g_cclosure_marshal_VOID__VOID,