summaryrefslogtreecommitdiffstats
path: root/usr.sbin/bluetooth/btpand/btpand.c
blob: 938744448bea2847ce334c63cb2c5f0cf3b3270f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*	$NetBSD: btpand.c,v 1.1 2008/08/17 13:20:57 plunky Exp $	*/

/*-
 * Copyright (c) 2008 Iain Hibbert
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* $FreeBSD$ */

#include <sys/cdefs.h>
__COPYRIGHT("@(#) Copyright (c) 2008 Iain Hibbert. All rights reserved.");
__RCSID("$NetBSD: btpand.c,v 1.1 2008/08/17 13:20:57 plunky Exp $");

#include <sys/wait.h>

#include <bluetooth.h>
#include <err.h>
#include <fcntl.h>
#include <paths.h>
#include <sdp.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "btpand.h"

/* global variables */
const char *	control_path;		/* -c <path> */
const char *	interface_name;		/* -i <ifname> */
const char *	service_name;		/* -s <service> */
uint16_t	service_class;

bdaddr_t	local_bdaddr;		/* -d <addr> */
bdaddr_t	remote_bdaddr;		/* -a <addr> */
uint16_t	l2cap_psm;		/* -p <psm> */
int		l2cap_mode;		/* -m <mode> */

int		server_limit;		/* -n <limit> */

static const struct {
	const char *	name;
	uint16_t	class;
	const char *	desc;
} services[] = {
	{ "PANU", SDP_SERVICE_CLASS_PANU, "Personal Area Networking User" },
	{ "NAP",  SDP_SERVICE_CLASS_NAP,  "Network Acess Point"		  },
	{ "GN",	  SDP_SERVICE_CLASS_GN,   "Group Network"		  },
};

static void main_exit(int);
static void main_detach(void);
static void usage(void);

int
main(int argc, char *argv[])
{
	unsigned long	ul;
	char *		ep;
	int		ch, status;

	while ((ch = getopt(argc, argv, "a:c:d:i:l:m:p:S:s:")) != -1) {
		switch (ch) {
		case 'a': /* remote address */
			if (!bt_aton(optarg, &remote_bdaddr)) {
				struct hostent  *he;

				if ((he = bt_gethostbyname(optarg)) == NULL)
					errx(EXIT_FAILURE, "%s: %s",
					    optarg, hstrerror(h_errno));

				bdaddr_copy(&remote_bdaddr,
					(bdaddr_t *)he->h_addr);
			}

			break;

		case 'c': /* control socket path */
			control_path = optarg;
			break;

		case 'd': /* local address */
			if (!bt_devaddr(optarg, &local_bdaddr)) {
				struct hostent  *he;

				if ((he = bt_gethostbyname(optarg)) == NULL)
					errx(EXIT_FAILURE, "%s: %s",
					    optarg, hstrerror(h_errno));

				bdaddr_copy(&local_bdaddr,
					(bdaddr_t *)he->h_addr);
			}
			break;

		case 'i': /* tap interface name */
			if (strchr(optarg, '/') == NULL) {
				asprintf(&ep, "/dev/%s", optarg);
				interface_name = ep;
			} else
				interface_name = optarg;
			break;

		case 'l': /* limit server sessions */
			ul = strtoul(optarg, &ep, 10);
			if (*optarg == '\0' || *ep != '\0' || ul == 0)
				errx(EXIT_FAILURE, "%s: invalid session limit",
					optarg);

			server_limit = ul;
			break;

		case 'm': /* link mode */
			warnx("Setting link mode is not yet supported");
			break;

		case 'p': /* protocol/service multiplexer */
			ul = strtoul(optarg, &ep, 0);
			if (*optarg == '\0' || *ep != '\0'
			    || ul > 0xffff || L2CAP_PSM_INVALID(ul))
				errx(EXIT_FAILURE, "%s: invalid PSM", optarg);

			l2cap_psm = ul;
			break;

		case 's': /* service */
		case 'S': /* service (no SDP) */
			for (ul = 0; strcasecmp(optarg, services[ul].name); ul++) {
				if (ul == __arraycount(services))
					errx(EXIT_FAILURE, "%s: unknown service", optarg);
			}

			if (ch == 's')
				service_name = services[ul].name;

			service_class = services[ul].class;
			break;

		default:
			usage();
			/* NOTREACHED */
		}
	}

	argc -= optind;
	argv += optind;

	/* validate options */
	if (bdaddr_any(&local_bdaddr) || service_class == 0)
		usage();

	if (!bdaddr_any(&remote_bdaddr) && (server_limit != 0 ||
	    control_path != 0 || (service_name != NULL && l2cap_psm != 0)))
		usage();

	/* default options */
	if (interface_name == NULL)
		interface_name = "/dev/tap";

	if (l2cap_psm == 0)
		l2cap_psm = L2CAP_PSM_BNEP;

	if (bdaddr_any(&remote_bdaddr) && server_limit == 0) {
		if (service_class == SDP_SERVICE_CLASS_PANU)
			server_limit = 1;
		else
			server_limit = 7;
	}

#ifdef L2CAP_LM_MASTER
	if (server_limit > 1 && service_class != SDP_SERVICE_CLASS_PANU)
		l2cap_mode |= L2CAP_LM_MASTER;
#endif

	/*
	 * fork() now so that the setup can be done in the child process
	 * (as kqueue is not inherited) but block in the parent until the
	 * setup is finished so we can return an error if necessary.
	 */
	switch(fork()) {
	case -1: /* bad */
		err(EXIT_FAILURE, "fork() failed");

	case 0:	/* child */
		signal(SIGPIPE, SIG_IGN);

		openlog(getprogname(), LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_DAEMON);

		channel_init();
		server_init();
		event_init();
		client_init();
		tap_init();

		main_detach();

		event_dispatch();
		break;

	default: /* parent */
		signal(SIGUSR1, main_exit);
		wait(&status);

		if (WIFEXITED(status))
			exit(WEXITSTATUS(status));

		break;
	}

	err(EXIT_FAILURE, "exiting");
}

