device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-glib / nm-device-generic.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 2013 Red Hat, Inc.
19  */
20
21 #include "nm-default.h"
22
23 #include <string.h>
24
25 #include "nm-device-generic.h"
26 #include "nm-device-private.h"
27 #include "nm-object-private.h"
28 #include "nm-setting-generic.h"
29
30 G_DEFINE_TYPE (NMDeviceGeneric, nm_device_generic, NM_TYPE_DEVICE)
31
32 #define NM_DEVICE_GENERIC_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_GENERIC, NMDeviceGenericPrivate))
33
34 typedef struct {
35         DBusGProxy *proxy;
36
37         char *hw_address;
38         char *type_description;
39 } NMDeviceGenericPrivate;
40
41 enum {
42         PROP_0,
43         PROP_HW_ADDRESS,
44         PROP_TYPE_DESCRIPTION,
45
46         LAST_PROP
47 };
48
49 /**
50  * nm_device_generic_error_quark:
51  *
52  * Registers an error quark for #NMDeviceGeneric if necessary.
53  *
54  * Returns: the error quark used for #NMDeviceGeneric errors.
55  *
56  * Since: 0.9.10
57  **/
58 GQuark
59 nm_device_generic_error_quark (void)
60 {
61         static GQuark quark = 0;
62
63         if (G_UNLIKELY (quark == 0))
64                 quark = g_quark_from_static_string ("nm-device-generic-error-quark");
65         return quark;
66 }
67
68 /**
69  * nm_device_generic_new:
70  * @connection: the #DBusGConnection
71  * @path: the DBus object path of the device
72  *
73  * Creates a new #NMDeviceGeneric.
74  *
75  * Returns: (transfer full): a new device
76  *
77  * Since: 0.9.10
78  **/
79 GObject *
80 nm_device_generic_new (DBusGConnection *connection, const char *path)
81 {
82         GObject *device;
83
84         g_return_val_if_fail (connection != NULL, NULL);
85         g_return_val_if_fail (path != NULL, NULL);
86
87         device = g_object_new (NM_TYPE_DEVICE_GENERIC,
88                                NM_OBJECT_DBUS_CONNECTION, connection,
89                                NM_OBJECT_DBUS_PATH, path,
90                                NULL);
91         _nm_object_ensure_inited (NM_OBJECT (device));
92         return device;
93 }
94
95 /**
96  * nm_device_generic_get_hw_address:
97  * @device: a #NMDeviceGeneric
98  *
99  * Gets the hardware address of the #NMDeviceGeneric
100  *
101  * Returns: the hardware address. This is the internal string used by the
102  * device, and must not be modified.
103  *
104  * Since: 0.9.10
105  **/
106 const char *
107 nm_device_generic_get_hw_address (NMDeviceGeneric *device)
108 {
109         g_return_val_if_fail (NM_IS_DEVICE_GENERIC (device), NULL);
110
111         _nm_object_ensure_inited (NM_OBJECT (device));
112         return NM_DEVICE_GENERIC_GET_PRIVATE (device)->hw_address;
113 }
114
115 /***********************************************************/
116
117 static const char *
118 get_type_description (NMDevice *device)
119 {
120         NMDeviceGenericPrivate *priv = NM_DEVICE_GENERIC_GET_PRIVATE (device);
121
122         _nm_object_ensure_inited (NM_OBJECT (device));
123         return priv->type_description;
124 }
125
126 static const char *
127 get_hw_address (NMDevice *device)
128 {
129         return nm_device_generic_get_hw_address (NM_DEVICE_GENERIC (device));
130 }
131
132 static gboolean
133 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
134 {
135         NMSettingConnection *s_con;
136         const char *ctype, *iface_name;
137
138         s_con = nm_connection_get_setting_connection (connection);
139         g_assert (s_con);
140
141         ctype = nm_setting_connection_get_connection_type (s_con);
142         if (strcmp (ctype, NM_SETTING_GENERIC_SETTING_NAME) != 0) {
143                 g_set_error (error, NM_DEVICE_GENERIC_ERROR, NM_DEVICE_GENERIC_ERROR_NOT_GENERIC_CONNECTION,
144                              "The connection was not a generic connection.");
145                 return FALSE;
146         }
147
148         iface_name = nm_setting_connection_get_interface_name (s_con);
149         if (!iface_name) {
150                 g_set_error (error, NM_DEVICE_GENERIC_ERROR, NM_DEVICE_GENERIC_ERROR_MISSING_INTERFACE_NAME,
151                              "The connection did not specify an interface name.");
152                 return FALSE;
153         }
154
155         return NM_DEVICE_CLASS (nm_device_generic_parent_class)->connection_compatible (device, connection, error);
156 }
157
158 static GType
159 get_setting_type (NMDevice *device)
160 {
161         return NM_TYPE_SETTING_GENERIC;
162 }
163
164 /***********************************************************/
165
166 static void
167 nm_device_generic_init (NMDeviceGeneric *device)
168 {
169         _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_GENERIC);
170 }
171
172 static void
173 register_properties (NMDeviceGeneric *device)
174 {
175         NMDeviceGenericPrivate *priv = NM_DEVICE_GENERIC_GET_PRIVATE (device);
176         const NMPropertiesInfo property_info[] = {
177                 { NM_DEVICE_GENERIC_HW_ADDRESS, &priv->hw_address },
178                 { NM_DEVICE_GENERIC_TYPE_DESCRIPTION, &priv->type_description },
179                 { NULL },
180         };
181
182         _nm_object_register_properties (NM_OBJECT (device),
183                                         priv->proxy,
184                                         property_info);
185 }
186
187 static const char *
188 _device_type_to_interface (NMDeviceType type)
189 {
190         switch (type) {
191         case NM_DEVICE_TYPE_GENERIC:
192                 return NM_DBUS_INTERFACE_DEVICE_GENERIC;
193         case NM_DEVICE_TYPE_TUN:
194                 return NM_DBUS_INTERFACE_DEVICE_TUN;
195         default:
196                 return NULL;
197         }
198 }
199
200 static void
201 constructed (GObject *object)
202 {
203         NMDeviceGenericPrivate *priv = NM_DEVICE_GENERIC_GET_PRIVATE (object);
204         NMDeviceType type;
205         DBusGConnection *connection;
206         const char *path, *interface;
207
208         G_OBJECT_CLASS (nm_device_generic_parent_class)->constructed (object);
209
210         g_object_get (object,
211                       NM_OBJECT_DBUS_CONNECTION, &connection,
212                       NM_OBJECT_DBUS_PATH, &path,
213                       NULL);
214
215         type = _nm_device_type_for_path (connection, path);
216         interface = _device_type_to_interface (type);
217
218         if (interface) {
219                 priv->proxy = _nm_object_new_proxy (NM_OBJECT (object), NULL, interface);
220                 register_properties (NM_DEVICE_GENERIC (object));
221         }
222 }
223
224 static void
225 dispose (GObject *object)
226 {
227         NMDeviceGenericPrivate *priv = NM_DEVICE_GENERIC_GET_PRIVATE (object);
228
229         g_clear_object (&priv->proxy);
230
231         G_OBJECT_CLASS (nm_device_generic_parent_class)->dispose (object);
232 }
233
234 static void
235 finalize (GObject *object)
236 {
237         NMDeviceGenericPrivate *priv = NM_DEVICE_GENERIC_GET_PRIVATE (object);
238
239         g_free (priv->hw_address);
240         g_free (priv->type_description);
241
242         G_OBJECT_CLASS (nm_device_generic_parent_class)->finalize (object);
243 }
244
245 static void
246 get_property (GObject *object,
247               guint prop_id,
248               GValue *value,
249               GParamSpec *pspec)
250 {
251         NMDeviceGenericPrivate *priv = NM_DEVICE_GENERIC_GET_PRIVATE (object);
252
253         _nm_object_ensure_inited (NM_OBJECT (object));
254
255         switch (prop_id) {
256         case PROP_HW_ADDRESS:
257                 g_value_set_string (value, priv->hw_address);
258                 break;
259         case PROP_TYPE_DESCRIPTION:
260                 g_value_set_string (value, priv->type_description);
261                 break;
262         default:
263                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
264                 break;
265         }
266 }
267
268 static void
269 nm_device_generic_class_init (NMDeviceGenericClass *klass)
270 {
271         GObjectClass *object_class = G_OBJECT_CLASS (klass);
272         NMDeviceClass *device_class = NM_DEVICE_CLASS (klass);
273
274         g_type_class_add_private (klass, sizeof (NMDeviceGenericPrivate));
275
276         object_class->constructed = constructed;
277         object_class->dispose = dispose;
278         object_class->finalize = finalize;
279         object_class->get_property = get_property;
280
281         device_class->get_type_description = get_type_description;
282         device_class->get_hw_address = get_hw_address;
283         device_class->connection_compatible = connection_compatible;
284         device_class->get_setting_type = get_setting_type;
285
286         /**
287          * NMDeviceGeneric:hw-address:
288          *
289          * The hardware address of the device.
290          **/
291         g_object_class_install_property
292                 (object_class, PROP_HW_ADDRESS,
293                  g_param_spec_string (NM_DEVICE_GENERIC_HW_ADDRESS, "", "",
294                                       NULL,
295                                       G_PARAM_READABLE |
296                                       G_PARAM_STATIC_STRINGS));
297
298         /**
299          * NMDeviceGeneric:type-description:
300          *
301          * A description of the specific type of device this is, or %NULL
302          * if not known.
303          **/
304         g_object_class_install_property
305                 (object_class, PROP_TYPE_DESCRIPTION,
306                  g_param_spec_string (NM_DEVICE_GENERIC_TYPE_DESCRIPTION, "", "",
307                                       NULL,
308                                       G_PARAM_READABLE |
309                                       G_PARAM_STATIC_STRINGS));
310 }