Path: blob/main/usr.bin/bluetooth/rfcomm_sppd/rfcomm_sppd.c
34865 views
/*1* rfcomm_sppd.c2*/34/*-5* SPDX-License-Identifier: BSD-2-Clause6*7* Copyright (c) 2003 Maksim Yevmenkin <[email protected]>8* All rights reserved.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18*19* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND20* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE22* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE23* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL24* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS25* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT27* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY28* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF29* SUCH DAMAGE.30*31* $Id: rfcomm_sppd.c,v 1.4 2003/09/07 18:15:55 max Exp $32*/3334#include <sys/stat.h>35#include <sys/types.h>36#define L2CAP_SOCKET_CHECKED37#include <bluetooth.h>38#include <ctype.h>39#include <err.h>40#include <errno.h>41#include <fcntl.h>42#include <grp.h>43#include <limits.h>44#include <paths.h>45#include <sdp.h>46#include <signal.h>47#include <stdarg.h>48#include <stdio.h>49#include <stdlib.h>50#include <string.h>51#include <syslog.h>52#include <termios.h>53#include <unistd.h>54#include <libutil.h>5556#define SPPD_IDENT "rfcomm_sppd"57#define SPPD_BUFFER_SIZE 102458#define max(a, b) (((a) > (b))? (a) : (b))5960int rfcomm_channel_lookup (bdaddr_t const *local,61bdaddr_t const *remote,62int service, int *channel, int *error);6364static int sppd_ttys_open (char **tty, int *amaster, int *aslave);65static int sppd_read (int fd, char *buffer, int size);66static int sppd_write (int fd, char *buffer, int size);67static void sppd_sighandler (int s);68static void usage (void);6970static int done; /* are we done? */7172/* Main */73int74main(int argc, char *argv[])75{76struct sigaction sa;77struct sockaddr_rfcomm ra;78bdaddr_t addr;79int n, background, channel, service,80s, amaster, aslave, fd, doserver,81dopty;82fd_set rfd;83char *tty = NULL, *ep = NULL, buf[SPPD_BUFFER_SIZE];8485memcpy(&addr, NG_HCI_BDADDR_ANY, sizeof(addr));86background = channel = 0;87service = SDP_SERVICE_CLASS_SERIAL_PORT;88doserver = 0;89dopty = 0;9091/* Parse command line options */92while ((n = getopt(argc, argv, "a:bc:thS")) != -1) {93switch (n) {94case 'a': /* BDADDR */95if (!bt_aton(optarg, &addr)) {96struct hostent *he = NULL;9798if ((he = bt_gethostbyname(optarg)) == NULL)99errx(1, "%s: %s", optarg, hstrerror(h_errno));100101memcpy(&addr, he->h_addr, sizeof(addr));102}103break;104105case 'c': /* RFCOMM channel */106channel = strtoul(optarg, &ep, 10);107if (*ep != '\0') {108channel = 0;109switch (tolower(optarg[0])) {110case 'd': /* DialUp Networking */111service = SDP_SERVICE_CLASS_DIALUP_NETWORKING;112break;113114case 'f': /* Fax */115service = SDP_SERVICE_CLASS_FAX;116break;117118case 'l': /* LAN */119service = SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP;120break;121122case 's': /* Serial Port */123service = SDP_SERVICE_CLASS_SERIAL_PORT;124break;125126default:127errx(1, "Unknown service name: %s",128optarg);129/* NOT REACHED */130}131}132break;133134case 'b': /* Run in background */135background = 1;136break;137138case 't': /* Open pseudo TTY */139dopty = 1;140break;141142case 'S':143doserver = 1;144break;145146case 'h':147default:148usage();149/* NOT REACHED */150}151}152153/* Check if we have everything we need */154if (!doserver && memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) == 0)155usage();156/* NOT REACHED */157158/* Set signal handlers */159memset(&sa, 0, sizeof(sa));160sa.sa_handler = sppd_sighandler;161162if (sigaction(SIGTERM, &sa, NULL) < 0)163err(1, "Could not sigaction(SIGTERM)");164165if (sigaction(SIGHUP, &sa, NULL) < 0)166err(1, "Could not sigaction(SIGHUP)");167168if (sigaction(SIGINT, &sa, NULL) < 0)169err(1, "Could not sigaction(SIGINT)");170171sa.sa_handler = SIG_IGN;172sa.sa_flags = SA_NOCLDWAIT;173174if (sigaction(SIGCHLD, &sa, NULL) < 0)175err(1, "Could not sigaction(SIGCHLD)");176177/* Open TTYs */178if (dopty) {179if (sppd_ttys_open(&tty, &amaster, &aslave) < 0)180exit(1);181182fd = amaster;183} else {184if (background)185usage();186187amaster = STDIN_FILENO;188fd = STDOUT_FILENO;189}190191/* Open RFCOMM connection */192193if (doserver) {194struct sockaddr_rfcomm ma;195bdaddr_t bt_addr_any;196sdp_sp_profile_t sp;197void *ss;198uint32_t sdp_handle;199int acceptsock, aaddrlen;200201acceptsock = socket(PF_BLUETOOTH, SOCK_STREAM,202BLUETOOTH_PROTO_RFCOMM);203if (acceptsock < 0)204err(1, "Could not create socket");205206memcpy(&bt_addr_any, NG_HCI_BDADDR_ANY, sizeof(bt_addr_any));207208memset(&ma, 0, sizeof(ma));209ma.rfcomm_len = sizeof(ma);210ma.rfcomm_family = AF_BLUETOOTH;211memcpy(&ma.rfcomm_bdaddr, &bt_addr_any, sizeof(bt_addr_any));212ma.rfcomm_channel = channel;213214if (bind(acceptsock, (struct sockaddr *)&ma, sizeof(ma)) < 0)215err(1, "Could not bind socket on channel %d", channel);216if (listen(acceptsock, 10) != 0)217err(1, "Could not listen on socket");218219aaddrlen = sizeof(ma);220if (getsockname(acceptsock, (struct sockaddr *)&ma, &aaddrlen) < 0)221err(1, "Could not get socket name");222channel = ma.rfcomm_channel;223224ss = sdp_open_local(NULL);225if (ss == NULL)226errx(1, "Unable to create local SDP session");227if (sdp_error(ss) != 0)228errx(1, "Unable to open local SDP session. %s (%d)",229strerror(sdp_error(ss)), sdp_error(ss));230memset(&sp, 0, sizeof(sp));231sp.server_channel = channel;232233if (sdp_register_service(ss, SDP_SERVICE_CLASS_SERIAL_PORT,234&bt_addr_any, (void *)&sp, sizeof(sp),235&sdp_handle) != 0) {236errx(1, "Unable to register LAN service with "237"local SDP daemon. %s (%d)",238strerror(sdp_error(ss)), sdp_error(ss));239}240241s = -1;242while (s < 0) {243aaddrlen = sizeof(ra);244s = accept(acceptsock, (struct sockaddr *)&ra,245&aaddrlen);246if (s < 0)247err(1, "Unable to accept()");248if (memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) &&249memcmp(&addr, &ra.rfcomm_bdaddr, sizeof(addr))) {250warnx("Connect from wrong client");251close(s);252s = -1;253}254}255sdp_unregister_service(ss, sdp_handle);256sdp_close(ss);257close(acceptsock);258} else {259/* Check channel, if was not set then obtain it via SDP */260if (channel == 0 && service != 0)261if (rfcomm_channel_lookup(NULL, &addr,262service, &channel, &n) != 0)263errc(1, n, "Could not obtain RFCOMM channel");264if (channel <= 0 || channel > 30)265errx(1, "Invalid RFCOMM channel number %d", channel);266267s = socket(PF_BLUETOOTH, SOCK_STREAM, BLUETOOTH_PROTO_RFCOMM);268if (s < 0)269err(1, "Could not create socket");270271memset(&ra, 0, sizeof(ra));272ra.rfcomm_len = sizeof(ra);273ra.rfcomm_family = AF_BLUETOOTH;274275if (bind(s, (struct sockaddr *) &ra, sizeof(ra)) < 0)276err(1, "Could not bind socket");277278memcpy(&ra.rfcomm_bdaddr, &addr, sizeof(ra.rfcomm_bdaddr));279ra.rfcomm_channel = channel;280281if (connect(s, (struct sockaddr *) &ra, sizeof(ra)) < 0)282err(1, "Could not connect socket");283}284285/* Became daemon if required */286if (background && daemon(0, 0) < 0)287err(1, "Could not daemon()");288289openlog(SPPD_IDENT, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_DAEMON);290syslog(LOG_INFO, "Starting on %s...", (tty != NULL)? tty : "stdin/stdout");291292/* Print used tty on stdout for wrappers to pick up */293if (!background)294fprintf(stdout, "%s\n", tty);295296for (done = 0; !done; ) {297FD_ZERO(&rfd);298FD_SET(amaster, &rfd);299FD_SET(s, &rfd);300301n = select(max(amaster, s) + 1, &rfd, NULL, NULL, NULL);302if (n < 0) {303if (errno == EINTR)304continue;305306syslog(LOG_ERR, "Could not select(). %s",307strerror(errno));308exit(1);309}310311if (n == 0)312continue;313314if (FD_ISSET(amaster, &rfd)) {315n = sppd_read(amaster, buf, sizeof(buf));316if (n < 0) {317syslog(LOG_ERR, "Could not read master pty, " \318"fd=%d. %s", amaster, strerror(errno));319exit(1);320}321322if (n == 0)323break; /* XXX */324325if (sppd_write(s, buf, n) < 0) {326syslog(LOG_ERR, "Could not write to socket, " \327"fd=%d, size=%d. %s",328s, n, strerror(errno));329exit(1);330}331}332333if (FD_ISSET(s, &rfd)) {334n = sppd_read(s, buf, sizeof(buf));335if (n < 0) {336syslog(LOG_ERR, "Could not read socket, " \337"fd=%d. %s", s, strerror(errno));338exit(1);339}340341if (n == 0)342break;343344if (sppd_write(fd, buf, n) < 0) {345syslog(LOG_ERR, "Could not write to master " \346"pty, fd=%d, size=%d. %s",347fd, n, strerror(errno));348exit(1);349}350}351}352353syslog(LOG_INFO, "Completed on %s", (tty != NULL)? tty : "stdin/stdout");354closelog();355356close(s);357358if (tty != NULL) {359close(aslave);360close(amaster);361}362363return (0);364}365366/* Open TTYs */367static int368sppd_ttys_open(char **tty, int *amaster, int *aslave)369{370char pty[PATH_MAX];371struct termios tio;372373cfmakeraw(&tio);374375if (openpty(amaster, aslave, pty, &tio, NULL) == -1) {376syslog(LOG_ERR, "Could not openpty(). %s", strerror(errno));377return (-1);378}379380if ((*tty = strdup(pty)) == NULL) {381syslog(LOG_ERR, "Could not strdup(). %s", strerror(errno));382close(*aslave);383close(*amaster);384return (-1);385}386387return (0);388} /* sppd_ttys_open */389390/* Read data */391static int392sppd_read(int fd, char *buffer, int size)393{394int n;395396again:397n = read(fd, buffer, size);398if (n < 0) {399if (errno == EINTR)400goto again;401402return (-1);403}404405return (n);406} /* sppd_read */407408/* Write data */409static int410sppd_write(int fd, char *buffer, int size)411{412int n, wrote;413414for (wrote = 0; size > 0; ) {415n = write(fd, buffer, size);416switch (n) {417case -1:418if (errno != EINTR)419return (-1);420break;421422case 0:423/* XXX can happen? */424break;425426default:427wrote += n;428buffer += n;429size -= n;430break;431}432}433434return (wrote);435} /* sppd_write */436437/* Signal handler */438static void439sppd_sighandler(int s)440{441syslog(LOG_INFO, "Signal %d received. Total %d signals received\n",442s, ++ done);443} /* sppd_sighandler */444445/* Display usage and exit */446static void447usage(void)448{449fprintf(stdout,450"Usage: %s options\n" \451"Where options are:\n" \452"\t-a address Peer address (required in client mode)\n" \453"\t-b Run in background\n" \454"\t-c channel RFCOMM channel to connect to or listen on\n" \455"\t-t use slave pseudo tty (required in background mode)\n" \456"\t-S Server mode\n" \457"\t-h Display this message\n", SPPD_IDENT);458exit(255);459} /* usage */460461462463