rfcomm_sppd.c revision 1.10 1 1.10 plunky /* $NetBSD: rfcomm_sppd.c,v 1.10 2009/05/12 18:43:35 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.10 plunky * Copyright (c) 2009 The NetBSD Foundation, Inc.
33 1.2 plunky * Copyright (c) 2007 Iain Hibbert
34 1.1 gdamore * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin (at) yahoo.com>
35 1.1 gdamore * All rights reserved.
36 1.1 gdamore *
37 1.1 gdamore * Redistribution and use in source and binary forms, with or without
38 1.1 gdamore * modification, are permitted provided that the following conditions
39 1.1 gdamore * are met:
40 1.1 gdamore * 1. Redistributions of source code must retain the above copyright
41 1.1 gdamore * notice, this list of conditions and the following disclaimer.
42 1.1 gdamore * 2. Redistributions in binary form must reproduce the above copyright
43 1.1 gdamore * notice, this list of conditions and the following disclaimer in the
44 1.1 gdamore * documentation and/or other materials provided with the distribution.
45 1.1 gdamore *
46 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47 1.1 gdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 1.1 gdamore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 1.1 gdamore * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50 1.1 gdamore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 1.1 gdamore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 1.1 gdamore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 1.1 gdamore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 1.1 gdamore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 1.1 gdamore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 1.1 gdamore * SUCH DAMAGE.
57 1.1 gdamore */
58 1.1 gdamore
59 1.1 gdamore #include <sys/cdefs.h>
60 1.10 plunky __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc.\
61 1.10 plunky Copyright (c) 2007 Iain Hibbert.\
62 1.9 lukem Copyright (c) 2006 Itronix, Inc.\
63 1.9 lukem Copyright (c) 2003 Maksim Yevmenkin m_evmenkin (at) yahoo.com.\
64 1.9 lukem All rights reserved.");
65 1.10 plunky __RCSID("$NetBSD: rfcomm_sppd.c,v 1.10 2009/05/12 18:43:35 plunky Exp $");
66 1.1 gdamore
67 1.1 gdamore #include <bluetooth.h>
68 1.1 gdamore #include <ctype.h>
69 1.1 gdamore #include <err.h>
70 1.1 gdamore #include <errno.h>
71 1.1 gdamore #include <fcntl.h>
72 1.1 gdamore #include <grp.h>
73 1.1 gdamore #include <limits.h>
74 1.1 gdamore #include <paths.h>
75 1.1 gdamore #include <sdp.h>
76 1.1 gdamore #include <signal.h>
77 1.1 gdamore #include <stdarg.h>
78 1.1 gdamore #include <stdio.h>
79 1.1 gdamore #include <stdlib.h>
80 1.1 gdamore #include <string.h>
81 1.1 gdamore #include <syslog.h>
82 1.1 gdamore #include <termios.h>
83 1.1 gdamore #include <unistd.h>
84 1.1 gdamore
85 1.7 plunky #include <netbt/rfcomm.h>
86 1.7 plunky
87 1.2 plunky #define max(a, b) ((a) > (b) ? (a) : (b))
88 1.1 gdamore
89 1.2 plunky int open_tty(const char *);
90 1.7 plunky int open_client(bdaddr_t *, bdaddr_t *, int, const char *);
91 1.7 plunky int open_server(bdaddr_t *, uint8_t, int, const char *);
92 1.2 plunky void copy_data(int, int);
93 1.10 plunky int channel_lookup(const bdaddr_t *, const bdaddr_t *, uint16_t, uintmax_t *);
94 1.2 plunky void sighandler(int);
95 1.2 plunky void usage(void);
96 1.2 plunky void reset_tio(void);
97 1.2 plunky
98 1.2 plunky int done; /* got a signal */
99 1.2 plunky struct termios tio; /* stored termios for reset on exit */
100 1.2 plunky
101 1.2 plunky struct service {
102 1.10 plunky const char * name;
103 1.10 plunky const char * description;
104 1.2 plunky uint16_t class;
105 1.2 plunky } services[] = {
106 1.2 plunky { "DUN", "Dialup Networking",
107 1.10 plunky SDP_SERVICE_CLASS_DIALUP_NETWORKING },
108 1.10 plunky { "LAN", "LAN access using PPP",
109 1.10 plunky SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP },
110 1.2 plunky { "SP", "Serial Port",
111 1.10 plunky SDP_SERVICE_CLASS_SERIAL_PORT },
112 1.10 plunky { NULL, NULL, 0 }
113 1.2 plunky };
114 1.1 gdamore
115 1.1 gdamore int
116 1.1 gdamore main(int argc, char *argv[])
117 1.1 gdamore {
118 1.2 plunky struct termios t;
119 1.2 plunky bdaddr_t laddr, raddr;
120 1.2 plunky fd_set rdset;
121 1.6 plunky const char *service;
122 1.6 plunky char *ep, *tty;
123 1.7 plunky int lm, n, rfcomm, tty_in, tty_out;
124 1.2 plunky uint8_t channel;
125 1.1 gdamore
126 1.1 gdamore bdaddr_copy(&laddr, BDADDR_ANY);
127 1.1 gdamore bdaddr_copy(&raddr, BDADDR_ANY);
128 1.3 plunky service = "SP";
129 1.3 plunky tty = NULL;
130 1.2 plunky channel = 0;
131 1.7 plunky lm = 0;
132 1.1 gdamore
133 1.1 gdamore /* Parse command line options */
134 1.7 plunky while ((n = getopt(argc, argv, "a:c:d:hm:s:t:")) != -1) {
135 1.1 gdamore switch (n) {
136 1.2 plunky case 'a': /* remote device address */
137 1.1 gdamore if (!bt_aton(optarg, &raddr)) {
138 1.1 gdamore struct hostent *he = NULL;
139 1.1 gdamore
140 1.1 gdamore if ((he = bt_gethostbyname(optarg)) == NULL)
141 1.2 plunky errx(EXIT_FAILURE, "%s: %s", optarg,
142 1.2 plunky hstrerror(h_errno));
143 1.1 gdamore
144 1.1 gdamore bdaddr_copy(&raddr, (bdaddr_t *)he->h_addr);
145 1.1 gdamore }
146 1.1 gdamore break;
147 1.1 gdamore
148 1.1 gdamore case 'c': /* RFCOMM channel */
149 1.1 gdamore channel = strtoul(optarg, &ep, 10);
150 1.3 plunky if (*ep != '\0' || channel < 1 || channel > 30)
151 1.3 plunky errx(EXIT_FAILURE, "Invalid channel: %s", optarg);
152 1.3 plunky
153 1.1 gdamore break;
154 1.1 gdamore
155 1.2 plunky case 'd': /* local device address */
156 1.2 plunky if (!bt_devaddr(optarg, &laddr))
157 1.2 plunky err(EXIT_FAILURE, "%s", optarg);
158 1.2 plunky
159 1.1 gdamore break;
160 1.1 gdamore
161 1.7 plunky case 'm': /* Link Mode */
162 1.7 plunky if (strcasecmp(optarg, "auth") == 0)
163 1.7 plunky lm = RFCOMM_LM_AUTH;
164 1.7 plunky else if (strcasecmp(optarg, "encrypt") == 0)
165 1.7 plunky lm = RFCOMM_LM_ENCRYPT;
166 1.7 plunky else if (strcasecmp(optarg, "secure") == 0)
167 1.7 plunky lm = RFCOMM_LM_SECURE;
168 1.7 plunky else
169 1.7 plunky errx(EXIT_FAILURE, "%s: unknown mode", optarg);
170 1.7 plunky
171 1.7 plunky break;
172 1.7 plunky
173 1.3 plunky case 's': /* service class */
174 1.2 plunky service = optarg;
175 1.1 gdamore break;
176 1.1 gdamore
177 1.1 gdamore case 't': /* Slave TTY name */
178 1.1 gdamore if (optarg[0] != '/')
179 1.1 gdamore asprintf(&tty, "%s%s", _PATH_DEV, optarg);
180 1.1 gdamore else
181 1.1 gdamore tty = optarg;
182 1.2 plunky
183 1.1 gdamore break;
184 1.1 gdamore
185 1.1 gdamore case 'h':
186 1.1 gdamore default:
187 1.1 gdamore usage();
188 1.1 gdamore /* NOT REACHED */
189 1.1 gdamore }
190 1.1 gdamore }
191 1.1 gdamore
192 1.2 plunky /*
193 1.2 plunky * validate options:
194 1.3 plunky * must have channel or remote address but not both
195 1.2 plunky */
196 1.3 plunky if ((channel == 0 && bdaddr_any(&raddr))
197 1.3 plunky || (channel != 0 && !bdaddr_any(&raddr)))
198 1.1 gdamore usage();
199 1.1 gdamore
200 1.2 plunky /*
201 1.2 plunky * grab ttys before we start the bluetooth
202 1.2 plunky */
203 1.1 gdamore if (tty == NULL) {
204 1.2 plunky tty_in = STDIN_FILENO;
205 1.2 plunky tty_out = STDOUT_FILENO;
206 1.2 plunky } else {
207 1.2 plunky tty_in = open_tty(tty);
208 1.2 plunky tty_out = tty_in;
209 1.2 plunky }
210 1.1 gdamore
211 1.2 plunky /* open RFCOMM */
212 1.3 plunky if (channel == 0)
213 1.7 plunky rfcomm = open_client(&laddr, &raddr, lm, service);
214 1.2 plunky else
215 1.7 plunky rfcomm = open_server(&laddr, channel, lm, service);
216 1.1 gdamore
217 1.2 plunky /*
218 1.4 plunky * now we are ready to go, so either detach or maybe turn
219 1.2 plunky * off some input processing, so that rfcomm_sppd can
220 1.2 plunky * be used directly with stdio
221 1.2 plunky */
222 1.2 plunky if (tty == NULL) {
223 1.2 plunky if (tcgetattr(tty_in, &t) < 0)
224 1.2 plunky err(EXIT_FAILURE, "tcgetattr");
225 1.1 gdamore
226 1.2 plunky memcpy(&tio, &t, sizeof(tio));
227 1.2 plunky t.c_lflag &= ~(ECHO | ICANON);
228 1.2 plunky t.c_iflag &= ~(ICRNL);
229 1.1 gdamore
230 1.4 plunky if (memcmp(&tio, &t, sizeof(tio))) {
231 1.4 plunky if (tcsetattr(tty_in, TCSANOW, &t) < 0)
232 1.4 plunky err(EXIT_FAILURE, "tcsetattr");
233 1.1 gdamore
234 1.4 plunky atexit(reset_tio);
235 1.4 plunky }
236 1.2 plunky } else {
237 1.2 plunky if (daemon(0, 0) < 0)
238 1.2 plunky err(EXIT_FAILURE, "daemon() failed");
239 1.1 gdamore }
240 1.1 gdamore
241 1.2 plunky /* catch signals */
242 1.2 plunky done = 0;
243 1.2 plunky (void)signal(SIGHUP, sighandler);
244 1.2 plunky (void)signal(SIGINT, sighandler);
245 1.2 plunky (void)signal(SIGPIPE, sighandler);
246 1.2 plunky (void)signal(SIGTERM, sighandler);
247 1.2 plunky
248 1.2 plunky openlog(getprogname(), LOG_PERROR | LOG_PID, LOG_DAEMON);
249 1.2 plunky syslog(LOG_INFO, "Starting on %s...", (tty ? tty : "stdio"));
250 1.2 plunky
251 1.2 plunky n = max(tty_in, rfcomm) + 1;
252 1.2 plunky while (!done) {
253 1.2 plunky FD_ZERO(&rdset);
254 1.2 plunky FD_SET(tty_in, &rdset);
255 1.2 plunky FD_SET(rfcomm, &rdset);
256 1.1 gdamore
257 1.2 plunky if (select(n, &rdset, NULL, NULL, NULL) < 0) {
258 1.1 gdamore if (errno == EINTR)
259 1.1 gdamore continue;
260 1.1 gdamore
261 1.2 plunky syslog(LOG_ERR, "select error: %m");
262 1.1 gdamore exit(EXIT_FAILURE);
263 1.1 gdamore }
264 1.1 gdamore
265 1.2 plunky if (FD_ISSET(tty_in, &rdset))
266 1.2 plunky copy_data(tty_in, rfcomm);
267 1.1 gdamore
268 1.2 plunky if (FD_ISSET(rfcomm, &rdset))
269 1.2 plunky copy_data(rfcomm, tty_out);
270 1.2 plunky }
271 1.1 gdamore
272 1.2 plunky syslog(LOG_INFO, "Completed on %s", (tty ? tty : "stdio"));
273 1.2 plunky exit(EXIT_SUCCESS);
274 1.2 plunky }
275 1.1 gdamore
276 1.2 plunky int
277 1.2 plunky open_tty(const char *tty)
278 1.1 gdamore {
279 1.1 gdamore char pty[PATH_MAX], *slash;
280 1.1 gdamore struct group *gr = NULL;
281 1.1 gdamore gid_t ttygid;
282 1.2 plunky int master;
283 1.1 gdamore
284 1.1 gdamore /*
285 1.1 gdamore * Construct master PTY name. The slave tty name must be less then
286 1.1 gdamore * PATH_MAX characters in length, must contain '/' character and
287 1.1 gdamore * must not end with '/'.
288 1.1 gdamore */
289 1.2 plunky if (strlen(tty) >= sizeof(pty))
290 1.2 plunky errx(EXIT_FAILURE, ": tty name too long");
291 1.1 gdamore
292 1.1 gdamore strlcpy(pty, tty, sizeof(pty));
293 1.1 gdamore slash = strrchr(pty, '/');
294 1.2 plunky if (slash == NULL || slash[1] == '\0')
295 1.2 plunky errx(EXIT_FAILURE, "%s: invalid tty", tty);
296 1.1 gdamore
297 1.1 gdamore slash[1] = 'p';
298 1.2 plunky if (strcmp(pty, tty) == 0)
299 1.2 plunky errx(EXIT_FAILURE, "Master and slave tty are the same (%s)", tty);
300 1.1 gdamore
301 1.2 plunky if ((master = open(pty, O_RDWR, 0)) < 0)
302 1.2 plunky err(EXIT_FAILURE, "%s", pty);
303 1.1 gdamore
304 1.1 gdamore /*
305 1.1 gdamore * Slave TTY
306 1.1 gdamore */
307 1.1 gdamore
308 1.1 gdamore if ((gr = getgrnam("tty")) != NULL)
309 1.1 gdamore ttygid = gr->gr_gid;
310 1.1 gdamore else
311 1.1 gdamore ttygid = (gid_t)-1;
312 1.1 gdamore
313 1.2 plunky (void)chown(tty, getuid(), ttygid);
314 1.2 plunky (void)chmod(tty, S_IRUSR | S_IWUSR | S_IWGRP);
315 1.2 plunky (void)revoke(tty);
316 1.2 plunky
317 1.2 plunky return master;
318 1.2 plunky }
319 1.1 gdamore
320 1.2 plunky int
321 1.7 plunky open_client(bdaddr_t *laddr, bdaddr_t *raddr, int lm, const char *service)
322 1.2 plunky {
323 1.2 plunky struct sockaddr_bt sa;
324 1.3 plunky struct service *s;
325 1.2 plunky struct linger l;
326 1.3 plunky char *ep;
327 1.10 plunky int fd, error;
328 1.10 plunky uintmax_t channel;
329 1.3 plunky
330 1.3 plunky for (s = services ; ; s++) {
331 1.3 plunky if (s->name == NULL) {
332 1.5 plunky channel = strtoul(service, &ep, 10);
333 1.10 plunky if (*ep != '\0')
334 1.10 plunky errx(EXIT_FAILURE, "Unknown service: %s", service);
335 1.3 plunky
336 1.3 plunky break;
337 1.3 plunky }
338 1.3 plunky
339 1.3 plunky if (strcasecmp(s->name, service) == 0) {
340 1.10 plunky error = channel_lookup(laddr, raddr, s->class, &channel);
341 1.10 plunky if (error != 0)
342 1.10 plunky errx(EXIT_FAILURE, "%s: %s", s->name, strerror(error));
343 1.3 plunky
344 1.3 plunky break;
345 1.3 plunky }
346 1.3 plunky }
347 1.1 gdamore
348 1.10 plunky if (channel < RFCOMM_CHANNEL_MIN || channel > RFCOMM_CHANNEL_MAX)
349 1.10 plunky errx(EXIT_FAILURE, "Invalid channel %"PRIuMAX, channel);
350 1.10 plunky
351 1.2 plunky memset(&sa, 0, sizeof(sa));
352 1.2 plunky sa.bt_len = sizeof(sa);
353 1.2 plunky sa.bt_family = AF_BLUETOOTH;
354 1.2 plunky bdaddr_copy(&sa.bt_bdaddr, laddr);
355 1.2 plunky
356 1.2 plunky fd = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
357 1.2 plunky if (fd < 0)
358 1.2 plunky err(EXIT_FAILURE, "socket()");
359 1.2 plunky
360 1.2 plunky if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
361 1.2 plunky err(EXIT_FAILURE, "bind(%s)", bt_ntoa(laddr, NULL));
362 1.2 plunky
363 1.2 plunky memset(&l, 0, sizeof(l));
364 1.2 plunky l.l_onoff = 1;
365 1.2 plunky l.l_linger = 5;
366 1.2 plunky if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) < 0)
367 1.2 plunky err(EXIT_FAILURE, "linger()");
368 1.2 plunky
369 1.7 plunky if (setsockopt(fd, BTPROTO_RFCOMM, SO_RFCOMM_LM, &lm, sizeof(lm)) < 0)
370 1.7 plunky err(EXIT_FAILURE, "link mode");
371 1.7 plunky
372 1.2 plunky sa.bt_channel = channel;
373 1.2 plunky bdaddr_copy(&sa.bt_bdaddr, raddr);
374 1.2 plunky
375 1.2 plunky if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
376 1.10 plunky err(EXIT_FAILURE, "connect(%s, %"PRIuMAX")", bt_ntoa(raddr, NULL),
377 1.2 plunky channel);
378 1.1 gdamore
379 1.2 plunky return fd;
380 1.2 plunky }
381 1.1 gdamore
382 1.2 plunky int
383 1.7 plunky open_server(bdaddr_t *laddr, uint8_t channel, int lm, const char *service)
384 1.1 gdamore {
385 1.10 plunky uint8_t buffer[256];
386 1.2 plunky struct sockaddr_bt sa;
387 1.10 plunky struct service *s;
388 1.2 plunky struct linger l;
389 1.2 plunky socklen_t len;
390 1.10 plunky sdp_session_t ss;
391 1.10 plunky sdp_data_t rec;
392 1.10 plunky int sv, fd;
393 1.10 plunky
394 1.10 plunky for (s = services; ; s++) {
395 1.10 plunky if (s->name == NULL)
396 1.10 plunky usage();
397 1.1 gdamore
398 1.10 plunky if (strcasecmp(s->name, service) == 0)
399 1.10 plunky break;
400 1.10 plunky }
401 1.2 plunky
402 1.10 plunky /* Open server socket */
403 1.2 plunky sv = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
404 1.2 plunky if (sv < 0)
405 1.2 plunky err(EXIT_FAILURE, "socket()");
406 1.2 plunky
407 1.10 plunky memset(&sa, 0, sizeof(sa));
408 1.10 plunky sa.bt_len = sizeof(sa);
409 1.10 plunky sa.bt_family = AF_BLUETOOTH;
410 1.10 plunky sa.bt_channel = channel;
411 1.10 plunky bdaddr_copy(&sa.bt_bdaddr, laddr);
412 1.2 plunky if (bind(sv, (struct sockaddr *)&sa, sizeof(sa)) < 0)
413 1.2 plunky err(EXIT_FAILURE, "bind(%s, %d)", bt_ntoa(laddr, NULL),
414 1.2 plunky channel);
415 1.2 plunky
416 1.7 plunky if (setsockopt(sv, BTPROTO_RFCOMM, SO_RFCOMM_LM, &lm, sizeof(lm)) < 0)
417 1.7 plunky err(EXIT_FAILURE, "link mode");
418 1.7 plunky
419 1.2 plunky if (listen(sv, 1) < 0)
420 1.2 plunky err(EXIT_FAILURE, "listen()");
421 1.2 plunky
422 1.10 plunky /* Build SDP record */
423 1.10 plunky rec.next = buffer;
424 1.10 plunky rec.end = buffer + sizeof(buffer);
425 1.10 plunky
426 1.10 plunky sdp_put_uint16(&rec, SDP_ATTR_SERVICE_RECORD_HANDLE);
427 1.10 plunky sdp_put_uint32(&rec, 0x00000000);
428 1.10 plunky
429 1.10 plunky sdp_put_uint16(&rec, SDP_ATTR_SERVICE_CLASS_ID_LIST);
430 1.10 plunky sdp_put_seq(&rec, 3);
431 1.10 plunky sdp_put_uuid16(&rec, s->class);
432 1.10 plunky
433 1.10 plunky sdp_put_uint16(&rec, SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST);
434 1.10 plunky sdp_put_seq(&rec, 12);
435 1.10 plunky sdp_put_seq(&rec, 3);
436 1.10 plunky sdp_put_uuid16(&rec, SDP_UUID_PROTOCOL_L2CAP);
437 1.10 plunky sdp_put_seq(&rec, 5);
438 1.10 plunky sdp_put_uuid16(&rec, SDP_UUID_PROTOCOL_RFCOMM);
439 1.10 plunky sdp_put_uint8(&rec, channel);
440 1.10 plunky
441 1.10 plunky sdp_put_uint16(&rec, SDP_ATTR_BROWSE_GROUP_LIST);
442 1.10 plunky sdp_put_seq(&rec, 3);
443 1.10 plunky sdp_put_uuid16(&rec, SDP_SERVICE_CLASS_PUBLIC_BROWSE_GROUP);
444 1.10 plunky
445 1.10 plunky sdp_put_uint16(&rec, SDP_ATTR_LANGUAGE_BASE_ATTRIBUTE_ID_LIST);
446 1.10 plunky sdp_put_seq(&rec, 9);
447 1.10 plunky sdp_put_uint16(&rec, 0x656e); /* "en" */
448 1.10 plunky sdp_put_uint16(&rec, 106); /* UTF-8 */
449 1.10 plunky sdp_put_uint16(&rec, SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID);
450 1.10 plunky
451 1.10 plunky if (s->class == SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP) {
452 1.10 plunky sdp_put_uint16(&rec, SDP_ATTR_SERVICE_AVAILABILITY);
453 1.10 plunky sdp_put_uint8(&rec, 0x00);
454 1.10 plunky }
455 1.10 plunky
456 1.10 plunky sdp_put_uint16(&rec, SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
457 1.10 plunky sdp_put_seq(&rec, 8);
458 1.10 plunky sdp_put_seq(&rec, 6);
459 1.10 plunky sdp_put_uuid16(&rec, s->class);
460 1.10 plunky sdp_put_uint16(&rec, 0x0100); /* v1.0 */
461 1.10 plunky
462 1.10 plunky sdp_put_uint16(&rec, SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID
463 1.10 plunky + SDP_ATTR_SERVICE_NAME_OFFSET);
464 1.10 plunky sdp_put_str(&rec, s->description, -1);
465 1.10 plunky
466 1.10 plunky if (s->class == SDP_SERVICE_CLASS_DIALUP_NETWORKING) {
467 1.10 plunky sdp_put_uint16(&rec, SDP_ATTR_AUDIO_FEEDBACK_SUPPORT);
468 1.10 plunky sdp_put_bool(&rec, false);
469 1.10 plunky }
470 1.1 gdamore
471 1.10 plunky #if 0
472 1.10 plunky if (s->class == SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP) {
473 1.10 plunky sdp_put_uint16(&rec, SDP_ATTR_IP_SUBNET); /* TODO */
474 1.10 plunky sdp_put_str(&rec, "0.0.0.0/0", -1);
475 1.1 gdamore }
476 1.10 plunky #endif
477 1.1 gdamore
478 1.10 plunky rec.end = rec.next;
479 1.10 plunky rec.next = buffer;
480 1.2 plunky
481 1.10 plunky /* Register service with SDP server */
482 1.2 plunky ss = sdp_open_local(NULL);
483 1.10 plunky if (ss == NULL)
484 1.2 plunky err(EXIT_FAILURE, "sdp_open_local");
485 1.2 plunky
486 1.10 plunky if (!sdp_record_insert(ss, laddr, NULL, &rec))
487 1.10 plunky err(EXIT_FAILURE, "sdp_record_insert");
488 1.2 plunky
489 1.10 plunky /* Accept client connection */
490 1.2 plunky len = sizeof(sa);
491 1.2 plunky fd = accept(sv, (struct sockaddr *)&sa, &len);
492 1.2 plunky if (fd < 0)
493 1.2 plunky err(EXIT_FAILURE, "accept");
494 1.2 plunky
495 1.2 plunky memset(&l, 0, sizeof(l));
496 1.2 plunky l.l_onoff = 1;
497 1.2 plunky l.l_linger = 5;
498 1.2 plunky if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) < 0)
499 1.2 plunky err(EXIT_FAILURE, "linger()");
500 1.2 plunky
501 1.2 plunky close(sv);
502 1.2 plunky return fd;
503 1.2 plunky }
504 1.1 gdamore
505 1.2 plunky void
506 1.2 plunky copy_data(int src, int dst)
507 1.1 gdamore {
508 1.2 plunky static char buf[BUFSIZ];
509 1.2 plunky ssize_t nr, nw, off;
510 1.1 gdamore
511 1.2 plunky while ((nr = read(src, buf, sizeof(buf))) == -1) {
512 1.2 plunky if (errno != EINTR) {
513 1.2 plunky syslog(LOG_ERR, "read failed: %m");
514 1.2 plunky exit(EXIT_FAILURE);
515 1.2 plunky }
516 1.2 plunky }
517 1.1 gdamore
518 1.3 plunky if (nr == 0) /* reached EOF */
519 1.3 plunky done++;
520 1.3 plunky
521 1.2 plunky for (off = 0 ; nr ; nr -= nw, off += nw) {
522 1.2 plunky if ((nw = write(dst, buf + off, (size_t)nr)) == -1) {
523 1.2 plunky syslog(LOG_ERR, "write failed: %m");
524 1.2 plunky exit(EXIT_FAILURE);
525 1.1 gdamore }
526 1.1 gdamore }
527 1.2 plunky }
528 1.1 gdamore
529 1.10 plunky int
530 1.10 plunky channel_lookup(bdaddr_t const *laddr, bdaddr_t const *raddr,
531 1.10 plunky uint16_t class, uintmax_t *channel)
532 1.10 plunky {
533 1.10 plunky uint8_t buffer[6]; /* SSP (3 bytes) + AIL (3 bytes) */
534 1.10 plunky sdp_session_t ss;
535 1.10 plunky sdp_data_t ail, ssp, rsp, rec, value, pdl, seq;
536 1.10 plunky uint16_t attr;
537 1.10 plunky bool rv;
538 1.10 plunky
539 1.10 plunky seq.next = buffer;
540 1.10 plunky seq.end = buffer + sizeof(buffer);
541 1.10 plunky
542 1.10 plunky /*
543 1.10 plunky * build ServiceSearchPattern (3 bytes)
544 1.10 plunky */
545 1.10 plunky ssp.next = seq.next;
546 1.10 plunky sdp_put_uuid16(&seq, class);
547 1.10 plunky ssp.end = seq.next;
548 1.10 plunky
549 1.10 plunky /*
550 1.10 plunky * build AttributeIDList (3 bytes)
551 1.10 plunky */
552 1.10 plunky ail.next = seq.next;
553 1.10 plunky sdp_put_uint16(&seq, SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST);
554 1.10 plunky ail.end = seq.next;
555 1.10 plunky
556 1.10 plunky ss = sdp_open(laddr, raddr);
557 1.10 plunky if (ss == NULL)
558 1.10 plunky return errno;
559 1.10 plunky
560 1.10 plunky rv = sdp_service_search_attribute(ss, &ssp, &ail, &rsp);
561 1.10 plunky if (!rv) {
562 1.10 plunky sdp_close(ss);
563 1.10 plunky return errno;
564 1.10 plunky }
565 1.10 plunky
566 1.10 plunky /*
567 1.10 plunky * The response will be a list of records that matched our
568 1.10 plunky * ServiceSearchPattern, where each record is a sequence
569 1.10 plunky * containing a single ProtocolDescriptorList attribute and
570 1.10 plunky * value
571 1.10 plunky *
572 1.10 plunky * seq
573 1.10 plunky * uint16 ProtocolDescriptorList
574 1.10 plunky * value
575 1.10 plunky * seq
576 1.10 plunky * uint16 ProtocolDescriptorList
577 1.10 plunky * value
578 1.10 plunky *
579 1.10 plunky * If the ProtocolDescriptorList describes a single stack,
580 1.10 plunky * the attribute value takes the form of a single Data Element
581 1.10 plunky * Sequence where each member is a protocol descriptor.
582 1.10 plunky *
583 1.10 plunky * seq
584 1.10 plunky * list
585 1.10 plunky *
586 1.10 plunky * If it is possible for more than one kind of protocol
587 1.10 plunky * stack to be used to gain access to the service, the
588 1.10 plunky * ProtocolDescriptorList takes the form of a Data Element
589 1.10 plunky * Alternative where each member is a Data Element Sequence
590 1.10 plunky * describing an alternative protocol stack.
591 1.10 plunky *
592 1.10 plunky * alt
593 1.10 plunky * seq
594 1.10 plunky * list
595 1.10 plunky * seq
596 1.10 plunky * list
597 1.10 plunky *
598 1.10 plunky * Each protocol stack description contains a sequence for each
599 1.10 plunky * protocol, where each sequence contains the protocol UUID as
600 1.10 plunky * the first element, and any ProtocolSpecificParameters. We are
601 1.10 plunky * interested in the RFCOMM channel number, stored as parameter#1.
602 1.10 plunky *
603 1.10 plunky * seq
604 1.10 plunky * uuid L2CAP
605 1.10 plunky * uint16 psm
606 1.10 plunky * seq
607 1.10 plunky * uuid RFCOMM
608 1.10 plunky * uint8 channel
609 1.10 plunky */
610 1.10 plunky
611 1.10 plunky rv = false;
612 1.10 plunky while (!rv && sdp_get_seq(&rsp, &rec)) {
613 1.10 plunky if (!sdp_get_attr(&rec, &attr, &value)
614 1.10 plunky || attr != SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST)
615 1.10 plunky continue;
616 1.10 plunky
617 1.10 plunky sdp_get_alt(&value, &value); /* strip any alt container */
618 1.10 plunky while (!rv && sdp_get_seq(&value, &pdl)) {
619 1.10 plunky if (sdp_get_seq(&pdl, &seq)
620 1.10 plunky && sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_L2CAP)
621 1.10 plunky && sdp_get_seq(&pdl, &seq)
622 1.10 plunky && sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_RFCOMM)
623 1.10 plunky && sdp_get_uint(&seq, channel))
624 1.10 plunky rv = true;
625 1.10 plunky }
626 1.10 plunky }
627 1.10 plunky
628 1.10 plunky sdp_close(ss);
629 1.10 plunky return (rv) ? 0 : ENOATTR;
630 1.10 plunky }
631 1.10 plunky
632 1.2 plunky void
633 1.2 plunky sighandler(int s)
634 1.2 plunky {
635 1.1 gdamore
636 1.2 plunky done++;
637 1.2 plunky }
638 1.2 plunky
639 1.2 plunky void
640 1.2 plunky reset_tio(void)
641 1.1 gdamore {
642 1.1 gdamore
643 1.2 plunky tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio);
644 1.2 plunky }
645 1.2 plunky
646 1.2 plunky void
647 1.1 gdamore usage(void)
648 1.1 gdamore {
649 1.7 plunky const char *cmd = getprogname();
650 1.2 plunky struct service *s;
651 1.2 plunky
652 1.7 plunky fprintf(stderr, "Usage: %s [-d device] [-m mode] [-s service] [-t tty]\n"
653 1.7 plunky " %*s {-a bdaddr | -c channel}\n"
654 1.2 plunky "\n"
655 1.2 plunky "Where:\n"
656 1.2 plunky "\t-a bdaddr remote device address\n"
657 1.3 plunky "\t-c channel local RFCOMM channel\n"
658 1.2 plunky "\t-d device local device address\n"
659 1.7 plunky "\t-m mode link mode\n"
660 1.3 plunky "\t-s service service class\n"
661 1.2 plunky "\t-t tty run in background using pty\n"
662 1.8 dsl "\n", cmd, (int)strlen(cmd), "");
663 1.2 plunky
664 1.3 plunky fprintf(stderr, "Known service classes:\n");
665 1.2 plunky for (s = services ; s->name != NULL ; s++)
666 1.3 plunky fprintf(stderr, "\t%-13s%s\n", s->name, s->description);
667 1.1 gdamore
668 1.1 gdamore exit(EXIT_FAILURE);
669 1.2 plunky }
670