rfcomm_sppd.c revision 123734
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 123734 2003-12-22 22:50:21Z emax $ 30114878Sjulian */ 31114878Sjulian 32114878Sjulian#include <sys/stat.h> 33121054Semax#include <bluetooth.h> 34114878Sjulian#include <err.h> 35114878Sjulian#include <errno.h> 36114878Sjulian#include <fcntl.h> 37114878Sjulian#include <grp.h> 38114878Sjulian#include <limits.h> 39123676Semax#include <paths.h> 40121054Semax#include <sdp.h> 41114878Sjulian#include <signal.h> 42114878Sjulian#include <stdarg.h> 43114878Sjulian#include <stdio.h> 44114878Sjulian#include <stdlib.h> 45114878Sjulian#include <string.h> 46114878Sjulian#include <syslog.h> 47114878Sjulian#include <termios.h> 48114878Sjulian#include <unistd.h> 49114878Sjulian 50114878Sjulian#define SPPD_IDENT "rfcomm_sppd" 51114878Sjulian#define SPPD_BUFFER_SIZE 1024 52114878Sjulian#define max(a, b) (((a) > (b))? (a) : (b)) 53114878Sjulian 54121054Semaxint rfcomm_channel_lookup (bdaddr_t const *local, 55121054Semax bdaddr_t const *remote, 56121054Semax int service, int *channel, int *error); 57121054Semax 58114878Sjulianstatic int sppd_ttys_open (char const *tty, int *amaster, int *aslave); 59114878Sjulianstatic int sppd_read (int fd, char *buffer, int size); 60114878Sjulianstatic int sppd_write (int fd, char *buffer, int size); 61114878Sjulianstatic void sppd_sighandler (int s); 62114878Sjulianstatic void usage (void); 63114878Sjulian 64114878Sjulianstatic int done; /* are we done? */ 65114878Sjulian 66114878Sjulian/* Main */ 67114878Sjulianint 68114878Sjulianmain(int argc, char *argv[]) 69114878Sjulian{ 70114878Sjulian struct sigaction sa; 71114878Sjulian struct sockaddr_rfcomm ra; 72114878Sjulian bdaddr_t addr; 73114878Sjulian int n, background, channel, s, amaster, aslave; 74114878Sjulian fd_set rfd; 75114878Sjulian char *tty = NULL, buf[SPPD_BUFFER_SIZE]; 76114878Sjulian 77114878Sjulian memcpy(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)); 78114878Sjulian background = channel = 0; 79114878Sjulian 80114878Sjulian /* Parse command line options */ 81114878Sjulian while ((n = getopt(argc, argv, "a:bc:t:h")) != -1) { 82114878Sjulian switch (n) { 83121054Semax case 'a': /* BDADDR */ 84121054Semax if (!bt_aton(optarg, &addr)) { 85121054Semax struct hostent *he = NULL; 86114878Sjulian 87121054Semax if ((he = bt_gethostbyname(optarg)) == NULL) 88121054Semax errx(1, "%s: %s", optarg, hstrerror(h_errno)); 89114878Sjulian 90121054Semax memcpy(&addr, he->h_addr, sizeof(addr)); 91121054Semax } 92121054Semax break; 93114878Sjulian 94114878Sjulian case 'c': /* RFCOMM channel */ 95114878Sjulian channel = atoi(optarg); 96114878Sjulian break; 97114878Sjulian 98114878Sjulian case 'b': /* Run in background */ 99114878Sjulian background = 1; 100114878Sjulian break; 101114878Sjulian 102114878Sjulian case 't': /* Slave TTY name */ 103123676Semax if (optarg[0] != '/') 104123676Semax asprintf(&tty, "%s%s", _PATH_DEV, optarg); 105123676Semax else 106123676Semax tty = optarg; 107114878Sjulian break; 108114878Sjulian 109114878Sjulian case 'h': 110114878Sjulian default: 111114878Sjulian usage(); 112114878Sjulian /* NOT REACHED */ 113114878Sjulian } 114114878Sjulian } 115114878Sjulian 116114878Sjulian /* Check if we have everything we need */ 117121054Semax if (tty == NULL || memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) == 0) 118114878Sjulian usage(); 119114878Sjulian /* NOT REACHED */ 120114878Sjulian 121114878Sjulian /* Set signal handlers */ 122114878Sjulian memset(&sa, 0, sizeof(sa)); 123114878Sjulian sa.sa_handler = sppd_sighandler; 124114878Sjulian 125114878Sjulian if (sigaction(SIGTERM, &sa, NULL) < 0) 126114878Sjulian err(1, "Could not sigaction(SIGTERM)"); 127114878Sjulian 128114878Sjulian if (sigaction(SIGHUP, &sa, NULL) < 0) 129114878Sjulian err(1, "Could not sigaction(SIGHUP)"); 130114878Sjulian 131114878Sjulian if (sigaction(SIGINT, &sa, NULL) < 0) 132114878Sjulian err(1, "Could not sigaction(SIGINT)"); 133114878Sjulian 134114878Sjulian sa.sa_handler = SIG_IGN; 135114878Sjulian sa.sa_flags = SA_NOCLDWAIT; 136114878Sjulian 137114878Sjulian if (sigaction(SIGCHLD, &sa, NULL) < 0) 138114878Sjulian err(1, "Could not sigaction(SIGCHLD)"); 139114878Sjulian 140121054Semax /* Check channel, if was not set then obtain it via SDP */ 141121054Semax if (channel == 0) 142121054Semax if (rfcomm_channel_lookup(NULL, &addr, 143121054Semax SDP_SERVICE_CLASS_SERIAL_PORT, &channel, &n) != 0) 144121054Semax errc(1, n, "Could not obtain RFCOMM channel"); 145121054Semax if (channel <= 0 || channel > 30) 146121054Semax errx(1, "Invalid RFCOMM channel number %d", channel); 147121054Semax 148114878Sjulian /* Open TTYs */ 149114878Sjulian if (sppd_ttys_open(tty, &amaster, &aslave) < 0) 150114878Sjulian exit(1); 151114878Sjulian 152114878Sjulian /* Open RFCOMM connection */ 153114878Sjulian memset(&ra, 0, sizeof(ra)); 154114878Sjulian ra.rfcomm_len = sizeof(ra); 155114878Sjulian ra.rfcomm_family = AF_BLUETOOTH; 156114878Sjulian 157114878Sjulian s = socket(PF_BLUETOOTH, SOCK_STREAM, BLUETOOTH_PROTO_RFCOMM); 158114878Sjulian if (s < 0) 159114878Sjulian err(1, "Could not create socket"); 160114878Sjulian 161114878Sjulian if (bind(s, (struct sockaddr *) &ra, sizeof(ra)) < 0) 162114878Sjulian err(1, "Could not bind socket"); 163114878Sjulian 164114878Sjulian memcpy(&ra.rfcomm_bdaddr, &addr, sizeof(ra.rfcomm_bdaddr)); 165114878Sjulian ra.rfcomm_channel = channel; 166114878Sjulian 167114878Sjulian if (connect(s, (struct sockaddr *) &ra, sizeof(ra)) < 0) 168114878Sjulian err(1, "Could not connect socket"); 169114878Sjulian 170114878Sjulian /* Became daemon if required */ 171114878Sjulian if (background) { 172114878Sjulian switch (fork()) { 173114878Sjulian case -1: 174114878Sjulian err(1, "Could not fork()"); 175114878Sjulian /* NOT REACHED */ 176114878Sjulian 177114878Sjulian case 0: 178114878Sjulian exit(0); 179114878Sjulian /* NOT REACHED */ 180114878Sjulian 181114878Sjulian default: 182114878Sjulian if (daemon(0, 0) < 0) 183114878Sjulian err(1, "Could not daemon()"); 184114878Sjulian break; 185114878Sjulian } 186114878Sjulian } 187114878Sjulian 188114878Sjulian openlog(SPPD_IDENT, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_DAEMON); 189114878Sjulian syslog(LOG_INFO, "Starting on %s...", tty); 190114878Sjulian 191114878Sjulian for (done = 0; !done; ) { 192114878Sjulian FD_ZERO(&rfd); 193114878Sjulian FD_SET(amaster, &rfd); 194114878Sjulian FD_SET(s, &rfd); 195114878Sjulian 196114878Sjulian n = select(max(amaster, s) + 1, &rfd, NULL, NULL, NULL); 197114878Sjulian if (n < 0) { 198114878Sjulian if (errno == EINTR) 199114878Sjulian continue; 200114878Sjulian 201114878Sjulian syslog(LOG_ERR, "Could not select(). %s", 202114878Sjulian strerror(errno)); 203114878Sjulian exit(1); 204114878Sjulian } 205114878Sjulian 206114878Sjulian if (n == 0) 207114878Sjulian continue; 208114878Sjulian 209114878Sjulian if (FD_ISSET(amaster, &rfd)) { 210114878Sjulian n = sppd_read(amaster, buf, sizeof(buf)); 211114878Sjulian if (n < 0) { 212114878Sjulian syslog(LOG_ERR, "Could not read master pty, " \ 213114878Sjulian "fd=%d. %s", amaster, strerror(errno)); 214114878Sjulian exit(1); 215114878Sjulian } 216114878Sjulian 217114878Sjulian if (n == 0) 218114878Sjulian break; /* XXX */ 219114878Sjulian 220114878Sjulian if (sppd_write(s, buf, n) < 0) { 221114878Sjulian syslog(LOG_ERR, "Could not write to socket, " \ 222114878Sjulian "fd=%d, size=%d. %s", 223114878Sjulian s, n, strerror(errno)); 224114878Sjulian exit(1); 225114878Sjulian } 226114878Sjulian } 227114878Sjulian 228114878Sjulian if (FD_ISSET(s, &rfd)) { 229114878Sjulian n = sppd_read(s, buf, sizeof(buf)); 230114878Sjulian if (n < 0) { 231114878Sjulian syslog(LOG_ERR, "Could not read socket, " \ 232114878Sjulian "fd=%d. %s", s, strerror(errno)); 233114878Sjulian exit(1); 234114878Sjulian } 235114878Sjulian 236114878Sjulian if (n == 0) 237114878Sjulian break; 238114878Sjulian 239114878Sjulian if (sppd_write(amaster, buf, n) < 0) { 240114878Sjulian syslog(LOG_ERR, "Could not write to master " \ 241114878Sjulian "pty, fd=%d, size=%d. %s", 242114878Sjulian amaster, n, strerror(errno)); 243114878Sjulian exit(1); 244114878Sjulian } 245114878Sjulian } 246114878Sjulian } 247114878Sjulian 248114878Sjulian syslog(LOG_INFO, "Completed on %s", tty); 249114878Sjulian closelog(); 250114878Sjulian 251114878Sjulian close(s); 252114878Sjulian close(aslave); 253114878Sjulian close(amaster); 254114878Sjulian 255114878Sjulian return (0); 256114878Sjulian} 257114878Sjulian 258114878Sjulian/* Open TTYs */ 259114878Sjulianstatic int 260114878Sjuliansppd_ttys_open(char const *tty, int *amaster, int *aslave) 261114878Sjulian{ 262123734Semax char pty[PATH_MAX], *slash; 263114878Sjulian struct group *gr = NULL; 264114878Sjulian gid_t ttygid; 265114878Sjulian struct termios tio; 266114878Sjulian 267114878Sjulian /* 268123676Semax * Construct master PTY name. The slave tty name must be less then 269123676Semax * PATH_MAX characters in length, must contain '/' character and 270123676Semax * must not end with '/'. 271114878Sjulian */ 272114878Sjulian 273123676Semax if (strlen(tty) >= sizeof(pty)) { 274123676Semax syslog(LOG_ERR, "Slave tty name is too long"); 275123676Semax return (-1); 276123676Semax } 277123676Semax 278114878Sjulian strlcpy(pty, tty, sizeof(pty)); 279123676Semax slash = strrchr(pty, '/'); 280123734Semax if (slash == NULL || slash[1] == '\0') { 281123676Semax syslog(LOG_ERR, "Invalid slave tty name (%s)", tty); 282123676Semax return (-1); 283123676Semax } 284114878Sjulian 285123676Semax slash[1] = 'p'; 286123676Semax 287114878Sjulian if (strcmp(pty, tty) == 0) { 288114878Sjulian syslog(LOG_ERR, "Master and slave tty are the same (%s)", tty); 289114878Sjulian return (-1); 290114878Sjulian } 291114878Sjulian 292114878Sjulian if ((*amaster = open(pty, O_RDWR, 0)) < 0) { 293114878Sjulian syslog(LOG_ERR, "Could not open(%s). %s", pty, strerror(errno)); 294114878Sjulian return (-1); 295114878Sjulian } 296114878Sjulian 297114878Sjulian /* 298114878Sjulian * Slave TTY 299114878Sjulian */ 300114878Sjulian 301114878Sjulian if ((gr = getgrnam("tty")) != NULL) 302114878Sjulian ttygid = gr->gr_gid; 303114878Sjulian else 304114878Sjulian ttygid = -1; 305114878Sjulian 306114878Sjulian (void) chown(tty, getuid(), ttygid); 307114878Sjulian (void) chmod(tty, S_IRUSR|S_IWUSR|S_IWGRP); 308114878Sjulian (void) revoke(tty); 309114878Sjulian 310114878Sjulian if ((*aslave = open(tty, O_RDWR, 0)) < 0) { 311114878Sjulian syslog(LOG_ERR, "Could not open(%s). %s", tty, strerror(errno)); 312114878Sjulian close(*amaster); 313114878Sjulian return (-1); 314114878Sjulian } 315114878Sjulian 316114878Sjulian /* 317114878Sjulian * Make slave TTY raw 318114878Sjulian */ 319114878Sjulian 320114878Sjulian cfmakeraw(&tio); 321114878Sjulian 322114878Sjulian if (tcsetattr(*aslave, TCSANOW, &tio) < 0) { 323114878Sjulian syslog(LOG_ERR, "Could not tcsetattr(). %s", strerror(errno)); 324114878Sjulian close(*aslave); 325114878Sjulian close(*amaster); 326114878Sjulian return (-1); 327114878Sjulian } 328114878Sjulian 329114878Sjulian return (0); 330114878Sjulian} /* sppd_ttys_open */ 331114878Sjulian 332114878Sjulian/* Read data */ 333114878Sjulianstatic int 334114878Sjuliansppd_read(int fd, char *buffer, int size) 335114878Sjulian{ 336114878Sjulian int n; 337114878Sjulian 338114878Sjulianagain: 339114878Sjulian n = read(fd, buffer, size); 340114878Sjulian if (n < 0) { 341114878Sjulian if (errno == EINTR) 342114878Sjulian goto again; 343114878Sjulian 344114878Sjulian return (-1); 345114878Sjulian } 346114878Sjulian 347114878Sjulian return (n); 348114878Sjulian} /* sppd_read */ 349114878Sjulian 350114878Sjulian/* Write data */ 351114878Sjulianstatic int 352114878Sjuliansppd_write(int fd, char *buffer, int size) 353114878Sjulian{ 354114878Sjulian int n, wrote; 355114878Sjulian 356114878Sjulian for (wrote = 0; size > 0; ) { 357114878Sjulian n = write(fd, buffer, size); 358114878Sjulian switch (n) { 359114878Sjulian case -1: 360114878Sjulian if (errno != EINTR) 361114878Sjulian return (-1); 362114878Sjulian break; 363114878Sjulian 364114878Sjulian case 0: 365114878Sjulian /* XXX can happen? */ 366114878Sjulian break; 367114878Sjulian 368114878Sjulian default: 369114878Sjulian wrote += n; 370114878Sjulian buffer += n; 371114878Sjulian size -= n; 372114878Sjulian break; 373114878Sjulian } 374114878Sjulian } 375114878Sjulian 376114878Sjulian return (wrote); 377114878Sjulian} /* sppd_write */ 378114878Sjulian 379114878Sjulian/* Signal handler */ 380114878Sjulianstatic void 381114878Sjuliansppd_sighandler(int s) 382114878Sjulian{ 383114878Sjulian syslog(LOG_INFO, "Signal %d received. Total %d signals received\n", 384114878Sjulian s, ++ done); 385114878Sjulian} /* sppd_sighandler */ 386114878Sjulian 387114878Sjulian/* Display usage and exit */ 388114878Sjulianstatic void 389114878Sjulianusage(void) 390114878Sjulian{ 391114878Sjulian fprintf(stdout, 392114878Sjulian"Usage: %s options\n" \ 393114878Sjulian"Where options are:\n" \ 394114878Sjulian"\t-a bdaddr BDADDR to connect to (required)\n" \ 395114878Sjulian"\t-b Run in background\n" \ 396121054Semax"\t-c channel RFCOMM channel to connect to\n" \ 397114878Sjulian"\t-t tty TTY name\n" \ 398114878Sjulian"\t-h Display this message\n", SPPD_IDENT); 399114878Sjulian 400114878Sjulian exit(255); 401114878Sjulian} /* usage */ 402114878Sjulian 403