xref: /freebsd/usr.bin/bluetooth/bthost/bthost.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*
2  * bthost.c
3  *
4  * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $Id: bthost.c,v 1.5 2003/05/21 20:30:01 max Exp $
29  * $FreeBSD$
30  */
31 
32 #include <bluetooth.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 
37 static int  hostmode  (char const *arg, int brief);
38 static int  protomode (char const *arg, int brief);
39 static void usage     (void);
40 
41 int
42 main(int argc, char **argv)
43 {
44 	int	opt, brief = 0, proto = 0;
45 
46 	while ((opt = getopt(argc, argv, "bhp")) != -1) {
47 		switch (opt) {
48 		case 'b':
49 			brief = 1;
50 			break;
51 
52 		case 'p':
53 			proto = 1;
54 			break;
55 
56 		case 'h':
57 		default:
58 			usage();
59 			/* NOT REACHED */
60 		}
61 	}
62 
63 	argc -= optind;
64 	argv += optind;
65 
66 	if (argc < 1)
67 		usage();
68 
69 	exit(proto? protomode(*argv, brief) : hostmode(*argv, brief));
70 }
71 
72 static int
73 hostmode(char const *arg, int brief)
74 {
75 	struct hostent	*he = NULL;
76 	bdaddr_t	 ba;
77 	char		 bastr[32];
78 	int		 reverse;
79 
80 	if (bt_aton(arg, &ba) == 1) {
81 		reverse = 1;
82 		he = bt_gethostbyaddr((char const *) &ba, sizeof(ba),
83 					AF_BLUETOOTH);
84 	} else {
85 		reverse = 0;
86 		he = bt_gethostbyname(arg);
87 	}
88 
89 	if (he == NULL) {
90 		herror(reverse? bt_ntoa(&ba, bastr) : arg);
91 		return (1);
92 	}
93 
94 	if (brief)
95 		printf("%s", reverse? he->h_name :
96 				bt_ntoa((bdaddr_t *)(he->h_addr), bastr));
97 	else
98 		printf("Host %s has %s %s\n",
99 			reverse? bt_ntoa(&ba, bastr) : arg,
100 			reverse? "name" : "address",
101 			reverse? he->h_name :
102 				bt_ntoa((bdaddr_t *)(he->h_addr), bastr));
103 
104 	return (0);
105 }
106 
107 static int
108 protomode(char const *arg, int brief)
109 {
110 	struct protoent	*pe = NULL;
111 	int		 proto;
112 
113 	if ((proto = atoi(arg)) != 0)
114 		pe = bt_getprotobynumber(proto);
115 	else
116 		pe = bt_getprotobyname(arg);
117 
118 	if (pe == NULL) {
119 		fprintf(stderr, "%s: Unknown Protocol/Service Multiplexor\n", arg);
120 		return (1);
121 	}
122 
123 	if (brief) {
124 		if (proto)
125 			printf("%s", pe->p_name);
126 		else
127 			printf("%d", pe->p_proto);
128 	} else {
129 		printf("Protocol/Service Multiplexor %s has number %d\n",
130 			pe->p_name, pe->p_proto);
131 	}
132 
133 	return (0);
134 }
135 
136 static void
137 usage(void)
138 {
139 	fprintf(stdout, "Usage: bthost [-b -h -p] host_or_protocol\n");
140 	exit(255);
141 }
142 
143