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: releng/10.3/usr.bin/bluetooth/rfcomm_sppd/rfcomm_sppd.c 274182 2014-11-06 16:32:00Z emax $
32114878Sjulian */
33114878Sjulian
34114878Sjulian#include <sys/stat.h>
35274182Semax#include <sys/types.h>
36121054Semax#include <bluetooth.h>
37152704Semax#include <ctype.h>
38114878Sjulian#include <err.h>
39114878Sjulian#include <errno.h>
40114878Sjulian#include <fcntl.h>
41114878Sjulian#include <grp.h>
42114878Sjulian#include <limits.h>
43123676Semax#include <paths.h>
44121054Semax#include <sdp.h>
45114878Sjulian#include <signal.h>
46114878Sjulian#include <stdarg.h>
47114878Sjulian#include <stdio.h>
48114878Sjulian#include <stdlib.h>
49114878Sjulian#include <string.h>
50114878Sjulian#include <syslog.h>
51114878Sjulian#include <termios.h>
52114878Sjulian#include <unistd.h>
53274182Semax#include <libutil.h>
54114878Sjulian
55114878Sjulian#define SPPD_IDENT		"rfcomm_sppd"
56114878Sjulian#define SPPD_BUFFER_SIZE	1024
57114878Sjulian#define max(a, b)		(((a) > (b))? (a) : (b))
58114878Sjulian
59121054Semaxint		rfcomm_channel_lookup	(bdaddr_t const *local,
60121054Semax					 bdaddr_t const *remote,
61121054Semax					 int service, int *channel, int *error);
62121054Semax
63274182Semaxstatic int	sppd_ttys_open	(char **tty, int *amaster, int *aslave);
64114878Sjulianstatic int	sppd_read	(int fd, char *buffer, int size);
65114878Sjulianstatic int	sppd_write	(int fd, char *buffer, int size);
66114878Sjulianstatic void	sppd_sighandler	(int s);
67114878Sjulianstatic void	usage		(void);
68114878Sjulian
69114878Sjulianstatic int	done;	/* are we done? */
70114878Sjulian
71114878Sjulian/* Main */
72114878Sjulianint
73114878Sjulianmain(int argc, char *argv[])
74114878Sjulian{
75114878Sjulian	struct sigaction	 sa;
76114878Sjulian	struct sockaddr_rfcomm	 ra;
77114878Sjulian	bdaddr_t		 addr;
78152704Semax	int			 n, background, channel, service,
79274182Semax				 s, amaster, aslave, fd, doserver,
80274182Semax				 dopty;
81114878Sjulian	fd_set			 rfd;
82152704Semax	char			*tty = NULL, *ep = NULL, buf[SPPD_BUFFER_SIZE];
83114878Sjulian
84114878Sjulian	memcpy(&addr, NG_HCI_BDADDR_ANY, sizeof(addr));
85114878Sjulian	background = channel = 0;
86152704Semax	service = SDP_SERVICE_CLASS_SERIAL_PORT;
87166233Semax	doserver = 0;
88274182Semax	dopty = 0;
89114878Sjulian
90114878Sjulian	/* Parse command line options */
91274182Semax	while ((n = getopt(argc, argv, "a:bc:thS")) != -1) {
92114878Sjulian		switch (n) {
93121054Semax		case 'a': /* BDADDR */
94121054Semax			if (!bt_aton(optarg, &addr)) {
95121054Semax				struct hostent	*he = NULL;
96114878Sjulian
97121054Semax				if ((he = bt_gethostbyname(optarg)) == NULL)
98121054Semax					errx(1, "%s: %s", optarg, hstrerror(h_errno));
99114878Sjulian
100121054Semax				memcpy(&addr, he->h_addr, sizeof(addr));
101121054Semax			}
102121054Semax			break;
103114878Sjulian
104114878Sjulian		case 'c': /* RFCOMM channel */
105152704Semax			channel = strtoul(optarg, &ep, 10);
106152704Semax			if (*ep != '\0') {
107152704Semax				channel = 0;
108152704Semax				switch (tolower(optarg[0])) {
109152704Semax				case 'd': /* DialUp Networking */
110152704Semax					service = SDP_SERVICE_CLASS_DIALUP_NETWORKING;
111152704Semax					break;
112152704Semax
113152704Semax				case 'f': /* Fax */
114152704Semax					service = SDP_SERVICE_CLASS_FAX;
115152704Semax					break;
116152704Semax
117153209Semax				case 'l': /* LAN */
118153209Semax					service = SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP;
119153209Semax					break;
120153209Semax
121152704Semax				case 's': /* Serial Port */
122152704Semax					service = SDP_SERVICE_CLASS_SERIAL_PORT;
123152704Semax					break;
124152704Semax
125152704Semax				default:
126152704Semax					errx(1, "Unknown service name: %s",
127152704Semax						optarg);
128152704Semax					/* NOT REACHED */
129152704Semax				}
130152704Semax			}
131114878Sjulian			break;
132114878Sjulian
133114878Sjulian		case 'b': /* Run in background */
134114878Sjulian			background = 1;
135114878Sjulian			break;
136114878Sjulian
137274182Semax		case 't': /* Open pseudo TTY */
138274182Semax			dopty = 1;
139114878Sjulian			break;
140114878Sjulian
141166233Semax		case 'S':
142166233Semax			doserver = 1;
143166233Semax			break;
144166233Semax
145114878Sjulian		case 'h':
146114878Sjulian		default:
147114878Sjulian			usage();
148114878Sjulian			/* NOT REACHED */
149114878Sjulian		}
150114878Sjulian	}
151114878Sjulian
152114878Sjulian	/* Check if we have everything we need */
153166233Semax	if (!doserver && memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) == 0)
154114878Sjulian		usage();
155114878Sjulian		/* NOT REACHED */
156114878Sjulian
157114878Sjulian	/* Set signal handlers */
158114878Sjulian	memset(&sa, 0, sizeof(sa));
159114878Sjulian	sa.sa_handler = sppd_sighandler;
160114878Sjulian
161114878Sjulian	if (sigaction(SIGTERM, &sa, NULL) < 0)
162114878Sjulian		err(1, "Could not sigaction(SIGTERM)");
163114878Sjulian
164114878Sjulian	if (sigaction(SIGHUP, &sa, NULL) < 0)
165114878Sjulian		err(1, "Could not sigaction(SIGHUP)");
166114878Sjulian
167114878Sjulian	if (sigaction(SIGINT, &sa, NULL) < 0)
168114878Sjulian		err(1, "Could not sigaction(SIGINT)");
169114878Sjulian
170114878Sjulian	sa.sa_handler = SIG_IGN;
171114878Sjulian	sa.sa_flags = SA_NOCLDWAIT;
172114878Sjulian
173114878Sjulian	if (sigaction(SIGCHLD, &sa, NULL) < 0)
174114878Sjulian		err(1, "Could not sigaction(SIGCHLD)");
175114878Sjulian
176114878Sjulian	/* Open TTYs */
177274182Semax	if (dopty) {
178274182Semax		if (sppd_ttys_open(&tty, &amaster, &aslave) < 0)
179274182Semax			exit(1);
180274182Semax
181274182Semax		fd = amaster;
182274182Semax	} else {
183178992Semax		if (background)
184135993Semax			usage();
185114878Sjulian
186135993Semax		amaster = STDIN_FILENO;
187135993Semax		fd = STDOUT_FILENO;
188274182Semax	}
189135993Semax
190114878Sjulian	/* Open RFCOMM connection */
191114878Sjulian
192166233Semax	if (doserver) {
193166233Semax		struct sockaddr_rfcomm	 ma;
194166233Semax		bdaddr_t		 bt_addr_any;
195178992Semax		sdp_sp_profile_t	 sp;
196166233Semax		void			*ss;
197166233Semax		uint32_t		 sdp_handle;
198166233Semax		int			 acceptsock, aaddrlen;
199114878Sjulian
200166233Semax		acceptsock = socket(PF_BLUETOOTH, SOCK_STREAM,
201178992Semax					BLUETOOTH_PROTO_RFCOMM);
202166233Semax		if (acceptsock < 0)
203166233Semax			err(1, "Could not create socket");
204114878Sjulian
205178992Semax		memcpy(&bt_addr_any, NG_HCI_BDADDR_ANY, sizeof(bt_addr_any));
206178992Semax
207166233Semax		memset(&ma, 0, sizeof(ma));
208166233Semax		ma.rfcomm_len = sizeof(ma);
209166233Semax		ma.rfcomm_family = AF_BLUETOOTH;
210178992Semax		memcpy(&ma.rfcomm_bdaddr, &bt_addr_any, sizeof(bt_addr_any));
211166233Semax		ma.rfcomm_channel = channel;
212114878Sjulian
213166233Semax		if (bind(acceptsock, (struct sockaddr *)&ma, sizeof(ma)) < 0)
214178992Semax			err(1, "Could not bind socket on channel %d", channel);
215172142Skevlo		if (listen(acceptsock, 10) != 0)
216172142Skevlo			err(1, "Could not listen on socket");
217114878Sjulian
218178992Semax		aaddrlen = sizeof(ma);
219178992Semax		if (getsockname(acceptsock, (struct sockaddr *)&ma, &aaddrlen) < 0)
220178992Semax			err(1, "Could not get socket name");
221178992Semax		channel = ma.rfcomm_channel;
222178992Semax
223166233Semax		ss = sdp_open_local(NULL);
224166233Semax		if (ss == NULL)
225166233Semax			errx(1, "Unable to create local SDP session");
226166233Semax		if (sdp_error(ss) != 0)
227166233Semax			errx(1, "Unable to open local SDP session. %s (%d)",
228166233Semax			    strerror(sdp_error(ss)), sdp_error(ss));
229178992Semax		memset(&sp, 0, sizeof(sp));
230178992Semax		sp.server_channel = channel;
231166233Semax
232178992Semax		if (sdp_register_service(ss, SDP_SERVICE_CLASS_SERIAL_PORT,
233178992Semax				&bt_addr_any, (void *)&sp, sizeof(sp),
234178992Semax				&sdp_handle) != 0) {
235166233Semax			errx(1, "Unable to register LAN service with "
236166233Semax			    "local SDP daemon. %s (%d)",
237166233Semax			    strerror(sdp_error(ss)), sdp_error(ss));
238166233Semax		}
239166233Semax
240166233Semax		s = -1;
241166233Semax		while (s < 0) {
242166233Semax			aaddrlen = sizeof(ra);
243166233Semax			s = accept(acceptsock, (struct sockaddr *)&ra,
244166233Semax			    &aaddrlen);
245166233Semax			if (s < 0)
246166233Semax				err(1, "Unable to accept()");
247166233Semax			if (memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) &&
248166233Semax			    memcmp(&addr, &ra.rfcomm_bdaddr, sizeof(addr))) {
249166233Semax				warnx("Connect from wrong client");
250166233Semax				close(s);
251166233Semax				s = -1;
252166233Semax			}
253166233Semax		}
254166233Semax		sdp_unregister_service(ss, sdp_handle);
255166233Semax		sdp_close(ss);
256166233Semax		close(acceptsock);
257166233Semax	} else {
258166233Semax		/* Check channel, if was not set then obtain it via SDP */
259166233Semax		if (channel == 0 && service != 0)
260166233Semax			if (rfcomm_channel_lookup(NULL, &addr,
261166233Semax				    service, &channel, &n) != 0)
262166233Semax				errc(1, n, "Could not obtain RFCOMM channel");
263166233Semax		if (channel <= 0 || channel > 30)
264166233Semax			errx(1, "Invalid RFCOMM channel number %d", channel);
265166233Semax
266166233Semax		s = socket(PF_BLUETOOTH, SOCK_STREAM, BLUETOOTH_PROTO_RFCOMM);
267166233Semax		if (s < 0)
268166233Semax			err(1, "Could not create socket");
269166233Semax
270166233Semax		memset(&ra, 0, sizeof(ra));
271166233Semax		ra.rfcomm_len = sizeof(ra);
272166233Semax		ra.rfcomm_family = AF_BLUETOOTH;
273166233Semax
274166233Semax		if (bind(s, (struct sockaddr *) &ra, sizeof(ra)) < 0)
275166233Semax			err(1, "Could not bind socket");
276166233Semax
277166233Semax		memcpy(&ra.rfcomm_bdaddr, &addr, sizeof(ra.rfcomm_bdaddr));
278166233Semax		ra.rfcomm_channel = channel;
279166233Semax
280166233Semax		if (connect(s, (struct sockaddr *) &ra, sizeof(ra)) < 0)
281166233Semax			err(1, "Could not connect socket");
282166233Semax	}
283166233Semax
284114878Sjulian	/* Became daemon if required */
285188130Semax	if (background && daemon(0, 0) < 0)
286188130Semax		err(1, "Could not daemon()");
287114878Sjulian
288114878Sjulian	openlog(SPPD_IDENT, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_DAEMON);
289135993Semax	syslog(LOG_INFO, "Starting on %s...", (tty != NULL)? tty : "stdin/stdout");
290114878Sjulian
291274182Semax	/* Print used tty on stdout for wrappers to pick up */
292274182Semax	if (!background)
293274182Semax		fprintf(stdout, "%s\n", tty);
294274182Semax
295114878Sjulian	for (done = 0; !done; ) {
296114878Sjulian		FD_ZERO(&rfd);
297114878Sjulian		FD_SET(amaster, &rfd);
298114878Sjulian		FD_SET(s, &rfd);
299114878Sjulian
300114878Sjulian		n = select(max(amaster, s) + 1, &rfd, NULL, NULL, NULL);
301114878Sjulian		if (n < 0) {
302114878Sjulian			if (errno == EINTR)
303114878Sjulian				continue;
304114878Sjulian
305114878Sjulian			syslog(LOG_ERR, "Could not select(). %s",
306114878Sjulian					strerror(errno));
307114878Sjulian			exit(1);
308114878Sjulian		}
309114878Sjulian
310114878Sjulian		if (n == 0)
311114878Sjulian			continue;
312114878Sjulian
313114878Sjulian		if (FD_ISSET(amaster, &rfd)) {
314114878Sjulian			n = sppd_read(amaster, buf, sizeof(buf));
315114878Sjulian			if (n < 0) {
316114878Sjulian				syslog(LOG_ERR, "Could not read master pty, " \
317114878Sjulian					"fd=%d. %s", amaster, strerror(errno));
318114878Sjulian				exit(1);
319114878Sjulian			}
320114878Sjulian
321114878Sjulian			if (n == 0)
322114878Sjulian				break; /* XXX */
323114878Sjulian
324114878Sjulian			if (sppd_write(s, buf, n) < 0) {
325114878Sjulian				syslog(LOG_ERR, "Could not write to socket, " \
326114878Sjulian					"fd=%d, size=%d. %s",
327114878Sjulian					s, n, strerror(errno));
328114878Sjulian				exit(1);
329114878Sjulian			}
330114878Sjulian		}
331114878Sjulian
332114878Sjulian		if (FD_ISSET(s, &rfd)) {
333114878Sjulian			n = sppd_read(s, buf, sizeof(buf));
334114878Sjulian			if (n < 0) {
335114878Sjulian				syslog(LOG_ERR, "Could not read socket, " \
336114878Sjulian					"fd=%d. %s", s, strerror(errno));
337114878Sjulian				exit(1);
338114878Sjulian			}
339114878Sjulian
340114878Sjulian			if (n == 0)
341114878Sjulian				break;
342114878Sjulian
343135993Semax			if (sppd_write(fd, buf, n) < 0) {
344114878Sjulian				syslog(LOG_ERR, "Could not write to master " \
345114878Sjulian					"pty, fd=%d, size=%d. %s",
346135993Semax					fd, n, strerror(errno));
347114878Sjulian				exit(1);
348114878Sjulian			}
349114878Sjulian		}
350114878Sjulian	}
351114878Sjulian
352135993Semax	syslog(LOG_INFO, "Completed on %s", (tty != NULL)? tty : "stdin/stdout");
353114878Sjulian	closelog();
354114878Sjulian
355114878Sjulian	close(s);
356114878Sjulian
357135993Semax	if (tty != NULL) {
358135993Semax		close(aslave);
359135993Semax		close(amaster);
360135993Semax	}
361135993Semax
362114878Sjulian	return (0);
363114878Sjulian}
364114878Sjulian
365114878Sjulian/* Open TTYs */
366114878Sjulianstatic int
367274182Semaxsppd_ttys_open(char **tty, int *amaster, int *aslave)
368114878Sjulian{
369274182Semax	char		 pty[PATH_MAX];
370114878Sjulian	struct termios	 tio;
371114878Sjulian
372274182Semax	cfmakeraw(&tio);
373114878Sjulian
374274182Semax	if (openpty(amaster, aslave, pty, &tio, NULL) == -1) {
375274182Semax		syslog(LOG_ERR, "Could not openpty(). %s", strerror(errno));
376123676Semax		return (-1);
377123676Semax	}
378123676Semax
379274182Semax	if ((*tty = strdup(pty)) == NULL) {
380274182Semax		syslog(LOG_ERR, "Could not strdup(). %s", strerror(errno));
381114878Sjulian		close(*aslave);
382114878Sjulian		close(*amaster);
383114878Sjulian		return (-1);
384114878Sjulian	}
385114878Sjulian
386114878Sjulian	return (0);
387114878Sjulian} /* sppd_ttys_open */
388114878Sjulian
389114878Sjulian/* Read data */
390114878Sjulianstatic int
391114878Sjuliansppd_read(int fd, char *buffer, int size)
392114878Sjulian{
393114878Sjulian	int	n;
394114878Sjulian
395114878Sjulianagain:
396114878Sjulian	n = read(fd, buffer, size);
397114878Sjulian	if (n < 0) {
398114878Sjulian		if (errno == EINTR)
399114878Sjulian			goto again;
400114878Sjulian
401114878Sjulian		return (-1);
402114878Sjulian	}
403114878Sjulian
404114878Sjulian	return (n);
405114878Sjulian} /* sppd_read */
406114878Sjulian
407114878Sjulian/* Write data */
408114878Sjulianstatic int
409114878Sjuliansppd_write(int fd, char *buffer, int size)
410114878Sjulian{
411114878Sjulian	int	n, wrote;
412114878Sjulian
413114878Sjulian	for (wrote = 0; size > 0; ) {
414114878Sjulian		n = write(fd, buffer, size);
415114878Sjulian		switch (n) {
416114878Sjulian		case -1:
417114878Sjulian			if (errno != EINTR)
418114878Sjulian				return (-1);
419114878Sjulian			break;
420114878Sjulian
421114878Sjulian		case 0:
422114878Sjulian			/* XXX can happen? */
423114878Sjulian			break;
424114878Sjulian
425114878Sjulian		default:
426114878Sjulian			wrote += n;
427114878Sjulian			buffer += n;
428114878Sjulian			size -= n;
429114878Sjulian			break;
430114878Sjulian		}
431114878Sjulian	}
432114878Sjulian
433114878Sjulian	return (wrote);
434114878Sjulian} /* sppd_write */
435114878Sjulian
436114878Sjulian/* Signal handler */
437114878Sjulianstatic void
438114878Sjuliansppd_sighandler(int s)
439114878Sjulian{
440114878Sjulian	syslog(LOG_INFO, "Signal %d received. Total %d signals received\n",
441114878Sjulian			s, ++ done);
442114878Sjulian} /* sppd_sighandler */
443114878Sjulian
444114878Sjulian/* Display usage and exit */
445114878Sjulianstatic void
446114878Sjulianusage(void)
447114878Sjulian{
448114878Sjulian	fprintf(stdout,
449114878Sjulian"Usage: %s options\n" \
450114878Sjulian"Where options are:\n" \
451166233Semax"\t-a address Peer address (required in client mode)\n" \
452114878Sjulian"\t-b         Run in background\n" \
453166233Semax"\t-c channel RFCOMM channel to connect to or listen on\n" \
454274182Semax"\t-t         use slave pseudo tty (required in background mode)\n" \
455166233Semax"\t-S         Server mode\n" \
456114878Sjulian"\t-h         Display this message\n", SPPD_IDENT);
457114878Sjulian	exit(255);
458114878Sjulian} /* usage */
459114878Sjulian
460