summaryrefslogtreecommitdiff
path: root/src/supplicant-manager/nm-supplicant-manager.c
blob: 3d5d9a74a3fd57989788c833a9760b0e36be83fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 *  Copyright (C) 2006 Red Hat, Inc.
 *
 *  Written by Dan Williams <dcbw@redhat.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <string.h>
#include <glib.h>
#include <dbus/dbus.h>

#include "nm-supplicant-manager.h"
#include "nm-supplicant-interface.h"
#include "nm-dbus-manager.h"
#include "nm-marshal.h"
#include "nm-utils.h"

#define SUPPLICANT_POKE_INTERVAL 120000

typedef struct {
	NMDBusManager *	dbus_mgr;
	guint32         state;
	GSList *        ifaces;
	gboolean        dispose_has_run;
	guint			poke_id;
} NMSupplicantManagerPrivate;

#define NM_SUPPLICANT_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
                                              NM_TYPE_SUPPLICANT_MANAGER, \
                                              NMSupplicantManagerPrivate))

G_DEFINE_TYPE (NMSupplicantManager, nm_supplicant_manager, G_TYPE_OBJECT)


static void nm_supplicant_manager_name_owner_changed (NMDBusManager *dbus_mgr,
                                                      const char *name,
                                                      const char *old,
                                                      const char *new,
                                                      gpointer user_data);

static void nm_supplicant_manager_set_state (NMSupplicantManager * self,
                                             guint32 new_state);

static gboolean nm_supplicant_manager_startup (NMSupplicantManager * self);


/* Signals */
enum {
	STATE,       /* change in the manager's state */
	LAST_SIGNAL
};
static guint nm_supplicant_manager_signals[LAST_SIGNAL] = { 0 };


NMSupplicantManager *
nm_supplicant_manager_get (void)
{
	static NMSupplicantManager * singleton = NULL;

	if (!singleton) {
		singleton = NM_SUPPLICANT_MANAGER (g_object_new (NM_TYPE_SUPPLICANT_MANAGER, NULL));
	} else {
		g_object_ref (singleton);
	}

	g_assert (singleton);
	return singleton;
}

static gboolean
poke_supplicant_cb (gpointer user_data)
{
	NMSupplicantManager *self = NM_SUPPLICANT_MANAGER (user_data);
	NMSupplicantManagerPrivate *priv = NM_SUPPLICANT_MANAGER_GET_PRIVATE (self);
	DBusGConnection *g_connection;
	DBusGProxy *proxy;
	const char *tmp = "ignoreme";

	g_connection = nm_dbus_manager_get_connection (priv->dbus_mgr);
	proxy = dbus_g_proxy_new_for_name (g_connection,
	                                   WPAS_DBUS_SERVICE,
	                                   WPAS_DBUS_PATH,
	                                   WPAS_DBUS_INTERFACE);
	if (!proxy) {
		nm_warning ("Error: could not init wpa_supplicant proxy");
		goto out;
	}

	nm_info ("Trying to start the supplicant...");
	dbus_g_proxy_call_no_reply (proxy, "getInterface", G_TYPE_STRING, tmp, G_TYPE_INVALID);
	g_object_unref (proxy);

out:
	/* Reschedule the poke */	
	priv->poke_id = g_timeout_add (SUPPLICANT_POKE_INTERVAL,
	                               poke_supplicant_cb,
	                               (gpointer) self);

	return FALSE;
}

static void
nm_supplicant_manager_init (NMSupplicantManager * self)
{
	NMSupplicantManagerPrivate *priv = NM_SUPPLICANT_MANAGER_GET_PRIVATE (self);
	gboolean running;

	priv->dispose_has_run = FALSE;
	priv->state = NM_SUPPLICANT_MANAGER_STATE_DOWN;
	priv->dbus_mgr = nm_dbus_manager_get ();
	priv->poke_id = 0;

	running = nm_supplicant_manager_startup (self);

	g_signal_connect (priv->dbus_mgr,
	                  "name-owner-changed",
	                  G_CALLBACK (nm_supplicant_manager_name_owner_changed),
	                  self);

	if (!running) {
		/* Try to activate the supplicant */
		priv->poke_id = g_idle_add (poke_supplicant_cb, (gpointer) self);
	}
}

static void
nm_supplicant_manager_dispose (GObject *object)
{
	NMSupplicantManagerPrivate *priv = NM_SUPPLICANT_MANAGER_GET_PRIVATE (object);

	if (priv->dispose_has_run) {
		G_OBJECT_CLASS (nm_supplicant_manager_parent_class)->dispose (object);
		return;
	}

	priv->dispose_has_run = TRUE;

	if (priv->poke_id) {
		g_source_remove (priv->poke_id);
		priv->poke_id = 0;
	}

	if (priv->dbus_mgr) {
		g_object_unref (G_OBJECT (priv->dbus_mgr));
		priv->dbus_mgr = NULL;
	}

	/* Chain up to the parent class */
	G_OBJECT_CLASS (nm_supplicant_manager_parent_class)->dispose (object);
}

