1 /* $NetBSD: client.c,v 1.3 2006/09/26 19:18:19 plunky Exp $ */
4 * Copyright (c) 2006 Itronix Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 * 3. The name of Itronix Inc. may not be used to endorse
16 * or promote products derived from this software without specific
17 * prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: client.c,v 1.3 2006/09/26 19:18:19 plunky Exp $");
35 #include <sys/ioctl.h>
36 #include <sys/queue.h>
39 #include <bluetooth.h>
51 * A client is anybody who connects to our control socket to
52 * receive PIN requests.
56 int fd
; /* client descriptor */
57 LIST_ENTRY(client
) next
;
61 * PIN cache items are made when we have sent a client pin
62 * request. The event is used to expire the item.
66 bdaddr_t laddr
; /* local device BDADDR */
67 bdaddr_t raddr
; /* remote device BDADDR */
68 uint8_t pin
[HCI_PIN_SIZE
]; /* PIN */
69 int hci
; /* HCI socket */
70 LIST_ENTRY(item
) next
;
73 static struct event control_ev
;
75 static LIST_HEAD(,client
) client_list
;
76 static LIST_HEAD(,item
) item_list
;
78 static void process_control (int, short, void *);
79 static void process_client (int, short, void *);
80 static void process_item (int, short, void *);
82 #define PIN_REQUEST_TIMEOUT 30 /* Request is valid */
83 #define PIN_TIMEOUT 300 /* PIN is valid */
86 init_control(const char *name
, mode_t mode
)
88 struct sockaddr_un un
;
91 LIST_INIT(&client_list
);
92 LIST_INIT(&item_list
);
97 if (unlink(name
) < 0 && errno
!= ENOENT
)
100 ctl
= socket(PF_LOCAL
, SOCK_STREAM
, 0);
104 memset(&un
, 0, sizeof(un
));
105 un
.sun_len
= sizeof(un
);
106 un
.sun_family
= AF_LOCAL
;
107 strlcpy(un
.sun_path
, name
, sizeof(un
.sun_path
));
108 if (bind(ctl
, (struct sockaddr
*)&un
, sizeof(un
)) < 0) {
113 if (chmod(name
, mode
) < 0) {
119 if (listen(ctl
, 10) < 0) {
125 event_set(&control_ev
, ctl
, EV_READ
| EV_PERSIST
, process_control
, NULL
);
126 if (event_add(&control_ev
, NULL
) < 0) {
135 /* Process control socket event */
137 process_control(int sock
, short ev
, void *arg
)
139 struct sockaddr_un un
;
145 fd
= accept(sock
, (struct sockaddr
*)&un
, &n
);
147 syslog(LOG_ERR
, "Could not accept PIN client connection");
152 if (ioctl(fd
, FIONBIO
, &n
) < 0) {
153 syslog(LOG_ERR
, "Could not set non blocking IO for client");
158 cl
= malloc(sizeof(struct client
));
160 syslog(LOG_ERR
, "Could not malloc client");
165 memset(cl
, 0, sizeof(struct client
));
168 event_set(&cl
->ev
, fd
, EV_READ
| EV_PERSIST
, process_client
, cl
);
169 if (event_add(&cl
->ev
, NULL
) < 0) {
170 syslog(LOG_ERR
, "Could not add client event");
176 syslog(LOG_DEBUG
, "New Client");
177 LIST_INSERT_HEAD(&client_list
, cl
, next
);
180 /* Process client response packet */
182 process_client(int sock
, short ev
, void *arg
)
184 bthcid_pin_response_t rp
;
186 struct sockaddr_bt sa
;
187 struct client
*cl
= arg
;
191 n
= recv(sock
, &rp
, sizeof(rp
), 0);
192 if (n
!= sizeof(rp
)) {
194 syslog(LOG_ERR
, "Bad Client");
197 LIST_REMOVE(cl
, next
);
200 syslog(LOG_DEBUG
, "Client Closed");
204 syslog(LOG_DEBUG
, "Received PIN for %s", bt_ntoa(&rp
.raddr
, NULL
));
206 LIST_FOREACH(item
, &item_list
, next
) {
207 if (bdaddr_same(&rp
.laddr
, &item
->laddr
) == 0
208 || bdaddr_same(&rp
.raddr
, &item
->raddr
) == 0)
211 evtimer_del(&item
->ev
);
212 if (item
->hci
!= -1) {
213 memset(&sa
, 0, sizeof(sa
));
214 sa
.bt_len
= sizeof(sa
);
215 sa
.bt_family
= AF_BLUETOOTH
;
216 bdaddr_copy(&sa
.bt_bdaddr
, &item
->laddr
);
218 send_pin_code_reply(item
->hci
, &sa
, &item
->raddr
, rp
.pin
);
219 LIST_REMOVE(item
, next
);
226 item
= malloc(sizeof(struct item
));
228 syslog(LOG_ERR
, "Item allocation failed");
232 memset(item
, 0, sizeof(struct item
));
233 bdaddr_copy(&item
->laddr
, &rp
.laddr
);
234 bdaddr_copy(&item
->raddr
, &rp
.raddr
);
235 evtimer_set(&item
->ev
, process_item
, item
);
236 LIST_INSERT_HEAD(&item_list
, item
, next
);
239 syslog(LOG_DEBUG
, "Caching PIN for %s", bt_ntoa(&rp
.raddr
, NULL
));
241 memcpy(item
->pin
, rp
.pin
, HCI_PIN_SIZE
);
244 tv
.tv_sec
= PIN_TIMEOUT
;
247 if (evtimer_add(&item
->ev
, &tv
) < 0) {
248 syslog(LOG_ERR
, "Cannot add event timer for item");
249 LIST_REMOVE(item
, next
);
254 /* Send PIN request to client */
256 send_client_request(bdaddr_t
*laddr
, bdaddr_t
*raddr
, int hci
)
258 bthcid_pin_request_t cp
;
264 memset(&cp
, 0, sizeof(cp
));
265 bdaddr_copy(&cp
.laddr
, laddr
);
266 bdaddr_copy(&cp
.raddr
, raddr
);
267 cp
.time
= PIN_REQUEST_TIMEOUT
;
269 LIST_FOREACH(cl
, &client_list
, next
) {
270 if (send(cl
->fd
, &cp
, sizeof(cp
), 0) != sizeof(cp
))
271 syslog(LOG_ERR
, "send PIN request failed");
279 syslog(LOG_DEBUG
, "Sent PIN requests to %d client%s.",
280 n
, (n
== 1 ? "" : "s"));
282 item
= malloc(sizeof(struct item
));
284 syslog(LOG_ERR
, "Cannot allocate PIN request item");
288 memset(item
, 0, sizeof(struct item
));
289 bdaddr_copy(&item
->laddr
, laddr
);
290 bdaddr_copy(&item
->raddr
, raddr
);
292 evtimer_set(&item
->ev
, process_item
, item
);
297 if (evtimer_add(&item
->ev
, &tv
) < 0) {
298 syslog(LOG_ERR
, "Cannot add request timer");
303 LIST_INSERT_HEAD(&item_list
, item
, next
);
307 /* Process item event (by expiring it) */
309 process_item(int fd
, short ev
, void *arg
)
311 struct item
*item
= arg
;
313 syslog(LOG_DEBUG
, "PIN for %s expired", bt_ntoa(&item
->raddr
, NULL
));
314 LIST_REMOVE(item
, next
);
315 evtimer_del(&item
->ev
);
319 /* lookup PIN in item cache */
321 lookup_pin(bdaddr_t
*laddr
, bdaddr_t
*raddr
)
323 static uint8_t pin
[HCI_PIN_SIZE
];
326 LIST_FOREACH(item
, &item_list
, next
) {
327 if (bdaddr_same(raddr
, &item
->raddr
) == 0)
330 if (bdaddr_same(laddr
, &item
->laddr
) == 0
331 && bdaddr_any(&item
->laddr
) == 0)
337 syslog(LOG_DEBUG
, "Matched PIN from cache");
338 memcpy(pin
, item
->pin
, sizeof(pin
));
340 LIST_REMOVE(item
, next
);
341 evtimer_del(&item
->ev
);