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