bluez/tools/l2ping.c

325 lines
6.6 KiB
C
Raw Normal View History

2002-03-09 05:12:35 +08:00
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2000-2001 Qualcomm Incorporated
* Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
2010-01-02 09:08:17 +08:00
* Copyright (C) 2002-2010 Marcel Holtmann <marcel@holtmann.org>
*
*
* This program is free software; you can redistribute it and/or modify
2005-10-30 06:36:31 +08:00
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
2005-10-30 06:36:31 +08:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
2005-10-30 06:36:31 +08:00
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
2002-03-09 05:12:35 +08:00
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
2002-03-09 05:12:35 +08:00
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
2008-06-11 21:20:50 +08:00
#include <string.h>
#include <getopt.h>
2002-03-09 05:12:35 +08:00
#include <signal.h>
#include <sys/time.h>
#include <sys/poll.h>
#include <sys/socket.h>
2002-08-20 12:39:54 +08:00
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
2002-08-20 12:39:54 +08:00
#include <bluetooth/l2cap.h>
2002-03-09 05:12:35 +08:00
/* Defaults */
static bdaddr_t bdaddr;
2005-03-18 21:49:49 +08:00
static int size = 44;
static int ident = 200;
static int delay = 1;
static int count = -1;
static int timeout = 10;
static int reverse = 0;
2009-01-07 10:19:25 +08:00
static int verify = 0;
2002-03-09 05:12:35 +08:00
/* Stats */
static int sent_pkt = 0;
static int recv_pkt = 0;
2002-03-09 05:12:35 +08:00
static float tv2fl(struct timeval tv)
{
return (float)(tv.tv_sec*1000.0) + (float)(tv.tv_usec/1000.0);
}
static void stat(int sig)
{
int loss = sent_pkt ? (float)((sent_pkt-recv_pkt)/(sent_pkt/100.0)) : 0;
printf("%d sent, %d received, %d%% loss\n", sent_pkt, recv_pkt, loss);
exit(0);
}
static void ping(char *svr)
{
struct sigaction sa;
struct sockaddr_l2 addr;
socklen_t optlen;
2009-01-07 10:19:25 +08:00
unsigned char *send_buf;
unsigned char *recv_buf;
2003-07-25 01:34:34 +08:00
char str[18];
int i, sk, lost;
2002-03-09 05:12:35 +08:00
uint8_t id;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = stat;
sigaction(SIGINT, &sa, NULL);
2009-01-07 10:19:25 +08:00
send_buf = malloc(L2CAP_CMD_HDR_SIZE + size);
recv_buf = malloc(L2CAP_CMD_HDR_SIZE + size);
if (!send_buf || !recv_buf) {
perror("Can't allocate buffer");
exit(1);
}
/* Create socket */
sk = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_L2CAP);
if (sk < 0) {
perror("Can't create socket");
2009-01-07 10:19:25 +08:00
goto error;
2002-03-09 05:12:35 +08:00
}
/* Bind to local address */
2002-03-09 05:12:35 +08:00
memset(&addr, 0, sizeof(addr));
addr.l2_family = AF_BLUETOOTH;
bacpy(&addr.l2_bdaddr, &bdaddr);
if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
perror("Can't bind socket");
2009-01-07 10:19:25 +08:00
goto error;
}
2002-03-09 05:12:35 +08:00
/* Connect to remote device */
memset(&addr, 0, sizeof(addr));
addr.l2_family = AF_BLUETOOTH;
2003-07-25 01:34:34 +08:00
str2ba(svr, &addr.l2_bdaddr);
if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
perror("Can't connect");
2009-01-07 10:19:25 +08:00
goto error;
2002-03-09 05:12:35 +08:00
}
/* Get local address */
memset(&addr, 0, sizeof(addr));
optlen = sizeof(addr);
if (getsockname(sk, (struct sockaddr *) &addr, &optlen) < 0) {
perror("Can't get local address");
2009-01-07 10:19:25 +08:00
goto error;
2002-03-09 05:12:35 +08:00
}
ba2str(&addr.l2_bdaddr, str);
2003-07-25 01:34:34 +08:00
printf("Ping: %s from %s (data size %d) ...\n", svr, str, size);
2002-03-09 05:12:35 +08:00
2009-01-07 10:19:25 +08:00
/* Initialize send buffer */
for (i = 0; i < size; i++)
2009-01-07 10:19:25 +08:00
send_buf[L2CAP_CMD_HDR_SIZE + i] = (i % 40) + 'A';
2002-03-09 05:12:35 +08:00
id = ident;
while (count == -1 || count-- > 0) {
2002-03-09 05:12:35 +08:00
struct timeval tv_send, tv_recv, tv_diff;
2009-01-07 10:19:25 +08:00
l2cap_cmd_hdr *send_cmd = (l2cap_cmd_hdr *) send_buf;
l2cap_cmd_hdr *recv_cmd = (l2cap_cmd_hdr *) recv_buf;
2002-03-09 05:12:35 +08:00
/* Build command header */
2009-01-07 10:19:25 +08:00
send_cmd->ident = id;
send_cmd->len = htobs(size);
2002-03-09 05:12:35 +08:00
if (reverse)
2009-01-07 10:19:25 +08:00
send_cmd->code = L2CAP_ECHO_RSP;
else
2009-01-07 10:19:25 +08:00
send_cmd->code = L2CAP_ECHO_REQ;
2002-03-09 05:12:35 +08:00
gettimeofday(&tv_send, NULL);
/* Send Echo Command */
2009-01-07 10:19:25 +08:00
if (send(sk, send_buf, L2CAP_CMD_HDR_SIZE + size, 0) <= 0) {
2002-03-09 05:12:35 +08:00
perror("Send failed");
2009-01-07 10:19:25 +08:00
goto error;
2002-03-09 05:12:35 +08:00
}
/* Wait for Echo Response */
lost = 0;
while (1) {
2002-03-09 05:12:35 +08:00
struct pollfd pf[1];
int err;
pf[0].fd = sk;
pf[0].events = POLLIN;
2002-03-09 05:12:35 +08:00
2004-10-11 18:31:33 +08:00
if ((err = poll(pf, 1, timeout * 1000)) < 0) {
2002-03-09 05:12:35 +08:00
perror("Poll failed");
2009-01-07 10:19:25 +08:00
goto error;
2002-03-09 05:12:35 +08:00
}
if (!err) {
2002-03-09 05:12:35 +08:00
lost = 1;
break;
}
2009-01-07 10:19:25 +08:00
if ((err = recv(sk, recv_buf, L2CAP_CMD_HDR_SIZE + size, 0)) < 0) {
2002-03-09 05:12:35 +08:00
perror("Recv failed");
2009-01-07 10:19:25 +08:00
goto error;
2002-03-09 05:12:35 +08:00
}
if (!err){
2002-03-09 05:12:35 +08:00
printf("Disconnected\n");
2009-01-07 10:19:25 +08:00
goto error;
2002-03-09 05:12:35 +08:00
}
2009-01-07 10:19:25 +08:00
recv_cmd->len = btohs(recv_cmd->len);
2002-03-09 05:12:35 +08:00
/* Check for our id */
2009-01-07 10:19:25 +08:00
if (recv_cmd->ident != id)
2002-03-09 05:12:35 +08:00
continue;
/* Check type */
2009-01-07 10:19:25 +08:00
if (!reverse && recv_cmd->code == L2CAP_ECHO_RSP)
2002-03-09 05:12:35 +08:00
break;
2009-01-07 10:19:25 +08:00
if (recv_cmd->code == L2CAP_COMMAND_REJ) {
2002-03-09 05:12:35 +08:00
printf("Peer doesn't support Echo packets\n");
2009-01-07 10:19:25 +08:00
goto error;
2002-03-09 05:12:35 +08:00
}
}
sent_pkt++;
if (!lost) {
2002-03-09 05:12:35 +08:00
recv_pkt++;
gettimeofday(&tv_recv, NULL);
timersub(&tv_recv, &tv_send, &tv_diff);
2009-01-07 10:19:25 +08:00
if (verify) {
/* Check payload length */
if (recv_cmd->len != size) {
fprintf(stderr, "Received %d bytes, expected %d\n",
recv_cmd->len, size);
goto error;
}
/* Check payload */
if (memcmp(&send_buf[L2CAP_CMD_HDR_SIZE],
&recv_buf[L2CAP_CMD_HDR_SIZE], size)) {
fprintf(stderr, "Response payload different.\n");
goto error;
}
}
printf("%d bytes from %s id %d time %.2fms\n", recv_cmd->len, svr,
id - ident, tv2fl(tv_diff));
2002-03-09 05:12:35 +08:00
2004-10-11 18:31:33 +08:00
if (delay)
sleep(delay);
2002-03-09 05:12:35 +08:00
} else {
2005-02-01 19:37:23 +08:00
printf("no response from %s: id %d\n", svr, id - ident);
2002-03-09 05:12:35 +08:00
}
2004-10-11 18:31:33 +08:00
if (++id > 254)
id = ident;
2002-03-09 05:12:35 +08:00
}
stat(0);
free(send_buf);
free(recv_buf);
2009-01-07 10:19:25 +08:00
return;
error:
close(sk);
free(send_buf);
free(recv_buf);
exit(1);
2002-03-09 05:12:35 +08:00
}
static void usage(void)
{
printf("l2ping - L2CAP ping\n");
printf("Usage:\n");
2009-01-07 10:19:25 +08:00
printf("\tl2ping [-i device] [-s size] [-c count] [-t timeout] [-d delay] [-f] [-r] [-v] <bdaddr>\n");
printf("\t-f Flood ping (delay = 0)\n");
printf("\t-r Reverse ping\n");
2009-01-18 23:54:41 +08:00
printf("\t-v Verify request and response payload\n");
2002-03-09 05:12:35 +08:00
}
int main(int argc, char *argv[])
{
int opt;
2002-03-09 05:12:35 +08:00
/* Default options */
bacpy(&bdaddr, BDADDR_ANY);
2009-01-07 10:19:25 +08:00
while ((opt=getopt(argc,argv,"i:d:s:c:t:frv")) != EOF) {
2002-03-09 05:12:35 +08:00
switch(opt) {
case 'i':
if (!strncasecmp(optarg, "hci", 3))
hci_devba(atoi(optarg + 3), &bdaddr);
else
str2ba(optarg, &bdaddr);
break;
2009-01-07 10:19:25 +08:00
case 'd':
delay = atoi(optarg);
break;
2002-03-09 05:12:35 +08:00
case 'f':
/* Kinda flood ping */
delay = 0;
break;
case 'r':
/* Use responses instead of requests */
reverse = 1;
break;
2009-01-07 10:19:25 +08:00
case 'v':
verify = 1;
break;
2002-03-09 05:12:35 +08:00
case 'c':
count = atoi(optarg);
break;
2004-10-11 18:31:33 +08:00
case 't':
timeout = atoi(optarg);
break;
2002-03-09 05:12:35 +08:00
case 's':
size = atoi(optarg);
break;
default:
usage();
exit(1);
}
}
if (!(argc - optind)) {
usage();
exit(1);
}
ping(argv[optind]);
return 0;
}