xref: /freebsd/usr.bin/bluetooth/bthost/bthost.c (revision 952364486a4b9d135e4b28f7f88a8703a74eae6f)
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 #define L2CAP_SOCKET_CHECKED
33 #include <bluetooth.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 
38 static int  hostmode  (char const *arg, int brief);
39 static int  protomode (char const *arg, int brief);
40 static void usage     (void);
41 
42 int
43 main(int argc, char **argv)
44 {
45 	int	opt, brief = 0, proto = 0;
46 
47 	while ((opt = getopt(argc, argv, "bhp")) != -1) {
48 		switch (opt) {
49 		case 'b':
50 			brief = 1;
51 			break;
52 
53 		case 'p':
54 			proto = 1;
55 			break;
56 
57 		case 'h':
58 		default:
59 			usage();
60 			/* NOT REACHED */
61 		}
62 	}
63 
64 	argc -= optind;
65 	argv += optind;
66 
67 	if (argc < 1)
68 		usage();
69 
70 	exit(proto? protomode(*argv, brief) : hostmode(*argv, brief));
71 }
72 
73 static int
74 hostmode(char const *arg, int brief)
75 {
76 	struct hostent	*he = NULL;
77 	bdaddr_t	 ba;
78 	char		 bastr[32];
79 	int		 reverse;
80 
81 	if (bt_aton(arg, &ba) == 1) {
82 		reverse = 1;
83 		he = bt_gethostbyaddr((char const *) &ba, sizeof(ba),
84 					AF_BLUETOOTH);
85 	} else {
86 		reverse = 0;
87 		he = bt_gethostbyname(arg);
88 	}
89 
90 	if (he == NULL) {
91 		herror(reverse? bt_ntoa(&ba, bastr) : arg);
92 		return (1);
93 	}
94 
95 	if (brief)
96 		printf("%s", reverse? he->h_name :
97 				bt_ntoa((bdaddr_t *)(he->h_addr), bastr));
98 	else
99 		printf("Host %s has %s %s\n",
100 			reverse? bt_ntoa(&ba, bastr) : arg,
101 			reverse? "name" : "address",
102 			reverse? he->h_name :
103 				bt_ntoa((bdaddr_t *)(he->h_addr), bastr));
104 
105 	return (0);
106 }
107 
108 static int
109 protomode(char const *arg, int brief)
110 {
111 	struct protoent	*pe = NULL;
112 	int		 proto;
113 
114 	if ((proto = atoi(arg)) != 0)
115 		pe = bt_getprotobynumber(proto);
116 	else
117 		pe = bt_getprotobyname(arg);
118 
119 	if (pe == NULL) {
120 		fprintf(stderr, "%s: Unknown Protocol/Service Multiplexor\n", arg);
121 		return (1);
122 	}
123 
124 	if (brief) {
125 		if (proto)
126 			printf("%s", pe->p_name);
127 		else
128 			printf("%d", pe->p_proto);
129 	} else {
130 		printf("Protocol/Service Multiplexor %s has number %d\n",
131 			pe->p_name, pe->p_proto);
132 	}
133 
134 	return (0);
135 }
136 
137 static void
138 usage(void)
139 {
140 	fprintf(stdout, "Usage: bthost [-b -h -p] host_or_protocol\n");
141 	exit(255);
142 }
143 
144