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 2012 Red Hat, Inc.
21 #include "nm-default.h"
24 #include <netinet/ether.h>
26 #include "nm-setting-connection.h"
27 #include "nm-setting-bridge.h"
30 #include "nm-device-bridge.h"
31 #include "nm-device-private.h"
32 #include "nm-object-private.h"
35 G_DEFINE_TYPE (NMDeviceBridge, nm_device_bridge, NM_TYPE_DEVICE)
37 #define NM_DEVICE_BRIDGE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_BRIDGE, NMDeviceBridgePrivate))
45 } NMDeviceBridgePrivate;
57 * nm_device_bridge_error_quark:
59 * Registers an error quark for #NMDeviceBridge if necessary.
61 * Returns: the error quark used for #NMDeviceBridge errors.
66 nm_device_bridge_error_quark (void)
68 static GQuark quark = 0;
70 if (G_UNLIKELY (quark == 0))
71 quark = g_quark_from_static_string ("nm-device-bridge-error-quark");
76 * nm_device_bridge_new:
77 * @connection: the #DBusGConnection
78 * @path: the DBus object path of the device
80 * Creates a new #NMDeviceBridge.
82 * Returns: (transfer full): a new device
87 nm_device_bridge_new (DBusGConnection *connection, const char *path)
91 g_return_val_if_fail (connection != NULL, NULL);
92 g_return_val_if_fail (path != NULL, NULL);
94 device = g_object_new (NM_TYPE_DEVICE_BRIDGE,
95 NM_OBJECT_DBUS_CONNECTION, connection,
96 NM_OBJECT_DBUS_PATH, path,
98 _nm_object_ensure_inited (NM_OBJECT (device));
103 * nm_device_bridge_get_hw_address:
104 * @device: a #NMDeviceBridge
106 * Gets the hardware (MAC) address of the #NMDeviceBridge
108 * Returns: the hardware address. This is the internal string used by the
109 * device, and must not be modified.
114 nm_device_bridge_get_hw_address (NMDeviceBridge *device)
116 g_return_val_if_fail (NM_IS_DEVICE_BRIDGE (device), NULL);
118 _nm_object_ensure_inited (NM_OBJECT (device));
119 return NM_DEVICE_BRIDGE_GET_PRIVATE (device)->hw_address;
123 * nm_device_bridge_get_carrier:
124 * @device: a #NMDeviceBridge
126 * Whether the device has carrier.
128 * Returns: %TRUE if the device has carrier
133 nm_device_bridge_get_carrier (NMDeviceBridge *device)
135 g_return_val_if_fail (NM_IS_DEVICE_BRIDGE (device), FALSE);
137 _nm_object_ensure_inited (NM_OBJECT (device));
138 return NM_DEVICE_BRIDGE_GET_PRIVATE (device)->carrier;
142 * nm_device_bridge_get_slaves:
143 * @device: a #NMDeviceBridge
145 * Gets the devices currently slaved to @device.
147 * Returns: (element-type NMDevice): the #GPtrArray containing
148 * #NMDevices that are slaves of @device. This is the internal
149 * copy used by the device, and must not be modified.
154 nm_device_bridge_get_slaves (NMDeviceBridge *device)
156 g_return_val_if_fail (NM_IS_DEVICE_BRIDGE (device), FALSE);
158 _nm_object_ensure_inited (NM_OBJECT (device));
159 return handle_ptr_array_return (NM_DEVICE_BRIDGE_GET_PRIVATE (device)->slaves);
163 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
165 NMSettingConnection *s_con;
166 NMSettingBridge *s_bridge;
167 const char *ctype, *dev_iface_name, *bridge_iface_name;
169 s_con = nm_connection_get_setting_connection (connection);
172 ctype = nm_setting_connection_get_connection_type (s_con);
173 if (strcmp (ctype, NM_SETTING_BRIDGE_SETTING_NAME) != 0) {
174 g_set_error (error, NM_DEVICE_BRIDGE_ERROR, NM_DEVICE_BRIDGE_ERROR_NOT_BRIDGE_CONNECTION,
175 "The connection was not a bridge connection.");
179 s_bridge = nm_connection_get_setting_bridge (connection);
181 g_set_error (error, NM_DEVICE_BRIDGE_ERROR, NM_DEVICE_BRIDGE_ERROR_INVALID_BRIDGE_CONNECTION,
182 "The connection was not a valid bridge connection.");
186 dev_iface_name = nm_device_get_iface (device);
187 bridge_iface_name = nm_setting_bridge_get_interface_name (s_bridge);
188 if (g_strcmp0 (dev_iface_name, bridge_iface_name) != 0) {
189 g_set_error (error, NM_DEVICE_BRIDGE_ERROR, NM_DEVICE_BRIDGE_ERROR_INTERFACE_MISMATCH,
190 "The interfaces of the device and the connection didn't match.");
194 /* FIXME: check ports? */
196 return NM_DEVICE_CLASS (nm_device_bridge_parent_class)->connection_compatible (device, connection, error);
200 get_setting_type (NMDevice *device)
202 return NM_TYPE_SETTING_BRIDGE;
206 get_hw_address (NMDevice *device)
208 return nm_device_bridge_get_hw_address (NM_DEVICE_BRIDGE (device));
211 /***********************************************************/
214 nm_device_bridge_init (NMDeviceBridge *device)
216 _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_BRIDGE);
220 register_properties (NMDeviceBridge *device)
222 NMDeviceBridgePrivate *priv = NM_DEVICE_BRIDGE_GET_PRIVATE (device);
223 const NMPropertiesInfo property_info[] = {
224 { NM_DEVICE_BRIDGE_HW_ADDRESS, &priv->hw_address },
225 { NM_DEVICE_BRIDGE_CARRIER, &priv->carrier },
226 { NM_DEVICE_BRIDGE_SLAVES, &priv->slaves, NULL, NM_TYPE_DEVICE },
230 _nm_object_register_properties (NM_OBJECT (device),
236 constructed (GObject *object)
238 NMDeviceBridgePrivate *priv = NM_DEVICE_BRIDGE_GET_PRIVATE (object);
240 G_OBJECT_CLASS (nm_device_bridge_parent_class)->constructed (object);
242 priv->proxy = _nm_object_new_proxy (NM_OBJECT (object), NULL, NM_DBUS_INTERFACE_DEVICE_BRIDGE);
243 register_properties (NM_DEVICE_BRIDGE (object));
247 dispose (GObject *object)
249 NMDeviceBridgePrivate *priv = NM_DEVICE_BRIDGE_GET_PRIVATE (object);
251 g_clear_object (&priv->proxy);
254 g_ptr_array_set_free_func (priv->slaves, g_object_unref);
255 g_ptr_array_free (priv->slaves, TRUE);
259 G_OBJECT_CLASS (nm_device_bridge_parent_class)->dispose (object);
263 finalize (GObject *object)
265 NMDeviceBridgePrivate *priv = NM_DEVICE_BRIDGE_GET_PRIVATE (object);
267 g_free (priv->hw_address);
269 G_OBJECT_CLASS (nm_device_bridge_parent_class)->finalize (object);
273 get_property (GObject *object,
278 NMDeviceBridge *device = NM_DEVICE_BRIDGE (object);
280 _nm_object_ensure_inited (NM_OBJECT (object));
283 case PROP_HW_ADDRESS:
284 g_value_set_string (value, nm_device_bridge_get_hw_address (device));
287 g_value_set_boolean (value, nm_device_bridge_get_carrier (device));
290 g_value_set_boxed (value, nm_device_bridge_get_slaves (device));
293 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
299 nm_device_bridge_class_init (NMDeviceBridgeClass *bridge_class)
301 GObjectClass *object_class = G_OBJECT_CLASS (bridge_class);
302 NMDeviceClass *device_class = NM_DEVICE_CLASS (bridge_class);
304 g_type_class_add_private (bridge_class, sizeof (NMDeviceBridgePrivate));
306 /* virtual methods */
307 object_class->constructed = constructed;
308 object_class->dispose = dispose;
309 object_class->finalize = finalize;
310 object_class->get_property = get_property;
311 device_class->connection_compatible = connection_compatible;
312 device_class->get_setting_type = get_setting_type;
313 device_class->get_hw_address = get_hw_address;
318 * NMDeviceBridge:hw-address:
320 * The hardware (MAC) address of the device.
324 g_object_class_install_property
325 (object_class, PROP_HW_ADDRESS,
326 g_param_spec_string (NM_DEVICE_BRIDGE_HW_ADDRESS, "", "",
329 G_PARAM_STATIC_STRINGS));
332 * NMDeviceBridge:carrier:
334 * Whether the device has carrier.
338 g_object_class_install_property
339 (object_class, PROP_CARRIER,
340 g_param_spec_boolean (NM_DEVICE_BRIDGE_CARRIER, "", "",
343 G_PARAM_STATIC_STRINGS));
346 * NMDeviceBridge:slaves:
348 * The devices (#NMDevice) slaved to the bridge device.
352 g_object_class_install_property
353 (object_class, PROP_SLAVES,
354 g_param_spec_boxed (NM_DEVICE_BRIDGE_SLAVES, "", "",
355 NM_TYPE_OBJECT_ARRAY,
357 G_PARAM_STATIC_STRINGS));