-
Notifications
You must be signed in to change notification settings - Fork 82
/
client.c
348 lines (287 loc) · 7.96 KB
/
client.c
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* $NetBSD: client.c,v 1.4 2006/09/29 20:06:11 plunky Exp $ */
/*-
* Copyright (c) 2006 Itronix Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of Itronix Inc. may not be used to endorse
* or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: client.c,v 1.4 2006/09/29 20:06:11 plunky Exp $");
#include <sys/ioctl.h>
#include <sys/queue.h>
#include <sys/time.h>
#include <sys/un.h>
#include <bluetooth.h>
#include <errno.h>
#include <event.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include "bthcid.h"
/*
* A client is anybody who connects to our control socket to
* receive PIN requests.
*/
struct client {
struct event ev;
int fd; /* client descriptor */
LIST_ENTRY(client) next;
};
/*
* PIN cache items are made when we have sent a client pin
* request. The event is used to expire the item.
*/
struct item {
struct event ev;
bdaddr_t laddr; /* local device BDADDR */
bdaddr_t raddr; /* remote device BDADDR */
uint8_t pin[HCI_PIN_SIZE]; /* PIN */
int hci; /* HCI socket */
LIST_ENTRY(item) next;
};
static struct event control_ev;
static LIST_HEAD(,client) client_list;
static LIST_HEAD(,item) item_list;
static void process_control (int, short, void *);
static void process_client (int, short, void *);
static void process_item (int, short, void *);
#define PIN_REQUEST_TIMEOUT 30 /* Request is valid */
#define PIN_TIMEOUT 300 /* PIN is valid */
int
init_control(const char *name, mode_t mode)
{
struct sockaddr_un un;
int ctl;
LIST_INIT(&client_list);
LIST_INIT(&item_list);
if (name == NULL)
return 0;
if (unlink(name) < 0 && errno != ENOENT)
return -1;
ctl = socket(PF_LOCAL, SOCK_STREAM, 0);
if (ctl < 0)
return -1;
memset(&un, 0, sizeof(un));
un.sun_len = sizeof(un);
un.sun_family = AF_LOCAL;
strlcpy(un.sun_path, name, sizeof(un.sun_path));
if (bind(ctl, (struct sockaddr *)&un, sizeof(un)) < 0) {
close(ctl);
return -1;
}
if (chmod(name, mode) < 0) {
close(ctl);
unlink(name);
return -1;
}
if (listen(ctl, 10) < 0) {
close(ctl);
unlink(name);
return -1;
}
event_set(&control_ev, ctl, EV_READ | EV_PERSIST, process_control, NULL);
if (event_add(&control_ev, NULL) < 0) {
close(ctl);
unlink(name);
return -1;
}
return 0;
}
/* Process control socket event */
static void
process_control(int sock, short ev, void *arg)
{
struct sockaddr_un un;
socklen_t n;
int fd;
struct client *cl;
n = sizeof(un);
fd = accept(sock, (struct sockaddr *)&un, &n);
if (fd < 0) {
syslog(LOG_ERR, "Could not accept PIN client connection");
return;
}
n = 1;
if (ioctl(fd, FIONBIO, &n) < 0) {
syslog(LOG_ERR, "Could not set non blocking IO for client");
close(fd);
return;
}
cl = malloc(sizeof(struct client));
if (cl == NULL) {
syslog(LOG_ERR, "Could not malloc client");
close(fd);
return;
}
memset(cl, 0, sizeof(struct client));
cl->fd = fd;
event_set(&cl->ev, fd, EV_READ | EV_PERSIST, process_client, cl);
if (event_add(&cl->ev, NULL) < 0) {
syslog(LOG_ERR, "Could not add client event");
free(cl);
close(fd);
return;
}
syslog(LOG_DEBUG, "New Client");
LIST_INSERT_HEAD(&client_list, cl, next);
}
/* Process client response packet */
static void
process_client(int sock, short ev, void *arg)
{
bthcid_pin_response_t rp;
struct timeval tv;
struct sockaddr_bt sa;
struct client *cl = arg;
struct item *item;
int n;
n = recv(sock, &rp, sizeof(rp), 0);
if (n != sizeof(rp)) {
if (n != 0)
syslog(LOG_ERR, "Bad Client");
close(sock);
LIST_REMOVE(cl, next);
free(cl);
syslog(LOG_DEBUG, "Client Closed");
return;
}
syslog(LOG_DEBUG, "Received PIN for %s", bt_ntoa(&rp.raddr, NULL));
LIST_FOREACH(item, &item_list, next) {
if (bdaddr_same(&rp.laddr, &item->laddr) == 0
|| bdaddr_same(&rp.raddr, &item->raddr) == 0)
continue;
evtimer_del(&item->ev);
if (item->hci != -1) {
memset(&sa, 0, sizeof(sa));
sa.bt_len = sizeof(sa);
sa.bt_family = AF_BLUETOOTH;
bdaddr_copy(&sa.bt_bdaddr, &item->laddr);
send_pin_code_reply(item->hci, &sa, &item->raddr, rp.pin);
LIST_REMOVE(item, next);
free(item);
return;
}
goto newpin;
}
item = malloc(sizeof(struct item));
if (item == NULL) {
syslog(LOG_ERR, "Item allocation failed");
return;
}
memset(item, 0, sizeof(struct item));
bdaddr_copy(&item->laddr, &rp.laddr);
bdaddr_copy(&item->raddr, &rp.raddr);
evtimer_set(&item->ev, process_item, item);
LIST_INSERT_HEAD(&item_list, item, next);
newpin:
syslog(LOG_DEBUG, "Caching PIN for %s", bt_ntoa(&rp.raddr, NULL));
memcpy(item->pin, rp.pin, HCI_PIN_SIZE);
item->hci = -1;
tv.tv_sec = PIN_TIMEOUT;
tv.tv_usec = 0;
if (evtimer_add(&item->ev, &tv) < 0) {
syslog(LOG_ERR, "Cannot add event timer for item");
LIST_REMOVE(item, next);
free(item);
}
}
/* Send PIN request to client */
int
send_client_request(bdaddr_t *laddr, bdaddr_t *raddr, int hci)
{
bthcid_pin_request_t cp;
struct client *cl;
struct item *item;
int n = 0;
struct timeval tv;
memset(&cp, 0, sizeof(cp));
bdaddr_copy(&cp.laddr, laddr);
bdaddr_copy(&cp.raddr, raddr);
cp.time = PIN_REQUEST_TIMEOUT;
LIST_FOREACH(cl, &client_list, next) {
if (send(cl->fd, &cp, sizeof(cp), 0) != sizeof(cp))
syslog(LOG_ERR, "send PIN request failed");
else
n++;
}
if (n == 0)
return 0;
syslog(LOG_DEBUG, "Sent PIN requests to %d client%s.",
n, (n == 1 ? "" : "s"));
item = malloc(sizeof(struct item));
if (item == NULL) {
syslog(LOG_ERR, "Cannot allocate PIN request item");
return 0;
}
memset(item, 0, sizeof(struct item));
bdaddr_copy(&item->laddr, laddr);
bdaddr_copy(&item->raddr, raddr);
item->hci = hci;
evtimer_set(&item->ev, process_item, item);
tv.tv_sec = cp.time;
tv.tv_usec = 0;
if (evtimer_add(&item->ev, &tv) < 0) {
syslog(LOG_ERR, "Cannot add request timer");
free(item);
return 0;
}
LIST_INSERT_HEAD(&item_list, item, next);
return 1;
}
/* Process item event (by expiring it) */
static void
process_item(int fd, short ev, void *arg)
{
struct item *item = arg;
syslog(LOG_DEBUG, "PIN for %s expired", bt_ntoa(&item->raddr, NULL));
LIST_REMOVE(item, next);
evtimer_del(&item->ev);
free(item);
}
/* lookup PIN in item cache */
uint8_t *
lookup_pin(bdaddr_t *laddr, bdaddr_t *raddr)
{
static uint8_t pin[HCI_PIN_SIZE];
struct item *item;
LIST_FOREACH(item, &item_list, next) {
if (bdaddr_same(raddr, &item->raddr) == 0)
continue;
if (bdaddr_same(laddr, &item->laddr) == 0
&& bdaddr_any(&item->laddr) == 0)
continue;
if (item->hci >= 0)
break;
syslog(LOG_DEBUG, "Matched PIN from cache");
memcpy(pin, item->pin, sizeof(pin));
LIST_REMOVE(item, next);
evtimer_del(&item->ev);
free(item);
return pin;
}
return NULL;
}