bluez/tools/hcidump.c

616 lines
13 KiB
C
Raw Normal View History

2001-08-02 10:40:48 +08:00
/*
2004-03-03 10:03:49 +08:00
*
* Bluetooth packet analyzer - HCI sniffer
2004-03-03 10:03:49 +08:00
*
* Copyright (C) 2000-2002 Maxim Krasnyansky <maxk@qualcomm.com>
* Copyright (C) 2003-2005 Marcel Holtmann <marcel@holtmann.org>
2004-03-03 10:03:49 +08:00
*
*
* This program is free software; you can redistribute it and/or modify
* 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.
*
* 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.
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* $Id$
2001-08-02 10:40:48 +08:00
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
2001-08-02 10:40:48 +08:00
#include <stdio.h>
2004-03-03 10:03:49 +08:00
#include <errno.h>
#include <fcntl.h>
2001-08-02 10:40:48 +08:00
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
#include <getopt.h>
2004-03-03 10:03:49 +08:00
#include <pwd.h>
2001-08-02 10:40:48 +08:00
#include <sys/types.h>
#include <sys/socket.h>
2004-03-03 10:03:49 +08:00
#include <sys/ioctl.h>
#include <sys/uio.h>
2003-07-02 07:45:20 +08:00
#include <sys/stat.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
2001-08-02 10:40:48 +08:00
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
2002-03-20 02:19:34 +08:00
#include <bluetooth/hci_lib.h>
2001-08-02 10:40:48 +08:00
2004-02-29 09:14:47 +08:00
#include "parser.h"
#include "sdp.h"
#define SNAP_LEN HCI_MAX_FRAME_SIZE
#define DEFAULT_PORT 10839;
2004-03-03 10:03:49 +08:00
/* Modes */
enum {
PARSE,
READ,
WRITE,
RECEIVE,
SEND
2004-03-03 10:03:49 +08:00
};
2001-08-02 10:40:48 +08:00
/* Default options */
2001-08-17 08:00:02 +08:00
static int device;
static int snap_len = SNAP_LEN;
2002-04-23 05:49:02 +08:00
static int defpsm = 0;
2004-08-27 07:59:20 +08:00
static int defcompid = DEFAULT_COMPID;
static int mode = PARSE;
2002-02-04 14:15:35 +08:00
static long flags;
static long filter;
2001-08-17 08:00:02 +08:00
static char *dump_file;
static in_addr_t dump_addr = INADDR_LOOPBACK;
static in_port_t dump_port = DEFAULT_PORT;
2001-08-02 10:40:48 +08:00
2004-03-03 10:03:49 +08:00
struct dump_hdr {
uint16_t len;
uint8_t in;
uint8_t pad;
uint32_t ts_sec;
uint32_t ts_usec;
} __attribute__ ((packed));
#define DUMP_HDR_SIZE (sizeof(struct dump_hdr))
static inline int read_n(int fd, char *buf, int len)
{
register int t = 0, w;
while (len > 0) {
if ((w = read(fd, buf, len)) < 0) {
2004-06-16 17:57:43 +08:00
if (errno == EINTR || errno == EAGAIN)
2004-03-03 10:03:49 +08:00
continue;
return -1;
}
if (!w)
return 0;
len -= w; buf += w; t += w;
}
return t;
}
static inline int write_n(int fd, char *buf, int len)
{
register int t = 0, w;
while (len > 0) {
if ((w = write(fd, buf, len)) < 0) {
2004-06-16 17:57:43 +08:00
if (errno == EINTR || errno == EAGAIN)
2004-03-03 10:03:49 +08:00
continue;
return -1;
}
if (!w)
return 0;
len -= w; buf += w; t += w;
}
return t;
}
2001-08-17 08:00:02 +08:00
static void process_frames(int dev, int sock, int file)
{
struct cmsghdr *cmsg;
struct msghdr msg;
struct iovec iv;
2001-08-16 11:46:14 +08:00
struct dump_hdr *dh;
2001-08-16 12:56:12 +08:00
struct frame frm;
char *buf, *ctrl;
2001-08-12 10:36:41 +08:00
if (snap_len < SNAP_LEN)
snap_len = SNAP_LEN;
if (!(buf = malloc(snap_len + DUMP_HDR_SIZE))) {
2001-08-12 06:19:59 +08:00
perror("Can't allocate data buffer");
exit(1);
}
2004-02-29 09:14:47 +08:00
dh = (void *) buf;
2001-08-16 12:56:12 +08:00
frm.data = buf + DUMP_HDR_SIZE;
2004-02-29 09:14:47 +08:00
2001-08-12 06:19:59 +08:00
if (!(ctrl = malloc(100))) {
perror("Can't allocate control buffer");
exit(1);
}
2004-02-29 09:14:47 +08:00
printf("device: hci%d snap_len: %d filter: 0x%lx\n",
dev, snap_len, filter);
memset(&msg, 0, sizeof(msg));
while (1) {
2001-08-16 12:56:12 +08:00
iv.iov_base = frm.data;
iv.iov_len = snap_len;
msg.msg_iov = &iv;
msg.msg_iovlen = 1;
msg.msg_control = ctrl;
2001-08-12 06:19:59 +08:00
msg.msg_controllen = 100;
2001-08-16 12:56:12 +08:00
if ((frm.data_len = recvmsg(sock, &msg, 0)) < 0) {
perror("Receive failed");
exit(1);
}
/* Process control message */
2001-08-16 12:56:12 +08:00
frm.in = 0;
cmsg = CMSG_FIRSTHDR(&msg);
2001-08-15 13:39:50 +08:00
while (cmsg) {
switch (cmsg->cmsg_type) {
case HCI_CMSG_DIR:
2001-08-16 12:56:12 +08:00
frm.in = *((int *)CMSG_DATA(cmsg));
2001-08-15 13:39:50 +08:00
break;
2002-02-04 14:15:35 +08:00
case HCI_CMSG_TSTAMP:
frm.ts = *((struct timeval *)CMSG_DATA(cmsg));
break;
}
cmsg = CMSG_NXTHDR(&msg, cmsg);
}
frm.ptr = frm.data;
frm.len = frm.data_len;
switch (mode) {
case WRITE:
case SEND:
/* Save or send dump */
dh->len = htobs(frm.data_len);
2001-08-16 12:56:12 +08:00
dh->in = frm.in;
dh->ts_sec = htobl(frm.ts.tv_sec);
dh->ts_usec = htobl(frm.ts.tv_usec);
2001-08-16 12:56:12 +08:00
if (write_n(file, buf, frm.data_len + DUMP_HDR_SIZE) < 0) {
2001-08-16 11:46:14 +08:00
perror("Write error");
exit(1);
}
break;
default:
/* Parse and print */
parse(&frm);
break;
2001-08-16 11:46:14 +08:00
}
}
}
2001-08-17 08:00:02 +08:00
static void read_dump(int file)
2001-08-16 11:46:14 +08:00
{
struct dump_hdr dh;
2001-08-16 12:56:12 +08:00
struct frame frm;
2001-08-16 11:46:14 +08:00
int err;
2001-08-16 12:56:12 +08:00
if (!(frm.data = malloc(HCI_MAX_FRAME_SIZE))) {
2001-08-16 11:46:14 +08:00
perror("Can't allocate data buffer");
exit(1);
}
2004-02-29 09:14:47 +08:00
2001-08-16 11:46:14 +08:00
while (1) {
if ((err = read_n(file, (void *) &dh, DUMP_HDR_SIZE)) < 0)
goto failed;
if (!err) return;
2004-02-29 09:14:47 +08:00
frm.data_len = btohs(dh.len);
2001-08-16 12:56:12 +08:00
if ((err = read_n(file, frm.data, frm.data_len)) < 0)
2001-08-16 11:46:14 +08:00
goto failed;
if (!err) return;
2001-08-16 12:56:12 +08:00
frm.ptr = frm.data;
frm.len = frm.data_len;
frm.in = dh.in;
frm.ts.tv_sec = btohl(dh.ts_sec);
frm.ts.tv_usec = btohl(dh.ts_usec);
2004-02-29 09:14:47 +08:00
parse(&frm);
}
2001-08-16 11:46:14 +08:00
failed:
perror("Read failed");
exit(1);
}
2001-08-17 08:00:02 +08:00
static int open_file(char *file, int mode)
2001-08-02 10:40:48 +08:00
{
2001-08-16 11:46:14 +08:00
int f, flags;
2001-08-02 10:40:48 +08:00
if (mode == WRITE)
2001-08-16 11:46:14 +08:00
flags = O_WRONLY | O_CREAT | O_APPEND;
else
flags = O_RDONLY;
2001-08-02 10:40:48 +08:00
2003-07-02 07:45:20 +08:00
if ((f = open(file, flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
perror("Can't open output file");
exit(1);
2001-08-02 10:40:48 +08:00
}
return f;
}
static int open_socket(int dev, unsigned long flags)
{
struct sockaddr_hci addr;
struct hci_filter flt;
2004-06-16 17:57:43 +08:00
int sk, opt;
2001-08-02 10:40:48 +08:00
/* Create HCI socket */
2004-06-16 17:57:43 +08:00
if ((sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)) < 0) {
2001-08-02 10:40:48 +08:00
perror("Can't create HCI socket");
exit(1);
}
2004-02-29 09:14:47 +08:00
2001-08-02 10:40:48 +08:00
opt = 1;
2004-06-16 17:57:43 +08:00
if (setsockopt(sk, SOL_HCI, HCI_DATA_DIR, &opt, sizeof(opt)) < 0) {
2001-08-02 10:40:48 +08:00
perror("Can't enable data direction info");
exit(1);
}
2002-02-04 14:15:35 +08:00
opt = 1;
2004-06-16 17:57:43 +08:00
if (setsockopt(sk, SOL_HCI, HCI_TIME_STAMP, &opt, sizeof(opt)) < 0) {
2002-02-04 14:15:35 +08:00
perror("Can't enable time stamp");
exit(1);
}
/* Setup filter */
2002-03-20 02:19:34 +08:00
hci_filter_clear(&flt);
2005-03-13 23:13:36 +08:00
hci_filter_all_ptypes(&flt);
hci_filter_all_events(&flt);
2004-06-16 17:57:43 +08:00
if (setsockopt(sk, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
perror("Can't set HCI filter");
exit(1);
}
2001-08-02 10:40:48 +08:00
/* Bind socket to the HCI device */
addr.hci_family = AF_BLUETOOTH;
addr.hci_dev = dev;
2004-06-16 17:57:43 +08:00
if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
printf("Can't attach to device hci%d. %s(%d)\n",
dev, strerror(errno), errno);
2001-08-02 10:40:48 +08:00
exit(1);
}
2004-06-16 17:57:43 +08:00
return sk;
}
2001-08-02 10:40:48 +08:00
static int open_connection(in_addr_t addr, in_port_t port)
{
struct sockaddr_in sa;
int sk, opt;
if ((sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("Can't create inet socket");
exit(1);
}
opt = 1;
setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(INADDR_ANY);
sa.sin_port = htons(0);
if (bind(sk, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
perror("Can't bind inet socket");
close(sk);
exit(1);
}
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(addr);
sa.sin_port = htons(port);
if (connect(sk, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
perror("Can't connect inet socket");
close(sk);
exit(1);
}
return sk;
}
static int wait_connection(in_addr_t addr, in_port_t port)
{
struct sockaddr_in sa;
struct hostent *host;
int sk, nsk, opt, len;
if ((sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("Can't create inet socket");
exit(1);
}
opt = 1;
setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(addr);
sa.sin_port = htons(port);
if (bind(sk, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
perror("Can't bind inet socket");
close(sk);
exit(1);
}
host = gethostbyaddr(&sa.sin_addr, sizeof(sa.sin_addr), AF_INET);
printf("device: %s:%d snap_len: %d filter: 0x%lx\n",
host ? host->h_name : inet_ntoa(sa.sin_addr),
ntohs(sa.sin_port), snap_len, filter);
if (listen(sk, 1)) {
perror("Can't listen on inet socket");
close(sk);
exit(1);
}
len = sizeof(sa);
if ((nsk = accept(sk, (struct sockaddr *) &sa, &len)) < 0) {
perror("Can't accept new inet socket");
close(sk);
exit(1);
}
host = gethostbyaddr(&sa.sin_addr, sizeof(sa.sin_addr), AF_INET);
printf("device: %s snap_len: %d filter: 0x%lx\n",
host ? host->h_name : inet_ntoa(sa.sin_addr), snap_len, filter);
close(sk);
return nsk;
}
static struct {
char *name;
int flag;
} filters[] = {
{ "lmp", FILT_LMP },
2004-02-29 09:14:47 +08:00
{ "hci", FILT_HCI },
{ "sco", FILT_SCO },
{ "l2cap", FILT_L2CAP },
{ "rfcomm", FILT_RFCOMM },
{ "sdp", FILT_SDP },
{ "bnep", FILT_BNEP },
{ "cmtp", FILT_CMTP },
{ "hidp", FILT_HIDP },
{ "hcrp", FILT_HCRP },
{ "avdtp", FILT_AVDTP },
{ "obex", FILT_OBEX },
{ "capi", FILT_CAPI },
2005-03-13 23:13:36 +08:00
{ "csr", FILT_CSR },
{ "dga", FILT_DGA },
{ 0 }
};
static void parse_filter(int argc, char **argv)
{
int i,n;
2004-02-29 09:14:47 +08:00
for (i = 0; i < argc; i++) {
for (n = 0; filters[n].name; n++) {
if (!strcasecmp(filters[n].name, argv[i])) {
filter |= filters[n].flag;
break;
}
}
}
}
static void usage(void)
{
printf(
"Usage: hcidump [OPTION...] [filter]\n"
" -i, --device=hci_dev HCI device\n"
" -l, --snap-len=len Snap len (in bytes)\n"
2004-02-29 09:14:47 +08:00
" -p, --psm=psm Default PSM\n"
2004-08-27 07:59:20 +08:00
" -m, --manufacturer=compid Default manufacturer\n"
" -w, --save-dump=file Save dump to a file\n"
2004-02-29 09:14:47 +08:00
" -r, --read-dump=file Read dump from a file\n"
" -s, --send-dump=host Send dump to a host\n"
" -n, --recv-dump=host Receive dump on a host\n"
2004-02-29 09:14:47 +08:00
" -t, --ts Display time stamps\n"
" -a, --ascii Dump data in ascii\n"
" -x, --hex Dump data in hex\n"
" -X, --ext Dump data in hex and ascii\n"
2005-03-13 23:13:36 +08:00
" -R, --raw Dump raw data\n"
2004-02-29 09:14:47 +08:00
" -C, --cmtp=psm PSM for CMTP\n"
" -H, --hcrp=psm PSM for HCRP\n"
" -O, --obex=channel Channel for OBEX\n"
" -V, --verbose Verbose decoding\n"
" -h, --help Give this help list\n"
" --usage Give a short usage message\n"
);
}
static struct option main_options[] = {
2004-08-27 07:59:20 +08:00
{ "device", 1, 0, 'i' },
{ "snap-len", 1, 0, 'l' },
2004-08-27 07:59:20 +08:00
{ "psm", 1, 0, 'p' },
{ "manufacturer", 1, 0, 'm' },
{ "save-dump", 1, 0, 'w' },
{ "read-dump", 1, 0, 'r' },
{ "send-dump", 1, 0, 's' },
{ "recv-dump", 1, 0, 'n' },
{ "timestamp", 0, 0, 't' },
2004-08-27 07:59:20 +08:00
{ "ascii", 0, 0, 'a' },
{ "hex", 0, 0, 'x' },
{ "ext", 0, 0, 'X' },
{ "raw", 0, 0, 'R' },
{ "cmtp", 1, 0, 'C' },
{ "hcrp", 1, 0, 'H' },
{ "obex", 1, 0, 'O' },
{ "verbose", 0, 0, 'V' },
2004-08-27 07:59:20 +08:00
{ "help", 0, 0, 'h' },
{ 0 }
};
int main(int argc, char *argv[])
{
struct hostent *host;
struct in_addr addr;
int opt;
printf("HCI sniffer - Bluetooth packet analyzer ver %s\n", VERSION);
2005-03-13 23:13:36 +08:00
while ((opt=getopt_long(argc, argv, "i:l:p:m:w:r:s:n:taxXRC:H:O:Vh", main_options, NULL)) != -1) {
2004-02-29 09:14:47 +08:00
switch(opt) {
case 'i':
2004-06-16 17:57:43 +08:00
device = atoi(optarg + 3);
break;
case 'l':
snap_len = atoi(optarg);
break;
2002-04-23 05:49:02 +08:00
case 'p':
defpsm = atoi(optarg);
2002-04-23 05:49:02 +08:00
break;
2004-08-27 07:59:20 +08:00
case 'm':
defcompid = atoi(optarg);
break;
2004-02-29 09:14:47 +08:00
case 'w':
mode = WRITE;
dump_file = strdup(optarg);
break;
case 'r':
mode = READ;
dump_file = strdup(optarg);
break;
case 's':
mode = SEND;
host = gethostbyname(optarg);
if (host) {
bcopy(host->h_addr, &addr, sizeof(struct in_addr));
dump_addr = ntohl(addr.s_addr);
dump_port = DEFAULT_PORT;
} else {
dump_addr = INADDR_LOOPBACK;
dump_port = DEFAULT_PORT;
}
break;
case 'n':
mode = RECEIVE;
host = gethostbyname(optarg);
if (host) {
bcopy(host->h_addr, &addr, sizeof(struct in_addr));
dump_addr = ntohl(addr.s_addr);
dump_port = DEFAULT_PORT;
} else {
dump_addr = INADDR_LOOPBACK;
dump_port = DEFAULT_PORT;
}
break;
2002-02-04 14:15:35 +08:00
case 't':
flags |= DUMP_TSTAMP;
break;
case 'a':
flags |= DUMP_ASCII;
break;
2004-02-29 09:14:47 +08:00
case 'x':
flags |= DUMP_HEX;
break;
case 'X':
flags |= DUMP_EXT;
break;
2001-08-02 10:40:48 +08:00
2004-02-29 09:14:47 +08:00
case 'R':
flags |= DUMP_RAW;
2001-08-17 08:00:02 +08:00
break;
2004-02-29 09:14:47 +08:00
case 'C':
set_proto(0, atoi(optarg), 0, SDP_UUID_CMTP);
2004-02-29 09:14:47 +08:00
break;
case 'H':
set_proto(0, atoi(optarg), 0, SDP_UUID_HARDCOPY_CONTROL_CHANNEL);
break;
case 'O':
set_proto(0, 0, atoi(optarg), SDP_UUID_OBEX);
break;
case 'V':
flags |= DUMP_VERBOSE;
break;
2004-02-29 09:14:47 +08:00
case 'h':
default:
usage();
exit(0);
}
}
2004-02-29 09:14:47 +08:00
argc -= optind;
argv += optind;
optind = 0;
if (argc > 0)
parse_filter(argc, argv);
2001-08-02 10:40:48 +08:00
/* Default settings */
if (!filter)
filter = ~0L;
switch (mode) {
case PARSE:
2004-08-27 07:59:20 +08:00
init_parser(flags, filter, defpsm, defcompid);
process_frames(device, open_socket(device, flags), -1);
break;
case READ:
init_parser(flags, filter, defpsm, defcompid);
read_dump(open_file(dump_file, mode));
break;
case WRITE:
process_frames(device, open_socket(device, flags), open_file(dump_file, mode));
break;
case RECEIVE:
2004-08-27 07:59:20 +08:00
init_parser(flags, filter, defpsm, defcompid);
read_dump(wait_connection(dump_addr, dump_port));
break;
case SEND:
process_frames(device, open_socket(device, flags), open_connection(dump_addr, dump_port));
break;
}
2004-02-29 09:14:47 +08:00
return 0;
2001-08-02 10:40:48 +08:00
}