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