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