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