summaryrefslogtreecommitdiff
path: root/client/services.c
blob: 99b1507921c050de655abd2ea4b0d8b94ff1c3bd (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2012  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 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 St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

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

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include "client/services.h"
#include "src/connman.h"
#include "dbus.h"

static void append_property_array(DBusMessageIter *iter, char *property,
						char **data, int num_args)
{
	DBusMessageIter value, array;
	int i;

	dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &property);

	connman_dbus_array_open(iter, &value);
	dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
					 DBUS_TYPE_STRING_AS_STRING, &array);

	for (i = 0; i < num_args; i++) {
		dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING,
						&data[i]);
		printf("Added: %s\n", data[i]);
	}
	dbus_message_iter_close_container(&value, &array);
	dbus_message_iter_close_container(iter, &value);
}

static void append_property_dict(DBusMessageIter *iter, char *property,
					char **keys, char **data, int num_args)
{
	DBusMessageIter value, dict, entry, dict_key;
	int i;
	unsigned char prefix;

	dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &property);

	/* Top most level is a{sv} */
	connman_dbus_dict_open_variant(iter, &value);

	connman_dbus_dict_open(&value, &dict);

	for (i = 0; i < num_args; i++) {
		dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
							NULL, &entry);
		dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING,
							&keys[i]);

		if (strcmp(property, "IPv6.Configuration") == 0 &&
					   g_strcmp0(keys[i], "PrefixLength")) {
			if (data[i] == NULL) {
				fprintf(stderr, "No values entered!\n");
				exit(EXIT_FAILURE);
			}
			prefix = atoi(data[i]);

			dbus_message_iter_open_container(&entry,
						DBUS_TYPE_VARIANT,
						DBUS_TYPE_BYTE_AS_STRING,
						&dict_key);
			dbus_message_iter_append_basic(&dict_key,
						       DBUS_TYPE_BYTE, &prefix);
		} else {
			dbus_message_iter_open_container(&entry,
						 DBUS_TYPE_VARIANT,
						 DBUS_TYPE_STRING_AS_STRING,
						 &dict_key);
			dbus_message_iter_append_basic(&dict_key,
							DBUS_TYPE_STRING,
							&data[i]);
		}
		dbus_message_iter_close_container(&entry, &dict_key);
		dbus_message_iter_close_container(&dict, &entry);
	}
	/* Close {sv}, then close a{sv} */
	connman_dbus_dict_close(&value, &dict);
	connman_dbus_dict_close(iter, &value);
}

void iterate_array(DBusMessageIter *iter)
{
	DBusMessageIter array_item;
	dbus_bool_t key_bool;
	char *key_str;

	dbus_message_iter_recurse(iter, &array_item);
	/* Make sure the entry is not NULL! */
	printf("[ ");
	while (dbus_message_iter_get_arg_type(&array_item) !=
					DBUS_TYPE_INVALID) {
		if (dbus_message_iter_get_arg_type(&array_item) ==
					DBUS_TYPE_STRING) {
			dbus_message_iter_get_basic(&array_item,
						&key_str);
			printf("%s ", key_str);
		} else if (dbus_message_iter_get_arg_type(&array_item) ==
					DBUS_TYPE_BOOLEAN) {
			dbus_message_iter_get_basic(&array_item, &key_bool);
			printf("%s ", key_bool == TRUE ? "True"
						: "False");
		}
		dbus_message_iter_next(&array_item);
	}
	if (dbus_message_iter_get_arg_type(&array_item) ==
					DBUS_TYPE_INVALID)
		printf("] ");
}

