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