static void
nm_supplicant_manager_class_init (NMSupplicantManagerClass *klass)
{
	GObjectClass * object_class = G_OBJECT_CLASS (klass);

	g_type_class_add_private (object_class, sizeof (NMSupplicantManagerPrivate));

	object_class->dispose = nm_supplicant_manager_dispose;

	/* Signals */
	nm_supplicant_manager_signals[STATE] =
		g_signal_new ("state",
		              G_OBJECT_CLASS_TYPE (object_class),
		              G_SIGNAL_RUN_LAST,
		              G_STRUCT_OFFSET (NMSupplicantManagerClass, state),
		              NULL, NULL,
		              nm_marshal_VOID__UINT_UINT,
		              G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
}

static void
nm_supplicant_manager_name_owner_changed (NMDBusManager *dbus_mgr,
                                          const char *name,
										  const char *old_owner,
										  const char *new_owner,
                                          gpointer user_data)
{
	NMSupplicantManager * self = (NMSupplicantManager *) user_data;
	NMSupplicantManagerPrivate *priv = NM_SUPPLICANT_MANAGER_GET_PRIVATE (self);
	gboolean old_owner_good = (old_owner && strlen (old_owner));
	gboolean new_owner_good = (new_owner && strlen (new_owner));

	/* Can't handle the signal if its not from the supplicant service */
	if (strcmp (WPAS_DBUS_SERVICE, name) != 0)
		return;

	if (!old_owner_good && new_owner_good) {
		gboolean running;

		running = nm_supplicant_manager_startup (self);

		if (running && priv->poke_id) {
			g_source_remove (priv->poke_id);
			priv->poke_id = 0;
		}
	} else if (old_owner_good && !new_owner_good) {
		nm_supplicant_manager_set_state (self, NM_SUPPLICANT_MANAGER_STATE_DOWN);

		if (priv->poke_id)
			g_source_remove (priv->poke_id);

		/* Poke the supplicant so that it gets activated by dbus system bus
		 * activation.
		 */
		priv->poke_id = g_idle_add (poke_supplicant_cb, (gpointer) self);
	}
}


guint32
nm_supplicant_manager_get_state (NMSupplicantManager * self)
{
	g_return_val_if_fail (NM_IS_SUPPLICANT_MANAGER (self), FALSE);

	return NM_SUPPLICANT_MANAGER_GET_PRIVATE (self)->state;
}

static void
nm_supplicant_manager_set_state (NMSupplicantManager * self, guint32 new_state)
{
	NMSupplicantManagerPrivate *priv = NM_SUPPLICANT_MANAGER_GET_PRIVATE (self);
	guint32 old_state;

	if (new_state == priv->state)
		return;

	old_state = priv->state;
	priv->state = new_state;
	g_signal_emit (self,
	               nm_supplicant_manager_signals[STATE],
	               0,
	               priv->state,
	               old_state);
}

static gboolean
nm_supplicant_manager_startup (NMSupplicantManager * self)
{
	gboolean running;

	/* FIXME: convert to pending call */
	running = nm_dbus_manager_name_has_owner (NM_SUPPLICANT_MANAGER_GET_PRIVATE (self)->dbus_mgr,
	                                          WPAS_DBUS_SERVICE);
	if (running)
		nm_supplicant_manager_set_state (self, NM_SUPPLICANT_MANAGER_STATE_IDLE);

	return running;
}

NMSupplicantInterface *
nm_supplicant_manager_get_iface (NMSupplicantManager * self,
								 const char *ifname,
								 gboolean is_wireless)
{
	NMSupplicantManagerPrivate *priv;
	NMSupplicantInterface * iface = NULL;
	GSList * elt;

	g_return_val_if_fail (NM_IS_SUPPLICANT_MANAGER (self), NULL);
	g_return_val_if_fail (ifname != NULL, NULL);

	priv = NM_SUPPLICANT_MANAGER_GET_PRIVATE (self);

	/* Ensure we don't already have this interface */
	for (elt = priv->ifaces; elt; elt = g_slist_next (elt)) {
		NMSupplicantInterface * if_tmp = (NMSupplicantInterface *) elt->data;

		if (!strcmp (ifname, nm_supplicant_interface_get_device (if_tmp))) {
			iface = if_tmp;
			break;
		}
	}

	if (!iface) {
		iface = nm_supplicant_interface_new (self, ifname, is_wireless);
		if (iface)
			priv->ifaces = g_slist_append (priv->ifaces, iface);
	}

	return iface;
}

void
nm_supplicant_manager_release_iface (NMSupplicantManager * self,
                                     NMSupplicantInterface * iface)
{
	NMSupplicantManagerPrivate *priv;
	GSList * elt;

	g_return_if_fail (NM_IS_SUPPLICANT_MANAGER (self));
	g_return_if_fail (NM_IS_SUPPLICANT_INTERFACE (iface));

	priv = NM_SUPPLICANT_MANAGER_GET_PRIVATE (self);

	for (elt = priv->ifaces; elt; elt = g_slist_next (elt)) {
		NMSupplicantInterface * if_tmp = (NMSupplicantInterface *) elt->data;

		if (if_tmp == iface) {
			/* Remove the iface from the supplicant manager's list and
			 * dereference to match additional reference in get_iface.
			 */
			priv->ifaces = g_slist_remove_link (priv->ifaces, elt);
			g_slist_free_1 (elt);
			g_object_unref (iface);
			break;
		}
	}
}