xref: /freebsd/usr.sbin/bluetooth/l2control/l2control.c (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 /*-
2  * l2control.c
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
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  */
32 
33 #include <assert.h>
34 #define L2CAP_SOCKET_CHECKED
35 #include <bluetooth.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include "l2control.h"
43 
44 /* Prototypes */
45 static int                    do_l2cap_command    (bdaddr_p, int, char **);
46 static struct l2cap_command * find_l2cap_command  (char const *,
47                                                    struct l2cap_command *);
48 static void                   print_l2cap_command (struct l2cap_command *);
49 static void                   usage               (void);
50 
51 /* Main */
52 
53 int	numeric_bdaddr = 0;
54 
55 int
56 main(int argc, char *argv[])
57 {
58 	int		n;
59 	bdaddr_t	bdaddr;
60 
61 	memset(&bdaddr, 0, sizeof(bdaddr));
62 
63 	/* Process command line arguments */
64 	while ((n = getopt(argc, argv, "a:nh")) != -1) {
65 		switch (n) {
66 		case 'a':
67 			if (!bt_aton(optarg, &bdaddr)) {
68 				struct hostent	*he = NULL;
69 
70 				if ((he = bt_gethostbyname(optarg)) == NULL)
71 					errx(1, "%s: %s", optarg, hstrerror(h_errno));
72 
73 				memcpy(&bdaddr, he->h_addr, sizeof(bdaddr));
74 			}
75 			break;
76 
77 		case 'n':
78 			numeric_bdaddr = 1;
79 			break;
80 
81 		case 'h':
82 		default:
83 			usage();
84 			break;
85 		}
86 	}
87 
88 	argc -= optind;
89 	argv += optind;
90 
91 	if (*argv == NULL)
92 		usage();
93 
94 	return (do_l2cap_command(&bdaddr, argc, argv));
95 } /* main */
96 
97 /* Execute commands */
98 static int
99 do_l2cap_command(bdaddr_p bdaddr, int argc, char **argv)
100 {
101 	char			*cmd = argv[0];
102 	struct l2cap_command	*c = NULL;
103 	struct sockaddr_l2cap	 sa;
104 	int			 s, e, help;
105 
106 	help = 0;
107 	if (strcasecmp(cmd, "help") == 0) {
108 		argc --;
109 		argv ++;
110 
111 		if (argc <= 0) {
112 			fprintf(stdout, "Supported commands:\n");
113 			print_l2cap_command(l2cap_commands);
114 			fprintf(stdout, "\nFor more information use " \
115 				"'help command'\n");
116 
117 			return (OK);
118 		}
119 
120 		help = 1;
121 		cmd = argv[0];
122 	}
123 
124 	c = find_l2cap_command(cmd, l2cap_commands);
125 	if (c == NULL) {
126 		fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
127 		return (ERROR);
128 	}
129 
130 	if (!help) {
131 		if (memcmp(bdaddr, NG_HCI_BDADDR_ANY, sizeof(*bdaddr)) == 0)
132 			usage();
133 
134 		memset(&sa, 0, sizeof(sa));
135 		sa.l2cap_len = sizeof(sa);
136 		sa.l2cap_family = AF_BLUETOOTH;
137 		memcpy(&sa.l2cap_bdaddr, bdaddr, sizeof(sa.l2cap_bdaddr));
138 
139 		s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_L2CAP);
140 		if (s < 0)
141 			err(1, "Could not create socket");
142 
143 		if (bind(s, (struct sockaddr *) &sa, sizeof(sa)) < 0)
144 			err(2,
145 "Could not bind socket, bdaddr=%s", bt_ntoa(&sa.l2cap_bdaddr, NULL));
146 
147 		e = 0x0ffff;
148 		if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &e, sizeof(e)) < 0)
149 			err(3, "Could not setsockopt(RCVBUF, %d)", e);
150 
151 		e = (c->handler)(s, -- argc, ++ argv);
152 
153 		close(s);
154 	} else
155 		e = USAGE;
156 
157 	switch (e) {
158 	case OK:
159 	case FAILED:
160 		break;
161 
162 	case ERROR:
163 		fprintf(stdout, "Could not execute command \"%s\". %s\n",
164 			cmd, strerror(errno));
165 		break;
166 
167 	case USAGE:
168 		fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);
169 		break;
170 
171 	default: assert(0); break;
172 	}
173 
174 	return (e);
175 } /* do_l2cap_command */
176 
177 /* Try to find command in specified category */
178 static struct l2cap_command *
179 find_l2cap_command(char const *command, struct l2cap_command *category)
180 {
181 	struct l2cap_command	*c = NULL;
182 
183 	for (c = category; c->command != NULL; c++) {
184 		char 	*c_end = strchr(c->command, ' ');
185 
186 		if (c_end != NULL) {
187 			int	len = c_end - c->command;
188 
189 			if (strncasecmp(command, c->command, len) == 0)
190 				return (c);
191 		} else if (strcasecmp(command, c->command) == 0)
192 				return (c);
193 	}
194 
195 	return (NULL);
196 } /* find_l2cap_command */
197 
198 /* Print commands in specified category */
199 static void
200 print_l2cap_command(struct l2cap_command *category)
201 {
202 	struct l2cap_command	*c = NULL;
203 
204 	for (c = category; c->command != NULL; c++)
205 		fprintf(stdout, "\t%s\n", c->command);
206 } /* print_l2cap_command */
207 
208 /* Usage */
209 static void
210 usage(void)
211 {
212 	fprintf(stderr, "Usage: l2control [-hn] -a local cmd [params ..]\n");
213 	fprintf(stderr, "Where:\n");
214 	fprintf(stderr, "  -a local   Specify local device to connect to\n");
215 	fprintf(stderr, "  -h         Display this message\n");
216 	fprintf(stderr, "  -n         Show addresses as numbers\n");
217 	fprintf(stderr, "  cmd        Supported command " \
218 		"(see l2control help)\n");
219 	fprintf(stderr, "  params     Optional command parameters\n");
220 	exit(255);
221 } /* usage */
222 
223