1 /* $NetBSD: btpin.c,v 1.4 2008/07/21 14:19:21 lukem 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/cdefs.h>
35 __COPYRIGHT("@(#) Copyright (c) 2006 Itronix, Inc. All rights reserved.");
36 __RCSID("$NetBSD: btpin.c,v 1.4 2008/07/21 14:19:21 lukem Exp $");
38 #include <sys/types.h>
40 #include <bluetooth.h>
48 int main(int, char *[]);
52 main(int ac
, char *av
[])
54 bthcid_pin_response_t rp
;
55 struct sockaddr_un un
;
56 struct sockaddr_bt bt
;
60 memset(&rp
, 0, sizeof(rp
));
64 memset(&un
, 0, sizeof(un
));
65 un
.sun_len
= sizeof(un
);
66 un
.sun_family
= AF_LOCAL
;
67 strlcpy(un
.sun_path
, BTHCID_SOCKET_NAME
, sizeof(un
.sun_path
));
69 while ((ch
= getopt(ac
, av
, "a:d:l:Pp:rs:")) != -1) {
72 if (!bt_aton(optarg
, &rp
.raddr
)) {
73 struct hostent
*he
= NULL
;
75 if ((he
= bt_gethostbyname(optarg
)) == NULL
)
76 errx(EXIT_FAILURE
, "%s: %s", optarg
,
79 bdaddr_copy(&rp
.raddr
, (bdaddr_t
*)he
->h_addr
);
84 if (!bt_devaddr(optarg
, &rp
.laddr
))
85 err(EXIT_FAILURE
, "%s", optarg
);
91 if (len
< 1 || len
> HCI_PIN_SIZE
)
92 errx(EXIT_FAILURE
, "Invalid PIN length");
111 strlcpy(un
.sun_path
, optarg
, sizeof(un
.sun_path
));
119 if (bdaddr_any(&rp
.raddr
))
128 pin
= (char *)rp
.pin
;
130 *pin
++ = '0' + (random() % 10);
132 printf("PIN: %.*s\n", HCI_PIN_SIZE
, rp
.pin
);
137 strncpy((char *)rp
.pin
, pin
, HCI_PIN_SIZE
);
140 s
= socket(PF_LOCAL
, SOCK_STREAM
, 0);
142 err(EXIT_FAILURE
, "socket");
144 if (connect(s
, (struct sockaddr
*)&un
, sizeof(un
)) < 0)
145 err(EXIT_FAILURE
, "connect(\"%s\")", un
.sun_path
);
147 if (send(s
, &rp
, sizeof(rp
), 0) != sizeof(rp
))
148 err(EXIT_FAILURE
, "send");
155 s
= socket(PF_BLUETOOTH
, SOCK_SEQPACKET
, BTPROTO_L2CAP
);
157 err(EXIT_FAILURE
, "socket");
160 if (setsockopt(s
, BTPROTO_L2CAP
, SO_L2CAP_LM
, &ch
, sizeof(ch
)) < 0)
161 err(EXIT_FAILURE
, "SO_L2CAP_LM");
163 memset(&bt
, 0, sizeof(bt
));
164 bt
.bt_len
= sizeof(bt
);
165 bt
.bt_family
= AF_BLUETOOTH
;
166 bdaddr_copy(&bt
.bt_bdaddr
, &rp
.laddr
);
167 if (bind(s
, (struct sockaddr
*)&bt
, sizeof(bt
)) < 0)
168 err(EXIT_FAILURE
, "bind");
170 fprintf(stdout
, "Pairing.. ");
173 bt
.bt_psm
= L2CAP_PSM_SDP
;
174 bdaddr_copy(&bt
.bt_bdaddr
, &rp
.raddr
);
175 if (connect(s
, (struct sockaddr
*)&bt
, sizeof(bt
)) < 0) {
176 fprintf(stdout
, "failed (%s)\n", strerror(errno
));
181 fprintf(stdout
, "done\n");
191 "usage: %s [-P] [-d device] [-s socket] {-p pin | -r [-l len]} -a addr\n"