6 * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: rfcomm_sppd.c,v 1.4 2003/09/07 18:15:55 max Exp $
35 #include <sys/types.h>
36 #include <bluetooth.h>
55 #define SPPD_IDENT "rfcomm_sppd"
56 #define SPPD_BUFFER_SIZE 1024
57 #define max(a, b) (((a) > (b))? (a) : (b))
59 int rfcomm_channel_lookup (bdaddr_t const *local,
60 bdaddr_t const *remote,
61 int service, int *channel, int *error);
63 static int sppd_ttys_open (char **tty, int *amaster, int *aslave);
64 static int sppd_read (int fd, char *buffer, int size);
65 static int sppd_write (int fd, char *buffer, int size);
66 static void sppd_sighandler (int s);
67 static void usage (void);
69 static int done; /* are we done? */
73 main(int argc, char *argv[])
76 struct sockaddr_rfcomm ra;
78 int n, background, channel, service,
79 s, amaster, aslave, fd, doserver,
82 char *tty = NULL, *ep = NULL, buf[SPPD_BUFFER_SIZE];
84 memcpy(&addr, NG_HCI_BDADDR_ANY, sizeof(addr));
85 background = channel = 0;
86 service = SDP_SERVICE_CLASS_SERIAL_PORT;
90 /* Parse command line options */
91 while ((n = getopt(argc, argv, "a:bc:thS")) != -1) {
93 case 'a': /* BDADDR */
94 if (!bt_aton(optarg, &addr)) {
95 struct hostent *he = NULL;
97 if ((he = bt_gethostbyname(optarg)) == NULL)
98 errx(1, "%s: %s", optarg, hstrerror(h_errno));
100 memcpy(&addr, he->h_addr, sizeof(addr));
104 case 'c': /* RFCOMM channel */
105 channel = strtoul(optarg, &ep, 10);
108 switch (tolower(optarg[0])) {
109 case 'd': /* DialUp Networking */
110 service = SDP_SERVICE_CLASS_DIALUP_NETWORKING;
114 service = SDP_SERVICE_CLASS_FAX;
118 service = SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP;
121 case 's': /* Serial Port */
122 service = SDP_SERVICE_CLASS_SERIAL_PORT;
126 errx(1, "Unknown service name: %s",
133 case 'b': /* Run in background */
137 case 't': /* Open pseudo TTY */
152 /* Check if we have everything we need */
153 if (!doserver && memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) == 0)
157 /* Set signal handlers */
158 memset(&sa, 0, sizeof(sa));
159 sa.sa_handler = sppd_sighandler;
161 if (sigaction(SIGTERM, &sa, NULL) < 0)
162 err(1, "Could not sigaction(SIGTERM)");
164 if (sigaction(SIGHUP, &sa, NULL) < 0)
165 err(1, "Could not sigaction(SIGHUP)");
167 if (sigaction(SIGINT, &sa, NULL) < 0)
168 err(1, "Could not sigaction(SIGINT)");
170 sa.sa_handler = SIG_IGN;
171 sa.sa_flags = SA_NOCLDWAIT;
173 if (sigaction(SIGCHLD, &sa, NULL) < 0)
174 err(1, "Could not sigaction(SIGCHLD)");
178 if (sppd_ttys_open(&tty, &amaster, &aslave) < 0)
186 amaster = STDIN_FILENO;
190 /* Open RFCOMM connection */
193 struct sockaddr_rfcomm ma;
194 bdaddr_t bt_addr_any;
198 int acceptsock, aaddrlen;
200 acceptsock = socket(PF_BLUETOOTH, SOCK_STREAM,
201 BLUETOOTH_PROTO_RFCOMM);
203 err(1, "Could not create socket");
205 memcpy(&bt_addr_any, NG_HCI_BDADDR_ANY, sizeof(bt_addr_any));
207 memset(&ma, 0, sizeof(ma));
208 ma.rfcomm_len = sizeof(ma);
209 ma.rfcomm_family = AF_BLUETOOTH;
210 memcpy(&ma.rfcomm_bdaddr, &bt_addr_any, sizeof(bt_addr_any));
211 ma.rfcomm_channel = channel;
213 if (bind(acceptsock, (struct sockaddr *)&ma, sizeof(ma)) < 0)
214 err(1, "Could not bind socket on channel %d", channel);
215 if (listen(acceptsock, 10) != 0)
216 err(1, "Could not listen on socket");
218 aaddrlen = sizeof(ma);
219 if (getsockname(acceptsock, (struct sockaddr *)&ma, &aaddrlen) < 0)
220 err(1, "Could not get socket name");
221 channel = ma.rfcomm_channel;
223 ss = sdp_open_local(NULL);
225 errx(1, "Unable to create local SDP session");
226 if (sdp_error(ss) != 0)
227 errx(1, "Unable to open local SDP session. %s (%d)",
228 strerror(sdp_error(ss)), sdp_error(ss));
229 memset(&sp, 0, sizeof(sp));
230 sp.server_channel = channel;
232 if (sdp_register_service(ss, SDP_SERVICE_CLASS_SERIAL_PORT,
233 &bt_addr_any, (void *)&sp, sizeof(sp),
235 errx(1, "Unable to register LAN service with "
236 "local SDP daemon. %s (%d)",
237 strerror(sdp_error(ss)), sdp_error(ss));
242 aaddrlen = sizeof(ra);
243 s = accept(acceptsock, (struct sockaddr *)&ra,
246 err(1, "Unable to accept()");
247 if (memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) &&
248 memcmp(&addr, &ra.rfcomm_bdaddr, sizeof(addr))) {
249 warnx("Connect from wrong client");
254 sdp_unregister_service(ss, sdp_handle);
258 /* Check channel, if was not set then obtain it via SDP */
259 if (channel == 0 && service != 0)
260 if (rfcomm_channel_lookup(NULL, &addr,
261 service, &channel, &n) != 0)
262 errc(1, n, "Could not obtain RFCOMM channel");
263 if (channel <= 0 || channel > 30)
264 errx(1, "Invalid RFCOMM channel number %d", channel);
266 s = socket(PF_BLUETOOTH, SOCK_STREAM, BLUETOOTH_PROTO_RFCOMM);
268 err(1, "Could not create socket");
270 memset(&ra, 0, sizeof(ra));
271 ra.rfcomm_len = sizeof(ra);
272 ra.rfcomm_family = AF_BLUETOOTH;
274 if (bind(s, (struct sockaddr *) &ra, sizeof(ra)) < 0)
275 err(1, "Could not bind socket");
277 memcpy(&ra.rfcomm_bdaddr, &addr, sizeof(ra.rfcomm_bdaddr));
278 ra.rfcomm_channel = channel;
280 if (connect(s, (struct sockaddr *) &ra, sizeof(ra)) < 0)
281 err(1, "Could not connect socket");
284 /* Became daemon if required */
285 if (background && daemon(0, 0) < 0)
286 err(1, "Could not daemon()");
288 openlog(SPPD_IDENT, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_DAEMON);
289 syslog(LOG_INFO, "Starting on %s...", (tty != NULL)? tty : "stdin/stdout");
291 /* Print used tty on stdout for wrappers to pick up */
293 fprintf(stdout, "%s\n", tty);
295 for (done = 0; !done; ) {
297 FD_SET(amaster, &rfd);
300 n = select(max(amaster, s) + 1, &rfd, NULL, NULL, NULL);
305 syslog(LOG_ERR, "Could not select(). %s",
313 if (FD_ISSET(amaster, &rfd)) {
314 n = sppd_read(amaster, buf, sizeof(buf));
316 syslog(LOG_ERR, "Could not read master pty, " \
317 "fd=%d. %s", amaster, strerror(errno));
324 if (sppd_write(s, buf, n) < 0) {
325 syslog(LOG_ERR, "Could not write to socket, " \
326 "fd=%d, size=%d. %s",
327 s, n, strerror(errno));
332 if (FD_ISSET(s, &rfd)) {
333 n = sppd_read(s, buf, sizeof(buf));
335 syslog(LOG_ERR, "Could not read socket, " \
336 "fd=%d. %s", s, strerror(errno));
343 if (sppd_write(fd, buf, n) < 0) {
344 syslog(LOG_ERR, "Could not write to master " \
345 "pty, fd=%d, size=%d. %s",
346 fd, n, strerror(errno));
352 syslog(LOG_INFO, "Completed on %s", (tty != NULL)? tty : "stdin/stdout");
367 sppd_ttys_open(char **tty, int *amaster, int *aslave)
374 if (openpty(amaster, aslave, pty, &tio, NULL) == -1) {
375 syslog(LOG_ERR, "Could not openpty(). %s", strerror(errno));
379 if ((*tty = strdup(pty)) == NULL) {
380 syslog(LOG_ERR, "Could not strdup(). %s", strerror(errno));
387 } /* sppd_ttys_open */
391 sppd_read(int fd, char *buffer, int size)
396 n = read(fd, buffer, size);
409 sppd_write(int fd, char *buffer, int size)
413 for (wrote = 0; size > 0; ) {
414 n = write(fd, buffer, size);
422 /* XXX can happen? */
438 sppd_sighandler(int s)
440 syslog(LOG_INFO, "Signal %d received. Total %d signals received\n",
442 } /* sppd_sighandler */
444 /* Display usage and exit */
449 "Usage: %s options\n" \
450 "Where options are:\n" \
451 "\t-a address Peer address (required in client mode)\n" \
452 "\t-b Run in background\n" \
453 "\t-c channel RFCOMM channel to connect to or listen on\n" \
454 "\t-t use slave pseudo tty (required in background mode)\n" \
455 "\t-S Server mode\n" \
456 "\t-h Display this message\n", SPPD_IDENT);