device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm / nm-ip-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 - 2014 Red Hat, Inc.
20  */
21
22 #include "nm-default.h"
23
24 #include <string.h>
25
26 #include "nm-ip-config.h"
27 #include "nm-ip4-config.h"
28 #include "nm-ip6-config.h"
29 #include "nm-setting-ip-config.h"
30 #include "nm-dbus-interface.h"
31 #include "nm-object-private.h"
32 #include "nm-utils.h"
33 #include "nm-core-internal.h"
34
35 G_DEFINE_ABSTRACT_TYPE (NMIPConfig, nm_ip_config, NM_TYPE_OBJECT)
36
37 #define NM_IP_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_IP_CONFIG, NMIPConfigPrivate))
38
39 typedef struct {
40         char *gateway;
41         GPtrArray *addresses;
42         GPtrArray *routes;
43         char **nameservers;
44         char **domains;
45         char **searches;
46         char **wins;
47
48         gboolean new_style_data;
49 } NMIPConfigPrivate;
50
51 enum {
52         PROP_0,
53         PROP_FAMILY,
54         PROP_GATEWAY,
55         PROP_ADDRESSES,
56         PROP_ROUTES,
57         PROP_NAMESERVERS,
58         PROP_DOMAINS,
59         PROP_SEARCHES,
60         PROP_WINS_SERVERS,
61
62         LAST_PROP
63 };
64
65 static void
66 nm_ip_config_init (NMIPConfig *config)
67 {
68         NMIPConfigPrivate *priv = NM_IP_CONFIG_GET_PRIVATE (config);
69
70         priv->addresses = g_ptr_array_new ();
71         priv->routes = g_ptr_array_new ();
72         priv->nameservers = g_new0 (char *, 1);
73         priv->domains = g_new0 (char *, 1);
74         priv->searches = g_new0 (char *, 1);
75         priv->wins = g_new0 (char *, 1);
76 }
77
78 static gboolean
79 demarshal_ip_addresses (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field)
80 {
81         NMIPConfigPrivate *priv = NM_IP_CONFIG_GET_PRIVATE (object);
82
83         if (priv->new_style_data)
84                 return TRUE;
85
86         g_ptr_array_unref (priv->addresses);
87         if (NM_IS_IP4_CONFIG (object))
88                 priv->addresses = nm_utils_ip4_addresses_from_variant (value, NULL);
89         else
90                 priv->addresses = nm_utils_ip6_addresses_from_variant (value, NULL);
91         _nm_object_queue_notify (object, NM_IP_CONFIG_ADDRESSES);
92
93         return TRUE;
94 }
95
96 static gboolean
97 demarshal_ip_address_data (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field)
98 {
99         NMIPConfigPrivate *priv = NM_IP_CONFIG_GET_PRIVATE (object);
100
101         priv->new_style_data = TRUE;
102
103         g_ptr_array_unref (priv->addresses);
104         if (NM_IS_IP4_CONFIG (object))
105                 priv->addresses = nm_utils_ip_addresses_from_variant (value, AF_INET);
106         else
107                 priv->addresses = nm_utils_ip_addresses_from_variant (value, AF_INET6);
108         _nm_object_queue_notify (object, NM_IP_CONFIG_ADDRESSES);
109
110         return TRUE;
111 }
112
113 static gboolean
114 demarshal_ip_array (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field)
115 {
116         char ***obj_field;
117
118         obj_field = field;
119         if (*obj_field)
120                 g_strfreev (*obj_field);
121
122         if (NM_IS_IP4_CONFIG (object))
123                 *obj_field = nm_utils_ip4_dns_from_variant (value);
124         else
125                 *obj_field = nm_utils_ip6_dns_from_variant (value);
126
127         _nm_object_queue_notify (object, pspec->name);
128         return TRUE;
129 }
130
131 static gboolean
132 demarshal_ip_routes (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field)
133 {
134         NMIPConfigPrivate *priv = NM_IP_CONFIG_GET_PRIVATE (object);
135
136         if (priv->new_style_data)
137                 return TRUE;
138
139         g_ptr_array_unref (priv->routes);
140         if (NM_IS_IP4_CONFIG (object))
141                 priv->routes = nm_utils_ip4_routes_from_variant (value);
142         else
143                 priv->routes = nm_utils_ip6_routes_from_variant (value);
144         _nm_object_queue_notify (object, NM_IP_CONFIG_ROUTES);
145
146         return TRUE;
147 }
148
149 static gboolean
150 demarshal_ip_route_data (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field)
151 {
152         NMIPConfigPrivate *priv = NM_IP_CONFIG_GET_PRIVATE (object);
153
154         priv->new_style_data = TRUE;
155
156         g_ptr_array_unref (priv->routes);
157         if (NM_IS_IP4_CONFIG (object))
158                 priv->routes = nm_utils_ip_routes_from_variant (value, AF_INET);
159         else
160                 priv->routes = nm_utils_ip_routes_from_variant (value, AF_INET6);
161         _nm_object_queue_notify (object, NM_IP_CONFIG_ROUTES);
162
163         return TRUE;
164 }
165
166 static void
167 init_dbus (NMObject *object)
168 {
169         NMIPConfigPrivate *priv = NM_IP_CONFIG_GET_PRIVATE (object);
170         const NMPropertiesInfo property_info[] = {
171                 { NM_IP_CONFIG_GATEWAY,      &priv->gateway, },
172                 { NM_IP_CONFIG_ADDRESSES,    &priv->addresses, demarshal_ip_addresses },
173                 { "address-data",            &priv->addresses, demarshal_ip_address_data },
174                 { NM_IP_CONFIG_ROUTES,       &priv->routes, demarshal_ip_routes },
175                 { "route-data",              &priv->routes, demarshal_ip_route_data },
176                 { NM_IP_CONFIG_NAMESERVERS,  &priv->nameservers, demarshal_ip_array },
177                 { NM_IP_CONFIG_DOMAINS,      &priv->domains, },
178                 { NM_IP_CONFIG_SEARCHES,     &priv->searches, },
179                 { NM_IP_CONFIG_WINS_SERVERS, &priv->wins, demarshal_ip_array },
180                 { NULL },
181         };
182
183         NM_OBJECT_CLASS (nm_ip_config_parent_class)->init_dbus (object);
184
185         _nm_object_register_properties (object,
186                                         (NM_IS_IP4_CONFIG (object) ?
187                                          NM_DBUS_INTERFACE_IP4_CONFIG :
188                                          NM_DBUS_INTERFACE_IP6_CONFIG),
189                                         property_info);
190 }
191
192 static void
193 finalize (GObject *object)
194 {
195         NMIPConfigPrivate *priv = NM_IP_CONFIG_GET_PRIVATE (object);
196
197         g_free (priv->gateway);
198
199         g_ptr_array_unref (priv->addresses);
200         g_ptr_array_unref (priv->routes);
201
202         g_strfreev (priv->nameservers);
203         g_strfreev (priv->domains);
204         g_strfreev (priv->searches);
205         g_strfreev (priv->wins);
206
207         G_OBJECT_CLASS (nm_ip_config_parent_class)->finalize (object);
208 }
209
210 static void
211 get_property (GObject *object,
212               guint prop_id,
213               GValue *value,
214               GParamSpec *pspec)
215 {
216         NMIPConfig *self = NM_IP_CONFIG (object);
217
218         switch (prop_id) {
219         case PROP_FAMILY:
220                 g_value_set_int (value, nm_ip_config_get_family (self));
221                 break;
222         case PROP_GATEWAY:
223                 g_value_set_string (value, nm_ip_config_get_gateway (self));
224                 break;
225         case PROP_ADDRESSES:
226                 g_value_take_boxed (value, _nm_utils_copy_array (nm_ip_config_get_addresses (self),
227                                                                  (NMUtilsCopyFunc) nm_ip_address_dup,
228                                                                  (GDestroyNotify) nm_ip_address_unref));
229                 break;
230         case PROP_ROUTES:
231                 g_value_take_boxed (value, _nm_utils_copy_array (nm_ip_config_get_routes (self),
232                                                                  (NMUtilsCopyFunc) nm_ip_route_dup,
233                                                                  (GDestroyNotify) nm_ip_route_unref));
234                 break;
235         case PROP_NAMESERVERS:
236                 g_value_set_boxed (value, (char **) nm_ip_config_get_nameservers (self));
237                 break;
238         case PROP_DOMAINS:
239                 g_value_set_boxed (value, (char **) nm_ip_config_get_domains (self));
240                 break;
241         case PROP_SEARCHES:
242                 g_value_set_boxed (value, (char **) nm_ip_config_get_searches (self));
243                 break;
244         case PROP_WINS_SERVERS:
245                 g_value_set_boxed (value, (char **) nm_ip_config_get_wins_servers (self));
246                 break;
247         default:
248                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
249                 break;
250         }
251 }
252
253 static void
254 nm_ip_config_class_init (NMIPConfigClass *config_class)
255 {
256         GObjectClass *object_class = G_OBJECT_CLASS (config_class);
257         NMObjectClass *nm_object_class = NM_OBJECT_CLASS (config_class);
258
259         g_type_class_add_private (config_class, sizeof (NMIPConfigPrivate));
260
261         /* virtual methods */
262         object_class->get_property = get_property;
263         object_class->finalize = finalize;
264
265         nm_object_class->init_dbus = init_dbus;
266
267         /* properties */
268
269         /**
270          * NMIPConfig:family:
271          *
272          * The IP address family of the configuration; either
273          * <literal>AF_INET</literal> or <literal>AF_INET6</literal>.
274          **/
275         g_object_class_install_property
276             (object_class, PROP_FAMILY,
277              g_param_spec_int (NM_IP_CONFIG_FAMILY, "", "",
278                                0, 255, AF_UNSPEC,
279                                G_PARAM_READABLE |
280                                G_PARAM_STATIC_STRINGS));
281
282         /**
283          * NMIPConfig:gateway:
284          *
285          * The IP gateway address of the configuration as string.
286          **/
287         g_object_class_install_property
288                 (object_class, PROP_GATEWAY,
289                  g_param_spec_string (NM_IP_CONFIG_GATEWAY, "", "",
290                                       NULL,
291                                       G_PARAM_READABLE |
292                                       G_PARAM_STATIC_STRINGS));
293
294         /**
295          * NMIPConfig:addresses:
296          *
297          * A #GPtrArray containing the addresses (#NMIPAddress) of the configuration.
298          **/
299         g_object_class_install_property
300             (object_class, PROP_ADDRESSES,
301              g_param_spec_boxed (NM_IP_CONFIG_ADDRESSES, "", "",
302                                  G_TYPE_PTR_ARRAY,
303                                  G_PARAM_READABLE |
304                                  G_PARAM_STATIC_STRINGS));
305
306         /**
307          * NMIPConfig:routes:
308          *
309          * A #GPtrArray containing the routes (#NMIPRoute) of the configuration.
310          **/
311         g_object_class_install_property
312             (object_class, PROP_ROUTES,
313              g_param_spec_boxed (NM_IP_CONFIG_ROUTES, "", "",
314                                  G_TYPE_PTR_ARRAY,
315                                  G_PARAM_READABLE |
316                                  G_PARAM_STATIC_STRINGS));
317
318         /**
319          * NMIPConfig:nameservers:
320          *
321          * The array containing name server IP addresses of the configuration.
322          **/
323         g_object_class_install_property
324             (object_class, PROP_NAMESERVERS,
325              g_param_spec_boxed (NM_IP_CONFIG_NAMESERVERS, "", "",
326                                  G_TYPE_STRV,
327                                  G_PARAM_READABLE |
328                                  G_PARAM_STATIC_STRINGS));
329
330         /**
331          * NMIPConfig:domains:
332          *
333          * The array containing domain strings of the configuration.
334          **/
335         g_object_class_install_property
336             (object_class, PROP_DOMAINS,
337              g_param_spec_boxed (NM_IP_CONFIG_DOMAINS, "", "",
338                                  G_TYPE_STRV,
339                                  G_PARAM_READABLE |
340                                  G_PARAM_STATIC_STRINGS));
341
342         /**
343          * NMIPConfig:searches:
344          *
345          * The array containing DNS search strings of the configuration.
346          **/
347         g_object_class_install_property
348             (object_class, PROP_SEARCHES,
349              g_param_spec_boxed (NM_IP_CONFIG_SEARCHES, "", "",
350                                  G_TYPE_STRV,
351                                  G_PARAM_READABLE |
352                                  G_PARAM_STATIC_STRINGS));
353
354         /**
355          * NMIPConfig:wins-servers:
356          *
357          * The array containing WINS server IP addresses of the configuration.
358          * (This will always be empty for IPv6 configurations.)
359          **/
360         g_object_class_install_property
361             (object_class, PROP_WINS_SERVERS,
362              g_param_spec_boxed (NM_IP_CONFIG_WINS_SERVERS, "", "",
363                                  G_TYPE_STRV,
364                                  G_PARAM_READABLE |
365                                  G_PARAM_STATIC_STRINGS));
366 }
367
368 /**
369  * nm_ip_config_get_family:
370  * @config: a #NMIPConfig
371  *
372  * Gets the IP address family
373  *
374  * Returns: the IP address family; either <literal>AF_INET</literal> or
375  * <literal>AF_INET6</literal>
376  **/
377 int
378 nm_ip_config_get_family (NMIPConfig *config)
379 {
380         g_return_val_if_fail (NM_IS_IP_CONFIG (config), AF_UNSPEC);
381
382         return NM_IS_IP4_CONFIG (config) ? AF_INET : AF_INET6;
383 }
384
385 /**
386  * nm_ip_config_get_gateway:
387  * @config: a #NMIPConfig
388  *
389  * Gets the IP gateway address.
390  *
391  * Returns: (transfer none): the IP address of the gateway.
392  **/
393 const char *
394 nm_ip_config_get_gateway (NMIPConfig *config)
395 {
396         g_return_val_if_fail (NM_IS_IP_CONFIG (config), NULL);
397
398         return NM_IP_CONFIG_GET_PRIVATE (config)->gateway;
399 }
400
401 /**
402  * nm_ip_config_get_addresses:
403  * @config: a #NMIPConfig
404  *
405  * Gets the IP addresses (containing the address, prefix, and gateway).
406  *
407  * Returns: (element-type NMIPAddress) (transfer none): the #GPtrArray
408  * containing #NMIPAddress<!-- -->es.  This is the internal copy used by the
409  * configuration and must not be modified. The library never modifies the
410  * returned array and thus it is safe for callers to reference and keep using it.
411  **/
412 GPtrArray *
413 nm_ip_config_get_addresses (NMIPConfig *config)
414 {
415         g_return_val_if_fail (NM_IS_IP_CONFIG (config), NULL);
416
417         return NM_IP_CONFIG_GET_PRIVATE (config)->addresses;
418 }
419
420 /**
421  * nm_ip_config_get_nameservers:
422  * @config: a #NMIPConfig
423  *
424  * Gets the domain name servers (DNS).
425  *
426  * Returns: (transfer none): the array of nameserver IP addresses
427  **/
428 const char * const *
429 nm_ip_config_get_nameservers (NMIPConfig *config)
430 {
431         g_return_val_if_fail (NM_IS_IP_CONFIG (config), NULL);
432
433         return (const char * const *) NM_IP_CONFIG_GET_PRIVATE (config)->nameservers;
434 }
435
436 /**
437  * nm_ip_config_get_domains:
438  * @config: a #NMIPConfig
439  *
440  * Gets the domain names.
441  *
442  * Returns: (transfer none): the array of domains.
443  * (This is never %NULL, though it may be 0-length).
444  **/
445 const char * const *
446 nm_ip_config_get_domains (NMIPConfig *config)
447 {
448         g_return_val_if_fail (NM_IS_IP_CONFIG (config), NULL);
449
450         return (const char * const *) NM_IP_CONFIG_GET_PRIVATE (config)->domains;
451 }
452
453 /**
454  * nm_ip_config_get_searches:
455  * @config: a #NMIPConfig
456  *
457  * Gets the DNS searches.
458  *
459  * Returns: (transfer none): the array of DNS search strings.
460  * (This is never %NULL, though it may be 0-length).
461  **/
462 const char * const *
463 nm_ip_config_get_searches (NMIPConfig *config)
464 {
465         g_return_val_if_fail (NM_IS_IP_CONFIG (config), NULL);
466
467         return (const char * const *) NM_IP_CONFIG_GET_PRIVATE (config)->searches;
468 }
469
470 /**
471  * nm_ip_config_get_wins_servers:
472  * @config: a #NMIPConfig
473  *
474  * Gets the Windows Internet Name Service servers (WINS).
475  *
476  * Returns: (transfer none): the arry of WINS server IP address strings.
477  * (This is never %NULL, though it may be 0-length.)
478  **/
479 const char * const *
480 nm_ip_config_get_wins_servers (NMIPConfig *config)
481 {
482         g_return_val_if_fail (NM_IS_IP_CONFIG (config), NULL);
483
484         return (const char * const *) NM_IP_CONFIG_GET_PRIVATE (config)->wins;
485 }
486
487 /**
488  * nm_ip_config_get_routes:
489  * @config: a #NMIPConfig
490  *
491  * Gets the routes.
492  *
493  * Returns: (element-type NMIPRoute) (transfer none): the #GPtrArray containing
494  * #NMIPRoutes. This is the internal copy used by the configuration, and must
495  * not be modified. The library never modifies the returned array and thus it is
496  * safe for callers to reference and keep using it.
497  *
498  **/
499 GPtrArray *
500 nm_ip_config_get_routes (NMIPConfig *config)
501 {
502         g_return_val_if_fail (NM_IS_IP_CONFIG (config), NULL);
503
504         return NM_IP_CONFIG_GET_PRIVATE (config)->routes;
505 }