2001-08-02 10:40:48 +08:00
|
|
|
/*
|
|
|
|
HCIDump - HCI packet analyzer
|
|
|
|
Copyright (C) 2000-2001 Maxim Krasnyansky <maxk@qualcomm.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License version 2 as
|
|
|
|
published by the Free Software Foundation;
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
|
|
|
|
IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY CLAIM,
|
|
|
|
OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER
|
|
|
|
RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
|
|
|
|
USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, COPYRIGHTS,
|
|
|
|
TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS SOFTWARE IS DISCLAIMED.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2002-05-24 07:13:30 +08:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <getopt.h>
|
2001-08-02 10:40:48 +08:00
|
|
|
|
|
|
|
#include <asm/types.h>
|
|
|
|
|
|
|
|
#include <bluetooth/bluetooth.h>
|
|
|
|
#include <bluetooth/hci.h>
|
2001-08-03 13:26:20 +08:00
|
|
|
#include <bluetooth/l2cap.h>
|
2002-03-20 02:19:34 +08:00
|
|
|
#include <bluetooth/hci_lib.h>
|
2001-08-02 10:40:48 +08:00
|
|
|
|
2001-08-12 06:19:59 +08:00
|
|
|
#include "parser.h"
|
2001-08-16 11:46:14 +08:00
|
|
|
#include "hcidump.h"
|
2001-08-16 08:45:16 +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;
|
2001-09-15 15:20:39 +08:00
|
|
|
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;
|
2001-08-02 10:40:48 +08:00
|
|
|
|
2001-08-17 08:00:02 +08:00
|
|
|
static void process_frames(int dev, int sock, int file)
|
2001-08-06 00:57:21 +08:00
|
|
|
{
|
|
|
|
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-06 00:57:21 +08:00
|
|
|
|
2001-08-12 10:36:41 +08:00
|
|
|
if (snap_len < SNAP_LEN)
|
|
|
|
snap_len = SNAP_LEN;
|
2001-08-06 00:57:21 +08:00
|
|
|
|
2001-08-16 12:29:28 +08:00
|
|
|
if (!(buf = malloc(snap_len + DUMP_HDR_SIZE))) {
|
2001-08-12 06:19:59 +08:00
|
|
|
perror("Can't allocate data buffer");
|
|
|
|
exit(1);
|
|
|
|
}
|
2001-08-16 12:29:28 +08:00
|
|
|
dh = (void *) buf;
|
2001-08-16 12:56:12 +08:00
|
|
|
frm.data = buf + DUMP_HDR_SIZE;
|
2001-08-16 11:46:14 +08:00
|
|
|
|
2001-08-12 06:19:59 +08:00
|
|
|
if (!(ctrl = malloc(100))) {
|
|
|
|
perror("Can't allocate control buffer");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2001-11-07 05:20:02 +08:00
|
|
|
printf("device: hci%d snap_len: %d filter: 0x%lx\n",
|
|
|
|
dev, snap_len, filter);
|
2001-08-06 00:57:21 +08:00
|
|
|
|
|
|
|
while (1) {
|
2001-08-16 12:56:12 +08:00
|
|
|
iv.iov_base = frm.data;
|
2001-08-06 00:57:21 +08:00
|
|
|
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-06 00:57:21 +08:00
|
|
|
|
2001-08-16 12:56:12 +08:00
|
|
|
if ((frm.data_len = recvmsg(sock, &msg, 0)) < 0) {
|
2001-08-06 00:57:21 +08:00
|
|
|
perror("Receive failed");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process control message */
|
2001-08-16 12:56:12 +08:00
|
|
|
frm.in = 0;
|
2001-08-06 00:57:21 +08:00
|
|
|
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;
|
2001-08-06 00:57:21 +08:00
|
|
|
}
|
|
|
|
cmsg = CMSG_NXTHDR(&msg, cmsg);
|
|
|
|
}
|
|
|
|
|
2001-08-21 08:41:59 +08:00
|
|
|
frm.ptr = frm.data;
|
|
|
|
frm.len = frm.data_len;
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case WRITE:
|
2001-08-16 11:46:14 +08:00
|
|
|
/* Save dump */
|
2002-03-13 02:47:34 +08:00
|
|
|
dh->len = htobs(frm.data_len);
|
2001-08-16 12:56:12 +08:00
|
|
|
dh->in = frm.in;
|
2002-03-13 02:47:34 +08:00
|
|
|
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);
|
|
|
|
}
|
2001-08-21 08:41:59 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if ((err = read_n(file, (void *) &dh, DUMP_HDR_SIZE)) < 0)
|
|
|
|
goto failed;
|
|
|
|
if (!err) return;
|
|
|
|
|
2002-03-13 02:47:34 +08:00
|
|
|
frm.data_len = btohs(dh.len);
|
2001-08-06 00:57:21 +08:00
|
|
|
|
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-06 00:57:21 +08:00
|
|
|
|
2001-08-16 12:56:12 +08:00
|
|
|
frm.ptr = frm.data;
|
|
|
|
frm.len = frm.data_len;
|
|
|
|
frm.in = dh.in;
|
2002-03-13 02:47:34 +08:00
|
|
|
frm.ts.tv_sec = btohl(dh.ts_sec);
|
|
|
|
frm.ts.tv_usec = btohl(dh.ts_usec);
|
2001-08-16 12:29:28 +08:00
|
|
|
|
|
|
|
parse(&frm);
|
2001-08-06 00:57:21 +08:00
|
|
|
}
|
2001-08-16 11:46:14 +08:00
|
|
|
|
|
|
|
failed:
|
|
|
|
perror("Read failed");
|
|
|
|
exit(1);
|
2001-08-06 00:57:21 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2001-08-16 08:45:16 +08:00
|
|
|
if (mode == WRITE)
|
2001-08-16 11:46:14 +08:00
|
|
|
flags = O_WRONLY | O_CREAT | O_APPEND;
|
2001-08-16 08:45:16 +08:00
|
|
|
else
|
|
|
|
flags = O_RDONLY;
|
2001-08-02 10:40:48 +08:00
|
|
|
|
2001-08-16 11:46:14 +08:00
|
|
|
if ((f = open(file, flags)) < 0) {
|
2001-08-16 08:45:16 +08:00
|
|
|
perror("Can't open output file");
|
|
|
|
exit(1);
|
2001-08-02 10:40:48 +08:00
|
|
|
}
|
2001-08-16 08:45:16 +08:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2001-08-17 08:00:02 +08:00
|
|
|
static int open_socket(int dev)
|
2001-08-16 08:45:16 +08:00
|
|
|
{
|
|
|
|
struct sockaddr_hci addr;
|
|
|
|
struct hci_filter flt;
|
|
|
|
int s, opt;
|
2001-08-02 10:40:48 +08:00
|
|
|
|
|
|
|
/* Create HCI socket */
|
2001-08-15 13:39:50 +08:00
|
|
|
if ((s=socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)) < 0) {
|
2001-08-02 10:40:48 +08:00
|
|
|
perror("Can't create HCI socket");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
opt = 1;
|
2001-08-15 13:39:50 +08:00
|
|
|
if (setsockopt(s, 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;
|
|
|
|
if (setsockopt(s, SOL_HCI, HCI_TIME_STAMP, &opt, sizeof(opt)) < 0) {
|
|
|
|
perror("Can't enable time stamp");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2001-08-05 14:04:37 +08:00
|
|
|
/* Setup filter */
|
2002-03-20 02:19:34 +08:00
|
|
|
hci_filter_clear(&flt);
|
|
|
|
hci_filter_all_ptypes(&flt);
|
|
|
|
hci_filter_all_events(&flt);
|
2001-08-05 14:04:37 +08:00
|
|
|
if (setsockopt(s, 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;
|
2001-08-15 13:39:50 +08:00
|
|
|
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
2001-08-06 00:57:21 +08:00
|
|
|
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);
|
|
|
|
}
|
2001-08-16 08:45:16 +08:00
|
|
|
return s;
|
|
|
|
}
|
2001-08-02 10:40:48 +08:00
|
|
|
|
2001-09-15 15:20:39 +08:00
|
|
|
static struct {
|
|
|
|
char *name;
|
|
|
|
int flag;
|
|
|
|
} filters[] = {
|
|
|
|
{ "hci", FILT_HCI },
|
|
|
|
{ "l2cap", FILT_L2CAP },
|
2001-12-22 01:35:48 +08:00
|
|
|
{ "sco", FILT_SCO },
|
2001-09-15 15:20:39 +08:00
|
|
|
{ "rfcomm", FILT_RFCOMM },
|
|
|
|
{ "sdp", FILT_SDP },
|
2002-03-13 02:47:34 +08:00
|
|
|
{ "bnep", FILT_BNEP },
|
2001-09-15 15:20:39 +08:00
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
2002-05-24 07:13:30 +08:00
|
|
|
static void parse_filter(int argc, char **argv)
|
2001-09-15 15:20:39 +08:00
|
|
|
{
|
|
|
|
int i,n;
|
|
|
|
|
2002-05-24 07:13:30 +08:00
|
|
|
for (i=0; i<argc; i++) {
|
2001-09-15 15:20:39 +08:00
|
|
|
for (n=0; filters[n].name; n++) {
|
2002-05-24 07:13:30 +08:00
|
|
|
if (!strcmp(filters[n].name, argv[i])) {
|
2001-09-15 15:20:39 +08:00
|
|
|
filter |= filters[n].flag;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-24 07:13:30 +08:00
|
|
|
static void usage(void)
|
|
|
|
{
|
|
|
|
printf(
|
|
|
|
"Usage: hcidump [OPTION...] [filter]\n"
|
|
|
|
" -i, --device=hci_dev HCI device\n"
|
|
|
|
" -p, --psm=psm Default PSM\n"
|
|
|
|
" -s, --snap-len=len Snap len (in bytes)\n"
|
|
|
|
" -r, --read-dump=file Read dump from a file\n"
|
|
|
|
" -w, --save-dump=file Save dump to a file\n"
|
|
|
|
" -a, --ascii Dump data in ascii\n"
|
|
|
|
" -x, --hex Dump data in hex\n"
|
|
|
|
" -R, --raw Raw mode\n"
|
|
|
|
" -t, --ts Display time stamps\n"
|
|
|
|
" -?, --help Give this help list\n"
|
|
|
|
" --usage Give a short usage message\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct option main_options[] = {
|
|
|
|
{"device", 1,0, 'i' },
|
|
|
|
{"snap-len", 1,0, 's' },
|
|
|
|
{"psm", 1,0, 'p' },
|
|
|
|
{"save-dump", 1,0, 'w' },
|
|
|
|
{"read-dump", 1,0, 'r' },
|
|
|
|
{"ts", 0,0, 't' },
|
|
|
|
{"hex", 0,0, 'x' },
|
|
|
|
{"ascii", 0,0, 'a' },
|
|
|
|
{"raw", 0,0, 'R' },
|
|
|
|
{"help", 0,0, 'h' },
|
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
2001-08-16 08:45:16 +08:00
|
|
|
{
|
2002-05-24 07:13:30 +08:00
|
|
|
int opt;
|
|
|
|
|
|
|
|
printf("HCIDump - HCI packet analyzer ver %s\n", VERSION);
|
|
|
|
|
2002-06-29 01:32:32 +08:00
|
|
|
while ((opt=getopt_long(argc, argv, "i:s:p:r:w:xathR", main_options, NULL)) != -1) {
|
2002-05-24 07:13:30 +08:00
|
|
|
switch(opt) {
|
2001-08-16 08:45:16 +08:00
|
|
|
case 'i':
|
2002-05-24 07:13:30 +08:00
|
|
|
device = atoi(optarg+3);
|
2001-08-16 08:45:16 +08:00
|
|
|
break;
|
|
|
|
|
2001-12-22 01:35:48 +08:00
|
|
|
case 'x':
|
2001-08-16 08:45:16 +08:00
|
|
|
flags |= DUMP_HEX;
|
|
|
|
break;
|
|
|
|
|
2001-08-17 08:00:02 +08:00
|
|
|
case 'a':
|
2001-08-16 08:45:16 +08:00
|
|
|
flags |= DUMP_ASCII;
|
|
|
|
break;
|
|
|
|
|
2001-08-17 08:00:02 +08:00
|
|
|
case 's':
|
2002-05-24 07:13:30 +08:00
|
|
|
snap_len = atoi(optarg);
|
2001-08-16 08:45:16 +08:00
|
|
|
break;
|
|
|
|
|
2002-04-23 05:49:02 +08:00
|
|
|
case 'p':
|
2002-05-24 07:13:30 +08:00
|
|
|
defpsm = atoi(optarg);
|
2002-04-23 05:49:02 +08:00
|
|
|
break;
|
|
|
|
|
2002-02-04 14:15:35 +08:00
|
|
|
case 't':
|
|
|
|
flags |= DUMP_TSTAMP;
|
|
|
|
break;
|
|
|
|
|
2001-08-21 08:41:59 +08:00
|
|
|
case 'R':
|
2002-02-07 12:47:36 +08:00
|
|
|
flags |= DUMP_RAW;
|
2001-08-21 08:41:59 +08:00
|
|
|
break;
|
|
|
|
|
2001-08-16 08:45:16 +08:00
|
|
|
case 'r':
|
|
|
|
mode = READ;
|
2002-05-24 07:13:30 +08:00
|
|
|
dump_file = strdup(optarg);
|
2001-08-16 08:45:16 +08:00
|
|
|
break;
|
2001-08-02 10:40:48 +08:00
|
|
|
|
2001-08-17 08:00:02 +08:00
|
|
|
case 'w':
|
|
|
|
mode = WRITE;
|
2002-05-24 07:13:30 +08:00
|
|
|
dump_file = strdup(optarg);
|
2001-08-17 08:00:02 +08:00
|
|
|
break;
|
2001-08-21 08:41:59 +08:00
|
|
|
|
2002-05-24 07:13:30 +08:00
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
2001-09-15 15:20:39 +08:00
|
|
|
|
2002-05-24 07:13:30 +08:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
optind = 0;
|
2001-08-16 08:45:16 +08:00
|
|
|
|
2002-05-24 07:13:30 +08:00
|
|
|
|
|
|
|
if (argc > 0)
|
|
|
|
parse_filter(argc, argv);
|
2001-08-02 10:40:48 +08:00
|
|
|
|
2001-09-15 15:20:39 +08:00
|
|
|
/* Default settings */
|
|
|
|
if (!filter)
|
|
|
|
filter = ~0L;
|
|
|
|
|
2001-08-16 08:45:16 +08:00
|
|
|
switch (mode) {
|
2001-08-21 08:41:59 +08:00
|
|
|
case PARSE:
|
2002-04-23 05:49:02 +08:00
|
|
|
init_parser(flags, filter, defpsm);
|
2001-08-17 08:00:02 +08:00
|
|
|
process_frames(device, open_socket(device), -1);
|
2001-08-16 08:45:16 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WRITE:
|
2001-08-17 08:00:02 +08:00
|
|
|
process_frames(device, open_socket(device), open_file(dump_file, mode));
|
2001-08-16 08:45:16 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case READ:
|
2002-04-23 05:49:02 +08:00
|
|
|
init_parser(flags, filter, defpsm);
|
2001-08-17 08:00:02 +08:00
|
|
|
read_dump(open_file(dump_file, mode));
|
2001-08-16 08:45:16 +08:00
|
|
|
break;
|
|
|
|
}
|
2001-08-06 00:57:21 +08:00
|
|
|
return 0;
|
2001-08-02 10:40:48 +08:00
|
|
|
}
|