1 /* $NetBSD: btpand.c,v 1.1 2008/08/17 13:20:57 plunky Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
6 * Copyright (c) 2008 Iain Hibbert
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 ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __COPYRIGHT("@(#) Copyright (c) 2008 Iain Hibbert. All rights reserved.");
34 __RCSID("$NetBSD: btpand.c,v 1.1 2008/08/17 13:20:57 plunky Exp $");
38 #define L2CAP_SOCKET_CHECKED
39 #include <bluetooth.h>
52 /* global variables */
53 const char * control_path; /* -c <path> */
54 const char * interface_name; /* -i <ifname> */
55 const char * service_name; /* -s <service> */
56 uint16_t service_class;
58 bdaddr_t local_bdaddr; /* -d <addr> */
59 bdaddr_t remote_bdaddr; /* -a <addr> */
60 uint16_t l2cap_psm; /* -p <psm> */
61 int l2cap_mode; /* -m <mode> */
63 int server_limit; /* -n <limit> */
70 { "PANU", SDP_SERVICE_CLASS_PANU, "Personal Area Networking User" },
71 { "NAP", SDP_SERVICE_CLASS_NAP, "Network Access Point" },
72 { "GN", SDP_SERVICE_CLASS_GN, "Group Network" },
75 static void main_exit(int);
76 static void main_detach(void);
77 static void usage(void);
80 main(int argc, char *argv[])
86 while ((ch = getopt(argc, argv, "a:c:d:i:l:m:p:S:s:")) != -1) {
88 case 'a': /* remote address */
89 if (!bt_aton(optarg, &remote_bdaddr)) {
92 if ((he = bt_gethostbyname(optarg)) == NULL)
93 errx(EXIT_FAILURE, "%s: %s",
94 optarg, hstrerror(h_errno));
96 bdaddr_copy(&remote_bdaddr,
97 (bdaddr_t *)he->h_addr);
102 case 'c': /* control socket path */
103 control_path = optarg;
106 case 'd': /* local address */
107 if (!bt_devaddr(optarg, &local_bdaddr)) {
110 if ((he = bt_gethostbyname(optarg)) == NULL)
111 errx(EXIT_FAILURE, "%s: %s",
112 optarg, hstrerror(h_errno));
114 bdaddr_copy(&local_bdaddr,
115 (bdaddr_t *)he->h_addr);
119 case 'i': /* tap interface name */
120 if (strchr(optarg, '/') == NULL) {
121 asprintf(&ep, "/dev/%s", optarg);
124 interface_name = optarg;
127 case 'l': /* limit server sessions */
128 ul = strtoul(optarg, &ep, 10);
129 if (*optarg == '\0' || *ep != '\0' || ul == 0)
130 errx(EXIT_FAILURE, "%s: invalid session limit",
136 case 'm': /* link mode */
137 warnx("Setting link mode is not yet supported");
140 case 'p': /* protocol/service multiplexer */
141 ul = strtoul(optarg, &ep, 0);
142 if (*optarg == '\0' || *ep != '\0'
143 || ul > 0xffff || L2CAP_PSM_INVALID(ul))
144 errx(EXIT_FAILURE, "%s: invalid PSM", optarg);
149 case 's': /* service */
150 case 'S': /* service (no SDP) */
151 for (ul = 0; strcasecmp(optarg, services[ul].name); ul++) {
152 if (ul == __arraycount(services))
153 errx(EXIT_FAILURE, "%s: unknown service", optarg);
157 service_name = services[ul].name;
159 service_class = services[ul].class;
171 /* validate options */
172 if (bdaddr_any(&local_bdaddr) || service_class == 0)
175 if (!bdaddr_any(&remote_bdaddr) && (server_limit != 0 ||
176 control_path != NULL || (service_name != NULL && l2cap_psm != 0)))
179 /* default options */
180 if (interface_name == NULL)
181 interface_name = "/dev/tap";
184 l2cap_psm = L2CAP_PSM_BNEP;
186 if (bdaddr_any(&remote_bdaddr) && server_limit == 0) {
187 if (service_class == SDP_SERVICE_CLASS_PANU)
193 #ifdef L2CAP_LM_MASTER
194 if (server_limit > 1 && service_class != SDP_SERVICE_CLASS_PANU)
195 l2cap_mode |= L2CAP_LM_MASTER;
199 * fork() now so that the setup can be done in the child process
200 * (as kqueue is not inherited) but block in the parent until the
201 * setup is finished so we can return an error if necessary.
205 err(EXIT_FAILURE, "fork() failed");
208 signal(SIGPIPE, SIG_IGN);
210 openlog(getprogname(), LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_DAEMON);
223 default: /* parent */
224 signal(SIGUSR1, main_exit);
227 if (WIFEXITED(status))
228 exit(WEXITSTATUS(status));
233 err(EXIT_FAILURE, "exiting");
240 /* child is all grown up */
249 if (kill(getppid(), SIGUSR1) == -1)
250 log_err("Could not signal main process: %m");
253 log_err("setsid() failed");
255 fd = open(_PATH_DEVNULL, O_RDWR, 0);
257 log_err("Could not open %s", _PATH_DEVNULL);
259 (void)dup2(fd, STDIN_FILENO);
260 (void)dup2(fd, STDOUT_FILENO);
261 (void)dup2(fd, STDERR_FILENO);
269 const char *p = getprogname();
273 "usage: %s [-i ifname] [-m mode] -a address -d device\n"
274 " %*s {-s service | -S service [-p psm]}\n"
275 " %s [-c path] [-i ifname] [-l limit] [-m mode] [-p psm] -d device\n"
276 " %*s {-s service | -S service}\n"
279 "\t-a address remote bluetooth device\n"
280 "\t-c path SDP server socket\n"
281 "\t-d device local bluetooth device\n"
282 "\t-i ifname tap interface\n"
283 "\t-l limit limit server sessions\n"
284 "\t-m mode L2CAP link mode (NOT YET SUPPORTED)\n"
285 "\t-p psm L2CAP PSM\n"
286 "\t-S service service name (no SDP)\n"
287 "\t-s service service name\n"
290 "", p, n, "", p, n, "");
292 for (n = 0; n < __arraycount(services); n++)
293 fprintf(stderr, "\t%s\t%s\n", services[n].name, services[n].desc);