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