No empty .Rs/.Re
[netbsd-mini2440.git] / usr.bin / btpin / btpin.c
blob7665afc24b45897da3f963c3df31d0bb505a4986
1 /* $NetBSD: btpin.c,v 1.4 2008/07/21 14:19:21 lukem Exp $ */
3 /*-
4 * Copyright (c) 2006 Itronix Inc.
5 * All rights reserved.
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
11 * are met:
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>
39 #include <sys/un.h>
40 #include <bluetooth.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <time.h>
46 #include <unistd.h>
48 int main(int, char *[]);
49 void usage(void);
51 int
52 main(int ac, char *av[])
54 bthcid_pin_response_t rp;
55 struct sockaddr_un un;
56 struct sockaddr_bt bt;
57 char *pin = NULL;
58 int ch, s, len, pair;
60 memset(&rp, 0, sizeof(rp));
61 len = -1;
62 pair = 0;
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) {
70 switch (ch) {
71 case 'a':
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,
77 hstrerror(h_errno));
79 bdaddr_copy(&rp.raddr, (bdaddr_t *)he->h_addr);
81 break;
83 case 'd':
84 if (!bt_devaddr(optarg, &rp.laddr))
85 err(EXIT_FAILURE, "%s", optarg);
87 break;
89 case 'l':
90 len = atoi(optarg);
91 if (len < 1 || len > HCI_PIN_SIZE)
92 errx(EXIT_FAILURE, "Invalid PIN length");
94 break;
96 case 'P':
97 pair++;
98 break;
100 case 'p':
101 pin = optarg;
102 break;
104 case 'r':
105 if (len == -1)
106 len = 4;
108 break;
110 case 's':
111 strlcpy(un.sun_path, optarg, sizeof(un.sun_path));
112 break;
114 default:
115 usage();
119 if (bdaddr_any(&rp.raddr))
120 usage();
122 if (pin == NULL) {
123 if (len == -1)
124 usage();
126 srandom(time(NULL));
128 pin = (char *)rp.pin;
129 while (len-- > 0)
130 *pin++ = '0' + (random() % 10);
132 printf("PIN: %.*s\n", HCI_PIN_SIZE, rp.pin);
133 } else {
134 if (len != -1)
135 usage();
137 strncpy((char *)rp.pin, pin, HCI_PIN_SIZE);
140 s = socket(PF_LOCAL, SOCK_STREAM, 0);
141 if (s < 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");
150 close(s);
152 if (pair == 0)
153 exit(EXIT_SUCCESS);
155 s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
156 if (s < 0)
157 err(EXIT_FAILURE, "socket");
159 ch = L2CAP_LM_AUTH;
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.. ");
171 fflush(stdout);
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));
177 exit(EXIT_FAILURE);
180 close(s);
181 fprintf(stdout, "done\n");
183 exit(EXIT_SUCCESS);
186 void
187 usage(void)
190 fprintf(stderr,
191 "usage: %s [-P] [-d device] [-s socket] {-p pin | -r [-l len]} -a addr\n"
192 "", getprogname());
194 exit(EXIT_FAILURE);