1 /* $NetBSD: btpand.c,v 1.1 2008/08/17 13:20:57 plunky Exp $ */
4 * Copyright (c) 2008 Iain Hibbert
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __COPYRIGHT("@(#) Copyright (c) 2008 Iain Hibbert. All rights reserved.");
32 __RCSID("$NetBSD: btpand.c,v 1.1 2008/08/17 13:20:57 plunky Exp $");
36 #include <bluetooth.h>
49 /* global variables */
50 const char * control_path; /* -c <path> */
51 const char * interface_name; /* -i <ifname> */
52 const char * service_name; /* -s <service> */
53 uint16_t service_class;
55 bdaddr_t local_bdaddr; /* -d <addr> */
56 bdaddr_t remote_bdaddr; /* -a <addr> */
57 uint16_t l2cap_psm; /* -p <psm> */
58 int l2cap_mode; /* -m <mode> */
60 int server_limit; /* -n <limit> */
67 { "PANU", SDP_SERVICE_CLASS_PANU, "Personal Area Networking User" },
68 { "NAP", SDP_SERVICE_CLASS_NAP, "Network Access Point" },
69 { "GN", SDP_SERVICE_CLASS_GN, "Group Network" },
72 static void main_exit(int);
73 static void main_detach(void);
74 static void usage(void);
77 main(int argc, char *argv[])
83 while ((ch = getopt(argc, argv, "a:c:d:i:l:m:p:S:s:")) != -1) {
85 case 'a': /* remote address */
86 if (!bt_aton(optarg, &remote_bdaddr)) {
89 if ((he = bt_gethostbyname(optarg)) == NULL)
90 errx(EXIT_FAILURE, "%s: %s",
91 optarg, hstrerror(h_errno));
93 bdaddr_copy(&remote_bdaddr,
94 (bdaddr_t *)he->h_addr);
99 case 'c': /* control socket path */
100 control_path = optarg;
103 case 'd': /* local address */
104 if (!bt_devaddr(optarg, &local_bdaddr)) {
107 if ((he = bt_gethostbyname(optarg)) == NULL)
108 errx(EXIT_FAILURE, "%s: %s",
109 optarg, hstrerror(h_errno));
111 bdaddr_copy(&local_bdaddr,
112 (bdaddr_t *)he->h_addr);
116 case 'i': /* tap interface name */
117 if (strchr(optarg, '/') == NULL) {
118 asprintf(&ep, "/dev/%s", optarg);
121 interface_name = optarg;
124 case 'l': /* limit server sessions */
125 ul = strtoul(optarg, &ep, 10);
126 if (*optarg == '\0' || *ep != '\0' || ul == 0)
127 errx(EXIT_FAILURE, "%s: invalid session limit",
133 case 'm': /* link mode */
134 warnx("Setting link mode is not yet supported");
137 case 'p': /* protocol/service multiplexer */
138 ul = strtoul(optarg, &ep, 0);
139 if (*optarg == '\0' || *ep != '\0'
140 || ul > 0xffff || L2CAP_PSM_INVALID(ul))
141 errx(EXIT_FAILURE, "%s: invalid PSM", optarg);
146 case 's': /* service */
147 case 'S': /* service (no SDP) */
148 for (ul = 0; strcasecmp(optarg, services[ul].name); ul++) {
149 if (ul == __arraycount(services))
150 errx(EXIT_FAILURE, "%s: unknown service", optarg);
154 service_name = services[ul].name;
156 service_class = services[ul].class;
168 /* validate options */
169 if (bdaddr_any(&local_bdaddr) || service_class == 0)
172 if (!bdaddr_any(&remote_bdaddr) && (server_limit != 0 ||
173 control_path != 0 || (service_name != NULL && l2cap_psm != 0)))
176 /* default options */
177 if (interface_name == NULL)
178 interface_name = "/dev/tap";
181 l2cap_psm = L2CAP_PSM_BNEP;
183 if (bdaddr_any(&remote_bdaddr) && server_limit == 0) {
184 if (service_class == SDP_SERVICE_CLASS_PANU)
190 #ifdef L2CAP_LM_MASTER
191 if (server_limit > 1 && service_class != SDP_SERVICE_CLASS_PANU)
192 l2cap_mode |= L2CAP_LM_MASTER;
196 * fork() now so that the setup can be done in the child process
197 * (as kqueue is not inherited) but block in the parent until the
198 * setup is finished so we can return an error if necessary.
202 err(EXIT_FAILURE, "fork() failed");
205 signal(SIGPIPE, SIG_IGN);
207 openlog(getprogname(), LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_DAEMON);
220 default: /* parent */
221 signal(SIGUSR1, main_exit);
224 if (WIFEXITED(status))
225 exit(WEXITSTATUS(status));
230 err(EXIT_FAILURE, "exiting");
237 /* child is all grown up */
246 if (kill(getppid(), SIGUSR1) == -1)
247 log_err("Could not signal main process: %m");
250 log_err("setsid() failed");
252 fd = open(_PATH_DEVNULL, O_RDWR, 0);
254 log_err("Could not open %s", _PATH_DEVNULL);
256 (void)dup2(fd, STDIN_FILENO);
257 (void)dup2(fd, STDOUT_FILENO);
258 (void)dup2(fd, STDERR_FILENO);
266 const char *p = getprogname();
270 "usage: %s [-i ifname] [-m mode] -a address -d device\n"
271 " %*s {-s service | -S service [-p psm]}\n"
272 " %s [-c path] [-i ifname] [-l limit] [-m mode] [-p psm] -d device\n"
273 " %*s {-s service | -S service}\n"
276 "\t-a address remote bluetooth device\n"
277 "\t-c path SDP server socket\n"
278 "\t-d device local bluetooth device\n"
279 "\t-i ifname tap interface\n"
280 "\t-l limit limit server sessions\n"
281 "\t-m mode L2CAP link mode (NOT YET SUPPORTED)\n"
282 "\t-p psm L2CAP PSM\n"
283 "\t-S service service name (no SDP)\n"
284 "\t-s service service name\n"
287 "", p, n, "", p, n, "");
289 for (n = 0; n < __arraycount(services); n++)
290 fprintf(stderr, "\t%s\t%s\n", services[n].name, services[n].desc);