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