1 /* 2 * rfcomm_sppd.c 3 * 4 * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com> 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $Id: rfcomm_sppd.c,v 1.2 2003/04/27 19:22:30 max Exp $ 29 * $FreeBSD$ 30 */ 31 32 #include <sys/types.h> 33 #include <sys/socket.h> 34 #include <sys/stat.h> 35 #include <bitstring.h> 36 #include <err.h> 37 #include <errno.h> 38 #include <fcntl.h> 39 #include <grp.h> 40 #include <limits.h> 41 #include <ng_hci.h> 42 #include <ng_l2cap.h> 43 #include <ng_btsocket.h> 44 #include <signal.h> 45 #include <stdarg.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <syslog.h> 50 #include <termios.h> 51 #include <unistd.h> 52 53 #define SPPD_IDENT "rfcomm_sppd" 54 #define SPPD_BUFFER_SIZE 1024 55 #define max(a, b) (((a) > (b))? (a) : (b)) 56 57 static int sppd_ttys_open (char const *tty, int *amaster, int *aslave); 58 static int sppd_read (int fd, char *buffer, int size); 59 static int sppd_write (int fd, char *buffer, int size); 60 static void sppd_sighandler (int s); 61 static void usage (void); 62 63 static int done; /* are we done? */ 64 65 /* Main */ 66 int 67 main(int argc, char *argv[]) 68 { 69 struct sigaction sa; 70 struct sockaddr_rfcomm ra; 71 bdaddr_t addr; 72 int n, background, channel, s, amaster, aslave; 73 fd_set rfd; 74 char *tty = NULL, buf[SPPD_BUFFER_SIZE]; 75 76 memcpy(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)); 77 background = channel = 0; 78 79 /* Parse command line options */ 80 while ((n = getopt(argc, argv, "a:bc:t:h")) != -1) { 81 switch (n) { 82 case 'a': { /* BDADDR */ 83 int a0, a1, a2, a3, a4, a5; 84 85 if (sscanf(optarg, "%x:%x:%x:%x:%x:%x", 86 &a5, &a4, &a3, &a2, &a1, &a0) != 6) 87 usage(); 88 /* NOT REACHED */ 89 90 addr.b[0] = a0 & 0xff; 91 addr.b[1] = a1 & 0xff; 92 addr.b[2] = a2 & 0xff; 93 addr.b[3] = a3 & 0xff; 94 addr.b[4] = a4 & 0xff; 95 addr.b[5] = a5 & 0xff; 96 } break; 97 98 case 'c': /* RFCOMM channel */ 99 channel = atoi(optarg); 100 break; 101 102 case 'b': /* Run in background */ 103 background = 1; 104 break; 105 106 case 't': /* Slave TTY name */ 107 tty = optarg; 108 break; 109 110 case 'h': 111 default: 112 usage(); 113 /* NOT REACHED */ 114 } 115 } 116 117 /* Check if we have everything we need */ 118 if (channel == 0 || tty == NULL || 119 memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) == 0) 120 usage(); 121 /* NOT REACHED */ 122 123 /* Set signal handlers */ 124 memset(&sa, 0, sizeof(sa)); 125 sa.sa_handler = sppd_sighandler; 126 127 if (sigaction(SIGTERM, &sa, NULL) < 0) 128 err(1, "Could not sigaction(SIGTERM)"); 129 130 if (sigaction(SIGHUP, &sa, NULL) < 0) 131 err(1, "Could not sigaction(SIGHUP)"); 132 133 if (sigaction(SIGINT, &sa, NULL) < 0) 134 err(1, "Could not sigaction(SIGINT)"); 135 136 sa.sa_handler = SIG_IGN; 137 sa.sa_flags = SA_NOCLDWAIT; 138 139 if (sigaction(SIGCHLD, &sa, NULL) < 0) 140 err(1, "Could not sigaction(SIGCHLD)"); 141 142 /* Open TTYs */ 143 if (sppd_ttys_open(tty, &amaster, &aslave) < 0) 144 exit(1); 145 146 /* Open RFCOMM connection */ 147 memset(&ra, 0, sizeof(ra)); 148 ra.rfcomm_len = sizeof(ra); 149 ra.rfcomm_family = AF_BLUETOOTH; 150 151 s = socket(PF_BLUETOOTH, SOCK_STREAM, BLUETOOTH_PROTO_RFCOMM); 152 if (s < 0) 153 err(1, "Could not create socket"); 154 155 if (bind(s, (struct sockaddr *) &ra, sizeof(ra)) < 0) 156 err(1, "Could not bind socket"); 157 158 memcpy(&ra.rfcomm_bdaddr, &addr, sizeof(ra.rfcomm_bdaddr)); 159 ra.rfcomm_channel = channel; 160 161 if (connect(s, (struct sockaddr *) &ra, sizeof(ra)) < 0) 162 err(1, "Could not connect socket"); 163 164 /* Became daemon if required */ 165 if (background) { 166 switch (fork()) { 167 case -1: 168 err(1, "Could not fork()"); 169 /* NOT REACHED */ 170 171 case 0: 172 exit(0); 173 /* NOT REACHED */ 174 175 default: 176 if (daemon(0, 0) < 0) 177 err(1, "Could not daemon()"); 178 break; 179 } 180 } 181 182 openlog(SPPD_IDENT, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_DAEMON); 183 syslog(LOG_INFO, "Starting on %s...", tty); 184 185 for (done = 0; !done; ) { 186 FD_ZERO(&rfd); 187 FD_SET(amaster, &rfd); 188 FD_SET(s, &rfd); 189 190 n = select(max(amaster, s) + 1, &rfd, NULL, NULL, NULL); 191 if (n < 0) { 192 if (errno == EINTR) 193 continue; 194 195 syslog(LOG_ERR, "Could not select(). %s", 196 strerror(errno)); 197 exit(1); 198 } 199 200 if (n == 0) 201 continue; 202 203 if (FD_ISSET(amaster, &rfd)) { 204 n = sppd_read(amaster, buf, sizeof(buf)); 205 if (n < 0) { 206 syslog(LOG_ERR, "Could not read master pty, " \ 207 "fd=%d. %s", amaster, strerror(errno)); 208 exit(1); 209 } 210 211 if (n == 0) 212 break; /* XXX */ 213 214 if (sppd_write(s, buf, n) < 0) { 215 syslog(LOG_ERR, "Could not write to socket, " \ 216 "fd=%d, size=%d. %s", 217 s, n, strerror(errno)); 218 exit(1); 219 } 220 } 221 222 if (FD_ISSET(s, &rfd)) { 223 n = sppd_read(s, buf, sizeof(buf)); 224 if (n < 0) { 225 syslog(LOG_ERR, "Could not read socket, " \ 226 "fd=%d. %s", s, strerror(errno)); 227 exit(1); 228 } 229 230 if (n == 0) 231 break; 232 233 if (sppd_write(amaster, buf, n) < 0) { 234 syslog(LOG_ERR, "Could not write to master " \ 235 "pty, fd=%d, size=%d. %s", 236 amaster, n, strerror(errno)); 237 exit(1); 238 } 239 } 240 } 241 242 syslog(LOG_INFO, "Completed on %s", tty); 243 closelog(); 244 245 close(s); 246 close(aslave); 247 close(amaster); 248 249 return (0); 250 } 251 252 /* Open TTYs */ 253 static int 254 sppd_ttys_open(char const *tty, int *amaster, int *aslave) 255 { 256 char pty[PATH_MAX]; 257 struct group *gr = NULL; 258 gid_t ttygid; 259 struct termios tio; 260 261 /* 262 * Master PTY 263 */ 264 265 strlcpy(pty, tty, sizeof(pty)); 266 pty[5] = 'p'; 267 268 if (strcmp(pty, tty) == 0) { 269 syslog(LOG_ERR, "Master and slave tty are the same (%s)", tty); 270 return (-1); 271 } 272 273 if ((*amaster = open(pty, O_RDWR, 0)) < 0) { 274 syslog(LOG_ERR, "Could not open(%s). %s", pty, strerror(errno)); 275 return (-1); 276 } 277 278 /* 279 * Slave TTY 280 */ 281 282 if ((gr = getgrnam("tty")) != NULL) 283 ttygid = gr->gr_gid; 284 else 285 ttygid = -1; 286 287 (void) chown(tty, getuid(), ttygid); 288 (void) chmod(tty, S_IRUSR|S_IWUSR|S_IWGRP); 289 (void) revoke(tty); 290 291 if ((*aslave = open(tty, O_RDWR, 0)) < 0) { 292 syslog(LOG_ERR, "Could not open(%s). %s", tty, strerror(errno)); 293 close(*amaster); 294 return (-1); 295 } 296 297 /* 298 * Make slave TTY raw 299 */ 300 301 cfmakeraw(&tio); 302 303 if (tcsetattr(*aslave, TCSANOW, &tio) < 0) { 304 syslog(LOG_ERR, "Could not tcsetattr(). %s", strerror(errno)); 305 close(*aslave); 306 close(*amaster); 307 return (-1); 308 } 309 310 return (0); 311 } /* sppd_ttys_open */ 312 313 /* Read data */ 314 static int 315 sppd_read(int fd, char *buffer, int size) 316 { 317 int n; 318 319 again: 320 n = read(fd, buffer, size); 321 if (n < 0) { 322 if (errno == EINTR) 323 goto again; 324 325 return (-1); 326 } 327 328 return (n); 329 } /* sppd_read */ 330 331 /* Write data */ 332 static int 333 sppd_write(int fd, char *buffer, int size) 334 { 335 int n, wrote; 336 337 for (wrote = 0; size > 0; ) { 338 n = write(fd, buffer, size); 339 switch (n) { 340 case -1: 341 if (errno != EINTR) 342 return (-1); 343 break; 344 345 case 0: 346 /* XXX can happen? */ 347 break; 348 349 default: 350 wrote += n; 351 buffer += n; 352 size -= n; 353 break; 354 } 355 } 356 357 return (wrote); 358 } /* sppd_write */ 359 360 /* Signal handler */ 361 static void 362 sppd_sighandler(int s) 363 { 364 syslog(LOG_INFO, "Signal %d received. Total %d signals received\n", 365 s, ++ done); 366 } /* sppd_sighandler */ 367 368 /* Display usage and exit */ 369 static void 370 usage(void) 371 { 372 fprintf(stdout, 373 "Usage: %s options\n" \ 374 "Where options are:\n" \ 375 "\t-a bdaddr BDADDR to connect to (required)\n" \ 376 "\t-b Run in background\n" \ 377 "\t-c channel RFCOMM channel to connect to (required)\n" \ 378 "\t-t tty TTY name\n" \ 379 "\t-h Display this message\n", SPPD_IDENT); 380 381 exit(255); 382 } /* usage */ 383 384