rfcomm_sppd.c revision 172142
1114878Sjulian/* 2114878Sjulian * rfcomm_sppd.c 3114878Sjulian * 4114878Sjulian * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com> 5114878Sjulian * All rights reserved. 6114878Sjulian * 7114878Sjulian * Redistribution and use in source and binary forms, with or without 8114878Sjulian * modification, are permitted provided that the following conditions 9114878Sjulian * are met: 10114878Sjulian * 1. Redistributions of source code must retain the above copyright 11114878Sjulian * notice, this list of conditions and the following disclaimer. 12114878Sjulian * 2. Redistributions in binary form must reproduce the above copyright 13114878Sjulian * notice, this list of conditions and the following disclaimer in the 14114878Sjulian * documentation and/or other materials provided with the distribution. 15114878Sjulian * 16114878Sjulian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17114878Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18114878Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19114878Sjulian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20114878Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21114878Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22114878Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23114878Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24114878Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25114878Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26114878Sjulian * SUCH DAMAGE. 27114878Sjulian * 28121054Semax * $Id: rfcomm_sppd.c,v 1.4 2003/09/07 18:15:55 max Exp $ 29114878Sjulian * $FreeBSD: head/usr.bin/bluetooth/rfcomm_sppd/rfcomm_sppd.c 172142 2007-09-11 01:59:00Z kevlo $ 30114878Sjulian */ 31114878Sjulian 32114878Sjulian#include <sys/stat.h> 33121054Semax#include <bluetooth.h> 34152704Semax#include <ctype.h> 35114878Sjulian#include <err.h> 36114878Sjulian#include <errno.h> 37114878Sjulian#include <fcntl.h> 38114878Sjulian#include <grp.h> 39114878Sjulian#include <limits.h> 40123676Semax#include <paths.h> 41121054Semax#include <sdp.h> 42114878Sjulian#include <signal.h> 43114878Sjulian#include <stdarg.h> 44114878Sjulian#include <stdio.h> 45114878Sjulian#include <stdlib.h> 46114878Sjulian#include <string.h> 47114878Sjulian#include <syslog.h> 48114878Sjulian#include <termios.h> 49114878Sjulian#include <unistd.h> 50114878Sjulian 51114878Sjulian#define SPPD_IDENT "rfcomm_sppd" 52114878Sjulian#define SPPD_BUFFER_SIZE 1024 53114878Sjulian#define max(a, b) (((a) > (b))? (a) : (b)) 54114878Sjulian 55121054Semaxint rfcomm_channel_lookup (bdaddr_t const *local, 56121054Semax bdaddr_t const *remote, 57121054Semax int service, int *channel, int *error); 58121054Semax 59114878Sjulianstatic int sppd_ttys_open (char const *tty, int *amaster, int *aslave); 60114878Sjulianstatic int sppd_read (int fd, char *buffer, int size); 61114878Sjulianstatic int sppd_write (int fd, char *buffer, int size); 62114878Sjulianstatic void sppd_sighandler (int s); 63114878Sjulianstatic void usage (void); 64114878Sjulian 65114878Sjulianstatic int done; /* are we done? */ 66114878Sjulian 67114878Sjulian/* Main */ 68114878Sjulianint 69114878Sjulianmain(int argc, char *argv[]) 70114878Sjulian{ 71114878Sjulian struct sigaction sa; 72114878Sjulian struct sockaddr_rfcomm ra; 73114878Sjulian bdaddr_t addr; 74152704Semax int n, background, channel, service, 75166233Semax s, amaster, aslave, fd, doserver; 76114878Sjulian fd_set rfd; 77152704Semax char *tty = NULL, *ep = NULL, buf[SPPD_BUFFER_SIZE]; 78114878Sjulian 79114878Sjulian memcpy(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)); 80114878Sjulian background = channel = 0; 81152704Semax service = SDP_SERVICE_CLASS_SERIAL_PORT; 82166233Semax doserver = 0; 83114878Sjulian 84114878Sjulian /* Parse command line options */ 85166233Semax while ((n = getopt(argc, argv, "a:bc:t:hS")) != -1) { 86114878Sjulian switch (n) { 87121054Semax case 'a': /* BDADDR */ 88121054Semax if (!bt_aton(optarg, &addr)) { 89121054Semax struct hostent *he = NULL; 90114878Sjulian 91121054Semax if ((he = bt_gethostbyname(optarg)) == NULL) 92121054Semax errx(1, "%s: %s", optarg, hstrerror(h_errno)); 93114878Sjulian 94121054Semax memcpy(&addr, he->h_addr, sizeof(addr)); 95121054Semax } 96121054Semax break; 97114878Sjulian 98114878Sjulian case 'c': /* RFCOMM channel */ 99152704Semax channel = strtoul(optarg, &ep, 10); 100152704Semax if (*ep != '\0') { 101152704Semax channel = 0; 102152704Semax switch (tolower(optarg[0])) { 103152704Semax case 'd': /* DialUp Networking */ 104152704Semax service = SDP_SERVICE_CLASS_DIALUP_NETWORKING; 105152704Semax break; 106152704Semax 107152704Semax case 'f': /* Fax */ 108152704Semax service = SDP_SERVICE_CLASS_FAX; 109152704Semax break; 110152704Semax 111153209Semax case 'l': /* LAN */ 112153209Semax service = SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP; 113153209Semax break; 114153209Semax 115152704Semax case 's': /* Serial Port */ 116152704Semax service = SDP_SERVICE_CLASS_SERIAL_PORT; 117152704Semax break; 118152704Semax 119152704Semax default: 120152704Semax errx(1, "Unknown service name: %s", 121152704Semax optarg); 122152704Semax /* NOT REACHED */ 123152704Semax } 124152704Semax } 125114878Sjulian break; 126114878Sjulian 127114878Sjulian case 'b': /* Run in background */ 128114878Sjulian background = 1; 129114878Sjulian break; 130114878Sjulian 131114878Sjulian case 't': /* Slave TTY name */ 132123676Semax if (optarg[0] != '/') 133123676Semax asprintf(&tty, "%s%s", _PATH_DEV, optarg); 134123676Semax else 135123676Semax tty = optarg; 136114878Sjulian break; 137114878Sjulian 138166233Semax case 'S': 139166233Semax doserver = 1; 140166233Semax break; 141166233Semax 142114878Sjulian case 'h': 143114878Sjulian default: 144114878Sjulian usage(); 145114878Sjulian /* NOT REACHED */ 146114878Sjulian } 147114878Sjulian } 148114878Sjulian 149114878Sjulian /* Check if we have everything we need */ 150166233Semax if (!doserver && memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) == 0) 151114878Sjulian usage(); 152114878Sjulian /* NOT REACHED */ 153114878Sjulian 154114878Sjulian /* Set signal handlers */ 155114878Sjulian memset(&sa, 0, sizeof(sa)); 156114878Sjulian sa.sa_handler = sppd_sighandler; 157114878Sjulian 158114878Sjulian if (sigaction(SIGTERM, &sa, NULL) < 0) 159114878Sjulian err(1, "Could not sigaction(SIGTERM)"); 160114878Sjulian 161114878Sjulian if (sigaction(SIGHUP, &sa, NULL) < 0) 162114878Sjulian err(1, "Could not sigaction(SIGHUP)"); 163114878Sjulian 164114878Sjulian if (sigaction(SIGINT, &sa, NULL) < 0) 165114878Sjulian err(1, "Could not sigaction(SIGINT)"); 166114878Sjulian 167114878Sjulian sa.sa_handler = SIG_IGN; 168114878Sjulian sa.sa_flags = SA_NOCLDWAIT; 169114878Sjulian 170114878Sjulian if (sigaction(SIGCHLD, &sa, NULL) < 0) 171114878Sjulian err(1, "Could not sigaction(SIGCHLD)"); 172114878Sjulian 173114878Sjulian /* Open TTYs */ 174135993Semax if (tty == NULL) { 175166233Semax if (background || doserver) 176135993Semax usage(); 177114878Sjulian 178135993Semax amaster = STDIN_FILENO; 179135993Semax fd = STDOUT_FILENO; 180135993Semax } else { 181135993Semax if (sppd_ttys_open(tty, &amaster, &aslave) < 0) 182135993Semax exit(1); 183135993Semax 184135993Semax fd = amaster; 185135993Semax } 186135993Semax 187114878Sjulian /* Open RFCOMM connection */ 188114878Sjulian 189166233Semax if (doserver) { 190166233Semax struct sockaddr_rfcomm ma; 191166233Semax bdaddr_t bt_addr_any; 192166233Semax sdp_lan_profile_t lan; 193166233Semax void *ss; 194166233Semax uint32_t sdp_handle; 195166233Semax int acceptsock, aaddrlen; 196114878Sjulian 197166233Semax if (channel == 0) { 198166233Semax /* XXX: should check if selected channel is unused */ 199166233Semax channel = (getpid() % 30) + 1; 200166233Semax } 201166233Semax acceptsock = socket(PF_BLUETOOTH, SOCK_STREAM, 202166233Semax BLUETOOTH_PROTO_RFCOMM); 203166233Semax if (acceptsock < 0) 204166233Semax err(1, "Could not create socket"); 205114878Sjulian 206166233Semax memset(&ma, 0, sizeof(ma)); 207166233Semax ma.rfcomm_len = sizeof(ma); 208166233Semax ma.rfcomm_family = AF_BLUETOOTH; 209166233Semax ma.rfcomm_channel = channel; 210114878Sjulian 211166233Semax if (bind(acceptsock, (struct sockaddr *)&ma, sizeof(ma)) < 0) 212166233Semax err(1, "Could not bind socket -- channel %d in use?", 213166233Semax channel); 214172142Skevlo if (listen(acceptsock, 10) != 0) 215172142Skevlo err(1, "Could not listen on socket"); 216114878Sjulian 217166233Semax ss = sdp_open_local(NULL); 218166233Semax if (ss == NULL) 219166233Semax errx(1, "Unable to create local SDP session"); 220166233Semax if (sdp_error(ss) != 0) 221166233Semax errx(1, "Unable to open local SDP session. %s (%d)", 222166233Semax strerror(sdp_error(ss)), sdp_error(ss)); 223166233Semax memset(&lan, 0, sizeof(lan)); 224166233Semax lan.server_channel = channel; 225166233Semax 226166233Semax memcpy(&bt_addr_any, NG_HCI_BDADDR_ANY, sizeof(bt_addr_any)); 227166233Semax if (sdp_register_service(ss, service, &bt_addr_any, 228166233Semax (void *)&lan, sizeof(lan), &sdp_handle) != 0) { 229166233Semax errx(1, "Unable to register LAN service with " 230166233Semax "local SDP daemon. %s (%d)", 231166233Semax strerror(sdp_error(ss)), sdp_error(ss)); 232166233Semax } 233166233Semax 234166233Semax s = -1; 235166233Semax while (s < 0) { 236166233Semax aaddrlen = sizeof(ra); 237166233Semax s = accept(acceptsock, (struct sockaddr *)&ra, 238166233Semax &aaddrlen); 239166233Semax if (s < 0) 240166233Semax err(1, "Unable to accept()"); 241166233Semax if (memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) && 242166233Semax memcmp(&addr, &ra.rfcomm_bdaddr, sizeof(addr))) { 243166233Semax warnx("Connect from wrong client"); 244166233Semax close(s); 245166233Semax s = -1; 246166233Semax } 247166233Semax } 248166233Semax sdp_unregister_service(ss, sdp_handle); 249166233Semax sdp_close(ss); 250166233Semax close(acceptsock); 251166233Semax } else { 252166233Semax /* Check channel, if was not set then obtain it via SDP */ 253166233Semax if (channel == 0 && service != 0) 254166233Semax if (rfcomm_channel_lookup(NULL, &addr, 255166233Semax service, &channel, &n) != 0) 256166233Semax errc(1, n, "Could not obtain RFCOMM channel"); 257166233Semax if (channel <= 0 || channel > 30) 258166233Semax errx(1, "Invalid RFCOMM channel number %d", channel); 259166233Semax 260166233Semax s = socket(PF_BLUETOOTH, SOCK_STREAM, BLUETOOTH_PROTO_RFCOMM); 261166233Semax if (s < 0) 262166233Semax err(1, "Could not create socket"); 263166233Semax 264166233Semax memset(&ra, 0, sizeof(ra)); 265166233Semax ra.rfcomm_len = sizeof(ra); 266166233Semax ra.rfcomm_family = AF_BLUETOOTH; 267166233Semax 268166233Semax if (bind(s, (struct sockaddr *) &ra, sizeof(ra)) < 0) 269166233Semax err(1, "Could not bind socket"); 270166233Semax 271166233Semax memcpy(&ra.rfcomm_bdaddr, &addr, sizeof(ra.rfcomm_bdaddr)); 272166233Semax ra.rfcomm_channel = channel; 273166233Semax 274166233Semax if (connect(s, (struct sockaddr *) &ra, sizeof(ra)) < 0) 275166233Semax err(1, "Could not connect socket"); 276166233Semax } 277166233Semax 278114878Sjulian /* Became daemon if required */ 279114878Sjulian if (background) { 280114878Sjulian switch (fork()) { 281114878Sjulian case -1: 282114878Sjulian err(1, "Could not fork()"); 283114878Sjulian /* NOT REACHED */ 284114878Sjulian 285114878Sjulian case 0: 286114878Sjulian exit(0); 287114878Sjulian /* NOT REACHED */ 288114878Sjulian 289114878Sjulian default: 290114878Sjulian if (daemon(0, 0) < 0) 291114878Sjulian err(1, "Could not daemon()"); 292114878Sjulian break; 293114878Sjulian } 294114878Sjulian } 295114878Sjulian 296114878Sjulian openlog(SPPD_IDENT, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_DAEMON); 297135993Semax syslog(LOG_INFO, "Starting on %s...", (tty != NULL)? tty : "stdin/stdout"); 298114878Sjulian 299114878Sjulian for (done = 0; !done; ) { 300114878Sjulian FD_ZERO(&rfd); 301114878Sjulian FD_SET(amaster, &rfd); 302114878Sjulian FD_SET(s, &rfd); 303114878Sjulian 304114878Sjulian n = select(max(amaster, s) + 1, &rfd, NULL, NULL, NULL); 305114878Sjulian if (n < 0) { 306114878Sjulian if (errno == EINTR) 307114878Sjulian continue; 308114878Sjulian 309114878Sjulian syslog(LOG_ERR, "Could not select(). %s", 310114878Sjulian strerror(errno)); 311114878Sjulian exit(1); 312114878Sjulian } 313114878Sjulian 314114878Sjulian if (n == 0) 315114878Sjulian continue; 316114878Sjulian 317114878Sjulian if (FD_ISSET(amaster, &rfd)) { 318114878Sjulian n = sppd_read(amaster, buf, sizeof(buf)); 319114878Sjulian if (n < 0) { 320114878Sjulian syslog(LOG_ERR, "Could not read master pty, " \ 321114878Sjulian "fd=%d. %s", amaster, strerror(errno)); 322114878Sjulian exit(1); 323114878Sjulian } 324114878Sjulian 325114878Sjulian if (n == 0) 326114878Sjulian break; /* XXX */ 327114878Sjulian 328114878Sjulian if (sppd_write(s, buf, n) < 0) { 329114878Sjulian syslog(LOG_ERR, "Could not write to socket, " \ 330114878Sjulian "fd=%d, size=%d. %s", 331114878Sjulian s, n, strerror(errno)); 332114878Sjulian exit(1); 333114878Sjulian } 334114878Sjulian } 335114878Sjulian 336114878Sjulian if (FD_ISSET(s, &rfd)) { 337114878Sjulian n = sppd_read(s, buf, sizeof(buf)); 338114878Sjulian if (n < 0) { 339114878Sjulian syslog(LOG_ERR, "Could not read socket, " \ 340114878Sjulian "fd=%d. %s", s, strerror(errno)); 341114878Sjulian exit(1); 342114878Sjulian } 343114878Sjulian 344114878Sjulian if (n == 0) 345114878Sjulian break; 346114878Sjulian 347135993Semax if (sppd_write(fd, buf, n) < 0) { 348114878Sjulian syslog(LOG_ERR, "Could not write to master " \ 349114878Sjulian "pty, fd=%d, size=%d. %s", 350135993Semax fd, n, strerror(errno)); 351114878Sjulian exit(1); 352114878Sjulian } 353114878Sjulian } 354114878Sjulian } 355114878Sjulian 356135993Semax syslog(LOG_INFO, "Completed on %s", (tty != NULL)? tty : "stdin/stdout"); 357114878Sjulian closelog(); 358114878Sjulian 359114878Sjulian close(s); 360114878Sjulian 361135993Semax if (tty != NULL) { 362135993Semax close(aslave); 363135993Semax close(amaster); 364135993Semax } 365135993Semax 366114878Sjulian return (0); 367114878Sjulian} 368114878Sjulian 369114878Sjulian/* Open TTYs */ 370114878Sjulianstatic int 371114878Sjuliansppd_ttys_open(char const *tty, int *amaster, int *aslave) 372114878Sjulian{ 373123734Semax char pty[PATH_MAX], *slash; 374114878Sjulian struct group *gr = NULL; 375114878Sjulian gid_t ttygid; 376114878Sjulian struct termios tio; 377114878Sjulian 378114878Sjulian /* 379123676Semax * Construct master PTY name. The slave tty name must be less then 380123676Semax * PATH_MAX characters in length, must contain '/' character and 381123676Semax * must not end with '/'. 382114878Sjulian */ 383114878Sjulian 384123676Semax if (strlen(tty) >= sizeof(pty)) { 385123676Semax syslog(LOG_ERR, "Slave tty name is too long"); 386123676Semax return (-1); 387123676Semax } 388123676Semax 389114878Sjulian strlcpy(pty, tty, sizeof(pty)); 390123676Semax slash = strrchr(pty, '/'); 391123734Semax if (slash == NULL || slash[1] == '\0') { 392123676Semax syslog(LOG_ERR, "Invalid slave tty name (%s)", tty); 393123676Semax return (-1); 394123676Semax } 395114878Sjulian 396123676Semax slash[1] = 'p'; 397123676Semax 398114878Sjulian if (strcmp(pty, tty) == 0) { 399114878Sjulian syslog(LOG_ERR, "Master and slave tty are the same (%s)", tty); 400114878Sjulian return (-1); 401114878Sjulian } 402114878Sjulian 403114878Sjulian if ((*amaster = open(pty, O_RDWR, 0)) < 0) { 404114878Sjulian syslog(LOG_ERR, "Could not open(%s). %s", pty, strerror(errno)); 405114878Sjulian return (-1); 406114878Sjulian } 407114878Sjulian 408114878Sjulian /* 409114878Sjulian * Slave TTY 410114878Sjulian */ 411114878Sjulian 412114878Sjulian if ((gr = getgrnam("tty")) != NULL) 413114878Sjulian ttygid = gr->gr_gid; 414114878Sjulian else 415114878Sjulian ttygid = -1; 416114878Sjulian 417114878Sjulian (void) chown(tty, getuid(), ttygid); 418166233Semax (void) chmod(tty, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); 419114878Sjulian (void) revoke(tty); 420114878Sjulian 421114878Sjulian if ((*aslave = open(tty, O_RDWR, 0)) < 0) { 422114878Sjulian syslog(LOG_ERR, "Could not open(%s). %s", tty, strerror(errno)); 423114878Sjulian close(*amaster); 424114878Sjulian return (-1); 425114878Sjulian } 426114878Sjulian 427114878Sjulian /* 428114878Sjulian * Make slave TTY raw 429114878Sjulian */ 430114878Sjulian 431114878Sjulian cfmakeraw(&tio); 432114878Sjulian 433114878Sjulian if (tcsetattr(*aslave, TCSANOW, &tio) < 0) { 434114878Sjulian syslog(LOG_ERR, "Could not tcsetattr(). %s", strerror(errno)); 435114878Sjulian close(*aslave); 436114878Sjulian close(*amaster); 437114878Sjulian return (-1); 438114878Sjulian } 439114878Sjulian 440114878Sjulian return (0); 441114878Sjulian} /* sppd_ttys_open */ 442114878Sjulian 443114878Sjulian/* Read data */ 444114878Sjulianstatic int 445114878Sjuliansppd_read(int fd, char *buffer, int size) 446114878Sjulian{ 447114878Sjulian int n; 448114878Sjulian 449114878Sjulianagain: 450114878Sjulian n = read(fd, buffer, size); 451114878Sjulian if (n < 0) { 452114878Sjulian if (errno == EINTR) 453114878Sjulian goto again; 454114878Sjulian 455114878Sjulian return (-1); 456114878Sjulian } 457114878Sjulian 458114878Sjulian return (n); 459114878Sjulian} /* sppd_read */ 460114878Sjulian 461114878Sjulian/* Write data */ 462114878Sjulianstatic int 463114878Sjuliansppd_write(int fd, char *buffer, int size) 464114878Sjulian{ 465114878Sjulian int n, wrote; 466114878Sjulian 467114878Sjulian for (wrote = 0; size > 0; ) { 468114878Sjulian n = write(fd, buffer, size); 469114878Sjulian switch (n) { 470114878Sjulian case -1: 471114878Sjulian if (errno != EINTR) 472114878Sjulian return (-1); 473114878Sjulian break; 474114878Sjulian 475114878Sjulian case 0: 476114878Sjulian /* XXX can happen? */ 477114878Sjulian break; 478114878Sjulian 479114878Sjulian default: 480114878Sjulian wrote += n; 481114878Sjulian buffer += n; 482114878Sjulian size -= n; 483114878Sjulian break; 484114878Sjulian } 485114878Sjulian } 486114878Sjulian 487114878Sjulian return (wrote); 488114878Sjulian} /* sppd_write */ 489114878Sjulian 490114878Sjulian/* Signal handler */ 491114878Sjulianstatic void 492114878Sjuliansppd_sighandler(int s) 493114878Sjulian{ 494114878Sjulian syslog(LOG_INFO, "Signal %d received. Total %d signals received\n", 495114878Sjulian s, ++ done); 496114878Sjulian} /* sppd_sighandler */ 497114878Sjulian 498114878Sjulian/* Display usage and exit */ 499114878Sjulianstatic void 500114878Sjulianusage(void) 501114878Sjulian{ 502114878Sjulian fprintf(stdout, 503114878Sjulian"Usage: %s options\n" \ 504114878Sjulian"Where options are:\n" \ 505166233Semax"\t-a address Peer address (required in client mode)\n" \ 506114878Sjulian"\t-b Run in background\n" \ 507166233Semax"\t-c channel RFCOMM channel to connect to or listen on\n" \ 508166233Semax"\t-t tty TTY name (required in background or server mode)\n" \ 509166233Semax"\t-S Server mode\n" \ 510114878Sjulian"\t-h Display this message\n", SPPD_IDENT); 511114878Sjulian exit(255); 512114878Sjulian} /* usage */ 513114878Sjulian 514