void iterate_dict(DBusMessageIter *dict, char *string, uint16_t key_int)
{
	DBusMessageIter dict_entry, sub_dict_entry;

	printf("{ ");
	while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
		dbus_message_iter_recurse(dict, &dict_entry);
		dbus_message_iter_get_basic(&dict_entry, &string);
		printf("%s=", string);
		dbus_message_iter_next(&dict_entry);
		while (dbus_message_iter_get_arg_type(&dict_entry)
							!= DBUS_TYPE_INVALID) {
			dbus_message_iter_recurse(&dict_entry, &sub_dict_entry);
			if (dbus_message_iter_get_arg_type(&sub_dict_entry)
							== DBUS_TYPE_UINT16) {
				dbus_message_iter_get_basic(&sub_dict_entry,
								&key_int);
				printf("%d ", key_int);
			} else if (dbus_message_iter_get_arg_type(&sub_dict_entry)
							== DBUS_TYPE_STRING) {
				dbus_message_iter_get_basic(&sub_dict_entry,
								&string);
				printf("%s ", string);
			} else if (dbus_message_iter_get_arg_type(&sub_dict_entry)
							== DBUS_TYPE_ARRAY) {
				iterate_array(&sub_dict_entry);
			}
			dbus_message_iter_next(&dict_entry);
		}
		dbus_message_iter_next(dict);
	}
	printf("}");
}

/* Get dictionary info about the current service and store it */
static void extract_service_properties(DBusMessageIter *dict,
				struct service_data *service)
{
	DBusMessageIter entry, value, array_item;
	char *key;
	char *key_str;
	uint16_t key_uint16;
	dbus_bool_t key_bool;

	while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
		dbus_message_iter_recurse(dict, &entry);
		dbus_message_iter_get_basic(&entry, &key);
		printf("\n  %s = ", key);
		if (strcmp(key, "Name") == 0 && strlen(key) < 5)
			service->name = key;

		dbus_message_iter_next(&entry);
		dbus_message_iter_recurse(&entry, &value);
		/* Check if entry is a dictionary itself */
		if (strcmp(key, "Ethernet") == 0 ||
			/* if just strcmp, the .Configuration names don't match
			 * and they are iterated with iterate_array instead*/
				strncmp(key, "IPv4", 4) == 0 ||
				strncmp(key, "IPv6", 4) == 0 ||
				strncmp(key, "Proxy", 5) == 0 ||
				strcmp(key, "Provider") == 0) {
			dbus_message_iter_recurse(&value, &array_item);
			iterate_dict(&array_item, key_str, key_uint16);
		} else
		switch (dbus_message_iter_get_arg_type(&value)) {
		case DBUS_TYPE_ARRAY:
			iterate_array(&value);
			break;
		case DBUS_TYPE_BOOLEAN:
			dbus_message_iter_get_basic(&value, &key_bool);
			printf("%s", key_bool == TRUE ? "True" : "False");
			break;
		case DBUS_TYPE_BYTE:
			dbus_message_iter_get_basic(&value, &key_uint16);
			printf("%d", key_uint16);
			break;
		case DBUS_TYPE_STRING:
			dbus_message_iter_get_basic(&value, &key_str);
			printf("%s", key_str);
			break;
		}
		dbus_message_iter_next(dict);
	}
	printf("\n\n");
}

static void match_service_name(DBusMessage *message, char *service_name,
						struct service_data *service)
{
	DBusMessageIter iter, array;

	dbus_message_iter_init(message, &iter);
	dbus_message_iter_recurse(&iter, &array);

	while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
		DBusMessageIter entry, dict;
		char *path;

		dbus_message_iter_recurse(&array, &entry);
		dbus_message_iter_get_basic(&entry, &path);

		service->path = strip_service_path(path);
		dbus_message_iter_next(&entry);
		dbus_message_iter_recurse(&entry, &dict);
		extract_service_name(&dict, service);
		if (g_strcmp0(service_name, service->name) == 0) {
			printf("    Matched %s with %s\n\n", service->name,
								service->path);
			break;
		}
		dbus_message_iter_next(&array);
	}
}

