l2ping.c revision 125943
1/*
2 * l2ping.c
3 *
4 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $Id: l2ping.c,v 1.5 2003/05/16 19:54:40 max Exp $
29 * $FreeBSD: head/usr.sbin/bluetooth/l2ping/l2ping.c 125943 2004-02-17 18:37:12Z emax $
30 */
31
32#include <sys/ioctl.h>
33#include <sys/time.h>
34#include <arpa/inet.h>
35#include <netinet/in.h>
36#include <assert.h>
37#include <bluetooth.h>
38#include <err.h>
39#include <errno.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
44
45static void	usage	(void);
46static void	tv_sub	(struct timeval *, struct timeval const *);
47static double	tv2msec	(struct timeval const *);
48
49#undef	min
50#define	min(x, y)	(((x) > (y))? (y) : (x))
51
52static char const		pattern[] = "1234567890-";
53#define PATTERN_SIZE		(sizeof(pattern) - 1)
54
55/*
56 * Main
57 */
58
59int
60main(int argc, char *argv[])
61{
62	bdaddr_t		 src, dst;
63	struct hostent		*he = NULL;
64	uint8_t			*echo_data = NULL;
65	struct sockaddr_l2cap	 sa;
66	int32_t			 n, s, count, wait, flood, echo_size;
67
68	/* Set defaults */
69	memcpy(&src, NG_HCI_BDADDR_ANY, sizeof(src));
70	memcpy(&dst, NG_HCI_BDADDR_ANY, sizeof(dst));
71
72	echo_data = (uint8_t *) calloc(NG_L2CAP_MAX_ECHO_SIZE, sizeof(uint8_t));
73	if (echo_data == NULL) {
74		fprintf(stderr, "Failed to allocate echo data buffer");
75		exit(1);
76	}
77
78	/*
79	 * Set default echo size to the NG_L2CAP_MTU_MINIMUM minus
80	 * the size of the L2CAP signalling command header.
81	 */
82
83	echo_size = NG_L2CAP_MTU_MINIMUM - sizeof(ng_l2cap_cmd_hdr_t);
84	count = -1; /* unimited */
85	wait = 1;   /* sec */
86	flood = 0;
87
88	/* Parse command line arguments */
89	while ((n = getopt(argc, argv, "a:c:fi:n:s:S:h")) != -1) {
90		switch (n) {
91		case 'a':
92			if (!bt_aton(optarg, &dst)) {
93				if ((he = bt_gethostbyname(optarg)) == NULL)
94					errx(1, "%s: %s", optarg, hstrerror(h_errno));
95
96				memcpy(&dst, he->h_addr, sizeof(dst));
97			}
98			break;
99
100		case 'S':
101			if (!bt_aton(optarg, &src)) {
102				if ((he = bt_gethostbyname(optarg)) == NULL)
103					errx(1, "%s: %s", optarg, hstrerror(h_errno));
104
105				memcpy(&src, he->h_addr, sizeof(src));
106			}
107			break;
108
109		case 'c':
110			count = atoi(optarg);
111			if (count <= 0)
112				usage();
113			break;
114
115		case 'f':
116			flood = 1;
117			break;
118
119		case 'i':
120			wait = atoi(optarg);
121			if (wait <= 0)
122				usage();
123			break;
124
125		case 's':
126			echo_size = atoi(optarg);
127			if (echo_size < sizeof(int32_t))
128				usage();
129
130			if (echo_size > NG_L2CAP_MAX_ECHO_SIZE)
131				echo_size = NG_L2CAP_MAX_ECHO_SIZE;
132			break;
133
134		case 'h':
135		default:
136			usage();
137			break;
138		}
139	}
140
141	if (memcmp(&dst, NG_HCI_BDADDR_ANY, sizeof(dst)) == 0)
142		usage();
143
144	s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_L2CAP);
145	if (s < 0)
146		err(2, "Could not create socket");
147
148	memset(&sa, 0, sizeof(sa));
149	sa.l2cap_len = sizeof(sa);
150	sa.l2cap_family = AF_BLUETOOTH;
151	memcpy(&sa.l2cap_bdaddr, &src, sizeof(sa.l2cap_bdaddr));
152
153	if (bind(s, (struct sockaddr *) &sa, sizeof(sa)) < 0)
154		err(3,
155"Could not bind socket, src bdaddr=%s", bt_ntoa(&sa.l2cap_bdaddr, NULL));
156
157	memset(&sa, 0, sizeof(sa));
158	sa.l2cap_len = sizeof(sa);
159	sa.l2cap_family = AF_BLUETOOTH;
160	memcpy(&sa.l2cap_bdaddr, &dst, sizeof(sa.l2cap_bdaddr));
161
162	if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) < 0)
163		err(4,
164"Could not connect socket, dst bdaddr=%s", bt_ntoa(&sa.l2cap_bdaddr, NULL));
165
166	/* Fill pattern */
167	for (n = 0; n < echo_size; ) {
168		int32_t	avail = min(echo_size - n, PATTERN_SIZE);
169
170		memcpy(echo_data + n, pattern, avail);
171		n += avail;
172	}
173
174	/* Start ping'ing */
175	for (n = 0; count == -1 || count > 0; n ++) {
176		struct ng_btsocket_l2cap_raw_ping	r;
177		struct timeval				a, b;
178		int32_t					fail;
179
180		if (gettimeofday(&a, NULL) < 0)
181			err(5, "Could not gettimeofday(a)");
182
183		fail = 0;
184		*((int32_t *) echo_data) = htonl(n);
185
186		r.result = 0;
187		r.echo_size = echo_size;
188		r.echo_data = echo_data;
189		if (ioctl(s, SIOC_L2CAP_L2CA_PING, &r, sizeof(r)) < 0) {
190			r.result = errno;
191			fail = 1;
192/*
193			warn("Could not ping, dst bdaddr=%s",
194				bt_ntoa(&r.echo_dst, NULL));
195*/
196		}
197
198		if (gettimeofday(&b, NULL) < 0)
199			err(7, "Could not gettimeofday(b)");
200
201		tv_sub(&b, &a);
202
203		fprintf(stdout,
204"%d bytes from %s seq_no=%d time=%.3f ms result=%#x %s\n",
205			r.echo_size,
206			bt_ntoa(&dst, NULL),
207			ntohl(*((int32_t *)(r.echo_data))),
208			tv2msec(&b), r.result,
209			((fail == 0)? "" : strerror(errno)));
210
211		if (!flood) {
212			/* Wait */
213			a.tv_sec = wait;
214			a.tv_usec = 0;
215			select(0, NULL, NULL, NULL, &a);
216		}
217
218		if (count != -1)
219			count --;
220	}
221
222	free(echo_data);
223	close(s);
224
225	return (0);
226} /* main */
227
228/*
229 * a -= b, for timevals
230 */
231
232static void
233tv_sub(struct timeval *a, struct timeval const *b)
234{
235	if (a->tv_usec < b->tv_usec) {
236		a->tv_usec += 1000000;
237		a->tv_sec -= 1;
238	}
239
240	a->tv_usec -= b->tv_usec;
241	a->tv_sec -= b->tv_sec;
242} /* tv_sub */
243
244/*
245 * convert tv to msec
246 */
247
248static double
249tv2msec(struct timeval const *tvp)
250{
251	return(((double)tvp->tv_usec)/1000.0 + ((double)tvp->tv_sec)*1000.0);
252} /* tv2msec */
253
254/*
255 * Usage
256 */
257
258static void
259usage(void)
260{
261	fprintf(stderr, "Usage: l2ping -a bd_addr " \
262		"[-S bd_addr -c count -i wait -s size -h]\n");
263	fprintf(stderr, "Where:\n");
264	fprintf(stderr, "\t-S bd_addr         - Source BD_ADDR\n");
265	fprintf(stderr, "\t-a bd_addr         - Remote BD_ADDR to ping\n");
266	fprintf(stderr, "\t-c count           - Number of packets to send\n");
267	fprintf(stderr, "\t-f                 - No delay (soft of flood)\n");
268	fprintf(stderr, "\t-i wait            - Delay between packets (sec)\n");
269	fprintf(stderr, "\t-s size            - Packet size (bytes), " \
270		"between %d and %d\n", sizeof(int32_t), NG_L2CAP_MAX_ECHO_SIZE);
271	fprintf(stderr, "\t-h                 - Display this message\n");
272
273	exit(255);
274} /* usage */
275
276