1114878Sjulian/* 2114878Sjulian * rfcomm_sppd.c 3178992Semax */ 4178992Semax 5178992Semax/*- 6114878Sjulian * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com> 7114878Sjulian * All rights reserved. 8114878Sjulian * 9114878Sjulian * Redistribution and use in source and binary forms, with or without 10114878Sjulian * modification, are permitted provided that the following conditions 11114878Sjulian * are met: 12114878Sjulian * 1. Redistributions of source code must retain the above copyright 13114878Sjulian * notice, this list of conditions and the following disclaimer. 14114878Sjulian * 2. Redistributions in binary form must reproduce the above copyright 15114878Sjulian * notice, this list of conditions and the following disclaimer in the 16114878Sjulian * documentation and/or other materials provided with the distribution. 17114878Sjulian * 18114878Sjulian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19114878Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20114878Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21114878Sjulian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22114878Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23114878Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24114878Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25114878Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26114878Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27114878Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28114878Sjulian * SUCH DAMAGE. 29114878Sjulian * 30121054Semax * $Id: rfcomm_sppd.c,v 1.4 2003/09/07 18:15:55 max Exp $ 31114878Sjulian * $FreeBSD$ 32114878Sjulian */ 33114878Sjulian 34114878Sjulian#include <sys/stat.h> 35121054Semax#include <bluetooth.h> 36152704Semax#include <ctype.h> 37114878Sjulian#include <err.h> 38114878Sjulian#include <errno.h> 39114878Sjulian#include <fcntl.h> 40114878Sjulian#include <grp.h> 41114878Sjulian#include <limits.h> 42123676Semax#include <paths.h> 43121054Semax#include <sdp.h> 44114878Sjulian#include <signal.h> 45114878Sjulian#include <stdarg.h> 46114878Sjulian#include <stdio.h> 47114878Sjulian#include <stdlib.h> 48114878Sjulian#include <string.h> 49114878Sjulian#include <syslog.h> 50114878Sjulian#include <termios.h> 51114878Sjulian#include <unistd.h> 52114878Sjulian 53114878Sjulian#define SPPD_IDENT "rfcomm_sppd" 54114878Sjulian#define SPPD_BUFFER_SIZE 1024 55114878Sjulian#define max(a, b) (((a) > (b))? (a) : (b)) 56114878Sjulian 57121054Semaxint rfcomm_channel_lookup (bdaddr_t const *local, 58121054Semax bdaddr_t const *remote, 59121054Semax int service, int *channel, int *error); 60121054Semax 61114878Sjulianstatic int sppd_ttys_open (char const *tty, int *amaster, int *aslave); 62114878Sjulianstatic int sppd_read (int fd, char *buffer, int size); 63114878Sjulianstatic int sppd_write (int fd, char *buffer, int size); 64114878Sjulianstatic void sppd_sighandler (int s); 65114878Sjulianstatic void usage (void); 66114878Sjulian 67114878Sjulianstatic int done; /* are we done? */ 68114878Sjulian 69114878Sjulian/* Main */ 70114878Sjulianint 71114878Sjulianmain(int argc, char *argv[]) 72114878Sjulian{ 73114878Sjulian struct sigaction sa; 74114878Sjulian struct sockaddr_rfcomm ra; 75114878Sjulian bdaddr_t addr; 76152704Semax int n, background, channel, service, 77166233Semax s, amaster, aslave, fd, doserver; 78114878Sjulian fd_set rfd; 79152704Semax char *tty = NULL, *ep = NULL, buf[SPPD_BUFFER_SIZE]; 80114878Sjulian 81114878Sjulian memcpy(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)); 82114878Sjulian background = channel = 0; 83152704Semax service = SDP_SERVICE_CLASS_SERIAL_PORT; 84166233Semax doserver = 0; 85114878Sjulian 86114878Sjulian /* Parse command line options */ 87166233Semax while ((n = getopt(argc, argv, "a:bc:t:hS")) != -1) { 88114878Sjulian switch (n) { 89121054Semax case 'a': /* BDADDR */ 90121054Semax if (!bt_aton(optarg, &addr)) { 91121054Semax struct hostent *he = NULL; 92114878Sjulian 93121054Semax if ((he = bt_gethostbyname(optarg)) == NULL) 94121054Semax errx(1, "%s: %s", optarg, hstrerror(h_errno)); 95114878Sjulian 96121054Semax memcpy(&addr, he->h_addr, sizeof(addr)); 97121054Semax } 98121054Semax break; 99114878Sjulian 100114878Sjulian case 'c': /* RFCOMM channel */ 101152704Semax channel = strtoul(optarg, &ep, 10); 102152704Semax if (*ep != '\0') { 103152704Semax channel = 0; 104152704Semax switch (tolower(optarg[0])) { 105152704Semax case 'd': /* DialUp Networking */ 106152704Semax service = SDP_SERVICE_CLASS_DIALUP_NETWORKING; 107152704Semax break; 108152704Semax 109152704Semax case 'f': /* Fax */ 110152704Semax service = SDP_SERVICE_CLASS_FAX; 111152704Semax break; 112152704Semax 113153209Semax case 'l': /* LAN */ 114153209Semax service = SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP; 115153209Semax break; 116153209Semax 117152704Semax case 's': /* Serial Port */ 118152704Semax service = SDP_SERVICE_CLASS_SERIAL_PORT; 119152704Semax break; 120152704Semax 121152704Semax default: 122152704Semax errx(1, "Unknown service name: %s", 123152704Semax optarg); 124152704Semax /* NOT REACHED */ 125152704Semax } 126152704Semax } 127114878Sjulian break; 128114878Sjulian 129114878Sjulian case 'b': /* Run in background */ 130114878Sjulian background = 1; 131114878Sjulian break; 132114878Sjulian 133114878Sjulian case 't': /* Slave TTY name */ 134123676Semax if (optarg[0] != '/') 135123676Semax asprintf(&tty, "%s%s", _PATH_DEV, optarg); 136123676Semax else 137123676Semax tty = optarg; 138114878Sjulian break; 139114878Sjulian 140166233Semax case 'S': 141166233Semax doserver = 1; 142166233Semax break; 143166233Semax 144114878Sjulian case 'h': 145114878Sjulian default: 146114878Sjulian usage(); 147114878Sjulian /* NOT REACHED */ 148114878Sjulian } 149114878Sjulian } 150114878Sjulian 151114878Sjulian /* Check if we have everything we need */ 152166233Semax if (!doserver && memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) == 0) 153114878Sjulian usage(); 154114878Sjulian /* NOT REACHED */ 155114878Sjulian 156114878Sjulian /* Set signal handlers */ 157114878Sjulian memset(&sa, 0, sizeof(sa)); 158114878Sjulian sa.sa_handler = sppd_sighandler; 159114878Sjulian 160114878Sjulian if (sigaction(SIGTERM, &sa, NULL) < 0) 161114878Sjulian err(1, "Could not sigaction(SIGTERM)"); 162114878Sjulian 163114878Sjulian if (sigaction(SIGHUP, &sa, NULL) < 0) 164114878Sjulian err(1, "Could not sigaction(SIGHUP)"); 165114878Sjulian 166114878Sjulian if (sigaction(SIGINT, &sa, NULL) < 0) 167114878Sjulian err(1, "Could not sigaction(SIGINT)"); 168114878Sjulian 169114878Sjulian sa.sa_handler = SIG_IGN; 170114878Sjulian sa.sa_flags = SA_NOCLDWAIT; 171114878Sjulian 172114878Sjulian if (sigaction(SIGCHLD, &sa, NULL) < 0) 173114878Sjulian err(1, "Could not sigaction(SIGCHLD)"); 174114878Sjulian 175114878Sjulian /* Open TTYs */ 176135993Semax if (tty == NULL) { 177178992Semax if (background) 178135993Semax usage(); 179114878Sjulian 180135993Semax amaster = STDIN_FILENO; 181135993Semax fd = STDOUT_FILENO; 182135993Semax } else { 183135993Semax if (sppd_ttys_open(tty, &amaster, &aslave) < 0) 184135993Semax exit(1); 185135993Semax 186135993Semax fd = amaster; 187135993Semax } 188135993Semax 189114878Sjulian /* Open RFCOMM connection */ 190114878Sjulian 191166233Semax if (doserver) { 192166233Semax struct sockaddr_rfcomm ma; 193166233Semax bdaddr_t bt_addr_any; 194178992Semax sdp_sp_profile_t sp; 195166233Semax void *ss; 196166233Semax uint32_t sdp_handle; 197166233Semax int acceptsock, aaddrlen; 198114878Sjulian 199166233Semax acceptsock = socket(PF_BLUETOOTH, SOCK_STREAM, 200178992Semax BLUETOOTH_PROTO_RFCOMM); 201166233Semax if (acceptsock < 0) 202166233Semax err(1, "Could not create socket"); 203114878Sjulian 204178992Semax memcpy(&bt_addr_any, NG_HCI_BDADDR_ANY, sizeof(bt_addr_any)); 205178992Semax 206166233Semax memset(&ma, 0, sizeof(ma)); 207166233Semax ma.rfcomm_len = sizeof(ma); 208166233Semax ma.rfcomm_family = AF_BLUETOOTH; 209178992Semax memcpy(&ma.rfcomm_bdaddr, &bt_addr_any, sizeof(bt_addr_any)); 210166233Semax ma.rfcomm_channel = channel; 211114878Sjulian 212166233Semax if (bind(acceptsock, (struct sockaddr *)&ma, sizeof(ma)) < 0) 213178992Semax err(1, "Could not bind socket on channel %d", channel); 214172142Skevlo if (listen(acceptsock, 10) != 0) 215172142Skevlo err(1, "Could not listen on socket"); 216114878Sjulian 217178992Semax aaddrlen = sizeof(ma); 218178992Semax if (getsockname(acceptsock, (struct sockaddr *)&ma, &aaddrlen) < 0) 219178992Semax err(1, "Could not get socket name"); 220178992Semax channel = ma.rfcomm_channel; 221178992Semax 222166233Semax ss = sdp_open_local(NULL); 223166233Semax if (ss == NULL) 224166233Semax errx(1, "Unable to create local SDP session"); 225166233Semax if (sdp_error(ss) != 0) 226166233Semax errx(1, "Unable to open local SDP session. %s (%d)", 227166233Semax strerror(sdp_error(ss)), sdp_error(ss)); 228178992Semax memset(&sp, 0, sizeof(sp)); 229178992Semax sp.server_channel = channel; 230166233Semax 231178992Semax if (sdp_register_service(ss, SDP_SERVICE_CLASS_SERIAL_PORT, 232178992Semax &bt_addr_any, (void *)&sp, sizeof(sp), 233178992Semax &sdp_handle) != 0) { 234166233Semax errx(1, "Unable to register LAN service with " 235166233Semax "local SDP daemon. %s (%d)", 236166233Semax strerror(sdp_error(ss)), sdp_error(ss)); 237166233Semax } 238166233Semax 239166233Semax s = -1; 240166233Semax while (s < 0) { 241166233Semax aaddrlen = sizeof(ra); 242166233Semax s = accept(acceptsock, (struct sockaddr *)&ra, 243166233Semax &aaddrlen); 244166233Semax if (s < 0) 245166233Semax err(1, "Unable to accept()"); 246166233Semax if (memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) && 247166233Semax memcmp(&addr, &ra.rfcomm_bdaddr, sizeof(addr))) { 248166233Semax warnx("Connect from wrong client"); 249166233Semax close(s); 250166233Semax s = -1; 251166233Semax } 252166233Semax } 253166233Semax sdp_unregister_service(ss, sdp_handle); 254166233Semax sdp_close(ss); 255166233Semax close(acceptsock); 256166233Semax } else { 257166233Semax /* Check channel, if was not set then obtain it via SDP */ 258166233Semax if (channel == 0 && service != 0) 259166233Semax if (rfcomm_channel_lookup(NULL, &addr, 260166233Semax service, &channel, &n) != 0) 261166233Semax errc(1, n, "Could not obtain RFCOMM channel"); 262166233Semax if (channel <= 0 || channel > 30) 263166233Semax errx(1, "Invalid RFCOMM channel number %d", channel); 264166233Semax 265166233Semax s = socket(PF_BLUETOOTH, SOCK_STREAM, BLUETOOTH_PROTO_RFCOMM); 266166233Semax if (s < 0) 267166233Semax err(1, "Could not create socket"); 268166233Semax 269166233Semax memset(&ra, 0, sizeof(ra)); 270166233Semax ra.rfcomm_len = sizeof(ra); 271166233Semax ra.rfcomm_family = AF_BLUETOOTH; 272166233Semax 273166233Semax if (bind(s, (struct sockaddr *) &ra, sizeof(ra)) < 0) 274166233Semax err(1, "Could not bind socket"); 275166233Semax 276166233Semax memcpy(&ra.rfcomm_bdaddr, &addr, sizeof(ra.rfcomm_bdaddr)); 277166233Semax ra.rfcomm_channel = channel; 278166233Semax 279166233Semax if (connect(s, (struct sockaddr *) &ra, sizeof(ra)) < 0) 280166233Semax err(1, "Could not connect socket"); 281166233Semax } 282166233Semax 283114878Sjulian /* Became daemon if required */ 284188130Semax if (background && daemon(0, 0) < 0) 285188130Semax err(1, "Could not daemon()"); 286114878Sjulian 287114878Sjulian openlog(SPPD_IDENT, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_DAEMON); 288135993Semax syslog(LOG_INFO, "Starting on %s...", (tty != NULL)? tty : "stdin/stdout"); 289114878Sjulian 290114878Sjulian for (done = 0; !done; ) { 291114878Sjulian FD_ZERO(&rfd); 292114878Sjulian FD_SET(amaster, &rfd); 293114878Sjulian FD_SET(s, &rfd); 294114878Sjulian 295114878Sjulian n = select(max(amaster, s) + 1, &rfd, NULL, NULL, NULL); 296114878Sjulian if (n < 0) { 297114878Sjulian if (errno == EINTR) 298114878Sjulian continue; 299114878Sjulian 300114878Sjulian syslog(LOG_ERR, "Could not select(). %s", 301114878Sjulian strerror(errno)); 302114878Sjulian exit(1); 303114878Sjulian } 304114878Sjulian 305114878Sjulian if (n == 0) 306114878Sjulian continue; 307114878Sjulian 308114878Sjulian if (FD_ISSET(amaster, &rfd)) { 309114878Sjulian n = sppd_read(amaster, buf, sizeof(buf)); 310114878Sjulian if (n < 0) { 311114878Sjulian syslog(LOG_ERR, "Could not read master pty, " \ 312114878Sjulian "fd=%d. %s", amaster, strerror(errno)); 313114878Sjulian exit(1); 314114878Sjulian } 315114878Sjulian 316114878Sjulian if (n == 0) 317114878Sjulian break; /* XXX */ 318114878Sjulian 319114878Sjulian if (sppd_write(s, buf, n) < 0) { 320114878Sjulian syslog(LOG_ERR, "Could not write to socket, " \ 321114878Sjulian "fd=%d, size=%d. %s", 322114878Sjulian s, n, strerror(errno)); 323114878Sjulian exit(1); 324114878Sjulian } 325114878Sjulian } 326114878Sjulian 327114878Sjulian if (FD_ISSET(s, &rfd)) { 328114878Sjulian n = sppd_read(s, buf, sizeof(buf)); 329114878Sjulian if (n < 0) { 330114878Sjulian syslog(LOG_ERR, "Could not read socket, " \ 331114878Sjulian "fd=%d. %s", s, strerror(errno)); 332114878Sjulian exit(1); 333114878Sjulian } 334114878Sjulian 335114878Sjulian if (n == 0) 336114878Sjulian break; 337114878Sjulian 338135993Semax if (sppd_write(fd, buf, n) < 0) { 339114878Sjulian syslog(LOG_ERR, "Could not write to master " \ 340114878Sjulian "pty, fd=%d, size=%d. %s", 341135993Semax fd, n, strerror(errno)); 342114878Sjulian exit(1); 343114878Sjulian } 344114878Sjulian } 345114878Sjulian } 346114878Sjulian 347135993Semax syslog(LOG_INFO, "Completed on %s", (tty != NULL)? tty : "stdin/stdout"); 348114878Sjulian closelog(); 349114878Sjulian 350114878Sjulian close(s); 351114878Sjulian 352135993Semax if (tty != NULL) { 353135993Semax close(aslave); 354135993Semax close(amaster); 355135993Semax } 356135993Semax 357114878Sjulian return (0); 358114878Sjulian} 359114878Sjulian 360114878Sjulian/* Open TTYs */ 361114878Sjulianstatic int 362114878Sjuliansppd_ttys_open(char const *tty, int *amaster, int *aslave) 363114878Sjulian{ 364123734Semax char pty[PATH_MAX], *slash; 365114878Sjulian struct group *gr = NULL; 366114878Sjulian gid_t ttygid; 367114878Sjulian struct termios tio; 368114878Sjulian 369114878Sjulian /* 370123676Semax * Construct master PTY name. The slave tty name must be less then 371123676Semax * PATH_MAX characters in length, must contain '/' character and 372123676Semax * must not end with '/'. 373114878Sjulian */ 374114878Sjulian 375123676Semax if (strlen(tty) >= sizeof(pty)) { 376123676Semax syslog(LOG_ERR, "Slave tty name is too long"); 377123676Semax return (-1); 378123676Semax } 379123676Semax 380114878Sjulian strlcpy(pty, tty, sizeof(pty)); 381123676Semax slash = strrchr(pty, '/'); 382123734Semax if (slash == NULL || slash[1] == '\0') { 383123676Semax syslog(LOG_ERR, "Invalid slave tty name (%s)", tty); 384123676Semax return (-1); 385123676Semax } 386114878Sjulian 387123676Semax slash[1] = 'p'; 388123676Semax 389114878Sjulian if (strcmp(pty, tty) == 0) { 390114878Sjulian syslog(LOG_ERR, "Master and slave tty are the same (%s)", tty); 391114878Sjulian return (-1); 392114878Sjulian } 393114878Sjulian 394114878Sjulian if ((*amaster = open(pty, O_RDWR, 0)) < 0) { 395114878Sjulian syslog(LOG_ERR, "Could not open(%s). %s", pty, strerror(errno)); 396114878Sjulian return (-1); 397114878Sjulian } 398114878Sjulian 399114878Sjulian /* 400114878Sjulian * Slave TTY 401114878Sjulian */ 402114878Sjulian 403114878Sjulian if ((gr = getgrnam("tty")) != NULL) 404114878Sjulian ttygid = gr->gr_gid; 405114878Sjulian else 406114878Sjulian ttygid = -1; 407114878Sjulian 408114878Sjulian (void) chown(tty, getuid(), ttygid); 409166233Semax (void) chmod(tty, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); 410114878Sjulian (void) revoke(tty); 411114878Sjulian 412114878Sjulian if ((*aslave = open(tty, O_RDWR, 0)) < 0) { 413114878Sjulian syslog(LOG_ERR, "Could not open(%s). %s", tty, strerror(errno)); 414114878Sjulian close(*amaster); 415114878Sjulian return (-1); 416114878Sjulian } 417114878Sjulian 418114878Sjulian /* 419114878Sjulian * Make slave TTY raw 420114878Sjulian */ 421114878Sjulian 422114878Sjulian cfmakeraw(&tio); 423114878Sjulian 424114878Sjulian if (tcsetattr(*aslave, TCSANOW, &tio) < 0) { 425114878Sjulian syslog(LOG_ERR, "Could not tcsetattr(). %s", strerror(errno)); 426114878Sjulian close(*aslave); 427114878Sjulian close(*amaster); 428114878Sjulian return (-1); 429114878Sjulian } 430114878Sjulian 431114878Sjulian return (0); 432114878Sjulian} /* sppd_ttys_open */ 433114878Sjulian 434114878Sjulian/* Read data */ 435114878Sjulianstatic int 436114878Sjuliansppd_read(int fd, char *buffer, int size) 437114878Sjulian{ 438114878Sjulian int n; 439114878Sjulian 440114878Sjulianagain: 441114878Sjulian n = read(fd, buffer, size); 442114878Sjulian if (n < 0) { 443114878Sjulian if (errno == EINTR) 444114878Sjulian goto again; 445114878Sjulian 446114878Sjulian return (-1); 447114878Sjulian } 448114878Sjulian 449114878Sjulian return (n); 450114878Sjulian} /* sppd_read */ 451114878Sjulian 452114878Sjulian/* Write data */ 453114878Sjulianstatic int 454114878Sjuliansppd_write(int fd, char *buffer, int size) 455114878Sjulian{ 456114878Sjulian int n, wrote; 457114878Sjulian 458114878Sjulian for (wrote = 0; size > 0; ) { 459114878Sjulian n = write(fd, buffer, size); 460114878Sjulian switch (n) { 461114878Sjulian case -1: 462114878Sjulian if (errno != EINTR) 463114878Sjulian return (-1); 464114878Sjulian break; 465114878Sjulian 466114878Sjulian case 0: 467114878Sjulian /* XXX can happen? */ 468114878Sjulian break; 469114878Sjulian 470114878Sjulian default: 471114878Sjulian wrote += n; 472114878Sjulian buffer += n; 473114878Sjulian size -= n; 474114878Sjulian break; 475114878Sjulian } 476114878Sjulian } 477114878Sjulian 478114878Sjulian return (wrote); 479114878Sjulian} /* sppd_write */ 480114878Sjulian 481114878Sjulian/* Signal handler */ 482114878Sjulianstatic void 483114878Sjuliansppd_sighandler(int s) 484114878Sjulian{ 485114878Sjulian syslog(LOG_INFO, "Signal %d received. Total %d signals received\n", 486114878Sjulian s, ++ done); 487114878Sjulian} /* sppd_sighandler */ 488114878Sjulian 489114878Sjulian/* Display usage and exit */ 490114878Sjulianstatic void 491114878Sjulianusage(void) 492114878Sjulian{ 493114878Sjulian fprintf(stdout, 494114878Sjulian"Usage: %s options\n" \ 495114878Sjulian"Where options are:\n" \ 496166233Semax"\t-a address Peer address (required in client mode)\n" \ 497114878Sjulian"\t-b Run in background\n" \ 498166233Semax"\t-c channel RFCOMM channel to connect to or listen on\n" \ 499178992Semax"\t-t tty TTY name (required in background mode)\n" \ 500166233Semax"\t-S Server mode\n" \ 501114878Sjulian"\t-h Display this message\n", SPPD_IDENT); 502114878Sjulian exit(255); 503114878Sjulian} /* usage */ 504114878Sjulian 505