void extract_service_name(DBusMessageIter *dict, struct service_data *service)
{
	DBusMessageIter dict_entry, value;
	const char *key;
	const char *state;

	while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
		dbus_message_iter_recurse(dict, &dict_entry);
		dbus_message_iter_get_basic(&dict_entry, &key);
		if (strcmp(key, "Name") == 0) {
			dbus_message_iter_next(&dict_entry);
			dbus_message_iter_recurse(&dict_entry, &value);
			dbus_message_iter_get_basic(&value, &service->name);
		}
		if (strcmp(key, "AutoConnect") == 0) {
			dbus_message_iter_next(&dict_entry);
			dbus_message_iter_recurse(&dict_entry, &value);
			dbus_message_iter_get_basic(&value, &service->autoconn);
		}
		if (strcmp(key, "State") == 0) {
			dbus_message_iter_next(&dict_entry);
			dbus_message_iter_recurse(&dict_entry, &value);
			dbus_message_iter_get_basic(&value, &state);
			if (strcmp(state, "ready") == 0) {
				service->connected = TRUE;
				service->online = FALSE;
			} else if (strcmp(state, "online") == 0) {
				service->connected = FALSE;
				service->online = TRUE;
			} else {
				service->connected = FALSE;
				service->online = FALSE;
			}
		}
		if (strcmp(key, "Favorite") == 0) {
			dbus_message_iter_next(&dict_entry);
			dbus_message_iter_recurse(&dict_entry, &value);
			dbus_message_iter_get_basic(&value, &service->favorite);
		}
		dbus_message_iter_next(dict);
	}
}

/* Show detailed information about a service */
void extract_services(DBusMessage *message, char *service_name)
{
	DBusMessageIter iter, array;

	dbus_message_iter_init(message, &iter);
	dbus_message_iter_recurse(&iter, &array);

	while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
		DBusMessageIter entry, dict;
		struct service_data service;
		char *path;

		dbus_message_iter_recurse(&array, &entry);
		dbus_message_iter_get_basic(&entry, &path);

		service.path = strip_service_path(path);
		if (g_strcmp0(service.path, service_name) == 0) {
			printf("[ %s ]\n", service.path);
			dbus_message_iter_next(&entry);
			dbus_message_iter_recurse(&entry, &dict);
			extract_service_properties(&dict, &service);
		}
		dbus_message_iter_next(&array);
	}
}

/* Support both string names and path names for connecting to services */
char *strip_service_path(char *service)
{
	char *service_name;
	service_name = strrchr(service, '/');
	if (service_name == NULL)
		return service;
	else
		return service_name + 1;
}

/* Show a simple list of service names only */
void get_services(DBusMessage *message)
{
	DBusMessageIter iter, array;

	dbus_message_iter_init(message, &iter);
	dbus_message_iter_recurse(&iter, &array);

	while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
		DBusMessageIter entry, dict;
		struct service_data service;
		char *path;

		dbus_message_iter_recurse(&array, &entry);
		dbus_message_iter_get_basic(&entry, &path);

		service.path = strip_service_path(path);
		dbus_message_iter_next(&entry);
		dbus_message_iter_recurse(&entry, &dict);
		extract_service_name(&dict, &service);
		printf("%-1s%-1s%-1s %-20s { %s }\n",
			service.favorite ? "*" : "",
			service.autoconn ? "A" : "",
			service.online ? "O" : (service.connected ? "R" : ""),
			service.name, service.path);
		dbus_message_iter_next(&array);
	}
}

const char *find_service(DBusConnection *connection, DBusMessage *message,
			  char *service_name, struct service_data *service)
{
	DBusMessageIter iter, array, entry;
	char *path;

	service_name = strip_service_path(service_name);
	match_service_name(message, service_name, service);
	/* Service name did not match, so check if entry is a path */
	if (g_strcmp0(service_name, service->name)) {
		dbus_message_iter_init(message, &iter);
		dbus_message_iter_recurse(&iter, &array);

		while (dbus_message_iter_get_arg_type(&array) ==
							DBUS_TYPE_STRUCT) {
			dbus_message_iter_recurse(&array, &entry);
			dbus_message_iter_get_basic(&entry, &path);

			service->path = strip_service_path(path);
			if (g_strcmp0(service->path, service_name) == 0)
				return service->path;
			dbus_message_iter_next(&array);
		}
		fprintf(stderr, "'%s' is not a valid service name or path.\n",
								service_name);
		fprintf(stderr, "Use the 'services' command to find available "
							"services.\n");
		return NULL;
	} else
		return service->path;
}

