diff options
Diffstat (limited to 'usr.sbin/bluetooth/l2ping')
| -rw-r--r-- | usr.sbin/bluetooth/l2ping/Makefile | 10 | ||||
| -rw-r--r-- | usr.sbin/bluetooth/l2ping/Makefile.depend | 17 | ||||
| -rw-r--r-- | usr.sbin/bluetooth/l2ping/l2ping.8 | 113 | ||||
| -rw-r--r-- | usr.sbin/bluetooth/l2ping/l2ping.c | 294 | 
4 files changed, 434 insertions, 0 deletions
| diff --git a/usr.sbin/bluetooth/l2ping/Makefile b/usr.sbin/bluetooth/l2ping/Makefile new file mode 100644 index 000000000000..70b8d19bc609 --- /dev/null +++ b/usr.sbin/bluetooth/l2ping/Makefile @@ -0,0 +1,10 @@ +# $Id: Makefile,v 1.6 2003/08/14 20:06:24 max Exp $ + +PACKAGE=	bluetooth +PROG=		l2ping +MAN=		l2ping.8 +WARNS?=		2 + +LIBADD=		bluetooth + +.include <bsd.prog.mk> diff --git a/usr.sbin/bluetooth/l2ping/Makefile.depend b/usr.sbin/bluetooth/l2ping/Makefile.depend new file mode 100644 index 000000000000..a1fc9c8c3375 --- /dev/null +++ b/usr.sbin/bluetooth/l2ping/Makefile.depend @@ -0,0 +1,17 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ +	include \ +	include/arpa \ +	include/xlocale \ +	lib/${CSU_DIR} \ +	lib/libbluetooth \ +	lib/libc \ +	lib/libcompiler_rt \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/usr.sbin/bluetooth/l2ping/l2ping.8 b/usr.sbin/bluetooth/l2ping/l2ping.8 new file mode 100644 index 000000000000..07ba6547a424 --- /dev/null +++ b/usr.sbin/bluetooth/l2ping/l2ping.8 @@ -0,0 +1,113 @@ +.\" Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com> +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\"    notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\"    notice, this list of conditions and the following disclaimer in the +.\"    documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $Id: l2ping.8,v 1.3 2003/05/21 01:00:19 max Exp $ +.\" +.Dd March 29, 2011 +.Dt L2PING 8 +.Os +.Sh NAME +.Nm l2ping +.Nd send L2CAP ECHO_REQUEST to remote devices +.Sh SYNOPSIS +.Nm +.Op Fl fhn +.Fl a Ar remote +.Op Fl c Ar count +.Op Fl i Ar wait +.Op Fl S Ar source +.Op Fl s Ar size +.Sh DESCRIPTION +The +.Nm +utility uses L2CAP +.Dv ECHO_REQUEST +datagram to elicit an L2CAP +.Dv ECHO_RESPONSE +datagram from a remote device. +.Pp +The options are as follows: +.Bl -tag -width indent +.It Fl a Ar remote +Specify the remote device to ping. +The remote device can be specified by either its BD_ADDR or name. +If name was specified then the +.Nm +utility will attempt to resolve the name via +.Xr bt_gethostbyname 3 . +.It Fl c Ar count +Number of packets to send. +If this option is not specified, +.Nm +will operate until interrupted. +.It Fl f +Do not wait between sending each packet. +.It Fl h +Display usage message and exit. +.It Fl i Ar wait +Wait +.Ar wait +seconds between sending each packet. +The default is to wait for one second between each packet. +This option is ignored if +.Fl f +has been specified. +.It Fl n +Numeric output only. +No attempt will be made to look up symbolic names for host addresses. +.It Fl S Ar source +Specify the local device which should be used to send L2CAP +.Dv ECHO_REQUEST +datagrams. +The local device can be specified by either its BD_ADDR or name. +If name was specified then the +.Nm +utility will attempt to resolve the name via +.Xr bt_gethostbyname 3 . +.It Fl s Ar size +Specify the number of payload bytes to be sent. +The default size is 44 bytes. +It is calculated as minimum L2CAP MTU (48 bytes) minus the size of the L2CAP +signalling command header (4 bytes). +The maximum size is 65531 bytes. +Is is calculated as maximum L2CAP MTU +(65535 bytes) minus four bytes of payload reserved for +.Nm +internal use. +Use this option with caution. +Some implementations may not like large sizes and may hang or even crash. +.El +.Sh EXIT STATUS +.Ex -std +.Sh SEE ALSO +.Xr bluetooth 3 , +.Xr netgraph 3 , +.Xr netgraph 4 , +.Xr ng_l2cap 4 , +.Xr l2control 8 +.Sh AUTHORS +.An Maksim Yevmenkin Aq Mt emax@FreeBSD.org +.Sh BUGS +Could collect more statistic. +Could check for duplicated, corrupted and lost packets. diff --git a/usr.sbin/bluetooth/l2ping/l2ping.c b/usr.sbin/bluetooth/l2ping/l2ping.c new file mode 100644 index 000000000000..25ddf06e8532 --- /dev/null +++ b/usr.sbin/bluetooth/l2ping/l2ping.c @@ -0,0 +1,294 @@ +/*- + * l2ping.c + * + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + *    notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + *    notice, this list of conditions and the following disclaimer in the + *    documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Id: l2ping.c,v 1.5 2003/05/16 19:54:40 max Exp $ + */ + +#include <sys/ioctl.h> +#include <sys/time.h> +#include <arpa/inet.h> +#include <netinet/in.h> +#include <assert.h> +#define L2CAP_SOCKET_CHECKED +#include <bluetooth.h> +#include <err.h> +#include <errno.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +static void	usage	(void); +static void	tv_sub	(struct timeval *, struct timeval const *); +static double	tv2msec	(struct timeval const *); + +#undef	min +#define	min(x, y)	(((x) > (y))? (y) : (x)) + +static char const		pattern[] = "1234567890-"; +#define PATTERN_SIZE		(sizeof(pattern) - 1) + +/*  + * Main  + */ + +int +main(int argc, char *argv[]) +{ +	bdaddr_t		 src, dst; +	struct hostent		*he; +	uint8_t			*echo_data; +	struct sockaddr_l2cap	 sa; +	int32_t			 n, s, count, wait, flood, echo_size, numeric; +	char			*endp, *rname; + +	/* Set defaults */ +	memcpy(&src, NG_HCI_BDADDR_ANY, sizeof(src)); +	memcpy(&dst, NG_HCI_BDADDR_ANY, sizeof(dst)); + +	echo_data = (uint8_t *) calloc(NG_L2CAP_MAX_ECHO_SIZE, sizeof(uint8_t)); +	if (echo_data == NULL) { +		fprintf(stderr, "Failed to allocate echo data buffer"); +		exit(1); +	} + +	/* +	 * Set default echo size to the NG_L2CAP_MTU_MINIMUM minus +	 * the size of the L2CAP signalling command header. +	 */ + +	echo_size = NG_L2CAP_MTU_MINIMUM - sizeof(ng_l2cap_cmd_hdr_t); +	count = -1; /* unimited */ +	wait = 1;   /* sec */ +	flood = 0; +	numeric = 0; + +	/* Parse command line arguments */ +	while ((n = getopt(argc, argv, "a:c:fi:nS:s:h")) != -1) { +		switch (n) { +		case 'a': +			if (!bt_aton(optarg, &dst)) { +				if ((he = bt_gethostbyname(optarg)) == NULL) +					errx(1, "%s: %s", optarg, hstrerror(h_errno)); + +				memcpy(&dst, he->h_addr, sizeof(dst)); +			} +			break; + +		case 'c': +			count = strtol(optarg, &endp, 10); +			if (count <= 0 || *endp != '\0') +				usage(); +			break; + +		case 'f': +			flood = 1; +			break; + +		case 'i': +			wait = strtol(optarg, &endp, 10); +			if (wait <= 0 || *endp != '\0') +				usage(); +			break; + +		case 'n': +			numeric = 1; +			break; + +		case 'S': +			if (!bt_aton(optarg, &src)) { +				if ((he = bt_gethostbyname(optarg)) == NULL) +					errx(1, "%s: %s", optarg, hstrerror(h_errno)); + +				memcpy(&src, he->h_addr, sizeof(src)); +			} +			break; + +		case 's': +                        echo_size = strtol(optarg, &endp, 10); +                        if (echo_size < sizeof(int32_t) || +			    echo_size > NG_L2CAP_MAX_ECHO_SIZE || +			    *endp != '\0') +				usage(); +			break; + +		case 'h': +		default: +			usage(); +			break; +		} +	} + +	if (memcmp(&dst, NG_HCI_BDADDR_ANY, sizeof(dst)) == 0) +		usage(); + +	he = bt_gethostbyaddr((const char *)&dst, sizeof(dst), AF_BLUETOOTH); +	if (he == NULL || he->h_name == NULL || he->h_name[0] == '\0' || numeric) +		asprintf(&rname, "%s", bt_ntoa(&dst, NULL)); +	else +		rname = strdup(he->h_name); + +	if (rname == NULL) +		errx(1, "Failed to create remote hostname"); + +	s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_L2CAP); +	if (s < 0) +		err(2, "Could not create socket"); + +	memset(&sa, 0, sizeof(sa)); +	sa.l2cap_len = sizeof(sa); +	sa.l2cap_family = AF_BLUETOOTH; +	memcpy(&sa.l2cap_bdaddr, &src, sizeof(sa.l2cap_bdaddr)); + +	if (bind(s, (struct sockaddr *) &sa, sizeof(sa)) < 0) +		err(3, +"Could not bind socket, src bdaddr=%s", bt_ntoa(&sa.l2cap_bdaddr, NULL)); + +	memset(&sa, 0, sizeof(sa)); +	sa.l2cap_len = sizeof(sa); +	sa.l2cap_family = AF_BLUETOOTH; +	memcpy(&sa.l2cap_bdaddr, &dst, sizeof(sa.l2cap_bdaddr)); + +	if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) < 0) +		err(4, +"Could not connect socket, dst bdaddr=%s", bt_ntoa(&sa.l2cap_bdaddr, NULL)); + +	/* Fill pattern */ +	for (n = 0; n < echo_size; ) { +		int32_t	avail = min(echo_size - n, PATTERN_SIZE); + +		memcpy(echo_data + n, pattern, avail); +		n += avail; +	} + +	/* Start ping'ing */ +	for (n = 0; count == -1 || count > 0; n ++) { +		struct ng_btsocket_l2cap_raw_ping	r; +		struct timeval				a, b; +		int32_t					fail; + +		if (gettimeofday(&a, NULL) < 0) +			err(5, "Could not gettimeofday(a)"); + +		fail = 0; +		*((int32_t *) echo_data) = htonl(n); + +		r.result = 0; +		r.echo_size = echo_size; +		r.echo_data = echo_data; +		if (ioctl(s, SIOC_L2CAP_L2CA_PING, &r, sizeof(r)) < 0) { +			r.result = errno; +			fail = 1; +/* +			warn("Could not ping, dst bdaddr=%s", +				bt_ntoa(&r.echo_dst, NULL)); +*/ +		} + +		if (gettimeofday(&b, NULL) < 0) +			err(7, "Could not gettimeofday(b)"); + +		tv_sub(&b, &a); + +		fprintf(stdout, +"%d bytes from %s seq_no=%d time=%.3f ms result=%#x %s\n", +			r.echo_size, +			rname, +			ntohl(*((int32_t *)(r.echo_data))), +			tv2msec(&b), r.result, +			((fail == 0)? "" : strerror(errno))); + +		if (!flood) { +			/* Wait */ +			a.tv_sec = wait; +			a.tv_usec = 0; +			select(0, NULL, NULL, NULL, &a); +		} + +		if (count != -1) +			count --; +	} + +	free(rname); +	free(echo_data); +	close(s); + +	return (0); +} /* main */ + +/*  + * a -= b, for timevals  + */ + +static void +tv_sub(struct timeval *a, struct timeval const *b) +{ +	if (a->tv_usec < b->tv_usec) { +		a->tv_usec += 1000000; +		a->tv_sec -= 1; +	} + +	a->tv_usec -= b->tv_usec; +	a->tv_sec -= b->tv_sec; +} /* tv_sub */ + +/*  + * convert tv to msec  + */ + +static double +tv2msec(struct timeval const *tvp) +{ +	return(((double)tvp->tv_usec)/1000.0 + ((double)tvp->tv_sec)*1000.0); +} /* tv2msec */ + +/*  + * Usage  + */ + +static void +usage(void) +{ +	fprintf(stderr, "Usage: l2ping [-fhn] -a remote " \ +		"[-c count] [-i wait] [-S source] [-s size]\n"); +	fprintf(stderr, "Where:\n"); +	fprintf(stderr, "  -a remote  Specify remote device to ping\n"); +	fprintf(stderr, "  -c count   Number of packets to send\n"); +	fprintf(stderr, "  -f         No delay between packets\n"); +	fprintf(stderr, "  -h         Display this message\n"); +	fprintf(stderr, "  -i wait    Delay between packets (sec)\n"); +	fprintf(stderr, "  -n         Numeric output only\n"); +	fprintf(stderr, "  -S source  Specify source device\n"); +	fprintf(stderr, "  -s size    Packet size (bytes), " \ +		"between %zd and %zd\n", sizeof(int32_t), NG_L2CAP_MAX_ECHO_SIZE); +	 +	exit(255); +} /* usage */ + | 
