1121054Semax/*
2121054Semax * bthost.c
3121054Semax *
4121054Semax * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5121054Semax * All rights reserved.
6121054Semax *
7121054Semax * Redistribution and use in source and binary forms, with or without
8121054Semax * modification, are permitted provided that the following conditions
9121054Semax * are met:
10121054Semax * 1. Redistributions of source code must retain the above copyright
11121054Semax *    notice, this list of conditions and the following disclaimer.
12121054Semax * 2. Redistributions in binary form must reproduce the above copyright
13121054Semax *    notice, this list of conditions and the following disclaimer in the
14121054Semax *    documentation and/or other materials provided with the distribution.
15121054Semax *
16121054Semax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17121054Semax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18121054Semax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19121054Semax * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20121054Semax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21121054Semax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22121054Semax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23121054Semax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24121054Semax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25121054Semax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26121054Semax * SUCH DAMAGE.
27121054Semax *
28121054Semax * $Id: bthost.c,v 1.5 2003/05/21 20:30:01 max Exp $
29121054Semax * $FreeBSD$
30121054Semax */
31121054Semax
32121054Semax#include <bluetooth.h>
33121054Semax#include <stdio.h>
34121054Semax#include <stdlib.h>
35121054Semax#include <unistd.h>
36121054Semax
37121054Semaxstatic int  hostmode  (char const *arg, int brief);
38121054Semaxstatic int  protomode (char const *arg, int brief);
39121054Semaxstatic void usage     (void);
40121054Semax
41121054Semaxint
42121054Semaxmain(int argc, char **argv)
43121054Semax{
44121054Semax	int	opt, brief = 0, proto = 0;
45121054Semax
46121054Semax	while ((opt = getopt(argc, argv, "bhp")) != -1) {
47121054Semax		switch (opt) {
48121054Semax		case 'b':
49121054Semax			brief = 1;
50121054Semax			break;
51121054Semax
52121054Semax		case 'p':
53121054Semax			proto = 1;
54121054Semax			break;
55121054Semax
56121054Semax		case 'h':
57121054Semax		default:
58121054Semax			usage();
59121054Semax			/* NOT REACHED */
60121054Semax		}
61121054Semax	}
62121054Semax
63121054Semax	argc -= optind;
64121054Semax	argv += optind;
65121054Semax
66121054Semax	if (argc < 1)
67121054Semax		usage();
68121054Semax
69121054Semax	exit(proto? protomode(*argv, brief) : hostmode(*argv, brief));
70121054Semax}
71121054Semax
72121054Semaxstatic int
73121054Semaxhostmode(char const *arg, int brief)
74121054Semax{
75121054Semax	struct hostent	*he = NULL;
76121054Semax	bdaddr_t	 ba;
77121054Semax	char		 bastr[32];
78121054Semax	int		 reverse;
79121054Semax
80121054Semax	if (bt_aton(arg, &ba) == 1) {
81121054Semax		reverse = 1;
82121054Semax		he = bt_gethostbyaddr((char const *) &ba, sizeof(ba),
83121054Semax					AF_BLUETOOTH);
84121054Semax	} else {
85121054Semax		reverse = 0;
86121054Semax		he = bt_gethostbyname(arg);
87121054Semax	}
88121054Semax
89121054Semax	if (he == NULL) {
90121054Semax		herror(reverse? bt_ntoa(&ba, bastr) : arg);
91121054Semax		return (1);
92121054Semax	}
93121054Semax
94121054Semax	if (brief)
95121054Semax		printf("%s", reverse? he->h_name :
96121054Semax				bt_ntoa((bdaddr_t *)(he->h_addr), bastr));
97121054Semax	else
98121054Semax		printf("Host %s has %s %s\n",
99121054Semax			reverse? bt_ntoa(&ba, bastr) : arg,
100121054Semax			reverse? "name" : "address",
101121054Semax			reverse? he->h_name :
102121054Semax				bt_ntoa((bdaddr_t *)(he->h_addr), bastr));
103121054Semax
104121054Semax	return (0);
105121054Semax}
106121054Semax
107121054Semaxstatic int
108121054Semaxprotomode(char const *arg, int brief)
109121054Semax{
110121054Semax	struct protoent	*pe = NULL;
111121054Semax	int		 proto;
112121054Semax
113121054Semax	if ((proto = atoi(arg)) != 0)
114121054Semax		pe = bt_getprotobynumber(proto);
115121054Semax	else
116121054Semax		pe = bt_getprotobyname(arg);
117121054Semax
118121054Semax	if (pe == NULL) {
119121054Semax		fprintf(stderr, "%s: Unknown Protocol/Service Multiplexor\n", arg);
120121054Semax		return (1);
121121054Semax	}
122121054Semax
123121054Semax	if (brief) {
124121054Semax		if (proto)
125121054Semax			printf("%s", pe->p_name);
126121054Semax		else
127121054Semax			printf("%d", pe->p_proto);
128121054Semax	} else {
129121054Semax		printf("Protocol/Service Multiplexor %s has number %d\n",
130121054Semax			pe->p_name, pe->p_proto);
131121054Semax	}
132121054Semax
133121054Semax	return (0);
134121054Semax}
135121054Semax
136121054Semaxstatic void
137121054Semaxusage(void)
138121054Semax{
139121054Semax	fprintf(stdout, "Usage: bthost [-b -h -p] host_or_protocol\n");
140121054Semax	exit(255);
141121054Semax}
142121054Semax
143