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
|
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2004-2007 Marcel Holtmann <marcel@holtmann.org>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/l2cap.h>
#include <bluetooth/hidp.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <glib.h>
#include "logging.h"
#include "dbus.h"
#include "device.h"
#include "server.h"
#include "storage.h"
static DBusConnection *connection = NULL;
static void cancel_authorization(const char *addr)
{
DBusMessage *msg;
const char *uuid = "";
msg = dbus_message_new_method_call("org.bluez", "/org/bluez",
"org.bluez.Database",
"CancelAuthorizationRequest");
if (!msg) {
error("Unable to allocate new method call");
return;
}
dbus_message_append_args(msg,
DBUS_TYPE_STRING, &addr,
DBUS_TYPE_STRING, &uuid,
DBUS_TYPE_INVALID);
send_message_and_unref(connection, msg);
}
struct authorization_data {
bdaddr_t src;
bdaddr_t dst;
};
static void authorization_callback(DBusPendingCall *pcall, void *data)
{
struct authorization_data *auth = data;
DBusMessage *reply = dbus_pending_call_steal_reply(pcall);
DBusError derr;
dbus_error_init(&derr);
if (dbus_set_error_from_message(&derr, reply) != TRUE) {
dbus_message_unref(reply);
input_device_connadd(&auth->src, &auth->dst);
return;
}
error("Authorization denied: %s", derr.message);
if (dbus_error_has_name(&derr, DBUS_ERROR_NO_REPLY)) {
char addr[18];
memset(addr, 0, sizeof(addr));
ba2str(&auth->dst, addr);
cancel_authorization(addr);
}
input_device_close_channels(&auth->src, &auth->dst);
dbus_error_free(&derr);
dbus_message_unref(reply);
}
static int authorize_device(bdaddr_t *src, bdaddr_t *dst)
{
struct authorization_data *auth;
DBusMessage *msg;
DBusPendingCall *pending;
char addr[18];
const char *paddr = addr;
const char *uuid = ""; /* FIXME: */
msg = dbus_message_new_method_call("org.bluez", "/org/bluez",
"org.bluez.Database", "RequestAuthorization");
if (!msg) {
error("Unable to allocat new RequestAuthorization method call");
return -ENOMEM;
}
memset(addr, 0, sizeof(addr));
ba2str(dst, addr);
dbus_message_append_args(msg,
DBUS_TYPE_STRING, &paddr,
DBUS_TYPE_STRING, &uuid,
DBUS_TYPE_INVALID);
if (dbus_connection_send_with_reply(connection,
msg, &pending, -1) == FALSE)
return -EACCES;
auth = g_new0(struct authorization_data, 1);
bacpy(&auth->src, src);
bacpy(&auth->dst, dst);
dbus_pending_call_set_notify(pending, authorization_callback, auth, g_free);
dbus_pending_call_unref(pending);
dbus_message_unref(msg);
return 0;
}
static gboolean connect_event(GIOChannel *chan, GIOCondition cond, gpointer data)
{
struct sockaddr_l2 addr;
socklen_t addrlen;
bdaddr_t src, dst;
unsigned char psm;
int sk, nsk;
sk = g_io_channel_unix_get_fd(chan);
memset(&addr, 0, sizeof(addr));
addrlen = sizeof(addr);
nsk = accept(sk, (struct sockaddr *) &addr, &addrlen);
if (nsk < 0)
return TRUE;
bacpy(&dst, &addr.l2_bdaddr);
psm = btohs(addr.l2_psm);
memset(&addr, 0, sizeof(addr));
addrlen = sizeof(addr);
if (getsockname(nsk, (struct sockaddr *) &addr, &addrlen) < 0) {
close(nsk);
return TRUE;
}
bacpy(&src, &addr.l2_bdaddr);
debug("Incoming connection on PSM %d", psm);
if (input_device_set_channel(&src, &dst, psm, nsk) < 0) {
/* Send unplug virtual cable to unknown devices */
if (psm == L2CAP_PSM_HIDP_CTRL) {
int err;
unsigned char unplug = 0x15;
err = write(nsk, &unplug, sizeof(unplug));
}
close(nsk);
return TRUE;
}
if ((psm == L2CAP_PSM_HIDP_INTR) && (authorize_device(&src, &dst) < 0))
input_device_close_channels(&src, &dst);
return TRUE;
}
static GIOChannel *setup_l2cap(unsigned int psm)
{
GIOChannel *io;
struct sockaddr_l2 addr;
int sk;
sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
if (sk < 0)
return NULL;
memset(&addr, 0, sizeof(addr));
addr.l2_family = AF_BLUETOOTH;
bacpy(&addr.l2_bdaddr, BDADDR_ANY);
addr.l2_psm = htobs(psm);
if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
close(sk);
return NULL;
}
if (listen(sk, 10) < 0) {
close(sk);
return NULL;
}
io = g_io_channel_unix_new(sk);
g_io_channel_set_close_on_unref(io, TRUE);
g_io_add_watch(io, G_IO_IN, connect_event, NULL);
return io;
}
static GIOChannel *ctrl_io = NULL;
static GIOChannel *intr_io = NULL;
int server_start(DBusConnection *conn)
{
ctrl_io = setup_l2cap(L2CAP_PSM_HIDP_CTRL);
if (!ctrl_io) {
error("Failed to listen on control channel");
return -1;
}
g_io_channel_set_close_on_unref(ctrl_io, TRUE);
intr_io = setup_l2cap(L2CAP_PSM_HIDP_INTR);
if (!intr_io) {
error("Failed to listen on interrupt channel");
g_io_channel_unref(ctrl_io);
ctrl_io = NULL;
}
g_io_channel_set_close_on_unref(intr_io, TRUE);
connection = conn;
return 0;
}
void server_stop(void)
{
if (intr_io)
g_io_channel_unref(intr_io);
if (ctrl_io)
g_io_channel_unref(ctrl_io);
}
|