btpin.c revision 1.1 1 /* $NetBSD: btpin.c,v 1.1 2006/06/19 15:44:56 gdamore Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Itronix Inc.
5 * All rights reserved.
6 *
7 * Written by Iain Hibbert for Itronix Inc.
8 *
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.
20 *
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.
32 */
33
34 #include <sys/cdefs.h>
35 __COPYRIGHT("@(#) Copyright (c) 2006 Itronix, Inc\n"
36 "All rights reserved.\n");
37 __RCSID("$NetBSD: btpin.c,v 1.1 2006/06/19 15:44:56 gdamore Exp $");
38
39 #include <sys/types.h>
40 #include <sys/un.h>
41 #include <bluetooth.h>
42 #include <err.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <time.h>
46 #include <unistd.h>
47
48 #include "bthcid.h"
49
50 int main(int, char *[]);
51 void usage(void);
52
53 int
54 main(int ac, char *av[])
55 {
56 client_pin_response_t rp;
57 struct sockaddr_un un;
58 char *pin = NULL;
59 int ch, s, len;
60
61 memset(&rp, 0, sizeof(rp));
62 len = -1;
63
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));
68
69 while ((ch = getopt(ac, av, "a:d:l:p:rs:")) != EOF) {
70 switch (ch) {
71 case 'a':
72 if (!bt_aton(optarg, &rp.raddr)) {
73 struct hostent *he = NULL;
74
75 if ((he = bt_gethostbyname(optarg)) == NULL)
76 errx(EXIT_FAILURE, "%s: %s", optarg,
77 hstrerror(h_errno));
78
79 bdaddr_copy(&rp.raddr, (bdaddr_t *)he->h_addr);
80 }
81 break;
82
83 case 'd':
84 if (!bt_devaddr(optarg, &rp.laddr))
85 err(EXIT_FAILURE, "%s", optarg);
86
87 break;
88
89 case 'l':
90 len = atoi(optarg);
91 if (len < 1 || len > HCI_PIN_SIZE)
92 errx(EXIT_FAILURE, "Invalid PIN length");
93
94 break;
95
96 case 'p':
97 pin = optarg;
98 break;
99
100 case 'r':
101 if (len == -1)
102 len = 4;
103
104 break;
105
106 case 's':
107 strlcpy(un.sun_path, optarg, sizeof(un.sun_path));
108 break;
109
110 default:
111 usage();
112 }
113 }
114
115 if (bdaddr_any(&rp.raddr))
116 usage();
117
118 if (pin == NULL) {
119 if (len == -1)
120 usage();
121
122 srandom(time(NULL));
123
124 pin = (char *)rp.pin;
125 while (len-- > 0)
126 *pin++ = '0' + (random() % 10);
127
128 printf("PIN: %.*s\n", HCI_PIN_SIZE, rp.pin);
129 } else {
130 if (len != -1)
131 usage();
132
133 strncpy((char *)rp.pin, pin, HCI_PIN_SIZE);
134 }
135
136 s = socket(PF_LOCAL, SOCK_STREAM, 0);
137 if (s < 0)
138 err(EXIT_FAILURE, "socket");
139
140 if (connect(s, (struct sockaddr *)&un, sizeof(un)) < 0)
141 err(EXIT_FAILURE, "connect(\"%s\")", un.sun_path);
142
143 if (send(s, &rp, sizeof(rp), 0) != sizeof(rp))
144 err(EXIT_FAILURE, "send");
145
146 close(s);
147 exit(EXIT_SUCCESS);
148 }
149
150 void
151 usage(void)
152 {
153
154 fprintf(stderr,
155 "usage: %s [-d device] [-s socket] [-p pin | -r [-l len]] -a addr\n"
156 "", getprogname());
157
158 exit(EXIT_FAILURE);
159 }
160