1187938Semax/* $NetBSD: btpand.c,v 1.1 2008/08/17 13:20:57 plunky Exp $ */ 2187938Semax 3187938Semax/*- 4187938Semax * Copyright (c) 2008 Iain Hibbert 5187938Semax * All rights reserved. 6187938Semax * 7187938Semax * Redistribution and use in source and binary forms, with or without 8187938Semax * modification, are permitted provided that the following conditions 9187938Semax * are met: 10187938Semax * 1. Redistributions of source code must retain the above copyright 11187938Semax * notice, this list of conditions and the following disclaimer. 12187938Semax * 2. Redistributions in binary form must reproduce the above copyright 13187938Semax * notice, this list of conditions and the following disclaimer in the 14187938Semax * documentation and/or other materials provided with the distribution. 15187938Semax * 16187938Semax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17187938Semax * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18187938Semax * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19187938Semax * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20187938Semax * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21187938Semax * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22187938Semax * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23187938Semax * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24187938Semax * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25187938Semax * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26187938Semax */ 27187938Semax 28187938Semax/* $FreeBSD$ */ 29187938Semax 30187938Semax#include <sys/cdefs.h> 31187938Semax__COPYRIGHT("@(#) Copyright (c) 2008 Iain Hibbert. All rights reserved."); 32187938Semax__RCSID("$NetBSD: btpand.c,v 1.1 2008/08/17 13:20:57 plunky Exp $"); 33187938Semax 34187938Semax#include <sys/wait.h> 35187938Semax 36187938Semax#include <bluetooth.h> 37187938Semax#include <err.h> 38187938Semax#include <fcntl.h> 39187938Semax#include <paths.h> 40187938Semax#include <sdp.h> 41187938Semax#include <stdio.h> 42187938Semax#include <signal.h> 43187938Semax#include <stdlib.h> 44187938Semax#include <string.h> 45187938Semax#include <unistd.h> 46187938Semax 47187938Semax#include "btpand.h" 48187938Semax 49187938Semax/* global variables */ 50187938Semaxconst char * control_path; /* -c <path> */ 51187938Semaxconst char * interface_name; /* -i <ifname> */ 52187938Semaxconst char * service_name; /* -s <service> */ 53187938Semaxuint16_t service_class; 54187938Semax 55187938Semaxbdaddr_t local_bdaddr; /* -d <addr> */ 56187938Semaxbdaddr_t remote_bdaddr; /* -a <addr> */ 57188013Semaxuint16_t l2cap_psm; /* -p <psm> */ 58188013Semaxint l2cap_mode; /* -m <mode> */ 59187938Semax 60187938Semaxint server_limit; /* -n <limit> */ 61187938Semax 62187938Semaxstatic const struct { 63187938Semax const char * name; 64187938Semax uint16_t class; 65187938Semax const char * desc; 66187938Semax} services[] = { 67187938Semax { "PANU", SDP_SERVICE_CLASS_PANU, "Personal Area Networking User" }, 68187938Semax { "NAP", SDP_SERVICE_CLASS_NAP, "Network Acess Point" }, 69187938Semax { "GN", SDP_SERVICE_CLASS_GN, "Group Network" }, 70187938Semax}; 71187938Semax 72187938Semaxstatic void main_exit(int); 73187938Semaxstatic void main_detach(void); 74187938Semaxstatic void usage(void); 75187938Semax 76187938Semaxint 77187938Semaxmain(int argc, char *argv[]) 78187938Semax{ 79187938Semax unsigned long ul; 80187938Semax char * ep; 81187938Semax int ch, status; 82187938Semax 83187938Semax while ((ch = getopt(argc, argv, "a:c:d:i:l:m:p:S:s:")) != -1) { 84187938Semax switch (ch) { 85187938Semax case 'a': /* remote address */ 86187938Semax if (!bt_aton(optarg, &remote_bdaddr)) { 87187938Semax struct hostent *he; 88187938Semax 89187938Semax if ((he = bt_gethostbyname(optarg)) == NULL) 90187938Semax errx(EXIT_FAILURE, "%s: %s", 91187938Semax optarg, hstrerror(h_errno)); 92187938Semax 93187938Semax bdaddr_copy(&remote_bdaddr, 94187938Semax (bdaddr_t *)he->h_addr); 95187938Semax } 96187938Semax 97187938Semax break; 98187938Semax 99187938Semax case 'c': /* control socket path */ 100187938Semax control_path = optarg; 101187938Semax break; 102187938Semax 103187938Semax case 'd': /* local address */ 104192308Semax if (!bt_devaddr(optarg, &local_bdaddr)) { 105187938Semax struct hostent *he; 106187938Semax 107187938Semax if ((he = bt_gethostbyname(optarg)) == NULL) 108187938Semax errx(EXIT_FAILURE, "%s: %s", 109187938Semax optarg, hstrerror(h_errno)); 110187938Semax 111187938Semax bdaddr_copy(&local_bdaddr, 112187938Semax (bdaddr_t *)he->h_addr); 113187938Semax } 114187938Semax break; 115187938Semax 116187938Semax case 'i': /* tap interface name */ 117187938Semax if (strchr(optarg, '/') == NULL) { 118187938Semax asprintf(&ep, "/dev/%s", optarg); 119187938Semax interface_name = ep; 120187938Semax } else 121187938Semax interface_name = optarg; 122187938Semax break; 123187938Semax 124187938Semax case 'l': /* limit server sessions */ 125187938Semax ul = strtoul(optarg, &ep, 10); 126187938Semax if (*optarg == '\0' || *ep != '\0' || ul == 0) 127187938Semax errx(EXIT_FAILURE, "%s: invalid session limit", 128187938Semax optarg); 129187938Semax 130187938Semax server_limit = ul; 131187938Semax break; 132187938Semax 133187938Semax case 'm': /* link mode */ 134187938Semax warnx("Setting link mode is not yet supported"); 135187938Semax break; 136187938Semax 137187938Semax case 'p': /* protocol/service multiplexer */ 138187938Semax ul = strtoul(optarg, &ep, 0); 139187938Semax if (*optarg == '\0' || *ep != '\0' 140187938Semax || ul > 0xffff || L2CAP_PSM_INVALID(ul)) 141187938Semax errx(EXIT_FAILURE, "%s: invalid PSM", optarg); 142187938Semax 143187938Semax l2cap_psm = ul; 144187938Semax break; 145187938Semax 146187938Semax case 's': /* service */ 147187938Semax case 'S': /* service (no SDP) */ 148187938Semax for (ul = 0; strcasecmp(optarg, services[ul].name); ul++) { 149187938Semax if (ul == __arraycount(services)) 150187938Semax errx(EXIT_FAILURE, "%s: unknown service", optarg); 151187938Semax } 152187938Semax 153187938Semax if (ch == 's') 154187938Semax service_name = services[ul].name; 155187938Semax 156187938Semax service_class = services[ul].class; 157187938Semax break; 158187938Semax 159187938Semax default: 160187938Semax usage(); 161187938Semax /* NOTREACHED */ 162187938Semax } 163187938Semax } 164187938Semax 165187938Semax argc -= optind; 166187938Semax argv += optind; 167187938Semax 168187938Semax /* validate options */ 169187938Semax if (bdaddr_any(&local_bdaddr) || service_class == 0) 170187938Semax usage(); 171187938Semax 172187938Semax if (!bdaddr_any(&remote_bdaddr) && (server_limit != 0 || 173187938Semax control_path != 0 || (service_name != NULL && l2cap_psm != 0))) 174187938Semax usage(); 175187938Semax 176187938Semax /* default options */ 177187938Semax if (interface_name == NULL) 178187938Semax interface_name = "/dev/tap"; 179187938Semax 180188013Semax if (l2cap_psm == 0) 181188013Semax l2cap_psm = L2CAP_PSM_BNEP; 182188013Semax 183187938Semax if (bdaddr_any(&remote_bdaddr) && server_limit == 0) { 184187938Semax if (service_class == SDP_SERVICE_CLASS_PANU) 185187938Semax server_limit = 1; 186187938Semax else 187187938Semax server_limit = 7; 188187938Semax } 189187938Semax 190187938Semax#ifdef L2CAP_LM_MASTER 191187938Semax if (server_limit > 1 && service_class != SDP_SERVICE_CLASS_PANU) 192187938Semax l2cap_mode |= L2CAP_LM_MASTER; 193187938Semax#endif 194187938Semax 195187938Semax /* 196187938Semax * fork() now so that the setup can be done in the child process 197187938Semax * (as kqueue is not inherited) but block in the parent until the 198187938Semax * setup is finished so we can return an error if necessary. 199187938Semax */ 200187938Semax switch(fork()) { 201187938Semax case -1: /* bad */ 202187938Semax err(EXIT_FAILURE, "fork() failed"); 203187938Semax 204187938Semax case 0: /* child */ 205187938Semax signal(SIGPIPE, SIG_IGN); 206187938Semax 207187938Semax openlog(getprogname(), LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_DAEMON); 208187938Semax 209187938Semax channel_init(); 210187938Semax server_init(); 211187938Semax event_init(); 212187938Semax client_init(); 213187938Semax tap_init(); 214187938Semax 215187938Semax main_detach(); 216187938Semax 217187938Semax event_dispatch(); 218187938Semax break; 219187938Semax 220187938Semax default: /* parent */ 221187938Semax signal(SIGUSR1, main_exit); 222187938Semax wait(&status); 223187938Semax 224187938Semax if (WIFEXITED(status)) 225187938Semax exit(WEXITSTATUS(status)); 226187938Semax 227187938Semax break; 228187938Semax } 229187938Semax 230187938Semax err(EXIT_FAILURE, "exiting"); 231187938Semax} 232187938Semax 233187938Semaxstatic void 234187938Semaxmain_exit(int s) 235187938Semax{ 236187938Semax 237187938Semax /* child is all grown up */ 238187938Semax _exit(EXIT_SUCCESS); 239187938Semax} 240187938Semax 241187938Semaxstatic void 242187938Semaxmain_detach(void) 243187938Semax{ 244187938Semax int fd; 245187938Semax 246187938Semax if (kill(getppid(), SIGUSR1) == -1) 247187938Semax log_err("Could not signal main process: %m"); 248187938Semax 249187938Semax if (setsid() == -1) 250187938Semax log_err("setsid() failed"); 251187938Semax 252187938Semax fd = open(_PATH_DEVNULL, O_RDWR, 0); 253187938Semax if (fd == -1) { 254187938Semax log_err("Could not open %s", _PATH_DEVNULL); 255187938Semax } else { 256187938Semax (void)dup2(fd, STDIN_FILENO); 257187938Semax (void)dup2(fd, STDOUT_FILENO); 258187938Semax (void)dup2(fd, STDERR_FILENO); 259187938Semax close(fd); 260187938Semax } 261187938Semax} 262187938Semax 263187938Semaxstatic void 264187938Semaxusage(void) 265187938Semax{ 266187938Semax const char *p = getprogname(); 267187938Semax int n = strlen(p); 268187938Semax 269187938Semax fprintf(stderr, 270187938Semax "usage: %s [-i ifname] [-m mode] -a address -d device\n" 271187938Semax " %*s {-s service | -S service [-p psm]}\n" 272187938Semax " %s [-c path] [-i ifname] [-l limit] [-m mode] [-p psm] -d device\n" 273187938Semax " %*s {-s service | -S service}\n" 274187938Semax "\n" 275187938Semax "Where:\n" 276187938Semax "\t-a address remote bluetooth device\n" 277187938Semax "\t-c path SDP server socket\n" 278187938Semax "\t-d device local bluetooth device\n" 279187938Semax "\t-i ifname tap interface\n" 280187938Semax "\t-l limit limit server sessions\n" 281187938Semax "\t-m mode L2CAP link mode (NOT YET SUPPORTED)\n" 282187938Semax "\t-p psm L2CAP PSM\n" 283187938Semax "\t-S service service name (no SDP)\n" 284187938Semax "\t-s service service name\n" 285187938Semax "\n" 286187938Semax "Known services:\n" 287187938Semax "", p, n, "", p, n, ""); 288187938Semax 289187938Semax for (n = 0; n < __arraycount(services); n++) 290187938Semax fprintf(stderr, "\t%s\t%s\n", services[n].name, services[n].desc); 291187938Semax 292187938Semax exit(EXIT_FAILURE); 293187938Semax} 294