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