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