xref: /freebsd/usr.sbin/bluetooth/l2ping/l2ping.c (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
1 /*-
2  * l2ping.c
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
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  */
32 
33 #include <sys/ioctl.h>
34 #include <sys/time.h>
35 #include <arpa/inet.h>
36 #include <netinet/in.h>
37 #include <assert.h>
38 #define L2CAP_SOCKET_CHECKED
39 #include <bluetooth.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <limits.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 
48 static void	usage	(void);
49 static void	tv_sub	(struct timeval *, struct timeval const *);
50 static double	tv2msec	(struct timeval const *);
51 
52 #undef	min
53 #define	min(x, y)	(((x) > (y))? (y) : (x))
54 
55 static char const		pattern[] = "1234567890-";
56 #define PATTERN_SIZE		(sizeof(pattern) - 1)
57 
58 /*
59  * Main
60  */
61 
62 int
63 main(int argc, char *argv[])
64 {
65 	bdaddr_t		 src, dst;
66 	struct hostent		*he;
67 	uint8_t			*echo_data;
68 	struct sockaddr_l2cap	 sa;
69 	int32_t			 n, s, count, wait, flood, echo_size, numeric;
70 	char			*endp, *rname;
71 
72 	/* Set defaults */
73 	memcpy(&src, NG_HCI_BDADDR_ANY, sizeof(src));
74 	memcpy(&dst, NG_HCI_BDADDR_ANY, sizeof(dst));
75 
76 	echo_data = (uint8_t *) calloc(NG_L2CAP_MAX_ECHO_SIZE, sizeof(uint8_t));
77 	if (echo_data == NULL) {
78 		fprintf(stderr, "Failed to allocate echo data buffer");
79 		exit(1);
80 	}
81 
82 	/*
83 	 * Set default echo size to the NG_L2CAP_MTU_MINIMUM minus
84 	 * the size of the L2CAP signalling command header.
85 	 */
86 
87 	echo_size = NG_L2CAP_MTU_MINIMUM - sizeof(ng_l2cap_cmd_hdr_t);
88 	count = -1; /* unimited */
89 	wait = 1;   /* sec */
90 	flood = 0;
91 	numeric = 0;
92 
93 	/* Parse command line arguments */
94 	while ((n = getopt(argc, argv, "a:c:fi:nS:s:h")) != -1) {
95 		switch (n) {
96 		case 'a':
97 			if (!bt_aton(optarg, &dst)) {
98 				if ((he = bt_gethostbyname(optarg)) == NULL)
99 					errx(1, "%s: %s", optarg, hstrerror(h_errno));
100 
101 				memcpy(&dst, he->h_addr, sizeof(dst));
102 			}
103 			break;
104 
105 		case 'c':
106 			count = strtol(optarg, &endp, 10);
107 			if (count <= 0 || *endp != '\0')
108 				usage();
109 			break;
110 
111 		case 'f':
112 			flood = 1;
113 			break;
114 
115 		case 'i':
116 			wait = strtol(optarg, &endp, 10);
117 			if (wait <= 0 || *endp != '\0')
118 				usage();
119 			break;
120 
121 		case 'n':
122 			numeric = 1;
123 			break;
124 
125 		case 'S':
126 			if (!bt_aton(optarg, &src)) {
127 				if ((he = bt_gethostbyname(optarg)) == NULL)
128 					errx(1, "%s: %s", optarg, hstrerror(h_errno));
129 
130 				memcpy(&src, he->h_addr, sizeof(src));
131 			}
132 			break;
133 
134 		case 's':
135                         echo_size = strtol(optarg, &endp, 10);
136                         if (echo_size < sizeof(int32_t) ||
137 			    echo_size > NG_L2CAP_MAX_ECHO_SIZE ||
138 			    *endp != '\0')
139 				usage();
140 			break;
141 
142 		case 'h':
143 		default:
144 			usage();
145 			break;
146 		}
147 	}
148 
149 	if (memcmp(&dst, NG_HCI_BDADDR_ANY, sizeof(dst)) == 0)
150 		usage();
151 
152 	he = bt_gethostbyaddr((const char *)&dst, sizeof(dst), AF_BLUETOOTH);
153 	if (he == NULL || he->h_name == NULL || he->h_name[0] == '\0' || numeric)
154 		asprintf(&rname, "%s", bt_ntoa(&dst, NULL));
155 	else
156 		rname = strdup(he->h_name);
157 
158 	if (rname == NULL)
159 		errx(1, "Failed to create remote hostname");
160 
161 	s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_L2CAP);
162 	if (s < 0)
163 		err(2, "Could not create socket");
164 
165 	memset(&sa, 0, sizeof(sa));
166 	sa.l2cap_len = sizeof(sa);
167 	sa.l2cap_family = AF_BLUETOOTH;
168 	memcpy(&sa.l2cap_bdaddr, &src, sizeof(sa.l2cap_bdaddr));
169 
170 	if (bind(s, (struct sockaddr *) &sa, sizeof(sa)) < 0)
171 		err(3,
172 "Could not bind socket, src bdaddr=%s", bt_ntoa(&sa.l2cap_bdaddr, NULL));
173 
174 	memset(&sa, 0, sizeof(sa));
175 	sa.l2cap_len = sizeof(sa);
176 	sa.l2cap_family = AF_BLUETOOTH;
177 	memcpy(&sa.l2cap_bdaddr, &dst, sizeof(sa.l2cap_bdaddr));
178 
179 	if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) < 0)
180 		err(4,
181 "Could not connect socket, dst bdaddr=%s", bt_ntoa(&sa.l2cap_bdaddr, NULL));
182 
183 	/* Fill pattern */
184 	for (n = 0; n < echo_size; ) {
185 		int32_t	avail = min(echo_size - n, PATTERN_SIZE);
186 
187 		memcpy(echo_data + n, pattern, avail);
188 		n += avail;
189 	}
190 
191 	/* Start ping'ing */
192 	for (n = 0; count == -1 || count > 0; n ++) {
193 		struct ng_btsocket_l2cap_raw_ping	r;
194 		struct timeval				a, b;
195 		int32_t					fail;
196 
197 		if (gettimeofday(&a, NULL) < 0)
198 			err(5, "Could not gettimeofday(a)");
199 
200 		fail = 0;
201 		*((int32_t *) echo_data) = htonl(n);
202 
203 		r.result = 0;
204 		r.echo_size = echo_size;
205 		r.echo_data = echo_data;
206 		if (ioctl(s, SIOC_L2CAP_L2CA_PING, &r, sizeof(r)) < 0) {
207 			r.result = errno;
208 			fail = 1;
209 /*
210 			warn("Could not ping, dst bdaddr=%s",
211 				bt_ntoa(&r.echo_dst, NULL));
212 */
213 		}
214 
215 		if (gettimeofday(&b, NULL) < 0)
216 			err(7, "Could not gettimeofday(b)");
217 
218 		tv_sub(&b, &a);
219 
220 		fprintf(stdout,
221 "%d bytes from %s seq_no=%d time=%.3f ms result=%#x %s\n",
222 			r.echo_size,
223 			rname,
224 			ntohl(*((int32_t *)(r.echo_data))),
225 			tv2msec(&b), r.result,
226 			((fail == 0)? "" : strerror(errno)));
227 
228 		if (!flood) {
229 			/* Wait */
230 			a.tv_sec = wait;
231 			a.tv_usec = 0;
232 			select(0, NULL, NULL, NULL, &a);
233 		}
234 
235 		if (count != -1)
236 			count --;
237 	}
238 
239 	free(rname);
240 	free(echo_data);
241 	close(s);
242 
243 	return (0);
244 } /* main */
245 
246 /*
247  * a -= b, for timevals
248  */
249 
250 static void
251 tv_sub(struct timeval *a, struct timeval const *b)
252 {
253 	if (a->tv_usec < b->tv_usec) {
254 		a->tv_usec += 1000000;
255 		a->tv_sec -= 1;
256 	}
257 
258 	a->tv_usec -= b->tv_usec;
259 	a->tv_sec -= b->tv_sec;
260 } /* tv_sub */
261 
262 /*
263  * convert tv to msec
264  */
265 
266 static double
267 tv2msec(struct timeval const *tvp)
268 {
269 	return(((double)tvp->tv_usec)/1000.0 + ((double)tvp->tv_sec)*1000.0);
270 } /* tv2msec */
271 
272 /*
273  * Usage
274  */
275 
276 static void
277 usage(void)
278 {
279 	fprintf(stderr, "Usage: l2ping [-fhn] -a remote " \
280 		"[-c count] [-i wait] [-S source] [-s size]\n");
281 	fprintf(stderr, "Where:\n");
282 	fprintf(stderr, "  -a remote  Specify remote device to ping\n");
283 	fprintf(stderr, "  -c count   Number of packets to send\n");
284 	fprintf(stderr, "  -f         No delay between packets\n");
285 	fprintf(stderr, "  -h         Display this message\n");
286 	fprintf(stderr, "  -i wait    Delay between packets (sec)\n");
287 	fprintf(stderr, "  -n         Numeric output only\n");
288 	fprintf(stderr, "  -S source  Specify source device\n");
289 	fprintf(stderr, "  -s size    Packet size (bytes), " \
290 		"between %zd and %zd\n", sizeof(int32_t), NG_L2CAP_MAX_ECHO_SIZE);
291 
292 	exit(255);
293 } /* usage */
294 
295