device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-glib / nm-vpn-connection.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 - 2008 Novell, Inc.
19  * Copyright 2007 - 2012 Red Hat, Inc.
20  */
21
22 #include "nm-default.h"
23
24 #include <string.h>
25
26 #include "nm-vpn-connection.h"
27 #include "NetworkManager.h"
28 #include "nm-utils.h"
29 #include "nm-object-private.h"
30 #include "nm-active-connection.h"
31
32 G_DEFINE_TYPE (NMVPNConnection, nm_vpn_connection, NM_TYPE_ACTIVE_CONNECTION)
33
34 #define NM_VPN_CONNECTION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_VPN_CONNECTION, NMVPNConnectionPrivate))
35
36 typedef struct {
37         DBusGProxy *proxy;
38         char *banner;
39         NMVPNConnectionState vpn_state;
40 } NMVPNConnectionPrivate;
41
42 enum {
43         PROP_0,
44         PROP_VPN_STATE,
45         PROP_BANNER,
46
47         LAST_PROP
48 };
49
50 enum {
51         VPN_STATE_CHANGED,
52
53         LAST_SIGNAL
54 };
55
56 static guint signals[LAST_SIGNAL] = { 0 };
57
58
59 /**
60  * nm_vpn_connection_new:
61  * @connection: the #DBusGConnection
62  * @path: the DBus object path of the new connection
63  *
64  * Creates a new #NMVPNConnection.
65  *
66  * Returns: (transfer full): a new connection object
67  **/
68 GObject *
69 nm_vpn_connection_new (DBusGConnection *connection, const char *path)
70 {
71         g_return_val_if_fail (connection != NULL, NULL);
72         g_return_val_if_fail (path != NULL, NULL);
73
74         return g_object_new (NM_TYPE_VPN_CONNECTION,
75                              NM_OBJECT_DBUS_CONNECTION, connection,
76                              NM_OBJECT_DBUS_PATH, path,
77                              NULL);
78 }
79
80 /**
81  * nm_vpn_connection_get_banner:
82  * @vpn: a #NMVPNConnection
83  *
84  * Gets the VPN login banner of the active #NMVPNConnection.
85  *
86  * Returns: the VPN login banner of the VPN connection. This is the internal
87  * string used by the connection, and must not be modified.
88  **/
89 const char *
90 nm_vpn_connection_get_banner (NMVPNConnection *vpn)
91 {
92         NMVPNConnectionPrivate *priv;
93
94         g_return_val_if_fail (NM_IS_VPN_CONNECTION (vpn), NULL);
95
96         priv = NM_VPN_CONNECTION_GET_PRIVATE (vpn);
97
98         /* We need to update vpn_state first in case it's unknown. */
99         _nm_object_ensure_inited (NM_OBJECT (vpn));
100
101         if (priv->vpn_state != NM_VPN_CONNECTION_STATE_ACTIVATED)
102                 return NULL;
103
104         return priv->banner;
105 }
106
107 /**
108  * nm_vpn_connection_get_vpn_state:
109  * @vpn: a #NMVPNConnection
110  *
111  * Gets the current #NMVPNConnection state.
112  *
113  * Returns: the VPN state of the active VPN connection.
114  **/
115 NMVPNConnectionState
116 nm_vpn_connection_get_vpn_state (NMVPNConnection *vpn)
117 {
118         g_return_val_if_fail (NM_IS_VPN_CONNECTION (vpn), NM_VPN_CONNECTION_STATE_UNKNOWN);
119
120         _nm_object_ensure_inited (NM_OBJECT (vpn));
121         return NM_VPN_CONNECTION_GET_PRIVATE (vpn)->vpn_state;
122 }
123
124 static void
125 vpn_state_changed_proxy (DBusGProxy *proxy,
126                          NMVPNConnectionState vpn_state,
127                          NMVPNConnectionStateReason reason,
128                          gpointer user_data)
129 {
130         NMVPNConnection *connection = NM_VPN_CONNECTION (user_data);
131         NMVPNConnectionPrivate *priv = NM_VPN_CONNECTION_GET_PRIVATE (connection);
132
133         if (priv->vpn_state != vpn_state) {
134                 priv->vpn_state = vpn_state;
135                 g_signal_emit (connection, signals[VPN_STATE_CHANGED], 0, vpn_state, reason);
136                 g_object_notify (G_OBJECT (connection), NM_VPN_CONNECTION_VPN_STATE);
137         }
138 }
139
140 /*****************************************************************************/
141
142 static void
143 nm_vpn_connection_init (NMVPNConnection *connection)
144 {
145         NMVPNConnectionPrivate *priv = NM_VPN_CONNECTION_GET_PRIVATE (connection);
146
147         priv->vpn_state = NM_VPN_CONNECTION_STATE_UNKNOWN;
148 }
149
150 static void
151 register_properties (NMVPNConnection *connection)
152 {
153         NMVPNConnectionPrivate *priv = NM_VPN_CONNECTION_GET_PRIVATE (connection);
154         const NMPropertiesInfo property_info[] = {
155                 { NM_VPN_CONNECTION_BANNER,    &priv->banner },
156                 { NM_VPN_CONNECTION_VPN_STATE, &priv->vpn_state },
157                 { NULL },
158         };
159
160         _nm_object_register_properties (NM_OBJECT (connection),
161                                         priv->proxy,
162                                         property_info);
163 }
164
165 static void
166 constructed (GObject *object)
167 {
168         NMVPNConnectionPrivate *priv = NM_VPN_CONNECTION_GET_PRIVATE (object);
169
170         G_OBJECT_CLASS (nm_vpn_connection_parent_class)->constructed (object);
171
172         priv->proxy = _nm_object_new_proxy (NM_OBJECT (object), NULL, NM_DBUS_INTERFACE_VPN_CONNECTION);
173
174         dbus_g_object_register_marshaller (g_cclosure_marshal_generic,
175                                            G_TYPE_NONE,
176                                            G_TYPE_UINT, G_TYPE_UINT,
177                                            G_TYPE_INVALID);
178         dbus_g_proxy_add_signal (priv->proxy, "VpnStateChanged", G_TYPE_UINT, G_TYPE_UINT, G_TYPE_INVALID);
179         dbus_g_proxy_connect_signal (priv->proxy,
180                                      "VpnStateChanged",
181                                      G_CALLBACK (vpn_state_changed_proxy),
182                                      object,
183                                      NULL);
184
185         register_properties (NM_VPN_CONNECTION (object));
186 }
187
188 static void
189 finalize (GObject *object)
190 {
191         NMVPNConnectionPrivate *priv = NM_VPN_CONNECTION_GET_PRIVATE (object);
192
193         g_free (priv->banner);
194         g_object_unref (priv->proxy);
195
196         G_OBJECT_CLASS (nm_vpn_connection_parent_class)->finalize (object);
197 }
198
199 static void
200 get_property (GObject *object,
201               guint prop_id,
202               GValue *value,
203               GParamSpec *pspec)
204 {
205         NMVPNConnection *self = NM_VPN_CONNECTION (object);
206
207         _nm_object_ensure_inited (NM_OBJECT (object));
208
209         switch (prop_id) {
210         case PROP_VPN_STATE:
211                 g_value_set_uint (value, nm_vpn_connection_get_vpn_state (self));
212                 break;
213         case PROP_BANNER:
214                 g_value_set_string (value, nm_vpn_connection_get_banner (self));
215                 break;
216         default:
217                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218                 break;
219         }
220 }
221
222 static void
223 nm_vpn_connection_class_init (NMVPNConnectionClass *connection_class)
224 {
225         GObjectClass *object_class = G_OBJECT_CLASS (connection_class);
226
227         g_type_class_add_private (connection_class, sizeof (NMVPNConnectionPrivate));
228
229         /* virtual methods */
230         object_class->constructed = constructed;
231         object_class->get_property = get_property;
232         object_class->finalize = finalize;
233
234         /* properties */
235
236         /**
237          * NMVPNConnection:vpn-state:
238          *
239          * The VPN state of the active VPN connection.
240          **/
241         g_object_class_install_property
242                 (object_class, PROP_VPN_STATE,
243                  g_param_spec_uint (NM_VPN_CONNECTION_VPN_STATE, "", "",
244                                     NM_VPN_CONNECTION_STATE_UNKNOWN,
245                                     NM_VPN_CONNECTION_STATE_DISCONNECTED,
246                                     NM_VPN_CONNECTION_STATE_UNKNOWN,
247                                     G_PARAM_READABLE |
248                                     G_PARAM_STATIC_STRINGS));
249
250         /**
251          * NMVPNConnection:banner:
252          *
253          * The VPN login banner of the active VPN connection.
254          **/
255         g_object_class_install_property
256                 (object_class, PROP_BANNER,
257                  g_param_spec_string (NM_VPN_CONNECTION_BANNER, "", "",
258                                       NULL,
259                                       G_PARAM_READABLE |
260                                       G_PARAM_STATIC_STRINGS));
261
262         /* signals */
263         signals[VPN_STATE_CHANGED] =
264                 g_signal_new ("vpn-state-changed",
265                               G_OBJECT_CLASS_TYPE (object_class),
266                               G_SIGNAL_RUN_FIRST,
267                               G_STRUCT_OFFSET (NMVPNConnectionClass, vpn_state_changed),
268                               NULL, NULL, NULL,
269                               G_TYPE_NONE, 2,
270                               G_TYPE_UINT, G_TYPE_UINT);
271 }