xref: /freebsd/usr.sbin/bluetooth/l2control/l2control.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*-
2  * l2control.c
3  *
4  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5  *
6  * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $Id: l2control.c,v 1.6 2003/09/05 00:38:25 max Exp $
31  * $FreeBSD$
32  */
33 
34 #include <assert.h>
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include "l2control.h"
44 
45 /* Prototypes */
46 static int                    do_l2cap_command    (bdaddr_p, int, char **);
47 static struct l2cap_command * find_l2cap_command  (char const *,
48                                                    struct l2cap_command *);
49 static void                   print_l2cap_command (struct l2cap_command *);
50 static void                   usage               (void);
51 
52 /* Main */
53 
54 int	numeric_bdaddr = 0;
55 
56 int
57 main(int argc, char *argv[])
58 {
59 	int		n;
60 	bdaddr_t	bdaddr;
61 
62 	memset(&bdaddr, 0, sizeof(bdaddr));
63 
64 	/* Process command line arguments */
65 	while ((n = getopt(argc, argv, "a:nh")) != -1) {
66 		switch (n) {
67 		case 'a':
68 			if (!bt_aton(optarg, &bdaddr)) {
69 				struct hostent	*he = NULL;
70 
71 				if ((he = bt_gethostbyname(optarg)) == NULL)
72 					errx(1, "%s: %s", optarg, hstrerror(h_errno));
73 
74 				memcpy(&bdaddr, he->h_addr, sizeof(bdaddr));
75 			}
76 			break;
77 
78 		case 'n':
79 			numeric_bdaddr = 1;
80 			break;
81 
82 		case 'h':
83 		default:
84 			usage();
85 			break;
86 		}
87 	}
88 
89 	argc -= optind;
90 	argv += optind;
91 
92 	if (*argv == NULL)
93 		usage();
94 
95 	return (do_l2cap_command(&bdaddr, argc, argv));
96 } /* main */
97 
98 /* Execute commands */
99 static int
100 do_l2cap_command(bdaddr_p bdaddr, int argc, char **argv)
101 {
102 	char			*cmd = argv[0];
103 	struct l2cap_command	*c = NULL;
104 	struct sockaddr_l2cap	 sa;
105 	int			 s, e, help;
106 
107 	help = 0;
108 	if (strcasecmp(cmd, "help") == 0) {
109 		argc --;
110 		argv ++;
111 
112 		if (argc <= 0) {
113 			fprintf(stdout, "Supported commands:\n");
114 			print_l2cap_command(l2cap_commands);
115 			fprintf(stdout, "\nFor more information use " \
116 				"'help command'\n");
117 
118 			return (OK);
119 		}
120 
121 		help = 1;
122 		cmd = argv[0];
123 	}
124 
125 	c = find_l2cap_command(cmd, l2cap_commands);
126 	if (c == NULL) {
127 		fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
128 		return (ERROR);
129 	}
130 
131 	if (!help) {
132 		if (memcmp(bdaddr, NG_HCI_BDADDR_ANY, sizeof(*bdaddr)) == 0)
133 			usage();
134 
135 		memset(&sa, 0, sizeof(sa));
136 		sa.l2cap_len = sizeof(sa);
137 		sa.l2cap_family = AF_BLUETOOTH;
138 		memcpy(&sa.l2cap_bdaddr, bdaddr, sizeof(sa.l2cap_bdaddr));
139 
140 		s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_L2CAP);
141 		if (s < 0)
142 			err(1, "Could not create socket");
143 
144 		if (bind(s, (struct sockaddr *) &sa, sizeof(sa)) < 0)
145 			err(2,
146 "Could not bind socket, bdaddr=%s", bt_ntoa(&sa.l2cap_bdaddr, NULL));
147 
148 		e = 0x0ffff;
149 		if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &e, sizeof(e)) < 0)
150 			err(3, "Coult not setsockopt(RCVBUF, %d)", e);
151 
152 		e = (c->handler)(s, -- argc, ++ argv);
153 
154 		close(s);
155 	} else
156 		e = USAGE;
157 
158 	switch (e) {
159 	case OK:
160 	case FAILED:
161 		break;
162 
163 	case ERROR:
164 		fprintf(stdout, "Could not execute command \"%s\". %s\n",
165 			cmd, strerror(errno));
166 		break;
167 
168 	case USAGE:
169 		fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);
170 		break;
171 
172 	default: assert(0); break;
173 	}
174 
175 	return (e);
176 } /* do_l2cap_command */
177 
178 /* Try to find command in specified category */
179 static struct l2cap_command *
180 find_l2cap_command(char const *command, struct l2cap_command *category)
181 {
182 	struct l2cap_command	*c = NULL;
183 
184 	for (c = category; c->command != NULL; c++) {
185 		char 	*c_end = strchr(c->command, ' ');
186 
187 		if (c_end != NULL) {
188 			int	len = c_end - c->command;
189 
190 			if (strncasecmp(command, c->command, len) == 0)
191 				return (c);
192 		} else if (strcasecmp(command, c->command) == 0)
193 				return (c);
194 	}
195 
196 	return (NULL);
197 } /* find_l2cap_command */
198 
199 /* Print commands in specified category */
200 static void
201 print_l2cap_command(struct l2cap_command *category)
202 {
203 	struct l2cap_command	*c = NULL;
204 
205 	for (c = category; c->command != NULL; c++)
206 		fprintf(stdout, "\t%s\n", c->command);
207 } /* print_l2cap_command */
208 
209 /* Usage */
210 static void
211 usage(void)
212 {
213 	fprintf(stderr, "Usage: l2control [-hn] -a local cmd [params ..]\n");
214 	fprintf(stderr, "Where:\n");
215 	fprintf(stderr, "  -a local   Specify local device to connect to\n");
216 	fprintf(stderr, "  -h         Display this message\n");
217 	fprintf(stderr, "  -n         Show addresses as numbers\n");
218 	fprintf(stderr, "  cmd        Supported command " \
219 		"(see l2control help)\n");
220 	fprintf(stderr, "  params     Optional command parameters\n");
221 	exit(255);
222 } /* usage */
223 
224