camcontrol(8): Check scsiserial()'s error, too.
[dragonfly.git] / usr.bin / btpin / btpin.c
blob96f6a7f6e29c564c6eb437b5e0eaf83f89a0830b
1 /* $NetBSD: btpin.c,v 1.3 2007/04/14 09:28:39 plunky 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/types.h>
35 #include <sys/un.h>
36 #include <bluetooth.h>
37 #include <err.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <time.h>
41 #include <unistd.h>
43 void usage(void);
45 int
46 main(int ac, char *av[])
48 bthcid_pin_response_t rp;
49 struct sockaddr_un un;
50 char *pin = NULL;
51 int ch, s, len;
53 memset(&rp, 0, sizeof(rp));
54 len = -1;
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) {
62 switch (ch) {
63 case 'a':
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,
69 hstrerror(h_errno));
71 bdaddr_copy(&rp.raddr, (bdaddr_t *)he->h_addr);
73 break;
75 case 'd':
76 if (!bt_devaddr(optarg, &rp.laddr))
77 err(EXIT_FAILURE, "%s", optarg);
79 break;
81 case 'l':
82 len = atoi(optarg);
83 if (len < 1 || len > HCI_PIN_SIZE)
84 errx(EXIT_FAILURE, "Invalid PIN length");
86 break;
88 case 'p':
89 pin = optarg;
90 break;
92 case 'r':
93 if (len == -1)
94 len = 4;
96 break;
98 case 's':
99 strlcpy(un.sun_path, optarg, sizeof(un.sun_path));
100 break;
102 default:
103 usage();
107 if (bdaddr_any(&rp.raddr))
108 usage();
110 if (pin == NULL) {
111 if (len == -1)
112 usage();
114 srandom(time(NULL));
116 pin = (char *)rp.pin;
117 while (len-- > 0)
118 *pin++ = '0' + (random() % 10);
120 printf("PIN: %.*s\n", HCI_PIN_SIZE, rp.pin);
121 } else {
122 if (len != -1)
123 usage();
125 strncpy((char *)rp.pin, pin, HCI_PIN_SIZE);
128 s = socket(PF_LOCAL, SOCK_STREAM, 0);
129 if (s < 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");
138 close(s);
139 exit(EXIT_SUCCESS);
142 void
143 usage(void)
146 fprintf(stderr,
147 "usage: %s [-d device] [-s socket] {-p pin | -r [-l len]} -a addr\n"
148 "", getprogname());
150 exit(EXIT_FAILURE);