int set_proxy_manual(DBusConnection *connection, DBusMessage *message,
				char *name, char **servers, char **excludes,
				int num_servers, int num_excludes)
{
	DBusMessage *message_send;
	DBusMessageIter iter, value, dict, entry, data;
	struct service_data service;
	char *path;
	const char *path_name;
	char *property = "Proxy.Configuration";
	char *method = "Method";
	char *manual = "manual";

	path_name = find_service(connection, message, name, &service);
	if (path_name == NULL)
		return -ENXIO;

	path = g_strdup_printf("/net/connman/service/%s", path_name);
	message_send = dbus_message_new_method_call("net.connman", path,
							"net.connman.Service",
							"SetProperty");

	if (message_send == NULL) {
		g_free(path);
		return -ENOMEM;
	}

	dbus_message_iter_init_append(message_send, &iter);
	dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &property);
	connman_dbus_dict_open_variant(&iter, &value);
	connman_dbus_dict_open(&value, &dict);
	dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY, NULL,
							&entry);
	dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &method);
	dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
						DBUS_TYPE_STRING_AS_STRING,
						&data);
	dbus_message_iter_append_basic(&data, DBUS_TYPE_STRING, &manual);
	dbus_message_iter_close_container(&entry, &data);
	dbus_message_iter_close_container(&dict, &entry);
	dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY, NULL,
							&entry);
	append_property_array(&entry, "Servers", servers, num_servers);
	dbus_message_iter_close_container(&dict, &entry);

	if (num_excludes != 0) {
		dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
							NULL, &entry);
		append_property_array(&entry, "Excludes", excludes,
							num_excludes);
		dbus_message_iter_close_container(&dict, &entry);
	}

	dbus_message_iter_close_container(&value, &dict);
	dbus_message_iter_close_container(&iter, &value);
	dbus_connection_send(connection, message_send, NULL);
	dbus_connection_flush(connection);
	dbus_message_unref(message_send);

	g_free(path);

	return 0;
}

int set_service_property(DBusConnection *connection, DBusMessage *message,
				char *name, char *property, char **keys,
				void *data, int num_args)
{
	DBusMessage *message_send;
	DBusMessageIter iter;
	struct service_data service;
	char *path;
	const char *path_name;

	path_name = find_service(connection, message, name, &service);
	if (path_name == NULL)
		return -ENXIO;

	path = g_strdup_printf("/net/connman/service/%s", path_name);
	message_send = dbus_message_new_method_call("net.connman", path,
							"net.connman.Service",
							"SetProperty");

	if (message_send == NULL) {
		g_free(path);
		return -ENOMEM;
	}

	dbus_message_iter_init_append(message_send, &iter);

	if (strcmp(property, "AutoConnect") == 0)
		dbus_property_append_basic(&iter, (const char *) property,
						DBUS_TYPE_BOOLEAN, data);
	else if ((strcmp(property, "Domains.Configuration") == 0)
			|| (strcmp(property, "Timeservers.Configuration") == 0)
			|| (strcmp(property, "Nameservers.Configuration") == 0))
		append_property_array(&iter, property, data, num_args);
	else if ((strcmp(property, "IPv4.Configuration") == 0)
			|| (strcmp(property, "IPv6.Configuration") == 0)
			|| (strcmp(property, "Proxy.Configuration") == 0))
		append_property_dict(&iter, property, keys, data, num_args);

	dbus_connection_send(connection, message_send, NULL);
	dbus_connection_flush(connection);
	dbus_message_unref(message_send);
	g_free(path);

	return 0;
}

int remove_service(DBusConnection *connection, DBusMessage *message,
								char *name)
{
	struct service_data service;
	DBusMessage *message_send;
	const char *path_name;
	char *path;

	path_name = find_service(connection, message, name, &service);
	if (path_name == NULL)
		return -ENXIO;

	if (service.favorite == FALSE)
		return 0;

	path = g_strdup_printf("/net/connman/service/%s", path_name);
	message_send = dbus_message_new_method_call(CONNMAN_SERVICE, path,
						CONNMAN_SERVICE_INTERFACE,
						"Remove");
	if (message_send == NULL) {
		g_free(path);
		return -ENOMEM;
	}

	dbus_connection_send(connection, message_send, NULL);
	dbus_message_unref(message_send);
	g_free(path);

	return 0;
}