device: renew dhcp leases on awake for software devices
[NetworkManager.git] / src / nm-dhcp4-config.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* NetworkManager -- Network link manager
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * Copyright (C) 2008 Red Hat, Inc.
19  */
20
21 #include "nm-default.h"
22
23 #include <string.h>
24
25 #include "nm-dbus-interface.h"
26 #include "nm-dhcp4-config.h"
27 #include "nm-utils.h"
28
29 #include "nmdbus-dhcp4-config.h"
30
31 G_DEFINE_TYPE (NMDhcp4Config, nm_dhcp4_config, NM_TYPE_EXPORTED_OBJECT)
32
33 #define NM_DHCP4_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DHCP4_CONFIG, NMDhcp4ConfigPrivate))
34
35 typedef struct {
36         GVariant *options;
37 } NMDhcp4ConfigPrivate;
38
39
40 enum {
41         PROP_0,
42         PROP_OPTIONS,
43
44         LAST_PROP
45 };
46
47
48 NMDhcp4Config *
49 nm_dhcp4_config_new (void)
50 {
51         return NM_DHCP4_CONFIG (g_object_new (NM_TYPE_DHCP4_CONFIG, NULL));
52 }
53
54 void
55 nm_dhcp4_config_set_options (NMDhcp4Config *self,
56                              GHashTable *options)
57 {
58         NMDhcp4ConfigPrivate *priv = NM_DHCP4_CONFIG_GET_PRIVATE (self);
59         GHashTableIter iter;
60         const char *key, *value;
61         GVariantBuilder builder;
62
63         g_return_if_fail (NM_IS_DHCP4_CONFIG (self));
64         g_return_if_fail (options != NULL);
65
66         g_variant_unref (priv->options);
67
68         g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
69         g_hash_table_iter_init (&iter, options);
70         while (g_hash_table_iter_next (&iter, (gpointer) &key, (gpointer) &value))
71                 g_variant_builder_add (&builder, "{sv}", key, g_variant_new_string (value));
72
73         priv->options = g_variant_builder_end (&builder);
74         g_variant_ref_sink (priv->options);
75         g_object_notify (G_OBJECT (self), NM_DHCP4_CONFIG_OPTIONS);
76 }
77
78 const char *
79 nm_dhcp4_config_get_option (NMDhcp4Config *self, const char *key)
80 {
81         NMDhcp4ConfigPrivate *priv = NM_DHCP4_CONFIG_GET_PRIVATE (self);
82         const char *value;
83
84         g_return_val_if_fail (NM_IS_DHCP4_CONFIG (self), NULL);
85         g_return_val_if_fail (key != NULL, NULL);
86
87         if (g_variant_lookup (priv->options, key, "&s", &value))
88                 return value;
89         else
90                 return NULL;
91 }
92
93 GVariant *
94 nm_dhcp4_config_get_options (NMDhcp4Config *self)
95 {
96         g_return_val_if_fail (NM_IS_DHCP4_CONFIG (self), NULL);
97
98         return g_variant_ref (NM_DHCP4_CONFIG_GET_PRIVATE (self)->options);
99 }
100
101 static void
102 nm_dhcp4_config_init (NMDhcp4Config *self)
103 {
104         NMDhcp4ConfigPrivate *priv = NM_DHCP4_CONFIG_GET_PRIVATE (self);
105
106         priv->options = g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0);
107         g_variant_ref_sink (priv->options);
108 }
109
110 static void
111 finalize (GObject *object)
112 {
113         NMDhcp4ConfigPrivate *priv = NM_DHCP4_CONFIG_GET_PRIVATE (object);
114
115         g_variant_unref (priv->options);
116
117         G_OBJECT_CLASS (nm_dhcp4_config_parent_class)->finalize (object);
118 }
119
120 static void
121 get_property (GObject *object, guint prop_id,
122                           GValue *value, GParamSpec *pspec)
123 {
124         NMDhcp4ConfigPrivate *priv = NM_DHCP4_CONFIG_GET_PRIVATE (object);
125
126         switch (prop_id) {
127         case PROP_OPTIONS:
128                 g_value_set_variant (value, priv->options);
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_dhcp4_config_class_init (NMDhcp4ConfigClass *config_class)
138 {
139         GObjectClass *object_class = G_OBJECT_CLASS (config_class);
140         NMExportedObjectClass *exported_object_class = NM_EXPORTED_OBJECT_CLASS (config_class);
141
142         g_type_class_add_private (config_class, sizeof (NMDhcp4ConfigPrivate));
143
144         exported_object_class->export_path = NM_DBUS_PATH "/DHCP4Config/%u";
145         exported_object_class->export_on_construction = TRUE;
146
147         /* virtual methods */
148         object_class->get_property = get_property;
149         object_class->finalize = finalize;
150
151         /* properties */
152         g_object_class_install_property
153                 (object_class, PROP_OPTIONS,
154                  g_param_spec_variant (NM_DHCP4_CONFIG_OPTIONS, "", "",
155                                        G_VARIANT_TYPE ("a{sv}"),
156                                        NULL,
157                                        G_PARAM_READABLE |
158                                        G_PARAM_STATIC_STRINGS));
159
160         nm_exported_object_class_add_interface (NM_EXPORTED_OBJECT_CLASS (config_class),
161                                                 NMDBUS_TYPE_DHCP4_CONFIG_SKELETON,
162                                                 NULL);
163 }