device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-glib / nm-ip4-config.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 2007 - 2011 Novell, Inc.
19  * Copyright 2008 Red Hat, Inc.
20  */
21
22 #include "nm-default.h"
23
24 #include <string.h>
25
26 #include "nm-setting-ip4-config.h"
27 #include "nm-ip4-config.h"
28 #include "NetworkManager.h"
29 #include "nm-types-private.h"
30 #include "nm-object-private.h"
31 #include "nm-utils.h"
32
33 G_DEFINE_TYPE (NMIP4Config, nm_ip4_config, NM_TYPE_OBJECT)
34
35 #define NM_IP4_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_IP4_CONFIG, NMIP4ConfigPrivate))
36
37 typedef struct {
38         DBusGProxy *proxy;
39
40         char *gateway;
41         GSList *addresses;
42         GSList *routes;
43         GArray *nameservers;
44         GPtrArray *domains;
45         GPtrArray *searches;
46         GArray *wins;
47 } NMIP4ConfigPrivate;
48
49 enum {
50         PROP_0,
51         PROP_GATEWAY,
52         PROP_ADDRESSES,
53         PROP_ROUTES,
54         PROP_NAMESERVERS,
55         PROP_DOMAINS,
56         PROP_SEARCHES,
57         PROP_WINS_SERVERS,
58
59         LAST_PROP
60 };
61
62 static void
63 nm_ip4_config_init (NMIP4Config *config)
64 {
65 }
66
67 static gboolean
68 demarshal_ip4_address_array (NMObject *object, GParamSpec *pspec, GValue *value, gpointer field)
69 {
70         NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (object);
71
72         g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip4_address_unref);
73         priv->addresses = NULL;
74
75         priv->addresses = nm_utils_ip4_addresses_from_gvalue (value);
76         _nm_object_queue_notify (object, NM_IP4_CONFIG_ADDRESSES);
77
78         return TRUE;
79 }
80
81 static gboolean
82 demarshal_ip4_array (NMObject *object, GParamSpec *pspec, GValue *value, gpointer field)
83 {
84         if (!_nm_uint_array_demarshal (value, (GArray **) field))
85                 return FALSE;
86
87         if (!strcmp (pspec->name, NM_IP4_CONFIG_NAMESERVERS))
88                 _nm_object_queue_notify (object, NM_IP4_CONFIG_NAMESERVERS);
89         else if (!strcmp (pspec->name, NM_IP4_CONFIG_WINS_SERVERS))
90                 _nm_object_queue_notify (object, NM_IP4_CONFIG_WINS_SERVERS);
91
92         return TRUE;
93 }
94
95 static gboolean
96 demarshal_string_array (NMObject *object, GParamSpec *pspec, GValue *value, gpointer field)
97 {
98         if (!_nm_string_array_demarshal (value, (GPtrArray **) field))
99                 return FALSE;
100
101         _nm_object_queue_notify (object, pspec->name);
102         return TRUE;
103 }
104
105 static gboolean
106 demarshal_ip4_routes_array (NMObject *object, GParamSpec *pspec, GValue *value, gpointer field)
107 {
108         NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (object);
109
110         g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip4_route_unref);
111         priv->routes = NULL;
112
113         priv->routes = nm_utils_ip4_routes_from_gvalue (value);
114         _nm_object_queue_notify (object, NM_IP4_CONFIG_ROUTES);
115
116         return TRUE;
117 }
118
119 static void
120 register_properties (NMIP4Config *config)
121 {
122         NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (config);
123         const NMPropertiesInfo property_info[] = {
124                 { NM_IP4_CONFIG_GATEWAY,      &priv->gateway, },
125                 { NM_IP4_CONFIG_ADDRESSES,    &priv->addresses, demarshal_ip4_address_array },
126                 { NM_IP4_CONFIG_ROUTES,       &priv->routes, demarshal_ip4_routes_array },
127                 { NM_IP4_CONFIG_NAMESERVERS,  &priv->nameservers, demarshal_ip4_array },
128                 { NM_IP4_CONFIG_DOMAINS,      &priv->domains, demarshal_string_array },
129                 { NM_IP4_CONFIG_SEARCHES,     &priv->searches, demarshal_string_array },
130                 { NM_IP4_CONFIG_WINS_SERVERS, &priv->wins, demarshal_ip4_array },
131                 { NULL },
132         };
133
134         _nm_object_register_properties (NM_OBJECT (config),
135                                         priv->proxy,
136                                         property_info);
137 }
138
139 static void
140 constructed (GObject *object)
141 {
142         NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (object);
143
144         G_OBJECT_CLASS (nm_ip4_config_parent_class)->constructed (object);
145
146         priv->proxy = _nm_object_new_proxy (NM_OBJECT (object), NULL, NM_DBUS_INTERFACE_IP4_CONFIG);
147         register_properties (NM_IP4_CONFIG (object));
148 }
149
150 static void
151 finalize (GObject *object)
152 {
153         NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (object);
154
155         g_free (priv->gateway);
156
157         g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip4_address_unref);
158         g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip4_route_unref);
159
160         if (priv->nameservers)
161                 g_array_free (priv->nameservers, TRUE);
162
163         if (priv->wins)
164                 g_array_free (priv->wins, TRUE);
165
166         if (priv->domains) {
167                 g_ptr_array_set_free_func (priv->domains, g_free);
168                 g_ptr_array_free (priv->domains, TRUE);
169         }
170
171         if (priv->searches) {
172                 g_ptr_array_set_free_func (priv->searches, g_free);
173                 g_ptr_array_free (priv->searches, TRUE);
174         }
175
176         g_object_unref (priv->proxy);
177
178         G_OBJECT_CLASS (nm_ip4_config_parent_class)->finalize (object);
179 }
180
181 static void
182 get_property (GObject *object,
183               guint prop_id,
184               GValue *value,
185               GParamSpec *pspec)
186 {
187         NMIP4Config *self = NM_IP4_CONFIG (object);
188         NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (self);
189
190         _nm_object_ensure_inited (NM_OBJECT (object));
191
192         switch (prop_id) {
193         case PROP_GATEWAY:
194                 g_value_set_string (value, nm_ip4_config_get_gateway (self));
195                 break;
196         case PROP_ADDRESSES:
197                 nm_utils_ip4_addresses_to_gvalue (priv->addresses, value);
198                 break;
199         case PROP_ROUTES:
200                 nm_utils_ip4_routes_to_gvalue (priv->routes, value);
201                 break;
202         case PROP_NAMESERVERS:
203                 g_value_set_boxed (value, nm_ip4_config_get_nameservers (self));
204                 break;
205         case PROP_DOMAINS:
206                 g_value_set_boxed (value, nm_ip4_config_get_domains (self));
207                 break;
208         case PROP_SEARCHES:
209                 g_value_set_boxed (value, nm_ip4_config_get_searches (self));
210                 break;
211         case PROP_WINS_SERVERS:
212                 g_value_set_boxed (value, nm_ip4_config_get_wins_servers (self));
213                 break;
214         default:
215                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216                 break;
217         }
218 }
219
220 static void
221 nm_ip4_config_class_init (NMIP4ConfigClass *config_class)
222 {
223         GObjectClass *object_class = G_OBJECT_CLASS (config_class);
224
225         g_type_class_add_private (config_class, sizeof (NMIP4ConfigPrivate));
226
227         /* virtual methods */
228         object_class->constructed = constructed;
229         object_class->get_property = get_property;
230         object_class->finalize = finalize;
231
232         /* properties */
233
234         /**
235          * NMIP4Config:gateway:
236          *
237          * The IP4 gateway address of the configuration as string.
238          *
239          * Since: 0.9.10
240          **/
241         g_object_class_install_property
242             (object_class, PROP_GATEWAY,
243              g_param_spec_string (NM_IP4_CONFIG_GATEWAY, "", "",
244                                   NULL,
245                                   G_PARAM_READABLE |
246                                   G_PARAM_STATIC_STRINGS));
247
248         /**
249          * NMIP4Config:addresses:
250          *
251          * The #GPtrArray containing #NMIP4Address<!-- -->es of the configuration.
252          **/
253         g_object_class_install_property
254             (object_class, PROP_ADDRESSES,
255              g_param_spec_pointer (NM_IP4_CONFIG_ADDRESSES, "", "",
256                                    G_PARAM_READABLE |
257                                    G_PARAM_STATIC_STRINGS));
258
259         /**
260          * NMIP4Config:routes:
261          *
262          * The #GPtrArray containing #NMSettingIP4Routes of the configuration.
263          **/
264         g_object_class_install_property
265             (object_class, PROP_ROUTES,
266              g_param_spec_pointer (NM_IP4_CONFIG_ROUTES, "", "",
267                                    G_PARAM_READABLE |
268                                    G_PARAM_STATIC_STRINGS));
269
270         /**
271          * NMIP4Config:nameservers:
272          *
273          * The #GArray containing name servers (#guint32s) of the configuration.
274          **/
275         g_object_class_install_property
276             (object_class, PROP_NAMESERVERS,
277              g_param_spec_boxed (NM_IP4_CONFIG_NAMESERVERS, "", "",
278                                  NM_TYPE_UINT_ARRAY,
279                                  G_PARAM_READABLE |
280                                  G_PARAM_STATIC_STRINGS));
281
282         /**
283          * NMIP4Config:domains:
284          *
285          * The #GPtrArray containing domain strings of the configuration.
286          **/
287         g_object_class_install_property
288             (object_class, PROP_DOMAINS,
289              g_param_spec_boxed (NM_IP4_CONFIG_DOMAINS, "", "",
290                                  NM_TYPE_STRING_ARRAY,
291                                  G_PARAM_READABLE |
292                                  G_PARAM_STATIC_STRINGS));
293
294         /**
295          * NMIP4Config:searches:
296          *
297          * The #GPtrArray containing dns search strings of the configuration.
298          *
299          * Since: 0.9.10
300          **/
301         g_object_class_install_property
302             (object_class, PROP_SEARCHES,
303              g_param_spec_boxed (NM_IP4_CONFIG_SEARCHES, "", "",
304                                  NM_TYPE_STRING_ARRAY,
305                                  G_PARAM_READABLE |
306                                  G_PARAM_STATIC_STRINGS));
307
308         /**
309          * NMIP4Config:wins-servers:
310          *
311          * The #GArray containing WINS servers (#guint32s) of the configuration.
312          **/
313         g_object_class_install_property
314             (object_class, PROP_WINS_SERVERS,
315              g_param_spec_boxed (NM_IP4_CONFIG_WINS_SERVERS, "", "",
316                                  NM_TYPE_UINT_ARRAY,
317                                  G_PARAM_READABLE |
318                                  G_PARAM_STATIC_STRINGS));
319 }
320
321 /**
322  * nm_ip4_config_new:
323  * @connection: the #DBusGConnection
324  * @object_path: the DBus object path of the device
325  *
326  * Creates a new #NMIP4Config.
327  *
328  * Returns: (transfer full): a new IP4 configuration
329  **/
330 GObject *
331 nm_ip4_config_new (DBusGConnection *connection, const char *object_path)
332 {
333         return (GObject *) g_object_new (NM_TYPE_IP4_CONFIG,
334                                          NM_OBJECT_DBUS_CONNECTION, connection,
335                                          NM_OBJECT_DBUS_PATH, object_path,
336                                          NULL);
337 }
338
339 /**
340  * nm_ip4_config_get_gateway:
341  * @config: a #NMIP4Config
342  *
343  * Gets the IP4 gateway address.
344  *
345  * Returns: the IP4 address of the gateway.
346  *
347  * Since: 0.9.10
348  **/
349 const char *
350 nm_ip4_config_get_gateway (NMIP4Config *config)
351 {
352         g_return_val_if_fail (NM_IS_IP4_CONFIG (config), NULL);
353
354         _nm_object_ensure_inited (NM_OBJECT (config));
355         return NM_IP4_CONFIG_GET_PRIVATE (config)->gateway;
356 }
357
358 /**
359  * nm_ip4_config_get_addresses:
360  * @config: a #NMIP4Config
361  *
362  * Gets the IP4 addresses (containing the address, prefix, and gateway).
363  *
364  * Returns: (element-type NMIP4Address): the #GSList containing #NMIP4Address<!-- -->es.
365  * This is the internal copy used by the configuration and must not be modified.
366  **/
367 const GSList *
368 nm_ip4_config_get_addresses (NMIP4Config *config)
369 {
370         g_return_val_if_fail (NM_IS_IP4_CONFIG (config), NULL);
371
372         _nm_object_ensure_inited (NM_OBJECT (config));
373         return NM_IP4_CONFIG_GET_PRIVATE (config)->addresses;
374 }
375
376 /**
377  * nm_ip4_config_get_nameservers:
378  * @config: a #NMIP4Config
379  *
380  * Gets the domain name servers (DNS).
381  *
382  * Returns: (element-type guint32): the #GArray containing #guint32s.
383  * This is the internal copy used by the configuration and must not be
384  * modified.
385  **/
386 const GArray *
387 nm_ip4_config_get_nameservers (NMIP4Config *config)
388 {
389         g_return_val_if_fail (NM_IS_IP4_CONFIG (config), NULL);
390
391         _nm_object_ensure_inited (NM_OBJECT (config));
392         return NM_IP4_CONFIG_GET_PRIVATE (config)->nameservers;
393 }
394
395 /**
396  * nm_ip4_config_get_domains:
397  * @config: a #NMIP4Config
398  *
399  * Gets the domain names.
400  *
401  * Returns: (element-type utf8): the #GPtrArray containing domains as strings. This is the
402  * internal copy used by the configuration, and must not be modified.
403  **/
404 const GPtrArray *
405 nm_ip4_config_get_domains (NMIP4Config *config)
406 {
407         g_return_val_if_fail (NM_IS_IP4_CONFIG (config), NULL);
408
409         _nm_object_ensure_inited (NM_OBJECT (config));
410         return handle_ptr_array_return (NM_IP4_CONFIG_GET_PRIVATE (config)->domains);
411 }
412
413 /**
414  * nm_ip4_config_get_searches:
415  * @config: a #NMIP4Config
416  *
417  * Gets the dns searches.
418  *
419  * Returns: (element-type utf8): the #GPtrArray containing dns searches as strings. This is the
420  * internal copy used by the configuration, and must not be modified.
421  *
422  * Since: 0.9.10
423  **/
424 const GPtrArray *
425 nm_ip4_config_get_searches (NMIP4Config *config)
426 {
427         g_return_val_if_fail (NM_IS_IP4_CONFIG (config), NULL);
428
429         _nm_object_ensure_inited (NM_OBJECT (config));
430         return handle_ptr_array_return (NM_IP4_CONFIG_GET_PRIVATE (config)->searches);
431 }
432
433 /**
434  * nm_ip4_config_get_wins_servers:
435  * @config: a #NMIP4Config
436  *
437  * Gets the Windows Internet Name Service servers (WINS).
438  *
439  * Returns: (element-type guint32): the #GArray containing #guint32s.
440  * This is the internal copy used by the configuration and must not be
441  * modified.
442  **/
443 const GArray *
444 nm_ip4_config_get_wins_servers (NMIP4Config *config)
445 {
446         g_return_val_if_fail (NM_IS_IP4_CONFIG (config), NULL);
447
448         _nm_object_ensure_inited (NM_OBJECT (config));
449         return NM_IP4_CONFIG_GET_PRIVATE (config)->wins;
450 }
451
452 /**
453  * nm_ip4_config_get_routes:
454  * @config: a #NMIP4Config
455  *
456  * Gets the routes.
457  *
458  * Returns: (element-type NMIP4Route): the #GSList containing
459  * #NMIP4Routes. This is the internal copy used by the configuration,
460  * and must not be modified.
461  **/
462 const GSList *
463 nm_ip4_config_get_routes (NMIP4Config *config)
464 {
465         g_return_val_if_fail (NM_IS_IP4_CONFIG (config), NULL);
466
467         _nm_object_ensure_inited (NM_OBJECT (config));
468         return NM_IP4_CONFIG_GET_PRIVATE (config)->routes;
469 }