4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
30 * $Id: l2control.c,v 1.6 2003/09/05 00:38:25 max Exp $
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
43 #include "l2control.h"
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);
54 int numeric_bdaddr = 0;
57 main(int argc, char *argv[])
62 memset(&bdaddr, 0, sizeof(bdaddr));
64 /* Process command line arguments */
65 while ((n = getopt(argc, argv, "a:nh")) != -1) {
68 if (!bt_aton(optarg, &bdaddr)) {
69 struct hostent *he = NULL;
71 if ((he = bt_gethostbyname(optarg)) == NULL)
72 errx(1, "%s: %s", optarg, hstrerror(h_errno));
74 memcpy(&bdaddr, he->h_addr, sizeof(bdaddr));
95 return (do_l2cap_command(&bdaddr, argc, argv));
98 /* Execute commands */
100 do_l2cap_command(bdaddr_p bdaddr, int argc, char **argv)
103 struct l2cap_command *c = NULL;
104 struct sockaddr_l2cap sa;
108 if (strcasecmp(cmd, "help") == 0) {
113 fprintf(stdout, "Supported commands:\n");
114 print_l2cap_command(l2cap_commands);
115 fprintf(stdout, "\nFor more information use " \
125 c = find_l2cap_command(cmd, l2cap_commands);
127 fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
132 if (memcmp(bdaddr, NG_HCI_BDADDR_ANY, sizeof(*bdaddr)) == 0)
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));
140 s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_L2CAP);
142 err(1, "Could not create socket");
144 if (bind(s, (struct sockaddr *) &sa, sizeof(sa)) < 0)
146 "Could not bind socket, bdaddr=%s", bt_ntoa(&sa.l2cap_bdaddr, NULL));
149 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &e, sizeof(e)) < 0)
150 err(3, "Coult not setsockopt(RCVBUF, %d)", e);
152 e = (c->handler)(s, -- argc, ++ argv);
164 fprintf(stdout, "Could not execute command \"%s\". %s\n",
165 cmd, strerror(errno));
169 fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);
172 default: assert(0); break;
176 } /* do_l2cap_command */
178 /* Try to find command in specified category */
179 static struct l2cap_command *
180 find_l2cap_command(char const *command, struct l2cap_command *category)
182 struct l2cap_command *c = NULL;
184 for (c = category; c->command != NULL; c++) {
185 char *c_end = strchr(c->command, ' ');
188 int len = c_end - c->command;
190 if (strncasecmp(command, c->command, len) == 0)
192 } else if (strcasecmp(command, c->command) == 0)
197 } /* find_l2cap_command */
199 /* Print commands in specified category */
201 print_l2cap_command(struct l2cap_command *category)
203 struct l2cap_command *c = NULL;
205 for (c = category; c->command != NULL; c++)
206 fprintf(stdout, "\t%s\n", c->command);
207 } /* print_l2cap_command */
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");