static void
main_exit(int s)
{

	/* child is all grown up */
	_exit(EXIT_SUCCESS);
}

static void
main_detach(void)
{
	int fd;

	if (kill(getppid(), SIGUSR1) == -1)
		log_err("Could not signal main process: %m");

	if (setsid() == -1)
		log_err("setsid() failed");

	fd = open(_PATH_DEVNULL, O_RDWR, 0);
	if (fd == -1) {
		log_err("Could not open %s", _PATH_DEVNULL);
	} else {
		(void)dup2(fd, STDIN_FILENO);
		(void)dup2(fd, STDOUT_FILENO);
		(void)dup2(fd, STDERR_FILENO);
		close(fd);
	}
}

static void
usage(void)
{
	const char *p = getprogname();
	int n = strlen(p);

	fprintf(stderr,
	    "usage: %s [-i ifname] [-m mode] -a address -d device\n"
	    "       %*s {-s service | -S service [-p psm]}\n"
	    "       %s [-c path] [-i ifname] [-l limit] [-m mode] [-p psm] -d device\n"
	    "       %*s {-s service | -S service}\n"
	    "\n"
	    "Where:\n"
	    "\t-a address  remote bluetooth device\n"
	    "\t-c path     SDP server socket\n"
	    "\t-d device   local bluetooth device\n"
	    "\t-i ifname   tap interface\n"
	    "\t-l limit    limit server sessions\n"
	    "\t-m mode     L2CAP link mode (NOT YET SUPPORTED)\n"
	    "\t-p psm      L2CAP PSM\n"
	    "\t-S service  service name (no SDP)\n"
	    "\t-s service  service name\n"
	    "\n"
	    "Known services:\n"
	    "", p, n, "", p, n, "");

	for (n = 0; n < __arraycount(services); n++)
		fprintf(stderr, "\t%s\t%s\n", services[n].name, services[n].desc);

	exit(EXIT_FAILURE);
}
OpenPOWER on IntegriCloud