1 /* $NetBSD: rfcomm_sppd.c,v 1.13 2010/11/03 08:27:27 plunky Exp $ */ 2 3 /*- 4 * Copyright (c) 2006 Itronix Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of Itronix Inc. may not be used to endorse 16 * or promote products derived from this software without specific 17 * prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY 23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26 * ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 /* 32 * Copyright (c) 2009 The NetBSD Foundation, Inc. 33 * Copyright (c) 2007 Iain Hibbert 34 * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com> 35 * All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 49 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 56 * SUCH DAMAGE. 57 */ 58 59 #include <sys/cdefs.h> 60 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc.\ 61 Copyright (c) 2007 Iain Hibbert.\ 62 Copyright (c) 2006 Itronix, Inc.\ 63 Copyright (c) 2003 Maksim Yevmenkin m_evmenkin@yahoo.com.\ 64 All rights reserved."); 65 __RCSID("$NetBSD: rfcomm_sppd.c,v 1.13 2010/11/03 08:27:27 plunky Exp $"); 66 67 #include <sys/param.h> 68 69 #include <bluetooth.h> 70 #include <ctype.h> 71 #include <err.h> 72 #include <errno.h> 73 #include <fcntl.h> 74 #include <grp.h> 75 #include <limits.h> 76 #include <paths.h> 77 #include <sdp.h> 78 #include <signal.h> 79 #include <stdarg.h> 80 #include <stdio.h> 81 #include <stdlib.h> 82 #include <string.h> 83 #include <syslog.h> 84 #include <termios.h> 85 #include <unistd.h> 86 87 #include <netbt/rfcomm.h> 88 89 int open_tty(const char *); 90 int open_client(bdaddr_t *, bdaddr_t *, int, uintmax_t, const char *); 91 int open_server(bdaddr_t *, uint16_t, uint8_t, int, const char *); 92 void copy_data(int, int); 93 int service_search(const bdaddr_t *, const bdaddr_t *, uint16_t, uintmax_t *, uintmax_t *); 94 void sighandler(int); 95 void usage(void); 96 void reset_tio(void); 97 98 int done; /* got a signal */ 99 struct termios tio; /* stored termios for reset on exit */ 100 101 struct service { 102 const char * name; 103 const char * description; 104 uint16_t class; 105 } services[] = { 106 { "DUN", "Dialup Networking", 107 SDP_SERVICE_CLASS_DIALUP_NETWORKING }, 108 { "LAN", "LAN access using PPP", 109 SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP }, 110 { "SP", "Serial Port", 111 SDP_SERVICE_CLASS_SERIAL_PORT }, 112 { NULL, NULL, 0 } 113 }; 114 115 int 116 main(int argc, char *argv[]) 117 { 118 struct termios t; 119 bdaddr_t laddr, raddr; 120 fd_set rdset; 121 const char *service; 122 char *ep, *tty; 123 int lm, n, rfcomm, tty_in, tty_out; 124 uint16_t psm; 125 uint8_t channel; 126 127 bdaddr_copy(&laddr, BDADDR_ANY); 128 bdaddr_copy(&raddr, BDADDR_ANY); 129 service = "SP"; 130 tty = NULL; 131 channel = RFCOMM_CHANNEL_ANY; 132 psm = L2CAP_PSM_RFCOMM; 133 lm = 0; 134 135 /* Parse command line options */ 136 while ((n = getopt(argc, argv, "a:c:d:hm:p:s:t:")) != -1) { 137 switch (n) { 138 case 'a': /* remote device address */ 139 if (!bt_aton(optarg, &raddr)) { 140 struct hostent *he = NULL; 141 142 if ((he = bt_gethostbyname(optarg)) == NULL) 143 errx(EXIT_FAILURE, "%s: %s", optarg, 144 hstrerror(h_errno)); 145 146 bdaddr_copy(&raddr, (bdaddr_t *)he->h_addr); 147 } 148 break; 149 150 case 'c': /* RFCOMM channel */ 151 channel = strtoul(optarg, &ep, 10); 152 if (*ep != '\0' 153 || channel < RFCOMM_CHANNEL_MIN 154 || channel > RFCOMM_CHANNEL_MAX) 155 errx(EXIT_FAILURE, "Invalid channel: %s", optarg); 156 157 break; 158 159 case 'd': /* local device address */ 160 if (!bt_devaddr(optarg, &laddr)) 161 err(EXIT_FAILURE, "%s", optarg); 162 163 break; 164 165 case 'm': /* Link Mode */ 166 if (strcasecmp(optarg, "auth") == 0) 167 lm = RFCOMM_LM_AUTH; 168 else if (strcasecmp(optarg, "encrypt") == 0) 169 lm = RFCOMM_LM_ENCRYPT; 170 else if (strcasecmp(optarg, "secure") == 0) 171 lm = RFCOMM_LM_SECURE; 172 else 173 errx(EXIT_FAILURE, "%s: unknown mode", optarg); 174 175 break; 176 177 case 'p': /* PSM */ 178 psm = strtoul(optarg, &ep, 0); 179 if (*ep != '\0' || L2CAP_PSM_INVALID(psm)) 180 errx(EXIT_FAILURE, "Invalid PSM: %s", optarg); 181 182 break; 183 184 case 's': /* service class */ 185 service = optarg; 186 break; 187 188 case 't': /* Slave TTY name */ 189 if (optarg[0] != '/') 190 asprintf(&tty, "%s%s", _PATH_DEV, optarg); 191 else 192 tty = optarg; 193 194 break; 195 196 case 'h': 197 default: 198 usage(); 199 /* NOT REACHED */ 200 } 201 } 202 203 /* 204 * validate options: 205 * cannot have remote address if channel was given 206 */ 207 if (channel != RFCOMM_CHANNEL_ANY && !bdaddr_any(&raddr)) 208 usage(); 209 210 /* 211 * grab ttys before we start the bluetooth 212 */ 213 if (tty == NULL) { 214 tty_in = STDIN_FILENO; 215 tty_out = STDOUT_FILENO; 216 } else { 217 tty_in = open_tty(tty); 218 tty_out = tty_in; 219 } 220 221 /* open RFCOMM */ 222 if (!bdaddr_any(&raddr)) 223 rfcomm = open_client(&laddr, &raddr, lm, psm, service); 224 else 225 rfcomm = open_server(&laddr, psm, channel, lm, service); 226 227 /* 228 * now we are ready to go, so either detach or maybe turn 229 * off some input processing, so that rfcomm_sppd can 230 * be used directly with stdio 231 */ 232 if (tty == NULL) { 233 if (tcgetattr(tty_in, &t) < 0) 234 err(EXIT_FAILURE, "tcgetattr"); 235 236 memcpy(&tio, &t, sizeof(tio)); 237 t.c_lflag &= ~(ECHO | ICANON); 238 t.c_iflag &= ~(ICRNL); 239 240 if (memcmp(&tio, &t, sizeof(tio))) { 241 if (tcsetattr(tty_in, TCSANOW, &t) < 0) 242 err(EXIT_FAILURE, "tcsetattr"); 243 244 atexit(reset_tio); 245 } 246 } else { 247 if (daemon(0, 0) < 0) 248 err(EXIT_FAILURE, "daemon() failed"); 249 } 250 251 /* catch signals */ 252 done = 0; 253 (void)signal(SIGHUP, sighandler); 254 (void)signal(SIGINT, sighandler); 255 (void)signal(SIGPIPE, sighandler); 256 (void)signal(SIGTERM, sighandler); 257 258 openlog(getprogname(), LOG_PERROR | LOG_PID, LOG_DAEMON); 259 syslog(LOG_INFO, "Starting on %s...", (tty ? tty : "stdio")); 260 261 n = MAX(tty_in, rfcomm) + 1; 262 while (!done) { 263 FD_ZERO(&rdset); 264 FD_SET(tty_in, &rdset); 265 FD_SET(rfcomm, &rdset); 266 267 if (select(n, &rdset, NULL, NULL, NULL) < 0) { 268 if (errno == EINTR) 269 continue; 270 271 syslog(LOG_ERR, "select error: %m"); 272 exit(EXIT_FAILURE); 273 } 274 275 if (FD_ISSET(tty_in, &rdset)) 276 copy_data(tty_in, rfcomm); 277 278 if (FD_ISSET(rfcomm, &rdset)) 279 copy_data(rfcomm, tty_out); 280 } 281 282 syslog(LOG_INFO, "Completed on %s", (tty ? tty : "stdio")); 283 exit(EXIT_SUCCESS); 284 } 285 286 int 287 open_tty(const char *tty) 288 { 289 char pty[PATH_MAX], *slash; 290 struct group *gr = NULL; 291 gid_t ttygid; 292 int master; 293 294 /* 295 * Construct master PTY name. The slave tty name must be less then 296 * PATH_MAX characters in length, must contain '/' character and 297 * must not end with '/'. 298 */ 299 if (strlen(tty) >= sizeof(pty)) 300 errx(EXIT_FAILURE, ": tty name too long"); 301 302 strlcpy(pty, tty, sizeof(pty)); 303 slash = strrchr(pty, '/'); 304 if (slash == NULL || slash[1] == '\0') 305 errx(EXIT_FAILURE, "%s: invalid tty", tty); 306 307 slash[1] = 'p'; 308 if (strcmp(pty, tty) == 0) 309 errx(EXIT_FAILURE, "Master and slave tty are the same (%s)", tty); 310 311 if ((master = open(pty, O_RDWR, 0)) < 0) 312 err(EXIT_FAILURE, "%s", pty); 313 314 /* 315 * Slave TTY 316 */ 317 318 if ((gr = getgrnam("tty")) != NULL) 319 ttygid = gr->gr_gid; 320 else 321 ttygid = (gid_t)-1; 322 323 (void)chown(tty, getuid(), ttygid); 324 (void)chmod(tty, S_IRUSR | S_IWUSR | S_IWGRP); 325 (void)revoke(tty); 326 327 return master; 328 } 329 330 int 331 open_client(bdaddr_t *laddr, bdaddr_t *raddr, int lm, uintmax_t psm, const char *service) 332 { 333 struct sockaddr_bt sa; 334 struct service *s; 335 struct linger l; 336 char *ep; 337 int fd, error; 338 uintmax_t channel; 339 340 for (s = services ; ; s++) { 341 if (s->name == NULL) { 342 channel = strtoul(service, &ep, 10); 343 if (*ep != '\0') 344 errx(EXIT_FAILURE, "Unknown service: %s", service); 345 346 break; 347 } 348 349 if (strcasecmp(s->name, service) == 0) { 350 error = service_search(laddr, raddr, s->class, &psm, &channel); 351 if (error != 0) 352 errx(EXIT_FAILURE, "%s: %s", s->name, strerror(error)); 353 354 break; 355 } 356 } 357 358 if (channel < RFCOMM_CHANNEL_MIN || channel > RFCOMM_CHANNEL_MAX) 359 errx(EXIT_FAILURE, "Invalid channel %"PRIuMAX, channel); 360 361 if (L2CAP_PSM_INVALID(psm)) 362 errx(EXIT_FAILURE, "Invalid PSM 0x%04"PRIxMAX, psm); 363 364 memset(&sa, 0, sizeof(sa)); 365 sa.bt_len = sizeof(sa); 366 sa.bt_family = AF_BLUETOOTH; 367 bdaddr_copy(&sa.bt_bdaddr, laddr); 368 369 fd = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); 370 if (fd < 0) 371 err(EXIT_FAILURE, "socket()"); 372 373 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) 374 err(EXIT_FAILURE, "bind(%s)", bt_ntoa(laddr, NULL)); 375 376 memset(&l, 0, sizeof(l)); 377 l.l_onoff = 1; 378 l.l_linger = 5; 379 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) < 0) 380 err(EXIT_FAILURE, "linger()"); 381 382 if (setsockopt(fd, BTPROTO_RFCOMM, SO_RFCOMM_LM, &lm, sizeof(lm)) < 0) 383 err(EXIT_FAILURE, "link mode"); 384 385 sa.bt_psm = psm; 386 sa.bt_channel = channel; 387 bdaddr_copy(&sa.bt_bdaddr, raddr); 388 389 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) 390 err(EXIT_FAILURE, "connect(%s, 0x%04"PRIxMAX", %"PRIuMAX")", 391 bt_ntoa(raddr, NULL), psm, channel); 392 393 return fd; 394 } 395 396 int 397 open_server(bdaddr_t *laddr, uint16_t psm, uint8_t channel, int lm, const char *service) 398 { 399 uint8_t buffer[256]; 400 struct sockaddr_bt sa; 401 struct service *s; 402 struct linger l; 403 socklen_t len; 404 sdp_session_t ss; 405 sdp_data_t rec; 406 int sv, fd; 407 408 for (s = services; ; s++) { 409 if (s->name == NULL) 410 usage(); 411 412 if (strcasecmp(s->name, service) == 0) 413 break; 414 } 415 416 /* Open server socket */ 417 sv = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); 418 if (sv < 0) 419 err(EXIT_FAILURE, "socket()"); 420 421 memset(&sa, 0, sizeof(sa)); 422 sa.bt_len = sizeof(sa); 423 sa.bt_family = AF_BLUETOOTH; 424 sa.bt_psm = psm; 425 sa.bt_channel = channel; 426 bdaddr_copy(&sa.bt_bdaddr, laddr); 427 if (bind(sv, (struct sockaddr *)&sa, sizeof(sa)) < 0) 428 err(EXIT_FAILURE, "bind(%s, 0x%04x, %d)", 429 bt_ntoa(laddr, NULL), psm, channel); 430 431 if (setsockopt(sv, BTPROTO_RFCOMM, SO_RFCOMM_LM, &lm, sizeof(lm)) < 0) 432 err(EXIT_FAILURE, "link mode"); 433 434 if (listen(sv, 1) < 0) 435 err(EXIT_FAILURE, "listen()"); 436 437 len = sizeof(sa); 438 if (getsockname(sv, (struct sockaddr *)&sa, &len) < 0) 439 err(EXIT_FAILURE, "getsockname()"); 440 if (len != sizeof(sa)) 441 errx(EXIT_FAILURE, "getsockname()"); 442 443 /* Build SDP record */ 444 rec.next = buffer; 445 rec.end = buffer + sizeof(buffer); 446 447 sdp_put_uint16(&rec, SDP_ATTR_SERVICE_RECORD_HANDLE); 448 sdp_put_uint32(&rec, 0x00000000); 449 450 sdp_put_uint16(&rec, SDP_ATTR_SERVICE_CLASS_ID_LIST); 451 sdp_put_seq(&rec, 3); 452 sdp_put_uuid16(&rec, s->class); 453 454 len = (psm == L2CAP_PSM_RFCOMM ? 0 : 3); 455 456 sdp_put_uint16(&rec, SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST); 457 sdp_put_seq(&rec, 12 + len); 458 sdp_put_seq(&rec, 3 + len); 459 sdp_put_uuid16(&rec, SDP_UUID_PROTOCOL_L2CAP); 460 if (len > 0) 461 sdp_put_uint16(&rec, psm); 462 sdp_put_seq(&rec, 5); 463 sdp_put_uuid16(&rec, SDP_UUID_PROTOCOL_RFCOMM); 464 sdp_put_uint8(&rec, sa.bt_channel); 465 466 sdp_put_uint16(&rec, SDP_ATTR_BROWSE_GROUP_LIST); 467 sdp_put_seq(&rec, 3); 468 sdp_put_uuid16(&rec, SDP_SERVICE_CLASS_PUBLIC_BROWSE_GROUP); 469 470 sdp_put_uint16(&rec, SDP_ATTR_LANGUAGE_BASE_ATTRIBUTE_ID_LIST); 471 sdp_put_seq(&rec, 9); 472 sdp_put_uint16(&rec, 0x656e); /* "en" */ 473 sdp_put_uint16(&rec, 106); /* UTF-8 */ 474 sdp_put_uint16(&rec, SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID); 475 476 if (s->class == SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP) { 477 sdp_put_uint16(&rec, SDP_ATTR_SERVICE_AVAILABILITY); 478 sdp_put_uint8(&rec, 0x00); 479 } 480 481 sdp_put_uint16(&rec, SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); 482 sdp_put_seq(&rec, 8); 483 sdp_put_seq(&rec, 6); 484 sdp_put_uuid16(&rec, s->class); 485 sdp_put_uint16(&rec, 0x0100); /* v1.0 */ 486 487 sdp_put_uint16(&rec, SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID 488 + SDP_ATTR_SERVICE_NAME_OFFSET); 489 sdp_put_str(&rec, s->description, -1); 490 491 if (s->class == SDP_SERVICE_CLASS_DIALUP_NETWORKING) { 492 sdp_put_uint16(&rec, SDP_ATTR_AUDIO_FEEDBACK_SUPPORT); 493 sdp_put_bool(&rec, false); 494 } 495 496 #if 0 497 if (s->class == SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP) { 498 sdp_put_uint16(&rec, SDP_ATTR_IP_SUBNET); /* TODO */ 499 sdp_put_str(&rec, "0.0.0.0/0", -1); 500 } 501 #endif 502 503 rec.end = rec.next; 504 rec.next = buffer; 505 506 /* Register service with SDP server */ 507 ss = sdp_open_local(NULL); 508 if (ss == NULL) 509 err(EXIT_FAILURE, "sdp_open_local"); 510 511 if (!sdp_record_insert(ss, laddr, NULL, &rec)) 512 err(EXIT_FAILURE, "sdp_record_insert"); 513 514 /* Accept client connection */ 515 len = sizeof(sa); 516 fd = accept(sv, (struct sockaddr *)&sa, &len); 517 if (fd < 0) 518 err(EXIT_FAILURE, "accept"); 519 520 memset(&l, 0, sizeof(l)); 521 l.l_onoff = 1; 522 l.l_linger = 5; 523 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) < 0) 524 err(EXIT_FAILURE, "linger()"); 525 526 close(sv); 527 return fd; 528 } 529 530 void 531 copy_data(int src, int dst) 532 { 533 static char buf[BUFSIZ]; 534 ssize_t nr, nw, off; 535 536 while ((nr = read(src, buf, sizeof(buf))) == -1) { 537 if (errno != EINTR) { 538 syslog(LOG_ERR, "read failed: %m"); 539 exit(EXIT_FAILURE); 540 } 541 } 542 543 if (nr == 0) /* reached EOF */ 544 done++; 545 546 for (off = 0 ; nr ; nr -= nw, off += nw) { 547 if ((nw = write(dst, buf + off, (size_t)nr)) == -1) { 548 syslog(LOG_ERR, "write failed: %m"); 549 exit(EXIT_FAILURE); 550 } 551 } 552 } 553 554 int 555 service_search(bdaddr_t const *laddr, bdaddr_t const *raddr, 556 uint16_t class, uintmax_t *psm, uintmax_t *channel) 557 { 558 uint8_t buffer[6]; /* SSP (3 bytes) + AIL (3 bytes) */ 559 sdp_session_t ss; 560 sdp_data_t ail, ssp, rsp, rec, value, pdl, seq; 561 uint16_t attr; 562 bool rv; 563 564 seq.next = buffer; 565 seq.end = buffer + sizeof(buffer); 566 567 /* 568 * build ServiceSearchPattern (3 bytes) 569 */ 570 ssp.next = seq.next; 571 sdp_put_uuid16(&seq, class); 572 ssp.end = seq.next; 573 574 /* 575 * build AttributeIDList (3 bytes) 576 */ 577 ail.next = seq.next; 578 sdp_put_uint16(&seq, SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST); 579 ail.end = seq.next; 580 581 ss = sdp_open(laddr, raddr); 582 if (ss == NULL) 583 return errno; 584 585 rv = sdp_service_search_attribute(ss, &ssp, &ail, &rsp); 586 if (!rv) { 587 sdp_close(ss); 588 return errno; 589 } 590 591 /* 592 * The response will be a list of records that matched our 593 * ServiceSearchPattern, where each record is a sequence 594 * containing a single ProtocolDescriptorList attribute and 595 * value 596 * 597 * seq 598 * uint16 ProtocolDescriptorList 599 * value 600 * seq 601 * uint16 ProtocolDescriptorList 602 * value 603 * 604 * If the ProtocolDescriptorList describes a single stack, 605 * the attribute value takes the form of a single Data Element 606 * Sequence where each member is a protocol descriptor. 607 * 608 * seq 609 * list 610 * 611 * If it is possible for more than one kind of protocol 612 * stack to be used to gain access to the service, the 613 * ProtocolDescriptorList takes the form of a Data Element 614 * Alternative where each member is a Data Element Sequence 615 * describing an alternative protocol stack. 616 * 617 * alt 618 * seq 619 * list 620 * seq 621 * list 622 * 623 * Each protocol stack description contains a sequence for each 624 * protocol, where each sequence contains the protocol UUID as 625 * the first element, and any ProtocolSpecificParameters. We are 626 * interested in the L2CAP psm if provided, and the RFCOMM channel 627 * number, stored as parameter#1 in each case. 628 * 629 * seq 630 * uuid L2CAP 631 * uint16 psm 632 * seq 633 * uuid RFCOMM 634 * uint8 channel 635 */ 636 637 rv = false; 638 while (!rv && sdp_get_seq(&rsp, &rec)) { 639 if (!sdp_get_attr(&rec, &attr, &value) 640 || attr != SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST) 641 continue; 642 643 sdp_get_alt(&value, &value); /* strip any alt container */ 644 while (!rv && sdp_get_seq(&value, &pdl)) { 645 *psm = L2CAP_PSM_RFCOMM; 646 if (sdp_get_seq(&pdl, &seq) 647 && sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_L2CAP) 648 && (sdp_get_uint(&seq, psm) || true) 649 && sdp_get_seq(&pdl, &seq) 650 && sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_RFCOMM) 651 && sdp_get_uint(&seq, channel)) 652 rv = true; 653 } 654 } 655 656 sdp_close(ss); 657 return (rv) ? 0 : ENOATTR; 658 } 659 660 void 661 sighandler(int s) 662 { 663 664 done++; 665 } 666 667 void 668 reset_tio(void) 669 { 670 671 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio); 672 } 673 674 void 675 usage(void) 676 { 677 const char *cmd = getprogname(); 678 struct service *s; 679 680 fprintf(stderr, "Usage: %s [-d device] [-m mode] [-p psm] [-s service] [-t tty]\n" 681 " %*s {-a bdaddr | [-c channel]}\n" 682 "\n" 683 "Where:\n" 684 "\t-a bdaddr remote device address\n" 685 "\t-c channel local RFCOMM channel\n" 686 "\t-d device local device address\n" 687 "\t-m mode link mode\n" 688 "\t-p psm protocol/service multiplexer\n" 689 "\t-s service service class\n" 690 "\t-t tty run in background using pty\n" 691 "\n", cmd, (int)strlen(cmd), ""); 692 693 fprintf(stderr, "Known service classes:\n"); 694 for (s = services ; s->name != NULL ; s++) 695 fprintf(stderr, "\t%-13s%s\n", s->name, s->description); 696 697 exit(EXIT_FAILURE); 698 } 699