summaryrefslogtreecommitdiffstats
path: root/plugins/connman.c
blob: 875dd2d38cc70a70539476c89c1c05117e6e41ea (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
/*
 *
 *  oFono - Open Source Telephony
 *
 *  Copyright (C) 2008-2011  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  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 St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <stdlib.h>
#include <unistd.h>

#include <gdbus.h>
#include <string.h>

#define OFONO_API_SUBJECT_TO_CHANGE
#include <ofono/log.h>
#include <ofono/dbus.h>
#include <ofono/plugin.h>
#include <ofono/private-network.h>

#ifndef DBUS_TYPE_UNIX_FD
#define DBUS_TYPE_UNIX_FD -1
#endif

#define CONNMAN_SERVICE			"net.connman"
#define CONNMAN_PATH			"/net/connman"

#define CONNMAN_MANAGER_INTERFACE	CONNMAN_SERVICE ".Manager"
#define CONNMAN_MANAGER_PATH		"/"

static DBusConnection *connection;
static GHashTable *requests;
static unsigned int id;

struct connman_req {
	int uid;
	DBusPendingCall *pending;
	ofono_private_network_cb_t cb;
	void *data;
	gboolean redundant;
	char *path;
};

static void send_release(const char *path)
{
	DBusMessage *message;

	message = dbus_message_new_method_call(CONNMAN_SERVICE,
						CONNMAN_MANAGER_PATH,
						CONNMAN_MANAGER_INTERFACE,
						"ReleasePrivateNetwork");
	if (message == NULL)
		return;

	dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &path,
					DBUS_TYPE_INVALID);
	dbus_message_set_no_reply(message, TRUE);
	dbus_connection_send(connection, message, NULL);
	dbus_message_unref(message);
}

static void connman_release(int uid)
{
	struct connman_req *req;

	DBG("");

	req = g_hash_table_lookup(requests, &uid);
	if (req == NULL)
		return;

	if (req->pending) {
		/*
		* We want to cancel the request but we have to wait
		* the response of ConnMan. So we mark request as
		* redundant until we get the response, then we remove
		* it from hash table.
		*/
		req->redundant = TRUE;
		return;
	}

	send_release(req->path);
	g_hash_table_remove(requests, &req->uid);
}

static gboolean parse_reply(DBusMessage *reply, const char **path,
				struct ofono_private_network_settings *pns)
{
	DBusMessageIter array, dict, entry;

	if (!reply)
		return FALSE;

	if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
		return FALSE;

	if (dbus_message_iter_init(reply, &array) == FALSE)
		return FALSE;

	if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_OBJECT_PATH)
		return FALSE;

	dbus_message_iter_get_basic(&array, path);

	dbus_message_iter_next(&array);
	if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
		return FALSE;

	dbus_message_iter_recurse(&array, &dict);

	while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
		DBusMessageIter iter;
		const char *key;
		int type;

		dbus_message_iter_recurse(&dict, &entry);

		dbus_message_iter_get_basic(&entry, &key);

		dbus_message_iter_next(&entry);
		dbus_message_iter_recurse(&entry, &iter);

		type = dbus_message_iter_get_arg_type(&iter);
		if (type != DBUS_TYPE_STRING)
			break;

		if (g_str_equal(key, "ServerIPv4") &&
				type == DBUS_TYPE_STRING)
			dbus_message_iter_get_basic(&iter, &pns->server_ip);
		else if (g_str_equal(key, "PeerIPv4") &&
				type == DBUS_TYPE_STRING)
			dbus_message_iter_get_basic(&iter, &pns->peer_ip);
		else if (g_str_equal(key, "PrimaryDNS") &&
				type == DBUS_TYPE_STRING)
			dbus_message_iter_get_basic(&iter, &pns->primary_dns);
		else if (g_str_equal(key, "SecondaryDNS") &&
				type == DBUS_TYPE_STRING)
			dbus_message_iter_get_basic(&iter, &pns->secondary_dns);

		dbus_message_iter_next(&dict);
	}

	dbus_message_iter_next(&array);
	if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_UNIX_FD)
		return FALSE;

	dbus_message_iter_get_basic(&array, &pns->fd);

	return TRUE;
}

static void request_reply(DBusPendingCall *call, void *user_data)
{
	struct connman_req *req = user_data;
	DBusMessage *reply;
	const char *path = NULL;
	struct ofono_private_network_settings pns;

	DBG("");

	req->pending = NULL;

	memset(&pns, 0, sizeof(pns));
	pns.fd = -1;

	reply = dbus_pending_call_steal_reply(call);
	if (reply == NULL)
		goto badreply;

	if (parse_reply(reply, &path, &pns) == FALSE)
		goto error;

	DBG("fd: %d, path: %s", pns.fd, path);

	if (req->redundant == TRUE)
		goto redundant;

	if (pns.server_ip == NULL || pns.peer_ip == NULL ||
			pns.primary_dns == NULL || pns.secondary_dns == NULL ||
			pns.fd < 0) {
		ofono_error("Error while reading dictionary...\n");
		goto error;
	}

	req->path = g_strdup(path);
	req->cb(&pns, req->data);

	dbus_message_unref(reply);
	dbus_pending_call_unref(call);
	return;

error:
redundant:
	if (pns.fd != -1)
		close(pns.fd);

	if (path != NULL)
		send_release(path);

	dbus_message_unref(reply);

badreply:
	if (req->redundant == FALSE)
		req->cb(NULL, req->data);

	g_hash_table_remove(requests, &req->uid);
	dbus_pending_call_unref(call);
}

static int connman_request(ofono_private_network_cb_t cb, void *data)
{
	DBusMessage *message;
	DBusPendingCall *call;
	struct connman_req *req;

	DBG("");

	if (DBUS_TYPE_UNIX_FD < 0)
		return -EBADF;

	req = g_try_new(struct connman_req, 1);
	if (req == NULL)
		return -ENOMEM;

	message = dbus_message_new_method_call(CONNMAN_SERVICE,
						CONNMAN_MANAGER_PATH,
						CONNMAN_MANAGER_INTERFACE,
						"RequestPrivateNetwork");

	if (message == NULL) {
		g_free(req);
		return -ENOMEM;
	}

	if (dbus_connection_send_with_reply(connection, message,
						&call, 5000) == FALSE) {
		g_free(req);
		dbus_message_unref(message);
		return -EIO;
	}

	id++;
	req->pending = call;
	req->cb = cb;
	req->data = data;
	req->uid = id;
	req->redundant = FALSE;
	req->path = NULL;

	dbus_pending_call_set_notify(call, request_reply, req, NULL);
	g_hash_table_insert(requests, &req->uid, req);
	dbus_message_unref(message);

	return req->uid;
}

static struct ofono_private_network_driver pn_driver = {
	.name		= "ConnMan Private Network",
	.request	= connman_request,
	.release	= connman_release,
};

static void request_free(gpointer user_data)
{
	struct connman_req *req = user_data;

	g_free(req->path);
	g_free(req);
}

static int connman_init(void)
{
	DBG("");

	connection = ofono_dbus_get_connection();
	requests = g_hash_table_new_full(g_int_hash, g_int_equal, NULL,
						request_free);

	return ofono_private_network_driver_register(&pn_driver);
}

static void connman_exit(void)
{
	g_hash_table_destroy(requests);
	ofono_private_network_driver_unregister(&pn_driver);
}

OFONO_PLUGIN_DEFINE(connman, "ConnMan plugin", VERSION,
		OFONO_PLUGIN_PRIORITY_DEFAULT, connman_init, connman_exit)