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)
334 const char *setting_name;
335 GHashTable *setting_hash;
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);
357 g_signal_emit (connection, signals[CHANGED], 0);
361 * nm_connection_replace_settings:
362 * @connection: a #NMConnection
363 * @new_settings: (element-type utf8 GLib.HashTable): a #GHashTable of settings
364 * @error: location to store error, or %NULL
366 * Returns: %TRUE if the settings were valid and added to the connection, %FALSE
370 nm_connection_replace_settings (NMConnection *connection,
371 GHashTable *new_settings,
374 g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
375 g_return_val_if_fail (new_settings != NULL, FALSE);
377 g_return_val_if_fail (*error == NULL, FALSE);
379 if (!validate_permissions_type (new_settings, error))
382 hash_to_connection (connection, new_settings);
383 return nm_connection_verify (connection, error);
387 * nm_connection_replace_settings_from_connection:
388 * @connection: a #NMConnection
389 * @new_connection: a #NMConnection to replace the settings of @connection with
390 * @error: location to store error, or %NULL
392 * Deep-copies the settings of @new_conenction and replaces the settings of @connection
393 * with the copied settings.
395 * Returns: %TRUE if the settings were valid after replacing the connection, %FALSE
396 * if they were not. Regardless of whether %TRUE or %FALSE is returned, the connection
397 * is successfully replaced. %FALSE only means, that the connection does not verify
398 * at the end of the operation.
403 nm_connection_replace_settings_from_connection (NMConnection *connection,
404 NMConnection *new_connection,
407 NMConnectionPrivate *priv;
410 gboolean changed = FALSE;
413 g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
414 g_return_val_if_fail (NM_IS_CONNECTION (new_connection), FALSE);
415 g_return_val_if_fail (!error || !*error, FALSE);
417 /* When 'connection' and 'new_connection' are the same object simply return
418 * in order not to destroy 'connection' */
419 if (connection == new_connection)
422 /* No need to validate permissions like nm_connection_replace_settings()
423 * since we're dealing with an NMConnection which has already done that.
426 priv = NM_CONNECTION_GET_PRIVATE (connection);
427 if ((changed = g_hash_table_size (priv->settings) > 0))
428 g_hash_table_foreach_remove (priv->settings, _setting_release, connection);
430 if (g_hash_table_size (NM_CONNECTION_GET_PRIVATE (new_connection)->settings)) {
431 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (new_connection)->settings);
432 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting))
433 _nm_connection_add_setting (connection, nm_setting_duplicate (setting));
438 valid = nm_connection_verify (connection, error);
440 g_signal_emit (connection, signals[CHANGED], 0);
445 * nm_connection_compare:
446 * @a: a #NMConnection
447 * @b: a second #NMConnection to compare with the first
448 * @flags: compare flags, e.g. %NM_SETTING_COMPARE_FLAG_EXACT
450 * Compares two #NMConnection objects for similarity, with comparison behavior
451 * modified by a set of flags. See nm_setting_compare() for a description of
452 * each flag's behavior.
454 * Returns: %TRUE if the comparison succeeds, %FALSE if it does not
457 nm_connection_compare (NMConnection *a,
459 NMSettingCompareFlags flags)
469 /* B / A: ensure settings in B that are not in A make the comparison fail */
470 if (g_hash_table_size (NM_CONNECTION_GET_PRIVATE (a)->settings) !=
471 g_hash_table_size (NM_CONNECTION_GET_PRIVATE (b)->settings))
474 /* A / B: ensure all settings in A match corresponding ones in B */
475 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (a)->settings);
476 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &src)) {
477 NMSetting *cmp = nm_connection_get_setting (b, G_OBJECT_TYPE (src));
479 if (!cmp || !nm_setting_compare (src, cmp, flags))
488 diff_one_connection (NMConnection *a,
490 NMSettingCompareFlags flags,
491 gboolean invert_results,
494 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (a);
496 NMSetting *a_setting = NULL;
498 g_hash_table_iter_init (&iter, priv->settings);
499 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &a_setting)) {
500 NMSetting *b_setting = NULL;
501 const char *setting_name = nm_setting_get_name (a_setting);
503 gboolean new_results = TRUE;
506 b_setting = nm_connection_get_setting (b, G_OBJECT_TYPE (a_setting));
508 results = g_hash_table_lookup (diffs, setting_name);
512 if (!nm_setting_diff (a_setting, b_setting, flags, invert_results, &results)) {
514 g_hash_table_insert (diffs, g_strdup (setting_name), results);
520 * nm_connection_diff:
521 * @a: a #NMConnection
522 * @b: a second #NMConnection to compare with the first
523 * @flags: compare flags, e.g. %NM_SETTING_COMPARE_FLAG_EXACT
524 * @out_settings: (element-type utf8 GLib.HashTable): if the
525 * connections differ, on return a hash table mapping setting names to
526 * second-level GHashTable (utf8 to guint32), which contains the key names that
527 * differ mapped to one or more of %NMSettingDiffResult as a bitfield
529 * Compares two #NMConnection objects for similarity, with comparison behavior
530 * modified by a set of flags. See nm_setting_compare() for a description of
531 * each flag's behavior. If the connections differ, settings and keys within
532 * each setting that differ are added to the returned @out_settings hash table.
533 * No values are returned, only key names.
535 * Returns: %TRUE if the connections contain the same values, %FALSE if they do
539 nm_connection_diff (NMConnection *a,
541 NMSettingCompareFlags flags,
542 GHashTable **out_settings)
546 g_return_val_if_fail (NM_IS_CONNECTION (a), FALSE);
547 g_return_val_if_fail (out_settings != NULL, FALSE);
548 g_return_val_if_fail (*out_settings == NULL, FALSE);
550 g_return_val_if_fail (NM_IS_CONNECTION (b), FALSE);
555 diffs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_destroy);
557 /* Diff A to B, then B to A to capture keys in B that aren't in A */
558 diff_one_connection (a, b, flags, FALSE, diffs);
560 diff_one_connection (b, a, flags, TRUE, diffs);
562 if (g_hash_table_size (diffs) == 0)
563 g_hash_table_destroy (diffs);
565 *out_settings = diffs;
567 return *out_settings ? FALSE : TRUE;
571 _normalize_virtual_iface_name (NMConnection *self)
573 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (self);
574 GHashTableIter h_iter;
576 NMSettingConnection *s_con;
577 const char *interface_name;
578 char *virtual_iface_name = NULL;
579 gboolean was_modified = FALSE;
580 const char *prop_name = NULL;
582 /* search for settings that might need normalization of the interface name. */
583 g_hash_table_iter_init (&h_iter, priv->settings);
585 && g_hash_table_iter_next (&h_iter, NULL, (void **) &setting)) {
586 if (NM_IS_SETTING_BOND (setting))
587 prop_name = NM_SETTING_BOND_INTERFACE_NAME;
588 else if (NM_IS_SETTING_BRIDGE (setting))
589 prop_name = NM_SETTING_BRIDGE_INTERFACE_NAME;
590 else if (NM_IS_SETTING_TEAM (setting))
591 prop_name = NM_SETTING_TEAM_INTERFACE_NAME;
592 else if (NM_IS_SETTING_VLAN (setting))
593 prop_name = NM_SETTING_VLAN_INTERFACE_NAME;
598 s_con = nm_connection_get_setting_connection (self);
599 g_return_val_if_fail (s_con, FALSE);
601 interface_name = nm_setting_connection_get_interface_name (s_con);
603 /* read the potential virtual_iface_name from the setting. */
604 g_object_get (setting, prop_name, &virtual_iface_name, NULL);
606 if (g_strcmp0 (interface_name, virtual_iface_name) != 0) {
607 if (interface_name) {
608 /* interface_name is set and overwrites the virtual_iface_name. */
609 g_object_set (setting, prop_name, interface_name, NULL);
611 /* interface in NMSettingConnection must be set. */
612 g_object_set (s_con, NM_SETTING_CONNECTION_INTERFACE_NAME, virtual_iface_name, NULL);
617 g_free (virtual_iface_name);
623 _normalize_ip_config (NMConnection *self, GHashTable *parameters)
625 NMSettingConnection *s_con = nm_connection_get_setting_connection (self);
626 const char *default_ip4_method = NM_SETTING_IP4_CONFIG_METHOD_AUTO;
627 const char *default_ip6_method = NULL;
628 NMSettingIP4Config *s_ip4;
629 NMSettingIP6Config *s_ip6;
633 default_ip6_method = g_hash_table_lookup (parameters, NM_CONNECTION_NORMALIZE_PARAM_IP6_CONFIG_METHOD);
634 if (!default_ip6_method)
635 default_ip6_method = NM_SETTING_IP6_CONFIG_METHOD_AUTO;
637 s_ip4 = nm_connection_get_setting_ip4_config (self);
638 s_ip6 = nm_connection_get_setting_ip6_config (self);
640 if (nm_setting_connection_get_master (s_con)) {
641 /* Slave connections don't have IP configuration. */
644 nm_connection_remove_setting (self, NM_TYPE_SETTING_IP4_CONFIG);
647 nm_connection_remove_setting (self, NM_TYPE_SETTING_IP6_CONFIG);
649 return s_ip4 || s_ip6;
651 /* Ensure all non-slave connections have IP4 and IP6 settings objects. If no
652 * IP6 setting was specified, then assume that means IP6 config is allowed
653 * to fail. But if no IP4 setting was specified, assume the caller was just
657 setting = nm_setting_ip4_config_new ();
659 g_object_set (setting,
660 NM_SETTING_IP4_CONFIG_METHOD, default_ip4_method,
662 nm_connection_add_setting (self, setting);
665 setting = nm_setting_ip6_config_new ();
667 g_object_set (setting,
668 NM_SETTING_IP6_CONFIG_METHOD, default_ip6_method,
669 NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE,
671 nm_connection_add_setting (self, setting);
673 return !s_ip4 || !s_ip6;
678 * nm_connection_verify:
679 * @connection: the #NMConnection to verify
680 * @error: location to store error, or %NULL
682 * Validates the connection and all its settings. Each setting's properties
683 * have allowed values, and some values are dependent on other values. For
684 * example, if a Wi-Fi connection is security enabled, the #NMSettingWireless
685 * setting object's 'security' property must contain the setting name of the
686 * #NMSettingWirelessSecurity object, which must also be present in the
687 * connection for the connection to be valid. As another example, the
688 * #NMSettingWired object's 'mac-address' property must be a validly formatted
689 * MAC address. The returned #GError contains information about which
690 * setting and which property failed validation, and how it failed validation.
692 * Returns: %TRUE if the connection is valid, %FALSE if it is not
695 nm_connection_verify (NMConnection *connection, GError **error)
697 NMSettingVerifyResult result;
699 result = _nm_connection_verify (connection, error);
701 /* we treat normalizable connections as valid. */
702 if (result == NM_SETTING_VERIFY_NORMALIZABLE)
703 g_clear_error (error);
705 return result == NM_SETTING_VERIFY_SUCCESS || result == NM_SETTING_VERIFY_NORMALIZABLE;
708 static NMSettingVerifyResult
709 _nm_connection_verify (NMConnection *connection, GError **error)
711 NMConnectionPrivate *priv;
712 NMSettingConnection *s_con;
713 NMSettingIP4Config *s_ip4;
714 NMSettingIP6Config *s_ip6;
717 GSList *all_settings = NULL, *setting_i;
718 NMSettingVerifyResult success = NM_SETTING_VERIFY_ERROR;
721 GError *normalizable_error = NULL;
722 NMSettingVerifyResult normalizable_error_type = NM_SETTING_VERIFY_SUCCESS;
725 g_return_val_if_fail (*error == NULL, NM_SETTING_VERIFY_ERROR);
727 if (!NM_IS_CONNECTION (connection)) {
728 g_set_error_literal (error,
729 NM_SETTING_CONNECTION_ERROR,
730 NM_SETTING_CONNECTION_ERROR_UNKNOWN,
731 "invalid connection; failed verification");
732 g_return_val_if_fail (NM_IS_CONNECTION (connection), NM_SETTING_VERIFY_ERROR);
735 priv = NM_CONNECTION_GET_PRIVATE (connection);
737 /* First, make sure there's at least 'connection' setting */
738 s_con = nm_connection_get_setting_connection (connection);
740 g_set_error_literal (error,
742 NM_CONNECTION_ERROR_CONNECTION_SETTING_NOT_FOUND,
743 "connection setting not found");
747 /* Build up the list of settings */
748 g_hash_table_iter_init (&iter, priv->settings);
749 while (g_hash_table_iter_next (&iter, NULL, &value)) {
750 /* Order NMSettingConnection so that it will be verified first.
751 * The reason is, that NMSettingConnection:verify() modifies the connection
752 * by setting NMSettingConnection:interface_name. So we want to call that
753 * verify() first, because the order can affect the outcome.
754 * Another reason is, that errors in this setting might be more fundamental
755 * and should be checked and reported with higher priority.
756 * Another reason is, that some settings look especially at the
757 * NMSettingConnection, so they find it first in the all_settings list. */
759 all_settings = g_slist_append (all_settings, value);
761 all_settings = g_slist_prepend (all_settings, value);
763 all_settings = g_slist_reverse (all_settings);
765 /* Now, run the verify function of each setting */
766 for (setting_i = all_settings; setting_i; setting_i = setting_i->next) {
767 GError *verify_error = NULL;
768 NMSettingVerifyResult verify_result;
770 /* verify all settings. We stop if we find the first non-normalizable
771 * @NM_SETTING_VERIFY_ERROR. If we find normalizable errors we continue
772 * but remember the error to return it to the user.
773 * @NM_SETTING_VERIFY_NORMALIZABLE_ERROR has a higher priority then
774 * @NM_SETTING_VERIFY_NORMALIZABLE, so, if we encounter such an error type,
775 * we remember it instead (to return it as output).
777 verify_result = _nm_setting_verify (NM_SETTING (setting_i->data), all_settings, &verify_error);
778 if (verify_result == NM_SETTING_VERIFY_NORMALIZABLE ||
779 verify_result == NM_SETTING_VERIFY_NORMALIZABLE_ERROR) {
780 if ( verify_result == NM_SETTING_VERIFY_NORMALIZABLE_ERROR
781 && normalizable_error_type == NM_SETTING_VERIFY_NORMALIZABLE) {
782 /* NORMALIZABLE_ERROR has higher priority. */
783 g_clear_error (&normalizable_error);
785 if (!normalizable_error) {
786 g_propagate_error (&normalizable_error, verify_error);
788 normalizable_error_type = verify_result;
790 } else if (verify_result != NM_SETTING_VERIFY_SUCCESS) {
791 g_propagate_error (error, verify_error);
792 g_slist_free (all_settings);
793 g_return_val_if_fail (verify_result == NM_SETTING_VERIFY_ERROR, success);
796 g_clear_error (&verify_error);
798 g_slist_free (all_settings);
800 /* Now make sure the given 'type' setting can actually be the base setting
801 * of the connection. Can't have type=ppp for example.
803 ctype = nm_setting_connection_get_connection_type (s_con);
805 g_set_error_literal (error,
807 NM_CONNECTION_ERROR_CONNECTION_TYPE_INVALID,
808 "connection type missing");
812 base = nm_connection_get_setting_by_name (connection, ctype);
814 g_set_error_literal (error,
816 NM_CONNECTION_ERROR_CONNECTION_TYPE_INVALID,
817 "base setting GType not found");
821 if (!_nm_setting_is_base_type (base)) {
824 NM_CONNECTION_ERROR_CONNECTION_TYPE_INVALID,
825 "connection type '%s' is not a base type",
830 s_ip4 = nm_connection_get_setting_ip4_config (connection);
831 s_ip6 = nm_connection_get_setting_ip6_config (connection);
833 if (nm_setting_connection_get_master (s_con)) {
834 if ((normalizable_error_type == NM_SETTING_VERIFY_SUCCESS ||
835 (normalizable_error_type == NM_SETTING_VERIFY_NORMALIZABLE)) && (s_ip4 || s_ip6)) {
836 g_clear_error (&normalizable_error);
837 g_set_error (&normalizable_error,
839 NM_CONNECTION_ERROR_INVALID_SETTING,
840 "slave connection cannot have an IP%c setting",
842 /* having a slave with IP config *was* and is a verify() error. */
843 normalizable_error_type = NM_SETTING_VERIFY_NORMALIZABLE_ERROR;
846 if (normalizable_error_type == NM_SETTING_VERIFY_SUCCESS && (!s_ip4 || !s_ip6)) {
847 g_set_error (&normalizable_error,
849 NM_CONNECTION_ERROR_SETTING_NOT_FOUND,
850 "connection needs an IP%c setting",
852 /* having a master without IP config was not a verify() error, accept
853 * it for backward compatibility. */
854 normalizable_error_type = NM_SETTING_VERIFY_NORMALIZABLE;
858 if (normalizable_error_type != NM_SETTING_VERIFY_SUCCESS) {
859 g_propagate_error (error, normalizable_error);
860 normalizable_error = NULL;
861 success = normalizable_error_type;
863 success = NM_SETTING_VERIFY_SUCCESS;
866 g_clear_error (&normalizable_error);
871 * nm_connection_normalize:
872 * @connection: the #NMConnection to normalize
873 * @parameters: (allow-none) (element-type utf8 gpointer): a #GHashTable with
874 * normalization parameters to allow customization of the normalization by providing
875 * specific arguments. Unknown arguments will be ignored and the default will be
876 * used. The keys must be strings, hashed by g_str_hash() and g_str_equal() functions.
877 * The values are opaque and depend on the parameter name.
878 * @modified: (out) (allow-none): outputs whether any settings were modified.
879 * @error: location to store error, or %NULL. Contains the reason,
880 * why the connection is invalid, if the function returns an error.
882 * Does some basic normalization and fixup of well known inconsistencies
883 * and deprecated fields. If the connection was modified in any way,
884 * the output parameter @modified is set %TRUE.
886 * Finally the connection will be verified and %TRUE returns if the connection
887 * is valid. As this function only performs some specific normalization steps
888 * it cannot repair all connections. If the connection has errors that
889 * cannot be normalized, the connection will not be modified.
891 * Returns: %TRUE if the connection is valid, %FALSE if it is not
896 nm_connection_normalize (NMConnection *connection,
897 GHashTable *parameters,
901 NMSettingVerifyResult success;
902 gboolean was_modified = FALSE;
903 GError *normalizable_error = NULL;
905 success = _nm_connection_verify (connection, &normalizable_error);
907 if (success == NM_SETTING_VERIFY_ERROR ||
908 success == NM_SETTING_VERIFY_SUCCESS) {
909 if (normalizable_error)
910 g_propagate_error (error, normalizable_error);
913 g_assert (success == NM_SETTING_VERIFY_NORMALIZABLE || success == NM_SETTING_VERIFY_NORMALIZABLE_ERROR);
914 g_clear_error (&normalizable_error);
916 /* Try to perform all kind of normalizations on the settings to fix it.
917 * We only do this, after verifying that the connection contains no un-normalizable
918 * errors, because in that case we rather fail without touching the settings. */
920 was_modified |= _normalize_virtual_iface_name (connection);
921 was_modified |= _normalize_ip_config (connection, parameters);
924 success = _nm_connection_verify (connection, error);
926 /* we would expect, that after normalization, the connection can be verified. */
927 g_return_val_if_fail (success == NM_SETTING_VERIFY_SUCCESS, success);
929 /* we would expect, that the connection was modified during normalization. */
930 g_return_val_if_fail (was_modified, success);
934 *modified = was_modified;
936 return success == NM_SETTING_VERIFY_SUCCESS;
940 * nm_connection_update_secrets:
941 * @connection: the #NMConnection
942 * @setting_name: the setting object name to which the secrets apply
943 * @secrets: (element-type utf8 GObject.Value): a #GHashTable mapping
944 * string:#GValue of setting property names and secrets of the given @setting_name
945 * @error: location to store error, or %NULL
947 * Update the specified setting's secrets, given a hash table of secrets
948 * intended for that setting (deserialized from D-Bus for example). Will also
949 * extract the given setting's secrets hash if given a hash of hashes, as would
950 * be returned from nm_connection_to_hash(). If @setting_name is %NULL, expects
951 * a fully serialized #NMConnection as returned by nm_connection_to_hash() and
952 * will update all secrets from all settings contained in @secrets.
954 * Returns: %TRUE if the secrets were successfully updated, %FALSE if the update
955 * failed (tried to update secrets for a setting that doesn't exist, etc)
958 nm_connection_update_secrets (NMConnection *connection,
959 const char *setting_name,
964 gboolean success = TRUE, updated = FALSE;
965 GHashTable *setting_hash = NULL;
968 gboolean hashed_connection = FALSE;
971 g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
972 g_return_val_if_fail (secrets != NULL, FALSE);
974 g_return_val_if_fail (*error == NULL, FALSE);
976 /* Empty @secrets means success */
977 if (g_hash_table_size (secrets) == 0)
980 /* For backwards compatibility, this function accepts either a hashed
981 * connection (GHashTable of GHashTables of GValues) or a single hashed
982 * setting (GHashTable of GValues).
984 g_hash_table_iter_init (&iter, secrets);
985 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL)) {
986 if (_nm_setting_lookup_setting_type (key) != G_TYPE_INVALID) {
987 /* @secrets looks like a hashed connection */
988 hashed_connection = TRUE;
994 /* Update just one setting's secrets */
995 setting = nm_connection_get_setting_by_name (connection, setting_name);
997 g_set_error_literal (error,
999 NM_CONNECTION_ERROR_SETTING_NOT_FOUND,
1004 if (hashed_connection) {
1005 setting_hash = g_hash_table_lookup (secrets, setting_name);
1006 if (!setting_hash) {
1007 /* The hashed connection that didn't contain any secrets for
1008 * @setting_name; just return success.
1014 g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection);
1015 success_detail = _nm_setting_update_secrets (setting,
1016 setting_hash ? setting_hash : secrets,
1018 g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection);
1020 if (success_detail == NM_SETTING_UPDATE_SECRET_ERROR)
1022 if (success_detail == NM_SETTING_UPDATE_SECRET_SUCCESS_MODIFIED)
1025 if (!hashed_connection) {
1026 g_set_error_literal (error,
1027 NM_CONNECTION_ERROR,
1028 NM_CONNECTION_ERROR_SETTING_NOT_FOUND,
1033 /* check first, whether all the settings exist... */
1034 g_hash_table_iter_init (&iter, secrets);
1035 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL)) {
1036 setting = nm_connection_get_setting_by_name (connection, key);
1038 g_set_error_literal (error,
1039 NM_CONNECTION_ERROR,
1040 NM_CONNECTION_ERROR_SETTING_NOT_FOUND,
1046 /* Update each setting with any secrets from the hashed connection */
1047 g_hash_table_iter_init (&iter, secrets);
1048 while (g_hash_table_iter_next (&iter, (gpointer) &key, (gpointer) &setting_hash)) {
1049 /* Update the secrets for this setting */
1050 setting = nm_connection_get_setting_by_name (connection, key);
1052 g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection);
1053 success_detail = _nm_setting_update_secrets (setting, setting_hash, error);
1054 g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection);
1056 if (success_detail == NM_SETTING_UPDATE_SECRET_ERROR) {
1060 if (success_detail == NM_SETTING_UPDATE_SECRET_SUCCESS_MODIFIED)
1066 g_signal_emit (connection, signals[SECRETS_UPDATED], 0, setting_name);
1067 g_signal_emit (connection, signals[CHANGED], 0);
1074 * nm_connection_need_secrets:
1075 * @connection: the #NMConnection
1076 * @hints: (out) (element-type utf8) (allow-none) (transfer container):
1077 * the address of a pointer to a #GPtrArray, initialized to %NULL, which on
1078 * return points to an allocated #GPtrArray containing the property names of
1079 * secrets of the #NMSetting which may be required; the caller owns the array
1080 * and must free the array itself with g_ptr_array_free(), but not free its
1083 * Returns the name of the first setting object in the connection which would
1084 * need secrets to make a successful connection. The returned hints are only
1085 * intended as a guide to what secrets may be required, because in some
1086 * circumstances, there is no way to conclusively determine exactly which
1087 * secrets are needed.
1089 * Returns: the setting name of the #NMSetting object which has invalid or
1093 nm_connection_need_secrets (NMConnection *connection,
1096 NMConnectionPrivate *priv;
1097 GHashTableIter hiter;
1098 GSList *settings = NULL;
1100 const char *name = NULL;
1103 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1105 g_return_val_if_fail (*hints == NULL, NULL);
1107 priv = NM_CONNECTION_GET_PRIVATE (connection);
1109 /* Get list of settings in priority order */
1110 g_hash_table_iter_init (&hiter, priv->settings);
1111 while (g_hash_table_iter_next (&hiter, NULL, (gpointer) &setting))
1112 settings = g_slist_insert_sorted (settings, setting, _nm_setting_compare_priority);
1114 for (iter = settings; iter; iter = g_slist_next (iter)) {
1117 setting = NM_SETTING (iter->data);
1118 secrets = nm_setting_need_secrets (setting);
1123 g_ptr_array_free (secrets, TRUE);
1125 name = nm_setting_get_name (setting);
1130 g_slist_free (settings);
1135 * nm_connection_clear_secrets:
1136 * @connection: the #NMConnection
1138 * Clears and frees any secrets that may be stored in the connection, to avoid
1139 * keeping secret data in memory when not needed.
1142 nm_connection_clear_secrets (NMConnection *connection)
1144 GHashTableIter iter;
1146 gboolean changed = FALSE;
1148 g_return_if_fail (NM_IS_CONNECTION (connection));
1150 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1151 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting)) {
1152 g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection);
1153 changed |= _nm_setting_clear_secrets (setting);
1154 g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection);
1157 g_signal_emit (connection, signals[SECRETS_CLEARED], 0);
1159 g_signal_emit (connection, signals[CHANGED], 0);
1163 * nm_connection_clear_secrets_with_flags:
1164 * @connection: the #NMConnection
1165 * @func: (scope call): function to be called to determine whether a
1166 * specific secret should be cleared or not
1167 * @user_data: caller-supplied data passed to @func
1169 * Clears and frees secrets determined by @func.
1172 nm_connection_clear_secrets_with_flags (NMConnection *connection,
1173 NMSettingClearSecretsWithFlagsFn func,
1176 GHashTableIter iter;
1178 gboolean changed = FALSE;
1180 g_return_if_fail (NM_IS_CONNECTION (connection));
1182 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1183 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting)) {
1184 g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection);
1185 changed |= _nm_setting_clear_secrets_with_flags (setting, func, user_data);
1186 g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection);
1189 g_signal_emit (connection, signals[SECRETS_CLEARED], 0);
1191 g_signal_emit (connection, signals[CHANGED], 0);
1195 * nm_connection_to_hash:
1196 * @connection: the #NMConnection
1197 * @flags: hash flags, e.g. %NM_SETTING_HASH_FLAG_ALL
1199 * Converts the #NMConnection into a #GHashTable describing the connection,
1200 * suitable for marshalling over D-Bus or serializing. The hash table mapping
1201 * is string:#GHashTable with each element in the returned hash representing
1202 * a #NMSetting object. The keys are setting object names, and the values
1203 * are #GHashTables mapping string:GValue, each of which represents the
1204 * properties of the #NMSetting object.
1206 * Returns: (transfer full) (element-type utf8 GLib.HashTable): a new
1207 * #GHashTable describing the connection, its settings, and each setting's
1208 * properties. The caller owns the hash table and must unref the hash table
1209 * with g_hash_table_unref() when it is no longer needed.
1212 nm_connection_to_hash (NMConnection *connection, NMSettingHashFlags flags)
1214 NMConnectionPrivate *priv;
1215 GHashTableIter iter;
1217 GHashTable *ret, *setting_hash;
1219 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1221 ret = g_hash_table_new_full (g_str_hash, g_str_equal,
1222 g_free, (GDestroyNotify) g_hash_table_unref);
1224 priv = NM_CONNECTION_GET_PRIVATE (connection);
1226 /* Add each setting's hash to the main hash */
1227 g_hash_table_iter_init (&iter, priv->settings);
1228 while (g_hash_table_iter_next (&iter, &key, &data)) {
1229 NMSetting *setting = NM_SETTING (data);
1231 setting_hash = nm_setting_to_hash (setting, flags);
1233 g_hash_table_insert (ret, g_strdup (nm_setting_get_name (setting)), setting_hash);
1236 /* Don't send empty hashes */
1237 if (g_hash_table_size (ret) < 1) {
1238 g_hash_table_destroy (ret);
1246 * nm_connection_is_type:
1247 * @connection: the #NMConnection
1248 * @type: a setting name to check the connection's type against (like
1249 * %NM_SETTING_WIRELESS_SETTING_NAME or %NM_SETTING_WIRED_SETTING_NAME)
1251 * A convenience function to check if the given @connection is a particular
1252 * type (ie wired, Wi-Fi, ppp, etc). Checks the #NMSettingConnection:type
1253 * property of the connection and matches that against @type.
1255 * Returns: %TRUE if the connection is of the given @type, %FALSE if not
1258 nm_connection_is_type (NMConnection *connection, const char *type)
1260 NMSettingConnection *s_con;
1263 g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
1264 g_return_val_if_fail (type != NULL, FALSE);
1266 s_con = nm_connection_get_setting_connection (connection);
1270 type2 = nm_setting_connection_get_connection_type (s_con);
1272 return (g_strcmp0 (type2, type) == 0);
1276 * nm_connection_for_each_setting_value:
1277 * @connection: the #NMConnection
1278 * @func: (scope call): user-supplied function called for each setting's property
1279 * @user_data: user data passed to @func at each invocation
1281 * Iterates over the properties of each #NMSetting object in the #NMConnection,
1282 * calling the supplied user function for each property.
1285 nm_connection_for_each_setting_value (NMConnection *connection,
1286 NMSettingValueIterFn func,
1289 GHashTableIter iter;
1292 g_return_if_fail (NM_IS_CONNECTION (connection));
1293 g_return_if_fail (func != NULL);
1295 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1296 while (g_hash_table_iter_next (&iter, NULL, &value))
1297 nm_setting_enumerate_values (NM_SETTING (value), func, user_data);
1301 * nm_connection_dump:
1302 * @connection: the #NMConnection
1304 * Print the connection to stdout. For debugging purposes ONLY, should NOT
1305 * be used for serialization of the connection or machine-parsed in any way. The
1306 * output format is not guaranteed to be stable and may change at any time.
1309 nm_connection_dump (NMConnection *connection)
1311 GHashTableIter iter;
1313 const char *setting_name;
1319 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1320 while (g_hash_table_iter_next (&iter, (gpointer) &setting_name, (gpointer) &setting)) {
1321 str = nm_setting_to_string (setting);
1322 g_print ("%s\n", str);
1328 * nm_connection_set_path:
1329 * @connection: the #NMConnection
1330 * @path: the D-Bus path of the connection as given by the settings service
1331 * which provides the connection
1333 * Sets the D-Bus path of the connection. This property is not serialized, and
1334 * is only for the reference of the caller. Sets the #NMConnection:path
1338 nm_connection_set_path (NMConnection *connection, const char *path)
1340 NMConnectionPrivate *priv;
1342 g_return_if_fail (NM_IS_CONNECTION (connection));
1344 priv = NM_CONNECTION_GET_PRIVATE (connection);
1346 g_free (priv->path);
1350 priv->path = g_strdup (path);
1354 * nm_connection_get_path:
1355 * @connection: the #NMConnection
1357 * Returns the connection's D-Bus path.
1359 * Returns: the D-Bus path of the connection, previously set by a call to
1360 * nm_connection_set_path().
1363 nm_connection_get_path (NMConnection *connection)
1365 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1367 return NM_CONNECTION_GET_PRIVATE (connection)->path;
1371 * nm_connection_get_interface_name:
1372 * @connection: The #NMConnection
1374 * Returns the interface name as stored in NMSettingConnection:interface_name.
1375 * If the connection contains no NMSettingConnection, it will return %NULL.
1377 * For hardware devices and software devices created outside of NetworkManager,
1378 * this name is used to match the device. for software devices created by
1379 * NetworkManager, this is the name of the created interface.
1381 * Returns: Name of the kernel interface or %NULL
1386 nm_connection_get_interface_name (NMConnection *connection)
1388 NMSettingConnection *s_con;
1390 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1392 s_con = nm_connection_get_setting_connection (connection);
1394 return s_con ? nm_setting_connection_get_interface_name (s_con) : NULL;
1398 * nm_connection_get_virtual_iface_name:
1399 * @connection: The #NMConnection
1401 * Returns the name of the virtual kernel interface which the connection
1402 * needs to use if specified in the settings. This function abstracts all
1403 * connection types which require this functionality. For all other
1404 * connection types, this function will return %NULL.
1406 * Returns: Name of the kernel interface or %NULL
1409 nm_connection_get_virtual_iface_name (NMConnection *connection)
1411 NMSettingConnection *s_con;
1415 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1417 s_con = nm_connection_get_setting_connection (connection);
1418 g_return_val_if_fail (s_con, NULL);
1420 type = nm_setting_connection_get_connection_type (s_con);
1421 g_return_val_if_fail (type, NULL);
1423 base = nm_connection_get_setting_by_name (connection, type);
1427 return nm_setting_get_virtual_iface_name (base);
1431 * nm_connection_new:
1433 * Creates a new #NMConnection object with no #NMSetting objects.
1435 * Returns: the new empty #NMConnection object
1438 nm_connection_new (void)
1440 return (NMConnection *) g_object_new (NM_TYPE_CONNECTION, NULL);
1444 * nm_connection_new_from_hash:
1445 * @hash: (element-type utf8 GLib.HashTable): the #GHashTable describing
1447 * @error: on unsuccessful return, an error
1449 * Creates a new #NMConnection from a hash table describing the connection. See
1450 * nm_connection_to_hash() for a description of the expected hash table.
1452 * Returns: the new #NMConnection object, populated with settings created
1453 * from the values in the hash table, or %NULL if the connection failed to
1457 nm_connection_new_from_hash (GHashTable *hash, GError **error)
1459 NMConnection *connection;
1461 g_return_val_if_fail (hash != NULL, NULL);
1463 if (!validate_permissions_type (hash, error))
1466 connection = nm_connection_new ();
1467 hash_to_connection (connection, hash);
1468 if (!nm_connection_verify (connection, error))
1469 g_clear_object (&connection);
1474 * nm_connection_duplicate:
1475 * @connection: the #NMConnection to duplicate
1477 * Duplicates a #NMConnection.
1479 * Returns: (transfer full): a new #NMConnection containing the same settings and properties
1480 * as the source #NMConnection
1483 nm_connection_duplicate (NMConnection *connection)
1486 GHashTableIter iter;
1489 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1491 dup = nm_connection_new ();
1492 nm_connection_set_path (dup, nm_connection_get_path (connection));
1494 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1495 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting))
1496 _nm_connection_add_setting (dup, nm_setting_duplicate (setting));
1502 * nm_connection_get_uuid:
1503 * @connection: the #NMConnection
1505 * A shortcut to return the UUID from the connection's #NMSettingConnection.
1507 * Returns: the UUID from the connection's 'connection' setting
1510 nm_connection_get_uuid (NMConnection *connection)
1512 NMSettingConnection *s_con;
1514 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1516 s_con = nm_connection_get_setting_connection (connection);
1517 g_return_val_if_fail (s_con != NULL, NULL);
1519 return nm_setting_connection_get_uuid (s_con);
1523 * nm_connection_get_id:
1524 * @connection: the #NMConnection
1526 * A shortcut to return the ID from the connection's #NMSettingConnection.
1528 * Returns: the ID from the connection's 'connection' setting
1531 nm_connection_get_id (NMConnection *connection)
1533 NMSettingConnection *s_con;
1535 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1537 s_con = nm_connection_get_setting_connection (connection);
1538 g_return_val_if_fail (s_con != NULL, NULL);
1540 return nm_setting_connection_get_id (s_con);
1544 * nm_connection_get_connection_type:
1545 * @connection: the #NMConnection
1547 * A shortcut to return the type from the connection's #NMSettingConnection.
1549 * Returns: the type from the connection's 'connection' setting
1554 nm_connection_get_connection_type (NMConnection *connection)
1556 NMSettingConnection *s_con;
1558 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1560 s_con = nm_connection_get_setting_connection (connection);
1561 g_return_val_if_fail (s_con != NULL, NULL);
1563 return nm_setting_connection_get_connection_type (s_con);
1567 * nm_connection_get_virtual_device_description:
1568 * @connection: an #NMConnection for a virtual device type
1570 * Returns the name that nm_device_disambiguate_names() would
1571 * return for the virtual device that would be created for @connection.
1572 * Eg, "VLAN (eth1.1)".
1574 * Returns: (transfer full): the name of @connection's device,
1575 * or %NULL if @connection is not a virtual connection type
1580 nm_connection_get_virtual_device_description (NMConnection *connection)
1582 const char *iface, *type, *display_type;
1583 NMSettingConnection *s_con;
1585 iface = nm_connection_get_virtual_iface_name (connection);
1589 s_con = nm_connection_get_setting_connection (connection);
1590 g_return_val_if_fail (s_con != NULL, NULL);
1591 type = nm_setting_connection_get_connection_type (s_con);
1593 if (!strcmp (type, NM_SETTING_BOND_SETTING_NAME))
1594 display_type = _("Bond");
1595 else if (!strcmp (type, NM_SETTING_TEAM_SETTING_NAME))
1596 display_type = _("Team");
1597 else if (!strcmp (type, NM_SETTING_BRIDGE_SETTING_NAME))
1598 display_type = _("Bridge");
1599 else if (!strcmp (type, NM_SETTING_VLAN_SETTING_NAME))
1600 display_type = _("VLAN");
1602 g_warning ("Unrecognized virtual device type '%s'", type);
1603 display_type = type;
1606 return g_strdup_printf ("%s (%s)", display_type, iface);
1609 /*************************************************************/
1612 * nm_connection_get_setting_802_1x:
1613 * @connection: the #NMConnection
1615 * A shortcut to return any #NMSetting8021x the connection might contain.
1617 * Returns: (transfer none): an #NMSetting8021x if the connection contains one, otherwise %NULL
1620 nm_connection_get_setting_802_1x (NMConnection *connection)
1622 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1624 return (NMSetting8021x *) nm_connection_get_setting (connection, NM_TYPE_SETTING_802_1X);
1628 * nm_connection_get_setting_bluetooth:
1629 * @connection: the #NMConnection
1631 * A shortcut to return any #NMSettingBluetooth the connection might contain.
1633 * Returns: (transfer none): an #NMSettingBluetooth if the connection contains one, otherwise %NULL
1635 NMSettingBluetooth *
1636 nm_connection_get_setting_bluetooth (NMConnection *connection)
1638 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1640 return (NMSettingBluetooth *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BLUETOOTH);
1644 * nm_connection_get_setting_bond:
1645 * @connection: the #NMConnection
1647 * A shortcut to return any #NMSettingBond the connection might contain.
1649 * Returns: (transfer none): an #NMSettingBond if the connection contains one, otherwise %NULL
1652 nm_connection_get_setting_bond (NMConnection *connection)
1654 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1656 return (NMSettingBond *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BOND);
1660 * nm_connection_get_setting_team:
1661 * @connection: the #NMConnection
1663 * A shortcut to return any #NMSettingTeam the connection might contain.
1665 * Returns: (transfer none): an #NMSettingTeam if the connection contains one, otherwise %NULL
1670 nm_connection_get_setting_team (NMConnection *connection)
1672 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1674 return (NMSettingTeam *) nm_connection_get_setting (connection, NM_TYPE_SETTING_TEAM);
1678 * nm_connection_get_setting_team_port:
1679 * @connection: the #NMConnection
1681 * A shortcut to return any #NMSettingTeamPort the connection might contain.
1683 * Returns: (transfer none): an #NMSettingTeamPort if the connection contains one, otherwise %NULL
1688 nm_connection_get_setting_team_port (NMConnection *connection)
1690 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1692 return (NMSettingTeamPort *) nm_connection_get_setting (connection, NM_TYPE_SETTING_TEAM_PORT);
1696 * nm_connection_get_setting_bridge:
1697 * @connection: the #NMConnection
1699 * A shortcut to return any #NMSettingBridge the connection might contain.
1701 * Returns: (transfer none): an #NMSettingBridge if the connection contains one, otherwise %NULL
1704 nm_connection_get_setting_bridge (NMConnection *connection)
1706 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1708 return (NMSettingBridge *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BRIDGE);
1712 * nm_connection_get_setting_cdma:
1713 * @connection: the #NMConnection
1715 * A shortcut to return any #NMSettingCdma the connection might contain.
1717 * Returns: (transfer none): an #NMSettingCdma if the connection contains one, otherwise %NULL
1720 nm_connection_get_setting_cdma (NMConnection *connection)
1722 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1724 return (NMSettingCdma *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CDMA);
1728 * nm_connection_get_setting_connection:
1729 * @connection: the #NMConnection
1731 * A shortcut to return any #NMSettingConnection the connection might contain.
1733 * Returns: (transfer none): an #NMSettingConnection if the connection contains one, otherwise %NULL
1735 NMSettingConnection *
1736 nm_connection_get_setting_connection (NMConnection *connection)
1738 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1740 return (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
1744 * nm_connection_get_setting_dcb:
1745 * @connection: the #NMConnection
1747 * A shortcut to return any #NMSettingDcb the connection might contain.
1749 * Returns: (transfer none): an #NMSettingDcb if the connection contains one, otherwise NULL
1754 nm_connection_get_setting_dcb (NMConnection *connection)
1756 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1758 return (NMSettingDcb *) nm_connection_get_setting (connection, NM_TYPE_SETTING_DCB);
1762 * nm_connection_get_setting_generic:
1763 * @connection: the #NMConnection
1765 * A shortcut to return any #NMSettingGeneric the connection might contain.
1767 * Returns: (transfer none): an #NMSettingGeneric if the connection contains one, otherwise NULL
1772 nm_connection_get_setting_generic (NMConnection *connection)
1774 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1776 return (NMSettingGeneric *) nm_connection_get_setting (connection, NM_TYPE_SETTING_GENERIC);
1780 * nm_connection_get_setting_gsm:
1781 * @connection: the #NMConnection
1783 * A shortcut to return any #NMSettingGsm the connection might contain.
1785 * Returns: (transfer none): an #NMSettingGsm if the connection contains one, otherwise %NULL
1788 nm_connection_get_setting_gsm (NMConnection *connection)
1790 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1792 return (NMSettingGsm *) nm_connection_get_setting (connection, NM_TYPE_SETTING_GSM);
1796 * nm_connection_get_setting_infiniband:
1797 * @connection: the #NMConnection
1799 * A shortcut to return any #NMSettingInfiniband the connection might contain.
1801 * Returns: (transfer none): an #NMSettingInfiniband if the connection contains one, otherwise %NULL
1803 NMSettingInfiniband *
1804 nm_connection_get_setting_infiniband (NMConnection *connection)
1806 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1808 return (NMSettingInfiniband *) nm_connection_get_setting (connection, NM_TYPE_SETTING_INFINIBAND);
1812 * nm_connection_get_setting_ip4_config:
1813 * @connection: the #NMConnection
1815 * A shortcut to return any #NMSettingIP4Config the connection might contain.
1817 * Returns: (transfer none): an #NMSettingIP4Config if the connection contains one, otherwise %NULL
1819 NMSettingIP4Config *
1820 nm_connection_get_setting_ip4_config (NMConnection *connection)
1822 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1824 return (NMSettingIP4Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG);
1828 * nm_connection_get_setting_ip6_config:
1829 * @connection: the #NMConnection
1831 * A shortcut to return any #NMSettingIP6Config the connection might contain.
1833 * Returns: (transfer none): an #NMSettingIP6Config if the connection contains one, otherwise %NULL
1835 NMSettingIP6Config *
1836 nm_connection_get_setting_ip6_config (NMConnection *connection)
1838 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1840 return (NMSettingIP6Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP6_CONFIG);
1844 * nm_connection_get_setting_olpc_mesh:
1845 * @connection: the #NMConnection
1847 * A shortcut to return any #NMSettingOlpcMesh the connection might contain.
1849 * Returns: (transfer none): an #NMSettingOlpcMesh if the connection contains one, otherwise %NULL
1852 nm_connection_get_setting_olpc_mesh (NMConnection *connection)
1854 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1856 return (NMSettingOlpcMesh *) nm_connection_get_setting (connection, NM_TYPE_SETTING_OLPC_MESH);
1860 * nm_connection_get_setting_ppp:
1861 * @connection: the #NMConnection
1863 * A shortcut to return any #NMSettingPPP the connection might contain.
1865 * Returns: (transfer none): an #NMSettingPPP if the connection contains one, otherwise %NULL
1868 nm_connection_get_setting_ppp (NMConnection *connection)
1870 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1872 return (NMSettingPPP *) nm_connection_get_setting (connection, NM_TYPE_SETTING_PPP);
1876 * nm_connection_get_setting_pppoe:
1877 * @connection: the #NMConnection
1879 * A shortcut to return any #NMSettingPPPOE the connection might contain.
1881 * Returns: (transfer none): an #NMSettingPPPOE if the connection contains one, otherwise %NULL
1884 nm_connection_get_setting_pppoe (NMConnection *connection)
1886 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1888 return (NMSettingPPPOE *) nm_connection_get_setting (connection, NM_TYPE_SETTING_PPPOE);
1892 * nm_connection_get_setting_serial:
1893 * @connection: the #NMConnection
1895 * A shortcut to return any #NMSettingSerial the connection might contain.
1897 * Returns: (transfer none): an #NMSettingSerial if the connection contains one, otherwise %NULL
1900 nm_connection_get_setting_serial (NMConnection *connection)
1902 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1904 return (NMSettingSerial *) nm_connection_get_setting (connection, NM_TYPE_SETTING_SERIAL);
1908 * nm_connection_get_setting_vpn:
1909 * @connection: the #NMConnection
1911 * A shortcut to return any #NMSettingVPN the connection might contain.
1913 * Returns: (transfer none): an #NMSettingVPN if the connection contains one, otherwise %NULL
1916 nm_connection_get_setting_vpn (NMConnection *connection)
1918 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1920 return (NMSettingVPN *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VPN);
1924 * nm_connection_get_setting_wimax:
1925 * @connection: the #NMConnection
1927 * A shortcut to return any #NMSettingWimax the connection might contain.
1929 * Returns: (transfer none): an #NMSettingWimax if the connection contains one, otherwise %NULL
1932 nm_connection_get_setting_wimax (NMConnection *connection)
1934 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1936 return (NMSettingWimax *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIMAX);
1940 * nm_connection_get_setting_wired:
1941 * @connection: the #NMConnection
1943 * A shortcut to return any #NMSettingWired the connection might contain.
1945 * Returns: (transfer none): an #NMSettingWired if the connection contains one, otherwise %NULL
1948 nm_connection_get_setting_wired (NMConnection *connection)
1950 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1952 return (NMSettingWired *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRED);
1956 * nm_connection_get_setting_adsl:
1957 * @connection: the #NMConnection
1959 * A shortcut to return any #NMSettingAdsl the connection might contain.
1961 * Returns: (transfer none): an #NMSettingAdsl if the connection contains one, otherwise %NULL
1964 nm_connection_get_setting_adsl (NMConnection *connection)
1966 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1968 return (NMSettingAdsl *) nm_connection_get_setting (connection, NM_TYPE_SETTING_ADSL);
1972 * nm_connection_get_setting_wireless:
1973 * @connection: the #NMConnection
1975 * A shortcut to return any #NMSettingWireless the connection might contain.
1977 * Returns: (transfer none): an #NMSettingWireless if the connection contains one, otherwise %NULL
1980 nm_connection_get_setting_wireless (NMConnection *connection)
1982 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1984 return (NMSettingWireless *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS);
1988 * nm_connection_get_setting_wireless_security:
1989 * @connection: the #NMConnection
1991 * A shortcut to return any #NMSettingWirelessSecurity the connection might contain.
1993 * Returns: (transfer none): an #NMSettingWirelessSecurity if the connection contains one, otherwise %NULL
1995 NMSettingWirelessSecurity *
1996 nm_connection_get_setting_wireless_security (NMConnection *connection)
1998 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2000 return (NMSettingWirelessSecurity *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS_SECURITY);
2004 * nm_connection_get_setting_bridge_port:
2005 * @connection: the #NMConnection
2007 * A shortcut to return any #NMSettingBridgePort the connection might contain.
2009 * Returns: (transfer none): an #NMSettingBridgePort if the connection contains one, otherwise %NULL
2011 NMSettingBridgePort *
2012 nm_connection_get_setting_bridge_port (NMConnection *connection)
2014 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2016 return (NMSettingBridgePort *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BRIDGE_PORT);
2020 * nm_connection_get_setting_vlan:
2021 * @connection: the #NMConnection
2023 * A shortcut to return any #NMSettingVlan the connection might contain.
2025 * Returns: (transfer none): an #NMSettingVlan if the connection contains one, otherwise %NULL
2028 nm_connection_get_setting_vlan (NMConnection *connection)
2030 g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2032 return (NMSettingVlan *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VLAN);
2035 /*************************************************************/
2038 nm_connection_init (NMConnection *connection)
2040 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (connection);
2042 priv->settings = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_object_unref);
2046 dispose (GObject *object)
2048 NMConnection *self = NM_CONNECTION (object);
2049 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (self);
2051 g_hash_table_foreach_remove (priv->settings, _setting_release, self);
2053 G_OBJECT_CLASS (nm_connection_parent_class)->dispose (object);
2057 finalize (GObject *object)
2059 NMConnection *connection = NM_CONNECTION (object);
2060 NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (connection);
2062 g_assert (g_hash_table_size (priv->settings) == 0);
2063 g_hash_table_destroy (priv->settings);
2064 g_free (priv->path);
2066 G_OBJECT_CLASS (nm_connection_parent_class)->finalize (object);
2070 set_property (GObject *object, guint prop_id,
2071 const GValue *value, GParamSpec *pspec)
2073 NMConnection *connection = NM_CONNECTION (object);
2077 nm_connection_set_path (connection, g_value_get_string (value));
2080 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2086 get_property (GObject *object, guint prop_id,
2087 GValue *value, GParamSpec *pspec)
2089 NMConnection *connection = NM_CONNECTION (object);
2093 g_value_set_string (value, nm_connection_get_path (connection));
2096 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2102 nm_connection_class_init (NMConnectionClass *klass)
2104 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2106 g_type_class_add_private (klass, sizeof (NMConnectionPrivate));
2108 /* virtual methods */
2109 object_class->set_property = set_property;
2110 object_class->get_property = get_property;
2111 object_class->dispose = dispose;
2112 object_class->finalize = finalize;
2117 * NMConnection:path:
2119 * The connection's D-Bus path, used only by the calling process as a record
2120 * of the D-Bus path of the connection as provided by a settings service.
2122 g_object_class_install_property
2123 (object_class, PROP_PATH,
2124 g_param_spec_string (NM_CONNECTION_PATH, "", "",
2128 G_PARAM_STATIC_STRINGS));
2133 * NMConnection::secrets-updated:
2134 * @connection: the object on which the signal is emitted
2135 * @setting_name: the setting name of the #NMSetting for which secrets were
2138 * The ::secrets-updated signal is emitted when the secrets of a setting
2139 * have been changed.
2141 signals[SECRETS_UPDATED] =
2142 g_signal_new (NM_CONNECTION_SECRETS_UPDATED,
2143 G_OBJECT_CLASS_TYPE (object_class),
2145 G_STRUCT_OFFSET (NMConnectionClass, secrets_updated),
2147 g_cclosure_marshal_VOID__STRING,
2152 * NMConnection::secrets-cleared:
2153 * @connection: the object on which the signal is emitted
2155 * The ::secrets-cleared signal is emitted when the secrets of a connection
2158 signals[SECRETS_CLEARED] =
2159 g_signal_new (NM_CONNECTION_SECRETS_CLEARED,
2160 G_OBJECT_CLASS_TYPE (object_class),
2163 g_cclosure_marshal_VOID__VOID,
2167 * NMConnection::changed:
2168 * @connection: the object on which the signal is emitted
2170 * The ::changed signal is emitted when any property of any property
2171 * (including secrets) of any setting of the connection is modified,
2172 * or when settings are added or removed.
2177 g_signal_new (NM_CONNECTION_CHANGED,
2178 G_OBJECT_CLASS_TYPE (object_class),
2181 g_cclosure_marshal_VOID__VOID,