6 * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: rfcomm_sppd.c,v 1.4 2003/09/07 18:15:55 max Exp $
35 #include <bluetooth.h>
53 #define SPPD_IDENT "rfcomm_sppd"
54 #define SPPD_BUFFER_SIZE 1024
55 #define max(a, b) (((a) > (b))? (a) : (b))
57 int rfcomm_channel_lookup (bdaddr_t const *local,
58 bdaddr_t const *remote,
59 int service, int *channel, int *error);
61 static int sppd_ttys_open (char const *tty, int *amaster, int *aslave);
62 static int sppd_read (int fd, char *buffer, int size);
63 static int sppd_write (int fd, char *buffer, int size);
64 static void sppd_sighandler (int s);
65 static void usage (void);
67 static int done; /* are we done? */
71 main(int argc, char *argv[])
74 struct sockaddr_rfcomm ra;
76 int n, background, channel, service,
77 s, amaster, aslave, fd, doserver;
79 char *tty = NULL, *ep = NULL, buf[SPPD_BUFFER_SIZE];
81 memcpy(&addr, NG_HCI_BDADDR_ANY, sizeof(addr));
82 background = channel = 0;
83 service = SDP_SERVICE_CLASS_SERIAL_PORT;
86 /* Parse command line options */
87 while ((n = getopt(argc, argv, "a:bc:t:hS")) != -1) {
89 case 'a': /* BDADDR */
90 if (!bt_aton(optarg, &addr)) {
91 struct hostent *he = NULL;
93 if ((he = bt_gethostbyname(optarg)) == NULL)
94 errx(1, "%s: %s", optarg, hstrerror(h_errno));
96 memcpy(&addr, he->h_addr, sizeof(addr));
100 case 'c': /* RFCOMM channel */
101 channel = strtoul(optarg, &ep, 10);
104 switch (tolower(optarg[0])) {
105 case 'd': /* DialUp Networking */
106 service = SDP_SERVICE_CLASS_DIALUP_NETWORKING;
110 service = SDP_SERVICE_CLASS_FAX;
114 service = SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP;
117 case 's': /* Serial Port */
118 service = SDP_SERVICE_CLASS_SERIAL_PORT;
122 errx(1, "Unknown service name: %s",
129 case 'b': /* Run in background */
133 case 't': /* Slave TTY name */
134 if (optarg[0] != '/')
135 asprintf(&tty, "%s%s", _PATH_DEV, optarg);
151 /* Check if we have everything we need */
152 if (!doserver && memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) == 0)
156 /* Set signal handlers */
157 memset(&sa, 0, sizeof(sa));
158 sa.sa_handler = sppd_sighandler;
160 if (sigaction(SIGTERM, &sa, NULL) < 0)
161 err(1, "Could not sigaction(SIGTERM)");
163 if (sigaction(SIGHUP, &sa, NULL) < 0)
164 err(1, "Could not sigaction(SIGHUP)");
166 if (sigaction(SIGINT, &sa, NULL) < 0)
167 err(1, "Could not sigaction(SIGINT)");
169 sa.sa_handler = SIG_IGN;
170 sa.sa_flags = SA_NOCLDWAIT;
172 if (sigaction(SIGCHLD, &sa, NULL) < 0)
173 err(1, "Could not sigaction(SIGCHLD)");
180 amaster = STDIN_FILENO;
183 if (sppd_ttys_open(tty, &amaster, &aslave) < 0)
189 /* Open RFCOMM connection */
192 struct sockaddr_rfcomm ma;
193 bdaddr_t bt_addr_any;
197 int acceptsock, aaddrlen;
199 acceptsock = socket(PF_BLUETOOTH, SOCK_STREAM,
200 BLUETOOTH_PROTO_RFCOMM);
202 err(1, "Could not create socket");
204 memcpy(&bt_addr_any, NG_HCI_BDADDR_ANY, sizeof(bt_addr_any));
206 memset(&ma, 0, sizeof(ma));
207 ma.rfcomm_len = sizeof(ma);
208 ma.rfcomm_family = AF_BLUETOOTH;
209 memcpy(&ma.rfcomm_bdaddr, &bt_addr_any, sizeof(bt_addr_any));
210 ma.rfcomm_channel = channel;
212 if (bind(acceptsock, (struct sockaddr *)&ma, sizeof(ma)) < 0)
213 err(1, "Could not bind socket on channel %d", channel);
214 if (listen(acceptsock, 10) != 0)
215 err(1, "Could not listen on socket");
217 aaddrlen = sizeof(ma);
218 if (getsockname(acceptsock, (struct sockaddr *)&ma, &aaddrlen) < 0)
219 err(1, "Could not get socket name");
220 channel = ma.rfcomm_channel;
222 ss = sdp_open_local(NULL);
224 errx(1, "Unable to create local SDP session");
225 if (sdp_error(ss) != 0)
226 errx(1, "Unable to open local SDP session. %s (%d)",
227 strerror(sdp_error(ss)), sdp_error(ss));
228 memset(&sp, 0, sizeof(sp));
229 sp.server_channel = channel;
231 if (sdp_register_service(ss, SDP_SERVICE_CLASS_SERIAL_PORT,
232 &bt_addr_any, (void *)&sp, sizeof(sp),
234 errx(1, "Unable to register LAN service with "
235 "local SDP daemon. %s (%d)",
236 strerror(sdp_error(ss)), sdp_error(ss));
241 aaddrlen = sizeof(ra);
242 s = accept(acceptsock, (struct sockaddr *)&ra,
245 err(1, "Unable to accept()");
246 if (memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) &&
247 memcmp(&addr, &ra.rfcomm_bdaddr, sizeof(addr))) {
248 warnx("Connect from wrong client");
253 sdp_unregister_service(ss, sdp_handle);
257 /* Check channel, if was not set then obtain it via SDP */
258 if (channel == 0 && service != 0)
259 if (rfcomm_channel_lookup(NULL, &addr,
260 service, &channel, &n) != 0)
261 errc(1, n, "Could not obtain RFCOMM channel");
262 if (channel <= 0 || channel > 30)
263 errx(1, "Invalid RFCOMM channel number %d", channel);
265 s = socket(PF_BLUETOOTH, SOCK_STREAM, BLUETOOTH_PROTO_RFCOMM);
267 err(1, "Could not create socket");
269 memset(&ra, 0, sizeof(ra));
270 ra.rfcomm_len = sizeof(ra);
271 ra.rfcomm_family = AF_BLUETOOTH;
273 if (bind(s, (struct sockaddr *) &ra, sizeof(ra)) < 0)
274 err(1, "Could not bind socket");
276 memcpy(&ra.rfcomm_bdaddr, &addr, sizeof(ra.rfcomm_bdaddr));
277 ra.rfcomm_channel = channel;
279 if (connect(s, (struct sockaddr *) &ra, sizeof(ra)) < 0)
280 err(1, "Could not connect socket");
283 /* Became daemon if required */
284 if (background && daemon(0, 0) < 0)
285 err(1, "Could not daemon()");
287 openlog(SPPD_IDENT, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_DAEMON);
288 syslog(LOG_INFO, "Starting on %s...", (tty != NULL)? tty : "stdin/stdout");
290 for (done = 0; !done; ) {
292 FD_SET(amaster, &rfd);
295 n = select(max(amaster, s) + 1, &rfd, NULL, NULL, NULL);
300 syslog(LOG_ERR, "Could not select(). %s",
308 if (FD_ISSET(amaster, &rfd)) {
309 n = sppd_read(amaster, buf, sizeof(buf));
311 syslog(LOG_ERR, "Could not read master pty, " \
312 "fd=%d. %s", amaster, strerror(errno));
319 if (sppd_write(s, buf, n) < 0) {
320 syslog(LOG_ERR, "Could not write to socket, " \
321 "fd=%d, size=%d. %s",
322 s, n, strerror(errno));
327 if (FD_ISSET(s, &rfd)) {
328 n = sppd_read(s, buf, sizeof(buf));
330 syslog(LOG_ERR, "Could not read socket, " \
331 "fd=%d. %s", s, strerror(errno));
338 if (sppd_write(fd, buf, n) < 0) {
339 syslog(LOG_ERR, "Could not write to master " \
340 "pty, fd=%d, size=%d. %s",
341 fd, n, strerror(errno));
347 syslog(LOG_INFO, "Completed on %s", (tty != NULL)? tty : "stdin/stdout");
362 sppd_ttys_open(char const *tty, int *amaster, int *aslave)
364 char pty[PATH_MAX], *slash;
365 struct group *gr = NULL;
370 * Construct master PTY name. The slave tty name must be less then
371 * PATH_MAX characters in length, must contain '/' character and
372 * must not end with '/'.
375 if (strlen(tty) >= sizeof(pty)) {
376 syslog(LOG_ERR, "Slave tty name is too long");
380 strlcpy(pty, tty, sizeof(pty));
381 slash = strrchr(pty, '/');
382 if (slash == NULL || slash[1] == '\0') {
383 syslog(LOG_ERR, "Invalid slave tty name (%s)", tty);
389 if (strcmp(pty, tty) == 0) {
390 syslog(LOG_ERR, "Master and slave tty are the same (%s)", tty);
394 if ((*amaster = open(pty, O_RDWR, 0)) < 0) {
395 syslog(LOG_ERR, "Could not open(%s). %s", pty, strerror(errno));
403 if ((gr = getgrnam("tty")) != NULL)
408 (void) chown(tty, getuid(), ttygid);
409 (void) chmod(tty, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
412 if ((*aslave = open(tty, O_RDWR, 0)) < 0) {
413 syslog(LOG_ERR, "Could not open(%s). %s", tty, strerror(errno));
424 if (tcsetattr(*aslave, TCSANOW, &tio) < 0) {
425 syslog(LOG_ERR, "Could not tcsetattr(). %s", strerror(errno));
432 } /* sppd_ttys_open */
436 sppd_read(int fd, char *buffer, int size)
441 n = read(fd, buffer, size);
454 sppd_write(int fd, char *buffer, int size)
458 for (wrote = 0; size > 0; ) {
459 n = write(fd, buffer, size);
467 /* XXX can happen? */
483 sppd_sighandler(int s)
485 syslog(LOG_INFO, "Signal %d received. Total %d signals received\n",
487 } /* sppd_sighandler */
489 /* Display usage and exit */
494 "Usage: %s options\n" \
495 "Where options are:\n" \
496 "\t-a address Peer address (required in client mode)\n" \
497 "\t-b Run in background\n" \
498 "\t-c channel RFCOMM channel to connect to or listen on\n" \
499 "\t-t tty TTY name (required in background mode)\n" \
500 "\t-S Server mode\n" \
501 "\t-h Display this message\n", SPPD_IDENT);