rfcomm_sppd.c revision 135993
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 135993 2004-09-30 21:05:17Z 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;
73135993Semax	int			 n, background, channel, s, amaster, aslave, fd;
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 */
117135993Semax	if (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 */
149135993Semax	if (tty == NULL) {
150135993Semax		if (background)
151135993Semax			usage();
152114878Sjulian
153135993Semax		amaster = STDIN_FILENO;
154135993Semax		fd = STDOUT_FILENO;
155135993Semax	} else {
156135993Semax		if (sppd_ttys_open(tty, &amaster, &aslave) < 0)
157135993Semax			exit(1);
158135993Semax
159135993Semax		fd = amaster;
160135993Semax	}
161135993Semax
162135993Semax
163114878Sjulian	/* Open RFCOMM connection */
164114878Sjulian	memset(&ra, 0, sizeof(ra));
165114878Sjulian	ra.rfcomm_len = sizeof(ra);
166114878Sjulian	ra.rfcomm_family = AF_BLUETOOTH;
167114878Sjulian
168114878Sjulian	s = socket(PF_BLUETOOTH, SOCK_STREAM, BLUETOOTH_PROTO_RFCOMM);
169114878Sjulian	if (s < 0)
170114878Sjulian		err(1, "Could not create socket");
171114878Sjulian
172114878Sjulian	if (bind(s, (struct sockaddr *) &ra, sizeof(ra)) < 0)
173114878Sjulian		err(1, "Could not bind socket");
174114878Sjulian
175114878Sjulian	memcpy(&ra.rfcomm_bdaddr, &addr, sizeof(ra.rfcomm_bdaddr));
176114878Sjulian	ra.rfcomm_channel = channel;
177114878Sjulian
178114878Sjulian	if (connect(s, (struct sockaddr *) &ra, sizeof(ra)) < 0)
179114878Sjulian		err(1, "Could not connect socket");
180114878Sjulian
181114878Sjulian	/* Became daemon if required */
182114878Sjulian	if (background) {
183114878Sjulian		switch (fork()) {
184114878Sjulian		case -1:
185114878Sjulian			err(1, "Could not fork()");
186114878Sjulian			/* NOT REACHED */
187114878Sjulian
188114878Sjulian		case 0:
189114878Sjulian			exit(0);
190114878Sjulian			/* NOT REACHED */
191114878Sjulian
192114878Sjulian		default:
193114878Sjulian			if (daemon(0, 0) < 0)
194114878Sjulian				err(1, "Could not daemon()");
195114878Sjulian			break;
196114878Sjulian		}
197114878Sjulian	}
198114878Sjulian
199114878Sjulian	openlog(SPPD_IDENT, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_DAEMON);
200135993Semax	syslog(LOG_INFO, "Starting on %s...", (tty != NULL)? tty : "stdin/stdout");
201114878Sjulian
202114878Sjulian	for (done = 0; !done; ) {
203114878Sjulian		FD_ZERO(&rfd);
204114878Sjulian		FD_SET(amaster, &rfd);
205114878Sjulian		FD_SET(s, &rfd);
206114878Sjulian
207114878Sjulian		n = select(max(amaster, s) + 1, &rfd, NULL, NULL, NULL);
208114878Sjulian		if (n < 0) {
209114878Sjulian			if (errno == EINTR)
210114878Sjulian				continue;
211114878Sjulian
212114878Sjulian			syslog(LOG_ERR, "Could not select(). %s",
213114878Sjulian					strerror(errno));
214114878Sjulian			exit(1);
215114878Sjulian		}
216114878Sjulian
217114878Sjulian		if (n == 0)
218114878Sjulian			continue;
219114878Sjulian
220114878Sjulian		if (FD_ISSET(amaster, &rfd)) {
221114878Sjulian			n = sppd_read(amaster, buf, sizeof(buf));
222114878Sjulian			if (n < 0) {
223114878Sjulian				syslog(LOG_ERR, "Could not read master pty, " \
224114878Sjulian					"fd=%d. %s", amaster, strerror(errno));
225114878Sjulian				exit(1);
226114878Sjulian			}
227114878Sjulian
228114878Sjulian			if (n == 0)
229114878Sjulian				break; /* XXX */
230114878Sjulian
231114878Sjulian			if (sppd_write(s, buf, n) < 0) {
232114878Sjulian				syslog(LOG_ERR, "Could not write to socket, " \
233114878Sjulian					"fd=%d, size=%d. %s",
234114878Sjulian					s, n, strerror(errno));
235114878Sjulian				exit(1);
236114878Sjulian			}
237114878Sjulian		}
238114878Sjulian
239114878Sjulian		if (FD_ISSET(s, &rfd)) {
240114878Sjulian			n = sppd_read(s, buf, sizeof(buf));
241114878Sjulian			if (n < 0) {
242114878Sjulian				syslog(LOG_ERR, "Could not read socket, " \
243114878Sjulian					"fd=%d. %s", s, strerror(errno));
244114878Sjulian				exit(1);
245114878Sjulian			}
246114878Sjulian
247114878Sjulian			if (n == 0)
248114878Sjulian				break;
249114878Sjulian
250135993Semax			if (sppd_write(fd, buf, n) < 0) {
251114878Sjulian				syslog(LOG_ERR, "Could not write to master " \
252114878Sjulian					"pty, fd=%d, size=%d. %s",
253135993Semax					fd, n, strerror(errno));
254114878Sjulian				exit(1);
255114878Sjulian			}
256114878Sjulian		}
257114878Sjulian	}
258114878Sjulian
259135993Semax	syslog(LOG_INFO, "Completed on %s", (tty != NULL)? tty : "stdin/stdout");
260114878Sjulian	closelog();
261114878Sjulian
262114878Sjulian	close(s);
263114878Sjulian
264135993Semax	if (tty != NULL) {
265135993Semax		close(aslave);
266135993Semax		close(amaster);
267135993Semax	}
268135993Semax
269114878Sjulian	return (0);
270114878Sjulian}
271114878Sjulian
272114878Sjulian/* Open TTYs */
273114878Sjulianstatic int
274114878Sjuliansppd_ttys_open(char const *tty, int *amaster, int *aslave)
275114878Sjulian{
276123734Semax	char		 pty[PATH_MAX], *slash;
277114878Sjulian	struct group	*gr = NULL;
278114878Sjulian	gid_t		 ttygid;
279114878Sjulian	struct termios	 tio;
280114878Sjulian
281114878Sjulian	/*
282123676Semax	 * Construct master PTY name. The slave tty name must be less then
283123676Semax	 * PATH_MAX characters in length, must contain '/' character and
284123676Semax	 * must not end with '/'.
285114878Sjulian	 */
286114878Sjulian
287123676Semax	if (strlen(tty) >= sizeof(pty)) {
288123676Semax		syslog(LOG_ERR, "Slave tty name is too long");
289123676Semax		return (-1);
290123676Semax	}
291123676Semax
292114878Sjulian	strlcpy(pty, tty, sizeof(pty));
293123676Semax	slash = strrchr(pty, '/');
294123734Semax	if (slash == NULL || slash[1] == '\0') {
295123676Semax		syslog(LOG_ERR, "Invalid slave tty name (%s)", tty);
296123676Semax		return (-1);
297123676Semax	}
298114878Sjulian
299123676Semax	slash[1] = 'p';
300123676Semax
301114878Sjulian	if (strcmp(pty, tty) == 0) {
302114878Sjulian		syslog(LOG_ERR, "Master and slave tty are the same (%s)", tty);
303114878Sjulian		return (-1);
304114878Sjulian	}
305114878Sjulian
306114878Sjulian	if ((*amaster = open(pty, O_RDWR, 0)) < 0) {
307114878Sjulian		syslog(LOG_ERR, "Could not open(%s). %s", pty, strerror(errno));
308114878Sjulian		return (-1);
309114878Sjulian	}
310114878Sjulian
311114878Sjulian	/*
312114878Sjulian	 * Slave TTY
313114878Sjulian	 */
314114878Sjulian
315114878Sjulian	if ((gr = getgrnam("tty")) != NULL)
316114878Sjulian		ttygid = gr->gr_gid;
317114878Sjulian	else
318114878Sjulian		ttygid = -1;
319114878Sjulian
320114878Sjulian	(void) chown(tty, getuid(), ttygid);
321114878Sjulian	(void) chmod(tty, S_IRUSR|S_IWUSR|S_IWGRP);
322114878Sjulian	(void) revoke(tty);
323114878Sjulian
324114878Sjulian	if ((*aslave = open(tty, O_RDWR, 0)) < 0) {
325114878Sjulian		syslog(LOG_ERR, "Could not open(%s). %s", tty, strerror(errno));
326114878Sjulian		close(*amaster);
327114878Sjulian		return (-1);
328114878Sjulian	}
329114878Sjulian
330114878Sjulian	/*
331114878Sjulian	 * Make slave TTY raw
332114878Sjulian	 */
333114878Sjulian
334114878Sjulian	cfmakeraw(&tio);
335114878Sjulian
336114878Sjulian	if (tcsetattr(*aslave, TCSANOW, &tio) < 0) {
337114878Sjulian		syslog(LOG_ERR, "Could not tcsetattr(). %s", strerror(errno));
338114878Sjulian		close(*aslave);
339114878Sjulian		close(*amaster);
340114878Sjulian		return (-1);
341114878Sjulian	}
342114878Sjulian
343114878Sjulian	return (0);
344114878Sjulian} /* sppd_ttys_open */
345114878Sjulian
346114878Sjulian/* Read data */
347114878Sjulianstatic int
348114878Sjuliansppd_read(int fd, char *buffer, int size)
349114878Sjulian{
350114878Sjulian	int	n;
351114878Sjulian
352114878Sjulianagain:
353114878Sjulian	n = read(fd, buffer, size);
354114878Sjulian	if (n < 0) {
355114878Sjulian		if (errno == EINTR)
356114878Sjulian			goto again;
357114878Sjulian
358114878Sjulian		return (-1);
359114878Sjulian	}
360114878Sjulian
361114878Sjulian	return (n);
362114878Sjulian} /* sppd_read */
363114878Sjulian
364114878Sjulian/* Write data */
365114878Sjulianstatic int
366114878Sjuliansppd_write(int fd, char *buffer, int size)
367114878Sjulian{
368114878Sjulian	int	n, wrote;
369114878Sjulian
370114878Sjulian	for (wrote = 0; size > 0; ) {
371114878Sjulian		n = write(fd, buffer, size);
372114878Sjulian		switch (n) {
373114878Sjulian		case -1:
374114878Sjulian			if (errno != EINTR)
375114878Sjulian				return (-1);
376114878Sjulian			break;
377114878Sjulian
378114878Sjulian		case 0:
379114878Sjulian			/* XXX can happen? */
380114878Sjulian			break;
381114878Sjulian
382114878Sjulian		default:
383114878Sjulian			wrote += n;
384114878Sjulian			buffer += n;
385114878Sjulian			size -= n;
386114878Sjulian			break;
387114878Sjulian		}
388114878Sjulian	}
389114878Sjulian
390114878Sjulian	return (wrote);
391114878Sjulian} /* sppd_write */
392114878Sjulian
393114878Sjulian/* Signal handler */
394114878Sjulianstatic void
395114878Sjuliansppd_sighandler(int s)
396114878Sjulian{
397114878Sjulian	syslog(LOG_INFO, "Signal %d received. Total %d signals received\n",
398114878Sjulian			s, ++ done);
399114878Sjulian} /* sppd_sighandler */
400114878Sjulian
401114878Sjulian/* Display usage and exit */
402114878Sjulianstatic void
403114878Sjulianusage(void)
404114878Sjulian{
405114878Sjulian	fprintf(stdout,
406114878Sjulian"Usage: %s options\n" \
407114878Sjulian"Where options are:\n" \
408133178Semax"\t-a address Address to connect to (required)\n" \
409114878Sjulian"\t-b         Run in background\n" \
410121054Semax"\t-c channel RFCOMM channel to connect to\n" \
411135993Semax"\t-t tty     TTY name (required in background mode)\n" \
412114878Sjulian"\t-h         Display this message\n", SPPD_IDENT);
413114878Sjulian
414114878Sjulian	exit(255);
415114878Sjulian} /* usage */
416114878Sjulian
417