1 /* $NetBSD: client.c,v 1.4 2006/09/29 20:06:11 plunky Exp $ */
2 /* $DragonFly: src/usr.sbin/bthcid/client.c,v 1.1 2008/01/30 14:10:19 hasso Exp $ */
5 * Copyright (c) 2006 Itronix Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of Itronix Inc. may not be used to endorse
17 * or promote products derived from this software without specific
18 * prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/ioctl.h>
34 #include <sys/queue.h>
38 #include <bluetooth.h>
50 * A client is anybody who connects to our control socket to
51 * receive PIN requests.
55 int fd
; /* client descriptor */
56 LIST_ENTRY(client
) next
;
60 * PIN cache items are made when we have sent a client pin
61 * request. The event is used to expire the item.
65 bdaddr_t laddr
; /* local device BDADDR */
66 bdaddr_t raddr
; /* remote device BDADDR */
67 uint8_t pin
[HCI_PIN_SIZE
]; /* PIN */
68 int hci
; /* HCI socket */
69 LIST_ENTRY(item
) next
;
72 static struct event control_ev
;
74 static LIST_HEAD(,client
) client_list
;
75 static LIST_HEAD(,item
) item_list
;
77 static void process_control (int, short, void *);
78 static void process_client (int, short, void *);
79 static void process_item (int, short, void *);
81 #define PIN_REQUEST_TIMEOUT 30 /* Request is valid */
82 #define PIN_TIMEOUT 300 /* PIN is valid */
85 init_control(const char *name
, mode_t mode
)
87 struct sockaddr_un un
;
90 LIST_INIT(&client_list
);
91 LIST_INIT(&item_list
);
96 if (unlink(name
) < 0 && errno
!= ENOENT
)
99 ctl
= socket(PF_LOCAL
, SOCK_STREAM
, 0);
103 memset(&un
, 0, sizeof(un
));
104 un
.sun_len
= sizeof(un
);
105 un
.sun_family
= AF_LOCAL
;
106 strlcpy(un
.sun_path
, name
, sizeof(un
.sun_path
));
107 if (bind(ctl
, (struct sockaddr
*)&un
, sizeof(un
)) < 0) {
112 if (chmod(name
, mode
) < 0) {
118 if (listen(ctl
, 10) < 0) {
124 event_set(&control_ev
, ctl
, EV_READ
| EV_PERSIST
, process_control
, NULL
);
125 if (event_add(&control_ev
, NULL
) < 0) {
134 /* Process control socket event */
136 process_control(int sock
, short ev __unused
, void *arg __unused
)
138 struct sockaddr_un un
;
144 fd
= accept(sock
, (struct sockaddr
*)&un
, &n
);
146 syslog(LOG_ERR
, "Could not accept PIN client connection");
151 if (ioctl(fd
, FIONBIO
, &n
) < 0) {
152 syslog(LOG_ERR
, "Could not set non blocking IO for client");
157 cl
= malloc(sizeof(struct client
));
159 syslog(LOG_ERR
, "Could not malloc client");
164 memset(cl
, 0, sizeof(struct client
));
167 event_set(&cl
->ev
, fd
, EV_READ
| EV_PERSIST
, process_client
, cl
);
168 if (event_add(&cl
->ev
, NULL
) < 0) {
169 syslog(LOG_ERR
, "Could not add client event");
175 syslog(LOG_DEBUG
, "New Client");
176 LIST_INSERT_HEAD(&client_list
, cl
, next
);
179 /* Process client response packet */
181 process_client(int sock
, short ev __unused
, void *arg
)
183 bthcid_pin_response_t rp
;
185 struct sockaddr_bt sa
;
186 struct client
*cl
= arg
;
190 n
= recv(sock
, &rp
, sizeof(rp
), 0);
191 if (n
!= sizeof(rp
)) {
193 syslog(LOG_ERR
, "Bad Client");
196 LIST_REMOVE(cl
, next
);
199 syslog(LOG_DEBUG
, "Client Closed");
203 syslog(LOG_DEBUG
, "Received PIN for %s", bt_ntoa(&rp
.raddr
, NULL
));
205 LIST_FOREACH(item
, &item_list
, next
) {
206 if (bdaddr_same(&rp
.laddr
, &item
->laddr
) == 0
207 || bdaddr_same(&rp
.raddr
, &item
->raddr
) == 0)
210 evtimer_del(&item
->ev
);
211 if (item
->hci
!= -1) {
212 memset(&sa
, 0, sizeof(sa
));
213 sa
.bt_len
= sizeof(sa
);
214 sa
.bt_family
= AF_BLUETOOTH
;
215 bdaddr_copy(&sa
.bt_bdaddr
, &item
->laddr
);
217 send_pin_code_reply(item
->hci
, &sa
, &item
->raddr
, rp
.pin
);
218 LIST_REMOVE(item
, next
);
225 item
= malloc(sizeof(struct item
));
227 syslog(LOG_ERR
, "Item allocation failed");
231 memset(item
, 0, sizeof(struct item
));
232 bdaddr_copy(&item
->laddr
, &rp
.laddr
);
233 bdaddr_copy(&item
->raddr
, &rp
.raddr
);
234 evtimer_set(&item
->ev
, process_item
, item
);
235 LIST_INSERT_HEAD(&item_list
, item
, next
);
238 syslog(LOG_DEBUG
, "Caching PIN for %s", bt_ntoa(&rp
.raddr
, NULL
));
240 memcpy(item
->pin
, rp
.pin
, HCI_PIN_SIZE
);
243 tv
.tv_sec
= PIN_TIMEOUT
;
246 if (evtimer_add(&item
->ev
, &tv
) < 0) {
247 syslog(LOG_ERR
, "Cannot add event timer for item");
248 LIST_REMOVE(item
, next
);
253 /* Send PIN request to client */
255 send_client_request(bdaddr_t
*laddr
, bdaddr_t
*raddr
, int hci
)
257 bthcid_pin_request_t cp
;
263 memset(&cp
, 0, sizeof(cp
));
264 bdaddr_copy(&cp
.laddr
, laddr
);
265 bdaddr_copy(&cp
.raddr
, raddr
);
266 cp
.time
= PIN_REQUEST_TIMEOUT
;
268 LIST_FOREACH(cl
, &client_list
, next
) {
269 if (send(cl
->fd
, &cp
, sizeof(cp
), 0) != sizeof(cp
))
270 syslog(LOG_ERR
, "send PIN request failed");
278 syslog(LOG_DEBUG
, "Sent PIN requests to %d client%s.",
279 n
, (n
== 1 ? "" : "s"));
281 item
= malloc(sizeof(struct item
));
283 syslog(LOG_ERR
, "Cannot allocate PIN request item");
287 memset(item
, 0, sizeof(struct item
));
288 bdaddr_copy(&item
->laddr
, laddr
);
289 bdaddr_copy(&item
->raddr
, raddr
);
291 evtimer_set(&item
->ev
, process_item
, item
);
296 if (evtimer_add(&item
->ev
, &tv
) < 0) {
297 syslog(LOG_ERR
, "Cannot add request timer");
302 LIST_INSERT_HEAD(&item_list
, item
, next
);
306 /* Process item event (by expiring it) */
308 process_item(int fd __unused
, short ev __unused
, void *arg
)
310 struct item
*item
= arg
;
312 syslog(LOG_DEBUG
, "PIN for %s expired", bt_ntoa(&item
->raddr
, NULL
));
313 LIST_REMOVE(item
, next
);
314 evtimer_del(&item
->ev
);
318 /* lookup PIN in item cache */
320 lookup_pin(bdaddr_t
*laddr
, bdaddr_t
*raddr
)
322 static uint8_t pin
[HCI_PIN_SIZE
];
325 LIST_FOREACH(item
, &item_list
, next
) {
326 if (bdaddr_same(raddr
, &item
->raddr
) == 0)
329 if (bdaddr_same(laddr
, &item
->laddr
) == 0
330 && bdaddr_any(&item
->laddr
) == 0)
336 syslog(LOG_DEBUG
, "Matched PIN from cache");
337 memcpy(pin
, item
->pin
, sizeof(pin
));
339 LIST_REMOVE(item
, next
);
340 evtimer_del(&item
->ev
);