Home | History | Annotate | Download | only in src
      1 /*
      2  *
      3  *  BlueZ - Bluetooth protocol stack for Linux
      4  *
      5  *  Copyright (C) 2009-2010  Marcel Holtmann <marcel (at) holtmann.org>
      6  *  Copyright (C) 2009-2010  Nokia Corporation
      7  *
      8  *
      9  *  This program is free software; you can redistribute it and/or modify
     10  *  it under the terms of the GNU General Public License as published by
     11  *  the Free Software Foundation; either version 2 of the License, or
     12  *  (at your option) any later version.
     13  *
     14  *  This program is distributed in the hope that it will be useful,
     15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17  *  GNU General Public License for more details.
     18  *
     19  *  You should have received a copy of the GNU General Public License
     20  *  along with this program; if not, write to the Free Software
     21  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     22  *
     23  */
     24 #include <stdarg.h>
     25 #include <stdlib.h>
     26 #include <unistd.h>
     27 #include <errno.h>
     28 #include <poll.h>
     29 #include <sys/types.h>
     30 #include <sys/socket.h>
     31 
     32 #include <bluetooth/bluetooth.h>
     33 #include <bluetooth/l2cap.h>
     34 #include <bluetooth/rfcomm.h>
     35 #include <bluetooth/sco.h>
     36 #include <bluetooth/hci.h>
     37 #include <bluetooth/hci_lib.h>
     38 
     39 #include <glib.h>
     40 
     41 #include "btio.h"
     42 
     43 #define ERROR_FAILED(gerr, str, err) \
     44 		g_set_error(gerr, BT_IO_ERROR, BT_IO_ERROR_FAILED, \
     45 				str ": %s (%d)", strerror(err), err)
     46 
     47 #define DEFAULT_DEFER_TIMEOUT 30
     48 
     49 struct set_opts {
     50 	bdaddr_t src;
     51 	bdaddr_t dst;
     52 	int defer;
     53 	int sec_level;
     54 	uint8_t channel;
     55 	uint16_t psm;
     56 	uint16_t mtu;
     57 	uint16_t imtu;
     58 	uint16_t omtu;
     59 	int master;
     60 	uint8_t mode;
     61 };
     62 
     63 struct connect {
     64 	BtIOConnect connect;
     65 	gpointer user_data;
     66 	GDestroyNotify destroy;
     67 };
     68 
     69 struct accept {
     70 	BtIOConnect connect;
     71 	gpointer user_data;
     72 	GDestroyNotify destroy;
     73 };
     74 
     75 struct server {
     76 	BtIOConnect connect;
     77 	BtIOConfirm confirm;
     78 	gpointer user_data;
     79 	GDestroyNotify destroy;
     80 };
     81 
     82 static void server_remove(struct server *server)
     83 {
     84 	if (server->destroy)
     85 		server->destroy(server->user_data);
     86 	g_free(server);
     87 }
     88 
     89 static void connect_remove(struct connect *conn)
     90 {
     91 	if (conn->destroy)
     92 		conn->destroy(conn->user_data);
     93 	g_free(conn);
     94 }
     95 
     96 static void accept_remove(struct accept *accept)
     97 {
     98 	if (accept->destroy)
     99 		accept->destroy(accept->user_data);
    100 	g_free(accept);
    101 }
    102 
    103 static gboolean check_nval(GIOChannel *io)
    104 {
    105 	struct pollfd fds;
    106 
    107 	memset(&fds, 0, sizeof(fds));
    108 	fds.fd = g_io_channel_unix_get_fd(io);
    109 	fds.events = POLLNVAL;
    110 
    111 	if (poll(&fds, 1, 0) > 0 && (fds.revents & POLLNVAL))
    112 		return TRUE;
    113 
    114 	return FALSE;
    115 }
    116 
    117 static gboolean accept_cb(GIOChannel *io, GIOCondition cond,
    118 							gpointer user_data)
    119 {
    120 	struct accept *accept = user_data;
    121 	GError *err = NULL;
    122 
    123 	/* If the user aborted this accept attempt */
    124 	if ((cond & G_IO_NVAL) || check_nval(io))
    125 		return FALSE;
    126 
    127 	if (cond & (G_IO_HUP | G_IO_ERR))
    128 		g_set_error(&err, BT_IO_ERROR, BT_IO_ERROR_DISCONNECTED,
    129 				"HUP or ERR on socket");
    130 
    131 	accept->connect(io, err, accept->user_data);
    132 
    133 	g_clear_error(&err);
    134 
    135 	return FALSE;
    136 }
    137 
    138 static gboolean connect_cb(GIOChannel *io, GIOCondition cond,
    139 							gpointer user_data)
    140 {
    141 	struct connect *conn = user_data;
    142 	GError *gerr = NULL;
    143 
    144 	/* If the user aborted this connect attempt */
    145 	if ((cond & G_IO_NVAL) || check_nval(io))
    146 		return FALSE;
    147 
    148 	if (cond & G_IO_OUT) {
    149 		int err = 0, sock = g_io_channel_unix_get_fd(io);
    150 		socklen_t len = sizeof(err);
    151 
    152 		if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &len) < 0)
    153 			err = errno;
    154 
    155 		if (err)
    156 			g_set_error(&gerr, BT_IO_ERROR,
    157 					BT_IO_ERROR_CONNECT_FAILED, "%s (%d)",
    158 					strerror(err), err);
    159 	} else if (cond & (G_IO_HUP | G_IO_ERR))
    160 		g_set_error(&gerr, BT_IO_ERROR, BT_IO_ERROR_CONNECT_FAILED,
    161 				"HUP or ERR on socket");
    162 
    163 	conn->connect(io, gerr, conn->user_data);
    164 
    165 	if (gerr)
    166 		g_error_free(gerr);
    167 
    168 	return FALSE;
    169 }
    170 
    171 static gboolean server_cb(GIOChannel *io, GIOCondition cond,
    172 							gpointer user_data)
    173 {
    174 	struct server *server = user_data;
    175 	int srv_sock, cli_sock;
    176 	GIOChannel *cli_io;
    177 
    178 	/* If the user closed the server */
    179 	if ((cond & G_IO_NVAL) || check_nval(io))
    180 		return FALSE;
    181 
    182 	srv_sock = g_io_channel_unix_get_fd(io);
    183 
    184 	cli_sock = accept(srv_sock, NULL, NULL);
    185 	if (cli_sock < 0)
    186 		return TRUE;
    187 
    188 	cli_io = g_io_channel_unix_new(cli_sock);
    189 
    190 	g_io_channel_set_close_on_unref(cli_io, TRUE);
    191 	g_io_channel_set_flags(cli_io, G_IO_FLAG_NONBLOCK, NULL);
    192 
    193 	if (server->confirm)
    194 		server->confirm(cli_io, server->user_data);
    195 	else
    196 		server->connect(cli_io, NULL, server->user_data);
    197 
    198 	g_io_channel_unref(cli_io);
    199 
    200 	return TRUE;
    201 }
    202 
    203 static void server_add(GIOChannel *io, BtIOConnect connect,
    204 				BtIOConfirm confirm, gpointer user_data,
    205 				GDestroyNotify destroy)
    206 {
    207 	struct server *server;
    208 	GIOCondition cond;
    209 
    210 	server = g_new0(struct server, 1);
    211 	server->connect = connect;
    212 	server->confirm = confirm;
    213 	server->user_data = user_data;
    214 	server->destroy = destroy;
    215 
    216 	cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
    217 	g_io_add_watch_full(io, G_PRIORITY_DEFAULT, cond, server_cb, server,
    218 					(GDestroyNotify) server_remove);
    219 }
    220 
    221 static void connect_add(GIOChannel *io, BtIOConnect connect,
    222 				gpointer user_data, GDestroyNotify destroy)
    223 {
    224 	struct connect *conn;
    225 	GIOCondition cond;
    226 
    227 	conn = g_new0(struct connect, 1);
    228 	conn->connect = connect;
    229 	conn->user_data = user_data;
    230 	conn->destroy = destroy;
    231 
    232 	cond = G_IO_OUT | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
    233 	g_io_add_watch_full(io, G_PRIORITY_DEFAULT, cond, connect_cb, conn,
    234 					(GDestroyNotify) connect_remove);
    235 }
    236 
    237 static void accept_add(GIOChannel *io, BtIOConnect connect, gpointer user_data,
    238 							GDestroyNotify destroy)
    239 {
    240 	struct accept *accept;
    241 	GIOCondition cond;
    242 
    243 	accept = g_new0(struct accept, 1);
    244 	accept->connect = connect;
    245 	accept->user_data = user_data;
    246 	accept->destroy = destroy;
    247 
    248 	cond = G_IO_OUT | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
    249 	g_io_add_watch_full(io, G_PRIORITY_DEFAULT, cond, accept_cb, accept,
    250 					(GDestroyNotify) accept_remove);
    251 }
    252 
    253 static int l2cap_bind(int sock, const bdaddr_t *src, uint16_t psm, GError **err)
    254 {
    255 	struct sockaddr_l2 addr;
    256 
    257 	memset(&addr, 0, sizeof(addr));
    258 	addr.l2_family = AF_BLUETOOTH;
    259 	bacpy(&addr.l2_bdaddr, src);
    260 	addr.l2_psm = htobs(psm);
    261 
    262 	if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
    263 		ERROR_FAILED(err, "l2cap_bind", errno);
    264 		return -1;
    265 	}
    266 
    267 	return 0;
    268 }
    269 
    270 static int l2cap_connect(int sock, const bdaddr_t *dst, uint16_t psm)
    271 {
    272 	int err;
    273 	struct sockaddr_l2 addr;
    274 
    275 	memset(&addr, 0, sizeof(addr));
    276 	addr.l2_family = AF_BLUETOOTH;
    277 	bacpy(&addr.l2_bdaddr, dst);
    278 	addr.l2_psm = htobs(psm);
    279 
    280 	err = connect(sock, (struct sockaddr *) &addr, sizeof(addr));
    281 	if (err < 0 && !(errno == EAGAIN || errno == EINPROGRESS))
    282 		return err;
    283 
    284 	return 0;
    285 }
    286 
    287 static int l2cap_set_master(int sock, int master)
    288 {
    289 	int flags;
    290 	socklen_t len;
    291 
    292 	len = sizeof(flags);
    293 	if (getsockopt(sock, SOL_L2CAP, L2CAP_LM, &flags, &len) < 0)
    294 		return -errno;
    295 
    296 	if (master) {
    297 		if (flags & L2CAP_LM_MASTER)
    298 			return 0;
    299 		flags |= L2CAP_LM_MASTER;
    300 	} else {
    301 		if (!(flags & L2CAP_LM_MASTER))
    302 			return 0;
    303 		flags &= ~L2CAP_LM_MASTER;
    304 	}
    305 
    306 	if (setsockopt(sock, SOL_L2CAP, L2CAP_LM, &flags, sizeof(flags)) < 0)
    307 		return -errno;
    308 
    309 	return 0;
    310 }
    311 
    312 static int rfcomm_set_master(int sock, int master)
    313 {
    314 	int flags;
    315 	socklen_t len;
    316 
    317 	len = sizeof(flags);
    318 	if (getsockopt(sock, SOL_RFCOMM, RFCOMM_LM, &flags, &len) < 0)
    319 		return -errno;
    320 
    321 	if (master) {
    322 		if (flags & RFCOMM_LM_MASTER)
    323 			return 0;
    324 		flags |= RFCOMM_LM_MASTER;
    325 	} else {
    326 		if (!(flags & RFCOMM_LM_MASTER))
    327 			return 0;
    328 		flags &= ~RFCOMM_LM_MASTER;
    329 	}
    330 
    331 	if (setsockopt(sock, SOL_RFCOMM, RFCOMM_LM, &flags, sizeof(flags)) < 0)
    332 		return -errno;
    333 
    334 	return 0;
    335 }
    336 
    337 static int l2cap_set_lm(int sock, int level)
    338 {
    339 	int lm_map[] = {
    340 		0,
    341 		L2CAP_LM_AUTH,
    342 		L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT,
    343 		L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT | L2CAP_LM_SECURE,
    344 	}, opt = lm_map[level];
    345 
    346 	if (setsockopt(sock, SOL_L2CAP, L2CAP_LM, &opt, sizeof(opt)) < 0)
    347 		return -errno;
    348 
    349 	return 0;
    350 }
    351 
    352 static int rfcomm_set_lm(int sock, int level)
    353 {
    354 	int lm_map[] = {
    355 		0,
    356 		RFCOMM_LM_AUTH,
    357 		RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT,
    358 		RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT | RFCOMM_LM_SECURE,
    359 	}, opt = lm_map[level];
    360 
    361 	if (setsockopt(sock, SOL_RFCOMM, RFCOMM_LM, &opt, sizeof(opt)) < 0)
    362 		return -errno;
    363 
    364 	return 0;
    365 }
    366 
    367 static gboolean set_sec_level(int sock, BtIOType type, int level, GError **err)
    368 {
    369 	struct bt_security sec;
    370 	int ret;
    371 
    372 	if (level < BT_SECURITY_LOW || level > BT_SECURITY_HIGH) {
    373 		g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
    374 				"Valid security level range is %d-%d",
    375 				BT_SECURITY_LOW, BT_SECURITY_HIGH);
    376 		return FALSE;
    377 	}
    378 
    379 	memset(&sec, 0, sizeof(sec));
    380 	sec.level = level;
    381 
    382 	if (setsockopt(sock, SOL_BLUETOOTH, BT_SECURITY, &sec,
    383 							sizeof(sec)) == 0)
    384 		return TRUE;
    385 
    386 	if (errno != ENOPROTOOPT) {
    387 		ERROR_FAILED(err, "setsockopt(BT_SECURITY)", errno);
    388 		return FALSE;
    389 	}
    390 
    391 	if (type == BT_IO_L2CAP)
    392 		ret = l2cap_set_lm(sock, level);
    393 	else
    394 		ret = rfcomm_set_lm(sock, level);
    395 
    396 	if (ret < 0) {
    397 		ERROR_FAILED(err, "setsockopt(LM)", -ret);
    398 		return FALSE;
    399 	}
    400 
    401 	return TRUE;
    402 }
    403 
    404 static int l2cap_get_lm(int sock, int *sec_level)
    405 {
    406 	int opt;
    407 	socklen_t len;
    408 
    409 	len = sizeof(opt);
    410 	if (getsockopt(sock, SOL_L2CAP, L2CAP_LM, &opt, &len) < 0)
    411 		return -errno;
    412 
    413 	*sec_level = 0;
    414 
    415 	if (opt & L2CAP_LM_AUTH)
    416 		*sec_level = BT_SECURITY_LOW;
    417 	if (opt & L2CAP_LM_ENCRYPT)
    418 		*sec_level = BT_SECURITY_MEDIUM;
    419 	if (opt & L2CAP_LM_SECURE)
    420 		*sec_level = BT_SECURITY_HIGH;
    421 
    422 	return 0;
    423 }
    424 
    425 static int rfcomm_get_lm(int sock, int *sec_level)
    426 {
    427 	int opt;
    428 	socklen_t len;
    429 
    430 	len = sizeof(opt);
    431 	if (getsockopt(sock, SOL_RFCOMM, RFCOMM_LM, &opt, &len) < 0)
    432 		return -errno;
    433 
    434 	*sec_level = 0;
    435 
    436 	if (opt & RFCOMM_LM_AUTH)
    437 		*sec_level = BT_SECURITY_LOW;
    438 	if (opt & RFCOMM_LM_ENCRYPT)
    439 		*sec_level = BT_SECURITY_MEDIUM;
    440 	if (opt & RFCOMM_LM_SECURE)
    441 		*sec_level = BT_SECURITY_HIGH;
    442 
    443 	return 0;
    444 }
    445 
    446 static gboolean get_sec_level(int sock, BtIOType type, int *level,
    447 								GError **err)
    448 {
    449 	struct bt_security sec;
    450 	socklen_t len;
    451 	int ret;
    452 
    453 	memset(&sec, 0, sizeof(sec));
    454 	len = sizeof(sec);
    455 	if (getsockopt(sock, SOL_BLUETOOTH, BT_SECURITY, &sec, &len) == 0) {
    456 		*level = sec.level;
    457 		return TRUE;
    458 	}
    459 
    460 	if (errno != ENOPROTOOPT) {
    461 		ERROR_FAILED(err, "getsockopt(BT_SECURITY)", errno);
    462 		return FALSE;
    463 	}
    464 
    465 	if (type == BT_IO_L2CAP)
    466 		ret = l2cap_get_lm(sock, level);
    467 	else
    468 		ret = rfcomm_get_lm(sock, level);
    469 
    470 	if (ret < 0) {
    471 		ERROR_FAILED(err, "getsockopt(LM)", -ret);
    472 		return FALSE;
    473 	}
    474 
    475 	return TRUE;
    476 }
    477 
    478 static gboolean l2cap_set(int sock, int sec_level, uint16_t imtu, uint16_t omtu,
    479 					uint8_t mode, int master, GError **err)
    480 {
    481 	if (imtu || omtu || mode) {
    482 		struct l2cap_options l2o;
    483 		socklen_t len;
    484 
    485 		memset(&l2o, 0, sizeof(l2o));
    486 		len = sizeof(l2o);
    487 		if (getsockopt(sock, SOL_L2CAP, L2CAP_OPTIONS, &l2o,
    488 								&len) < 0) {
    489 			ERROR_FAILED(err, "getsockopt(L2CAP_OPTIONS)", errno);
    490 			return FALSE;
    491 		}
    492 
    493 		if (imtu)
    494 			l2o.imtu = imtu;
    495 		if (omtu)
    496 			l2o.omtu = omtu;
    497 		if (mode)
    498 			l2o.mode = mode;
    499 
    500 		if (setsockopt(sock, SOL_L2CAP, L2CAP_OPTIONS, &l2o,
    501 							sizeof(l2o)) < 0) {
    502 			ERROR_FAILED(err, "setsockopt(L2CAP_OPTIONS)", errno);
    503 			return FALSE;
    504 		}
    505 	}
    506 
    507 	if (master >= 0 && l2cap_set_master(sock, master) < 0) {
    508 		ERROR_FAILED(err, "l2cap_set_master", errno);
    509 		return FALSE;
    510 	}
    511 
    512 	if (sec_level && !set_sec_level(sock, BT_IO_L2CAP, sec_level, err))
    513 		return FALSE;
    514 
    515 	return TRUE;
    516 }
    517 
    518 static int rfcomm_bind(int sock,
    519 		const bdaddr_t *src, uint8_t channel, GError **err)
    520 {
    521 	struct sockaddr_rc addr;
    522 
    523 	memset(&addr, 0, sizeof(addr));
    524 	addr.rc_family = AF_BLUETOOTH;
    525 	bacpy(&addr.rc_bdaddr, src);
    526 	addr.rc_channel = channel;
    527 
    528 	if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
    529 		ERROR_FAILED(err, "rfcomm_bind", errno);
    530 		return -1;
    531 	}
    532 
    533 	return 0;
    534 }
    535 
    536 static int rfcomm_connect(int sock, const bdaddr_t *dst, uint8_t channel)
    537 {
    538 	int err;
    539 	struct sockaddr_rc addr;
    540 
    541 	memset(&addr, 0, sizeof(addr));
    542 	addr.rc_family = AF_BLUETOOTH;
    543 	bacpy(&addr.rc_bdaddr, dst);
    544 	addr.rc_channel = channel;
    545 
    546 	err = connect(sock, (struct sockaddr *) &addr, sizeof(addr));
    547 	if (err < 0 && !(errno == EAGAIN || errno == EINPROGRESS))
    548 		return err;
    549 
    550 	return 0;
    551 }
    552 
    553 static gboolean rfcomm_set(int sock, int sec_level, int master, GError **err)
    554 {
    555 	if (sec_level && !set_sec_level(sock, BT_IO_RFCOMM, sec_level, err))
    556 		return FALSE;
    557 
    558 	if (master >= 0 && rfcomm_set_master(sock, master) < 0) {
    559 		ERROR_FAILED(err, "rfcomm_set_master", errno);
    560 		return FALSE;
    561 	}
    562 
    563 	return TRUE;
    564 }
    565 
    566 static int sco_bind(int sock, const bdaddr_t *src, GError **err)
    567 {
    568 	struct sockaddr_sco addr;
    569 
    570 	memset(&addr, 0, sizeof(addr));
    571 	addr.sco_family = AF_BLUETOOTH;
    572 	bacpy(&addr.sco_bdaddr, src);
    573 
    574 	if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
    575 		ERROR_FAILED(err, "sco_bind", errno);
    576 		return -1;
    577 	}
    578 
    579 	return 0;
    580 }
    581 
    582 static int sco_connect(int sock, const bdaddr_t *dst)
    583 {
    584 	struct sockaddr_sco addr;
    585 	int err;
    586 
    587 	memset(&addr, 0, sizeof(addr));
    588 	addr.sco_family = AF_BLUETOOTH;
    589 	bacpy(&addr.sco_bdaddr, dst);
    590 
    591 	err = connect(sock, (struct sockaddr *) &addr, sizeof(addr));
    592 	if (err < 0 && !(errno == EAGAIN || errno == EINPROGRESS))
    593 		return err;
    594 
    595 	return 0;
    596 }
    597 
    598 static gboolean sco_set(int sock, uint16_t mtu, GError **err)
    599 {
    600 	struct sco_options sco_opt;
    601 	socklen_t len;
    602 
    603 	if (!mtu)
    604 		return TRUE;
    605 
    606 	len = sizeof(sco_opt);
    607 	memset(&sco_opt, 0, len);
    608 	if (getsockopt(sock, SOL_SCO, SCO_OPTIONS, &sco_opt, &len) < 0) {
    609 		ERROR_FAILED(err, "getsockopt(SCO_OPTIONS)", errno);
    610 		return FALSE;
    611 	}
    612 
    613 	sco_opt.mtu = mtu;
    614 	if (setsockopt(sock, SOL_SCO, SCO_OPTIONS, &sco_opt,
    615 						sizeof(sco_opt)) < 0) {
    616 		ERROR_FAILED(err, "setsockopt(SCO_OPTIONS)", errno);
    617 		return FALSE;
    618 	}
    619 
    620 	return TRUE;
    621 }
    622 
    623 static gboolean parse_set_opts(struct set_opts *opts, GError **err,
    624 						BtIOOption opt1, va_list args)
    625 {
    626 	BtIOOption opt = opt1;
    627 	const char *str;
    628 
    629 	memset(opts, 0, sizeof(*opts));
    630 
    631 	/* Set defaults */
    632 	opts->defer = DEFAULT_DEFER_TIMEOUT;
    633 	opts->master = -1;
    634 	opts->sec_level = BT_IO_SEC_MEDIUM;
    635 	opts->mode = L2CAP_MODE_BASIC;
    636 
    637 	while (opt != BT_IO_OPT_INVALID) {
    638 		switch (opt) {
    639 		case BT_IO_OPT_SOURCE:
    640 			str = va_arg(args, const char *);
    641 			if (strncasecmp(str, "hci", 3) == 0)
    642 				hci_devba(atoi(str + 3), &opts->src);
    643 			else
    644 				str2ba(str, &opts->src);
    645 			break;
    646 		case BT_IO_OPT_SOURCE_BDADDR:
    647 			bacpy(&opts->src, va_arg(args, const bdaddr_t *));
    648 			break;
    649 		case BT_IO_OPT_DEST:
    650 			str2ba(va_arg(args, const char *), &opts->dst);
    651 			break;
    652 		case BT_IO_OPT_DEST_BDADDR:
    653 			bacpy(&opts->dst, va_arg(args, const bdaddr_t *));
    654 			break;
    655 		case BT_IO_OPT_DEFER_TIMEOUT:
    656 			opts->defer = va_arg(args, int);
    657 			break;
    658 		case BT_IO_OPT_SEC_LEVEL:
    659 			opts->sec_level = va_arg(args, int);
    660 			break;
    661 		case BT_IO_OPT_CHANNEL:
    662 			opts->channel = va_arg(args, int);
    663 			break;
    664 		case BT_IO_OPT_PSM:
    665 			opts->psm = va_arg(args, int);
    666 			break;
    667 		case BT_IO_OPT_MTU:
    668 			opts->mtu = va_arg(args, int);
    669 			opts->imtu = opts->mtu;
    670 			opts->omtu = opts->mtu;
    671 			break;
    672 		case BT_IO_OPT_OMTU:
    673 			opts->omtu = va_arg(args, int);
    674 			if (!opts->mtu)
    675 				opts->mtu = opts->omtu;
    676 			break;
    677 		case BT_IO_OPT_IMTU:
    678 			opts->imtu = va_arg(args, int);
    679 			if (!opts->mtu)
    680 				opts->mtu = opts->imtu;
    681 			break;
    682 		case BT_IO_OPT_MASTER:
    683 			opts->master = va_arg(args, gboolean);
    684 			break;
    685 		case BT_IO_OPT_MODE:
    686 			opts->mode = va_arg(args, int);
    687 			break;
    688 		default:
    689 			g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
    690 					"Unknown option %d", opt);
    691 			return FALSE;
    692 		}
    693 
    694 		opt = va_arg(args, int);
    695 	}
    696 
    697 	return TRUE;
    698 }
    699 
    700 static gboolean get_peers(int sock, struct sockaddr *src, struct sockaddr *dst,
    701 				socklen_t len, GError **err)
    702 {
    703 	socklen_t olen;
    704 
    705 	memset(src, 0, len);
    706 	olen = len;
    707 	if (getsockname(sock, src, &olen) < 0) {
    708 		ERROR_FAILED(err, "getsockname", errno);
    709 		return FALSE;
    710 	}
    711 
    712 	memset(dst, 0, len);
    713 	olen = len;
    714 	if (getpeername(sock, dst, &olen) < 0) {
    715 		ERROR_FAILED(err, "getpeername", errno);
    716 		return FALSE;
    717 	}
    718 
    719 	return TRUE;
    720 }
    721 
    722 static int l2cap_get_info(int sock, uint16_t *handle, uint8_t *dev_class)
    723 {
    724 	struct l2cap_conninfo info;
    725 	socklen_t len;
    726 
    727 	len = sizeof(info);
    728 	if (getsockopt(sock, SOL_L2CAP, L2CAP_CONNINFO, &info, &len) < 0)
    729 		return -errno;
    730 
    731 	if (handle)
    732 		*handle = info.hci_handle;
    733 
    734 	if (dev_class)
    735 		memcpy(dev_class, info.dev_class, 3);
    736 
    737 	return 0;
    738 }
    739 
    740 static gboolean l2cap_get(int sock, GError **err, BtIOOption opt1,
    741 								va_list args)
    742 {
    743 	BtIOOption opt = opt1;
    744 	struct sockaddr_l2 src, dst;
    745 	struct l2cap_options l2o;
    746 	int flags;
    747 	uint8_t dev_class[3];
    748 	uint16_t handle;
    749 	socklen_t len;
    750 
    751 	len = sizeof(l2o);
    752 	memset(&l2o, 0, len);
    753 	if (getsockopt(sock, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &len) < 0) {
    754 		ERROR_FAILED(err, "getsockopt(L2CAP_OPTIONS)", errno);
    755 		return FALSE;
    756 	}
    757 
    758 	if (!get_peers(sock, (struct sockaddr *) &src,
    759 				(struct sockaddr *) &dst, sizeof(src), err))
    760 		return FALSE;
    761 
    762 	while (opt != BT_IO_OPT_INVALID) {
    763 		switch (opt) {
    764 		case BT_IO_OPT_SOURCE:
    765 			ba2str(&src.l2_bdaddr, va_arg(args, char *));
    766 			break;
    767 		case BT_IO_OPT_SOURCE_BDADDR:
    768 			bacpy(va_arg(args, bdaddr_t *), &src.l2_bdaddr);
    769 			break;
    770 		case BT_IO_OPT_DEST:
    771 			ba2str(&dst.l2_bdaddr, va_arg(args, char *));
    772 			break;
    773 		case BT_IO_OPT_DEST_BDADDR:
    774 			bacpy(va_arg(args, bdaddr_t *), &dst.l2_bdaddr);
    775 			break;
    776 		case BT_IO_OPT_DEFER_TIMEOUT:
    777 			len = sizeof(int);
    778 			if (getsockopt(sock, SOL_BLUETOOTH, BT_DEFER_SETUP,
    779 					va_arg(args, int *), &len) < 0) {
    780 				ERROR_FAILED(err, "getsockopt(DEFER_SETUP)",
    781 									errno);
    782 				return FALSE;
    783 			}
    784 			break;
    785 		case BT_IO_OPT_SEC_LEVEL:
    786 			if (!get_sec_level(sock, BT_IO_L2CAP,
    787 						va_arg(args, int *), err))
    788 				return FALSE;
    789 			break;
    790 		case BT_IO_OPT_PSM:
    791 			*(va_arg(args, uint16_t *)) = src.l2_psm ?
    792 						src.l2_psm : dst.l2_psm;
    793 			break;
    794 		case BT_IO_OPT_OMTU:
    795 			*(va_arg(args, uint16_t *)) = l2o.omtu;
    796 			break;
    797 		case BT_IO_OPT_IMTU:
    798 			*(va_arg(args, uint16_t *)) = l2o.imtu;
    799 			break;
    800 		case BT_IO_OPT_MASTER:
    801 			len = sizeof(flags);
    802 			if (getsockopt(sock, SOL_L2CAP, L2CAP_LM, &flags,
    803 								&len) < 0) {
    804 				ERROR_FAILED(err, "getsockopt(L2CAP_LM)",
    805 									errno);
    806 				return FALSE;
    807 			}
    808 			*(va_arg(args, gboolean *)) =
    809 				(flags & L2CAP_LM_MASTER) ? TRUE : FALSE;
    810 			break;
    811 		case BT_IO_OPT_HANDLE:
    812 			if (l2cap_get_info(sock, &handle, dev_class) < 0) {
    813 				ERROR_FAILED(err, "L2CAP_CONNINFO", errno);
    814 				return FALSE;
    815 			}
    816 			*(va_arg(args, uint16_t *)) = handle;
    817 			break;
    818 		case BT_IO_OPT_CLASS:
    819 			if (l2cap_get_info(sock, &handle, dev_class) < 0) {
    820 				ERROR_FAILED(err, "L2CAP_CONNINFO", errno);
    821 				return FALSE;
    822 			}
    823 			memcpy(va_arg(args, uint8_t *), dev_class, 3);
    824 			break;
    825 		default:
    826 			g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
    827 					"Unknown option %d", opt);
    828 			return FALSE;
    829 		}
    830 
    831 		opt = va_arg(args, int);
    832 	}
    833 
    834 	return TRUE;
    835 }
    836 
    837 static int rfcomm_get_info(int sock, uint16_t *handle, uint8_t *dev_class)
    838 {
    839 	struct rfcomm_conninfo info;
    840 	socklen_t len;
    841 
    842 	len = sizeof(info);
    843 	if (getsockopt(sock, SOL_RFCOMM, RFCOMM_CONNINFO, &info, &len) < 0)
    844 		return -errno;
    845 
    846 	if (handle)
    847 		*handle = info.hci_handle;
    848 
    849 	if (dev_class)
    850 		memcpy(dev_class, info.dev_class, 3);
    851 
    852 	return 0;
    853 }
    854 
    855 static gboolean rfcomm_get(int sock, GError **err, BtIOOption opt1,
    856 								va_list args)
    857 {
    858 	BtIOOption opt = opt1;
    859 	struct sockaddr_rc src, dst;
    860 	int flags;
    861 	socklen_t len;
    862 	uint8_t dev_class[3];
    863 	uint16_t handle;
    864 
    865 	if (!get_peers(sock, (struct sockaddr *) &src,
    866 				(struct sockaddr *) &dst, sizeof(src), err))
    867 		return FALSE;
    868 
    869 	while (opt != BT_IO_OPT_INVALID) {
    870 		switch (opt) {
    871 		case BT_IO_OPT_SOURCE:
    872 			ba2str(&src.rc_bdaddr, va_arg(args, char *));
    873 			break;
    874 		case BT_IO_OPT_SOURCE_BDADDR:
    875 			bacpy(va_arg(args, bdaddr_t *), &src.rc_bdaddr);
    876 			break;
    877 		case BT_IO_OPT_DEST:
    878 			ba2str(&dst.rc_bdaddr, va_arg(args, char *));
    879 			break;
    880 		case BT_IO_OPT_DEST_BDADDR:
    881 			bacpy(va_arg(args, bdaddr_t *), &dst.rc_bdaddr);
    882 			break;
    883 		case BT_IO_OPT_DEFER_TIMEOUT:
    884 			len = sizeof(int);
    885 			if (getsockopt(sock, SOL_BLUETOOTH, BT_DEFER_SETUP,
    886 					va_arg(args, int *), &len) < 0) {
    887 				ERROR_FAILED(err, "getsockopt(DEFER_SETUP)",
    888 									errno);
    889 				return FALSE;
    890 			}
    891 			break;
    892 		case BT_IO_OPT_SEC_LEVEL:
    893 			if (!get_sec_level(sock, BT_IO_RFCOMM,
    894 						va_arg(args, int *), err))
    895 				return FALSE;
    896 			break;
    897 		case BT_IO_OPT_CHANNEL:
    898 			*(va_arg(args, uint8_t *)) = src.rc_channel ?
    899 					src.rc_channel : dst.rc_channel;
    900 			break;
    901 		case BT_IO_OPT_SOURCE_CHANNEL:
    902 			*(va_arg(args, uint8_t *)) = src.rc_channel;
    903 			break;
    904 		case BT_IO_OPT_DEST_CHANNEL:
    905 			*(va_arg(args, uint8_t *)) = dst.rc_channel;
    906 			break;
    907 		case BT_IO_OPT_MASTER:
    908 			len = sizeof(flags);
    909 			if (getsockopt(sock, SOL_RFCOMM, RFCOMM_LM, &flags,
    910 								&len) < 0) {
    911 				ERROR_FAILED(err, "getsockopt(RFCOMM_LM)",
    912 									errno);
    913 				return FALSE;
    914 			}
    915 			*(va_arg(args, gboolean *)) =
    916 				(flags & RFCOMM_LM_MASTER) ? TRUE : FALSE;
    917 			break;
    918 		case BT_IO_OPT_HANDLE:
    919 			if (rfcomm_get_info(sock, &handle, dev_class) < 0) {
    920 				ERROR_FAILED(err, "RFCOMM_CONNINFO", errno);
    921 				return FALSE;
    922 			}
    923 			*(va_arg(args, uint16_t *)) = handle;
    924 			break;
    925 		case BT_IO_OPT_CLASS:
    926 			if (rfcomm_get_info(sock, &handle, dev_class) < 0) {
    927 				ERROR_FAILED(err, "RFCOMM_CONNINFO", errno);
    928 				return FALSE;
    929 			}
    930 			memcpy(va_arg(args, uint8_t *), dev_class, 3);
    931 			break;
    932 		default:
    933 			g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
    934 					"Unknown option %d", opt);
    935 			return FALSE;
    936 		}
    937 
    938 		opt = va_arg(args, int);
    939 	}
    940 
    941 	return TRUE;
    942 }
    943 
    944 static int sco_get_info(int sock, uint16_t *handle, uint8_t *dev_class)
    945 {
    946 	struct sco_conninfo info;
    947 	socklen_t len;
    948 
    949 	len = sizeof(info);
    950 	if (getsockopt(sock, SOL_SCO, SCO_CONNINFO, &info, &len) < 0)
    951 		return -errno;
    952 
    953 	if (handle)
    954 		*handle = info.hci_handle;
    955 
    956 	if (dev_class)
    957 		memcpy(dev_class, info.dev_class, 3);
    958 
    959 	return 0;
    960 }
    961 
    962 static gboolean sco_get(int sock, GError **err, BtIOOption opt1, va_list args)
    963 {
    964 	BtIOOption opt = opt1;
    965 	struct sockaddr_sco src, dst;
    966 	struct sco_options sco_opt;
    967 	socklen_t len;
    968 	uint8_t dev_class[3];
    969 	uint16_t handle;
    970 
    971 	len = sizeof(sco_opt);
    972 	memset(&sco_opt, 0, len);
    973 	if (getsockopt(sock, SOL_SCO, SCO_OPTIONS, &sco_opt, &len) < 0) {
    974 		ERROR_FAILED(err, "getsockopt(SCO_OPTIONS)", errno);
    975 		return FALSE;
    976 	}
    977 
    978 	if (!get_peers(sock, (struct sockaddr *) &src,
    979 				(struct sockaddr *) &dst, sizeof(src), err))
    980 		return FALSE;
    981 
    982 	while (opt != BT_IO_OPT_INVALID) {
    983 		switch (opt) {
    984 		case BT_IO_OPT_SOURCE:
    985 			ba2str(&src.sco_bdaddr, va_arg(args, char *));
    986 			break;
    987 		case BT_IO_OPT_SOURCE_BDADDR:
    988 			bacpy(va_arg(args, bdaddr_t *), &src.sco_bdaddr);
    989 			break;
    990 		case BT_IO_OPT_DEST:
    991 			ba2str(&dst.sco_bdaddr, va_arg(args, char *));
    992 			break;
    993 		case BT_IO_OPT_DEST_BDADDR:
    994 			bacpy(va_arg(args, bdaddr_t *), &dst.sco_bdaddr);
    995 			break;
    996 		case BT_IO_OPT_MTU:
    997 		case BT_IO_OPT_IMTU:
    998 		case BT_IO_OPT_OMTU:
    999 			*(va_arg(args, uint16_t *)) = sco_opt.mtu;
   1000 			break;
   1001 		case BT_IO_OPT_HANDLE:
   1002 			if (sco_get_info(sock, &handle, dev_class) < 0) {
   1003 				ERROR_FAILED(err, "RFCOMM_CONNINFO", errno);
   1004 				return FALSE;
   1005 			}
   1006 			*(va_arg(args, uint16_t *)) = handle;
   1007 			break;
   1008 		case BT_IO_OPT_CLASS:
   1009 			if (sco_get_info(sock, &handle, dev_class) < 0) {
   1010 				ERROR_FAILED(err, "RFCOMM_CONNINFO", errno);
   1011 				return FALSE;
   1012 			}
   1013 			memcpy(va_arg(args, uint8_t *), dev_class, 3);
   1014 			break;
   1015 		default:
   1016 			g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
   1017 					"Unknown option %d", opt);
   1018 			return FALSE;
   1019 		}
   1020 
   1021 		opt = va_arg(args, int);
   1022 	}
   1023 
   1024 	return TRUE;
   1025 }
   1026 
   1027 static gboolean get_valist(GIOChannel *io, BtIOType type, GError **err,
   1028 						BtIOOption opt1, va_list args)
   1029 {
   1030 	int sock;
   1031 
   1032 	sock = g_io_channel_unix_get_fd(io);
   1033 
   1034 	switch (type) {
   1035 	case BT_IO_L2RAW:
   1036 	case BT_IO_L2CAP:
   1037 		return l2cap_get(sock, err, opt1, args);
   1038 	case BT_IO_RFCOMM:
   1039 		return rfcomm_get(sock, err, opt1, args);
   1040 	case BT_IO_SCO:
   1041 		return sco_get(sock, err, opt1, args);
   1042 	}
   1043 
   1044 	g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
   1045 			"Unknown BtIO type %d", type);
   1046 	return FALSE;
   1047 }
   1048 
   1049 gboolean bt_io_accept(GIOChannel *io, BtIOConnect connect, gpointer user_data,
   1050 					GDestroyNotify destroy, GError **err)
   1051 {
   1052 	int sock;
   1053 	char c;
   1054 	struct pollfd pfd;
   1055 
   1056 	sock = g_io_channel_unix_get_fd(io);
   1057 
   1058 	memset(&pfd, 0, sizeof(pfd));
   1059 	pfd.fd = sock;
   1060 	pfd.events = POLLOUT;
   1061 
   1062 	if (poll(&pfd, 1, 0) < 0) {
   1063 		ERROR_FAILED(err, "poll", errno);
   1064 		return FALSE;
   1065 	}
   1066 
   1067 	if (!(pfd.revents & POLLOUT)) {
   1068 		int ret;
   1069 		ret = read(sock, &c, 1);
   1070 	}
   1071 
   1072 	accept_add(io, connect, user_data, destroy);
   1073 
   1074 	return TRUE;
   1075 }
   1076 
   1077 gboolean bt_io_set(GIOChannel *io, BtIOType type, GError **err,
   1078 							BtIOOption opt1, ...)
   1079 {
   1080 	va_list args;
   1081 	gboolean ret;
   1082 	struct set_opts opts;
   1083 	int sock;
   1084 
   1085 	va_start(args, opt1);
   1086 	ret = parse_set_opts(&opts, err, opt1, args);
   1087 	va_end(args);
   1088 
   1089 	if (!ret)
   1090 		return ret;
   1091 
   1092 	sock = g_io_channel_unix_get_fd(io);
   1093 
   1094 	switch (type) {
   1095 	case BT_IO_L2RAW:
   1096 	case BT_IO_L2CAP:
   1097 		return l2cap_set(sock, opts.sec_level, opts.imtu, opts.omtu,
   1098 						opts.mode, opts.master, err);
   1099 	case BT_IO_RFCOMM:
   1100 		return rfcomm_set(sock, opts.sec_level, opts.master, err);
   1101 	case BT_IO_SCO:
   1102 		return sco_set(sock, opts.mtu, err);
   1103 	}
   1104 
   1105 	g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
   1106 			"Unknown BtIO type %d", type);
   1107 	return FALSE;
   1108 }
   1109 
   1110 gboolean bt_io_get(GIOChannel *io, BtIOType type, GError **err,
   1111 							BtIOOption opt1, ...)
   1112 {
   1113 	va_list args;
   1114 	gboolean ret;
   1115 
   1116 	va_start(args, opt1);
   1117 	ret = get_valist(io, type, err, opt1, args);
   1118 	va_end(args);
   1119 
   1120 	return ret;
   1121 }
   1122 
   1123 static GIOChannel *create_io(BtIOType type, gboolean server,
   1124 					struct set_opts *opts, GError **err)
   1125 {
   1126 	int sock;
   1127 	GIOChannel *io;
   1128 
   1129 	switch (type) {
   1130 	case BT_IO_L2RAW:
   1131 		sock = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_L2CAP);
   1132 		if (sock < 0) {
   1133 			ERROR_FAILED(err, "socket(RAW, L2CAP)", errno);
   1134 			return NULL;
   1135 		}
   1136 		if (l2cap_bind(sock, &opts->src,
   1137 					server ? opts->psm : 0, err) < 0)
   1138 			goto failed;
   1139 		if (!l2cap_set(sock, opts->sec_level, 0, 0, 0, -1, err))
   1140 			goto failed;
   1141 		break;
   1142 	case BT_IO_L2CAP:
   1143 		sock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
   1144 		if (sock < 0) {
   1145 			ERROR_FAILED(err, "socket(SEQPACKET, L2CAP)", errno);
   1146 			return NULL;
   1147 		}
   1148 		if (l2cap_bind(sock, &opts->src,
   1149 					server ? opts->psm : 0, err) < 0)
   1150 			goto failed;
   1151 		if (!l2cap_set(sock, opts->sec_level, opts->imtu, opts->omtu,
   1152 						opts->mode, opts->master, err))
   1153 			goto failed;
   1154 		break;
   1155 	case BT_IO_RFCOMM:
   1156 		sock = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
   1157 		if (sock < 0) {
   1158 			ERROR_FAILED(err, "socket(STREAM, RFCOMM)", errno);
   1159 			return NULL;
   1160 		}
   1161 		if (rfcomm_bind(sock, &opts->src,
   1162 					server ? opts->channel : 0, err) < 0)
   1163 			goto failed;
   1164 		if (!rfcomm_set(sock, opts->sec_level, opts->master, err))
   1165 			goto failed;
   1166 		break;
   1167 	case BT_IO_SCO:
   1168 		sock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO);
   1169 		if (sock < 0) {
   1170 			ERROR_FAILED(err, "socket(SEQPACKET, SCO)", errno);
   1171 			return NULL;
   1172 		}
   1173 		if (sco_bind(sock, &opts->src, err) < 0)
   1174 			goto failed;
   1175 		if (!sco_set(sock, opts->mtu, err))
   1176 			goto failed;
   1177 		break;
   1178 	default:
   1179 		g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
   1180 				"Unknown BtIO type %d", type);
   1181 		return NULL;
   1182 	}
   1183 
   1184 	io = g_io_channel_unix_new(sock);
   1185 
   1186 	g_io_channel_set_close_on_unref(io, TRUE);
   1187 	g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
   1188 
   1189 	return io;
   1190 
   1191 failed:
   1192 	close(sock);
   1193 
   1194 	return NULL;
   1195 }
   1196 
   1197 GIOChannel *bt_io_connect(BtIOType type, BtIOConnect connect,
   1198 				gpointer user_data, GDestroyNotify destroy,
   1199 				GError **gerr, BtIOOption opt1, ...)
   1200 {
   1201 	GIOChannel *io;
   1202 	va_list args;
   1203 	struct set_opts opts;
   1204 	int err, sock;
   1205 	gboolean ret;
   1206 
   1207 	va_start(args, opt1);
   1208 	ret = parse_set_opts(&opts, gerr, opt1, args);
   1209 	va_end(args);
   1210 
   1211 	if (ret == FALSE)
   1212 		return NULL;
   1213 
   1214 	io = create_io(type, FALSE, &opts, gerr);
   1215 	if (io == NULL)
   1216 		return NULL;
   1217 
   1218 	sock = g_io_channel_unix_get_fd(io);
   1219 
   1220 	switch (type) {
   1221 	case BT_IO_L2RAW:
   1222 		err = l2cap_connect(sock, &opts.dst, 0);
   1223 		break;
   1224 	case BT_IO_L2CAP:
   1225 		err = l2cap_connect(sock, &opts.dst, opts.psm);
   1226 		break;
   1227 	case BT_IO_RFCOMM:
   1228 		err = rfcomm_connect(sock, &opts.dst, opts.channel);
   1229 		break;
   1230 	case BT_IO_SCO:
   1231 		err = sco_connect(sock, &opts.dst);
   1232 		break;
   1233 	default:
   1234 		g_set_error(gerr, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
   1235 						"Unknown BtIO type %d", type);
   1236 		return NULL;
   1237 	}
   1238 
   1239 	if (err < 0) {
   1240 		g_set_error(gerr, BT_IO_ERROR, BT_IO_ERROR_CONNECT_FAILED,
   1241 				"connect: %s (%d)", strerror(-err), -err);
   1242 		g_io_channel_unref(io);
   1243 		return NULL;
   1244 	}
   1245 
   1246 	connect_add(io, connect, user_data, destroy);
   1247 
   1248 	return io;
   1249 }
   1250 
   1251 GIOChannel *bt_io_listen(BtIOType type, BtIOConnect connect,
   1252 				BtIOConfirm confirm, gpointer user_data,
   1253 				GDestroyNotify destroy, GError **err,
   1254 				BtIOOption opt1, ...)
   1255 {
   1256 	GIOChannel *io;
   1257 	va_list args;
   1258 	struct set_opts opts;
   1259 	int sock;
   1260 	gboolean ret;
   1261 
   1262 	if (type == BT_IO_L2RAW) {
   1263 		g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
   1264 				"Server L2CAP RAW sockets not supported");
   1265 		return NULL;
   1266 	}
   1267 
   1268 	va_start(args, opt1);
   1269 	ret = parse_set_opts(&opts, err, opt1, args);
   1270 	va_end(args);
   1271 
   1272 	if (ret == FALSE)
   1273 		return NULL;
   1274 
   1275 	io = create_io(type, TRUE, &opts, err);
   1276 	if (io == NULL)
   1277 		return NULL;
   1278 
   1279 	sock = g_io_channel_unix_get_fd(io);
   1280 
   1281 	if (confirm)
   1282 		setsockopt(sock, SOL_BLUETOOTH, BT_DEFER_SETUP, &opts.defer,
   1283 							sizeof(opts.defer));
   1284 
   1285 	if (listen(sock, 5) < 0) {
   1286 		ERROR_FAILED(err, "listen", errno);
   1287 		g_io_channel_unref(io);
   1288 		return NULL;
   1289 	}
   1290 
   1291 	server_add(io, connect, confirm, user_data, destroy);
   1292 
   1293 	return io;
   1294 }
   1295 
   1296 GQuark bt_io_error_quark(void)
   1297 {
   1298 	return g_quark_from_static_string("bt-io-error-quark");
   1299 }
   1300 
   1301