1 /* $NetBSD: btpin.c,v 1.3 2007/04/14 09:28:39 plunky Exp $ */
2 /* $DragonFly: src/usr.bin/btpin/btpin.c,v 1.2 2008/07/10 18:29:51 swildner Exp $ */
5 * Copyright (c) 2006 Itronix Inc.
8 * Written by Iain Hibbert for Itronix Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of Itronix Inc. may not be used to endorse
19 * or promote products derived from this software without specific
20 * prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
26 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/types.h>
37 #include <bluetooth.h>
44 int main(int, char *[]);
48 main(int ac
, char *av
[])
50 bthcid_pin_response_t rp
;
51 struct sockaddr_un un
;
55 memset(&rp
, 0, sizeof(rp
));
58 memset(&un
, 0, sizeof(un
));
59 un
.sun_len
= sizeof(un
);
60 un
.sun_family
= AF_LOCAL
;
61 strlcpy(un
.sun_path
, BTHCID_SOCKET_NAME
, sizeof(un
.sun_path
));
63 while ((ch
= getopt(ac
, av
, "a:d:l:p:rs:")) != -1) {
66 if (!bt_aton(optarg
, &rp
.raddr
)) {
67 struct hostent
*he
= NULL
;
69 if ((he
= bt_gethostbyname(optarg
)) == NULL
)
70 errx(EXIT_FAILURE
, "%s: %s", optarg
,
73 bdaddr_copy(&rp
.raddr
, (bdaddr_t
*)he
->h_addr
);
78 if (!bt_devaddr(optarg
, &rp
.laddr
))
79 err(EXIT_FAILURE
, "%s", optarg
);
85 if (len
< 1 || len
> HCI_PIN_SIZE
)
86 errx(EXIT_FAILURE
, "Invalid PIN length");
101 strlcpy(un
.sun_path
, optarg
, sizeof(un
.sun_path
));
109 if (bdaddr_any(&rp
.raddr
))
118 pin
= (char *)rp
.pin
;
120 *pin
++ = '0' + (random() % 10);
122 printf("PIN: %.*s\n", HCI_PIN_SIZE
, rp
.pin
);
127 strncpy((char *)rp
.pin
, pin
, HCI_PIN_SIZE
);
130 s
= socket(PF_LOCAL
, SOCK_STREAM
, 0);
132 err(EXIT_FAILURE
, "socket");
134 if (connect(s
, (struct sockaddr
*)&un
, sizeof(un
)) < 0)
135 err(EXIT_FAILURE
, "connect(\"%s\")", un
.sun_path
);
137 if (send(s
, &rp
, sizeof(rp
), 0) != sizeof(rp
))
138 err(EXIT_FAILURE
, "send");
149 "usage: %s [-d device] [-s socket] {-p pin | -r [-l len]} -a addr\n"