1107120Sjulian/*
2107120Sjulian * l2ping.c
3107120Sjulian *
4107120Sjulian * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5107120Sjulian * All rights reserved.
6107120Sjulian *
7107120Sjulian * Redistribution and use in source and binary forms, with or without
8107120Sjulian * modification, are permitted provided that the following conditions
9107120Sjulian * are met:
10107120Sjulian * 1. Redistributions of source code must retain the above copyright
11107120Sjulian *    notice, this list of conditions and the following disclaimer.
12107120Sjulian * 2. Redistributions in binary form must reproduce the above copyright
13107120Sjulian *    notice, this list of conditions and the following disclaimer in the
14107120Sjulian *    documentation and/or other materials provided with the distribution.
15107120Sjulian *
16107120Sjulian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17107120Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18107120Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19107120Sjulian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20107120Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21107120Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22107120Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23107120Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24107120Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25107120Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26107120Sjulian * SUCH DAMAGE.
27107120Sjulian *
28121054Semax * $Id: l2ping.c,v 1.5 2003/05/16 19:54:40 max Exp $
29107120Sjulian * $FreeBSD$
30107120Sjulian */
31107120Sjulian
32107120Sjulian#include <sys/ioctl.h>
33107120Sjulian#include <sys/time.h>
34107120Sjulian#include <arpa/inet.h>
35107120Sjulian#include <netinet/in.h>
36107120Sjulian#include <assert.h>
37121054Semax#include <bluetooth.h>
38107120Sjulian#include <err.h>
39107120Sjulian#include <errno.h>
40220116Semax#include <limits.h>
41107120Sjulian#include <stdio.h>
42107120Sjulian#include <stdlib.h>
43107120Sjulian#include <string.h>
44107120Sjulian#include <unistd.h>
45107120Sjulian
46107120Sjulianstatic void	usage	(void);
47107120Sjulianstatic void	tv_sub	(struct timeval *, struct timeval const *);
48107120Sjulianstatic double	tv2msec	(struct timeval const *);
49107120Sjulian
50107120Sjulian#undef	min
51107120Sjulian#define	min(x, y)	(((x) > (y))? (y) : (x))
52107120Sjulian
53107120Sjulianstatic char const		pattern[] = "1234567890-";
54107120Sjulian#define PATTERN_SIZE		(sizeof(pattern) - 1)
55107120Sjulian
56107120Sjulian/*
57107120Sjulian * Main
58107120Sjulian */
59107120Sjulian
60107120Sjulianint
61107120Sjulianmain(int argc, char *argv[])
62107120Sjulian{
63125943Semax	bdaddr_t		 src, dst;
64220116Semax	struct hostent		*he;
65220116Semax	uint8_t			*echo_data;
66125943Semax	struct sockaddr_l2cap	 sa;
67133140Semax	int32_t			 n, s, count, wait, flood, echo_size, numeric;
68220116Semax	char			*endp, *rname;
69107120Sjulian
70107120Sjulian	/* Set defaults */
71114879Sjulian	memcpy(&src, NG_HCI_BDADDR_ANY, sizeof(src));
72114879Sjulian	memcpy(&dst, NG_HCI_BDADDR_ANY, sizeof(dst));
73114879Sjulian
74125943Semax	echo_data = (uint8_t *) calloc(NG_L2CAP_MAX_ECHO_SIZE, sizeof(uint8_t));
75125943Semax	if (echo_data == NULL) {
76107120Sjulian		fprintf(stderr, "Failed to allocate echo data buffer");
77107120Sjulian		exit(1);
78107120Sjulian	}
79107120Sjulian
80125943Semax	/*
81125943Semax	 * Set default echo size to the NG_L2CAP_MTU_MINIMUM minus
82125943Semax	 * the size of the L2CAP signalling command header.
83125943Semax	 */
84125943Semax
85125943Semax	echo_size = NG_L2CAP_MTU_MINIMUM - sizeof(ng_l2cap_cmd_hdr_t);
86125943Semax	count = -1; /* unimited */
87125943Semax	wait = 1;   /* sec */
88107120Sjulian	flood = 0;
89133140Semax	numeric = 0;
90107120Sjulian
91107120Sjulian	/* Parse command line arguments */
92133140Semax	while ((n = getopt(argc, argv, "a:c:fi:nS:s:h")) != -1) {
93107120Sjulian		switch (n) {
94107120Sjulian		case 'a':
95121054Semax			if (!bt_aton(optarg, &dst)) {
96121054Semax				if ((he = bt_gethostbyname(optarg)) == NULL)
97121054Semax					errx(1, "%s: %s", optarg, hstrerror(h_errno));
98107120Sjulian
99121054Semax				memcpy(&dst, he->h_addr, sizeof(dst));
100121054Semax			}
101121054Semax			break;
102107120Sjulian
103107120Sjulian		case 'c':
104220116Semax			count = strtol(optarg, &endp, 10);
105220116Semax			if (count <= 0 || *endp != '\0')
106107120Sjulian				usage();
107107120Sjulian			break;
108107120Sjulian
109107120Sjulian		case 'f':
110107120Sjulian			flood = 1;
111107120Sjulian			break;
112107120Sjulian
113107120Sjulian		case 'i':
114220116Semax			wait = strtol(optarg, &endp, 10);
115220116Semax			if (wait <= 0 || *endp != '\0')
116107120Sjulian				usage();
117107120Sjulian			break;
118107120Sjulian
119133140Semax		case 'n':
120133140Semax			numeric = 1;
121133140Semax			break;
122133140Semax
123133140Semax		case 'S':
124133140Semax			if (!bt_aton(optarg, &src)) {
125133140Semax				if ((he = bt_gethostbyname(optarg)) == NULL)
126133140Semax					errx(1, "%s: %s", optarg, hstrerror(h_errno));
127133140Semax
128133140Semax				memcpy(&src, he->h_addr, sizeof(src));
129133140Semax			}
130133140Semax			break;
131133140Semax
132107120Sjulian		case 's':
133220116Semax                        echo_size = strtol(optarg, &endp, 10);
134220116Semax                        if (echo_size < sizeof(int32_t) ||
135220116Semax			    echo_size > NG_L2CAP_MAX_ECHO_SIZE ||
136220116Semax			    *endp != '\0')
137107120Sjulian				usage();
138107120Sjulian			break;
139107120Sjulian
140114879Sjulian		case 'h':
141107120Sjulian		default:
142107120Sjulian			usage();
143107120Sjulian			break;
144107120Sjulian		}
145107120Sjulian	}
146107120Sjulian
147114879Sjulian	if (memcmp(&dst, NG_HCI_BDADDR_ANY, sizeof(dst)) == 0)
148107120Sjulian		usage();
149107120Sjulian
150133140Semax	he = bt_gethostbyaddr((const char *)&dst, sizeof(dst), AF_BLUETOOTH);
151133140Semax	if (he == NULL || he->h_name == NULL || he->h_name[0] == '\0' || numeric)
152133140Semax		asprintf(&rname, "%s", bt_ntoa(&dst, NULL));
153133140Semax	else
154133140Semax		rname = strdup(he->h_name);
155133140Semax
156133140Semax	if (rname == NULL)
157133140Semax		errx(1, "Failed to create remote hostname");
158133140Semax
159107120Sjulian	s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_L2CAP);
160107120Sjulian	if (s < 0)
161107120Sjulian		err(2, "Could not create socket");
162107120Sjulian
163114879Sjulian	memset(&sa, 0, sizeof(sa));
164114879Sjulian	sa.l2cap_len = sizeof(sa);
165114879Sjulian	sa.l2cap_family = AF_BLUETOOTH;
166114879Sjulian	memcpy(&sa.l2cap_bdaddr, &src, sizeof(sa.l2cap_bdaddr));
167107120Sjulian
168114879Sjulian	if (bind(s, (struct sockaddr *) &sa, sizeof(sa)) < 0)
169114879Sjulian		err(3,
170121054Semax"Could not bind socket, src bdaddr=%s", bt_ntoa(&sa.l2cap_bdaddr, NULL));
171107120Sjulian
172114879Sjulian	memset(&sa, 0, sizeof(sa));
173114879Sjulian	sa.l2cap_len = sizeof(sa);
174114879Sjulian	sa.l2cap_family = AF_BLUETOOTH;
175114879Sjulian	memcpy(&sa.l2cap_bdaddr, &dst, sizeof(sa.l2cap_bdaddr));
176107120Sjulian
177114879Sjulian	if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) < 0)
178114879Sjulian		err(4,
179121054Semax"Could not connect socket, dst bdaddr=%s", bt_ntoa(&sa.l2cap_bdaddr, NULL));
180114879Sjulian
181107120Sjulian	/* Fill pattern */
182125943Semax	for (n = 0; n < echo_size; ) {
183125943Semax		int32_t	avail = min(echo_size - n, PATTERN_SIZE);
184107120Sjulian
185125943Semax		memcpy(echo_data + n, pattern, avail);
186107120Sjulian		n += avail;
187107120Sjulian	}
188107120Sjulian
189107120Sjulian	/* Start ping'ing */
190107120Sjulian	for (n = 0; count == -1 || count > 0; n ++) {
191125943Semax		struct ng_btsocket_l2cap_raw_ping	r;
192125943Semax		struct timeval				a, b;
193125943Semax		int32_t					fail;
194125943Semax
195107120Sjulian		if (gettimeofday(&a, NULL) < 0)
196107120Sjulian			err(5, "Could not gettimeofday(a)");
197107120Sjulian
198107120Sjulian		fail = 0;
199125943Semax		*((int32_t *) echo_data) = htonl(n);
200125943Semax
201125943Semax		r.result = 0;
202125943Semax		r.echo_size = echo_size;
203125943Semax		r.echo_data = echo_data;
204107120Sjulian		if (ioctl(s, SIOC_L2CAP_L2CA_PING, &r, sizeof(r)) < 0) {
205107120Sjulian			r.result = errno;
206107120Sjulian			fail = 1;
207107120Sjulian/*
208121054Semax			warn("Could not ping, dst bdaddr=%s",
209121054Semax				bt_ntoa(&r.echo_dst, NULL));
210107120Sjulian*/
211107120Sjulian		}
212107120Sjulian
213107120Sjulian		if (gettimeofday(&b, NULL) < 0)
214107120Sjulian			err(7, "Could not gettimeofday(b)");
215107120Sjulian
216107120Sjulian		tv_sub(&b, &a);
217107120Sjulian
218107120Sjulian		fprintf(stdout,
219121054Semax"%d bytes from %s seq_no=%d time=%.3f ms result=%#x %s\n",
220121054Semax			r.echo_size,
221133140Semax			rname,
222125943Semax			ntohl(*((int32_t *)(r.echo_data))),
223107120Sjulian			tv2msec(&b), r.result,
224107120Sjulian			((fail == 0)? "" : strerror(errno)));
225107120Sjulian
226107120Sjulian		if (!flood) {
227107120Sjulian			/* Wait */
228107120Sjulian			a.tv_sec = wait;
229107120Sjulian			a.tv_usec = 0;
230107120Sjulian			select(0, NULL, NULL, NULL, &a);
231107120Sjulian		}
232107120Sjulian
233107120Sjulian		if (count != -1)
234107120Sjulian			count --;
235107120Sjulian	}
236107120Sjulian
237133140Semax	free(rname);
238125943Semax	free(echo_data);
239107120Sjulian	close(s);
240107120Sjulian
241107120Sjulian	return (0);
242107120Sjulian} /* main */
243107120Sjulian
244107120Sjulian/*
245107120Sjulian * a -= b, for timevals
246107120Sjulian */
247107120Sjulian
248107120Sjulianstatic void
249107120Sjuliantv_sub(struct timeval *a, struct timeval const *b)
250107120Sjulian{
251107120Sjulian	if (a->tv_usec < b->tv_usec) {
252107120Sjulian		a->tv_usec += 1000000;
253107120Sjulian		a->tv_sec -= 1;
254107120Sjulian	}
255107120Sjulian
256107120Sjulian	a->tv_usec -= b->tv_usec;
257107120Sjulian	a->tv_sec -= b->tv_sec;
258107120Sjulian} /* tv_sub */
259107120Sjulian
260107120Sjulian/*
261107120Sjulian * convert tv to msec
262107120Sjulian */
263107120Sjulian
264107120Sjulianstatic double
265107120Sjuliantv2msec(struct timeval const *tvp)
266107120Sjulian{
267107120Sjulian	return(((double)tvp->tv_usec)/1000.0 + ((double)tvp->tv_sec)*1000.0);
268107120Sjulian} /* tv2msec */
269107120Sjulian
270107120Sjulian/*
271107120Sjulian * Usage
272107120Sjulian */
273107120Sjulian
274107120Sjulianstatic void
275107120Sjulianusage(void)
276107120Sjulian{
277220116Semax	fprintf(stderr, "Usage: l2ping [-fhn] -a remote " \
278220116Semax		"[-c count] [-i wait] [-S source] [-s size]\n");
279107120Sjulian	fprintf(stderr, "Where:\n");
280133140Semax	fprintf(stderr, "  -a remote  Specify remote device to ping\n");
281133140Semax	fprintf(stderr, "  -c count   Number of packets to send\n");
282220116Semax	fprintf(stderr, "  -f         No delay between packets\n");
283133140Semax	fprintf(stderr, "  -h         Display this message\n");
284133140Semax	fprintf(stderr, "  -i wait    Delay between packets (sec)\n");
285133140Semax	fprintf(stderr, "  -n         Numeric output only\n");
286133140Semax	fprintf(stderr, "  -S source  Specify source device\n");
287133140Semax	fprintf(stderr, "  -s size    Packet size (bytes), " \
288128076Semax		"between %zd and %zd\n", sizeof(int32_t), NG_L2CAP_MAX_ECHO_SIZE);
289107120Sjulian
290107120Sjulian	exit(255);
291107120Sjulian} /* usage */
292107120Sjulian
293