device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-glib / nm-dhcp6-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 2008 - 2011 Red Hat, Inc.
19  * Copyright 2008 Novell, Inc.
20  */
21
22 #include "nm-default.h"
23
24 #include <string.h>
25
26 #include "nm-dhcp6-config.h"
27 #include "NetworkManager.h"
28 #include "nm-types-private.h"
29 #include "nm-object-private.h"
30 #include "nm-utils.h"
31
32 G_DEFINE_TYPE (NMDHCP6Config, nm_dhcp6_config, NM_TYPE_OBJECT)
33
34 #define NM_DHCP6_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DHCP6_CONFIG, NMDHCP6ConfigPrivate))
35
36 typedef struct {
37         DBusGProxy *proxy;
38
39         GHashTable *options;
40 } NMDHCP6ConfigPrivate;
41
42 enum {
43         PROP_0,
44         PROP_OPTIONS,
45
46         LAST_PROP
47 };
48
49 static void
50 nm_dhcp6_config_init (NMDHCP6Config *config)
51 {
52 }
53
54 static gboolean
55 demarshal_dhcp6_options (NMObject *object, GParamSpec *pspec, GValue *value, gpointer field)
56 {
57         NMDHCP6ConfigPrivate *priv = NM_DHCP6_CONFIG_GET_PRIVATE (object);
58         GHashTable *new_options;
59         GHashTableIter iter;
60         const char *key;
61         GValue *opt;
62
63         g_hash_table_remove_all (priv->options);
64
65         new_options = g_value_get_boxed (value);
66         if (new_options) {
67                 g_hash_table_iter_init (&iter, new_options);
68                 while (g_hash_table_iter_next (&iter, (gpointer) &key, (gpointer) &opt))
69                         g_hash_table_insert (priv->options, g_strdup (key), g_value_dup_string (opt));
70         }
71
72         _nm_object_queue_notify (object, NM_DHCP6_CONFIG_OPTIONS);
73         return TRUE;
74 }
75
76 static void
77 register_properties (NMDHCP6Config *config)
78 {
79         NMDHCP6ConfigPrivate *priv = NM_DHCP6_CONFIG_GET_PRIVATE (config);
80         const NMPropertiesInfo property_info[] = {
81                 { NM_DHCP6_CONFIG_OPTIONS,   &priv->options, demarshal_dhcp6_options },
82                 { NULL },
83         };
84
85         _nm_object_register_properties (NM_OBJECT (config),
86                                         priv->proxy,
87                                         property_info);
88 }
89
90 static void
91 constructed (GObject *object)
92 {
93         NMDHCP6ConfigPrivate *priv = NM_DHCP6_CONFIG_GET_PRIVATE (object);
94
95         G_OBJECT_CLASS (nm_dhcp6_config_parent_class)->constructed (object);
96
97         priv->options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
98
99         priv->proxy = _nm_object_new_proxy (NM_OBJECT (object), NULL, NM_DBUS_INTERFACE_DHCP6_CONFIG);
100         register_properties (NM_DHCP6_CONFIG (object));
101 }
102
103 static void
104 finalize (GObject *object)
105 {
106         NMDHCP6ConfigPrivate *priv = NM_DHCP6_CONFIG_GET_PRIVATE (object);
107
108         if (priv->options)
109                 g_hash_table_destroy (priv->options);
110
111         g_object_unref (priv->proxy);
112
113         G_OBJECT_CLASS (nm_dhcp6_config_parent_class)->finalize (object);
114 }
115
116 static void
117 get_property (GObject *object,
118               guint prop_id,
119               GValue *value,
120               GParamSpec *pspec)
121 {
122         NMDHCP6Config *self = NM_DHCP6_CONFIG (object);
123
124         _nm_object_ensure_inited (NM_OBJECT (object));
125
126         switch (prop_id) {
127         case PROP_OPTIONS:
128                 g_value_set_boxed (value, nm_dhcp6_config_get_options (self));
129                 break;
130         default:
131                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
132                 break;
133         }
134 }
135
136 static void
137 nm_dhcp6_config_class_init (NMDHCP6ConfigClass *config_class)
138 {
139         GObjectClass *object_class = G_OBJECT_CLASS (config_class);
140
141         g_type_class_add_private (config_class, sizeof (NMDHCP6ConfigPrivate));
142
143         /* virtual methods */
144         object_class->constructed = constructed;
145         object_class->get_property = get_property;
146         object_class->finalize = finalize;
147
148         /* properties */
149
150         /**
151          * NMDHCP6Config:options:
152          *
153          * The #GHashTable containing options of the configuration.
154          *
155          * Type: GLib.HashTable(utf8,GObject.Value)
156          **/
157         g_object_class_install_property
158                 (object_class, PROP_OPTIONS,
159                  g_param_spec_boxed (NM_DHCP6_CONFIG_OPTIONS, "", "",
160                                      G_TYPE_HASH_TABLE,
161                                      G_PARAM_READABLE |
162                                      G_PARAM_STATIC_STRINGS));
163 }
164
165 /**
166  * nm_dhcp6_config_new:
167  * @connection: the #DBusGConnection
168  * @object_path: the DBus object path of the device
169  *
170  * Creates a new #NMDHCP6Config.
171  *
172  * Returns: (transfer full): a new configuration
173  **/
174 GObject *
175 nm_dhcp6_config_new (DBusGConnection *connection, const char *object_path)
176 {
177         return (GObject *) g_object_new (NM_TYPE_DHCP6_CONFIG,
178                                          NM_OBJECT_DBUS_CONNECTION, connection,
179                                          NM_OBJECT_DBUS_PATH, object_path,
180                                          NULL);
181 }
182
183 /**
184  * nm_dhcp6_config_get_options:
185  * @config: a #NMDHCP6Config
186  *
187  * Gets all the options contained in the configuration.
188  *
189  * Returns: (transfer none) (element-type utf8 GObject.Value): the #GHashTable containing strings for keys and values.
190  * This is the internal copy used by the configuration, and must not be modified.
191  **/
192 GHashTable *
193 nm_dhcp6_config_get_options (NMDHCP6Config *config)
194 {
195         g_return_val_if_fail (NM_IS_DHCP6_CONFIG (config), NULL);
196
197         _nm_object_ensure_inited (NM_OBJECT (config));
198         return NM_DHCP6_CONFIG_GET_PRIVATE (config)->options;
199 }
200
201 /**
202  * nm_dhcp6_config_get_one_option:
203  * @config: a #NMDHCP6Config
204  * @option: the option to retrieve
205  *
206  * Gets one option by option name.
207  *
208  * Returns: the configuration option's value. This is the internal string used by the
209  * configuration, and must not be modified.
210  **/
211 const char *
212 nm_dhcp6_config_get_one_option (NMDHCP6Config *config, const char *option)
213 {
214         g_return_val_if_fail (NM_IS_DHCP6_CONFIG (config), NULL);
215
216         return g_hash_table_lookup (nm_dhcp6_config_get_options (config), option);
217 }