device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm / nm-device-bond.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /*
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.
7  *
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.
12  *
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.
17  *
18  * Copyright 2012 Red Hat, Inc.
19  */
20
21 #include "nm-default.h"
22
23 #include <string.h>
24
25 #include "nm-setting-connection.h"
26 #include "nm-setting-bond.h"
27 #include "nm-utils.h"
28
29 #include "nm-device-bond.h"
30 #include "nm-device-private.h"
31 #include "nm-object-private.h"
32 #include "nm-core-internal.h"
33
34 G_DEFINE_TYPE (NMDeviceBond, nm_device_bond, NM_TYPE_DEVICE)
35
36 #define NM_DEVICE_BOND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_BOND, NMDeviceBondPrivate))
37
38 typedef struct {
39         char *hw_address;
40         gboolean carrier;
41         GPtrArray *slaves;
42 } NMDeviceBondPrivate;
43
44 enum {
45         PROP_0,
46         PROP_HW_ADDRESS,
47         PROP_CARRIER,
48         PROP_SLAVES,
49
50         LAST_PROP
51 };
52
53 /**
54  * nm_device_bond_get_hw_address:
55  * @device: a #NMDeviceBond
56  *
57  * Gets the hardware (MAC) address of the #NMDeviceBond
58  *
59  * Returns: the hardware address. This is the internal string used by the
60  * device, and must not be modified.
61  **/
62 const char *
63 nm_device_bond_get_hw_address (NMDeviceBond *device)
64 {
65         g_return_val_if_fail (NM_IS_DEVICE_BOND (device), NULL);
66
67         return NM_DEVICE_BOND_GET_PRIVATE (device)->hw_address;
68 }
69
70 /**
71  * nm_device_bond_get_carrier:
72  * @device: a #NMDeviceBond
73  *
74  * Whether the device has carrier.
75  *
76  * Returns: %TRUE if the device has carrier
77  **/
78 gboolean
79 nm_device_bond_get_carrier (NMDeviceBond *device)
80 {
81         g_return_val_if_fail (NM_IS_DEVICE_BOND (device), FALSE);
82
83         return NM_DEVICE_BOND_GET_PRIVATE (device)->carrier;
84 }
85
86 /**
87  * nm_device_bond_get_slaves:
88  * @device: a #NMDeviceBond
89  *
90  * Gets the devices currently slaved to @device.
91  *
92  * Returns: (element-type NMDevice): the #GPtrArray containing
93  * #NMDevices that are slaves of @device. This is the internal
94  * copy used by the device, and must not be modified.
95  **/
96 const GPtrArray *
97 nm_device_bond_get_slaves (NMDeviceBond *device)
98 {
99         g_return_val_if_fail (NM_IS_DEVICE_BOND (device), FALSE);
100
101         return NM_DEVICE_BOND_GET_PRIVATE (device)->slaves;
102 }
103
104 static gboolean
105 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
106 {
107         if (!NM_DEVICE_CLASS (nm_device_bond_parent_class)->connection_compatible (device, connection, error))
108                 return FALSE;
109
110         if (!nm_connection_is_type (connection, NM_SETTING_BOND_SETTING_NAME)) {
111                 g_set_error_literal (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION,
112                                      _("The connection was not a bond connection."));
113                 return FALSE;
114         }
115
116         /* FIXME: check slaves? */
117
118         return TRUE;
119 }
120
121 static GType
122 get_setting_type (NMDevice *device)
123 {
124         return NM_TYPE_SETTING_BOND;
125 }
126
127 static const char *
128 get_hw_address (NMDevice *device)
129 {
130         return nm_device_bond_get_hw_address (NM_DEVICE_BOND (device));
131 }
132
133 /***********************************************************/
134
135 static void
136 nm_device_bond_init (NMDeviceBond *device)
137 {
138         NMDeviceBondPrivate *priv = NM_DEVICE_BOND_GET_PRIVATE (device);
139
140         _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_BOND);
141
142         priv->slaves = g_ptr_array_new ();
143 }
144
145 static void
146 init_dbus (NMObject *object)
147 {
148         NMDeviceBondPrivate *priv = NM_DEVICE_BOND_GET_PRIVATE (object);
149         const NMPropertiesInfo property_info[] = {
150                 { NM_DEVICE_BOND_HW_ADDRESS, &priv->hw_address },
151                 { NM_DEVICE_BOND_CARRIER,    &priv->carrier },
152                 { NM_DEVICE_BOND_SLAVES,     &priv->slaves, NULL, NM_TYPE_DEVICE },
153                 { NULL },
154         };
155
156         NM_OBJECT_CLASS (nm_device_bond_parent_class)->init_dbus (object);
157
158         _nm_object_register_properties (object,
159                                         NM_DBUS_INTERFACE_DEVICE_BOND,
160                                         property_info);
161 }
162
163 static void
164 dispose (GObject *object)
165 {
166         NMDeviceBondPrivate *priv = NM_DEVICE_BOND_GET_PRIVATE (object);
167
168         g_clear_pointer (&priv->slaves, g_ptr_array_unref);
169
170         G_OBJECT_CLASS (nm_device_bond_parent_class)->dispose (object);
171 }
172
173 static void
174 finalize (GObject *object)
175 {
176         NMDeviceBondPrivate *priv = NM_DEVICE_BOND_GET_PRIVATE (object);
177
178         g_free (priv->hw_address);
179
180         G_OBJECT_CLASS (nm_device_bond_parent_class)->finalize (object);
181 }
182
183 static void
184 get_property (GObject *object,
185               guint prop_id,
186               GValue *value,
187               GParamSpec *pspec)
188 {
189         NMDeviceBond *device = NM_DEVICE_BOND (object);
190
191         switch (prop_id) {
192         case PROP_HW_ADDRESS:
193                 g_value_set_string (value, nm_device_bond_get_hw_address (device));
194                 break;
195         case PROP_CARRIER:
196                 g_value_set_boolean (value, nm_device_bond_get_carrier (device));
197                 break;
198         case PROP_SLAVES:
199                 g_value_take_boxed (value, _nm_utils_copy_object_array (nm_device_bond_get_slaves (device)));
200                 break;
201         default:
202                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
203                 break;
204         }
205 }
206
207 static void
208 nm_device_bond_class_init (NMDeviceBondClass *bond_class)
209 {
210         GObjectClass *object_class = G_OBJECT_CLASS (bond_class);
211         NMObjectClass *nm_object_class = NM_OBJECT_CLASS (bond_class);
212         NMDeviceClass *device_class = NM_DEVICE_CLASS (bond_class);
213
214         g_type_class_add_private (bond_class, sizeof (NMDeviceBondPrivate));
215
216         _nm_object_class_add_interface (nm_object_class, NM_DBUS_INTERFACE_DEVICE_BOND);
217
218         /* virtual methods */
219         object_class->dispose = dispose;
220         object_class->finalize = finalize;
221         object_class->get_property = get_property;
222
223         nm_object_class->init_dbus = init_dbus;
224
225         device_class->connection_compatible = connection_compatible;
226         device_class->get_setting_type = get_setting_type;
227         device_class->get_hw_address = get_hw_address;
228
229         /* properties */
230
231         /**
232          * NMDeviceBond:hw-address:
233          *
234          * The hardware (MAC) address of the device.
235          **/
236         g_object_class_install_property
237                 (object_class, PROP_HW_ADDRESS,
238                  g_param_spec_string (NM_DEVICE_BOND_HW_ADDRESS, "", "",
239                                       NULL,
240                                       G_PARAM_READABLE |
241                                       G_PARAM_STATIC_STRINGS));
242
243         /**
244          * NMDeviceBond:carrier:
245          *
246          * Whether the device has carrier.
247          **/
248         g_object_class_install_property
249                 (object_class, PROP_CARRIER,
250                  g_param_spec_boolean (NM_DEVICE_BOND_CARRIER, "", "",
251                                        FALSE,
252                                        G_PARAM_READABLE |
253                                        G_PARAM_STATIC_STRINGS));
254
255         /**
256          * NMDeviceBond:slaves:
257          *
258          * The devices slaved to the bond device.
259          *
260          * Element-type: NMDevice
261          **/
262         g_object_class_install_property
263                 (object_class, PROP_SLAVES,
264                  g_param_spec_boxed (NM_DEVICE_BOND_SLAVES, "", "",
265                                      G_TYPE_PTR_ARRAY,
266                                      G_PARAM_READABLE |
267                                      G_PARAM_STATIC_STRINGS));
268 }