Path: blob/main/usr.sbin/bluetooth/l2ping/l2ping.c
103499 views
/*-1* l2ping.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: l2ping.c,v 1.5 2003/05/16 19:54:40 max Exp $30*/3132#include <sys/ioctl.h>33#include <sys/time.h>34#include <arpa/inet.h>35#include <netinet/in.h>36#include <assert.h>37#define L2CAP_SOCKET_CHECKED38#include <bluetooth.h>39#include <err.h>40#include <errno.h>41#include <limits.h>42#include <stdio.h>43#include <stdlib.h>44#include <string.h>45#include <unistd.h>4647static void usage (void);48static void tv_sub (struct timeval *, struct timeval const *);49static double tv2msec (struct timeval const *);5051#undef min52#define min(x, y) (((x) > (y))? (y) : (x))5354static char const pattern[] = "1234567890-";55#define PATTERN_SIZE (sizeof(pattern) - 1)5657/*58* Main59*/6061int62main(int argc, char *argv[])63{64bdaddr_t src, dst;65struct hostent *he;66uint8_t *echo_data;67struct sockaddr_l2cap sa;68int32_t n, s, count, wait, flood, echo_size, numeric;69char *endp, *rname;7071/* Set defaults */72memcpy(&src, NG_HCI_BDADDR_ANY, sizeof(src));73memcpy(&dst, NG_HCI_BDADDR_ANY, sizeof(dst));7475echo_data = (uint8_t *) calloc(NG_L2CAP_MAX_ECHO_SIZE, sizeof(uint8_t));76if (echo_data == NULL) {77fprintf(stderr, "Failed to allocate echo data buffer");78exit(1);79}8081/*82* Set default echo size to the NG_L2CAP_MTU_MINIMUM minus83* the size of the L2CAP signalling command header.84*/8586echo_size = NG_L2CAP_MTU_MINIMUM - sizeof(ng_l2cap_cmd_hdr_t);87count = -1; /* unimited */88wait = 1; /* sec */89flood = 0;90numeric = 0;9192/* Parse command line arguments */93while ((n = getopt(argc, argv, "a:c:fi:nS:s:h")) != -1) {94switch (n) {95case 'a':96if (!bt_aton(optarg, &dst)) {97if ((he = bt_gethostbyname(optarg)) == NULL)98errx(1, "%s: %s", optarg, hstrerror(h_errno));99100memcpy(&dst, he->h_addr, sizeof(dst));101}102break;103104case 'c':105count = strtol(optarg, &endp, 10);106if (count <= 0 || *endp != '\0')107usage();108break;109110case 'f':111flood = 1;112break;113114case 'i':115wait = strtol(optarg, &endp, 10);116if (wait <= 0 || *endp != '\0')117usage();118break;119120case 'n':121numeric = 1;122break;123124case 'S':125if (!bt_aton(optarg, &src)) {126if ((he = bt_gethostbyname(optarg)) == NULL)127errx(1, "%s: %s", optarg, hstrerror(h_errno));128129memcpy(&src, he->h_addr, sizeof(src));130}131break;132133case 's':134echo_size = strtol(optarg, &endp, 10);135if (echo_size < sizeof(int32_t) ||136echo_size > NG_L2CAP_MAX_ECHO_SIZE ||137*endp != '\0')138usage();139break;140141case 'h':142default:143usage();144break;145}146}147148if (memcmp(&dst, NG_HCI_BDADDR_ANY, sizeof(dst)) == 0)149usage();150151he = bt_gethostbyaddr((const char *)&dst, sizeof(dst), AF_BLUETOOTH);152if (he == NULL || he->h_name == NULL || he->h_name[0] == '\0' || numeric)153asprintf(&rname, "%s", bt_ntoa(&dst, NULL));154else155rname = strdup(he->h_name);156157if (rname == NULL)158errx(1, "Failed to create remote hostname");159160s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_L2CAP);161if (s < 0)162err(2, "Could not create socket");163164memset(&sa, 0, sizeof(sa));165sa.l2cap_len = sizeof(sa);166sa.l2cap_family = AF_BLUETOOTH;167memcpy(&sa.l2cap_bdaddr, &src, sizeof(sa.l2cap_bdaddr));168169if (bind(s, (struct sockaddr *) &sa, sizeof(sa)) < 0)170err(3,171"Could not bind socket, src bdaddr=%s", bt_ntoa(&sa.l2cap_bdaddr, NULL));172173memset(&sa, 0, sizeof(sa));174sa.l2cap_len = sizeof(sa);175sa.l2cap_family = AF_BLUETOOTH;176memcpy(&sa.l2cap_bdaddr, &dst, sizeof(sa.l2cap_bdaddr));177178if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) < 0)179err(4,180"Could not connect socket, dst bdaddr=%s", bt_ntoa(&sa.l2cap_bdaddr, NULL));181182/* Fill pattern */183for (n = 0; n < echo_size; ) {184int32_t avail = min(echo_size - n, PATTERN_SIZE);185186memcpy(echo_data + n, pattern, avail);187n += avail;188}189190/* Start ping'ing */191for (n = 0; count == -1 || count > 0; n ++) {192struct ng_btsocket_l2cap_raw_ping r;193struct timeval a, b;194int32_t fail;195196if (gettimeofday(&a, NULL) < 0)197err(5, "Could not gettimeofday(a)");198199fail = 0;200*((int32_t *) echo_data) = htonl(n);201202r.result = 0;203r.echo_size = echo_size;204r.echo_data = echo_data;205if (ioctl(s, SIOC_L2CAP_L2CA_PING, &r, sizeof(r)) < 0) {206r.result = errno;207fail = 1;208/*209warn("Could not ping, dst bdaddr=%s",210bt_ntoa(&r.echo_dst, NULL));211*/212}213214if (gettimeofday(&b, NULL) < 0)215err(7, "Could not gettimeofday(b)");216217tv_sub(&b, &a);218219fprintf(stdout,220"%d bytes from %s seq_no=%d time=%.3f ms result=%#x %s\n",221r.echo_size,222rname,223ntohl(*((int32_t *)(r.echo_data))),224tv2msec(&b), r.result,225((fail == 0)? "" : strerror(errno)));226227if (!flood) {228/* Wait */229a.tv_sec = wait;230a.tv_usec = 0;231select(0, NULL, NULL, NULL, &a);232}233234if (count != -1)235count --;236}237238free(rname);239free(echo_data);240close(s);241242return (0);243} /* main */244245/*246* a -= b, for timevals247*/248249static void250tv_sub(struct timeval *a, struct timeval const *b)251{252if (a->tv_usec < b->tv_usec) {253a->tv_usec += 1000000;254a->tv_sec -= 1;255}256257a->tv_usec -= b->tv_usec;258a->tv_sec -= b->tv_sec;259} /* tv_sub */260261/*262* convert tv to msec263*/264265static double266tv2msec(struct timeval const *tvp)267{268return(((double)tvp->tv_usec)/1000.0 + ((double)tvp->tv_sec)*1000.0);269} /* tv2msec */270271/*272* Usage273*/274275static void276usage(void)277{278fprintf(stderr, "Usage: l2ping [-fhn] -a remote " \279"[-c count] [-i wait] [-S source] [-s size]\n");280fprintf(stderr, "Where:\n");281fprintf(stderr, " -a remote Specify remote device to ping\n");282fprintf(stderr, " -c count Number of packets to send\n");283fprintf(stderr, " -f No delay between packets\n");284fprintf(stderr, " -h Display this message\n");285fprintf(stderr, " -i wait Delay between packets (sec)\n");286fprintf(stderr, " -n Numeric output only\n");287fprintf(stderr, " -S source Specify source device\n");288fprintf(stderr, " -s size Packet size (bytes), " \289"between %zd and %zd\n", sizeof(int32_t), NG_L2CAP_MAX_ECHO_SIZE);290291exit(255);292} /* usage */293294295296