1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
/* $OpenBSD: control.c,v 1.3 2008/11/26 06:51:43 uwe Exp $ */
/*
* Copyright (c) 2008 Uwe Stuehler <uwe@openbsd.org>
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <errno.h>
#include <event.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "btd.h"
#include "btctl.h"
#define CONTROL_BACKLOG 5
void socket_blockmode(int, int);
void control_acceptcb(int, short, void *);
void control_readcb(struct bufferevent *, void *);
void control_errorcb(struct bufferevent *, short, void *);
int control_interface_stmt(struct btd *, btctl_interface_stmt *);
int control_attach_stmt(struct btd *, btctl_attach_stmt *);
int control_commit(struct btd *, const struct btd *);
struct control_connection {
TAILQ_ENTRY(control_connection) entry;
struct bufferevent *ev;
struct btd *env;
struct btd *new_env;
int fd;
};
static struct event ev_control;
static TAILQ_HEAD(, control_connection) connections =
TAILQ_HEAD_INITIALIZER(connections);
void
control_init(struct btd *env)
{
struct sockaddr_un sun;
mode_t old_umask;
int fd;
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
fatal("control_init: socket");
sun.sun_family = AF_UNIX;
if (strlcpy(sun.sun_path, BTD_SOCKET,
sizeof(sun.sun_path)) >= sizeof(sun.sun_path))
fatalx("control_init: socket name too long");
if (unlink(BTD_SOCKET) == -1)
if (errno != ENOENT)
fatal("control_init: unlink");
old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1)
fatal("control_init: bind");
(void)umask(old_umask);
if (chmod(BTD_SOCKET, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
log_warn("control_init: chmod");
close(fd);
(void)unlink(BTD_SOCKET);
exit(1);
}
if (listen(fd, CONTROL_BACKLOG) == -1)
fatal("control_init: listen");
socket_blockmode(fd, 0);
event_set(&ev_control, fd, EV_READ | EV_PERSIST,
control_acceptcb, env);
event_add(&ev_control, NULL);
}
/* called from priv process */
void
control_cleanup(void)
{
if (unlink(BTD_SOCKET) == -1)
fatal("control_init: unlink");
}
void
socket_blockmode(int fd, int block)
{
int flags;
if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
fatal("fcntl F_GETFL");
if (block)
flags &= ~O_NONBLOCK;
else
flags |= O_NONBLOCK;
if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
fatal("fcntl F_SETFL");
}
void
control_acceptcb(int fd, short evflags, void *arg)
{
struct btd *env = arg;
struct control_connection *conn;
struct sockaddr_storage sa;
socklen_t salen;
int new_fd;
salen = sizeof(sa);
if ((new_fd = accept(fd, (struct sockaddr *)&sa, &salen)) == -1) {
log_warn("control_eventcb: accept");
return;
}
if ((conn = calloc(1, sizeof(*conn))) == NULL)
fatal("control_eventcb: calloc");
conn->env = env;
conn->fd = new_fd;
if ((conn->ev = bufferevent_new(new_fd, control_readcb, NULL,
control_errorcb, conn)) == NULL)
fatal("control_errorcb: bufferevent_new");
TAILQ_INSERT_TAIL(&connections, conn, entry);
bufferevent_enable(conn->ev, EV_READ);
}
void
control_readcb(struct bufferevent *ev, void *arg)
{
btctl_interface_stmt interface_stmt;
btctl_attach_stmt attach_stmt;
struct control_connection *conn = arg;
enum btctl_stmt_type stmt_type;
size_t stmt_size = 0;
int err;
if (EVBUFFER_LENGTH(EVBUFFER_INPUT(ev)) < sizeof(stmt_type))
return;
memcpy(&stmt_type, EVBUFFER_DATA(EVBUFFER_INPUT(ev)),
sizeof(stmt_type));
switch (stmt_type) {
case BTCTL_CONFIG:
case BTCTL_COMMIT:
case BTCTL_ROLLBACK:
break;
case BTCTL_INTERFACE_STMT:
stmt_size += sizeof(btctl_interface_stmt);
break;
case BTCTL_ATTACH_STMT:
stmt_size += sizeof(btctl_attach_stmt);
break;
}
if (EVBUFFER_LENGTH(EVBUFFER_INPUT(ev)) <= stmt_size)
return;
bufferevent_read(ev, &stmt_type, sizeof(stmt_type));
/*log_debug("stmt %#x size %lu", stmt_type, stmt_size);*/
switch (stmt_type) {
case BTCTL_CONFIG:
if (conn->new_env == NULL) {
conn->new_env = conf_new();
err = conn->new_env == NULL ? ENOMEM : 0;
} else
err = EALREADY;
break;
case BTCTL_COMMIT:
if (conn->new_env != NULL) {
err = control_commit(conn->env, conn->new_env);
if (err == 0) {
conf_delete(conn->new_env);
conn->new_env = NULL;
}
} else
err = 0;
break;
case BTCTL_ROLLBACK:
if (conn->new_env != NULL) {
conf_delete(conn->new_env);
conn->new_env = NULL;
}
err = 0;
break;
case BTCTL_INTERFACE_STMT:
if (conn->new_env == NULL) {
err = EOPNOTSUPP;
break;
}
bufferevent_read(ev, &interface_stmt, stmt_size);
err = control_interface_stmt(conn->new_env, &interface_stmt);
break;
case BTCTL_ATTACH_STMT:
if (conn->new_env == NULL) {
err = EOPNOTSUPP;
break;
}
bufferevent_read(ev, &attach_stmt, stmt_size);
err = control_attach_stmt(conn->new_env, &attach_stmt);
break;
default:
log_warnx("Invalid control packet of type %#x", stmt_type);
close(conn->fd);
return;
}
bufferevent_write(ev, &err, sizeof(err));
}
void
control_errorcb(struct bufferevent *ev, short what, void *arg)
{
struct control_connection *conn = arg;
TAILQ_REMOVE(&connections, conn, entry);
if (conn->new_env != NULL)
conf_delete(conn->new_env);
bufferevent_free(conn->ev);
close(conn->fd);
free(conn);
}
int
control_interface_stmt(struct btd *conf, btctl_interface_stmt *stmt)
{
struct bt_interface *iface;
if ((iface = conf_add_interface(conf, &stmt->addr)) == NULL)
return errno;
strlcpy(iface->name, stmt->name, sizeof(iface->name));
if (stmt->flags & BTCTL_INTERFACE_DISABLED)
iface->disabled = 1;
return 0;
}
int
control_attach_stmt(struct btd *conf, btctl_attach_stmt *stmt)
{
struct bt_device *btdev;
if ((btdev = conf_add_device(conf, &stmt->addr)) == NULL)
return errno;
btdev->type = stmt->type;
strlcpy(btdev->pin, stmt->pin, sizeof(btdev->pin));
btdev->pin_size = stmt->pin_size;
btdev->flags |= BTDF_ATTACH;
return 0;
}
int
control_commit(struct btd *env, const struct btd *conf)
{
return hci_reinit(env, conf);
}
|