1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the
15 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 * Boston, MA 02110-1301 USA.
18 * Copyright 2007 - 2014 Red Hat, Inc.
19 * Copyright 2008 Novell, Inc.
22 #include "nm-default.h"
26 #include "nm-dbus-interface.h"
27 #include "nm-active-connection.h"
28 #include "nm-object-private.h"
29 #include "nm-core-internal.h"
30 #include "nm-device.h"
31 #include "nm-device-private.h"
32 #include "nm-connection.h"
33 #include "nm-vpn-connection.h"
34 #include "nm-dbus-helpers.h"
35 #include "nm-dhcp4-config.h"
36 #include "nm-dhcp6-config.h"
37 #include "nm-ip4-config.h"
38 #include "nm-ip6-config.h"
39 #include "nm-remote-connection.h"
41 static GType _nm_active_connection_decide_type (GVariant *value);
43 G_DEFINE_TYPE_WITH_CODE (NMActiveConnection, nm_active_connection, NM_TYPE_OBJECT,
44 _nm_object_register_type_func (g_define_type_id,
45 _nm_active_connection_decide_type,
46 NM_DBUS_INTERFACE_ACTIVE_CONNECTION,
50 #define NM_ACTIVE_CONNECTION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_ACTIVE_CONNECTION, NMActiveConnectionPrivate))
53 NMRemoteConnection *connection;
57 char *specific_object_path;
59 NMActiveConnectionState state;
61 NMIPConfig *ip4_config;
62 NMDhcpConfig *dhcp4_config;
64 NMIPConfig *ip6_config;
65 NMDhcpConfig *dhcp6_config;
68 } NMActiveConnectionPrivate;
76 PROP_SPECIFIC_OBJECT_PATH,
92 _nm_active_connection_decide_type (GVariant *value)
94 /* @value is the value of the o.fd.NM.ActiveConnection property "VPN" */
95 if (g_variant_get_boolean (value))
96 return NM_TYPE_VPN_CONNECTION;
98 return NM_TYPE_ACTIVE_CONNECTION;
102 * nm_active_connection_get_connection:
103 * @connection: a #NMActiveConnection
105 * Gets the #NMRemoteConnection associated with @connection.
107 * Returns: (transfer none): the #NMRemoteConnection which this
108 * #NMActiveConnection is an active instance of.
111 nm_active_connection_get_connection (NMActiveConnection *connection)
113 g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (connection), NULL);
115 return NM_ACTIVE_CONNECTION_GET_PRIVATE (connection)->connection;
119 * nm_active_connection_get_id:
120 * @connection: a #NMActiveConnection
122 * Gets the #NMConnection's ID.
124 * Returns: the ID of the #NMConnection that backs the #NMActiveConnection.
125 * This is the internal string used by the connection, and must not be modified.
128 nm_active_connection_get_id (NMActiveConnection *connection)
130 g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (connection), NULL);
132 return NM_ACTIVE_CONNECTION_GET_PRIVATE (connection)->id;
136 * nm_active_connection_get_uuid:
137 * @connection: a #NMActiveConnection
139 * Gets the #NMConnection's UUID.
141 * Returns: the UUID of the #NMConnection that backs the #NMActiveConnection.
142 * This is the internal string used by the connection, and must not be modified.
145 nm_active_connection_get_uuid (NMActiveConnection *connection)
147 g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (connection), NULL);
149 return NM_ACTIVE_CONNECTION_GET_PRIVATE (connection)->uuid;
153 * nm_active_connection_get_connection_type:
154 * @connection: a #NMActiveConnection
156 * Gets the #NMConnection's type.
158 * Returns: the type of the #NMConnection that backs the #NMActiveConnection.
159 * This is the internal string used by the connection, and must not be modified.
162 nm_active_connection_get_connection_type (NMActiveConnection *connection)
164 g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (connection), NULL);
166 return NM_ACTIVE_CONNECTION_GET_PRIVATE (connection)->type;
170 * nm_active_connection_get_specific_object_path:
171 * @connection: a #NMActiveConnection
173 * Gets the path of the "specific object" used at activation.
175 * Currently there is no single method that will allow you to automatically turn
176 * this into an appropriate #NMObject; you need to know what kind of object it
177 * is based on other information. (Eg, if @connection corresponds to a Wi-Fi
178 * connection, then the specific object will be an #NMAccessPoint, and you can
179 * resolve it with nm_device_wifi_get_access_point_by_path().)
181 * Returns: the specific object's D-Bus path. This is the internal string used
182 * by the connection, and must not be modified.
185 nm_active_connection_get_specific_object_path (NMActiveConnection *connection)
187 g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (connection), NULL);
189 return NM_ACTIVE_CONNECTION_GET_PRIVATE (connection)->specific_object_path;
193 * nm_active_connection_get_devices:
194 * @connection: a #NMActiveConnection
196 * Gets the #NMDevices used for the active connections.
198 * Returns: (element-type NMDevice): the #GPtrArray containing #NMDevices.
199 * This is the internal copy used by the connection, and must not be modified.
202 nm_active_connection_get_devices (NMActiveConnection *connection)
204 g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (connection), NULL);
206 return NM_ACTIVE_CONNECTION_GET_PRIVATE (connection)->devices;
210 * nm_active_connection_get_state:
211 * @connection: a #NMActiveConnection
213 * Gets the active connection's state.
217 NMActiveConnectionState
218 nm_active_connection_get_state (NMActiveConnection *connection)
220 g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (connection), NM_ACTIVE_CONNECTION_STATE_UNKNOWN);
222 return NM_ACTIVE_CONNECTION_GET_PRIVATE (connection)->state;
226 * nm_active_connection_get_default:
227 * @connection: a #NMActiveConnection
229 * Whether the active connection is the default IPv4 one (that is, is used for
230 * the default IPv4 route and DNS information).
232 * Returns: %TRUE if the active connection is the default IPv4 connection
235 nm_active_connection_get_default (NMActiveConnection *connection)
237 g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (connection), FALSE);
239 return NM_ACTIVE_CONNECTION_GET_PRIVATE (connection)->is_default;
243 * nm_active_connection_get_ip4_config:
244 * @connection: an #NMActiveConnection
246 * Gets the current IPv4 #NMIPConfig associated with the #NMActiveConnection.
248 * Returns: (transfer none): the IPv4 #NMIPConfig, or %NULL if the connection is
249 * not in the %NM_ACTIVE_CONNECTION_STATE_ACTIVATED state.
252 nm_active_connection_get_ip4_config (NMActiveConnection *connection)
254 g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (connection), NULL);
256 return NM_ACTIVE_CONNECTION_GET_PRIVATE (connection)->ip4_config;
260 * nm_active_connection_get_dhcp4_config:
261 * @connection: an #NMActiveConnection
263 * Gets the current IPv4 #NMDhcpConfig (if any) associated with the
264 * #NMActiveConnection.
266 * Returns: (transfer none): the IPv4 #NMDhcpConfig, or %NULL if the connection
267 * does not use DHCP, or is not in the %NM_ACTIVE_CONNECTION_STATE_ACTIVATED
271 nm_active_connection_get_dhcp4_config (NMActiveConnection *connection)
273 g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (connection), NULL);
275 return NM_ACTIVE_CONNECTION_GET_PRIVATE (connection)->dhcp4_config;
279 * nm_active_connection_get_default6:
280 * @connection: a #NMActiveConnection
282 * Whether the active connection is the default IPv6 one (that is, is used for
283 * the default IPv6 route and DNS information).
285 * Returns: %TRUE if the active connection is the default IPv6 connection
288 nm_active_connection_get_default6 (NMActiveConnection *connection)
290 g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (connection), FALSE);
292 return NM_ACTIVE_CONNECTION_GET_PRIVATE (connection)->is_default6;
296 * nm_active_connection_get_ip6_config:
297 * @connection: an #NMActiveConnection
299 * Gets the current IPv6 #NMIPConfig associated with the #NMActiveConnection.
301 * Returns: (transfer none): the IPv6 #NMIPConfig, or %NULL if the connection is
302 * not in the %NM_ACTIVE_CONNECTION_STATE_ACTIVATED state.
305 nm_active_connection_get_ip6_config (NMActiveConnection *connection)
307 g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (connection), NULL);
309 return NM_ACTIVE_CONNECTION_GET_PRIVATE (connection)->ip6_config;
313 * nm_active_connection_get_dhcp6_config:
314 * @connection: an #NMActiveConnection
316 * Gets the current IPv6 #NMDhcpConfig (if any) associated with the
317 * #NMActiveConnection.
319 * Returns: (transfer none): the IPv6 #NMDhcpConfig, or %NULL if the connection
320 * does not use DHCPv6, or is not in the %NM_ACTIVE_CONNECTION_STATE_ACTIVATED
324 nm_active_connection_get_dhcp6_config (NMActiveConnection *connection)
326 g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (connection), NULL);
328 return NM_ACTIVE_CONNECTION_GET_PRIVATE (connection)->dhcp6_config;
332 * nm_active_connection_get_vpn:
333 * @connection: a #NMActiveConnection
335 * Whether the active connection is a VPN connection.
337 * Returns: %TRUE if the active connection is a VPN connection
340 nm_active_connection_get_vpn (NMActiveConnection *connection)
342 g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (connection), FALSE);
344 return NM_ACTIVE_CONNECTION_GET_PRIVATE (connection)->is_vpn;
348 * nm_active_connection_get_master:
349 * @connection: a #NMActiveConnection
351 * Gets the master #NMDevice of the connection.
353 * Returns: (transfer none): the master #NMDevice of the #NMActiveConnection.
356 nm_active_connection_get_master (NMActiveConnection *connection)
358 g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (connection), NULL);
360 return NM_ACTIVE_CONNECTION_GET_PRIVATE (connection)->master;
364 nm_active_connection_init (NMActiveConnection *connection)
366 NMActiveConnectionPrivate *priv = NM_ACTIVE_CONNECTION_GET_PRIVATE (connection);
368 priv->devices = g_ptr_array_new ();
372 dispose (GObject *object)
374 NMActiveConnectionPrivate *priv = NM_ACTIVE_CONNECTION_GET_PRIVATE (object);
376 g_clear_pointer (&priv->devices, g_ptr_array_unref);
378 g_clear_object (&priv->connection);
379 g_clear_object (&priv->master);
380 g_clear_object (&priv->ip4_config);
381 g_clear_object (&priv->dhcp4_config);
382 g_clear_object (&priv->ip6_config);
383 g_clear_object (&priv->dhcp6_config);
385 G_OBJECT_CLASS (nm_active_connection_parent_class)->dispose (object);
389 finalize (GObject *object)
391 NMActiveConnectionPrivate *priv = NM_ACTIVE_CONNECTION_GET_PRIVATE (object);
396 g_free (priv->specific_object_path);
398 G_OBJECT_CLASS (nm_active_connection_parent_class)->finalize (object);
402 get_property (GObject *object,
407 NMActiveConnection *self = NM_ACTIVE_CONNECTION (object);
410 case PROP_CONNECTION:
411 g_value_set_object (value, nm_active_connection_get_connection (self));
414 g_value_set_string (value, nm_active_connection_get_id (self));
417 g_value_set_string (value, nm_active_connection_get_uuid (self));
420 g_value_set_string (value, nm_active_connection_get_connection_type (self));
422 case PROP_SPECIFIC_OBJECT_PATH:
423 g_value_set_string (value, nm_active_connection_get_specific_object_path (self));
426 g_value_take_boxed (value, _nm_utils_copy_object_array (nm_active_connection_get_devices (self)));
429 g_value_set_enum (value, nm_active_connection_get_state (self));
432 g_value_set_boolean (value, nm_active_connection_get_default (self));
434 case PROP_IP4_CONFIG:
435 g_value_set_object (value, nm_active_connection_get_ip4_config (self));
437 case PROP_DHCP4_CONFIG:
438 g_value_set_object (value, nm_active_connection_get_dhcp4_config (self));
441 g_value_set_boolean (value, nm_active_connection_get_default6 (self));
443 case PROP_IP6_CONFIG:
444 g_value_set_object (value, nm_active_connection_get_ip6_config (self));
446 case PROP_DHCP6_CONFIG:
447 g_value_set_object (value, nm_active_connection_get_dhcp6_config (self));
450 g_value_set_boolean (value, nm_active_connection_get_vpn (self));
453 g_value_set_object (value, nm_active_connection_get_master (self));
456 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
462 demarshal_specific_object_path (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field)
464 char **param = (char **) field;
466 /* We have to demarshal this manually, because the D-Bus property name
467 * ("SpecificObject"), doesn't match the object property name
468 * ("specific-object-path"). (The name "specific-object" is reserved for
469 * future use as an NMObject-valued property.)
471 if (!g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH))
475 *param = g_variant_dup_string (value, NULL);
480 init_dbus (NMObject *object)
482 NMActiveConnectionPrivate *priv = NM_ACTIVE_CONNECTION_GET_PRIVATE (object);
483 const NMPropertiesInfo property_info[] = {
484 { NM_ACTIVE_CONNECTION_CONNECTION, &priv->connection, NULL, NM_TYPE_REMOTE_CONNECTION },
485 { NM_ACTIVE_CONNECTION_ID, &priv->id },
486 { NM_ACTIVE_CONNECTION_UUID, &priv->uuid },
487 { NM_ACTIVE_CONNECTION_TYPE, &priv->type },
488 { "specific-object", &priv->specific_object_path, demarshal_specific_object_path },
489 { NM_ACTIVE_CONNECTION_DEVICES, &priv->devices, NULL, NM_TYPE_DEVICE },
490 { NM_ACTIVE_CONNECTION_STATE, &priv->state },
491 { NM_ACTIVE_CONNECTION_DEFAULT, &priv->is_default },
492 { NM_ACTIVE_CONNECTION_IP4_CONFIG, &priv->ip4_config, NULL, NM_TYPE_IP4_CONFIG },
493 { NM_ACTIVE_CONNECTION_DHCP4_CONFIG, &priv->dhcp4_config, NULL, NM_TYPE_DHCP4_CONFIG },
494 { NM_ACTIVE_CONNECTION_DEFAULT6, &priv->is_default6 },
495 { NM_ACTIVE_CONNECTION_IP6_CONFIG, &priv->ip6_config, NULL, NM_TYPE_IP6_CONFIG },
496 { NM_ACTIVE_CONNECTION_DHCP6_CONFIG, &priv->dhcp6_config, NULL, NM_TYPE_DHCP6_CONFIG },
497 { NM_ACTIVE_CONNECTION_VPN, &priv->is_vpn },
498 { NM_ACTIVE_CONNECTION_MASTER, &priv->master, NULL, NM_TYPE_DEVICE },
503 NM_OBJECT_CLASS (nm_active_connection_parent_class)->init_dbus (object);
505 _nm_object_register_properties (object,
506 NM_DBUS_INTERFACE_ACTIVE_CONNECTION,
512 nm_active_connection_class_init (NMActiveConnectionClass *ap_class)
514 GObjectClass *object_class = G_OBJECT_CLASS (ap_class);
515 NMObjectClass *nm_object_class = NM_OBJECT_CLASS (ap_class);
517 g_type_class_add_private (ap_class, sizeof (NMActiveConnectionPrivate));
519 _nm_object_class_add_interface (nm_object_class, NM_DBUS_INTERFACE_ACTIVE_CONNECTION);
521 /* virtual methods */
522 object_class->get_property = get_property;
523 object_class->dispose = dispose;
524 object_class->finalize = finalize;
526 nm_object_class->init_dbus = init_dbus;
531 * NMActiveConnection:connection:
533 * The connection that this is an active instance of.
535 g_object_class_install_property
536 (object_class, PROP_CONNECTION,
537 g_param_spec_object (NM_ACTIVE_CONNECTION_CONNECTION, "", "",
538 NM_TYPE_REMOTE_CONNECTION,
540 G_PARAM_STATIC_STRINGS));
543 * NMActiveConnection:id:
545 * The active connection's ID
547 g_object_class_install_property
548 (object_class, PROP_ID,
549 g_param_spec_string (NM_ACTIVE_CONNECTION_ID, "", "",
552 G_PARAM_STATIC_STRINGS));
555 * NMActiveConnection:uuid:
557 * The active connection's UUID
559 g_object_class_install_property
560 (object_class, PROP_UUID,
561 g_param_spec_string (NM_ACTIVE_CONNECTION_UUID, "", "",
564 G_PARAM_STATIC_STRINGS));
567 * NMActiveConnection:type:
569 * The active connection's type
571 g_object_class_install_property
572 (object_class, PROP_TYPE,
573 g_param_spec_string (NM_ACTIVE_CONNECTION_TYPE, "", "",
576 G_PARAM_STATIC_STRINGS));
579 * NMActiveConnection:specific-object-path:
581 * The path to the "specific object" of the active connection; see
582 * nm_active_connection_get_specific_object_path() for more details.
584 g_object_class_install_property
585 (object_class, PROP_SPECIFIC_OBJECT_PATH,
586 g_param_spec_string (NM_ACTIVE_CONNECTION_SPECIFIC_OBJECT_PATH, "", "",
589 G_PARAM_STATIC_STRINGS));
592 * NMActiveConnection:devices:
594 * The devices of the active connection.
596 * Element-type: NMDevice
598 g_object_class_install_property
599 (object_class, PROP_DEVICES,
600 g_param_spec_boxed (NM_ACTIVE_CONNECTION_DEVICES, "", "",
603 G_PARAM_STATIC_STRINGS));
606 * NMActiveConnection:state:
608 * The state of the active connection.
610 g_object_class_install_property
611 (object_class, PROP_STATE,
612 g_param_spec_enum (NM_ACTIVE_CONNECTION_STATE, "", "",
613 NM_TYPE_ACTIVE_CONNECTION_STATE,
614 NM_ACTIVE_CONNECTION_STATE_UNKNOWN,
616 G_PARAM_STATIC_STRINGS));
619 * NMActiveConnection:default:
621 * Whether the active connection is the default IPv4 one.
623 g_object_class_install_property
624 (object_class, PROP_DEFAULT,
625 g_param_spec_boolean (NM_ACTIVE_CONNECTION_DEFAULT, "", "",
628 G_PARAM_STATIC_STRINGS));
631 * NMActiveConnection:ip4-config:
633 * The IPv4 #NMIPConfig of the connection.
635 g_object_class_install_property
636 (object_class, PROP_IP4_CONFIG,
637 g_param_spec_object (NM_ACTIVE_CONNECTION_IP4_CONFIG, "", "",
640 G_PARAM_STATIC_STRINGS));
643 * NMActiveConnection:dhcp4-config:
645 * The IPv4 #NMDhcpConfig of the connection.
647 g_object_class_install_property
648 (object_class, PROP_DHCP4_CONFIG,
649 g_param_spec_object (NM_ACTIVE_CONNECTION_DHCP4_CONFIG, "", "",
652 G_PARAM_STATIC_STRINGS));
655 * NMActiveConnection:default6:
657 * Whether the active connection is the default IPv6 one.
659 g_object_class_install_property
660 (object_class, PROP_DEFAULT6,
661 g_param_spec_boolean (NM_ACTIVE_CONNECTION_DEFAULT6, "", "",
664 G_PARAM_STATIC_STRINGS));
667 * NMActiveConnection:ip6-config:
669 * The IPv6 #NMIPConfig of the connection.
671 g_object_class_install_property
672 (object_class, PROP_IP6_CONFIG,
673 g_param_spec_object (NM_ACTIVE_CONNECTION_IP6_CONFIG, "", "",
676 G_PARAM_STATIC_STRINGS));
679 * NMActiveConnection:dhcp6-config:
681 * The IPv6 #NMDhcpConfig of the connection.
683 g_object_class_install_property
684 (object_class, PROP_DHCP6_CONFIG,
685 g_param_spec_object (NM_ACTIVE_CONNECTION_DHCP6_CONFIG, "", "",
688 G_PARAM_STATIC_STRINGS));
691 * NMActiveConnection:vpn:
693 * Whether the active connection is a VPN connection.
695 g_object_class_install_property
696 (object_class, PROP_VPN,
697 g_param_spec_boolean (NM_ACTIVE_CONNECTION_VPN, "", "",
700 G_PARAM_STATIC_STRINGS));
703 * NMActiveConnection:master:
705 * The master device if one exists.
707 g_object_class_install_property
708 (object_class, PROP_MASTER,
709 g_param_spec_object (NM_ACTIVE_CONNECTION_MASTER, "", "",
712 G_PARAM_STATIC_STRINGS));