/*-1* bthost.c2*3* SPDX-License-Identifier: BSD-2-Clause4*5* Copyright (c) 2003 Maksim Yevmenkin <[email protected]>6* All rights reserved.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*29* $Id: bthost.c,v 1.5 2003/05/21 20:30:01 max Exp $30*/3132#define L2CAP_SOCKET_CHECKED33#include <bluetooth.h>34#include <stdio.h>35#include <stdlib.h>36#include <unistd.h>3738static int hostmode (char const *arg, int brief);39static int protomode (char const *arg, int brief);40static void usage (void);4142int43main(int argc, char **argv)44{45int opt, brief = 0, proto = 0;4647while ((opt = getopt(argc, argv, "bhp")) != -1) {48switch (opt) {49case 'b':50brief = 1;51break;5253case 'p':54proto = 1;55break;5657case 'h':58default:59usage();60/* NOT REACHED */61}62}6364argc -= optind;65argv += optind;6667if (argc < 1)68usage();6970exit(proto? protomode(*argv, brief) : hostmode(*argv, brief));71}7273static int74hostmode(char const *arg, int brief)75{76struct hostent *he = NULL;77bdaddr_t ba;78char bastr[32];79int reverse;8081if (bt_aton(arg, &ba) == 1) {82reverse = 1;83he = bt_gethostbyaddr((char const *) &ba, sizeof(ba),84AF_BLUETOOTH);85} else {86reverse = 0;87he = bt_gethostbyname(arg);88}8990if (he == NULL) {91herror(reverse? bt_ntoa(&ba, bastr) : arg);92return (1);93}9495if (brief)96printf("%s", reverse? he->h_name :97bt_ntoa((bdaddr_t *)(he->h_addr), bastr));98else99printf("Host %s has %s %s\n",100reverse? bt_ntoa(&ba, bastr) : arg,101reverse? "name" : "address",102reverse? he->h_name :103bt_ntoa((bdaddr_t *)(he->h_addr), bastr));104105return (0);106}107108static int109protomode(char const *arg, int brief)110{111struct protoent *pe = NULL;112int proto;113114if ((proto = atoi(arg)) != 0)115pe = bt_getprotobynumber(proto);116else117pe = bt_getprotobyname(arg);118119if (pe == NULL) {120fprintf(stderr, "%s: Unknown Protocol/Service Multiplexor\n", arg);121return (1);122}123124if (brief) {125if (proto)126printf("%s", pe->p_name);127else128printf("%d", pe->p_proto);129} else {130printf("Protocol/Service Multiplexor %s has number %d\n",131pe->p_name, pe->p_proto);132}133134return (0);135}136137static void138usage(void)139{140fprintf(stdout, "Usage: bthost [-b -h -p] host_or_protocol\n");141exit(255);142}143144145146