proxy.c revision 71812b42755cf65c694873ac66bcaa6bf3c817ca
1/*
2 *
3 *  BlueZ - Bluetooth protocol stack for Linux
4 *
5 *  Copyright (C) 2004-2008  Marcel Holtmann <marcel@holtmann.org>
6 *
7 *
8 *  This program is free software; you can redistribute it and/or modify
9 *  it under the terms of the GNU General Public License as published by
10 *  the Free Software Foundation; either version 2 of the License, or
11 *  (at your option) any later version.
12 *
13 *  This program is distributed in the hope that it will be useful,
14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *  GNU General Public License for more details.
17 *
18 *  You should have received a copy of the GNU General Public License
19 *  along with this program; if not, write to the Free Software
20 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 *
22 */
23
24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
28#include <ctype.h>
29#include <dirent.h>
30#include <errno.h>
31#include <fcntl.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <termios.h>
36#include <unistd.h>
37#include <arpa/inet.h>
38#include <sys/param.h>
39#include <sys/ioctl.h>
40#include <sys/stat.h>
41#include <sys/types.h>
42#include <sys/un.h>
43
44#include <bluetooth/bluetooth.h>
45#include <bluetooth/hci.h>
46#include <bluetooth/hci_lib.h>
47#include <bluetooth/sdp.h>
48#include <bluetooth/sdp_lib.h>
49#include <bluetooth/rfcomm.h>
50
51#include <glib.h>
52#include <gdbus.h>
53
54#include "../src/dbus-common.h"
55
56#include "logging.h"
57#include "textfile.h"
58
59#include "error.h"
60#include "storage.h"
61#include "sdpd.h"
62#include "glib-helper.h"
63
64#define SERIAL_PORT_NAME	"spp"
65#define SERIAL_PORT_UUID	"00001101-0000-1000-8000-00805F9B34FB"
66
67#define DIALUP_NET_NAME		"dun"
68#define DIALUP_NET_UUID		"00001103-0000-1000-8000-00805F9B34FB"
69
70#define SERIAL_PROXY_INTERFACE	"org.bluez.SerialProxy"
71#define SERIAL_MANAGER_INTERFACE "org.bluez.SerialProxyManager"
72#define BUF_SIZE		1024
73
74typedef enum {
75	TTY_PROXY,
76	UNIX_SOCKET_PROXY,
77	TCP_SOCKET_PROXY,
78	UNKNOWN_PROXY_TYPE = 0xFF
79} proxy_type_t;
80
81struct serial_adapter {
82	bdaddr_t	src;		/* Adapter address */
83	char		*path;		/* D-Bus path */
84	DBusConnection	*conn;		/* Adapter connection */
85	GSList		*proxies;	/* Proxies list */
86};
87
88struct serial_proxy {
89	bdaddr_t	dst;		/* Remote address */
90	char		*path;		/* Proxy path */
91	char		*uuid128;	/* UUID 128 */
92	char		*address;	/* TTY or Unix socket name */
93	short int	port;		/* TCP port */
94	proxy_type_t	type;		/* TTY or Unix socket */
95	struct termios  sys_ti;		/* Default TTY setting */
96	struct termios  proxy_ti;	/* Proxy TTY settings */
97	uint8_t		channel;	/* RFCOMM channel */
98	uint32_t	record_id;	/* Service record id */
99	GIOChannel	*io;		/* Server listen */
100	guint		rfcomm_watch;	/* RFCOMM watch: Remote */
101	guint		local_watch;	/* Local watch: TTY or Unix socket */
102	struct serial_adapter *adapter;	/* Adapter pointer */
103};
104
105static GSList *adapters = NULL;
106static int sk_counter = 0;
107
108static void disable_proxy(struct serial_proxy *prx)
109{
110	if (prx->rfcomm_watch) {
111		g_source_remove(prx->rfcomm_watch);
112		prx->rfcomm_watch = 0;
113	}
114
115	if (prx->local_watch) {
116		g_source_remove(prx->local_watch);
117		prx->local_watch = 0;
118	}
119
120	remove_record_from_server(prx->record_id);
121	prx->record_id = 0;
122
123	g_io_channel_unref(prx->io);
124	prx->io = NULL;
125}
126
127static void proxy_free(struct serial_proxy *prx)
128{
129	g_free(prx->path);
130	g_free(prx->address);
131	g_free(prx->uuid128);
132	g_free(prx);
133}
134
135static inline DBusMessage *does_not_exist(DBusMessage *msg,
136					const char *description)
137{
138	return g_dbus_create_error(msg, ERROR_INTERFACE ".DoesNotExist",
139				description);
140}
141
142static inline DBusMessage *invalid_arguments(DBusMessage *msg,
143					const char *description)
144{
145	return g_dbus_create_error(msg, ERROR_INTERFACE ".InvalidArguments",
146				description);
147}
148
149static inline DBusMessage *failed(DBusMessage *msg, const char *description)
150{
151	return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
152				description);
153}
154
155static void add_lang_attr(sdp_record_t *r)
156{
157	sdp_lang_attr_t base_lang;
158	sdp_list_t *langs = 0;
159
160	/* UTF-8 MIBenum (http://www.iana.org/assignments/character-sets) */
161	base_lang.code_ISO639 = (0x65 << 8) | 0x6e;
162	base_lang.encoding = 106;
163	base_lang.base_offset = SDP_PRIMARY_LANG_BASE;
164	langs = sdp_list_append(0, &base_lang);
165	sdp_set_lang_attr(r, langs);
166	sdp_list_free(langs, 0);
167}
168
169static sdp_record_t *proxy_record_new(const char *uuid128, uint8_t channel)
170{
171	sdp_list_t *apseq, *aproto, *profiles, *proto[2], *root, *svclass_id;
172	uuid_t uuid, root_uuid, l2cap, rfcomm;
173	sdp_profile_desc_t profile;
174	sdp_record_t *record;
175	sdp_data_t *ch;
176
177	record = sdp_record_alloc();
178	if (!record)
179		return NULL;
180
181	sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
182	root = sdp_list_append(NULL, &root_uuid);
183	sdp_set_browse_groups(record, root);
184	sdp_list_free(root, NULL);
185
186	bt_string2uuid(&uuid, uuid128);
187	svclass_id = sdp_list_append(NULL, &uuid);
188	sdp_set_service_classes(record, svclass_id);
189	sdp_list_free(svclass_id, NULL);
190
191	sdp_uuid16_create(&profile.uuid, SERIAL_PORT_PROFILE_ID);
192	profile.version = 0x0100;
193	profiles = sdp_list_append(NULL, &profile);
194	sdp_set_profile_descs(record, profiles);
195	sdp_list_free(profiles, NULL);
196
197	sdp_uuid16_create(&l2cap, L2CAP_UUID);
198	proto[0] = sdp_list_append(NULL, &l2cap);
199	apseq = sdp_list_append(NULL, proto[0]);
200
201	sdp_uuid16_create(&rfcomm, RFCOMM_UUID);
202	proto[1] = sdp_list_append(NULL, &rfcomm);
203	ch = sdp_data_alloc(SDP_UINT8, &channel);
204	proto[1] = sdp_list_append(proto[1], ch);
205	apseq = sdp_list_append(apseq, proto[1]);
206
207	aproto = sdp_list_append(NULL, apseq);
208	sdp_set_access_protos(record, aproto);
209
210	add_lang_attr(record);
211
212	sdp_set_info_attr(record, "Port Proxy Entity",
213				NULL, "Port Proxy Entity");
214
215	sdp_data_free(ch);
216	sdp_list_free(proto[0], NULL);
217	sdp_list_free(proto[1], NULL);
218	sdp_list_free(apseq, NULL);
219	sdp_list_free(aproto, NULL);
220
221	return record;
222}
223
224static GIOError channel_write(GIOChannel *chan, char *buf, size_t size)
225{
226	GIOError err = G_IO_ERROR_NONE;
227	gsize wbytes, written;
228
229	wbytes = written = 0;
230	while (wbytes < size) {
231		err = g_io_channel_write(chan,
232				buf + wbytes,
233				size - wbytes,
234				&written);
235
236		if (err != G_IO_ERROR_NONE)
237			return err;
238
239		wbytes += written;
240	}
241
242	return err;
243}
244
245static gboolean forward_data(GIOChannel *chan, GIOCondition cond, gpointer data)
246{
247	char buf[BUF_SIZE];
248	GIOChannel *dest = data;
249	GIOError err;
250	size_t rbytes;
251
252	if (cond & G_IO_NVAL)
253		return FALSE;
254
255	if (cond & (G_IO_HUP | G_IO_ERR)) {
256		/* Try forward remaining data */
257		do {
258			rbytes = 0;
259			err = g_io_channel_read(chan, buf, sizeof(buf), &rbytes);
260			if (err != G_IO_ERROR_NONE || rbytes == 0)
261				break;
262
263			err = channel_write(dest, buf, rbytes);
264		} while (err == G_IO_ERROR_NONE);
265
266		g_io_channel_close(dest);
267		return FALSE;
268	}
269
270	rbytes = 0;
271	err = g_io_channel_read(chan, buf, sizeof(buf), &rbytes);
272	if (err != G_IO_ERROR_NONE)
273		return FALSE;
274
275	err = channel_write(dest, buf, rbytes);
276	if (err != G_IO_ERROR_NONE)
277		return FALSE;
278
279	return TRUE;
280}
281
282static inline int unix_socket_connect(const char *address)
283{
284	struct sockaddr_un addr;
285	int err, sk;
286
287	memset(&addr, 0, sizeof(addr));
288	addr.sun_family = PF_UNIX;
289
290	if (strncmp("x00", address, 3) == 0) {
291		/*
292		 * Abstract namespace: first byte NULL, x00
293		 * must be removed from the original address.
294		 */
295		strcpy(addr.sun_path + 1, address + 3);
296	} else {
297		/* Filesystem address */
298		strcpy(addr.sun_path, address);
299	}
300
301	/* Unix socket */
302	sk = socket(AF_UNIX, SOCK_STREAM, 0);
303	if (sk < 0) {
304		err = errno;
305		error("Unix socket(%s) create failed: %s(%d)",
306				address, strerror(err), err);
307		return -err;
308	}
309
310	if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
311		err = errno;
312		error("Unix socket(%s) connect failed: %s(%d)",
313				address, strerror(err), err);
314		close(sk);
315		errno = err;
316		return -err;
317	}
318
319	return sk;
320}
321
322static int tcp_socket_connect(const char *address)
323{
324	struct sockaddr_in addr;
325	int err, sk;
326	unsigned short int port;
327
328	memset(&addr, 0, sizeof(addr));
329
330	if (strncmp(address, "localhost", 9) != 0) {
331		error("Address should have the form localhost:port.");
332		return -1;
333	}
334	port = atoi(strchr(address, ':') + 1);
335	if (port <= 0) {
336		error("Invalid port '%d'.", port);
337		return -1;
338	}
339	addr.sin_family = AF_INET;
340	addr.sin_addr.s_addr = inet_addr("127.0.0.1");
341	addr.sin_port = htons(port);
342
343	sk = socket(PF_INET, SOCK_STREAM, 0);
344	if (sk < 0) {
345		err = errno;
346		error("TCP socket(%s) create failed %s(%d)", address,
347							strerror(err), err);
348		return -err;
349	}
350	if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
351		err = errno;
352		error("TCP socket(%s) connect failed: %s(%d)",
353						address, strerror(err), err);
354		close(sk);
355		errno = err;
356		return -err;
357	}
358	return sk;
359}
360
361static inline int tty_open(const char *tty, struct termios *ti)
362{
363	int err, sk;
364
365	sk = open(tty, O_RDWR | O_NOCTTY);
366	if (sk < 0) {
367		err = errno;
368		error("Can't open TTY %s: %s(%d)", tty, strerror(err), err);
369		return -err;
370	}
371
372	if (ti && tcsetattr(sk, TCSANOW, ti) < 0) {
373		err = errno;
374		error("Can't change serial settings: %s(%d)",
375				strerror(err), err);
376		close(sk);
377		errno = err;
378		return -err;
379	}
380
381	return sk;
382}
383
384static void connect_event_cb(GIOChannel *chan, int err, const bdaddr_t *src,
385				const bdaddr_t *dst, gpointer data)
386{
387	struct serial_proxy *prx = data;
388	GIOChannel *io;
389	int sk;
390
391	if (err < 0) {
392		error("accept: %s (%d)", strerror(-err), -err);
393		return;
394	}
395
396	bacpy(&prx->dst, dst);
397
398	switch (prx->type) {
399	case UNIX_SOCKET_PROXY:
400		sk = unix_socket_connect(prx->address);
401		break;
402	case TTY_PROXY:
403		sk = tty_open(prx->address, &prx->proxy_ti);
404		break;
405	case TCP_SOCKET_PROXY:
406		sk = tcp_socket_connect(prx->address);
407		break;
408	default:
409		sk = -1;
410	}
411
412	if (sk < 0) {
413		g_io_channel_unref(chan);
414		return;
415	}
416
417	g_io_channel_set_close_on_unref(chan, TRUE);
418	io = g_io_channel_unix_new(sk);
419	g_io_channel_set_close_on_unref(io, TRUE);
420
421	prx->rfcomm_watch = g_io_add_watch(chan,
422				G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
423				forward_data, io);
424
425	prx->local_watch = g_io_add_watch(io,
426				G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
427				forward_data, chan);
428
429	g_io_channel_unref(chan);
430	g_io_channel_unref(io);
431
432	return;
433}
434
435static DBusMessage *proxy_enable(DBusConnection *conn,
436				DBusMessage *msg, void *data)
437{
438	struct serial_proxy *prx = data;
439	struct serial_adapter *adapter = prx->adapter;
440	sdp_record_t *record;
441
442	if (prx->io)
443		return failed(msg, "Already enabled");
444
445	/* Listen */
446	prx->io = bt_rfcomm_listen_allocate(&adapter->src, &prx->channel, 0,
447				connect_event_cb, prx);
448	if (!prx->io) {
449		const char *strerr = strerror(errno);
450		error("RFCOMM listen socket failed: %s(%d)", strerr, errno);
451		return failed(msg, strerr);
452	}
453
454	g_io_channel_set_close_on_unref(prx->io, TRUE);
455
456	record = proxy_record_new(prx->uuid128, prx->channel);
457	if (!record) {
458		g_io_channel_unref(prx->io);
459		return failed(msg, "Unable to allocate new service record");
460	}
461
462	if (add_record_to_server(&adapter->src, record) < 0) {
463		sdp_record_free(record);
464		g_io_channel_unref(prx->io);
465		return failed(msg, "Service registration failed");
466	}
467
468	prx->record_id = record->handle;
469
470	return dbus_message_new_method_return(msg);
471}
472
473static DBusMessage *proxy_disable(DBusConnection *conn,
474				DBusMessage *msg, void *data)
475{
476	struct serial_proxy *prx = data;
477
478	if (!prx->io)
479		return failed(msg, "Not enabled");
480
481	/* Remove the watches and unregister the record */
482	disable_proxy(prx);
483
484	return dbus_message_new_method_return(msg);
485}
486
487static DBusMessage *proxy_get_info(DBusConnection *conn,
488				DBusMessage *msg, void *data)
489{
490	struct serial_proxy *prx = data;
491	DBusMessage *reply;
492	DBusMessageIter iter, dict;
493	dbus_bool_t boolean;
494
495	reply = dbus_message_new_method_return(msg);
496	if (!reply)
497		return NULL;
498
499	dbus_message_iter_init_append(reply, &iter);
500
501	dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
502			DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
503			DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
504			DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
505
506	dbus_message_iter_append_dict_entry(&dict, "uuid",
507			DBUS_TYPE_STRING, &prx->uuid128);
508
509	dbus_message_iter_append_dict_entry(&dict, "address",
510			DBUS_TYPE_STRING, &prx->address);
511
512	if (prx->channel)
513		dbus_message_iter_append_dict_entry(&dict, "channel",
514				DBUS_TYPE_BYTE, &prx->channel);
515
516	boolean = (prx->io ? TRUE : FALSE);
517	dbus_message_iter_append_dict_entry(&dict, "enabled",
518			DBUS_TYPE_BOOLEAN, &boolean);
519
520	boolean = (prx->rfcomm_watch ? TRUE : FALSE);
521	dbus_message_iter_append_dict_entry(&dict, "connected",
522			DBUS_TYPE_BOOLEAN, &boolean);
523
524	/* If connected: append the remote address */
525	if (boolean) {
526		char bda[18];
527		const char *pstr = bda;
528
529		ba2str(&prx->dst, bda);
530		dbus_message_iter_append_dict_entry(&dict, "address",
531				DBUS_TYPE_STRING, &pstr);
532	}
533
534	dbus_message_iter_close_container(&iter, &dict);
535
536	return reply;
537}
538
539static struct {
540	const char	*str;
541	speed_t		speed;
542} supported_speed[]  = {
543	{"50",		B50	},
544	{"300",		B300	},
545	{"600",		B600	},
546	{"1200",	B1200	},
547	{"1800",	B1800	},
548	{"2400",	B2400	},
549	{"4800",	B4800	},
550	{"9600",	B9600	},
551	{"19200",	B19200	},
552	{"38400",	B38400	},
553	{"57600",	B57600	},
554	{"115200",	B115200	},
555	{ NULL,		B0	}
556};
557
558static speed_t str2speed(const char *str, speed_t *speed)
559{
560	int i;
561
562	for (i = 0; supported_speed[i].str; i++) {
563		if (strcmp(supported_speed[i].str, str) != 0)
564			continue;
565
566		if (speed)
567			*speed = supported_speed[i].speed;
568
569		return supported_speed[i].speed;
570	}
571
572	return B0;
573}
574
575static int set_parity(const char *str, tcflag_t *ctrl)
576{
577	if (strcasecmp("even", str) == 0) {
578		*ctrl |= PARENB;
579		*ctrl &= ~PARODD;
580	} else if (strcasecmp("odd", str) == 0) {
581		*ctrl |= PARENB;
582		*ctrl |= PARODD;
583	} else if (strcasecmp("mark", str) == 0)
584		*ctrl |= PARENB;
585	else if ((strcasecmp("none", str) == 0) ||
586			(strcasecmp("space", str) == 0))
587		*ctrl &= ~PARENB;
588	else
589		return -1;
590
591	return 0;
592}
593
594static int set_databits(uint8_t databits, tcflag_t *ctrl)
595{
596	if (databits < 5 || databits > 8)
597		return -EINVAL;
598
599	*ctrl &= ~CSIZE;
600	switch (databits) {
601	case 5:
602		*ctrl |= CS5;
603		break;
604	case 6:
605		*ctrl |= CS6;
606		break;
607	case 7:
608		*ctrl |= CS7;
609		break;
610	case 8:
611		*ctrl |= CS8;
612		break;
613	}
614
615	return 0;
616}
617
618static int set_stopbits(uint8_t stopbits, tcflag_t *ctrl)
619{
620	/* 1.5 will not be allowed */
621	switch (stopbits) {
622	case 1:
623		*ctrl &= ~CSTOPB;
624		return 0;
625	case 2:
626		*ctrl |= CSTOPB;
627		return 0;
628	}
629
630	return -EINVAL;
631}
632
633static DBusMessage *proxy_set_serial_params(DBusConnection *conn,
634						DBusMessage *msg, void *data)
635{
636	struct serial_proxy *prx = data;
637	struct serial_adapter *adapter = prx->adapter;
638	const char *ratestr, *paritystr;
639	uint8_t databits, stopbits;
640	tcflag_t ctrl;		/* Control mode flags */
641	speed_t speed = B0;	/* In/Out speed */
642
643	/* Don't allow change TTY settings if it is open */
644	if (prx->local_watch)
645		return failed(msg, "Not allowed");
646
647	if (!dbus_message_get_args(msg, NULL,
648				DBUS_TYPE_STRING, &ratestr,
649				DBUS_TYPE_BYTE, &databits,
650				DBUS_TYPE_BYTE, &stopbits,
651				DBUS_TYPE_STRING, &paritystr,
652				DBUS_TYPE_INVALID))
653		return NULL;
654
655	if (str2speed(ratestr, &speed)  == B0)
656		return invalid_arguments(msg, "Invalid baud rate");
657
658	ctrl = prx->proxy_ti.c_cflag;
659	if (set_databits(databits, &ctrl) < 0)
660		return invalid_arguments(msg, "Invalid data bits");
661
662	if (set_stopbits(stopbits, &ctrl) < 0)
663		return invalid_arguments(msg, "Invalid stop bits");
664
665	if (set_parity(paritystr, &ctrl) < 0)
666		return invalid_arguments(msg, "Invalid parity");
667
668	prx->proxy_ti.c_cflag = ctrl;
669	prx->proxy_ti.c_cflag |= (CLOCAL | CREAD);
670	cfsetispeed(&prx->proxy_ti, speed);
671	cfsetospeed(&prx->proxy_ti, speed);
672
673	proxy_store(&adapter->src, prx->uuid128, prx->address, NULL,
674				prx->channel, 0, &prx->proxy_ti);
675
676	return dbus_message_new_method_return(msg);
677}
678
679static GDBusMethodTable proxy_methods[] = {
680	{ "Enable",			"",	"",	proxy_enable },
681	{ "Disable",			"",	"",	proxy_disable },
682	{ "GetInfo",			"",	"a{sv}",proxy_get_info },
683	{ "SetSerialParameters",	"syys",	"",	proxy_set_serial_params },
684	{ },
685};
686
687static void proxy_path_unregister(gpointer data)
688{
689	struct serial_proxy *prx = data;
690	int sk;
691
692	info("Unregistered proxy: %s", prx->address);
693
694	if (prx->type != TTY_PROXY)
695		goto done;
696
697	/* Restore the initial TTY configuration */
698	sk =  open(prx->address, O_RDWR | O_NOCTTY);
699	if (sk) {
700		tcsetattr(sk, TCSAFLUSH, &prx->sys_ti);
701		close(sk);
702	}
703done:
704
705	proxy_free(prx);
706}
707
708static int register_proxy_object(struct serial_proxy *prx, char *outpath, size_t size)
709{
710	struct serial_adapter *adapter = prx->adapter;
711	char path[MAX_PATH_LENGTH + 1];
712
713	snprintf(path, MAX_PATH_LENGTH, "%s/proxy%d",
714			adapter->path, sk_counter++);
715
716	if (!g_dbus_register_interface(adapter->conn, path,
717					SERIAL_PROXY_INTERFACE,
718					proxy_methods, NULL, NULL,
719					prx, proxy_path_unregister)) {
720		error("D-Bus failed to register %s path", path);
721		return -1;
722	}
723
724	prx->path = g_strdup(path);
725	adapter->proxies = g_slist_append(adapter->proxies, prx);
726
727	if (outpath)
728		strncpy(outpath, path, size);
729
730	info("Registered proxy: %s", path);
731
732	return 0;
733}
734
735static int proxy_tty_register(struct serial_adapter *adapter,
736				const char *uuid128, const char *address,
737				struct termios *ti, char *outpath, size_t size,
738				gboolean save)
739{
740	struct termios sys_ti;
741	struct serial_proxy *prx;
742	int sk, ret;
743
744	sk = open(address, O_RDONLY | O_NOCTTY);
745	if (sk < 0) {
746		error("Cant open TTY: %s(%d)", strerror(errno), errno);
747		return -EINVAL;
748	}
749
750	prx = g_new0(struct serial_proxy, 1);
751	prx->address = g_strdup(address);
752	prx->uuid128 = g_strdup(uuid128);
753	prx->type = TTY_PROXY;
754	prx->adapter = adapter;
755
756	/* Current TTY settings */
757	memset(&sys_ti, 0, sizeof(sys_ti));
758	tcgetattr(sk, &sys_ti);
759	memcpy(&prx->sys_ti, &sys_ti, sizeof(sys_ti));
760	close(sk);
761
762	if (!ti) {
763		/* Use current settings */
764		memcpy(&prx->proxy_ti, &sys_ti, sizeof(sys_ti));
765	} else {
766		/* New TTY settings: user provided */
767		memcpy(&prx->proxy_ti, ti, sizeof(*ti));
768	}
769
770	ret = register_proxy_object(prx, outpath, size);
771	if (ret < 0)
772		proxy_free(prx);
773
774	if (save)
775		proxy_store(&adapter->src, uuid128, address, NULL,
776			prx->channel, 0, &prx->proxy_ti);
777
778	return ret;
779}
780
781static int proxy_socket_register(struct serial_adapter *adapter,
782				const char *uuid128, const char *address,
783				char *outpath, size_t size, gboolean save)
784{
785	struct serial_proxy *prx;
786	int ret;
787
788	prx = g_new0(struct serial_proxy, 1);
789	prx->address = g_strdup(address);
790	prx->uuid128 = g_strdup(uuid128);
791	prx->type = UNIX_SOCKET_PROXY;
792	prx->adapter = adapter;
793
794	ret = register_proxy_object(prx, outpath, size);
795	if (ret < 0)
796		proxy_free(prx);
797
798	if (save)
799		proxy_store(&adapter->src, uuid128, address, NULL,
800				prx->channel, 0, NULL);
801
802	return ret;
803}
804
805static int proxy_tcp_register(struct serial_adapter *adapter,
806				const char *uuid128, const char *address,
807				char *outpath, size_t size, gboolean save)
808{
809	struct serial_proxy *prx;
810	int ret;
811
812	prx = g_new0(struct serial_proxy, 1);
813	prx->address = g_strdup(address);
814	prx->uuid128 = g_strdup(uuid128);
815	prx->type = TCP_SOCKET_PROXY;
816	prx->adapter = adapter;
817
818	ret = register_proxy_object(prx, outpath, size);
819	if (ret < 0)
820		proxy_free(prx);
821
822	if (save)
823		proxy_store(&adapter->src, uuid128, address, NULL,
824				prx->channel, 0, NULL);
825
826	return ret;
827}
828
829static proxy_type_t addr2type(const char *address)
830{
831	struct stat st;
832
833	if (stat(address, &st) < 0) {
834		/*
835		 * Unix socket: if the sun_path starts with null byte
836		 * it refers to abstract namespace. 'x00' will be used
837		 * to represent the null byte.
838		 */
839		if (strncmp("localhost:", address, 10) == 0)
840			return TCP_SOCKET_PROXY;
841		if (strncmp("x00", address, 3) != 0)
842			return UNKNOWN_PROXY_TYPE;
843		else
844			return UNIX_SOCKET_PROXY;
845	} else {
846		/* Filesystem: char device or unix socket */
847		if (S_ISCHR(st.st_mode) && strncmp("/dev/", address, 4) == 0)
848			return TTY_PROXY;
849		else if (S_ISSOCK(st.st_mode))
850			return UNIX_SOCKET_PROXY;
851		else
852			return UNKNOWN_PROXY_TYPE;
853	}
854}
855
856static int proxy_addrcmp(gconstpointer proxy, gconstpointer addr)
857{
858	const struct serial_proxy *prx = proxy;
859	const char *address = addr;
860
861	return strcmp(prx->address, address);
862}
863
864static int proxy_pathcmp(gconstpointer proxy, gconstpointer p)
865{
866	const struct serial_proxy *prx = proxy;
867	const char *path = p;
868
869	return strcmp(prx->path, path);
870}
871
872static DBusMessage *create_proxy(DBusConnection *conn,
873				DBusMessage *msg, void *data)
874{
875	struct serial_adapter *adapter = data;
876	char path[MAX_PATH_LENGTH + 1];
877	const char *uuid128, *address, *ppath = path;
878	DBusMessage *reply;
879	proxy_type_t type;
880	uuid_t uuid;
881	int ret;
882
883	if (!dbus_message_get_args(msg, NULL,
884				DBUS_TYPE_STRING, &uuid128,
885				DBUS_TYPE_STRING, &address,
886				DBUS_TYPE_INVALID))
887		return NULL;
888
889	if (bt_string2uuid(&uuid, uuid128) < 0)
890		return invalid_arguments(msg, "Invalid UUID");
891
892	type = addr2type(address);
893	if (type == UNKNOWN_PROXY_TYPE)
894		return invalid_arguments(msg, "Invalid address");
895
896	/* Only one proxy per address(TTY or unix socket) is allowed */
897	if (g_slist_find_custom(adapter->proxies, address, proxy_addrcmp))
898		return g_dbus_create_error(msg, ERROR_INTERFACE ".AlreadyExist",
899						"Proxy already exists");
900
901	reply = dbus_message_new_method_return(msg);
902	if (!reply)
903		return NULL;
904
905	switch (type) {
906	case UNIX_SOCKET_PROXY:
907		ret = proxy_socket_register(adapter, uuid128, address,
908						path, sizeof(path), TRUE);
909		break;
910	case TTY_PROXY:
911		ret = proxy_tty_register(adapter, uuid128, address,
912				NULL, path, sizeof(path), TRUE);
913		break;
914	case TCP_SOCKET_PROXY:
915		ret = proxy_tcp_register(adapter, uuid128, address,
916					path, sizeof(path), TRUE);
917		break;
918	default:
919		ret = -1;
920	}
921	if (ret < 0) {
922		dbus_message_unref(reply);
923		return failed(msg, "Create object path failed");
924	}
925
926	g_dbus_emit_signal(adapter->conn, adapter->path,
927			SERIAL_MANAGER_INTERFACE, "ProxyCreated",
928			DBUS_TYPE_STRING, &ppath,
929			DBUS_TYPE_INVALID);
930
931	dbus_message_append_args(reply,
932			DBUS_TYPE_STRING, &ppath,
933			DBUS_TYPE_INVALID);
934
935	return reply;
936}
937
938static DBusMessage *list_proxies(DBusConnection *conn,
939				DBusMessage *msg, void *data)
940{
941	struct serial_adapter *adapter = data;
942	const GSList *l;
943	DBusMessage *reply;
944	DBusMessageIter iter, iter_array;
945
946	reply = dbus_message_new_method_return(msg);
947	if (!reply)
948		return NULL;
949
950	dbus_message_iter_init_append(reply, &iter);
951	dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
952			DBUS_TYPE_STRING_AS_STRING, &iter_array);
953
954	for (l = adapter->proxies; l; l = l->next) {
955		struct serial_proxy *prx = l->data;
956
957		dbus_message_iter_append_basic(&iter_array,
958				DBUS_TYPE_STRING, &prx->path);
959	}
960
961	dbus_message_iter_close_container(&iter, &iter_array);
962
963	return reply;
964}
965
966static DBusMessage *remove_proxy(DBusConnection *conn,
967				DBusMessage *msg, void *data)
968{
969	struct serial_adapter *adapter = data;
970	struct serial_proxy *prx;
971	const char *path;
972	GSList *l;
973
974	if (!dbus_message_get_args(msg, NULL,
975				DBUS_TYPE_STRING, &path,
976				DBUS_TYPE_INVALID))
977		return NULL;
978
979	l = g_slist_find_custom(adapter->proxies, path, proxy_pathcmp);
980	if (!l)
981		return does_not_exist(msg, "Invalid proxy path");
982
983	g_dbus_emit_signal(conn, adapter->path,
984			SERIAL_MANAGER_INTERFACE, "ProxyRemoved",
985			DBUS_TYPE_STRING, &path,
986			DBUS_TYPE_INVALID);
987
988	prx = l->data;
989	proxy_delete(&adapter->src, prx->address);
990	adapter->proxies = g_slist_remove(adapter->proxies, prx);
991
992	g_dbus_unregister_interface(conn, path, SERIAL_PROXY_INTERFACE);
993
994	return dbus_message_new_method_return(msg);
995}
996
997static void manager_path_unregister(void *data)
998{
999	struct serial_adapter *adapter = data;
1000	GSList *l;
1001
1002	/* Remove proxy objects */
1003	for (l = adapter->proxies; l; l = l->next) {
1004		struct serial_proxy *prx = l->data;
1005		char *path = g_strdup(prx->path);
1006
1007		g_dbus_unregister_interface(adapter->conn, path,
1008					SERIAL_PROXY_INTERFACE);
1009		g_free(path);
1010	}
1011
1012	if (adapter->conn)
1013		dbus_connection_unref(adapter->conn);
1014
1015	adapters = g_slist_remove(adapters, adapter);
1016	g_slist_free(adapter->proxies);
1017	g_free(adapter->path);
1018	g_free(adapter);
1019}
1020
1021static GDBusMethodTable manager_methods[] = {
1022	{ "CreateProxy",		"ss",	"s",	create_proxy },
1023	{ "ListProxies",		"",	"as",	list_proxies },
1024	{ "RemoveProxy",		"s",	"",	remove_proxy },
1025	{ },
1026};
1027
1028static GDBusSignalTable manager_signals[] = {
1029	{ "ProxyCreated",		"s"	},
1030	{ "ProxyRemoved",		"s"	},
1031	{ }
1032};
1033
1034static void parse_proxy(char *key, char *value, void *data)
1035{
1036	struct serial_adapter *adapter = data;
1037	char uuid128[MAX_LEN_UUID_STR], tmp[3];
1038	char *pvalue;
1039	proxy_type_t type;
1040	int ch, opts, pos;
1041	struct termios ti;
1042	uint8_t *pti;
1043
1044	memset(uuid128, 0, sizeof(uuid128));
1045	ch = opts = pos = 0;
1046	if (sscanf(value,"%s %d 0x%04X %n", uuid128, &ch, &opts, &pos) != 3)
1047		return;
1048
1049	/* Extracting name */
1050	value += pos;
1051	pvalue = strchr(value, ':');
1052	if (!pvalue)
1053		return;
1054
1055	/* FIXME: currently name is not used */
1056	*pvalue = '\0';
1057
1058	type = addr2type(key);
1059	switch (type) {
1060	case TTY_PROXY:
1061		/* Extracting termios */
1062		pvalue++;
1063		if (!pvalue || strlen(pvalue) != (2 * sizeof(ti)))
1064			return;
1065
1066		memset(&ti, 0, sizeof(ti));
1067		memset(tmp, 0, sizeof(tmp));
1068
1069		/* Converting to termios struct */
1070		pti = (uint8_t *) &ti;
1071		for (pos = 0; pos < sizeof(ti); pos++, pvalue += 2, pti++) {
1072			memcpy(tmp, pvalue, 2);
1073			*pti = (uint8_t) strtol(tmp, NULL, 16);
1074		}
1075
1076		proxy_tty_register(adapter, uuid128, key, &ti, NULL, 0, FALSE);
1077		break;
1078	case UNIX_SOCKET_PROXY:
1079		proxy_socket_register(adapter, uuid128, key, NULL, 0, FALSE);
1080		break;
1081	case TCP_SOCKET_PROXY:
1082		proxy_tcp_register(adapter, uuid128, key, NULL, 0, FALSE);
1083		break;
1084	default:
1085		return;
1086	}
1087}
1088
1089static void register_stored(struct serial_adapter *adapter)
1090{
1091	char filename[PATH_MAX + 1];
1092	char address[18];
1093
1094	ba2str(&adapter->src, address);
1095	snprintf(filename, PATH_MAX, "%s/%s/proxy", STORAGEDIR, address);
1096	textfile_foreach(filename, parse_proxy, adapter);
1097}
1098
1099static struct serial_adapter *find_adapter(GSList *list, const char *path)
1100{
1101	GSList *l;
1102
1103	for (l = list; l; l = l->next) {
1104		struct serial_adapter *adapter = l->data;
1105
1106		if (g_str_equal(adapter->path, path))
1107			return adapter;
1108	}
1109
1110	return NULL;
1111}
1112
1113int proxy_register(DBusConnection *conn, const char *path, bdaddr_t *src)
1114{
1115	struct serial_adapter *adapter;
1116
1117	adapter = find_adapter(adapters, path);
1118	if (adapter)
1119		return -EINVAL;
1120
1121	adapter = g_new0(struct serial_adapter, 1);
1122	adapter->conn = dbus_connection_ref(conn);
1123	bacpy(&adapter->src, src);
1124	adapter->path = g_strdup(path);
1125
1126	if (!g_dbus_register_interface(conn, path,
1127					SERIAL_MANAGER_INTERFACE,
1128					manager_methods, manager_signals, NULL,
1129					adapter, manager_path_unregister)) {
1130		error("Failed to register %s interface to %s",
1131				SERIAL_MANAGER_INTERFACE, path);
1132		return -1;
1133	}
1134
1135	register_stored(adapter);
1136
1137	adapters = g_slist_append(adapters, adapter);
1138
1139	info("Registered interface %s on path %s",
1140		SERIAL_MANAGER_INTERFACE, path);
1141
1142	return 0;
1143}
1144
1145void proxy_unregister(const char *path)
1146{
1147	struct serial_adapter *adapter;
1148
1149	adapter = find_adapter(adapters, path);
1150	if (!adapter)
1151		return;
1152
1153	g_dbus_unregister_interface(adapter->conn, path,
1154			SERIAL_MANAGER_INTERFACE);
1155}
1156