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