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>
37 #include <bluetooth.h>
49 * A client is anybody who connects to our control socket to
50 * receive PIN requests.
54 int fd; /* client descriptor */
55 LIST_ENTRY(client) next;
59 * PIN cache items are made when we have sent a client pin
60 * request. The event is used to expire the item.
64 bdaddr_t laddr; /* local device BDADDR */
65 bdaddr_t raddr; /* remote device BDADDR */
66 uint8_t pin[HCI_PIN_SIZE]; /* PIN */
67 int hci; /* HCI socket */
68 LIST_ENTRY(item) next;
71 static struct event control_ev;
73 static LIST_HEAD(,client) client_list;
74 static LIST_HEAD(,item) item_list;
76 static void process_control (int, short, void *);
77 static void process_client (int, short, void *);
78 static void process_item (int, short, void *);
80 #define PIN_REQUEST_TIMEOUT 30 /* Request is valid */
81 #define PIN_TIMEOUT 300 /* PIN is valid */
84 init_control(const char *name, mode_t mode)
86 struct sockaddr_un un;
89 LIST_INIT(&client_list);
90 LIST_INIT(&item_list);
95 if (unlink(name) < 0 && errno != ENOENT)
98 ctl = socket(PF_LOCAL, SOCK_STREAM, 0);
102 memset(&un, 0, sizeof(un));
103 un.sun_len = sizeof(un);
104 un.sun_family = AF_LOCAL;
105 strlcpy(un.sun_path, name, sizeof(un.sun_path));
106 if (bind(ctl, (struct sockaddr *)&un, sizeof(un)) < 0) {
111 if (chmod(name, mode) < 0) {
117 if (listen(ctl, 10) < 0) {
123 event_set(&control_ev, ctl, EV_READ | EV_PERSIST, process_control, NULL);
124 if (event_add(&control_ev, NULL) < 0) {
133 /* Process control socket event */
135 process_control(int sock, short ev, void *arg)
137 struct sockaddr_un un;
143 fd = accept(sock, (struct sockaddr *)&un, &n);
145 syslog(LOG_ERR, "Could not accept PIN client connection");
150 if (ioctl(fd, FIONBIO, &n) < 0) {
151 syslog(LOG_ERR, "Could not set non blocking IO for client");
156 cl = malloc(sizeof(struct client));
158 syslog(LOG_ERR, "Could not malloc client");
163 memset(cl, 0, sizeof(struct client));
166 event_set(&cl->ev, fd, EV_READ | EV_PERSIST, process_client, cl);
167 if (event_add(&cl->ev, NULL) < 0) {
168 syslog(LOG_ERR, "Could not add client event");
174 syslog(LOG_DEBUG, "New Client");
175 LIST_INSERT_HEAD(&client_list, cl, next);
178 /* Process client response packet */
180 process_client(int sock, short ev, void *arg)
182 bthcid_pin_response_t rp;
184 struct sockaddr_bt sa;
185 struct client *cl = arg;
189 n = recv(sock, &rp, sizeof(rp), 0);
190 if (n != sizeof(rp)) {
192 syslog(LOG_ERR, "Bad Client");
195 LIST_REMOVE(cl, next);
198 syslog(LOG_DEBUG, "Client Closed");
202 syslog(LOG_DEBUG, "Received PIN for %s", bt_ntoa(&rp.raddr, NULL));
204 LIST_FOREACH(item, &item_list, next) {
205 if (bdaddr_same(&rp.laddr, &item->laddr) == 0
206 || bdaddr_same(&rp.raddr, &item->raddr) == 0)
209 evtimer_del(&item->ev);
210 if (item->hci != -1) {
211 memset(&sa, 0, sizeof(sa));
212 sa.bt_len = sizeof(sa);
213 sa.bt_family = AF_BLUETOOTH;
214 bdaddr_copy(&sa.bt_bdaddr, &item->laddr);
216 send_pin_code_reply(item->hci, &sa, &item->raddr, rp.pin);
217 LIST_REMOVE(item, next);
224 item = malloc(sizeof(struct item));
226 syslog(LOG_ERR, "Item allocation failed");
230 memset(item, 0, sizeof(struct item));
231 bdaddr_copy(&item->laddr, &rp.laddr);
232 bdaddr_copy(&item->raddr, &rp.raddr);
233 evtimer_set(&item->ev, process_item, item);
234 LIST_INSERT_HEAD(&item_list, item, next);
237 syslog(LOG_DEBUG, "Caching PIN for %s", bt_ntoa(&rp.raddr, NULL));
239 memcpy(item->pin, rp.pin, HCI_PIN_SIZE);
242 tv.tv_sec = PIN_TIMEOUT;
245 if (evtimer_add(&item->ev, &tv) < 0) {
246 syslog(LOG_ERR, "Cannot add event timer for item");
247 LIST_REMOVE(item, next);
252 /* Send PIN request to client */
254 send_client_request(bdaddr_t *laddr, bdaddr_t *raddr, int hci)
256 bthcid_pin_request_t cp;
262 memset(&cp, 0, sizeof(cp));
263 bdaddr_copy(&cp.laddr, laddr);
264 bdaddr_copy(&cp.raddr, raddr);
265 cp.time = PIN_REQUEST_TIMEOUT;
267 LIST_FOREACH(cl, &client_list, next) {
268 if (send(cl->fd, &cp, sizeof(cp), 0) != sizeof(cp))
269 syslog(LOG_ERR, "send PIN request failed");
277 syslog(LOG_DEBUG, "Sent PIN requests to %d client%s.",
278 n, (n == 1 ? "" : "s"));
280 item = malloc(sizeof(struct item));
282 syslog(LOG_ERR, "Cannot allocate PIN request item");
286 memset(item, 0, sizeof(struct item));
287 bdaddr_copy(&item->laddr, laddr);
288 bdaddr_copy(&item->raddr, raddr);
290 evtimer_set(&item->ev, process_item, item);
295 if (evtimer_add(&item->ev, &tv) < 0) {
296 syslog(LOG_ERR, "Cannot add request timer");
301 LIST_INSERT_HEAD(&item_list, item, next);
305 /* Process item event (by expiring it) */
307 process_item(int fd, short ev, void *arg)
309 struct item *item = arg;
311 syslog(LOG_DEBUG, "PIN for %s expired", bt_ntoa(&item->raddr, NULL));
312 LIST_REMOVE(item, next);
313 evtimer_del(&item->ev);
317 /* lookup PIN in item cache */
319 lookup_pin(bdaddr_t *laddr, bdaddr_t *raddr)
321 static uint8_t pin[HCI_PIN_SIZE];
324 LIST_FOREACH(item, &item_list, next) {
325 if (bdaddr_same(raddr, &item->raddr) == 0)
328 if (bdaddr_same(laddr, &item->laddr) == 0
329 && bdaddr_any(&item->laddr) == 0)
335 syslog(LOG_DEBUG, "Matched PIN from cache");
336 memcpy(pin, item->pin, sizeof(pin));
338 LIST_REMOVE(item, next);
339 evtimer_del(&item->ev);