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