1 /* $NetBSD: btpin.c,v 1.3 2007/04/14 09:28:39 plunky Exp $ */
4 * Copyright (c) 2006 Itronix Inc.
7 * Written by Iain Hibbert for Itronix Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of Itronix Inc. may not be used to endorse
18 * or promote products derived from this software without specific
19 * prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/types.h>
36 #include <bluetooth.h>
43 static void usage(void) __dead2;
46 main(int ac, char *av[])
48 bthcid_pin_response_t rp;
49 struct sockaddr_un un;
53 memset(&rp, 0, sizeof(rp));
56 memset(&un, 0, sizeof(un));
57 un.sun_len = sizeof(un);
58 un.sun_family = AF_LOCAL;
59 strlcpy(un.sun_path, BTHCID_SOCKET_NAME, sizeof(un.sun_path));
61 while ((ch = getopt(ac, av, "a:d:l:p:rs:")) != -1) {
64 if (!bt_aton(optarg, &rp.raddr)) {
65 struct hostent *he = NULL;
67 if ((he = bt_gethostbyname(optarg)) == NULL)
68 errx(EXIT_FAILURE, "%s: %s", optarg,
71 bdaddr_copy(&rp.raddr, (bdaddr_t *)he->h_addr);
76 if (!bt_devaddr(optarg, &rp.laddr))
77 err(EXIT_FAILURE, "%s", optarg);
83 if (len < 1 || len > HCI_PIN_SIZE)
84 errx(EXIT_FAILURE, "Invalid PIN length");
99 strlcpy(un.sun_path, optarg, sizeof(un.sun_path));
107 if (bdaddr_any(&rp.raddr))
116 pin = (char *)rp.pin;
118 *pin++ = '0' + (random() % 10);
120 printf("PIN: %.*s\n", HCI_PIN_SIZE, rp.pin);
125 strncpy((char *)rp.pin, pin, HCI_PIN_SIZE);
128 s = socket(PF_LOCAL, SOCK_STREAM, 0);
130 err(EXIT_FAILURE, "socket");
132 if (connect(s, (struct sockaddr *)&un, sizeof(un)) < 0)
133 err(EXIT_FAILURE, "connect(\"%s\")", un.sun_path);
135 if (send(s, &rp, sizeof(rp), 0) != sizeof(rp))
136 err(EXIT_FAILURE, "send");
147 "usage: %s [-d device] [-s socket] {-p pin | -r [-l len]} -a addr\n"