Path: blob/main/usr.sbin/bluetooth/l2control/l2control.c
103352 views
/*-1* l2control.c2*3* SPDX-License-Identifier: BSD-2-Clause4*5* Copyright (c) 2001-2002 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: l2control.c,v 1.6 2003/09/05 00:38:25 max Exp $30*/3132#include <assert.h>33#define L2CAP_SOCKET_CHECKED34#include <bluetooth.h>35#include <err.h>36#include <errno.h>37#include <stdio.h>38#include <stdlib.h>39#include <string.h>40#include <unistd.h>41#include "l2control.h"4243/* Prototypes */44static int do_l2cap_command (bdaddr_p, int, char **);45static struct l2cap_command * find_l2cap_command (char const *,46struct l2cap_command *);47static void print_l2cap_command (struct l2cap_command *);48static void usage (void);4950/* Main */5152int numeric_bdaddr = 0;5354int55main(int argc, char *argv[])56{57int n;58bdaddr_t bdaddr;5960memset(&bdaddr, 0, sizeof(bdaddr));6162/* Process command line arguments */63while ((n = getopt(argc, argv, "a:nh")) != -1) {64switch (n) {65case 'a':66if (!bt_aton(optarg, &bdaddr)) {67struct hostent *he = NULL;6869if ((he = bt_gethostbyname(optarg)) == NULL)70errx(1, "%s: %s", optarg, hstrerror(h_errno));7172memcpy(&bdaddr, he->h_addr, sizeof(bdaddr));73}74break;7576case 'n':77numeric_bdaddr = 1;78break;7980case 'h':81default:82usage();83break;84}85}8687argc -= optind;88argv += optind;8990if (*argv == NULL)91usage();9293return (do_l2cap_command(&bdaddr, argc, argv));94} /* main */9596/* Execute commands */97static int98do_l2cap_command(bdaddr_p bdaddr, int argc, char **argv)99{100char *cmd = argv[0];101struct l2cap_command *c = NULL;102struct sockaddr_l2cap sa;103int s, e, help;104105help = 0;106if (strcasecmp(cmd, "help") == 0) {107argc --;108argv ++;109110if (argc <= 0) {111fprintf(stdout, "Supported commands:\n");112print_l2cap_command(l2cap_commands);113fprintf(stdout, "\nFor more information use " \114"'help command'\n");115116return (OK);117}118119help = 1;120cmd = argv[0];121}122123c = find_l2cap_command(cmd, l2cap_commands);124if (c == NULL) {125fprintf(stdout, "Unknown command: \"%s\"\n", cmd);126return (ERROR);127}128129if (!help) {130if (memcmp(bdaddr, NG_HCI_BDADDR_ANY, sizeof(*bdaddr)) == 0)131usage();132133memset(&sa, 0, sizeof(sa));134sa.l2cap_len = sizeof(sa);135sa.l2cap_family = AF_BLUETOOTH;136memcpy(&sa.l2cap_bdaddr, bdaddr, sizeof(sa.l2cap_bdaddr));137138s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_L2CAP);139if (s < 0)140err(1, "Could not create socket");141142if (bind(s, (struct sockaddr *) &sa, sizeof(sa)) < 0)143err(2,144"Could not bind socket, bdaddr=%s", bt_ntoa(&sa.l2cap_bdaddr, NULL));145146e = 0x0ffff;147if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &e, sizeof(e)) < 0)148err(3, "Could not setsockopt(RCVBUF, %d)", e);149150e = (c->handler)(s, -- argc, ++ argv);151152close(s);153} else154e = USAGE;155156switch (e) {157case OK:158case FAILED:159break;160161case ERROR:162fprintf(stdout, "Could not execute command \"%s\". %s\n",163cmd, strerror(errno));164break;165166case USAGE:167fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);168break;169170default: assert(0); break;171}172173return (e);174} /* do_l2cap_command */175176/* Try to find command in specified category */177static struct l2cap_command *178find_l2cap_command(char const *command, struct l2cap_command *category)179{180struct l2cap_command *c = NULL;181182for (c = category; c->command != NULL; c++) {183char *c_end = strchr(c->command, ' ');184185if (c_end != NULL) {186int len = c_end - c->command;187188if (strncasecmp(command, c->command, len) == 0)189return (c);190} else if (strcasecmp(command, c->command) == 0)191return (c);192}193194return (NULL);195} /* find_l2cap_command */196197/* Print commands in specified category */198static void199print_l2cap_command(struct l2cap_command *category)200{201struct l2cap_command *c = NULL;202203for (c = category; c->command != NULL; c++)204fprintf(stdout, "\t%s\n", c->command);205} /* print_l2cap_command */206207/* Usage */208static void209usage(void)210{211fprintf(stderr, "Usage: l2control [-hn] -a local cmd [params ..]\n");212fprintf(stderr, "Where:\n");213fprintf(stderr, " -a local Specify local device to connect to\n");214fprintf(stderr, " -h Display this message\n");215fprintf(stderr, " -n Show addresses as numbers\n");216fprintf(stderr, " cmd Supported command " \217"(see l2control help)\n");218fprintf(stderr, " params Optional command parameters\n");219exit(255);220} /* usage */221222223224