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