rfcomm_sppd.c revision 1.5 1 /* $NetBSD: rfcomm_sppd.c,v 1.5 2007/03/08 19:13:14 plunky Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Itronix Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of Itronix Inc. may not be used to endorse
16 * or promote products derived from this software without specific
17 * prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 /*
32 * rfcomm_sppd.c
33 *
34 * Copyright (c) 2007 Iain Hibbert
35 * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin (at) yahoo.com>
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 */
59
60 #include <sys/cdefs.h>
61 __COPYRIGHT("@(#) Copyright (c) 2007 Iain Hibbert\n"
62 "@(#) Copyright (c) 2006 Itronix, Inc.\n"
63 "@(#) Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin (at) yahoo.com>\n"
64 "All rights reserved.\n");
65 __RCSID("$NetBSD: rfcomm_sppd.c,v 1.5 2007/03/08 19:13:14 plunky Exp $");
66
67 #include <bluetooth.h>
68 #include <ctype.h>
69 #include <err.h>
70 #include <errno.h>
71 #include <fcntl.h>
72 #include <grp.h>
73 #include <limits.h>
74 #include <paths.h>
75 #include <sdp.h>
76 #include <signal.h>
77 #include <stdarg.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <syslog.h>
82 #include <termios.h>
83 #include <unistd.h>
84
85 #include "rfcomm_sdp.h"
86
87 #define max(a, b) ((a) > (b) ? (a) : (b))
88
89 int open_tty(const char *);
90 int open_client(bdaddr_t *, bdaddr_t *, const char *);
91 int open_server(bdaddr_t *, uint8_t, const char *);
92 void copy_data(int, int);
93 void sighandler(int);
94 void usage(void);
95 void reset_tio(void);
96
97 int done; /* got a signal */
98 struct termios tio; /* stored termios for reset on exit */
99
100 struct service {
101 const char *name;
102 const char *description;
103 uint16_t class;
104 int pdulen;
105 } services[] = {
106 { "DUN", "Dialup Networking",
107 SDP_SERVICE_CLASS_DIALUP_NETWORKING,
108 sizeof(struct sdp_dun_profile)
109 },
110 { "LAN", "Lan access using PPP",
111 SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP,
112 sizeof(struct sdp_lan_profile)
113 },
114 { "SP", "Serial Port",
115 SDP_SERVICE_CLASS_SERIAL_PORT,
116 sizeof(struct sdp_sp_profile)
117 },
118 { NULL }
119 };
120
121 int
122 main(int argc, char *argv[])
123 {
124 struct termios t;
125 bdaddr_t laddr, raddr;
126 fd_set rdset;
127 char *ep, *service, *tty;
128 int n, rfcomm, tty_in, tty_out;
129 uint8_t channel;
130
131 bdaddr_copy(&laddr, BDADDR_ANY);
132 bdaddr_copy(&raddr, BDADDR_ANY);
133 service = "SP";
134 tty = NULL;
135 channel = 0;
136
137 /* Parse command line options */
138 while ((n = getopt(argc, argv, "a:c:d:hs:t:")) != -1) {
139 switch (n) {
140 case 'a': /* remote device address */
141 if (!bt_aton(optarg, &raddr)) {
142 struct hostent *he = NULL;
143
144 if ((he = bt_gethostbyname(optarg)) == NULL)
145 errx(EXIT_FAILURE, "%s: %s", optarg,
146 hstrerror(h_errno));
147
148 bdaddr_copy(&raddr, (bdaddr_t *)he->h_addr);
149 }
150 break;
151
152 case 'c': /* RFCOMM channel */
153 channel = strtoul(optarg, &ep, 10);
154 if (*ep != '\0' || channel < 1 || channel > 30)
155 errx(EXIT_FAILURE, "Invalid channel: %s", optarg);
156
157 break;
158
159 case 'd': /* local device address */
160 if (!bt_devaddr(optarg, &laddr))
161 err(EXIT_FAILURE, "%s", optarg);
162
163 break;
164
165 case 's': /* service class */
166 service = optarg;
167 break;
168
169 case 't': /* Slave TTY name */
170 if (optarg[0] != '/')
171 asprintf(&tty, "%s%s", _PATH_DEV, optarg);
172 else
173 tty = optarg;
174
175 break;
176
177 case 'h':
178 default:
179 usage();
180 /* NOT REACHED */
181 }
182 }
183
184 /*
185 * validate options:
186 * must have channel or remote address but not both
187 */
188 if ((channel == 0 && bdaddr_any(&raddr))
189 || (channel != 0 && !bdaddr_any(&raddr)))
190 usage();
191
192 /*
193 * grab ttys before we start the bluetooth
194 */
195 if (tty == NULL) {
196 tty_in = STDIN_FILENO;
197 tty_out = STDOUT_FILENO;
198 } else {
199 tty_in = open_tty(tty);
200 tty_out = tty_in;
201 }
202
203 /* open RFCOMM */
204 if (channel == 0)
205 rfcomm = open_client(&laddr, &raddr, service);
206 else
207 rfcomm = open_server(&laddr, channel, service);
208
209 /*
210 * now we are ready to go, so either detach or maybe turn
211 * off some input processing, so that rfcomm_sppd can
212 * be used directly with stdio
213 */
214 if (tty == NULL) {
215 if (tcgetattr(tty_in, &t) < 0)
216 err(EXIT_FAILURE, "tcgetattr");
217
218 memcpy(&tio, &t, sizeof(tio));
219 t.c_lflag &= ~(ECHO | ICANON);
220 t.c_iflag &= ~(ICRNL);
221
222 if (memcmp(&tio, &t, sizeof(tio))) {
223 if (tcsetattr(tty_in, TCSANOW, &t) < 0)
224 err(EXIT_FAILURE, "tcsetattr");
225
226 atexit(reset_tio);
227 }
228 } else {
229 if (daemon(0, 0) < 0)
230 err(EXIT_FAILURE, "daemon() failed");
231 }
232
233 /* catch signals */
234 done = 0;
235 (void)signal(SIGHUP, sighandler);
236 (void)signal(SIGINT, sighandler);
237 (void)signal(SIGPIPE, sighandler);
238 (void)signal(SIGTERM, sighandler);
239
240 openlog(getprogname(), LOG_PERROR | LOG_PID, LOG_DAEMON);
241 syslog(LOG_INFO, "Starting on %s...", (tty ? tty : "stdio"));
242
243 n = max(tty_in, rfcomm) + 1;
244 while (!done) {
245 FD_ZERO(&rdset);
246 FD_SET(tty_in, &rdset);
247 FD_SET(rfcomm, &rdset);
248
249 if (select(n, &rdset, NULL, NULL, NULL) < 0) {
250 if (errno == EINTR)
251 continue;
252
253 syslog(LOG_ERR, "select error: %m");
254 exit(EXIT_FAILURE);
255 }
256
257 if (FD_ISSET(tty_in, &rdset))
258 copy_data(tty_in, rfcomm);
259
260 if (FD_ISSET(rfcomm, &rdset))
261 copy_data(rfcomm, tty_out);
262 }
263
264 syslog(LOG_INFO, "Completed on %s", (tty ? tty : "stdio"));
265 exit(EXIT_SUCCESS);
266 }
267
268 int
269 open_tty(const char *tty)
270 {
271 char pty[PATH_MAX], *slash;
272 struct group *gr = NULL;
273 gid_t ttygid;
274 int master;
275
276 /*
277 * Construct master PTY name. The slave tty name must be less then
278 * PATH_MAX characters in length, must contain '/' character and
279 * must not end with '/'.
280 */
281 if (strlen(tty) >= sizeof(pty))
282 errx(EXIT_FAILURE, ": tty name too long");
283
284 strlcpy(pty, tty, sizeof(pty));
285 slash = strrchr(pty, '/');
286 if (slash == NULL || slash[1] == '\0')
287 errx(EXIT_FAILURE, "%s: invalid tty", tty);
288
289 slash[1] = 'p';
290 if (strcmp(pty, tty) == 0)
291 errx(EXIT_FAILURE, "Master and slave tty are the same (%s)", tty);
292
293 if ((master = open(pty, O_RDWR, 0)) < 0)
294 err(EXIT_FAILURE, "%s", pty);
295
296 /*
297 * Slave TTY
298 */
299
300 if ((gr = getgrnam("tty")) != NULL)
301 ttygid = gr->gr_gid;
302 else
303 ttygid = (gid_t)-1;
304
305 (void)chown(tty, getuid(), ttygid);
306 (void)chmod(tty, S_IRUSR | S_IWUSR | S_IWGRP);
307 (void)revoke(tty);
308
309 return master;
310 }
311
312 int
313 open_client(bdaddr_t *laddr, bdaddr_t *raddr, const char *service)
314 {
315 struct sockaddr_bt sa;
316 struct service *s;
317 struct linger l;
318 char *ep;
319 int fd;
320 uint8_t channel;
321
322 for (s = services ; ; s++) {
323 if (s->name == NULL) {
324 channel = strtoul(service, &ep, 10);
325 if (*ep != '\0' || channel < 1 || channel > 30)
326 errx(EXIT_FAILURE, "Invalid service: %s", service);
327
328 break;
329 }
330
331 if (strcasecmp(s->name, service) == 0) {
332 if (rfcomm_channel_lookup(laddr, raddr, s->class, &channel, &errno) < 0)
333 err(EXIT_FAILURE, "%s", s->name);
334
335 break;
336 }
337 }
338
339 memset(&sa, 0, sizeof(sa));
340 sa.bt_len = sizeof(sa);
341 sa.bt_family = AF_BLUETOOTH;
342 bdaddr_copy(&sa.bt_bdaddr, laddr);
343
344 fd = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
345 if (fd < 0)
346 err(EXIT_FAILURE, "socket()");
347
348 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
349 err(EXIT_FAILURE, "bind(%s)", bt_ntoa(laddr, NULL));
350
351 memset(&l, 0, sizeof(l));
352 l.l_onoff = 1;
353 l.l_linger = 5;
354 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) < 0)
355 err(EXIT_FAILURE, "linger()");
356
357 sa.bt_channel = channel;
358 bdaddr_copy(&sa.bt_bdaddr, raddr);
359
360 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
361 err(EXIT_FAILURE, "connect(%s, %d)", bt_ntoa(raddr, NULL),
362 channel);
363
364 return fd;
365 }
366
367 /*
368 * In all the profiles we currently support registering, the channel
369 * is the first octet in the PDU, and it seems all the rest can be
370 * zero, so we just use an array of uint8_t big enough to store the
371 * largest, currently LAN. See <sdp.h> for definitions..
372 */
373 #define pdu_len sizeof(struct sdp_lan_profile)
374
375 int
376 open_server(bdaddr_t *laddr, uint8_t channel, const char *service)
377 {
378 struct sockaddr_bt sa;
379 struct linger l;
380 socklen_t len;
381 void *ss;
382 int sv, fd, n;
383 uint8_t pdu[pdu_len];
384
385 memset(&sa, 0, sizeof(sa));
386 sa.bt_len = sizeof(sa);
387 sa.bt_family = AF_BLUETOOTH;
388 bdaddr_copy(&sa.bt_bdaddr, laddr);
389 sa.bt_channel = channel;
390
391 sv = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
392 if (sv < 0)
393 err(EXIT_FAILURE, "socket()");
394
395 if (bind(sv, (struct sockaddr *)&sa, sizeof(sa)) < 0)
396 err(EXIT_FAILURE, "bind(%s, %d)", bt_ntoa(laddr, NULL),
397 channel);
398
399 if (listen(sv, 1) < 0)
400 err(EXIT_FAILURE, "listen()");
401
402 /* Register service with SDP server */
403 for (n = 0 ; ; n++) {
404 if (services[n].name == NULL)
405 usage();
406
407 if (strcasecmp(services[n].name, service) == 0)
408 break;
409 }
410
411 memset(pdu, 0, pdu_len);
412 pdu[0] = channel;
413
414 ss = sdp_open_local(NULL);
415 if (ss == NULL || (errno = sdp_error(ss)) != 0)
416 err(EXIT_FAILURE, "sdp_open_local");
417
418 if (sdp_register_service(ss, services[n].class, laddr,
419 pdu, services[n].pdulen, NULL) != 0) {
420 errno = sdp_error(ss);
421 err(EXIT_FAILURE, "sdp_register_service");
422 }
423
424 len = sizeof(sa);
425 fd = accept(sv, (struct sockaddr *)&sa, &len);
426 if (fd < 0)
427 err(EXIT_FAILURE, "accept");
428
429 memset(&l, 0, sizeof(l));
430 l.l_onoff = 1;
431 l.l_linger = 5;
432 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) < 0)
433 err(EXIT_FAILURE, "linger()");
434
435 close(sv);
436 return fd;
437 }
438
439 void
440 copy_data(int src, int dst)
441 {
442 static char buf[BUFSIZ];
443 ssize_t nr, nw, off;
444
445 while ((nr = read(src, buf, sizeof(buf))) == -1) {
446 if (errno != EINTR) {
447 syslog(LOG_ERR, "read failed: %m");
448 exit(EXIT_FAILURE);
449 }
450 }
451
452 if (nr == 0) /* reached EOF */
453 done++;
454
455 for (off = 0 ; nr ; nr -= nw, off += nw) {
456 if ((nw = write(dst, buf + off, (size_t)nr)) == -1) {
457 syslog(LOG_ERR, "write failed: %m");
458 exit(EXIT_FAILURE);
459 }
460 }
461 }
462
463 void
464 sighandler(int s)
465 {
466
467 done++;
468 }
469
470 void
471 reset_tio(void)
472 {
473
474 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio);
475 }
476
477 void
478 usage(void)
479 {
480 struct service *s;
481
482 fprintf(stderr, "Usage: %s [-d device] [-s service] [-t tty] -a bdaddr | -c channel\n"
483 "\n"
484 "Where:\n"
485 "\t-a bdaddr remote device address\n"
486 "\t-c channel local RFCOMM channel\n"
487 "\t-d device local device address\n"
488 "\t-s service service class\n"
489 "\t-t tty run in background using pty\n"
490 "\n", getprogname());
491
492 fprintf(stderr, "Known service classes:\n");
493 for (s = services ; s->name != NULL ; s++)
494 fprintf(stderr, "\t%-13s%s\n", s->name, s->description);
495
496 exit(EXIT_FAILURE);
497 }
498