6 * SPDX-License-Identifier: BSD-2-Clause
8 * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $Id: rfcomm_sppd.c,v 1.4 2003/09/07 18:15:55 max Exp $
36 #include <sys/types.h>
37 #define L2CAP_SOCKET_CHECKED
38 #include <bluetooth.h>
57 #define SPPD_IDENT "rfcomm_sppd"
58 #define SPPD_BUFFER_SIZE 1024
59 #define max(a, b) (((a) > (b))? (a) : (b))
61 int rfcomm_channel_lookup (bdaddr_t const *local,
62 bdaddr_t const *remote,
63 int service, int *channel, int *error);
65 static int sppd_ttys_open (char **tty, int *amaster, int *aslave);
66 static int sppd_read (int fd, char *buffer, int size);
67 static int sppd_write (int fd, char *buffer, int size);
68 static void sppd_sighandler (int s);
69 static void usage (void);
71 static int done; /* are we done? */
75 main(int argc, char *argv[])
78 struct sockaddr_rfcomm ra;
80 int n, background, channel, service,
81 s, amaster, aslave, fd, doserver,
84 char *tty = NULL, *ep = NULL, buf[SPPD_BUFFER_SIZE];
86 memcpy(&addr, NG_HCI_BDADDR_ANY, sizeof(addr));
87 background = channel = 0;
88 service = SDP_SERVICE_CLASS_SERIAL_PORT;
92 /* Parse command line options */
93 while ((n = getopt(argc, argv, "a:bc:thS")) != -1) {
95 case 'a': /* BDADDR */
96 if (!bt_aton(optarg, &addr)) {
97 struct hostent *he = NULL;
99 if ((he = bt_gethostbyname(optarg)) == NULL)
100 errx(1, "%s: %s", optarg, hstrerror(h_errno));
102 memcpy(&addr, he->h_addr, sizeof(addr));
106 case 'c': /* RFCOMM channel */
107 channel = strtoul(optarg, &ep, 10);
110 switch (tolower(optarg[0])) {
111 case 'd': /* DialUp Networking */
112 service = SDP_SERVICE_CLASS_DIALUP_NETWORKING;
116 service = SDP_SERVICE_CLASS_FAX;
120 service = SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP;
123 case 's': /* Serial Port */
124 service = SDP_SERVICE_CLASS_SERIAL_PORT;
128 errx(1, "Unknown service name: %s",
135 case 'b': /* Run in background */
139 case 't': /* Open pseudo TTY */
154 /* Check if we have everything we need */
155 if (!doserver && memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) == 0)
159 /* Set signal handlers */
160 memset(&sa, 0, sizeof(sa));
161 sa.sa_handler = sppd_sighandler;
163 if (sigaction(SIGTERM, &sa, NULL) < 0)
164 err(1, "Could not sigaction(SIGTERM)");
166 if (sigaction(SIGHUP, &sa, NULL) < 0)
167 err(1, "Could not sigaction(SIGHUP)");
169 if (sigaction(SIGINT, &sa, NULL) < 0)
170 err(1, "Could not sigaction(SIGINT)");
172 sa.sa_handler = SIG_IGN;
173 sa.sa_flags = SA_NOCLDWAIT;
175 if (sigaction(SIGCHLD, &sa, NULL) < 0)
176 err(1, "Could not sigaction(SIGCHLD)");
180 if (sppd_ttys_open(&tty, &amaster, &aslave) < 0)
188 amaster = STDIN_FILENO;
192 /* Open RFCOMM connection */
195 struct sockaddr_rfcomm ma;
196 bdaddr_t bt_addr_any;
200 int acceptsock, aaddrlen;
202 acceptsock = socket(PF_BLUETOOTH, SOCK_STREAM,
203 BLUETOOTH_PROTO_RFCOMM);
205 err(1, "Could not create socket");
207 memcpy(&bt_addr_any, NG_HCI_BDADDR_ANY, sizeof(bt_addr_any));
209 memset(&ma, 0, sizeof(ma));
210 ma.rfcomm_len = sizeof(ma);
211 ma.rfcomm_family = AF_BLUETOOTH;
212 memcpy(&ma.rfcomm_bdaddr, &bt_addr_any, sizeof(bt_addr_any));
213 ma.rfcomm_channel = channel;
215 if (bind(acceptsock, (struct sockaddr *)&ma, sizeof(ma)) < 0)
216 err(1, "Could not bind socket on channel %d", channel);
217 if (listen(acceptsock, 10) != 0)
218 err(1, "Could not listen on socket");
220 aaddrlen = sizeof(ma);
221 if (getsockname(acceptsock, (struct sockaddr *)&ma, &aaddrlen) < 0)
222 err(1, "Could not get socket name");
223 channel = ma.rfcomm_channel;
225 ss = sdp_open_local(NULL);
227 errx(1, "Unable to create local SDP session");
228 if (sdp_error(ss) != 0)
229 errx(1, "Unable to open local SDP session. %s (%d)",
230 strerror(sdp_error(ss)), sdp_error(ss));
231 memset(&sp, 0, sizeof(sp));
232 sp.server_channel = channel;
234 if (sdp_register_service(ss, SDP_SERVICE_CLASS_SERIAL_PORT,
235 &bt_addr_any, (void *)&sp, sizeof(sp),
237 errx(1, "Unable to register LAN service with "
238 "local SDP daemon. %s (%d)",
239 strerror(sdp_error(ss)), sdp_error(ss));
244 aaddrlen = sizeof(ra);
245 s = accept(acceptsock, (struct sockaddr *)&ra,
248 err(1, "Unable to accept()");
249 if (memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) &&
250 memcmp(&addr, &ra.rfcomm_bdaddr, sizeof(addr))) {
251 warnx("Connect from wrong client");
256 sdp_unregister_service(ss, sdp_handle);
260 /* Check channel, if was not set then obtain it via SDP */
261 if (channel == 0 && service != 0)
262 if (rfcomm_channel_lookup(NULL, &addr,
263 service, &channel, &n) != 0)
264 errc(1, n, "Could not obtain RFCOMM channel");
265 if (channel <= 0 || channel > 30)
266 errx(1, "Invalid RFCOMM channel number %d", channel);
268 s = socket(PF_BLUETOOTH, SOCK_STREAM, BLUETOOTH_PROTO_RFCOMM);
270 err(1, "Could not create socket");
272 memset(&ra, 0, sizeof(ra));
273 ra.rfcomm_len = sizeof(ra);
274 ra.rfcomm_family = AF_BLUETOOTH;
276 if (bind(s, (struct sockaddr *) &ra, sizeof(ra)) < 0)
277 err(1, "Could not bind socket");
279 memcpy(&ra.rfcomm_bdaddr, &addr, sizeof(ra.rfcomm_bdaddr));
280 ra.rfcomm_channel = channel;
282 if (connect(s, (struct sockaddr *) &ra, sizeof(ra)) < 0)
283 err(1, "Could not connect socket");
286 /* Became daemon if required */
287 if (background && daemon(0, 0) < 0)
288 err(1, "Could not daemon()");
290 openlog(SPPD_IDENT, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_DAEMON);
291 syslog(LOG_INFO, "Starting on %s...", (tty != NULL)? tty : "stdin/stdout");
293 /* Print used tty on stdout for wrappers to pick up */
295 fprintf(stdout, "%s\n", tty);
297 for (done = 0; !done; ) {
299 FD_SET(amaster, &rfd);
302 n = select(max(amaster, s) + 1, &rfd, NULL, NULL, NULL);
307 syslog(LOG_ERR, "Could not select(). %s",
315 if (FD_ISSET(amaster, &rfd)) {
316 n = sppd_read(amaster, buf, sizeof(buf));
318 syslog(LOG_ERR, "Could not read master pty, " \
319 "fd=%d. %s", amaster, strerror(errno));
326 if (sppd_write(s, buf, n) < 0) {
327 syslog(LOG_ERR, "Could not write to socket, " \
328 "fd=%d, size=%d. %s",
329 s, n, strerror(errno));
334 if (FD_ISSET(s, &rfd)) {
335 n = sppd_read(s, buf, sizeof(buf));
337 syslog(LOG_ERR, "Could not read socket, " \
338 "fd=%d. %s", s, strerror(errno));
345 if (sppd_write(fd, buf, n) < 0) {
346 syslog(LOG_ERR, "Could not write to master " \
347 "pty, fd=%d, size=%d. %s",
348 fd, n, strerror(errno));
354 syslog(LOG_INFO, "Completed on %s", (tty != NULL)? tty : "stdin/stdout");
369 sppd_ttys_open(char **tty, int *amaster, int *aslave)
376 if (openpty(amaster, aslave, pty, &tio, NULL) == -1) {
377 syslog(LOG_ERR, "Could not openpty(). %s", strerror(errno));
381 if ((*tty = strdup(pty)) == NULL) {
382 syslog(LOG_ERR, "Could not strdup(). %s", strerror(errno));
389 } /* sppd_ttys_open */
393 sppd_read(int fd, char *buffer, int size)
398 n = read(fd, buffer, size);
411 sppd_write(int fd, char *buffer, int size)
415 for (wrote = 0; size > 0; ) {
416 n = write(fd, buffer, size);
424 /* XXX can happen? */
440 sppd_sighandler(int s)
442 syslog(LOG_INFO, "Signal %d received. Total %d signals received\n",
444 } /* sppd_sighandler */
446 /* Display usage and exit */
451 "Usage: %s options\n" \
452 "Where options are:\n" \
453 "\t-a address Peer address (required in client mode)\n" \
454 "\t-b Run in background\n" \
455 "\t-c channel RFCOMM channel to connect to or listen on\n" \
456 "\t-t use slave pseudo tty (required in background mode)\n" \
457 "\t-S Server mode\n" \
458 "\t-h Display this message\n", SPPD_IDENT);