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